You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by up...@apache.org on 2016/02/20 00:59:19 UTC

[01/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Repository: incubator-geode
Updated Branches:
  refs/heads/feature/GEODE-917 8780b5c4e -> 5beaaedce


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorDUnitTest.java
index 545a0ea,0000000..b3f627a
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorDUnitTest.java
@@@ -1,1910 -1,0 +1,1913 @@@
 +/*
 + * 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.distributed;
 +
 +import java.io.File;
 +import java.io.FileReader;
 +import java.io.IOException;
 +import java.io.LineNumberReader;
 +import java.util.ArrayList;
 +import java.util.List;
 +import java.util.Properties;
 +import java.util.Set;
 +
 +import com.gemstone.gemfire.ForcedDisconnectException;
 +import com.gemstone.gemfire.GemFireConfigException;
 +import com.gemstone.gemfire.LogWriter;
 +import com.gemstone.gemfire.SystemConnectException;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheFactory;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionShortcut;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 +import com.gemstone.gemfire.distributed.internal.DistributionException;
 +import com.gemstone.gemfire.distributed.internal.DistributionManager;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.distributed.internal.InternalLocator;
 +import com.gemstone.gemfire.distributed.internal.MembershipListener;
 +import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
 +import com.gemstone.gemfire.distributed.internal.membership.MembershipManager;
 +import com.gemstone.gemfire.distributed.internal.membership.MembershipTestHook;
 +import com.gemstone.gemfire.distributed.internal.membership.NetView;
 +import com.gemstone.gemfire.distributed.internal.membership.gms.MembershipManagerHelper;
 +import com.gemstone.gemfire.internal.Assert;
 +import com.gemstone.gemfire.internal.AvailablePort;
 +import com.gemstone.gemfire.internal.AvailablePortHelper;
 +import com.gemstone.gemfire.internal.logging.InternalLogWriter;
 +import com.gemstone.gemfire.internal.logging.LocalLogWriter;
 +import com.gemstone.gemfire.internal.tcp.Connection;
 +import com.gemstone.gemfire.test.dunit.AsyncInvocation;
 +import com.gemstone.gemfire.test.dunit.DistributedTestCase;
 +import com.gemstone.gemfire.test.dunit.DistributedTestUtils;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.IgnoredException;
 +import com.gemstone.gemfire.test.dunit.LogWriterUtils;
 +import com.gemstone.gemfire.test.dunit.NetworkUtils;
 +import com.gemstone.gemfire.test.dunit.SerializableCallable;
 +import com.gemstone.gemfire.test.dunit.SerializableRunnable;
 +import com.gemstone.gemfire.test.dunit.ThreadUtils;
 +import com.gemstone.gemfire.test.dunit.VM;
 +import com.gemstone.gemfire.test.dunit.Wait;
 +import com.gemstone.gemfire.test.dunit.WaitCriterion;
 +
 +/**
 + * Tests the ability of the {@link Locator} API to start and stop
 + * locators running in remote VMs.
 + *
 + * @since 4.0
 + */
 +public class LocatorDUnitTest extends DistributedTestCase {
 +
 +  static TestHook hook;
 +  
 +  /**
 +   * Creates a new <code>LocatorDUnitTest</code>
 +   */
 +  public LocatorDUnitTest(String name) {
 +    super(name);
 +  }
 +
 +  private static final String WAIT2_MS_NAME = "LocatorDUnitTest.WAIT2_MS";
 +  private static final int WAIT2_MS_DEFAULT = 5000; // 2000 -- see bug 36470
 +  private static final int WAIT2_MS 
 +      = Integer.getInteger(WAIT2_MS_NAME, WAIT2_MS_DEFAULT).intValue();
 +  
 +  private int port1;
 +  private int port2;
 +  
 +  @Override
 +  public void setUp() throws Exception {
 +    super.setUp();
 +    port1 = -1;
 +    port2 = -1;
 +    IgnoredException.addIgnoredException("Removing shunned member");
 +  }
 +  
 +  @Override
 +  protected final void preTearDown() throws Exception {
 +    if (Locator.hasLocator()) {
 +      Locator.getLocator().stop();
 +    }
 +    // delete locator state files so they don't accidentally
 +    // get used by other tests
 +    if (port1 > 0) {
 +      DistributedTestUtils.deleteLocatorStateFile(port1);
 +    }
 +    if (port2 > 0) {
 +      DistributedTestUtils.deleteLocatorStateFile(port2);
 +    }
 +  }
 +  
 +  ////////  Test Methods
 +
 +  
 +  /**
 +   * SQLFire uses a colocated locator in a dm-type=normal VM.  This tests that
 +   * the locator can resume control as coordinator after all locators have been
 +   * shut down and one is restarted.  It's necessary to have a lock service
 +   * start so elder failover is forced to happen.  Prior to fixing how this worked
 +   * it hung with the restarted locator trying to become elder again because
 +   * it put its address at the beginning of the new view it sent out.
 +   */
 +  public void testCollocatedLocatorWithSecurity() throws Exception {
 +    disconnectAllFromDS();
 +    Host host = Host.getHost(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    VM vm3 = host.getVM(3);
 +    
 +    port1 = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    DistributedTestUtils.deleteLocatorStateFile(port1);
 +
 +    final String locators = NetworkUtils.getServerHostName(host) + "[" + port1 + "]";
 +    final Properties properties = new Properties();
 +    properties.put("mcast-port", "0");
 +    properties.put("start-locator", locators);
 +    properties.put("log-level", LogWriterUtils.getDUnitLogLevel());
 +    properties.put("security-peer-auth-init","com.gemstone.gemfire.distributed.AuthInitializer.create");
 +    properties.put("security-peer-authenticator","com.gemstone.gemfire.distributed.MyAuthenticator.create");
 +    properties.put(DistributionConfig.ENABLE_CLUSTER_CONFIGURATION_NAME, "false");
 +    properties.put(DistributionConfig.USE_CLUSTER_CONFIGURATION_NAME, "false");
 +    system = (InternalDistributedSystem)DistributedSystem.connect(properties);
 +    InternalDistributedMember mbr = system.getDistributedMember();
 +    assertEquals("expected the VM to have NORMAL vmKind",
 +        DistributionManager.NORMAL_DM_TYPE, system.getDistributedMember().getVmKind());
 +    
 +    properties.remove("start-locator");
 +    properties.put("log-level", LogWriterUtils.getDUnitLogLevel());
 +    properties.put("locators", locators);
 +    SerializableRunnable startSystem = new SerializableRunnable("start system") {
 +      public void run() {
 +        system = (InternalDistributedSystem)DistributedSystem.connect(properties);
 +      }
 +    };
 +    vm1.invoke(startSystem);
 +    vm2.invoke(startSystem);
 +
 +    // ensure that I, as a collocated locator owner, can create a cache region
 +    Cache cache = CacheFactory.create(system);
 +    Region r = cache.createRegionFactory(RegionShortcut.REPLICATE).create("test region");
 +    assertNotNull("expected to create a region", r);
 +    
 +    // create a lock service and have every vm get a lock
 +    DistributedLockService service = DistributedLockService.create("test service", system);
 +    service.becomeLockGrantor();
 +    service.lock("foo0", 0, 0);
 +    
 +    vm1.invoke(new SerializableRunnable("get the lock service and lock something") {
 +      public void run() {
 +        final DistributedLockService service = DistributedLockService.create("test service", system);
 +        service.lock("foo1", 0, 0);
 +      }
 +    });
 +
 +    vm2.invoke(new SerializableRunnable("get the lock service and lock something") {
 +      public void run() {
 +        final DistributedLockService service = DistributedLockService.create("test service", system);
 +        service.lock("foo2", 0, 0);
 +      }
 +    });
 +      
 +      
 +    // cause elder failover.  vm1 will become the lock grantor
 +    system.disconnect();
 +    
 +    try {
 +      vm1.invoke(new SerializableRunnable("ensure grantor failover") {
 +        public void run() {
 +          final DistributedLockService service = DistributedLockService.getServiceNamed("test service");
 +          service.lock("foo3", 0, 0);
 +          Wait.waitForCriterion(new WaitCriterion() {
 +            @Override
 +            public boolean done() {
 +              return service.isLockGrantor();
 +            }
 +            @Override
 +            public String description() {
 +              return "waiting to become lock grantor after shutting down locator/grantor";
 +            }
 +            
 +          }, DistributionConfig.DEFAULT_MEMBER_TIMEOUT * 2, 1000, true);
 +          assertTrue(service.isLockGrantor());
 +        }
 +      });
 +  
 +      properties.put("start-locator", locators);
 +      properties.put("log-level", LogWriterUtils.getDUnitLogLevel());
 +      system = (InternalDistributedSystem)DistributedSystem.connect(properties);
 +      System.out.println("done connecting distributed system");
 +      
 +      assertEquals("should be the coordinator", system.getDistributedMember(), MembershipManagerHelper.getCoordinator(system));
 +      NetView view = MembershipManagerHelper.getMembershipManager(system).getView();
 +      com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("view after becoming coordinator is " + view);
 +      assertNotSame("should not be the first member in the view ("+view+")", system.getDistributedMember(), view.get(0));
 +      
 +      service = DistributedLockService.create("test service", system);
 +
 +      // now force a non-elder VM to get a lock.  This will hang if the bug is not fixed 
 +      vm2.invoke(new SerializableRunnable("get the lock service and lock something") {
 +        public void run() {
 +          final DistributedLockService service = DistributedLockService.getServiceNamed("test service");
 +          service.lock("foo4", 0, 0);
 +        }
 +      });
 +
 +      assertFalse("should not have become lock grantor", service.isLockGrantor());
 +      
 +      // Now demonstrate that a new member can join and use the lock service
 +      properties.remove("start-locator");
 +      vm3.invoke(startSystem);
 +      vm3.invoke(new SerializableRunnable("get the lock service and lock something(2)") {
 +        public void run() {
 +          final DistributedLockService service = DistributedLockService.create("test service", system);
 +          service.lock("foo5", 0, 0);
 +        }
 +      });
 +      
 +    } finally {
 +      disconnectAllFromDS();
 +    }
 +  }
 +
 +  /**
 +   * Bug 30341 concerns race conditions in JGroups that allow two locators to start up in a
 +   * split-brain configuration.  To work around this we have always told customers that they
 +   * need to stagger the starting of locators.  This test configures two locators to start up
 +   * simultaneously and shows that they find each other and form a single system.
 +   * 
 +   * @throws Exception
 +   */
 +  public void testStartTwoLocators() throws Exception {
 +    disconnectAllFromDS();
 +    Host host = Host.getHost(0);
 +    VM loc1 = host.getVM(1);
 +    VM loc2 = host.getVM(2);
 +    
-     final int port1 = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
++    int ports[] = AvailablePortHelper.getRandomAvailableTCPPorts(2);
++    final int port1 = ports[0];
 +    this.port1 = port1;
-     final int port2 = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
++    final int port2 = ports[1];
 +    this.port2 = port2; // for cleanup in tearDown2
 +    DistributedTestUtils.deleteLocatorStateFile(port1);
 +    DistributedTestUtils.deleteLocatorStateFile(port2);
 +    final String host0 = NetworkUtils.getServerHostName(host); 
 +    final String locators = host0 + "[" + port1 + "]," +
 +                            host0 + "[" + port2 + "]";
 +    final Properties properties = new Properties();
 +    properties.put("mcast-port", "0");
 +    properties.put("locators", locators);
 +    properties.put("enable-network-partition-detection", "false");
 +    properties.put("disable-auto-reconnect", "true");
 +    properties.put("member-timeout", "2000");
 +    properties.put("log-level", LogWriterUtils.getDUnitLogLevel());
 +    properties.put(DistributionConfig.ENABLE_CLUSTER_CONFIGURATION_NAME, "false");
 +
 +    SerializableCallable startLocator1 = new SerializableCallable("start locator1") {
 +      @Override
 +      public Object call() throws Exception {
 +        try {
 +          System.setProperty("p2p.joinTimeout", "5000"); // set a short join timeout.  default is 17000ms
 +          Locator myLocator = Locator.startLocatorAndDS(port1, new File(""), properties);
 +        } catch (SystemConnectException e) {
 +          return Boolean.FALSE;
 +        } catch (GemFireConfigException e) {
 +          return Boolean.FALSE;
 +        } finally {
 +          System.getProperties().remove("p2p.joinTimeout");
 +        }
 +        return Boolean.TRUE;
 +      }
 +    };
 +    SerializableCallable startLocator2 = new SerializableCallable("start locator2") {
 +      @Override
 +      public Object call() throws Exception {
 +        try {
 +          System.setProperty("p2p.joinTimeout", "5000"); // set a short join timeout.  default is 17000ms
 +          Locator myLocator = Locator.startLocatorAndDS(port2, new File(""), properties);
 +        } catch (SystemConnectException e) {
 +          return Boolean.FALSE;
 +        } finally {
 +          System.getProperties().remove("p2p.joinTimeout");
 +        }
 +        return Boolean.TRUE;
 +      }
 +    };
 +    AsyncInvocation async1 = null;
 +    AsyncInvocation async2 = null;
 +    try {
 +      async2 = loc2.invokeAsync(startLocator2);
 +      Wait.pause(2000);
 +      async1 = loc1.invokeAsync(startLocator1);
 +    } finally {
 +      try {
 +        if (async1 != null) {
 +          async1.join(45000);
 +          if (async1.isAlive()) {
 +            ThreadUtils.dumpAllStacks();
 +          }
 +          if (async2 != null) {
 +            async2.join();
 +            Object result1 = async1.getReturnValue();
 +            if (result1 instanceof Exception) {
 +              throw (Exception)result1;
 +            }
 +            Object result2 = async2.getReturnValue();
 +            if (result2 instanceof Exception) {
 +              throw (Exception)result2;
 +            }
 +            // verify that they found each other
 +            SerializableCallable verify = new SerializableCallable("verify no split-brain") {
 +              public Object call() {
 +                InternalDistributedSystem sys = InternalDistributedSystem.getAnyInstance();
 +                if (sys == null) {
 +                  fail("no distributed system found");
 +                }
 +                Assert.assertTrue(sys.getDM().getViewMembers().size() == 2,
 +                    "expected 2 members but found " + sys.getDM().getViewMembers().size()
 +                    );
 +                return true;
 +              }
 +            };
 +            loc2.invoke(verify);
 +            loc1.invoke(verify);
 +          }
 +        }
 +      } finally {
 +        SerializableRunnable r = new SerializableRunnable("stop locator") {
 +          public void run() {
 +            Locator loc = Locator.getLocator();
 +            if (loc != null) {
 +              loc.stop();
 +            }
 +          }
 +        };
 +        loc2.invoke(r);
 +        loc1.invoke(r);
 +      }
 +    }
 +    
 +  }
 +  /**
 +   * test lead member selection
 +   */
 +  public void testLeadMemberSelection() throws Exception {
 +    disconnectAllFromDS();
 +    Host host = Host.getHost(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    VM vm3 = host.getVM(3);
 +    
 +    port1 = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    DistributedTestUtils.deleteLocatorStateFile(port1);
 +    final String locators = NetworkUtils.getServerHostName(host) + "[" + port1 + "]";
 +    final Properties properties = new Properties();
 +    properties.put("mcast-port", "0");
 +    properties.put("locators", locators);
 +    properties.put("enable-network-partition-detection", "true");
 +    properties.put("disable-auto-reconnect", "true");
 +    
 +    File logFile = new File("");
 +    if (logFile.exists()) {
 +      logFile.delete();
 +    }
 +    Locator locator = Locator.startLocatorAndDS(port1, logFile, properties);
 +    try {
 +    DistributedSystem sys = locator.getDistributedSystem();
 +    
 +    Object[] connectArgs = new Object[]{ properties };
 +    
 +    SerializableRunnable disconnect =
 +      new SerializableRunnable("Disconnect from " + locators) {
 +          public void run() {
 +            DistributedSystem sys = InternalDistributedSystem.getAnyInstance();
 +            if (sys != null && sys.isConnected()) {
 +              sys.disconnect();
 +            }
 +          }
 +        };
 +        
 +    assertTrue(MembershipManagerHelper.getLeadMember(sys) == null);
 +    
 +    // connect three vms and then watch the lead member selection as they
 +    // are disconnected/reconnected
 +    properties.put("name", "vm1");
 +    DistributedMember mem1 = (DistributedMember)vm1.invoke(this.getClass(),
 +        "getDistributedMember", connectArgs);
 +    
 +//    assertTrue(MembershipManagerHelper.getLeadMember(sys) != null);
 +    assertLeadMember(mem1, sys, 5000);
 +    
 +    properties.put("name", "vm2");
 +    DistributedMember mem2 = (DistributedMember)vm2.invoke(this.getClass(),
 +        "getDistributedMember", connectArgs);
 +    assertLeadMember(mem1, sys, 5000);
 +    
 +    properties.put("name", "vm3");
 +    DistributedMember mem3 = (DistributedMember)vm3.invoke(this.getClass(),
 +        "getDistributedMember", connectArgs);
 +    assertLeadMember(mem1, sys, 5000);
 +    
 +    // after disconnecting the first vm, the second one should become the leader
 +    vm1.invoke(disconnect);
 +    MembershipManagerHelper.getMembershipManager(sys).waitForDeparture(mem1);
 +    assertLeadMember(mem2, sys, 5000);
 +    
 +    properties.put("name", "vm1");
 +    mem1 = (DistributedMember)vm1.invoke(this.getClass(),
 +        "getDistributedMember", connectArgs);
 +    assertLeadMember(mem2, sys, 5000);
 +    
 +    vm2.invoke(disconnect);
 +    MembershipManagerHelper.getMembershipManager(sys).waitForDeparture(mem2);
 +    assertLeadMember(mem3, sys, 5000);
 +    
 +    vm1.invoke(disconnect);
 +    MembershipManagerHelper.getMembershipManager(sys).waitForDeparture(mem1);
 +    assertLeadMember(mem3, sys, 5000);
 +
 +    vm3.invoke(disconnect);
 +    MembershipManagerHelper.getMembershipManager(sys).waitForDeparture(mem3);
 +    assertLeadMember(null, sys, 5000);
 +
 +    } finally {
 +      locator.stop();
 +    }
 +  }
 +
 +  private void assertLeadMember(final DistributedMember member,
 +      final DistributedSystem sys, long timeout) {
 +    WaitCriterion ev = new WaitCriterion() {
 +      public boolean done() {
 +        DistributedMember lead = MembershipManagerHelper.getLeadMember(sys);
 +        if (member != null) {
 +          return member.equals(lead);
 +        }
 +        return (lead == null);
 +      }
 +      public String description() {
 +        return null;
 +      }
 +    };
 +    Wait.waitForCriterion(ev, timeout, 200, true);
 +  }
 +  
 +  /**
 +   * test lead member and coordinator failure with network partition detection
 +   * enabled.  It would be nice for this test to have more than two "server"
 +   * vms, to demonstrate that they all exit when the leader and potential-
 +   * coordinator both disappear in the loss-correlation-window, but there
 +   * are only four vms available for dunit testing.
 +   * <p>
 +   * So, we start two locators with admin distributed systems, then start
 +   * two regular distributed members.
 +   * <p>
 +   * We kill the second locator (which is not
 +   * the view coordinator) and then kill the non-lead member.  That should be
 +   * okay - the lead and remaining locator continue to run.
 +   * <p>
 +   * We then kill the lead member and demonstrate that the original locator
 +   * (which is now the sole remaining member) shuts itself down.
 +   */
 +  public void testLeadAndCoordFailure() throws Exception {
 +    IgnoredException.addIgnoredException("Possible loss of quorum due");
 +    disconnectAllFromDS();
 +    Host host = Host.getHost(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    VM locvm = host.getVM(3);
 +    Locator locator = null;
 +    
-     final int port1 = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
++    int ports[] = AvailablePortHelper.getRandomAvailableTCPPorts(2);
++    final int port1 = ports[0];
 +    this.port1 = port1;
-     final int port2 = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
-     this.port2 = port2; // for cleanup in tearDown2()
++    final int port2 = ports[1];
 +    DistributedTestUtils.deleteLocatorStateFile(port1, port2);
 +    final String host0 = NetworkUtils.getServerHostName(host); 
 +    final String locators = host0 + "[" + port1 + "]," +
 +                            host0 + "[" + port2 + "]";
 +    final Properties properties = new Properties();
 +    properties.put("mcast-port", "0");
 +    properties.put("locators", locators);
 +    properties.put("enable-network-partition-detection", "true");
 +    properties.put("disable-auto-reconnect", "true");
 +    properties.put("member-timeout", "2000");
 +    properties.put("log-level", LogWriterUtils.getDUnitLogLevel());
 +//    properties.put("log-level", "fine");
 +    properties.put(DistributionConfig.ENABLE_CLUSTER_CONFIGURATION_NAME, "false");
 +    
 +    try {
 +      final String uname = getUniqueName();
 +      File logFile = new File("");
 +      locator = Locator.startLocatorAndDS(port1, logFile, properties);
 +      final DistributedSystem sys = locator.getDistributedSystem();
 +      sys.getLogWriter().info("<ExpectedException action=add>java.net.ConnectException</ExpectedException>");
 +      MembershipManagerHelper.inhibitForcedDisconnectLogging(true);
 +      locvm.invoke(new SerializableRunnable() {
 +        public void run() {
 +          File lf = new File("");
 +          try {
 +            Locator.startLocatorAndDS(port2, lf, properties);
 +          }
 +          catch (IOException ios) {
 +            com.gemstone.gemfire.test.dunit.Assert.fail("Unable to start locator2", ios);
 +          }
 +        }
 +      });
 +      
 +      Object[] connectArgs = new Object[]{ properties };
 +      
 +      SerializableRunnable crashLocator =
 +        new SerializableRunnable("Crash locator") {
 +          public void run() {
 +            Locator loc = Locator.getLocators().iterator().next();
 +            DistributedSystem msys = loc.getDistributedSystem();
 +            MembershipManagerHelper.crashDistributedSystem(msys);
 +            loc.stop();
 +          }
 +      };
 +
 +
 +      assertTrue(MembershipManagerHelper.getLeadMember(sys) == null);
 +      
 +//      properties.put("log-level", getDUnitLogLevel());
 +      
 +      DistributedMember mem1 = (DistributedMember)vm1.invoke(this.getClass(),
 +          "getDistributedMember", connectArgs);
 +      vm2.invoke(this.getClass(),
 +          "getDistributedMember", connectArgs);
 +      assertLeadMember(mem1, sys, 5000);
 +      
 +      assertEquals(sys.getDistributedMember(), MembershipManagerHelper.getCoordinator(sys));
 +      
 +      // crash the second vm and the locator.  Should be okay
 +      DistributedTestUtils.crashDistributedSystem(vm2);
 +      locvm.invoke(crashLocator);
 +      
 +      assertTrue("Distributed system should not have disconnected",
-           vm1.invokeBoolean(LocatorDUnitTest.class, "isSystemConnected"));
++          vm1.invoke(() -> LocatorDUnitTest.isSystemConnected()));
 +      
 +      // ensure quorumLost is properly invoked
 +      DistributionManager dm = (DistributionManager)((InternalDistributedSystem)sys).getDistributionManager();
 +      MyMembershipListener listener = new MyMembershipListener();
 +      dm.addMembershipListener(listener);
 +  
 +      // disconnect the first vm and demonstrate that the third vm and the
 +      // locator notice the failure and exit
 +      DistributedTestUtils.crashDistributedSystem(vm1);
 +
 +      /* This vm is watching vm1, which is watching vm2 which is watching locvm.
 +       * It will take 3 * (3 * member-timeout) milliseconds to detect the full
 +       * failure and eject the lost members from the view.
 +       */
 +      
 +      com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("waiting for my distributed system to disconnect due to partition detection");
 +      WaitCriterion ev = new WaitCriterion() {
 +        public boolean done() {
 +          return !sys.isConnected();
 +        }
 +        public String description() {
 +          return null;
 +        }
 +      };
 +      Wait.waitForCriterion(ev, 12 * 2000, 200, true);
 +      if (sys.isConnected()) {
 +        fail("Distributed system did not disconnect as expected - network partition detection is broken");
 +      }
 +      // quorumLost should be invoked if we get a ForcedDisconnect in this situation
 +      assertTrue("expected quorumLost to be invoked", listener.quorumLostInvoked);
 +      assertTrue("expected suspect processing initiated by TCPConduit", listener.suspectReasons.contains(Connection.INITIATING_SUSPECT_PROCESSING));
 +    }
 +    finally {
 +      if (locator != null) {
 +        locator.stop();
 +      }
 +      LogWriter bLogger =
 +        new LocalLogWriter(InternalLogWriter.ALL_LEVEL, System.out);
 +      bLogger.info("<ExpectedException action=remove>service failure</ExpectedException>");
 +      bLogger.info("<ExpectedException action=remove>java.net.ConnectException</ExpectedException>");
 +      bLogger.info("<ExpectedException action=remove>com.gemstone.gemfire.ForcedDisconnectException</ExpectedException>");
 +      disconnectAllFromDS();
 +    }
 +  }
 +  
 +
 +  /**
 +   * test lead member failure and normal coordinator shutdown with network partition detection
 +   * enabled.
 +   * <p>
 +   * Start two locators with admin distributed systems, then start
 +   * two regular distributed members.
 +   * <p>
 +   * We kill the lead member and demonstrate that the other members continue
 +   * to operate normally.
 +   * <p>
 +   * We then shut down the group coordinator and observe the second locator
 +   * pick up the job and the remaining member continues to operate normally.
 +   */
 +  public void testLeadFailureAndCoordShutdown() throws Exception {
 +    disconnectAllFromDS();
 +    Host host = Host.getHost(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    VM locvm = host.getVM(3);
 +    Locator locator = null;
 +    
-     final int port1 = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
++    final int[] ports = AvailablePortHelper.getRandomAvailableTCPPorts(2);
++    final int port1 = ports[0];
 +    this.port1 = port1;
-     final int port2 = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
++    final int port2 = ports[1];
 +    this.port2 = port2;
 +    DistributedTestUtils.deleteLocatorStateFile(port1, port2);
 +    final String host0 = NetworkUtils.getServerHostName(host);
 +    final String locators = host0 + "[" + port1 + "],"
 +          + host0 + "[" + port2 + "]";
 +    final Properties properties = new Properties();
 +    properties.put("mcast-port", "0");
 +    properties.put("locators", locators);
 +    properties.put("enable-network-partition-detection", "true");
 +    properties.put("disable-auto-reconnect", "true");
 +    properties.put("member-timeout", "2000");
 +    properties.put(DistributionConfig.ENABLE_CLUSTER_CONFIGURATION_NAME, "false");
 +
 +    SerializableRunnable stopLocator = getStopLocatorRunnable();
 +    
 +    try {
 +      final String uname = getUniqueName();
 +      File logFile = new File("");
 +      locator = Locator.startLocatorAndDS(port1, logFile, properties);
 +      DistributedSystem sys = locator.getDistributedSystem();
 +      locvm.invoke(new SerializableRunnable() {
 +        public void run() {
 +          File lf = new File("");
 +          try {
 +            Locator.startLocatorAndDS(port2, lf, properties);
 +            MembershipManagerHelper.inhibitForcedDisconnectLogging(true);
 +          }
 +          catch (IOException ios) {
 +            com.gemstone.gemfire.test.dunit.Assert.fail("Unable to start locator2", ios);
 +          }
 +        }
 +      });
 +      
 +      Object[] connectArgs = new Object[]{ properties };
 +      
 +      SerializableRunnable crashSystem =
 +        new SerializableRunnable("Crash system") {
 +          public void run() {
 +            DistributedSystem msys = InternalDistributedSystem.getAnyInstance();
 +            msys.getLogWriter().info("<ExpectedException action=add>service failure</ExpectedException>");
 +            msys.getLogWriter().info("<ExpectedException action=add>com.gemstone.gemfire.ConnectException</ExpectedException>");
 +            msys.getLogWriter().info("<ExpectedException action=add>com.gemstone.gemfire.ForcedDisconnectException</ExpectedException>");
 +            MembershipManagerHelper.crashDistributedSystem(msys);
 +          }
 +      };
 +
 +      assertTrue(MembershipManagerHelper.getLeadMember(sys) == null);
 +      
 +      DistributedMember mem1 = (DistributedMember)vm1.invoke(this.getClass(),
 +          "getDistributedMember", connectArgs);
 +      DistributedMember mem2 = (DistributedMember)vm2.invoke(this.getClass(),
 +          "getDistributedMember", connectArgs);
 +
 +      assertEquals(mem1, MembershipManagerHelper.getLeadMember(sys));
 +      
 +      assertEquals(sys.getDistributedMember(), MembershipManagerHelper.getCoordinator(sys));
 +      
 +      MembershipManagerHelper.inhibitForcedDisconnectLogging(true);
 +
 +      // crash the lead vm. Should be okay
 +      vm1.invoke(crashSystem);
 +
 +      Wait.pause(4 * 2000); // 4 x the member-timeout
 +      
 +      assertTrue("Distributed system should not have disconnected",
 +          isSystemConnected());
 +      
 +      assertTrue("Distributed system should not have disconnected",
-           vm2.invokeBoolean(LocatorDUnitTest.class, "isSystemConnected"));
++          vm2.invoke(() -> LocatorDUnitTest.isSystemConnected()));
 +      
 +      assertTrue("Distributed system should not have disconnected",
-           locvm.invokeBoolean(LocatorDUnitTest.class, "isSystemConnected"));
++          locvm.invoke(() -> LocatorDUnitTest.isSystemConnected()));
 +
 +      // stop the locator normally.  This should also be okay
 +      locator.stop();
 +      
 +      if (!Locator.getLocators().isEmpty()) {
 +        // log this for debugging purposes before throwing assertion error
 +        com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().warning("found locator " + Locator.getLocators().iterator().next());
 +      }
 +      assertTrue("locator is not stopped", Locator.getLocators().isEmpty());
 +      
 +      assertTrue("Distributed system should not have disconnected",
-           vm2.invokeBoolean(LocatorDUnitTest.class, "isSystemConnected"));
++          vm2.invoke(() -> LocatorDUnitTest.isSystemConnected()));
 +      
 +      assertTrue("Distributed system should not have disconnected",
-           locvm.invokeBoolean(LocatorDUnitTest.class, "isSystemConnected"));
++          locvm.invoke(() -> LocatorDUnitTest.isSystemConnected()));
 +
 +      // the remaining non-locator member should now be the lead member
 +      assertEquals("This test sometimes fails.  If the log contains " +
 +      		"'failed to collect all ACKs' it is a false failure.",
-       		mem2, vm2.invoke(LocatorDUnitTest.class, "getLeadMember", new Object[]{}));
++      		mem2, vm2.invoke(() -> LocatorDUnitTest.getLeadMember()));
 +      
 +      SerializableRunnable disconnect =
 +        new SerializableRunnable("Disconnect from " + locators) {
 +            public void run() {
 +              DistributedSystem sys = InternalDistributedSystem.getAnyInstance();
 +              if (sys != null && sys.isConnected()) {
 +                sys.disconnect();
 +              }
 +            }
 +          };
 +
 +      // disconnect the first vm and demonstrate that the third vm and the
 +      // locator notice the failure and exit
 +      vm2.invoke(disconnect);
 +      locvm.invoke(stopLocator);
 +    }
 +    finally {
 +      MembershipManagerHelper.inhibitForcedDisconnectLogging(false);
 +      if (locator != null) {
 +        locator.stop();
 +      }
 +      try {
 +        locvm.invoke(stopLocator);
 +      } catch (Exception e) {
 +        com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().severe("failed to stop locator in vm 3", e);
 +      }
 +    }
 +  }
 +
 +  /**
 +   * test lead member failure and normal coordinator shutdown with network partition detection
 +   * enabled.
 +   * <p>
 +   * Start one locators with admin distributed systems, then start
 +   * two regular distributed members.
 +   * <p>
 +   * We kill the lead member and demonstrate that the other members continue
 +   * to operate normally.
 +   * <p>
 +   * We then shut down the group coordinator and observe the second locator
 +   * pick up the job and the remaining member continues to operate normally.
 +   */
 +  // disabled on trunk - should be reenabled on cedar_dev_Oct12
 +  // this test leaves a CloserThread around forever that logs "pausing" messages every 500 ms
 +  public void testForceDisconnectAndPeerShutdownCause() throws Exception {
 +    disconnectAllFromDS();
 +    Host host = Host.getHost(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    VM locvm = host.getVM(3);
 +    Locator locator = null;
 +    
-     final int port1 = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
++    int ports[] = AvailablePortHelper.getRandomAvailableTCPPorts(2);
++    final int port1 = ports[0];
 +    this.port1 = port1;
-     final int port2 = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
-     this.port2 = port2;
++    final int port2 = ports[1];
 +    DistributedTestUtils.deleteLocatorStateFile(port1, port2);
 +    final String host0 = NetworkUtils.getServerHostName(host);
 +    final String locators = host0 + "[" + port1 + "]," + host0 + "[" + port2 + "]";
 +
 +    final Properties properties = new Properties();
 +    properties.put("mcast-port", "0");
 +    properties.put("locators", locators);
 +    properties.put("enable-network-partition-detection", "true");
 +    properties.put("disable-auto-reconnect", "true");
 +    properties.put("member-timeout", "2000");
 +    properties.put("log-level", LogWriterUtils.getDUnitLogLevel());
 +    properties.put(DistributionConfig.ENABLE_CLUSTER_CONFIGURATION_NAME, "false");
 +
 +    SerializableRunnable stopLocator = getStopLocatorRunnable();
 +    
 +    try {
 +      final String uname = getUniqueName();
 +      File logFile = new File("");
 +      locator = Locator.startLocatorAndDS(port1, logFile, properties);
 +      DistributedSystem sys = locator.getDistributedSystem();
 +      locvm.invoke(new SerializableRunnable() {
 +        public void run() {
 +          File lf = new File("");
 +          try {
 +            Locator.startLocatorAndDS(port2, lf, properties);
 +          }
 +          catch (IOException ios) {
 +            com.gemstone.gemfire.test.dunit.Assert.fail("Unable to start locator2", ios);
 +          }
 +        }
 +      });
 +      
 +      Object[] connectArgs = new Object[]{ properties };
 +      
 +      SerializableRunnable crashSystem =
 +        new SerializableRunnable("Crash system") {
 +          public void run() {
 +            DistributedSystem msys = InternalDistributedSystem.getAnyInstance();
 +            msys.getLogWriter().info("<ExpectedException action=add>service failure</ExpectedException>");
 +            msys.getLogWriter().info("<ExpectedException action=add>com.gemstone.gemfire.ConnectException</ExpectedException>");
 +            msys.getLogWriter().info("<ExpectedException action=add>com.gemstone.gemfire.ForcedDisconnectException</ExpectedException>");
 +            msys.getLogWriter().info("<ExpectedException action=add>Possible loss of quorum</ExpectedException>");
 +            hook = new TestHook();
 +            MembershipManagerHelper.getMembershipManager(msys).registerTestHook(hook);
 +            try {
 +              MembershipManagerHelper.crashDistributedSystem(msys);
 +            } finally {
 +              hook.reset();
 +            }
 +          }
 +      };
 +
 +      assertTrue(MembershipManagerHelper.getLeadMember(sys) == null);
 +      
 +      final DistributedMember mem1 = (DistributedMember)vm1.invoke(this.getClass(),
 +          "getDistributedMember", connectArgs);
 +      final DistributedMember mem2 = (DistributedMember)vm2.invoke(this.getClass(),
 +          "getDistributedMember", connectArgs);
 +
 +      assertEquals(mem1, MembershipManagerHelper.getLeadMember(sys));
 +      
 +      assertEquals(sys.getDistributedMember(), MembershipManagerHelper.getCoordinator(sys));
 +      
 +      // crash the lead vm. Should be okay. it should hang in test hook thats
 +      // why call is asynchronous.
 +      //vm1.invokeAsync(crashSystem);
 +
 +      assertTrue("Distributed system should not have disconnected",
 +          isSystemConnected());
 +      
 +      assertTrue("Distributed system should not have disconnected",
-           vm2.invokeBoolean(LocatorDUnitTest.class, "isSystemConnected"));
++          vm2.invoke(() -> LocatorDUnitTest.isSystemConnected()));
 +      
 +      assertTrue("Distributed system should not have disconnected",
-           locvm.invokeBoolean(LocatorDUnitTest.class, "isSystemConnected"));
++          locvm.invoke(() -> LocatorDUnitTest.isSystemConnected()));
 +
 +      vm2.invokeAsync(crashSystem);
 +
 +      Wait.pause(1000); // 4 x the member-timeout
 +      
 +      // request member removal for first peer from second peer.
 +      vm2.invoke(new SerializableRunnable("Request Member Removal") {
 +        
 +        @Override
 +        public void run() {
 +          DistributedSystem msys = InternalDistributedSystem.getAnyInstance();
 +          MembershipManager mmgr = MembershipManagerHelper.getMembershipManager(msys);
 +          
 +          // check for shutdown cause in MembershipManager. Following call should
 +          // throw DistributedSystemDisconnectedException which should have cause as
 +          // ForceDisconnectException.
 +          try {
 +            msys.getLogWriter().info("<ExpectedException action=add>Membership: requesting removal of </ExpectedException>");
 +            mmgr.requestMemberRemoval(mem1, "test reasons");
 +            msys.getLogWriter().info("<ExpectedException action=remove>Membership: requesting removal of </ExpectedException>");
 +            fail("It should have thrown exception in requestMemberRemoval");
 +          } catch (DistributedSystemDisconnectedException e) {
 +            Throwable cause = e.getCause();
 +            assertTrue(
 +                "This should have been ForceDisconnectException but found "
 +                    + cause, cause instanceof ForcedDisconnectException);
 +          } finally {
 +            hook.reset();
 +          }
 +        }
 +      });
 +
 +    }
 +    finally {
 +      if (locator != null) {
 +        locator.stop();
 +      }
 +      locvm.invoke(stopLocator);
 +      assertTrue("locator is not stopped", Locator.getLocators().isEmpty());
 +    }
 +  }
 +
 +  /**
 +   * test lead member shutdown and coordinator crashing with network partition detection
 +   * enabled.
 +   * <p>
 +   * Start two locators with admin distributed systems, then start
 +   * two regular distributed members.
 +   * <p>
 +   * We kill the coordinator and shut down the lead member and observe the second locator
 +   * pick up the job and the remaining member continue to operate normally.
 +   */
 +  public void testLeadShutdownAndCoordFailure() throws Exception {
 +    disconnectAllFromDS();
 +    Host host = Host.getHost(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    VM locvm = host.getVM(3);
 +    Locator locator = null;
 +    
-     final int port1 = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
++    int ports[] = AvailablePortHelper.getRandomAvailableTCPPorts(2);
++    final int port1 = ports[0];
 +    this.port1 = port1;
-     final int port2 = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
-     this.port2 = port2;
++    final int port2 = ports[1];
 +    DistributedTestUtils.deleteLocatorStateFile(port1, port2);
 +    final String host0 = NetworkUtils.getServerHostName(host);
 +    final String locators = host0 + "[" + port1 + "],"
 +          + host0 + "[" + port2 + "]";
 +    final Properties properties = new Properties();
 +    properties.put("mcast-port", "0");
 +    properties.put("locators", locators);
 +    properties.put("enable-network-partition-detection", "true");
 +    properties.put("disable-auto-reconnect", "true");
 +    properties.put("member-timeout", "2000");
 +    properties.put(DistributionConfig.ENABLE_CLUSTER_CONFIGURATION_NAME, "false");
 +
 +    SerializableRunnable disconnect =
 +      new SerializableRunnable("Disconnect from " + locators) {
 +          public void run() {
 +            DistributedSystem sys = InternalDistributedSystem.getAnyInstance();
 +            if (sys != null && sys.isConnected()) {
 +              sys.disconnect();
 +            }
 +            MembershipManagerHelper.inhibitForcedDisconnectLogging(false);
 +          }
 +        };
 +    SerializableRunnable expectedException =
 +      new SerializableRunnable("Add expected exceptions") {
 +        public void run() {
 +          MembershipManagerHelper.inhibitForcedDisconnectLogging(true);
 +        }
 +    };
 +    try {
 +      final String uname = getUniqueName();
 +      locvm.invoke(new SerializableRunnable() {
 +        public void run() {
 +          File lf = new File("");
 +          try {
 +            Locator.startLocatorAndDS(port2, lf, properties);
 +          }
 +          catch (IOException ios) {
 +            com.gemstone.gemfire.test.dunit.Assert.fail("Unable to start locator1", ios);
 +          }
 +        }
 +      });
 +
 +      File logFile = new File("");
 +      locator = Locator.startLocatorAndDS(port1, logFile, properties);
 +      DistributedSystem sys = locator.getDistributedSystem();
 +      sys.getLogWriter().info("<ExpectedException action=add>com.gemstone.gemfire.ForcedDisconnectException</ExpectedException>");
 +      Object[] connectArgs = new Object[]{ properties };
 +      
 +      SerializableRunnable crashLocator =
 +        new SerializableRunnable("Crash locator") {
 +          public void run() {
 +            Locator loc = Locator.getLocators().iterator().next();
 +            DistributedSystem msys = loc.getDistributedSystem();
 +            msys.getLogWriter().info("<ExpectedException action=add>service failure</ExpectedException>");
 +            msys.getLogWriter().info("<ExpectedException action=add>com.gemstone.gemfire.ForcedDisconnectException</ExpectedException>");
 +            msys.getLogWriter().info("<ExpectedException action=add>com.gemstone.gemfire.ConnectException</ExpectedException>");
 +            MembershipManagerHelper.crashDistributedSystem(msys);
 +            loc.stop();
 +          }
 +      };
 +
 +      assertTrue(MembershipManagerHelper.getLeadMember(sys) == null);
 +      
 +      DistributedMember mem1 = (DistributedMember)vm1.invoke(this.getClass(),
 +          "getDistributedMember", connectArgs);
 +      vm1.invoke(expectedException);
 +      DistributedMember mem2 = (DistributedMember)vm2.invoke(this.getClass(),
 +          "getDistributedMember", connectArgs);
 +      
-       DistributedMember loc1Mbr = (DistributedMember)locvm.invoke(this.getClass(),
-           "getLocatorDistributedMember", new Object[]{});
++      DistributedMember loc1Mbr = (DistributedMember)locvm.invoke(() -> this.getLocatorDistributedMember());
 +
 +      assertLeadMember(mem1, sys, 5000);
 +      
 +      assertEquals(loc1Mbr, MembershipManagerHelper.getCoordinator(sys));
 +      
 +      // crash the lead locator.  Should be okay
 +      locvm.invoke(crashLocator);
 +      Wait.pause(10 * 1000);
 +
 +      assertTrue("Distributed system should not have disconnected",
 +          sys.isConnected());
 +      
 +      assertTrue("Distributed system should not have disconnected",
-           vm1.invokeBoolean(LocatorDUnitTest.class, "isSystemConnected"));
++          vm1.invoke(() -> LocatorDUnitTest.isSystemConnected()));
 +      
 +      assertTrue("Distributed system should not have disconnected",
-           vm2.invokeBoolean(LocatorDUnitTest.class, "isSystemConnected"));
++          vm2.invoke(() -> LocatorDUnitTest.isSystemConnected()));
 +
 +      // disconnect the first vm and demonstrate that the non-lead vm and the
 +      // locator notice the failure and continue to run
 +      vm1.invoke(disconnect);
 +      Wait.pause(10 * 1000);
 +      
 +      assertTrue("Distributed system should not have disconnected",
-           vm2.invokeBoolean(LocatorDUnitTest.class, "isSystemConnected"));
++          vm2.invoke(() -> LocatorDUnitTest.isSystemConnected()));
 +      
 +      assertEquals(sys.getDistributedMember(),
 +          MembershipManagerHelper.getCoordinator(sys));
 +      assertEquals(mem2, MembershipManagerHelper.getLeadMember(sys));
 +      
 +    }
 +    finally {
 +      vm2.invoke(disconnect);
 +      
 +      if (locator != null) {
 +        locator.stop();
 +      }
 +      locvm.invoke(getStopLocatorRunnable());
 +    }
 +  }
 +
 +  /**
 +   * Tests that attempting to connect to a distributed system in which
 +   * no locator is defined throws an exception.
 +   */
 +  public void testNoLocator() {
 +    disconnectAllFromDS();
 +    Host host = Host.getHost(0);
 +    int port =
 +      AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    DistributedTestUtils.deleteLocatorStateFile(port1);
 +    String locators = NetworkUtils.getServerHostName(host) + "[" + port + "]";
 +    Properties props = new Properties();
 +    props.setProperty("mcast-port", "0");
 +    props.setProperty("locators", locators);
 +
 +    final String expected = "java.net.ConnectException";
 +    final String addExpected = 
 +      "<ExpectedException action=add>" + expected + "</ExpectedException>";
 +    final String removeExpected = 
 +      "<ExpectedException action=remove>" + expected + "</ExpectedException>";
 +      
 +    LogWriter bgexecLogger =
 +          new LocalLogWriter(InternalLogWriter.ALL_LEVEL, System.out);
 +    bgexecLogger.info(addExpected);
 +    
 +    boolean exceptionOccurred = true;
 +    String oldValue = (String)System.getProperties().put("p2p.joinTimeout", "15000");
 +    try {
 +      DistributedSystem.connect(props);
 +      exceptionOccurred = false;
 +
 +    } catch (DistributionException ex) {
 +      // I guess it can throw this too...
 +
 +    } catch (GemFireConfigException ex) {
 +      String s = ex.getMessage();
 +      assertTrue(s.indexOf("Locator does not exist") >= 0);
 +      
 +    } catch (Exception ex) {
 +      // if you see this fail, determine if unexpected exception is expected
 +      // if expected then add in a catch block for it above this catch
 +      com.gemstone.gemfire.test.dunit.Assert.fail("Failed with unexpected exception", ex);
 +    }
 +    finally {
 +      if (oldValue == null) {
 +        System.getProperties().remove("p2p.joinTimeout");
 +      } else {
 +        System.getProperties().put("p2p.joinTimeout", oldValue);
 +      }
 +      bgexecLogger.info(removeExpected);
 +    }
 +
 +    if (!exceptionOccurred) {
 +      fail("Should have thrown a GemFireConfigException");
 +    }
 +  }
 +
 +  /**
 +   * Tests starting one locator in a remote VM and having multiple
 +   * members of the distributed system join it.  This ensures that
 +   * members start up okay, and that handling of a stopped locator
 +   * is correct.
++   * <p>The locator is then restarted and is shown to take over the
++   * role of membership coordinator.
 +   */
 +  public void testOneLocator() throws Exception {
 +    disconnectAllFromDS();
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +
 +    final int port =
 +      AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    DistributedTestUtils.deleteLocatorStateFile(port1);
 +    final String locators = NetworkUtils.getServerHostName(host) + "[" + port + "]";
 +    final String uniqueName = getUniqueName();
 +    
 +    vm0.invoke(new SerializableRunnable("Start locator " + locators) {
 +        public void run() {
 +          File logFile = new File("");
 +          try {
 +            Properties locProps = new Properties();
 +            locProps.setProperty("mcast-port", "0");
 +            locProps.put(DistributionConfig.ENABLE_CLUSTER_CONFIGURATION_NAME, "false");
 +
 +            Locator.startLocatorAndDS(port, logFile, locProps);
 +          } catch (IOException ex) {
 +            com.gemstone.gemfire.test.dunit.Assert.fail("While starting locator on port " + port, ex);
 +          }
 +        }
 +      });
 +    try {
 +
 +    SerializableRunnable connect =
 +      new SerializableRunnable("Connect to " + locators) {
 +          public void run() {
 +            //System.setProperty("p2p.joinTimeout", "5000");
 +            Properties props = new Properties();
 +            props.setProperty("mcast-port", "0");
 +            props.setProperty("locators", locators);
 +            DistributedSystem.connect(props);
 +          }
 +        };
 +    vm1.invoke(connect);
 +    vm2.invoke(connect);
 +
 +    Properties props = new Properties();
 +    props.setProperty("mcast-port", "0");
 +    props.setProperty("locators", locators);
 +
 +    system = (InternalDistributedSystem)DistributedSystem.connect(props);
 +    
 +    final DistributedMember coord = MembershipManagerHelper.getCoordinator(system);
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("coordinator before termination of locator is " + coord);
 +
 +    vm0.invoke(getStopLocatorRunnable());
 +    
 +    // now ensure that one of the remaining members became the coordinator
 +    WaitCriterion ev = new WaitCriterion() {
 +      public boolean done() {
 +        return !coord.equals(MembershipManagerHelper.getCoordinator(system));
 +      }
 +      public String description() {
-         return "expected the coordinator to be " + coord + " but it is " +
++        return "expected the coordinator to not be " + coord + " but it is " +
 +          MembershipManagerHelper.getCoordinator(system);
 +      }
 +    };
-     Wait.waitForCriterion(ev, 15 * 1000, 200, true);
++    Wait.waitForCriterion(ev, 15 * 1000, 200, false);
 +    DistributedMember newCoord = MembershipManagerHelper.getCoordinator(system); 
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("coordinator after shutdown of locator was " +
 +        newCoord);
 +    if (coord.equals(newCoord)) {
 +      fail("another member should have become coordinator after the locator was stopped");
 +    }
 +      
 +
 +    system.disconnect();
 +
 +    SerializableRunnable disconnect =
 +      new SerializableRunnable("Disconnect from " + locators) {
 +          public void run() {
 +            DistributedSystem sys = InternalDistributedSystem.getAnyInstance();
 +            if (sys != null && sys.isConnected()) {
 +              sys.disconnect();
 +            }
 +          }
 +        };
 +    vm1.invoke(disconnect);
 +    vm2.invoke(disconnect);
 +
 +    } finally {
 +      vm0.invoke(getStopLocatorRunnable());
 +    }
 +  }
 +
 +//  public void testRepeat() throws Exception {
 +//    for (int i=0; i<10; i++) {
 +//      System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> run #"+i);
 +//      testLocatorBecomesCoordinator();
 +//      tearDown();
 +//      setUp();
 +//    }
 +//  }
 +  /**
 +   * Tests starting one locator in a remote VM and having multiple
 +   * members of the distributed system join it.  This ensures that
 +   * members start up okay, and that handling of a stopped locator
 +   * is correct.  It then restarts the locator to demonstrate that
 +   * it can connect to and function as the group coordinator
 +   */
 +  public void testLocatorBecomesCoordinator() throws Exception {
 +    disconnectAllFromDS();
 +    final String expected = "java.net.ConnectException";
 +    final String addExpected = 
 +      "<ExpectedException action=add>" + expected + "</ExpectedException>";
 +    final String removeExpected = 
 +      "<ExpectedException action=remove>" + expected + "</ExpectedException>";
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +
 +    final int port =
 +      AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    DistributedTestUtils.deleteLocatorStateFile(port1);
 +    final String locators = NetworkUtils.getServerHostName(host) + "[" + port + "]";
 +    
 +    vm0.invoke(getStartSBLocatorRunnable(port, getUniqueName()+"1"));
 +    try {
 +
 +    final Properties props = new Properties();
 +    props.setProperty("mcast-port", "0");
 +    props.setProperty("locators", locators);
 +    props.put(DistributionConfig.ENABLE_CLUSTER_CONFIGURATION_NAME, "false");
 +
 +    SerializableRunnable connect =
 +      new SerializableRunnable("Connect to " + locators) {
 +          public void run() {
 +            //System.setProperty("p2p.joinTimeout", "5000");
 +            DistributedSystem sys = getSystem(props);
 +            sys.getLogWriter().info(addExpected);
 +          }
 +        };
 +    vm1.invoke(connect);
 +    vm2.invoke(connect);
 +
 +    system = (InternalDistributedSystem)getSystem(props);
 +
 +    final DistributedMember coord = MembershipManagerHelper.getCoordinator(system);
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("coordinator before termination of locator is " + coord);
 +
 +    vm0.invoke(getStopLocatorRunnable());
 +    
 +    // now ensure that one of the remaining members became the coordinator
 +    WaitCriterion ev = new WaitCriterion() {
 +      public boolean done() {
 +        return !coord.equals(MembershipManagerHelper.getCoordinator(system));
 +      }
 +      public String description() {
 +        return "expected the coordinator to be " + coord + " but it is " +
 +          MembershipManagerHelper.getCoordinator(system);
 +      }
 +    };
 +    Wait.waitForCriterion(ev, 15000, 200, true);
 +    DistributedMember newCoord = MembershipManagerHelper.getCoordinator(system); 
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("coordinator after shutdown of locator was " +
 +        newCoord);
 +    if (newCoord == null || coord.equals(newCoord)) {
 +      fail("another member should have become coordinator after the locator was stopped: "
 +          + newCoord);
 +    }
 +      
 +
 +    // restart the locator to demonstrate reconnection & make disconnects faster
 +    // it should also regain the role of coordinator, so we check to make sure
 +    // that the coordinator has changed
 +    vm0.invoke(getStartSBLocatorRunnable(port, getUniqueName()+"2"));
 +    
 +    final DistributedMember tempCoord = newCoord;
 +    ev = new WaitCriterion() {
 +      public boolean done() {
 +        return !tempCoord.equals(MembershipManagerHelper.getCoordinator(system));
 +      }
 +      public String description() {
 +        return null;
 +      }
 +    };
 +    Wait.waitForCriterion(ev, 5000, 200, true);
 +    
 +    system.disconnect();
 +    LogWriter bgexecLogger =
 +      new LocalLogWriter(InternalLogWriter.ALL_LEVEL, System.out);
 +    bgexecLogger.info(removeExpected);
 +
 +    SerializableRunnable disconnect =
 +      new SerializableRunnable("Disconnect from " + locators) {
 +          public void run() {
 +            DistributedSystem sys = InternalDistributedSystem.getAnyInstance();
 +            if (sys != null && sys.isConnected()) {
 +              sys.disconnect();
 +            }
 +            // connectExceptions occur during disconnect, so we need the
 +            // expectedexception hint to be in effect until this point
 +            LogWriter bLogger =
 +              new LocalLogWriter(InternalLogWriter.ALL_LEVEL, System.out);
 +            bLogger.info(removeExpected);
 +          }
 +        };
 +    vm1.invoke(disconnect);
 +    vm2.invoke(disconnect);
 +    vm0.invoke(getStopLocatorRunnable());
 +    } finally {
 +      vm0.invoke(getStopLocatorRunnable());
 +    }
 +
 +  }
 +
 +  
 +  /** set a short locator refresh rate */
 +  public static void setShortRefreshWait() {
 +    System.setProperty("p2p.gossipRefreshRate", "2000");
 +  }
 +  
 +  /** remove shortened locator refresh rate */
 +  public static void resetRefreshWait() {
 +    System.getProperties().remove("p2p.gossipRefreshRate");
 +  }
 +  
 +  public static boolean isSystemConnected() {
 +    DistributedSystem sys = InternalDistributedSystem.getAnyInstance();
 +    if (sys != null && sys.isConnected()) {
 +      return true;
 +    }
 +    return false;
 +  }
 +  
 +
 +  static boolean beforeFailureNotificationReceived;
 +  static boolean afterFailureNotificationReceived;
 +
 +
 +  /**
 +   * Tests starting multiple locators in multiple VMs.
 +   */
 +  public void testMultipleLocators() throws Exception {
 +    disconnectAllFromDS();
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    VM vm3 = host.getVM(3);
 +
 +    int[] freeTCPPorts = AvailablePortHelper.getRandomAvailableTCPPorts(2);
 +    final int port1 = freeTCPPorts[0];
 +    this.port1 = port1;
 +    final int port2 = freeTCPPorts[1];
 +    this.port2 = port2;
 +    DistributedTestUtils.deleteLocatorStateFile(port1, port2);
 +    final String host0 = NetworkUtils.getServerHostName(host); 
 +    final String locators = host0 + "[" + port1 + "]," +
 +                            host0 + "[" + port2 + "]";
 +
 +    final Properties dsProps = new Properties();
 +    dsProps.setProperty("locators", locators);
 +    dsProps.setProperty("mcast-port", "0");
 +    dsProps.setProperty(DistributionConfig.ENABLE_CLUSTER_CONFIGURATION_NAME, "false");
 +//    dsProps.setProperty("log-level", "finest");
 +    final String uniqueName = getUniqueName();
 +
 +    vm0.invoke(new SerializableRunnable("Start locator on " + port1) {
 +      public void run() {
 +        File logFile = new File("");
 +        try {
 +          Locator.startLocatorAndDS(port1, logFile, dsProps);
 +        } catch (IOException ex) {
 +          com.gemstone.gemfire.test.dunit.Assert.fail("While starting locator on port " + port1, ex);
 +        }
 +      }
 +    });
 +    try {
 +
 +      //try { Thread.currentThread().sleep(4000); } catch (InterruptedException ie) { }
 +
 +      vm3.invoke(new SerializableRunnable("Start locator on " + port2) {
 +        public void run() {
 +          File logFile = new File("");
 +          try {
 +            Locator.startLocatorAndDS(port2, logFile, dsProps);
 +
 +          } catch (IOException ex) {
 +            com.gemstone.gemfire.test.dunit.Assert.fail("While starting locator on port " + port2, ex);
 +          }
 +        }
 +      });
 +      try {
 +
 +        SerializableRunnable connect =
 +          new SerializableRunnable("Connect to " + locators) {
 +          public void run() {
 +            Properties props = new Properties();
 +            props.setProperty("mcast-port", "0");
 +            props.setProperty("locators", locators);
 +            DistributedSystem.connect(props);
 +          }
 +        };
 +        vm1.invoke(connect);
 +        vm2.invoke(connect);
 +
 +        Properties props = new Properties();
 +        props.setProperty("mcast-port", "0");
 +        props.setProperty("locators", locators);
 +
 +        system = (InternalDistributedSystem)DistributedSystem.connect(props);
 +
 +        WaitCriterion ev = new WaitCriterion() {
 +          public boolean done() {
 +            try {
 +              return system.getDM().getViewMembers().size() >= 3;
 +            }
 +            catch (Exception e) {
 +              e.printStackTrace();
 +              fail("unexpected exception");
 +            }
 +            return false; // NOTREACHED
 +          }
 +          public String description() {
 +            return null;
 +          }
 +        };
 +        Wait.waitForCriterion(ev, 10 * 1000, 200, true);
 +
 +        // three applications plus
 +        assertEquals(5, system.getDM().getViewMembers().size());
 +
 +        system.disconnect();
 +
 +        SerializableRunnable disconnect =
 +          new SerializableRunnable("Disconnect from " + locators) {
 +          public void run() {
 +            DistributedSystem sys = InternalDistributedSystem.getAnyInstance();
 +            if (sys != null && sys.isConnected()) {
 +              sys.disconnect();
 +            }
 +          }
 +        };
 +        vm1.invoke(disconnect);
 +        vm2.invoke(disconnect);
 +
 +      } finally {
 +        vm3.invoke(getStopLocatorRunnable());
 +      }
 +    } finally {
 +      vm0.invoke(getStopLocatorRunnable());
 +    }
 +  }
 +  
 +  /**
 +   * Tests starting multiple locators in multiple VMs.
 +   */
 +  public void testMultipleMcastLocators() throws Exception {
 +    disconnectAllFromDS();
 +    IgnoredException.addIgnoredException("Could not stop  Distribution Locator"); // shutdown timing issue in InternalLocator
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    VM vm3 = host.getVM(3);
 +
 +    final int[] freeTCPPorts = AvailablePortHelper.getRandomAvailableTCPPorts(2);
 +    final int port1 = freeTCPPorts[0];
 +    this.port1 = port1;
 +    final int port2 = freeTCPPorts[1];
 +    this.port2 = port2;
 +    DistributedTestUtils.deleteLocatorStateFile(port1, port2);
 +    final int mcastport = AvailablePort.getRandomAvailablePort(AvailablePort.MULTICAST);
 +    
 +    final String host0 = NetworkUtils.getServerHostName(host); 
 +    final String locators = host0 + "[" + port1 + "]," +
 +                            host0 + "[" + port2 + "]";
 +    final String uniqueName = getUniqueName();
 +    
 +    vm0.invoke(new SerializableRunnable("Start locator on " + port1) {
 +        public void run() {
 +          File logFile = new File("");
 +          try {
 +            Properties props = new Properties();
 +            props.setProperty("mcast-port", String.valueOf(mcastport));
 +            props.setProperty("locators", locators);
 +            props.setProperty("log-level", LogWriterUtils.getDUnitLogLevel());
 +            props.setProperty("mcast-ttl", "0");
 +            props.setProperty("enable-network-partition-detection", "true");
 +            props.setProperty(DistributionConfig.ENABLE_CLUSTER_CONFIGURATION_NAME, "false");
 +
 +            Locator.startLocatorAndDS(port1, logFile, null, props);
 +          }
 +          catch (IOException ex) {
 +            com.gemstone.gemfire.test.dunit.Assert.fail("While starting locator on port " + port1, ex);
 +          }
 +        }
 +      });
 +    vm3.invoke(new SerializableRunnable("Start locator on " + port2) {
 +        public void run() {
 +          File logFile = new File("");
 +          try {
 +            Properties props = new Properties();
 +            props.setProperty("mcast-port", String.valueOf(mcastport));
 +            props.setProperty("locators", locators);
 +            props.setProperty("log-level", LogWriterUtils.getDUnitLogLevel());
 +            props.setProperty("mcast-ttl", "0");
 +            props.setProperty("enable-network-partition-detection", "true");
 +            props.setProperty(DistributionConfig.ENABLE_CLUSTER_CONFIGURATION_NAME, "false");
 +            Locator.startLocatorAndDS(port2, logFile, null, props);
 +          }
 +          catch (IOException ex) {
 +            com.gemstone.gemfire.test.dunit.Assert.fail("While starting locator on port " + port2, ex);
 +          }
 +        }
 +      });
 +
 +    SerializableRunnable connect =
 +      new SerializableRunnable("Connect to " + locators) {
 +          public void run() {
 +            Properties props = new Properties();
 +            props.setProperty("mcast-port", String.valueOf(mcastport));
 +            props.setProperty("locators", locators);
 +            props.setProperty("log-level", LogWriterUtils.getDUnitLogLevel());
 +            props.setProperty("mcast-ttl", "0");
 +            props.setProperty("enable-network-partition-detection", "true");
 +            DistributedSystem.connect(props);
 +          }
 +        };
 +    try {
 +      vm1.invoke(connect);
 +      vm2.invoke(connect);
 +
 +      Properties props = new Properties();
 +      props.setProperty("mcast-port", String.valueOf(mcastport));
 +      props.setProperty("locators", locators);
 +      props.setProperty("log-level", LogWriterUtils.getDUnitLogLevel());
 +      props.setProperty("mcast-ttl", "0");
 +      props.setProperty("enable-network-partition-detection", "true");
 +
 +      system = (InternalDistributedSystem)DistributedSystem.connect(props);
 +      WaitCriterion ev = new WaitCriterion() {
 +        public boolean done() {
 +          try {
 +            return system.getDM().getViewMembers().size() == 5;
 +          }
 +          catch (Exception e) {
 +            com.gemstone.gemfire.test.dunit.Assert.fail("unexpected exception", e);
 +          }
 +          return false; // NOTREACHED
 +        }
 +        public String description() {
 +          return "waiting for 5 members - have " + system.getDM().getViewMembers().size();
 +        }
 +      };
 +      Wait.waitForCriterion(ev, WAIT2_MS, 200, true);
 +      system.disconnect();
 +
 +      SerializableRunnable disconnect =
 +        new SerializableRunnable("Disconnect from " + locators) {
 +            public void run() {
 +              DistributedSystem sys = InternalDistributedSystem.getAnyInstance();
 +              if (sys != null && sys.isConnected()) {
 +                sys.disconnect();
 +              }
 +            }
 +          };
 +      vm1.invoke(disconnect);
 +      vm2.invoke(disconnect);
 +    }
 +    finally {
 +      SerializableRunnable stop = getStopLocatorRunnable();
 +      vm0.invoke(stop);
 +      vm3.invoke(stop);
 +      if (system != null) {
 +        system.disconnect();
 +      }
 +    }
 +  }
 +
 +
 +  /**
 +   * Tests that a VM can connect to a locator that is hosted in its
 +   * own VM.
 +   */
 +  public void testConnectToOwnLocator() throws Exception {
 +    disconnectAllFromDS();
 +    Host host = Host.getHost(0);
 +
 +    port1 =
 +      AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    DistributedTestUtils.deleteLocatorStateFile(port1);
 +    File logFile = new File("");
 +    Locator locator = Locator.startLocator(port1, logFile);
 +    try {
 +
 +    final String locators = NetworkUtils.getServerHostName(host) + "[" + port1 + "]";
 +
 +    Properties props = new Properties();
 +    props.setProperty("mcast-port", "0");
 +    props.setProperty("locators", locators);
 +    props.setProperty(DistributionConfig.ENABLE_CLUSTER_CONFIGURATION_NAME, "false");
 +    system = (InternalDistributedSystem)DistributedSystem.connect(props);
 +    system.disconnect();
 +    } finally {
 +    locator.stop();
 +    }
 +  }
 +
 +  /**
 +   * Tests that a VM cannot connect if the locator has a different
 +   * enable-network-partition-detection setting
 +   */
 +  public void testLocatorForcesDetection() throws Exception {
 +    disconnectAllFromDS();
 +    Host host = Host.getHost(0);
 +    VM vm1 = host.getVM(1);
 +    Locator locator = null;
 +    
 +    try {
 +      port1 = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +      DistributedTestUtils.deleteLocatorStateFile(port1);
 +      final String locators = NetworkUtils.getServerHostName(host) + "[" + port1 + "]";
 +      final Properties properties = new Properties();
 +      properties.put("mcast-port", "0");
 +      properties.put("locators", locators);
 +      properties.put(DistributionConfig.ENABLE_NETWORK_PARTITION_DETECTION_NAME, "true");
 +      properties.put("disable-auto-reconnect", "true");
 +      properties.put(DistributionConfig.ENABLE_CLUSTER_CONFIGURATION_NAME, "false");
 +      File logFile = new File("");
 +      locator = Locator.startLocatorAndDS(port1, logFile, properties);
 +  
 +      final Properties properties2 = new Properties();
 +      properties2.put("mcast-port", "0");
 +      properties2.put("locators", locators);
 +      properties2.put(DistributionConfig.ENABLE_NETWORK_PARTITION_DETECTION_NAME, "false");
 +      properties2.put(DistributionConfig.ENABLE_CLUSTER_CONFIGURATION_NAME, "false");
 +      properties2.put("disable-auto-reconnect", "true");
 +      
 +      vm1.invoke(new SerializableRunnable("try to connect") {
 +        public void run() {
 +          DistributedSystem s = null;
 +          try {
 +            s = DistributedSystem.connect(properties2);
 +            boolean enabled = ((InternalDistributedSystem)s).getConfig().getEnableNetworkPartitionDetection();
 +            s.disconnect();
 +            if (!enabled) {
 +              fail("should not have been able to connect with different enable-network-partition-detection settings");
 +            }
 +          }
 +          catch (GemFireConfigException e) {
 +            fail("should have been able to connect and have enable-network-partion-detection enabled");
 +          }
 +        }
 +      });
 +      
 +      locator.stop();
 +      
 +      // now start the locator with enable-network-partition-detection=false
 +      logFile = new File("");
 +      locator = Locator.startLocatorAndDS(port1, logFile , properties2);
 +  
 +      vm1.invoke(new SerializableRunnable("try to connect") {
 +        public void run() {
 +          DistributedSystem s = null;
 +          try {
 +            s = DistributedSystem.connect(properties);
 +            s.disconnect();
 +            fail("should not have been able to connect with different enable-network-partition-detection settings");
 +          }
 +          catch (GemFireConfigException e) {
 +            // passed
 +          }
 +        }
 +      });
 +      
 +      locator.stop();
 +      locator = null;
 +    }
 +    finally {
 +      if (locator != null) {
 +        locator.stop();
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Tests that a single VM can NOT host multiple locators
 +   */
 +  public void testHostingMultipleLocators() throws Exception {
 +    disconnectAllFromDS();
 +    Host host = Host.getHost(0);
 +    //VM vm = host.getVM(0);
 +    //VM vm1 = host.getVM(1);
 +
 +    port1 =
 +      AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    File logFile1 = new File("");
 +    DistributedTestUtils.deleteLocatorStateFile(port1);
 +    Locator locator1 = Locator.startLocator(port1, logFile1);
 +    
 +    try {
 +
 +    int port2 =
 +      AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    File logFile2 = new File("");
 +
 +    DistributedTestUtils.deleteLocatorStateFile(port2);
 +    
 +    try {
 +      Locator locator2 = Locator.startLocator(port2, logFile2);
 +      fail("expected second locator start to fail.");
 +    } catch (IllegalStateException expected) {
 +    }
 +
 +    final String host0 = NetworkUtils.getServerHostName(host);
 +    final String locators = host0 + "[" + port1 + "]," +
 +                            host0 + "[" + port2 + "]";
 +
 +    SerializableRunnable connect =
 +      new SerializableRunnable("Connect to " + locators) {
 +          public void run() {
 +            Properties props = new Properties();
 +            props.setProperty("mcast-port", "0");
 +            props.setProperty("locators", locators);
 +            props.setProperty("log-level", LogWriterUtils.getDUnitLogLevel());
 +            DistributedSystem.connect(props);
 +          }
 +        };
 +    connect.run();
 +    //vm1.invoke(connect);
 +
 +    SerializableRunnable disconnect =
 +      new SerializableRunnable("Disconnect from " + locators) {
 +          public void run() {
 +            DistributedSystem sys = InternalDistributedSystem.getAnyInstance();
 +            if (sys != null && sys.isConnected()) {
 +              sys.disconnect();
 +            }
 +          }
 +        };
 +
 +    disconnect.run();
 +    //vm1.invoke(disconnect);
 +
 +    } finally {
 +    locator1.stop();
 +    }
 +  }
 +
 +  /**
 +   * Tests starting, stopping, and restarting a locator.  See bug
 +   * 32856.
 +   *
 +   * @since 4.1
 +   */
 +  public void testRestartLocator() throws Exception {
 +    disconnectAllFromDS();
 +    port1 =
 +      AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    DistributedTestUtils.deleteLocatorStateFile(port1);
 +    File logFile = new File("");
 +    File stateFile = new File("locator"+port1+"state.dat");
 +    VM vm0 = Host.getHost(0).getVM(0);
 +    final Properties p = new Properties();
 +    p.setProperty(DistributionConfig.LOCATORS_NAME, Host.getHost(0).getHostName() + "["+port1+"]");
 +    p.setProperty(DistributionConfig.MCAST_PORT_NAME, "0");
 +    p.setProperty(DistributionConfig.ENABLE_CLUSTER_CONFIGURATION_NAME, "false");
 +    if (stateFile.exists()) {
 +      stateFile.delete();
 +    }
 +
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("Starting locator");
 +    Locator locator = Locator.startLocatorAndDS(port1, logFile, p);
 +    try {
 +    
 +    SerializableRunnable connect =
 +      new SerializableRunnable("Connect to locator on port " + port1) {
 +          public void run() {
 +            DistributedSystem.connect(p);
 +          }
 +        };
 +    vm0.invoke(connect);
 +    
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("Stopping locator");
 +    locator.stop();
 +    
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("Starting locator");
 +    locator = Locator.startLocatorAndDS(port1, logFile, p);
 +    
 +    vm0.invoke(new SerializableRunnable("disconnect") {
 +      public void run() {
 +        DistributedSystem.connect(p).disconnect();
 +      }
 +    });
 +    
 +    } finally {
 +    locator.stop();
 +    }
 +
 +  }
 +  
 +  /** return the distributed member id for the ds on this vm */
 +  public static DistributedMember getDistributedMember(Properties props) {
 +    props.put("name", "vm_"+VM.getCurrentVMNum());
 +    DistributedSystem sys = DistributedSystem.connect(props);
 +    sys.getLogWriter().info("<ExpectedException action=add>service failure</ExpectedException>");
 +    sys.getLogWriter().info("<ExpectedException action=add>com.gemstone.gemfire.ConnectException</ExpectedException>");
 +    sys.getLogWriter().info("<ExpectedException action=add>com.gemstone.gemfire.ForcedDisconnectException</ExpectedException>");
 +    return DistributedSystem.connect(props).getDistributedMember();
 +  }
 +  
 +  /** find a running locator and return its distributed member id */
 +  public static DistributedMember getLocatorDistributedMember() {
 +    return (Locator.getLocators().iterator().next())
 +      .getDistributedSystem().getDistributedMember();
 +  }
 +  
 +  /** find the lead member and return its id */
 +  public static DistributedMember getLeadMember() {
 +    DistributedSystem sys = InternalDistributedSystem.getAnyInstance();
 +    return MembershipManagerHelper.getLeadMember(sys);
 +  }
 +
 +  private SerializableRunnable getStopLocatorRunnable() {
 +    return new SerializableRunnable("stop locator") {
 +      public void run() {
 +        MembershipManagerHelper.inhibitForcedDisconnectLogging(false);
 +        Locator loc = Locator.getLocator();
 +        if (loc != null) {
 +          loc.stop();
 +          assertFalse(Locator.hasLocator());
 +        }
 +      }
 +    };
 +  }
 +  
 +  private SerializableRunnable getStartSBLocatorRunnable(final int port, final String name) {
 +    return new SerializableRunnable("Start locator on port " + port) {
 +      public void run() {
 +        File logFile = new File("");
 +        try {
 +          System.setProperty(InternalLocator.LOCATORS_PREFERRED_AS_COORDINATORS, "true");
 +          System.setProperty("p2p.joinTimeout", "1000");
 +          Properties locProps = new Properties();
 +          locProps.put("mcast-port", "0");
 +          locProps.put("log-level", LogWriterUtils.getDUnitLogLevel());
 +          Locator.startLocatorAndDS(port, logFile, locProps);
 +        } catch (IOException ex) {
 +          com.gemstone.gemfire.test.dunit.Assert.fail("While starting locator on port " + port, ex);
 +        }
 +        finally {
 +          System.getProperties().remove(InternalLocator.LOCATORS_PREFERRED_AS_COORDINATORS);
 +          System.getProperties().remove("p2p.joinTimeout");
 +        }
 +      }
 +    };
 +  }
 +  
 +  protected void nukeJChannel(DistributedSystem sys) {
 +    sys.getLogWriter().info("<ExpectedException action=add>service failure</ExpectedException>");
 +    sys.getLogWriter().info("<ExpectedException action=add>com.gemstone.gemfire.ConnectException</ExpectedException>");
 +    sys.getLogWriter().info("<ExpectedException action=add>com.gemstone.gemfire.ForcedDisconnectException</ExpectedException>");
 +    try {
 +      MembershipManagerHelper.crashDistributedSystem(sys);
 +    }
 +    catch (DistributedSystemDisconnectedException se) {
 +      // it's okay for the system to already be shut down
 +    }
 +    sys.getLogWriter().info("<ExpectedException action=remove>service failure</ExpectedException>");
 +    sys.getLogWriter().info("<ExpectedException action=remove>com.gemstone.gemfire.ForcedDisconnectException</ExpectedException>");
 +  }
 +
 +  
 +  //New test hook which blocks before closing channel.
 +  class TestHook implements MembershipTestHook {
 +
 +    volatile boolean unboundedWait = true;
 +    @Override
 +    public void beforeMembershipFailure(String reason, Throwable cause) {
 +      System.out.println("Inside TestHook.beforeMembershipFailure with " + cause);
 +      long giveUp = System.currentTimeMillis() + 30000;
 +      if (cause instanceof ForcedDisconnectException) {
 +        while (unboundedWait && System.currentTimeMillis() < giveUp) {
 +          Wait.pause(1000);
 +        }
 +      } else {
 +        cause.printStackTrace();
 +      }
 +    }
 +
 +    @Override
 +    public void afterMembershipFailure(String reason, Throwable cause) {
 +    }
 +
 +    public void reset() {
 +      unboundedWait = false;
 +    }
 +
 +  }
 +  class MyMembershipListener implements MembershipListener {
 +    boolean quorumLostInvoked;
 +    List<String> suspectReasons = new ArrayList<>(50);
 +    
 +    public void memberJoined(InternalDistributedMember id) {  }
 +    public void memberDeparted(InternalDistributedMember id, boolean crashed) { }
 +    public void memberSuspect(InternalDistributedMember id,
 +        InternalDistributedMember whoSuspected, String reason) {
 +      suspectReasons.add(reason);
 +    }
 +    public void quorumLost(Set<InternalDistributedMember> failures,
 +        List<InternalDistributedMember> remaining) {
 +      quorumLostInvoked = true;
 +      com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("quorumLost invoked in test code");
 +    }
 +  }
 +  
 +  
 +}


[07/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/MultiVMRegionTestCase.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/MultiVMRegionTestCase.java
index d3b065c,0000000..7a4d09e
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/MultiVMRegionTestCase.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/MultiVMRegionTestCase.java
@@@ -1,9177 -1,0 +1,9177 @@@
 +/*
 + * 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.cache30;
 +
 +import java.io.ByteArrayInputStream;
 +import java.io.ByteArrayOutputStream;
 +import java.io.DataInput;
 +import java.io.DataInputStream;
 +import java.io.DataOutput;
 +import java.io.DataOutputStream;
 +import java.io.IOException;
 +import java.io.Serializable;
 +import java.util.ArrayList;
 +import java.util.Arrays;
 +import java.util.Collection;
 +import java.util.Collections;
 +import java.util.Comparator;
 +import java.util.HashMap;
 +import java.util.HashSet;
 +import java.util.Iterator;
 +import java.util.LinkedList;
 +import java.util.List;
 +import java.util.Map;
 +import java.util.Properties;
 +import java.util.Random;
 +import java.util.Set;
 +import java.util.concurrent.LinkedBlockingQueue;
 +
 +import junit.framework.Assert;
 +import junit.framework.AssertionFailedError;
 +
 +import org.apache.logging.log4j.Logger;
 +import org.junit.Ignore;
 +
 +import com.gemstone.gemfire.DataSerializable;
 +import com.gemstone.gemfire.DataSerializer;
 +import com.gemstone.gemfire.Instantiator;
 +import com.gemstone.gemfire.InvalidDeltaException;
 +import com.gemstone.gemfire.LogWriter;
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.AttributesMutator;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheEvent;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheFactory;
 +import com.gemstone.gemfire.cache.CacheListener;
 +import com.gemstone.gemfire.cache.CacheLoader;
 +import com.gemstone.gemfire.cache.CacheLoaderException;
 +import com.gemstone.gemfire.cache.CacheTransactionManager;
 +import com.gemstone.gemfire.cache.CacheWriter;
 +import com.gemstone.gemfire.cache.CacheWriterException;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.EntryEvent;
 +import com.gemstone.gemfire.cache.EntryExistsException;
 +import com.gemstone.gemfire.cache.EntryNotFoundException;
 +import com.gemstone.gemfire.cache.ExpirationAction;
 +import com.gemstone.gemfire.cache.ExpirationAttributes;
 +import com.gemstone.gemfire.cache.InterestPolicy;
 +import com.gemstone.gemfire.cache.LoaderHelper;
 +import com.gemstone.gemfire.cache.Operation;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionAttributes;
 +import com.gemstone.gemfire.cache.RegionEvent;
 +import com.gemstone.gemfire.cache.RegionFactory;
 +import com.gemstone.gemfire.cache.RegionShortcut;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.SubscriptionAttributes;
 +import com.gemstone.gemfire.cache.TimeoutException;
 +import com.gemstone.gemfire.cache.TransactionEvent;
 +import com.gemstone.gemfire.cache.TransactionId;
 +import com.gemstone.gemfire.cache.TransactionListener;
 +import com.gemstone.gemfire.cache.partition.PartitionRegionHelper;
 +import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.cache.util.CacheListenerAdapter;
 +import com.gemstone.gemfire.distributed.internal.DMStats;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
 +import com.gemstone.gemfire.internal.AvailablePortHelper;
 +import com.gemstone.gemfire.internal.HeapDataOutputStream;
 +import com.gemstone.gemfire.internal.InternalDataSerializer;
 +import com.gemstone.gemfire.internal.InternalInstantiator;
 +import com.gemstone.gemfire.internal.Version;
 +import com.gemstone.gemfire.internal.cache.EntryExpiryTask;
 +import com.gemstone.gemfire.internal.cache.ExpiryTask;
 +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
 +import com.gemstone.gemfire.internal.cache.LocalRegion;
 +import com.gemstone.gemfire.internal.cache.PartitionedRegion;
 +import com.gemstone.gemfire.internal.cache.RegionEntry;
 +import com.gemstone.gemfire.internal.cache.TXManagerImpl;
 +import com.gemstone.gemfire.internal.cache.TXStateProxy;
 +import com.gemstone.gemfire.internal.cache.Token;
 +import com.gemstone.gemfire.internal.cache.TombstoneService;
 +import com.gemstone.gemfire.internal.cache.delta.Delta;
 +import com.gemstone.gemfire.internal.cache.versions.RegionVersionHolder;
 +import com.gemstone.gemfire.internal.cache.versions.RegionVersionVector;
 +import com.gemstone.gemfire.internal.cache.versions.VMRegionVersionVector;
 +import com.gemstone.gemfire.internal.cache.versions.VersionTag;
 +import com.gemstone.gemfire.internal.logging.LogService;
 +import com.gemstone.gemfire.internal.offheap.MemoryChunkWithRefCount;
 +import com.gemstone.gemfire.internal.offheap.SimpleMemoryAllocatorImpl;
 +import com.gemstone.gemfire.test.dunit.AsyncInvocation;
 +import com.gemstone.gemfire.test.dunit.DistributedTestCase;
 +import com.gemstone.gemfire.test.dunit.DistributedTestUtils;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.RMIException;
 +import com.gemstone.gemfire.test.dunit.SerializableCallable;
 +import com.gemstone.gemfire.test.dunit.SerializableRunnable;
 +import com.gemstone.gemfire.test.dunit.ThreadUtils;
 +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.IgnoredException;
 +import com.gemstone.gemfire.test.dunit.Invoke;
 +
 +
 +/**
 + * Abstract superclass of {@link Region} tests that involve more than
 + * one VM.
 + */
 +public abstract class MultiVMRegionTestCase extends RegionTestCase {
 +
 +  private static final Logger logger = LogService.getLogger();
 +  
 +  Properties props = new Properties();
 +
 +  final int putRange_1Start = 1;
 +
 +  final int putRange_1End = 5;
 +
 +  final int putRange_2Start = 6;
 +
 +  final int putRange_2End = 10;
 +
 +  final int putRange_3Start = 11;
 +
 +  final int putRange_3End = 15;
 +
 +  final int putRange_4Start = 16;
 +
 +  final int putRange_4End = 20;
 +
 +  final int removeRange_1Start = 2;
 +
 +  final int removeRange_1End = 4;
 +
 +  final int removeRange_2Start = 7;
 +
 +  final int removeRange_2End = 9;
 +
 +  public MultiVMRegionTestCase(String name) {
 +    super(name);
 +  }
 +  
 +  public static void caseTearDown() throws Exception {
 +    disconnectAllFromDS();
 +  }
 +  
 +  @Override
 +  protected final void postTearDownRegionTestCase() throws Exception {
 +    DistributedTestCase.cleanupAllVms();
 +    CCRegion = null;
 +  }
 +
 +  // @todo can be used in tests
 +//  protected CacheSerializableRunnable createRegionTask(final String name) {
 +//    return new CacheSerializableRunnable("Create Region") {
 +//      public void run2() throws CacheException {
 +//        assertNotNull(createRegion(name));
 +//      }
 +//    };
 +//  }
 +
 +
 +  ////////  Test Methods
 +
 +  /**
 +   * This is a for the ConcurrentMap operations.
 +   * 4 VMs are used to
 +   * create the region and operations are performed on one of the nodes
 +   */
 +  public void testConcurrentOperations() throws Exception {
 +    SerializableRunnable createRegion = new CacheSerializableRunnable(
 +    "createRegion") {
 +
 +      public void run2() throws CacheException {
 +        Cache cache = getCache();
 +        RegionAttributes regionAttribs = getRegionAttributes();
 +        cache.createRegion("R1",
 +            regionAttribs);
 +      }
 +    };
 +
 +    Host host = Host.getHost(0);
 +    // create the VM(0 - 4)
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    VM vm3 = host.getVM(3);
 +    vm0.invoke(createRegion);
 +    vm1.invoke(createRegion);
 +    vm2.invoke(createRegion);
 +    vm3.invoke(createRegion);
 +    concurrentMapTest("/R1");
 +  }
 +
 +  /**
 +   * Do putIfAbsent(), replace(Object, Object),
 +   * replace(Object, Object, Object), remove(Object, Object) operations
 +   */
 +  public void concurrentMapTest(final String rName) {
 +    
 +    //String exceptionStr = "";
 +    VM vm0 = Host.getHost(0).getVM(0);
 +    vm0.invoke(new CacheSerializableRunnable("doConcurrentMapOperations") {
 +      public void run2() throws CacheException {
 +        Cache cache = getCache();
 +        final Region pr = cache.getRegion(rName);
 +        assertNotNull(rName + " not created", pr);
 +               
 +        // test successful putIfAbsent
 +        for (int i = putRange_1Start; i <= putRange_1End; i++) {
 +          Object putResult = pr.putIfAbsent(Integer.toString(i),
 +                                            Integer.toString(i));
 +          assertNull("Expected null, but got " + putResult + " for key " + i,
 +                     putResult);
 +        }
 +        int size = pr.size();
 +        assertEquals("Size doesn't return expected value", putRange_1End, size);
 +        assertFalse("isEmpty doesnt return proper state of the region", 
 +                    pr.isEmpty());
 +        
 +        // test unsuccessful putIfAbsent
 +        for (int i = putRange_1Start; i <= putRange_1End; i++) {
 +          Object putResult = pr.putIfAbsent(Integer.toString(i),
 +                                            Integer.toString(i + 1));
 +          assertEquals("for i=" + i, Integer.toString(i), putResult);
 +          assertEquals("for i=" + i, Integer.toString(i), pr.get(Integer.toString(i)));
 +        }
 +        size = pr.size();
 +        assertEquals("Size doesn't return expected value", putRange_1End, size);
 +        assertFalse("isEmpty doesnt return proper state of the region", 
 +                    pr.isEmpty());
 +               
 +        // test successful replace(key, oldValue, newValue)
 +        for (int i = putRange_1Start; i <= putRange_1End; i++) {
 +         boolean replaceSucceeded = pr.replace(Integer.toString(i),
 +                                               Integer.toString(i),
 +                                               "replaced" + i);
 +          assertTrue("for i=" + i, replaceSucceeded);
 +          assertEquals("for i=" + i, "replaced" + i, pr.get(Integer.toString(i)));
 +        }
 +        size = pr.size();
 +        assertEquals("Size doesn't return expected value", putRange_1End, size);
 +        assertFalse("isEmpty doesnt return proper state of the region", 
 +                   pr.isEmpty());
 +               
 +        // test unsuccessful replace(key, oldValue, newValue)
 +        for (int i = putRange_1Start; i <= putRange_2End; i++) {
 +         boolean replaceSucceeded = pr.replace(Integer.toString(i),
 +                                               Integer.toString(i), // wrong expected old value
 +                                               "not" + i);
 +         assertFalse("for i=" + i, replaceSucceeded);
 +         assertEquals("for i=" + i,
 +                      i <= putRange_1End ? "replaced" + i : null,
 +                      pr.get(Integer.toString(i)));
 +        }
 +        size = pr.size();
 +        assertEquals("Size doesn't return expected value", putRange_1End, size);
 +        assertFalse("isEmpty doesnt return proper state of the region", 
 +                   pr.isEmpty());
 +                                    
 +        // test successful replace(key, value)
 +        for (int i = putRange_1Start; i <= putRange_1End; i++) {
 +          Object replaceResult = pr.replace(Integer.toString(i),
 +                                            "twice replaced" + i);
 +          assertEquals("for i=" + i, "replaced" + i, replaceResult);
 +          assertEquals("for i=" + i,
 +                       "twice replaced" + i,
 +                       pr.get(Integer.toString(i)));
 +        }
 +        size = pr.size();
 +        assertEquals("Size doesn't return expected value", putRange_1End, size);
 +        assertFalse("isEmpty doesnt return proper state of the region", 
 +                    pr.isEmpty());
 +                                    
 +        // test unsuccessful replace(key, value)
 +        for (int i = putRange_2Start; i <= putRange_2End; i++) {
 +          Object replaceResult = pr.replace(Integer.toString(i),
 +                                           "thrice replaced" + i);
 +          assertNull("for i=" + i, replaceResult);
 +          assertNull("for i=" + i, pr.get(Integer.toString(i)));
 +        }
 +        size = pr.size();
 +        assertEquals("Size doesn't return expected value", putRange_1End, size);
 +        assertFalse("isEmpty doesnt return proper state of the region", 
 +                    pr.isEmpty());
 +                                    
 +        // test unsuccessful remove(key, value)
 +        for (int i = putRange_1Start; i <= putRange_2End; i++) {
 +          boolean removeResult = pr.remove(Integer.toString(i),
 +                                           Integer.toString(-i));
 +          assertFalse("for i=" + i, removeResult);
 +          assertEquals("for i=" + i,
 +                       i <= putRange_1End ? "twice replaced" + i : null,
 +                       pr.get(Integer.toString(i)));
 +        }
 +        size = pr.size();
 +        assertEquals("Size doesn't return expected value", putRange_1End, size);
 +        assertFalse("isEmpty doesnt return proper state of the region", 
 +                    pr.isEmpty());
 +
 +        // test successful remove(key, value)
 +        for (int i = putRange_1Start; i <= putRange_1End; i++) {
 +          boolean removeResult = pr.remove(Integer.toString(i),
 +                                           "twice replaced" + i);
 +          assertTrue("for i=" + i, removeResult);
 +          assertEquals("for i=" + i, null, pr.get(Integer.toString(i)));
 +        }
 +        size = pr.size();
 +        assertEquals("Size doesn't return expected value", 0, size);
 +        assertTrue("isEmpty doesnt return proper state of the region", 
 +                 pr.isEmpty());
 +        }
 +    });
 +    
 +    
 +    /*
 +     * destroy the Region.
 +     */
 +    vm0.invoke(new CacheSerializableRunnable("destroyRegionOp") {
 +               
 +       public void run2() {
 +         Cache cache = getCache();
 +         Region pr = cache.getRegion(rName);
 +         assertNotNull("Region already destroyed.", pr);
 +         pr.destroyRegion();
 +         assertTrue("Region isDestroyed false", pr.isDestroyed());
 +         assertNull("Region not destroyed.", cache.getRegion(rName));
 +       }
 +     });
 +  }
 +
 +  /**
 +   * Tests that doing a {@link Region#put put} in a distributed region
 +   * one VM updates the value in another VM.
 +   */
 +  public void testDistributedUpdate() {
 +    assertTrue(getRegionAttributes().getScope().isDistributed());
 +
 +    final String name = this.getUniqueName();
 +
 +    SerializableRunnable create =
 +      new CacheSerializableRunnable("Create Region") {
 +          public void run2() throws CacheException {
 +            createRegion(name);
 +          }
 +        };
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +
 +    vm0.invoke(create);
 +    vm1.invoke(create);
 +
 +    final Object key = "KEY";
 +    final Object oldValue = "OLD_VALUE";
 +    final Object newValue = "NEW_VALUE";
 +
 +    SerializableRunnable put =
 +      new CacheSerializableRunnable("Put key/value") {
 +          public void run2() throws CacheException {
 +            Region region =
 +              getRootRegion().getSubregion(name);
 +            region.put(key, oldValue);
 +            flushIfNecessary(region);
 +          }
 +      };
 +
 +    vm0.invoke(put);
 +    vm1.invoke(put);
 +
 +    vm0.invoke(new CacheSerializableRunnable("Update") {
 +        public void run2() throws CacheException {
 +          Region region = getRootRegion().getSubregion(name);
 +          region.put(key, newValue);
 +          flushIfNecessary(region);
 +        }
 +      });
 +
 +    vm1.invoke(new CacheSerializableRunnable("Validate update") {
 +        public void run2() throws CacheException {
 +          Region region = getRootRegion().getSubregion(name);
 +          Region.Entry entry = region.getEntry(key);
 +          assertNotNull(entry);
 +          assertEquals(newValue, entry.getValue());
 +        }
 +      });
 +  }
 +
 +  /**
 +   * Tests that distributed updates are delivered in order
 +   *
 +   * <P>
 +   *
 +   * Note that this test does not make sense for regions that are
 +   * {@link Scope#DISTRIBUTED_NO_ACK} for which we do not guarantee
 +   * the ordering of updates for a single producer/single consumer.
 +   *
 +   * DISABLED 4-16-04 - the current implementation assumes events
 +   * are processed synchronously, which is no longer true.
 +   */
 +  public void _ttestOrderedUpdates() throws Throwable {
 +    if (getRegionAttributes().getScope() ==
 +        Scope.DISTRIBUTED_NO_ACK) {
 +      return;
 +    }
 +
 +    final String name = this.getUniqueName();
 +    final Object key = "KEY";
 +    final int lastNumber = 10;
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +
 +    SerializableRunnable create =
 +      new CacheSerializableRunnable("Create region entry") {
 +          public void run2() throws CacheException {
 +            Region region = createRegion(name);
 +            region.create(key, null);
 +          }
 +        };
 +
 +    vm0.invoke(create);
 +    vm1.invoke(create);
 +
 +    vm1.invoke(new CacheSerializableRunnable("Set listener") {
 +        public void run2() throws CacheException {
 +          Region region = getRootRegion().getSubregion(name);
 +          region.setUserAttribute(new LinkedBlockingQueue());
 +          region.getAttributesMutator().addCacheListener(new
 +            CacheListenerAdapter() {
 +              public void afterUpdate(EntryEvent e) {
 +                Region region2 = e.getRegion();
 +                LinkedBlockingQueue queue =
 +                  (LinkedBlockingQueue) region2.getUserAttribute();
 +                Object value = e.getNewValue();
 +                assertNotNull(value);
 +                try {
 +                  com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("++ Adding " + value);
 +                  queue.put(value);
 +
 +                } catch (InterruptedException ex) {
 +                  com.gemstone.gemfire.test.dunit.Assert.fail("Why was I interrupted?", ex);
 +                }
 +              }
 +            });
 +          flushIfNecessary(region);
 +        }
 +      });
 +    AsyncInvocation ai1 =
 +      vm1.invokeAsync(new CacheSerializableRunnable("Verify") {
 +          public void run2() throws CacheException {
 +            Region region =
 +              getRootRegion().getSubregion(name);
 +            LinkedBlockingQueue queue =
 +              (LinkedBlockingQueue) region.getUserAttribute();
 +            for (int i = 0; i <= lastNumber; i++) {
 +              try {
 +                com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("++ Waiting for " + i);
 +                Integer value = (Integer) queue.take();
 +                com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("++ Got " + value);
 +                assertEquals(i, value.intValue());
 +
 +              } catch (InterruptedException ex) {
 +                com.gemstone.gemfire.test.dunit.Assert.fail("Why was I interrupted?", ex);
 +              }
 +            }
 +          }
 +        });
 +
 +    AsyncInvocation ai0 =
 +      vm0.invokeAsync(new CacheSerializableRunnable("Populate") {
 +          public void run2() throws CacheException {
 +            Region region =
 +              getRootRegion().getSubregion(name);
 +            for (int i = 0; i <= lastNumber; i++) {
 +//              com.gemstone.gemfire.internal.GemFireVersion.waitForJavaDebugger(getLogWriter());
 +              region.put(key, new Integer(i));
 +            }
 +          }
 +        });
 +
 +    ThreadUtils.join(ai0, 30 * 1000);
 +    ThreadUtils.join(ai1, 30 * 1000);
 +
 +    if (ai0.exceptionOccurred()) {
 +      com.gemstone.gemfire.test.dunit.Assert.fail("ai0 failed", ai0.getException());
 +
 +    } else if (ai1.exceptionOccurred()) {
 +      com.gemstone.gemfire.test.dunit.Assert.fail("ai1 failed", ai1.getException());
 +    }
 +  }
 +
 +  /**
 +   * Tests that doing a distributed get results in a
 +   * <code>netSearch</code>.
 +   */
 +  public void testDistributedGet() {
 +    assertTrue(getRegionAttributes().getScope().isDistributed());
 +
 +    final String name = this.getUniqueName();
 +    final Object key = "KEY";
 +    final Object value = "VALUE";
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +
 +    vm0.invoke(new CacheSerializableRunnable("Populate region") {
 +        public void run2() throws CacheException {
 +          Region region = createRegion(name);
 +          region.put(key, value);
 +        }
 +      });
 +
 +    SerializableRunnable get = new CacheSerializableRunnable("Distributed get") {
 +        public void run2() throws CacheException {
 +          Region region = createRegion(name);
 +          assertEquals(value, region.get(key));
 +        }
 +    };
 +
 +
 +    vm1.invoke(get);
 +  }
 +
 +  /**
 +   * Tests that doing a {@link Region#put put} on a distributed region
 +   * in one VM does not effect a region in a different VM that does
 +   * not have that key defined.
 +   */
 +  public void testDistributedPutNoUpdate()
 +    throws InterruptedException {
 +
 +    assertTrue(getRegionAttributes().getScope().isDistributed());
 +
 +    final String name = this.getUniqueName();
 +
 +    SerializableRunnable create =
 +      new CacheSerializableRunnable("Create Region") {
 +          public void run2() throws CacheException {
 +            createRegion(name);
 +          }
 +        };
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +
 +    vm0.invoke(create);
 +    vm1.invoke(create);
 +
 +    Thread.sleep(250);
 +
 +    final Object key = "KEY";
 +    final Object value = "VALUE";
 +
 +    vm0.invoke(new CacheSerializableRunnable("Put key/value") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          region.put(key, value);
 +        }
 +      });
 +
 +    Thread.sleep(250);
 +
 +    vm1.invoke(new CacheSerializableRunnable("Verify no update") {
 +        public void run2() throws CacheException {
 +          Region region = getRootRegion().getSubregion(name);
 +          Region.Entry entry = region.getEntry(key);
 +          if (getRegionAttributes().getDataPolicy().withReplication() ||
 +              getRegionAttributes().getPartitionAttributes() != null) {
 +            assertEquals(value, region.get(key));
 +          }
 +          else {
 +            assertNull(entry);
 +          }
 +        }
 +      });
 +  }
 +
 +  /**
 +   * Two VMs create a region. One populates a region entry.  The other
 +   * VM defines that entry.  The first VM updates the entry.  The
 +   * second VM should see the updated value.
 +   */
 +  public void testDefinedEntryUpdated() {
 +    final String name = this.getUniqueName();
 +    final Object key = "KEY";
 +    final Object oldValue = "OLD_VALUE";
 +    final Object newValue = "NEW_VALUE";
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +
 +    SerializableRunnable create =
 +      new CacheSerializableRunnable("Create Region") {
 +          public void run2() throws CacheException {
 +            createRegion(name);
 +          }
 +        };
 +
 +    vm0.invoke(create);
 +    vm1.invoke(create);
 +
 +    vm0.invoke(new CacheSerializableRunnable("Create and populate") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          region.put(key, oldValue);
 +        }
 +      });
 +    vm1.invoke(new CacheSerializableRunnable("Define entry") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          if (!getRegionAttributes().getDataPolicy().withReplication())
 +            region.create(key, null);
 +        }
 +      });
 +    vm0.invoke(new CacheSerializableRunnable("Update entry") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          region.put(key, newValue);
 +        }
 +      });
 +    Invoke.invokeRepeatingIfNecessary(vm1, new CacheSerializableRunnable("Get entry") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          assertEquals(newValue, region.get(key));
 +        }
 +      }, getRepeatTimeoutMs());
 +  }
 +
 +
 +  /**
 +   * Tests that {@linkplain Region#destroy destroying} an entry is
 +   * propagated to all VMs that define that entry.
 +   */
 +  public void testDistributedDestroy() throws InterruptedException {
 +    assertTrue(getRegionAttributes().getScope().isDistributed());
 +
 +    final String name = this.getUniqueName();
 +
 +    SerializableRunnable create =
 +      new CacheSerializableRunnable("Create Region") {
 +          public void run2() throws CacheException {
 +//DebuggerSupport.waitForJavaDebugger(getLogWriter(), " about to create region");
 +            Region region = createRegion(name);
 +            assertTrue(!region.isDestroyed());
 +            Region root = region.getParentRegion();
 +            assertTrue(!root.isDestroyed());
 +          }
 +        };
 +
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +
 +    vm0.invoke(create);
 +    vm1.invoke(create);
 +    vm2.invoke(create);
 +
 +    final Object key = "KEY";
 +    final Object value = "VALUE";
 +
 +    SerializableRunnable put =
 +      new CacheSerializableRunnable("Put key/value") {
 +          public void run2() throws CacheException {
 +//DebuggerSupport.waitForJavaDebugger(getLogWriter(), " about to put");
 +            Region region =
 +              getRootRegion().getSubregion(name);
 +            region.put(key, value);
 +            assertTrue(!region.isDestroyed());
 +            assertTrue(!region.getParentRegion().isDestroyed());
 +            flushIfNecessary(region);
 +          }
 +        };
 +
 +    vm0.invoke(put);
 +    vm1.invoke(put);
 +    vm2.invoke(put);
 +
 +    SerializableRunnable verifyPut =
 +      new CacheSerializableRunnable("Verify Put") {
 +          public void run2() throws CacheException {
 +            Region root = getRootRegion();
 +            assertTrue(!root.isDestroyed());
 +            Region region = root.getSubregion(name);
 +            assertTrue(!region.isDestroyed());
 +//DebuggerSupport.waitForJavaDebugger(getLogWriter(), " about to get");
 +            assertEquals(value, region.getEntry(key).getValue());
 +          }
 +        };
 +
 +    vm0.invoke(verifyPut);
 +    vm1.invoke(verifyPut);
 +    vm2.invoke(verifyPut);
 +
 +    vm0.invoke(new CacheSerializableRunnable("Destroy Entry") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          region.destroy(key);
 +          flushIfNecessary(region);
 +        }
 +      });
 +
 +    CacheSerializableRunnable verifyDestroy =
 +      new CacheSerializableRunnable("Verify entry destruction") {
 +          public void run2() throws CacheException {
 +            Region root = getRootRegion();
 +            assertTrue(!root.isDestroyed());
 +            Region region = root.getSubregion(name);
 +            assertTrue(!region.isDestroyed());
 +            assertNull(region.getEntry(key));
 +          }
 +        };
 +    vm0.invoke(verifyDestroy);
 +    vm1.invoke(verifyDestroy);
 +    vm2.invoke(verifyDestroy);
 +  }
 +
 +  /**
 +   * Tests that {@linkplain Region#destroy destroying} a region is
 +   * propagated to all VMs that define that region.
 +   */
 +  public void testDistributedRegionDestroy()
 +    throws InterruptedException {
 +
 +    assertTrue(getRegionAttributes().getScope().isDistributed());
 +
 +    final String name = this.getUniqueName();
 +
 +    SerializableRunnable create =
 +      new CacheSerializableRunnable("Create Region") {
 +          public void run2() throws CacheException {
 +            createRegion(name);
 +          }
 +        };
 +
 +    Invoke.invokeInEveryVM(create);
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +
 +    vm0.invoke(new CacheSerializableRunnable("Destroy Region") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          region.destroyRegion();
 +          flushIfNecessary(region);
 +        }
 +      });
 +
 +    Invoke.invokeInEveryVM(new CacheSerializableRunnable("Verify region destruction") {
 +      public void run2() throws CacheException {
 +        WaitCriterion ev = new WaitCriterion() {
 +          public boolean done() {
 +            return getRootRegion().getSubregion(name) == null;
 +          }
 +          public String description() {
 +            return "Waiting for region " + name + " to be destroyed";
 +          }
 +        };
 +        Wait.waitForCriterion(ev, 60 * 1000, 10, true);
 +        Region region = getRootRegion().getSubregion(name);
 +        assertNull(region);
 +      }
 +    });
 +  }
 +
 +  /**
 +   * Tests that a {@linkplain Region#localDestroy} does not effect
 +   * other VMs that define that entry.
 +   */
 +  public void testLocalDestroy() throws InterruptedException {
 +    if (!supportsLocalDestroyAndLocalInvalidate()) {
 +      return;
 +    }
 +    // test not valid for persistBackup region since they have to be
 +    // mirrored KEYS_VALUES
 +    if (getRegionAttributes().getDataPolicy().withPersistence()) return;
 +
 +    assertTrue(getRegionAttributes().getScope().isDistributed());
 +
 +    final String name = this.getUniqueName();
 +
 +    SerializableRunnable create =
 +      new CacheSerializableRunnable("Create Region") {
 +          public void run2() throws CacheException {
 +            createRegion(name);
 +          }
 +        };
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +
 +    vm0.invoke(create);
 +    vm1.invoke(create);
 +
 +    Thread.sleep(250);
 +
 +    final Object key = "KEY";
 +    final Object value = "VALUE";
 +
 +    SerializableRunnable put =
 +      new CacheSerializableRunnable("Put key/value") {
 +          public void run2() throws CacheException {
 +            Region region =
 +              getRootRegion().getSubregion(name);
 +            region.put(key, value);
 +          }
 +        };
 +
 +    vm0.invoke(put);
 +    vm1.invoke(put);
 +
 +    Thread.sleep(250);
 +
 +    vm0.invoke(new CacheSerializableRunnable("Local Destroy Entry") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          region.localDestroy(key);
 +        }
 +      });
 +
 +    Thread.sleep(250);
 +
 +    SerializableRunnable verify =
 +      new CacheSerializableRunnable("Verify entry existence") {
 +          public void run2() throws CacheException {
 +            Region region = getRootRegion().getSubregion(name);
 +            assertNotNull(region.getEntry(key));
 +          }
 +        };
 +    vm1.invoke(verify);
 +  }
 +
 +  /**
 +   * Tests that a {@link Region#localDestroyRegion} is not propagated
 +   * to other VMs that define that region.
 +   */
 +  public void testLocalRegionDestroy()
 +    throws InterruptedException {
 +
 +    assertTrue(getRegionAttributes().getScope().isDistributed());
 +
 +    final String name = this.getUniqueName();
 +
 +    SerializableRunnable create =
 +      new CacheSerializableRunnable("Create Region") {
 +          public void run2() throws CacheException {
 +            createRegion(name);
 +          }
 +        };
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +
 +    vm0.invoke(create);
 +    vm1.invoke(create);
 +
 +    Thread.sleep(250);
 +
 +    vm0.invoke(new CacheSerializableRunnable("Local Destroy Region") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          region.localDestroyRegion();
 +        }
 +      });
 +
 +    Thread.sleep(250);
 +
 +    SerializableRunnable verify =
 +      new CacheSerializableRunnable("Verify region existence") {
 +          public void run2() throws CacheException {
 +            Region region =
 +              getRootRegion().getSubregion(name);
 +            assertNotNull(region);
 +          }
 +        };
 +    vm1.invoke(verify);
 +  }
 +
 +  /**
 +   * Tests that {@linkplain Region#invalidate invalidating} an entry is
 +   * propagated to all VMs that define that entry.
 +   */
 +  public void testDistributedInvalidate() {
 +    assertTrue(getRegionAttributes().getScope().isDistributed());
 +
 +    final String name = this.getUniqueName();
 +
 +    SerializableRunnable create =
 +      new CacheSerializableRunnable("Create Region") {
 +          public void run2() throws CacheException {
 +            createRegion(name);
 +          }
 +        };
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +
 +    vm0.invoke(create);
 +    vm1.invoke(create);
 +
 +    // vm2 is on a different gemfire system
 +    vm2.invoke(create);
 +
 +    final Object key = "KEY";
 +    final Object value = "VALUE";
 +
 +    SerializableRunnable put =
 +      new CacheSerializableRunnable("Put key/value") {
 +          public void run2() throws CacheException {
 +            Region region =
 +              getRootRegion().getSubregion(name);
 +            region.put(key, value);
 +            flushIfNecessary(region);
 +          }
 +        };
 +
 +    vm0.invoke(put);
 +    vm1.invoke(put);
 +    vm2.invoke(put);
 +
 +    vm0.invoke(new CacheSerializableRunnable("Invalidate Entry") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          region.invalidate(key);
 +          flushIfNecessary(region);
 +        }
 +      });
 +
 +    CacheSerializableRunnable verify =
 +      new CacheSerializableRunnable("Verify entry invalidation") {
 +          public void run2() throws CacheException {
 +            Region region =
 +              getRootRegion().getSubregion(name);
 +            Region.Entry entry = region.getEntry(key);
 +            assertNotNull(entry);
 +            if (entry.getValue() != null) {
 +              // changed from severe to fine because it is possible
 +              // for this to return non-null on d-no-ack
 +              // that is was invokeRepeatingIfNecessary is called
 +              com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().fine("invalidated entry has value of " + entry.getValue());
 +            }
 +            assertNull(entry.getValue());
 +          }
 +        };
 +
 +
 +    vm1.invoke(verify);
 +    vm2.invoke(verify);
 +  }
 +
 +  /**
 +   * Tests that {@linkplain Region#invalidate invalidating} an entry
 +   * in multiple VMs does not cause any problems.
 +   */
 +  public void testDistributedInvalidate4() throws InterruptedException {
 +    assertTrue(getRegionAttributes().getScope().isDistributed());
 +
 +    final String name = this.getUniqueName();
 +    final Object key = "KEY";
 +    final Object value = "VALUE";
 +
 +    SerializableRunnable create =
 +      new CacheSerializableRunnable("Create Region") {
 +          public void run2() throws CacheException {
 +            createRegion(name);
 +          }
 +        };
 +
 +    Host host = Host.getHost(0);
 +    int vmCount = host.getVMCount();
 +    for (int i = 0; i < vmCount; i++) {
 +      VM vm = host.getVM(i);
 +      vm.invoke(create);
 +    }
 +
 +    SerializableRunnable put =
 +        new CacheSerializableRunnable("put entry") {
 +            public void run2() throws CacheException {
 +              Region region =
 +                  getRootRegion().getSubregion(name);
 +              region.put(key, value);
 +              flushIfNecessary(region);
 +            }
 +          };
 +
 +      for (int i = 0; i < vmCount; i++) {
 +        VM vm = host.getVM(i);
 +        vm.invoke(put);
 +      }
 +
 +      SerializableRunnable invalidate =
 +      new CacheSerializableRunnable("Invalidate Entry") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          region.invalidate(key);
 +          flushIfNecessary(region);
 +        }
 +      };
 +
 +    for (int i = 0; i < vmCount; i++) {
 +      VM vm = host.getVM(i);
 +      vm.invoke(invalidate);
 +    }
 +
 +    SerializableRunnable verify =
 +      new CacheSerializableRunnable("Verify entry invalidation") {
 +          public void run2() throws CacheException {
 +            Region region =
 +              getRootRegion().getSubregion(name);
 +            Region.Entry entry = region.getEntry(key);
 +            assertNotNull(entry);
 +            assertNull(entry.getValue());
 +          }
 +        };
 +
 +    for (int i = 0; i < vmCount; i++) {
 +      VM vm = host.getVM(i);
 +      vm.invoke(verify);
 +    }
 +  }
 +
 +  /**
 +   * Tests that {@linkplain Region#invalidateRegion invalidating} a
 +   * region is propagated to all VMs that define that entry.
 +   */
 +  public void testDistributedRegionInvalidate()
 +    throws InterruptedException {
 +    if (!supportsSubregions()) {
 +      return;
 +    }
 +    final String name = this.getUniqueName();
 +    final String subname = "sub";
 +    final boolean useSubs = getRegionAttributes().getPartitionAttributes() == null;
 +
 +    SerializableRunnable create =
 +      new CacheSerializableRunnable("Create Region") {
 +          public void run2() throws CacheException {
 +            Region region;
 +            region = createRegion(name);
 +            if (useSubs) {
 +              region.createSubregion(subname, region.getAttributes());
 +            }
 +          }
 +        };
 +
 +    Invoke.invokeInEveryVM(create);
 +
 +    final Object key = "KEY";
 +    final Object value = "VALUE";
 +    final Object key2 = "KEY2";
 +    final Object value2 = "VALUE2";
 +
 +    SerializableRunnable put =
 +      new CacheSerializableRunnable("Put key/value") {
 +          public void run2() throws CacheException {
 +            Region region =
 +              getRootRegion().getSubregion(name);
 +            region.put(key, value);
 +            region.put(key2, value2);
 +            flushIfNecessary(region);
 +
 +            if (useSubs) {
 +              Region subregion = region.getSubregion(subname);
 +              subregion.put(key, value);
 +              subregion.put(key2, value2);
 +              flushIfNecessary(subregion);
 +            }
 +          }
 +        };
 +
 +    Invoke.invokeInEveryVM(put);
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +
 +    vm0.invoke(new CacheSerializableRunnable("Invalidate Region") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          region.invalidateRegion();
 +        }
 +      });
 +
 +    CacheSerializableRunnable verify =
 +      new CacheSerializableRunnable("Verify region invalidation") {
 +          public void run2() throws CacheException {
 +            Region region =
 +              getRootRegion().getSubregion(name);
 +            {
 +              Region.Entry entry = region.getEntry(key);
 +              assertNotNull(entry);
 +              Object v = entry.getValue();
 +              assertNull("Expected null but was " + v, v);
 +
 +              entry = region.getEntry(key2);
 +              assertNotNull(entry);
 +              assertNull(entry.getValue());
 +            }
 +
 +            if (useSubs) {
 +              Region subregion = region.getSubregion(subname);
 +              Region.Entry entry = subregion.getEntry(key);
 +              assertNotNull(entry);
 +              assertNull(entry.getValue());
 +
 +              entry = subregion.getEntry(key2);
 +              assertNotNull(entry);
 +              assertNull(entry.getValue());
 +            }
 +          }
 +        };
 +
 +    Invoke.invokeInEveryVMRepeatingIfNecessary(verify, getRepeatTimeoutMs());
 +  }
 +
 +  /**
 +   * Tests that a {@link CacheListener} is invoked in a remote VM.
 +   */
 +  public void testRemoteCacheListener() throws InterruptedException {
 +    assertTrue(getRegionAttributes().getScope().isDistributed());
 +
 +    final String name = this.getUniqueName();
 +    final Object key = "KEY";
 +    final Object oldValue = "OLD_VALUE";
 +    final Object newValue = "NEW_VALUE";
 +//    final Object key2 = "KEY2";
 +//    final Object value2 = "VALUE2";
 +
 +    SerializableRunnable populate =
 +      new CacheSerializableRunnable("Create Region and Put") {
 +          public void run2() throws CacheException {
 +            Region region = createRegion(name);
 +            region.put(key, oldValue);
 +          }
 +        };
 +
 +    Host host = Host.getHost(0);
 +    final VM vm0 = host.getVM(0);
 +    final VM vm1 = host.getVM(1);
 +
 +    vm0.invoke(populate);
 +    vm1.invoke(populate);
 +
 +    vm1.invoke(new CacheSerializableRunnable("Set listener") {
 +        public void run2() throws CacheException {
 +          final Region region =
 +            getRootRegion().getSubregion(name);
 +          listener = new TestCacheListener() {
 +              public void afterUpdate2(EntryEvent event) {
 +                assertEquals(Operation.UPDATE, event.getOperation());
 +                assertEquals(region, event.getRegion());
 +                assertTrue(event.getOperation().isDistributed());
 +                assertFalse(event.getOperation().isExpiration());
 +                assertTrue(event.isOriginRemote());
 +                assertEquals(event.getCallbackArgument(), event.getDistributedMember());
 +                assertEquals(key, event.getKey());
 +                assertEquals(oldValue, event.getOldValue());
 +                assertEquals(newValue, event.getNewValue());
 +                assertFalse(event.getOperation().isLoad());
 +                assertFalse(event.getOperation().isLocalLoad());
 +                assertFalse(event.getOperation().isNetLoad());
 +                assertFalse(event.getOperation().isNetSearch());
 +                if (event.getRegion().getAttributes().getOffHeap()) {
 +                  // since off heap always serializes the old value is serialized and available
 +                  assertEquals(oldValue, event.getSerializedOldValue().getDeserializedValue());
 +                } else {
 +                  assertEquals(null, event.getSerializedOldValue()); // since it was put originally in this VM
 +                }
 +                DataInputStream dis = new DataInputStream(new ByteArrayInputStream(event.getSerializedNewValue().getSerializedValue()));
 +                try {
 +                  assertEquals(newValue, DataSerializer.readObject(dis));
 +                } catch (Exception e) {
 +                  com.gemstone.gemfire.test.dunit.Assert.fail("Unexpected Exception", e);
 +                }
 +              }
 +            };
 +          region.getAttributesMutator().addCacheListener(listener);
 +        }
 +      });
 +
 +    // I see no reason to pause here.
 +    // The test used to pause here but only if no-ack.
 +    // But we have no operations to wait for.
 +    // The last thing we did was install a listener in vm1
 +    // and it is possible that vm0 does not yet know we have
 +    // a listener but for this test it does not matter.
 +    // So I'm commenting out the following pause:
 +    //pauseIfNecessary();
 +    // If needed then do a flushIfNecessary(region) after adding the cache listener
 +
 +    vm0.invoke(new CacheSerializableRunnable("Update") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          region.put(key, newValue, getSystem().getDistributedMember());
 +        }
 +      });
 +
 +    vm1.invoke(new CacheSerializableRunnable("Verify Update") {
 +        public void run2() throws CacheException {
 +          listener.waitForInvocation(3000, 10);
 +
 +          // Setup listener for next test
 +          final Region region =
 +            getRootRegion().getSubregion(name);
 +          listener = new TestCacheListener() {
 +              public void afterInvalidate2(EntryEvent event) {
 +                assertEquals(Operation.INVALIDATE, event.getOperation());
 +                assertEquals(region, event.getRegion());
 +                assertTrue(event.getOperation().isDistributed());
 +                assertFalse(event.getOperation().isExpiration());
 +                assertTrue(event.isOriginRemote());
 +                assertEquals(event.getCallbackArgument(), event.getDistributedMember());
 +                assertEquals(key, event.getKey());
 +                assertEquals(newValue, event.getOldValue());
 +                assertNull(event.getNewValue());
 +                assertFalse(event.getOperation().isLoad());
 +                assertFalse(event.getOperation().isLocalLoad());
 +                assertFalse(event.getOperation().isNetLoad());
 +                assertFalse(event.getOperation().isNetSearch());
 +                assertNull(event.getSerializedNewValue());
 +                DataInputStream dis = new DataInputStream(new ByteArrayInputStream(event.getSerializedOldValue().getSerializedValue()));
 +                try {
 +                  assertEquals(newValue, DataSerializer.readObject(dis));
 +                } catch (Exception e) {
 +                  com.gemstone.gemfire.test.dunit.Assert.fail("Unexpected Exception", e);
 +                }
 +              }
 +            };
 +          region.getAttributesMutator().addCacheListener(listener);
 +        }
 +      });
 +
 +    vm0.invoke(new CacheSerializableRunnable("Invalidate") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          region.invalidate(key, getSystem().getDistributedMember());
 +        }
 +      });
 +
 +    vm1.invoke(new CacheSerializableRunnable("Verify Invalidate") {
 +        public void run2() throws CacheException {
 +          listener.waitForInvocation(3000, 10);
 +
 +          // Setup listener for next test
 +          final Region region =
 +            getRootRegion().getSubregion(name);
 +          listener = new TestCacheListener() {
 +              public void afterDestroy2(EntryEvent event) {
 +                assertTrue(event.getOperation().isDestroy());
 +                assertEquals(region, event.getRegion());
 +                assertTrue(event.getOperation().isDistributed());
 +                assertFalse(event.getOperation().isExpiration());
 +                assertTrue(event.isOriginRemote());
 +                assertEquals(event.getCallbackArgument(), event.getDistributedMember());
 +                assertEquals(key, event.getKey());
 +                assertNull(event.getOldValue());
 +                assertNull(event.getNewValue());
 +                assertFalse(event.getOperation().isLoad());
 +                assertFalse(event.getOperation().isLocalLoad());
 +                assertFalse(event.getOperation().isNetLoad());
 +                assertFalse(event.getOperation().isNetSearch());
 +                assertNull(event.getSerializedOldValue());
 +                assertNull(event.getSerializedNewValue());
 +              }
 +            };
 +          region.getAttributesMutator().addCacheListener(listener);
 +        }
 +      });
 +
 +    vm0.invoke(new CacheSerializableRunnable("Destroy") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          region.destroy(key, getSystem().getDistributedMember());
 +        }
 +      });
 +
 +    vm1.invoke(new CacheSerializableRunnable("Verify Destroy") {
 +        public void run2() throws CacheException {
 +          listener.waitForInvocation(3000, 10);
 +
 +          // Setup listener for next test
 +          final Region region =
 +            getRootRegion().getSubregion(name);
 +          listener = new TestCacheListener() {
 +              public void afterRegionInvalidate2(RegionEvent event) {
 +                assertEquals(Operation.REGION_INVALIDATE, event.getOperation());
 +                assertEquals(region, event.getRegion());
 +                assertTrue(event.getOperation().isDistributed());
 +                assertFalse(event.getOperation().isExpiration());
 +                assertTrue(event.isOriginRemote());
 +                assertEquals(event.getCallbackArgument(), event.getDistributedMember());
 +              }
 +            };
 +          region.getAttributesMutator().addCacheListener(listener);
 +        }
 +      });
 +
 +    vm0.invoke(new CacheSerializableRunnable("Invalidate Region") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          region.invalidateRegion(getSystem().getDistributedMember());
 +        }
 +      });
 +
 +    vm1.invoke(new CacheSerializableRunnable("Verify Invalidate Region") {
 +        public void run2() throws CacheException {
 +          listener.waitForInvocation(3000, 10);
 +
 +          // Setup listener for next test
 +          final Region region =
 +            getRootRegion().getSubregion(name);
 +          listener = new TestCacheListener() {
 +              public void afterRegionDestroy2(RegionEvent event) {
 +                assertEquals(Operation.REGION_DESTROY, event.getOperation());
 +                assertEquals(region, event.getRegion());
 +                assertTrue(event.getOperation().isDistributed());
 +                assertFalse(event.getOperation().isExpiration());
 +                assertTrue(event.isOriginRemote());
 +                assertEquals(event.getCallbackArgument(), event.getDistributedMember());
 +              }
 +            };
 +          region.getAttributesMutator().addCacheListener(listener);
 +        }
 +      });
 +
 +    vm0.invoke(new CacheSerializableRunnable("Destroy Region") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          region.destroyRegion(getSystem().getDistributedMember());
 +        }
 +      });
 +
 +    vm1.invoke(new CacheSerializableRunnable("Verify Destroy Region") {
 +        public void run2() throws CacheException {
 +          listener.waitForInvocation(3000, 10);
 +        }
 +      });
 +  }
 +
 +
 +  /**
 +   * Tests that a {@link CacheListener} is invoked in a remote VM.
 +   */
 +  public void testRemoteCacheListenerInSubregion() throws InterruptedException {
 +    if (!supportsSubregions()) {
 +      return;
 +    }
 +    assertTrue(getRegionAttributes().getScope().isDistributed());
 +
 +    final String name = this.getUniqueName();
 +
 +    SerializableRunnable create =
 +      new CacheSerializableRunnable("Create Region") {
 +          public void run2() throws CacheException {
 +            createRegion(name);
 +          }
 +        };
 +
 +    Host host = Host.getHost(0);
 +    final VM vm0 = host.getVM(0);
 +    final VM vm1 = host.getVM(1);
 +
 +    vm0.invoke(new CacheSerializableRunnable("Create Root") {
 +      public void run2() throws CacheException {
 +        createRootRegion();
 +      }
 +    });
 +
 +    vm1.invoke(create);
 +
 +    vm1.invoke(new CacheSerializableRunnable("Set listener") {
 +        public void run2() throws CacheException {
 +          final Region region =
 +            getRootRegion().getSubregion(name);
 +          listener = new TestCacheListener() {
 +              public void afterRegionInvalidate2(RegionEvent event) {
 +                assertEquals(Operation.REGION_INVALIDATE, event.getOperation());
 +                assertEquals(region, event.getRegion());
 +                assertTrue(event.getOperation().isDistributed());
 +                assertFalse(event.getOperation().isExpiration());
 +                assertTrue(event.isOriginRemote());
 +                assertEquals(event.getCallbackArgument(), event.getDistributedMember());
 +              }
 +            };
 +          region.getAttributesMutator().addCacheListener(listener);
 +        }
 +      });
 +
 +    vm0.invoke(new CacheSerializableRunnable("Invalidate Root Region") {
 +        public void run2() throws CacheException {
 +          getRootRegion().invalidateRegion(getSystem().getDistributedMember());
 +        }
 +      });
 +
 +    vm1.invoke(new CacheSerializableRunnable("Verify Invalidate Region") {
 +        public void run2() throws CacheException {
 +          listener.waitForInvocation(3000, 10);
 +
 +          // Setup listener for next test
 +          final Region region =
 +            getRootRegion().getSubregion(name);
 +          listener = new TestCacheListener() {
 +              public void afterRegionDestroy2(RegionEvent event) {
 +                assertEquals(Operation.REGION_DESTROY, event.getOperation());
 +                assertEquals(region, event.getRegion());
 +                assertTrue(event.getOperation().isDistributed());
 +                assertFalse(event.getOperation().isExpiration());
 +                assertTrue(event.isOriginRemote());
 +                assertEquals(event.getCallbackArgument(), event.getDistributedMember());
 +              }
 +            };
 +          region.getAttributesMutator().addCacheListener(listener);
 +        }
 +      });
 +
 +    vm0.invoke(new CacheSerializableRunnable("Destroy Root Region") {
 +        public void run2() throws CacheException {
 +          getRootRegion().destroyRegion(getSystem().getDistributedMember());
 +        }
 +      });
 +
 +    vm1.invoke(new CacheSerializableRunnable("Verify Destroy Region") {
 +        public void run2() throws CacheException {
 +          listener.waitForInvocation(3000, 10);
 +        }
 +      });
 +  }
 +
 +
 +  /**
 +   * Indicate whether this region supports netload
 +   * @return true if it supports netload
 +   */
 +  protected boolean supportsNetLoad() {
 +    return true;
 +  }
 +  
 +  /**
 +   * Tests that a {@link CacheLoader} is invoked in a remote VM.  This
 +   * essentially tests <code>netLoad</code>.
 +   */
 +  public void testRemoteCacheLoader() throws InterruptedException {
 +    if (!supportsNetLoad()) {
 +      return;
 +    }
 +    assertTrue(getRegionAttributes().getScope().isDistributed());
 +
 +    final String name = this.getUniqueName();
 +    final Object key = "KEY";
 +    final Object value = "VALUE";
 +
 +    SerializableRunnable create =
 +      new CacheSerializableRunnable("Create Region") {
 +          public void run2() throws CacheException {
 +            createRegion(name);
 +          }
 +        };
 +
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +
 +    vm0.invoke(create);
 +    vm1.invoke(create);
 +
 +
 +    vm1.invoke(new CacheSerializableRunnable("Set CacheLoader") {
 +        public void run2() throws CacheException {
 +          final Region region =
 +            getRootRegion().getSubregion(name);
 +          loader = new TestCacheLoader() {
 +              public Object load2(LoaderHelper helper)
 +                throws CacheLoaderException {
 +                assertEquals(region, helper.getRegion());
 +                assertEquals(key, helper.getKey());
 +                assertNull(helper.getArgument());
 +
 +                return value;
 +              }
 +            };
 +          region.getAttributesMutator().setCacheLoader(loader);
 +        }
 +      });
 +
 +    vm0.invoke(new CacheSerializableRunnable("Remote load") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          assertEquals(value, region.get(key));
 +        }
 +      });
 +
 +    vm1.invoke(new SerializableRunnable("Verify loader") {
 +        public void run() {
 +          assertTrue(loader.wasInvoked());
 +        }
 +      });
 +  }
 +
 +  /**
 +   * Tests that the parameter passed to a remote {@link CacheLoader}
 +   * is actually passed.
 +   */
 +  public void testRemoteCacheLoaderArg() throws InterruptedException {
 +    if (!supportsNetLoad()) {
 +      return;
 +    }
 +    assertTrue(getRegionAttributes().getScope().isDistributed());
 +
 +    final String name = this.getUniqueName();
 +    final Object key = "KEY";
 +    final Object value = "VALUE";
 +    final String arg = "ARG";
 +
 +    SerializableRunnable create =
 +      new CacheSerializableRunnable("Create Region") {
 +          public void run2() throws CacheException {
 +            createRegion(name);
 +            // Can't test non-Serializable callback argument here
 +            // because netLoad will not occur because there are no
 +            // other members with the region defined when it is
 +            // created.  Hooray for intelligent messaging.
 +          }
 +        };
 +
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +
 +    vm0.invoke(create);
 +    vm1.invoke(create);
 +
 +    vm1.invoke(new CacheSerializableRunnable("Set CacheLoader") {
 +        public void run2() throws CacheException {
 +          final Region region =
 +            getRootRegion().getSubregion(name);
 +          loader = new TestCacheLoader() {
 +              public Object load2(LoaderHelper helper)
 +                throws CacheLoaderException {
 +                assertEquals(region, helper.getRegion());
 +                assertEquals(key, helper.getKey());
 +                assertEquals(arg, helper.getArgument());
 +
 +                return value;
 +              }
 +            };
 +          region.getAttributesMutator().setCacheLoader(loader);
 +          flushIfNecessary(region);
 +        }
 +      });
 +
 +    vm0.invoke(new CacheSerializableRunnable("Remote load") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +
 +          try {
 +            // Use a non-serializable arg object
 +            region.get(key, new Object() { });
 +            fail("Should have thrown an IllegalArgumentException");
 +
 +          } catch (IllegalArgumentException ex) {
 +            // pass...
 +          }
 +          assertNull(region.getEntry(key));
 +          try {
 +           assertEquals(value, region.get(key, arg));
 +          }
 +          catch(IllegalArgumentException e) {}
 +       }
 +      });
 +
 +    vm1.invoke(new SerializableRunnable("Verify loader") {
 +        public void run() {
 +          assertTrue(loader.wasInvoked());
 +        }
 +      });
 +  }
 +
 +  /**
 +   * Tests that a remote {@link CacheLoader} that throws a {@link
 +   * CacheLoaderException} results is propagated back to the caller.
 +   */
 +  public void testRemoteCacheLoaderException() throws InterruptedException {
 +    if (!supportsNetLoad()) {
 +      return;
 +    }
 +    assertTrue(getRegionAttributes().getScope().isDistributed());
 +
 +    final String name = this.getUniqueName();
 +    final Object key = "KEY";
 +//    final Object value = "VALUE";
 +
 +    SerializableRunnable create =
 +      new CacheSerializableRunnable("Create Region") {
 +          public void run2() throws CacheException {
 +            createRegion(name);
 +          }
 +        };
 +
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +
 +    vm0.invoke(create);
 +    vm1.invoke(create);
 +
 +    vm1.invoke(new CacheSerializableRunnable("Set CacheLoader") {
 +        public void run2() throws CacheException {
 +          final Region region =
 +            getRootRegion().getSubregion(name);
 +          loader = new TestCacheLoader() {
 +              public Object load2(LoaderHelper helper)
 +                throws CacheLoaderException {
 +                assertEquals(region, helper.getRegion());
 +                assertEquals(key, helper.getKey());
 +                assertNull(helper.getArgument());
 +
 +                String s = "Test Exception";
 +                throw new CacheLoaderException(s);
 +              }
 +            };
 +          region.getAttributesMutator().setCacheLoader(loader);
 +          flushIfNecessary(region);
 +        }
 +      });
 +
 +    vm0.invoke(new CacheSerializableRunnable("Remote load") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          try {
 +            region.get(key);
 +            fail("Should have thrown a CacheLoaderException");
 +
 +          } catch (CacheLoaderException ex) {
 +            // pass...
 +          }
 +        }
 +      });
 +
 +    vm1.invoke(new SerializableRunnable("Verify loader") {
 +        public void run() {
 +          assertTrue(loader.wasInvoked());
 +        }
 +      });
 +  }
 +
 +
 +  public void testCacheLoaderWithNetSearch() throws CacheException {
 +    if (!supportsNetLoad()) {
 +      return;
 +    }
 +    // some tests use mirroring by default (e.g. persistBackup regions)
 +    // if so, then this test won't work right
 +    if (getRegionAttributes().getDataPolicy().withReplication()
 +        || getRegionAttributes().getDataPolicy().isPreloaded()) {
 +      return;
 +    }
 +
 +    final String name = this.getUniqueName();
 +    final Object key = this.getUniqueName();
 +    final Object value = new Integer(42);
 +
 +    Host host = Host.getHost(0);
 +    // use vm on other gemfire system
 +    VM vm1 = host.getVM(1);
 +    vm1.invoke(new CacheSerializableRunnable("set remote value") {
 +      public void run2() throws CacheException {
 +//        final TestCacheLoader remoteloader = new TestCacheLoader() {
 +//            public Object load2(LoaderHelper helper)
 +//              throws CacheLoaderException {
 +//
 +//              assertEquals(key, helper.getKey());
 +//              assertEquals(name, helper.getRegion().getName());
 +//              return value;
 +//            }
 +//          };
 +//
 +//        AttributesFactory factory =
 +//          new AttributesFactory(getRegionAttributes());
 +//        factory.setCacheLoader(remoteloader);
 +        Region rgn = createRegion(name);
 +        rgn.put(key, value);
 +        flushIfNecessary(rgn);
 +      }
 +    });
 +
 +    final TestCacheLoader loader1 = new TestCacheLoader() {
 +        public Object load2(LoaderHelper helper)
 +          throws CacheLoaderException {
 +
 +          assertEquals(key, helper.getKey());
 +          assertEquals(name, helper.getRegion().getName());
 +
 +          try {
 +            helper.getRegion().getAttributes();
 +            Object result = helper.netSearch(false);
 +            assertEquals(value, result);
 +            return result;
 +          } catch (TimeoutException ex) {
 +            com.gemstone.gemfire.test.dunit.Assert.fail("Why did I time out?", ex);
 +          }
 +          return null;
 +        }
 +      };
 +
 +    AttributesFactory f = new AttributesFactory(getRegionAttributes());
 +    f.setCacheLoader(loader1);
 +    Region region =
 +      createRegion(name, f.create());
 +
 +    loader1.wasInvoked();
 +
 +    Region.Entry entry = region.getEntry(key);
 +    assertNull(entry);
 +    region.create(key, null);
 +
 +    entry = region.getEntry(key);
 +    assertNotNull(entry);
 +    assertNull(entry.getValue());
 +
 +    // make sure value is still there in vm1
 +    vm1.invoke(new CacheSerializableRunnable("verify remote value") {
 +      public void run2() throws CacheException {
 +        Region rgn = getRootRegion().getSubregion(name);
 +        assertEquals(value, rgn.getEntry(key).getValue());
 +      }
 +    });
 +
 +//    com.gemstone.gemfire.internal.util.DebuggerSupport.waitForJavaDebugger(getLogWriter());
 +    assertEquals(value, region.get(key));
 +    // if global scope, then a netSearch is done BEFORE the loader is invoked,
 +    // so we get the value but the loader is never invoked.
 +    if (region.getAttributes().getScope().isGlobal()) {
 +      assertTrue(!loader1.wasInvoked());
 +    }
 +    else {
 +      assertTrue(loader1.wasInvoked());
 +    }
 +    assertEquals(value, region.getEntry(key).getValue());
 +  }
 +
 +
 +  public void testCacheLoaderWithNetLoad() throws CacheException {
 +
 +
 +    // replicated regions and partitioned regions make no sense for this
 +    // test
 +    if (getRegionAttributes().getDataPolicy().withReplication() ||
 +        getRegionAttributes().getDataPolicy().isPreloaded() ||
 +        getRegionAttributes().getPartitionAttributes() != null)
 +    {
 +      return;
 +    }
 +
 +    final String name = this.getUniqueName();
 +    final Object key = this.getUniqueName();
 +    final Object value = new Integer(42);
 +
 +    Host host = Host.getHost(0);
 +    // use vm on other gemfire system
 +    VM vm1 = host.getVM(1);
 +    vm1.invoke(new CacheSerializableRunnable("set up remote loader") {
 +      public void run2() throws CacheException {
 +        final TestCacheLoader remoteloader = new TestCacheLoader() {
 +            public Object load2(LoaderHelper helper)
 +              throws CacheLoaderException {
 +
 +              assertEquals(key, helper.getKey());
 +              assertEquals(name, helper.getRegion().getName());
 +              return value;
 +            }
 +          };
 +
 +        AttributesFactory factory =
 +          new AttributesFactory(getRegionAttributes());
 +        factory.setCacheLoader(remoteloader);
 +        createRegion(name, factory.create());
 +      }
 +    });
 +
 +    final TestCacheLoader loader1 = new TestCacheLoader() {
 +        public Object load2(LoaderHelper helper)
 +          throws CacheLoaderException {
 +
 +          assertEquals(key, helper.getKey());
 +          assertEquals(name, helper.getRegion().getName());
 +
 +          try {
 +            helper.getRegion().getAttributes();
 +            Object result = helper.netSearch(true);
 +            assertEquals(value, result);
 +            return result;
 +          } catch (TimeoutException ex) {
 +            com.gemstone.gemfire.test.dunit.Assert.fail("Why did I time out?", ex);
 +          }
 +          return null;
 +        }
 +      };
 +
 +    AttributesFactory f = new AttributesFactory(getRegionAttributes());
 +    f.setCacheLoader(loader1);
 +    Region region = createRegion(name, f.create());
 +
 +    loader1.wasInvoked();
 +
 +    Region.Entry entry = region.getEntry(key);
 +    assertNull(entry);
 +
 +    region.create(key, null);
 +
 +    entry = region.getEntry(key);
 +    assertNotNull(entry);
 +    assertNull(entry.getValue());
 +
 +//    com.gemstone.gemfire.internal.util.DebuggerSupport.waitForJavaDebugger(getLogWriter());
 +    assertEquals(value, region.get(key));
 +
 +    assertTrue(loader1.wasInvoked());
 +    assertEquals(value, region.getEntry(key).getValue());
 +  }
 +
 +
 +  /**
 +   * Tests that {@link Region#get} returns <code>null</code> when
 +   * there is no remote loader.
 +   */
 +  public void testNoRemoteCacheLoader() throws InterruptedException {
 +    assertTrue(getRegionAttributes().getScope().isDistributed());
 +
 +    final String name = this.getUniqueName();
 +    final Object key = "KEY";
 +//    final Object value = "VALUE";
 +
 +    SerializableRunnable create =
 +      new CacheSerializableRunnable("Create Region") {
 +          public void run2() throws CacheException {
 +            createRegion(name);
 +          }
 +        };
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +
 +    vm0.invoke(create);
 +    vm1.invoke(create);
 +
 +    vm0.invoke(new CacheSerializableRunnable("Remote load") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          assertNull(region.get(key));
 +        }
 +      });
 +  }
 +
 +  /**
 +   * Tests that a remote <code>CacheLoader</code> is not invoked if
 +   * the remote region has an invalid entry (that is, a key, but no
 +   * value).
 +   */
 +  public void testNoLoaderWithInvalidEntry() {
 +    if (!supportsNetLoad()) {
 +      return;
 +    }
 +    final String name = this.getUniqueName();
 +    final Object key = "KEY";
 +    final Object value = "VALUE";
 +
 +    SerializableRunnable create =
 +      new CacheSerializableRunnable("Create Region") {
 +          public void run2() throws CacheException {
 +            Region region = createRegion(name);
 +            loader = new TestCacheLoader() {
 +                public Object load2(LoaderHelper helper)
 +                  throws CacheLoaderException {
 +
 +                  return value;
 +                }
 +              };
 +            region.getAttributesMutator().setCacheLoader(loader);
 +          }
 +        };
 +
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +
 +    vm0.invoke(create);
 +    vm1.invoke(create);
 +
 +    vm1.invoke(new CacheSerializableRunnable("Create invalid entry") {
 +      public void run2() throws CacheException {
 +        Region region =
 +          getRootRegion().getSubregion(name);
 +          region.create(key, null);
 +        }
 +      });
 +
 +    vm0.invoke(new CacheSerializableRunnable("Remote get") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +// DebuggerSupport.waitForJavaDebugger(getLogWriter());
 +          assertEquals(value, region.get(key));
 +          assertTrue(loader.wasInvoked());
 +        }
 +      });
 +
 +    vm1.invoke(new SerializableRunnable("Verify loader") {
 +        public void run() {
 +          assertFalse(loader.wasInvoked());
 +        }
 +      });
 +  }
 +
 +  /**
 +   * Tests that a remote {@link CacheWriter} is invoked and that
 +   * <code>CacheWriter</code> arguments and {@link
 +   * CacheWriterException}s are propagated appropriately.
 +   */
 +  public void testRemoteCacheWriter() throws InterruptedException {
 +    assertTrue(getRegionAttributes().getScope().isDistributed());
 +
 +    final String name = this.getUniqueName();
 +    final Object key = "KEY";
 +    final Object oldValue = "OLD_VALUE";
 +    final Object newValue = "NEW_VALUE";
 +    final Object arg = "ARG";
 +    final Object exception = "EXCEPTION";
 +
 +    final Object key2 = "KEY2";
 +    final Object value2 = "VALUE2";
 +
 +    SerializableRunnable create =
 +      new CacheSerializableRunnable("Create Region") {
 +          public void run2() throws CacheException {
 +            Region region = createRegion(name);
 +
 +            // Put key2 in the region before any callbacks are
 +            // registered, so it can be destroyed later
 +            region.put(key2, value2);
 +            assertEquals(1, region.size());
 +            if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
 +              GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
 +              SimpleMemoryAllocatorImpl ma = (SimpleMemoryAllocatorImpl) gfc.getOffHeapStore();
 +              LocalRegion reRegion;
 +              reRegion = (LocalRegion) region;
 +              RegionEntry re = reRegion.getRegionEntry(key2);
 +              MemoryChunkWithRefCount mc = (MemoryChunkWithRefCount) re._getValue();
 +              assertEquals(1, mc.getRefCount());
 +              assertEquals(1, ma.getStats().getObjects());
 +            }
 +          }
 +        };
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +
 +    vm0.invoke(create);
 +    vm1.invoke(create);
 +
 +    ////////  Create
 +
 +    vm1.invoke(new CacheSerializableRunnable("Set Writer") {
 +        public void run2() throws CacheException {
 +          final Region region =
 +            getRootRegion().getSubregion(name);
 +          writer = new TestCacheWriter() {
 +              public void beforeCreate2(EntryEvent event)
 +                throws CacheWriterException {
 +
 +                if (exception.equals(event.getCallbackArgument())) {
 +                  String s = "Test Exception";
 +                  throw new CacheWriterException(s);
 +                }
 +
 +                assertEquals(region, event.getRegion());
 +                assertTrue(event.getOperation().isCreate());
 +                assertTrue(event.getOperation().isDistributed());
 +                assertFalse(event.getOperation().isExpiration());
 +                assertTrue(event.isOriginRemote());
 +                assertEquals(key, event.getKey());
 +                assertEquals(null, event.getOldValue());
 +                assertEquals(oldValue, event.getNewValue());
 +                assertFalse(event.getOperation().isLoad());
 +                assertFalse(event.getOperation().isLocalLoad());
 +                assertFalse(event.getOperation().isNetLoad());
 +                assertFalse(event.getOperation().isNetSearch());
 +
 +              }
 +            };
 +          region.getAttributesMutator().setCacheWriter(writer);
 +          flushIfNecessary(region);
 +        }
 +      });
 +
 +    vm0.invoke(new CacheSerializableRunnable("Create with Exception") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          try {
 +            region.put(key, oldValue, exception);
 +            fail("Should have thrown a CacheWriterException");
 +
 +          } catch (CacheWriterException ex) {
 +            assertNull(region.getEntry(key));
 +            assertEquals(1, region.size());
 +            if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
 +              GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
 +              SimpleMemoryAllocatorImpl ma = (SimpleMemoryAllocatorImpl) gfc.getOffHeapStore();
 +              assertEquals(1, ma.getStats().getObjects());
 +            }
 +          }
 +        }
 +      });
 +
 +    vm1.invoke(new SerializableRunnable("Verify callback") {
 +        public void run() {
 +          assertTrue(writer.wasInvoked());
 +        }
 +      });
 +
 +    vm0.invoke(new CacheSerializableRunnable("Create with Argument") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          region.put(key, oldValue, arg);
 +          assertEquals(2, region.size());
 +          if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
 +            GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
 +            SimpleMemoryAllocatorImpl ma = (SimpleMemoryAllocatorImpl) gfc.getOffHeapStore();
 +            assertEquals(2, ma.getStats().getObjects());
 +            LocalRegion reRegion;
 +            reRegion = (LocalRegion) region;
 +            MemoryChunkWithRefCount mc = (MemoryChunkWithRefCount) reRegion.getRegionEntry(key)._getValue();
 +            assertEquals(1, mc.getRefCount());
 +          }
 +        }
 +      });
 +    vm1.invoke(new SerializableRunnable("Verify callback") {
 +        public void run() {
 +          assertTrue(writer.wasInvoked());
 +        }
 +      });
 +
 +    ////////  Update
 +
 +    vm1.invoke(new CacheSerializableRunnable("Set Writer") {
 +        public void run2() throws CacheException {
 +          final Region region =
 +            getRootRegion().getSubregion(name);
 +          writer = new TestCacheWriter() {
 +              public void beforeUpdate2(EntryEvent event)
 +                throws CacheWriterException {
 +
 +                Object argument = event.getCallbackArgument();
 +                if (exception.equals(argument)) {
 +                  String s = "Test Exception";
 +                  throw new CacheWriterException(s);
 +                }
 +
 +                assertEquals(arg, argument);
 +
 +                assertEquals(region, event.getRegion());
 +                assertTrue(event.getOperation().isUpdate());
 +                assertTrue(event.getOperation().isDistributed());
 +                assertFalse(event.getOperation().isExpiration());
 +                assertTrue(event.isOriginRemote());
 +                assertEquals(key, event.getKey());
 +                assertEquals(oldValue, event.getOldValue());
 +                assertEquals(newValue, event.getNewValue());
 +                assertFalse(event.getOperation().isLoad());
 +                assertFalse(event.getOperation().isLocalLoad());
 +                assertFalse(event.getOperation().isNetLoad());
 +                assertFalse(event.getOperation().isNetSearch());
 +
 +              }
 +            };
 +          region.getAttributesMutator().setCacheWriter(writer);
 +        }
 +      });
 +
 +    vm0.invoke(new CacheSerializableRunnable("Update with Exception") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          try {
 +            region.put(key, newValue, exception);
 +            fail("Should have thrown a CacheWriterException");
 +
 +          } catch (CacheWriterException ex) {
 +            Region.Entry entry = region.getEntry(key);
 +            assertEquals(oldValue, entry.getValue());
 +            assertEquals(2, region.size());
 +            if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
 +              GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
 +              SimpleMemoryAllocatorImpl ma = (SimpleMemoryAllocatorImpl) gfc.getOffHeapStore();
 +              assertEquals(2, ma.getStats().getObjects());
 +              LocalRegion reRegion;
 +              reRegion = (LocalRegion) region;
 +              MemoryChunkWithRefCount mc = (MemoryChunkWithRefCount) reRegion.getRegionEntry(key)._getValue();
 +              assertEquals(1, mc.getRefCount());
 +            }
 +          }
 +        }
 +      });
 +    vm1.invoke(new SerializableRunnable("Verify callback") {
 +        public void run() {
 +          assertTrue(writer.wasInvoked());
 +        }
 +      });
 +
 +    vm0.invoke(new CacheSerializableRunnable("Update with Argument") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          region.put(key, newValue, arg);
 +          assertEquals(2, region.size());
 +          if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
 +            GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
 +            SimpleMemoryAllocatorImpl ma = (SimpleMemoryAllocatorImpl) gfc.getOffHeapStore();
 +            assertEquals(2, ma.getStats().getObjects());
 +          }
 +        }
 +      });
 +    vm1.invoke(new SerializableRunnable("Verify callback") {
 +        public void run() {
 +          assertTrue(writer.wasInvoked());
 +        }
 +      });
 +
 +    ////////  Destroy
 +
 +    vm1.invoke(new CacheSerializableRunnable("Set Writer") {
 +        public void run2() throws CacheException {
 +          final Region region =
 +            getRootRegion().getSubregion(name);
 +          writer = new TestCacheWriter() {
 +              public void beforeDestroy2(EntryEvent event)
 +                throws CacheWriterException {
 +
 +                Object argument = event.getCallbackArgument();
 +                if (exception.equals(argument)) {
 +                  String s = "Test Exception";
 +                  throw new CacheWriterException(s);
 +                }
 +
 +                assertEquals(arg, argument);
 +
 +                assertEquals(region, event.getRegion());
 +                assertTrue(event.getOperation().isDestroy());
 +                assertTrue(event.getOperation().isDistributed());
 +                assertFalse(event.getOperation().isExpiration());
 +                assertTrue(event.isOriginRemote());
 +                assertEquals(key, event.getKey());
 +                assertEquals(newValue, event.getOldValue());
 +                assertNull(event.getNewValue());
 +                assertFalse(event.getOperation().isLoad());
 +                assertFalse(event.getOperation().isLocalLoad());
 +                assertFalse(event.getOperation().isNetLoad());
 +                assertFalse(event.getOperation().isNetSearch());
 +              }
 +            };
 +          region.getAttributesMutator().setCacheWriter(writer);
 +        }
 +      });
 +
 +    vm0.invoke(new CacheSerializableRunnable("Destroy with Exception") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          try {
 +            region.destroy(key, exception);
 +            fail("Should have thrown a CacheWriterException");
 +
 +          } catch (CacheWriterException ex) {
 +            assertNotNull(region.getEntry(key));
 +            assertEquals(2, region.size());
 +            if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
 +              GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
 +              SimpleMemoryAllocatorImpl ma = (SimpleMemoryAllocatorImpl) gfc.getOffHeapStore();
 +              assertEquals(2, ma.getStats().getObjects());
 +            }
 +          }
 +        }
 +      });
 +    vm1.invoke(new SerializableRunnable("Verify callback") {
 +        public void run() {
 +          assertTrue(writer.wasInvoked());
 +        }
 +      });
 +
 +    vm0.invoke(new CacheSerializableRunnable("Destroy with Argument") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          region.destroy(key, arg);
 +          assertEquals(1, region.size());
 +          if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
 +            GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
 +            SimpleMemoryAllocatorImpl ma = (SimpleMemoryAllocatorImpl) gfc.getOffHeapStore();
 +            assertEquals(1, ma.getStats().getObjects());
 +          }
 +       }
 +      });
 +    vm1.invoke(new SerializableRunnable("Verify callback") {
 +        public void run() {
 +          assertTrue(writer.wasInvoked());
 +        }
 +      });
 +
 +    ////////  Region Destroy
 +
 +    vm1.invoke(new CacheSerializableRunnable("Set Writer") {
 +        public void run2() throws CacheException {
 +          final Region region =
 +            getRootRegion().getSubregion(name);
 +          writer = new TestCacheWriter() {
 +              public void beforeRegionDestroy2(RegionEvent event)
 +                throws CacheWriterException {
 +
 +                Object argument = event.getCallbackArgument();
 +                if (exception.equals(argument)) {
 +                  String s = "Test Exception";
 +                  throw new CacheWriterException(s);
 +                }
 +
 +                assertEquals(arg, argument);
 +
 +                assertEquals(region, event.getRegion());
 +                assertTrue(event.getOperation().isRegionDestroy());
 +                assertTrue(event.getOperation().isDistributed());
 +                assertFalse(event.getOperation().isExpiration());
 +                assertTrue(event.isOriginRemote());
 +              }
 +            };
 +          region.getAttributesMutator().setCacheWriter(writer);
 +        }
 +      });
 +
 +    vm0.invoke(new CacheSerializableRunnable("Destroy with Exception") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          try {
 +            region.destroyRegion(exception);
 +            fail("Should have thrown a CacheWriterException");
 +
 +          } catch (CacheWriterException ex) {
 +            if (region.isDestroyed()) {
 +              com.gemstone.gemfire.test.dunit.Assert.fail("should not have an exception if region is destroyed", ex);
 +            }
 +            assertEquals(1, region.size());
 +            if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
 +              GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
 +              SimpleMemoryAllocatorImpl ma = (SimpleMemoryAllocatorImpl) gfc.getOffHeapStore();
 +              assertEquals(1, ma.getStats().getObjects());
 +            }
 +          }
 +        }
 +      });
 +    vm1.invoke(new SerializableRunnable("Verify callback") {
 +        public void run() {
 +          assertTrue(writer.wasInvoked());
 +        }
 +      });
 +
 +    vm0.invoke(new CacheSerializableRunnable("Destroy with Argument") {
 +        public void run2() throws CacheException {
 +          Region region =
 +            getRootRegion().getSubregion(name);
 +          assertEquals(1, region.size());
 +          if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
 +            GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
 +            SimpleMemoryAllocatorImpl ma = (SimpleMemoryAllocatorImpl) gfc.getOffHeapStore();
 +            assertEquals(1, ma.getStats().getObjects());
 +          }
 +          region.destroyRegion(arg);
 +          if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
 +            GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
 +            final SimpleMemoryAllocatorImpl ma = (SimpleMemoryAllocatorImpl) gfc.getOffHeapStore();
 +            WaitCriterion waitForStatChange = new WaitCriterion() {
 +              public boolean done() {
 +                return ma.getStats().getObjects() == 0;
 +              }
 +              public String description() {
 +                return "never saw off-heap object count go to zero. Last value was " + ma.getStats().getObjects();
 +              }
 +            };
 +            Wait.waitForCriterion(waitForStatChange, 3000, 10, true);
 +          }
 +        }
 +      });
 +    vm1.invoke(new SerializableRunnable("Verify callback") {
 +        public void run() {
 +          assertTrue(writer.wasInvoked());
 +        }
 +      });
 +  }
 +
 +  /**
 +   * Tests that, when given a choice, a local <code>CacheWriter</code>
 +   * is invoked instead of a remote one.
 +   */
 +  public void testLocalAndRemoteCacheWriters()
 +    throws InterruptedException {
 +
 +    assertTrue(getRegionAttributes().getScope().isDistributed());
 +
 +    final String name = this.getUniqueName();
 +    final Object key = "KEY";
 +    final Object oldValue = "OLD_VALUE";
 +    final Object newValue = "NEW_VALUE";
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +
 +    vm0.invoke(new CacheSerializableRunnable("Create \"Local\" Region") {
 +        public void run2() throws CacheException {
 +          Region region = createRegion(name);
 +          writer = new TestCacheWriter() {
 +              public void beforeUpdate2(EntryEvent event)
 +                throws CacheWriterException { }
 +
 +              p

<TRUNCATED>


[45/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/IndexManager.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/IndexManager.java
index 3784327,0000000..7a0b1a9
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/IndexManager.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/IndexManager.java
@@@ -1,1718 -1,0 +1,1721 @@@
 +/*
 + * 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.
 + */
 +/*
 + * IndexManager.java
 + *
 + * Created on February 15, 2005, 11:49 AM
 + */
 +package com.gemstone.gemfire.cache.query.internal.index;
 +
 +import java.util.ArrayList;
 +import java.util.Arrays;
 +import java.util.Collection;
 +import java.util.HashMap;
 +import java.util.Iterator;
 +import java.util.List;
 +import java.util.Map;
 +import java.util.concurrent.ArrayBlockingQueue;
 +import java.util.concurrent.BlockingQueue;
 +import java.util.concurrent.Callable;
 +import java.util.concurrent.ConcurrentHashMap;
 +import java.util.concurrent.ConcurrentMap;
 +import java.util.concurrent.ExecutionException;
 +import java.util.concurrent.FutureTask;
 +import java.util.concurrent.atomic.AtomicLong;
 +import java.util.concurrent.LinkedBlockingQueue;
 +
 +import org.apache.logging.log4j.Logger;
 +
 +import com.gemstone.gemfire.SystemFailure;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.query.AmbiguousNameException;
 +import com.gemstone.gemfire.cache.query.Index;
 +import com.gemstone.gemfire.cache.query.IndexExistsException;
 +import com.gemstone.gemfire.cache.query.IndexInvalidException;
 +import com.gemstone.gemfire.cache.query.IndexMaintenanceException;
 +import com.gemstone.gemfire.cache.query.IndexNameConflictException;
 +import com.gemstone.gemfire.cache.query.IndexStatistics;
 +import com.gemstone.gemfire.cache.query.IndexType;
 +import com.gemstone.gemfire.cache.query.MultiIndexCreationException;
 +import com.gemstone.gemfire.cache.query.NameResolutionException;
 +import com.gemstone.gemfire.cache.query.QueryException;
 +import com.gemstone.gemfire.cache.query.TypeMismatchException;
 +import com.gemstone.gemfire.cache.query.internal.CompiledPath;
 +import com.gemstone.gemfire.cache.query.internal.CompiledValue;
 +import com.gemstone.gemfire.cache.query.internal.DefaultQuery;
 +import com.gemstone.gemfire.cache.query.internal.ExecutionContext;
 +import com.gemstone.gemfire.cache.query.internal.MapIndexable;
 +import com.gemstone.gemfire.cache.query.internal.NullToken;
 +import com.gemstone.gemfire.cache.query.internal.QueryMonitor;
 +import com.gemstone.gemfire.cache.query.internal.QueryObserver;
 +import com.gemstone.gemfire.cache.query.internal.QueryObserverHolder;
 +import com.gemstone.gemfire.cache.query.internal.index.AbstractIndex.InternalIndexStatistics;
 +import com.gemstone.gemfire.cache.query.internal.parse.OQLLexerTokenTypes;
 +import com.gemstone.gemfire.internal.Assert;
 +import com.gemstone.gemfire.internal.cache.BucketRegion;
 +import com.gemstone.gemfire.internal.cache.CachePerfStats;
 +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
 +import com.gemstone.gemfire.internal.cache.LocalRegion;
 +import com.gemstone.gemfire.internal.cache.PartitionedRegion;
 +import com.gemstone.gemfire.internal.cache.RegionEntry;
 +import com.gemstone.gemfire.internal.cache.TXManagerImpl;
 +import com.gemstone.gemfire.internal.cache.TXStateProxy;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.logging.LogService;
 +import com.gemstone.gemfire.internal.logging.LoggingThreadGroup;
 +
 +/**
 + * @author vaibhav
 + * @author asif
 + */
 +public class IndexManager  {
 +  private static final Logger logger = LogService.getLogger();
 +
 +  public static final int ADD_ENTRY = 1;
 +  public static final int UPDATE_ENTRY = 2;
 +  public static final int REMOVE_ENTRY = 3;
 +  //Asif : This action is to rerun Index creation after
 +  //clear is called on the region
 +  public static final int RECREATE_INDEX = 4;
 +  protected final Region region;
 +
 +  private final boolean isOverFlowToDisk;
 +  private final boolean offHeap;
 +  private final boolean indexMaintenanceSynchronous;
 +  private int numCreators = 0;
 +  private int numUpdatersInProgress = 0;
 +  private int numUpdatersInWaiting = 0;
 +  private int iternameCounter = 0;
 +  /*
 +   * Map containing <IndexTask, FutureTask<IndexTask> or Index>. IndexTask
 +   * represents an index thats completely created or one thats in create phase.
 +   * This is done in order to avoid synchronization on the indexes.
 +   */
 +  private final ConcurrentMap indexes = new ConcurrentHashMap();
 +  //TODO Asif : Fix the appropriate size of the Map & the concurrency level
 +  private ConcurrentMap canonicalizedIteratorNameMap = new ConcurrentHashMap();
 +  private IndexUpdaterThread updater;
 +
 +  // Threshold for Queue.
 +  private final int INDEX_MAINTENANCE_BUFFER = Integer.getInteger("gemfire.AsynchIndexMaintenanceThreshold", -1).intValue();
 +
 +  public static boolean JOIN_OPTIMIZATION = !Boolean.getBoolean("gemfire.index.DisableJoinOptimization");
 +  
 +  // Added for test purposes only.
 +  public static boolean INPLACE_OBJECT_MODIFICATION_FOR_TEST = false;    
 +
 +  //Added for testing only
 +  public static boolean IS_TEST_LDM = false; 
 +
 +  public static boolean IS_TEST_EXPANSION = false;
 +  
 +  
 +
 +  /**
 +   * System property to maintain the ReverseMap to take care in-place modification of the 
 +   * objects by the application. 
 +   * In case of in-place modification the EntryEvent will not have the old-value, without this
 +   * the old-values are not removed from the index-maps thus resulting in inconsistent results.
 +   */
 +  public static final boolean INPLACE_OBJECT_MODIFICATION =     
 +    Boolean.valueOf(System.getProperty("gemfire.index.INPLACE_OBJECT_MODIFICATION", "false")).booleanValue(); 
 +
 +  /**
 +   * System property to turn-off the compact-index support.
 +   */
 +  public static final boolean RANGEINDEX_ONLY =     
 +    Boolean.valueOf(System.getProperty("gemfire.index.RANGEINDEX_ONLY", "false")).booleanValue();
 +
 +  /** For test purpose only */
 +  public static boolean TEST_RANGEINDEX_ONLY = false;
 +  public static final String INDEX_ELEMARRAY_THRESHOLD_PROP = "index_elemarray_threshold";
 +  public static final String INDEX_ELEMARRAY_SIZE_PROP = "index_elemarray_size";
 +  public static final int INDEX_ELEMARRAY_THRESHOLD = Integer.parseInt(System.getProperty(INDEX_ELEMARRAY_THRESHOLD_PROP,"100"));
 +  public static final int INDEX_ELEMARRAY_SIZE = Integer.parseInt(System.getProperty(INDEX_ELEMARRAY_SIZE_PROP,"5"));
 +  public final static AtomicLong SAFE_QUERY_TIME = new AtomicLong(0);
 +  public static boolean ENABLE_UPDATE_IN_PROGRESS_INDEX_CALCULATION = true;
 +  /** The NULL constant */
 +  public static final Object NULL = new NullToken();
 +
 +  public static TestHook testHook;
 +
 +  //private int numCreatorsInWaiting = 0;
 +  // @todo ericz
 +  // there should be a read/write lock PER INDEX in order to maximize
 +  // the concurrency of query execution.
 +  public IndexManager(Region region) {
 +    this.region = region;
 +    // must be a SortedMap to ensure the indexes are iterated over in fixed
 +    // order
 +    // to avoid deadlocks when acquiring locks
 +    //indexes = Collections.synchronizedSortedMap(new TreeMap());
 +    indexMaintenanceSynchronous = region.getAttributes()
 +        .getIndexMaintenanceSynchronous();
 +    isOverFlowToDisk = region.getAttributes().getEvictionAttributes()
 +        .getAction().isOverflowToDisk();
 +    this.offHeap = region.getAttributes().getOffHeap();
 +    if (!indexMaintenanceSynchronous) {
 +      final LoggingThreadGroup group =
 +        LoggingThreadGroup.createThreadGroup("QueryMonitor Thread Group", logger);
 +      updater = new IndexUpdaterThread(group, this.INDEX_MAINTENANCE_BUFFER,
 +                                       "OqlIndexUpdater:" + region.getFullPath());
 +      updater.start();
 +    }
 +  }
 +   
 +  /**
 +   * Stores the largest combination of current time + delta
 +   * If there is a large delta/hiccup in timings, this allows us to calculate the 
 +   * correct results for a query but, reevaluate more aggressively.
 +   * But the large hiccup will eventually be rolled off as time is always increasing
 +   * This is a fix for #47475
 +   * 
 +   * @param operationTime the last modified time from version tag
 +   * @param currentCacheTime
 +   */
 +  public static boolean setIndexBufferTime(long operationTime, long currentCacheTime) {
 +    long timeDifference = currentCacheTime - operationTime;
 +    return setNewLargestValue(SAFE_QUERY_TIME, currentCacheTime + timeDifference);
 +  }
 +  
 +  /** only for test purposes 
 +   * This should not be called from any product code.  Calls from product code will 
 +   * possibly cause continous reevaluation (performance issue) OR
 +   * incorrect query results (functional issue)
 +   **/
 +  public static void resetIndexBufferTime() {
 +    SAFE_QUERY_TIME.set(0);
 +  }
 +  
 +  /**
 +   * Calculates whether we need to reevluate the key for the region entry
 +   * We added a way to determine whether to reevaluate an entry for query execution
 +   * The method is to keep track of the delta and current time in a single long value
 +   * The value is then used by the query to determine if a region entry needs to be reevaluated,
 +   * based on subtracting the value with the query execution time.  This provides a delta + some false positive time (dts)
 +   * If the dts + last modified time of the region entry is > query start time, 
 +   * we can assume that it needs to be reevaluated
 +   *
 +   * This is to fix bug 47475, where references to region entries can be held
 +   * by the executing query either directly or indirectly (iterators can hold 
 +   * references for next) but the values underneath could change.
 +   * 
 +   * Small amounts of false positives are ok as it will have a slight impact on performance
 +   * @param queryStartTime
 +   * @param lastModifiedTime
 +   */
 +  public static boolean needsRecalculation(long queryStartTime, long lastModifiedTime) {
 +    return ENABLE_UPDATE_IN_PROGRESS_INDEX_CALCULATION && queryStartTime <= SAFE_QUERY_TIME.get() - queryStartTime + lastModifiedTime;
 +  }
 +  
 +  /**
 +   * 
 +   * @param value
 +   * @param newValue
 +   */
 +  private static boolean setNewLargestValue(AtomicLong value, long newValue) {
 +    boolean done = false;
 +    while (!done) {
 +      long oldValue = value.get();
 +      if (oldValue < newValue ) {
 +        return value.compareAndSet(oldValue, newValue);
 +      }
 +      else {
 +        done = true;
 +      }
 +    }
 +    return false;
 +  }
 +
 +  /** Test Hook */
 +  public interface TestHook {
 +    public void hook(final int spot) throws RuntimeException;
 +  }
 +  
 +  /**
 +   * The Region this IndexManager is associated with
 +   * 
 +   * @return the Region for this IndexManager
 +   */
 +  public Region getRegion() {
 +    return region;
 +  }
 +
 +  /**
 +   * Used by tests to access the updater thread to determine its progress
 +   */
 +  public IndexUpdaterThread getUpdaterThread() {
 +    return this.updater;
 +  }
 +
 +  // @todo need more specific list of exceptions
 +  /**
 +   * Create an index that can be used when executing queries.
 +   * 
 +   * @param indexName the name of this index, used for statistics collection
 +   * @param indexType the type of index
 +   * @param origIndexedExpression the expression to index on, a function
 +   *          dependent on region entries individually.
 +   * @param origFromClause expression that evaluates to the collection(s) that
 +   *          will be queried over, must contain one and only one region path.
 +   * @return the newly created Index
 +   */
 +  public Index createIndex(String indexName, IndexType indexType,
 +      String origIndexedExpression, String origFromClause, String imports,
 +      ExecutionContext externalContext, PartitionedIndex prIndex, boolean loadEntries)
 +      throws IndexNameConflictException, IndexExistsException,
 +      IndexInvalidException {
 +    
 +    if (QueryMonitor.isLowMemory()) {
 +      throw new IndexInvalidException(LocalizedStrings.IndexCreationMsg_CANCELED_DUE_TO_LOW_MEMORY.toLocalizedString());
 +    }
 +    
 +    boolean oldReadSerialized = DefaultQuery.getPdxReadSerialized();
 +    DefaultQuery.setPdxReadSerialized(this.region.getCache(), true);
 +
 +    TXStateProxy tx = null;
 +    if (!((GemFireCacheImpl)this.region.getCache()).isClient()) {
 +      tx = ((TXManagerImpl) this.region.getCache().getCacheTransactionManager()).internalSuspend();
 +    }
 +
 +    try {
 +      String projectionAttributes = "*"; // for now this is the only option
 +
 +      if (getIndex(indexName) != null) { 
 +        throw new IndexNameConflictException(LocalizedStrings.
 +            IndexManager_INDEX_NAMED_0_ALREADY_EXISTS.toLocalizedString(indexName)); 
 +      }
 +
 +      IndexCreationHelper helper = null;
 +      boolean isCompactOrHash = false;
 +      if (indexType != IndexType.PRIMARY_KEY) {
 +        helper = new FunctionalIndexCreationHelper(origFromClause,
 +            origIndexedExpression, projectionAttributes, imports, region.getCache(),
 +            externalContext, this);
 +        //Asif: For now support Map index as non compact .expand later
 +        //The limitation for compact range index also apply to hash index for now
 +        isCompactOrHash = shouldCreateCompactIndex((FunctionalIndexCreationHelper)helper);
 +      } else if (indexType == IndexType.PRIMARY_KEY) {
 +        helper = new PrimaryKeyIndexCreationHelper(origFromClause,
 +            origIndexedExpression, projectionAttributes, region.getCache(),
 +            externalContext, this);
 +      } else {
 +        throw new AssertionError("Don't know how to set helper for " + indexType);
 +      }
 +      if (!isCompactOrHash && indexType != IndexType.PRIMARY_KEY) {
 +        
 +        if (indexType == IndexType.HASH ) {
 +          if (!isIndexMaintenanceTypeSynchronous()) {
 +            throw new UnsupportedOperationException(LocalizedStrings.DefaultQueryService_HASH_INDEX_CREATION_IS_NOT_SUPPORTED_FOR_ASYNC_MAINTENANCE.toLocalizedString());
 +          } 
 +          throw new UnsupportedOperationException(LocalizedStrings.DefaultQueryService_HASH_INDEX_CREATION_IS_NOT_SUPPORTED_FOR_MULTIPLE_ITERATORS.toLocalizedString());
 +        }
 +        // Overflow is not supported with range index.
 +        if(isOverFlowRegion()) {
 +          throw new UnsupportedOperationException(LocalizedStrings.DefaultQueryService_INDEX_CREATION_IS_NOT_SUPPORTED_FOR_REGIONS_WHICH_OVERFLOW_TO_DISK_THE_REGION_INVOLVED_IS_0.toLocalizedString(region.getFullPath()));
 +        }
 +        // OffHeap is not supported with range index.
 +        if(isOffHeap()) {
 +          if (!isIndexMaintenanceTypeSynchronous()) {
 +            throw new UnsupportedOperationException(LocalizedStrings.DefaultQueryService_OFF_HEAP_INDEX_CREATION_IS_NOT_SUPPORTED_FOR_ASYNC_MAINTENANCE_THE_REGION_IS_0.toLocalizedString(region.getFullPath()));
 +          } 
 +          throw new UnsupportedOperationException(LocalizedStrings.DefaultQueryService_OFF_HEAP_INDEX_CREATION_IS_NOT_SUPPORTED_FOR_MULTIPLE_ITERATORS_THE_REGION_IS_0.toLocalizedString(region.getFullPath()));
 +        }
 +      }
 +
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("Started creating index with indexName: {} On region: {}", indexName, region.getFullPath());
 +      }
 +      
 +      if (IndexManager.testHook != null) {
 +        if (logger.isDebugEnabled()) {
 +          logger.debug("IndexManager TestHook is set.");
 +        }
 +        
 +        if (((LocalRegion)this.region).isInitialized()) {
 +          testHook.hook(1);
 +        } else {
 +          testHook.hook(0);
 +        }
 +      }
 +
 +      IndexTask indexTask = new IndexTask(indexName, indexType, origFromClause,
 +          origIndexedExpression, helper, isCompactOrHash, prIndex, loadEntries);
 +      FutureTask<Index> indexFutureTask = new FutureTask<Index>(indexTask);
 +      Object oldIndex = this.indexes.putIfAbsent(indexTask, indexFutureTask);
 +
 +      Index index = null;
 +
 +      boolean interrupted = false;
 +      try {
 +        if (oldIndex == null){
 +            // Initialize index.
 +            indexFutureTask.run();
 +            // Set the index.
 +            index = (Index)indexFutureTask.get();
 +        } else {
 +          // Index with same name or characteristic already exists.
 +          // Check if index creation is complete.
 +          if (!(oldIndex instanceof Index)){
 +            // Some other thread is creating the same index.
 +            // Wait for index to be initialized from other thread.
 +            ((FutureTask)oldIndex).get();
 +          }
 +
 +          // The Index is successfully created, throw appropriate error message
 +          // from this thread.
 +          if (getIndex(indexName) != null) {
 +            throw new IndexNameConflictException(LocalizedStrings.
 +                IndexManager_INDEX_NAMED_0_ALREADY_EXISTS.toLocalizedString(indexName));
 +          } else {
 +            throw new IndexExistsException(LocalizedStrings.
 +                IndexManager_SIMILAR_INDEX_EXISTS.toLocalizedString());          
 +          }
 +        }
 +      } catch (InterruptedException ie){
 +        interrupted = true;
 +      } catch (ExecutionException ee){
 +        Throwable c = ee.getCause();
 +        if (c instanceof IndexNameConflictException) {
 +          throw (IndexNameConflictException)c;
 +        } else if (c instanceof IndexExistsException){
 +          throw (IndexExistsException)c;
 +        } else if (c instanceof IMQException) {
 +          throw new IndexInvalidException(c.getMessage());
 +        }
 +        throw new IndexInvalidException(ee);
 +
 +      } finally {
 +        // If the index is not successfully created, remove IndexTask from 
 +        // the map.
 +        if (oldIndex == null && index == null){
 +          Object ind = this.indexes.get(indexTask);
 +          if (ind != null && !(ind instanceof Index)){
 +            this.indexes.remove(indexTask);
 +          }
 +        }
 +        if (interrupted){
 +          Thread.currentThread().interrupt();
 +        }
 +      }
 +      assert (index != null);
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("Completed creating index with indexName: {} On region: {}", indexName, region.getFullPath());
 +      }
 +      return index;
 +
 +    } finally {
 +      DefaultQuery.setPdxReadSerialized(this.region.getCache(), oldReadSerialized);
 +      
 +      if (tx != null) {
 +        ((TXManagerImpl) this.region.getCache().getCacheTransactionManager())
 +        .resume(tx);
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Return true if we should create CompactRangeIndex
 +   * Required conditions: indexedExpression is a path expression,
 +   * fromClause has only one iterator and it is directly on the
 +   * region values.
 +   * Currently we have to use the "fat" implementation when asynchronous
 +   * index updates are on.
 +   */   
 +  private boolean shouldCreateCompactIndex(FunctionalIndexCreationHelper helper) {
 +    if (RANGEINDEX_ONLY || TEST_RANGEINDEX_ONLY){
 +      return false;
 +    }
 +    
 +    // compact range index is not supported on asynchronous index maintenance.
 +    // since compact range index maintains reference to region entry, in case of
 +    // asynchronous updates the window between cache operation updating the 
 +    // index increases causing query thread to return new value before doing
 +    // index evaluation (resulting in wrong value. There is still a small window
 +    // which can be addressed by the sys property:
 +    // gemfire.index.acquireCompactIndexLocksWithRegionEntryLocks
 +    if (!getRegion().getAttributes().getIndexMaintenanceSynchronous()) {
 +      return false;
 +    } 
 +    
 +    // indexedExpression requirement
 +    CompiledValue cv = helper.getCompiledIndexedExpression();
 +    int nodeType;
 +    do {
 +      nodeType = cv.getType();
 +      if (nodeType == CompiledValue.PATH) {
 +        cv = ((CompiledPath)cv).getReceiver();
 +      }
 +    } while (nodeType == CompiledValue.PATH);
 +    // end of path, nodeType at this point should be an Identifier
 +    if (nodeType != OQLLexerTokenTypes.Identifier && nodeType != OQLLexerTokenTypes.METHOD_INV) {
 +      if (nodeType == OQLLexerTokenTypes.TOK_LBRACK && !helper.isMapTypeIndex() && helper.modifiedIndexExpr instanceof MapIndexable) {
 +        if (((MapIndexable)helper.modifiedIndexExpr).getIndexingKeys().size() == 1) {
 +          
 +        }
 +        else {
 +          return false;
 +        }
 +      }
 +      else {
 +        return false;
 +      }
 +    } 
 +    
 +    // fromClause requirement
 +    List iterators = helper.getIterators();
 +    if (iterators.size() != 1) {
 +      return false;
 +    }
 +    // "missing link" must be "value". Later to support key, entry, etc.
 +    CompiledValue missingLink = helper.missingLink;
 +    if (helper.isFirstIteratorRegionEntry) {
 +      return true;
 +    } else if (!(missingLink instanceof CompiledPath)) {
 +      return false;
 +
 +    }
 +    String tailId = ((CompiledPath)missingLink).getTailID();
 +    if (!(tailId.equals("value") || tailId.equals("key"))) {
 +      return false;
 +    } 
 +    return true;
 +  }
 +
 +  public Index getIndex(String indexName) {
 +    IndexTask indexTask = new IndexTask(indexName);
 +    Object ind = this.indexes.get(indexTask);
 +    // Check if the returned value is instance of Index (this means
 +    // the index is not in create phase, its created successfully).
 +    if (ind instanceof Index){
 +      return (Index)ind;
 +    }
 +    return null;
 +  }
 +
 +  public void addIndex(String indexName, Index index) {
 +    IndexTask indexTask = new IndexTask(indexName);
 +    this.indexes.put(indexTask, index);
 +  }
 +
 +  /**
 +   * Get the Index with the specified indexType, fromClause, indexedExpression
 +   * TODO: Asif :Check if synchronization is needed while obtaining Array of
 +   * Indexes as similar to what we have used during index updates. This function
 +   * will get the exact index , if available, else will return null
 +   * 
 +   * @param indexType the type of index
 +   * @param definitions the String array containing the required defintions of the
 +   *          fromClause of the index
 +   * @param indexedExpression the indexedExpression for the index
 +   * @param context ExecutionContext
 +   * @return the sole index of the region with these parameters, or null if
 +   *         there isn't one
 +   * @throws NameResolutionException 
 +   * @throws TypeMismatchException 
 +   * @throws AmbiguousNameException 
 +   */
 +  public IndexData getIndex(IndexType  indexType, String[] definitions,
 +      CompiledValue indexedExpression, ExecutionContext context) 
 +  throws AmbiguousNameException, TypeMismatchException, NameResolutionException {
 +    IndexData indxData = null;
 +    int qItrSize = definitions.length;
 +    Iterator it = this.indexes.values().iterator();
 +    StringBuffer sb = new StringBuffer();
 +    indexedExpression.generateCanonicalizedExpression(sb, context);
 +    String indexExprStr = sb.toString();
 +    while (it.hasNext()) {
 +      int mapping[] = new int[qItrSize];
 +      Object ind = it.next();
 +      // Check if the returned value is instance of Index (this means
 +      // the index is not in create phase, its created successfully).
 +      if (!(ind instanceof Index)){
 +        continue;
 +      }
 +      Index index = (Index)ind;    
 +      if( !((IndexProtocol)ind).isMatchingWithIndexExpression(indexedExpression,
 +          indexExprStr, context) || index.getType() != indexType) {
 +        continue;
 +      }
 +      
 +      int matchLevel = getMatchLevel(definitions, 
 +          ((IndexProtocol)index).getCanonicalizedIteratorDefinitions(), mapping); 
 +        
 +      if (matchLevel == 0){
 +        indxData = new IndexData((IndexProtocol) index, 0/*Exact Match*/, mapping);
 +        break;
 +      }
 +
 +    }
 +    return indxData;
 +  }
 +
 +  public int compareIndexData(IndexType indexType, String[] indexDefinitions,
 +      String indexExpression, IndexType otherType, String[] otherDefinitions,
 +      String otherExpression, int mapping[]) {
 +
 +    int matchLevel = -2;
 +
 +    if (indexExpression.equals(otherExpression) && indexType == otherType) {
 +      /* Asif : A match level of zero means exact match. */
 +      matchLevel = getMatchLevel(otherDefinitions, indexDefinitions, mapping);
 +    }
 +    return matchLevel;
 +  }
 +  
 +  /**
 +   * Asif : Returns the best available Index based on the available iterators in
 +   * the Group
 +   * 
 +   * TODO: Asif :Check if synchronization is needed while obtaining Array of
 +   * Indexes as similar to what we have used during index updates
 +   * 
 +   * @param indexType Primary or Range Index
 +   * @param definitions String array containing the canonicalized definitions of
 +   *          the Iterators of the Group
 +   * @param indexedExpression Index Expression path(CompiledValue) on which
 +   *          index needs to be created
 +   * @param context ExecutionContext object
 +   * @return IndexData object
 +   * @throws NameResolutionException 
 +   * @throws TypeMismatchException 
 +   * @throws AmbiguousNameException 
 +   */
 +  public IndexData getBestMatchIndex(IndexType indexType, String[] definitions,
 +      CompiledValue indexedExpression, ExecutionContext context) 
 +  throws AmbiguousNameException, TypeMismatchException, NameResolutionException {
 +
 +    Index bestIndex = null;
 +    Index bestPRIndex = null;
 +    int[] bestMapping = null;
 +
 +    int qItrSize = definitions.length;
 +    int bestIndexMatchLevel = qItrSize;
 +    Iterator iter = this.indexes.values().iterator();
 +    StringBuffer sb = new StringBuffer();
 +    indexedExpression.generateCanonicalizedExpression(sb, context);
 +    String indexExprStr = sb.toString();
 +    PartitionedIndex prIndex = null;
 +    Index prevBestPRIndex = null;
 +    Index prevBestIndex = null;
 +
 +    Index index;
 +    while (iter.hasNext()) {
 +      Object ind = iter.next();
 +      // Check if the value is instance of FutureTask, this means
 +      // the index is in create phase.
 +      if (ind instanceof FutureTask){
 +        continue;
 +      }
 +      
 +      //If the index is still empty
 +      if(!((AbstractIndex)ind).isPopulated()) {
 +        continue;
 +      }
 +
 +      index = (Index)ind;    
 +      
 +      if (index instanceof PartitionedIndex) {
 +        prIndex = (PartitionedIndex)index;
 +        // Get one of the bucket index. This index will be
 +        // available on all the buckets.
 +        index = prIndex.getBucketIndex();
 +        if (index == null) {
 +          continue;
 +        }
 +      }
 +      
 +      //System.out.println(" Index = "+index);
 +      //Use simple strategy just pick first compatible index
 +      if ( ((IndexProtocol)index).isMatchingWithIndexExpression(indexedExpression,indexExprStr,
 +          context) && index.getType() == indexType ){
 +  
 +        // For PR the matched index needs to be available on all the query buckets.
 +        if (prIndex != null) {
 +          try {
 +
 +            // Protect the PartitionedIndex from being removed when it is being used.
 +            if (!prIndex.acquireIndexReadLockForRemove()) {
 +              continue;
 +            }
 +
 +            prIndex.verifyAndCreateMissingIndex(context.getBucketList());
 +          } catch (Exception ex) {
 +            // Index is not there on all buckets. 
 +            // ignore this index.
 +            prIndex.releaseIndexReadLockForRemove();
 +            prIndex = null;
 +            continue;
 +          }
 +        } else {
 +          // For index on replicated regions
 +          if (!((AbstractIndex)index).acquireIndexReadLockForRemove()) {
 +            continue;
 +          }
 +        }
 +        
 +        /*
 +         * Asif : A match level of zero means exact match. A match level greater
 +         * than 0 means the query from clauses have extra iterators as compared
 +         * to Index resultset ( This does not neccessarily mean that Index
 +         * resultset is not having extra fields. It is just that the criteria
 +         * for match level is the absence or presence of extra iterators. The
 +         * order of preference will be 0 , <0 , > 0 for first cut.
 +         */
 +        String indexDefinitions[] = ((IndexProtocol) index).getCanonicalizedIteratorDefinitions();
 +        
 +        int mapping[] = new int[qItrSize];
 +        int matchLevel = getMatchLevel(definitions, indexDefinitions, mapping);
 +        
 +        if (matchLevel == 0) {
 +          prevBestPRIndex = bestPRIndex;
 +          bestPRIndex = prIndex;
 +          prevBestIndex = bestIndex;
 +          bestIndex = index;
 +          bestIndexMatchLevel = matchLevel;
 +          bestMapping = mapping;
 +
 +          // If we chose new index we should release lock on previous index
 +          // chosen as bestIndex.
 +         if (prIndex != null && prevBestPRIndex != null && prevBestPRIndex instanceof PartitionedIndex) {
 +            ((PartitionedIndex) prevBestPRIndex).releaseIndexReadLockForRemove();
 +            prevBestPRIndex = null;
 +          } else if (prevBestIndex != null) {
 +            ((AbstractIndex) prevBestIndex).releaseIndexReadLockForRemove();
 +            prevBestIndex = null;
 +          } 
 +          break;
 +        } else if ((bestIndexMatchLevel > 0 && matchLevel < bestIndexMatchLevel)
 +            || (bestIndexMatchLevel < 0 && matchLevel < 0 && matchLevel > bestIndexMatchLevel)) {
 +          prevBestPRIndex = bestPRIndex;
 +          bestPRIndex = prIndex;
 +          prevBestIndex = bestIndex;
 +          bestIndex = index;
 +          bestIndexMatchLevel = matchLevel;
 +          bestMapping = mapping;
 +        }
 +
 +        // release the lock if this index is not chosen as bestIndex.
 +        if (prIndex != null && bestPRIndex != prIndex) {
 +          prIndex.releaseIndexReadLockForRemove();
 +          prIndex = null;
 +        } else if(bestIndex != index) {
 +          ((AbstractIndex)index).releaseIndexReadLockForRemove();
 +          index = null;
 +        }
 +
 +        // If we chose new index we should release lock on previous  index
 +        // chosen as bestIndex.
 +        if (prevBestPRIndex != null && prevBestPRIndex instanceof PartitionedIndex) {
 +          ((PartitionedIndex) prevBestPRIndex).releaseIndexReadLockForRemove();
 +          prevBestPRIndex = null;
 +        } else if (prevBestIndex != null) {
 +          ((AbstractIndex) prevBestIndex).releaseIndexReadLockForRemove();
 +          prevBestIndex = null;
 +        }
 +      }
 +    }
 +    if (bestIndex != null) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("The best index found for index expression: {} is: {} with Match-level: {} and mapping: {}", indexExprStr, bestIndex, bestIndexMatchLevel, Arrays.toString(bestMapping));
 +      }
 +    }
 +    return bestIndex != null ? new IndexData((IndexProtocol) bestIndex,
 +        bestIndexMatchLevel, bestMapping) : null;
 +  }
 +
 +  /*
 +   * Asif : This function returns the best match index. The crietria used to
 +   * identify best match index is based currently , relative to the query from
 +   * clause. If the iterators of query from clause exactly match the index from
 +   * clause , then match level is zero & is the best match. If the query from
 +   * clause contain extra iterators , not available in index from clause, then
 +   * mach level is > 0 & is not the best. If the match level is < 0 that means
 +   * Index from clause have some extra iterators as compared to query. The int
 +   * array gives the mapping of query iterator's position relative to the index
 +   * resultset fields . The position is '1' based index. That is for the first
 +   * query iterator ( 0 position), the mapping will contain 1 which can be
 +   * thought of as Index ResultSet value at the field with name index_iter1. If
 +   * the second query iterator has a value 3 , that means for (1 position)
 +   * iterator , the field will have name index_iter3
 +   */
 +  private static int getMatchLevel(String[] queryDefintions,
 +      String[] indexDefinitions, int[] mapping) {
 +    int qItrLen = queryDefintions.length;
 +    int indxItrLen = indexDefinitions.length;
 +    // Asif : We know that because index expressions have matched that
 +    // itself indicates that the first iterator, which is regon iterator
 +    // has matched. So the index field position of the first RuntimeIterator
 +    // of the Query group is 1
 +    mapping[0] = 1;
 +    int matchLevel = qItrLen - 1;
 +    for (int i = 1; i < qItrLen; ++i) {
 +      for (int j = 1; j < indxItrLen; ++j) {
 +        if (queryDefintions[i].equals(indexDefinitions[j])) {
 +          mapping[i] = ++j;
 +          --matchLevel;
 +          break;
 +        }
 +      }
 +    }
 +    if (matchLevel == 0 && indxItrLen > qItrLen) {
 +      matchLevel = qItrLen - indxItrLen;
 +    }
 +    return matchLevel;
 +  }
 +
 +  /*
 +   * private static int getMatchLevel(String fromClause, String iFromClause) {
 +   * if (fromClause.equals(iFromClause)) return 0; if
 +   * (fromClause.startsWith(iFromClause)) { int cnt = -1; int index =
 +   * fromClause.indexOf(',', iFromClause.length() + 1); while (index > 0) {
 +   * cnt--; index = fromClause.indexOf(',', index + 1); } return cnt; } else if
 +   * (iFromClause.startsWith(fromClause)) { int cnt = 1; int index =
 +   * iFromClause.indexOf(',', fromClause.length() + 1); while (index > 0) {
 +   * cnt++; index = iFromClause.indexOf(',', index + 1); } return cnt; } //No
 +   * compatible return Integer.MAX_VALUE; }
 +   */
 +  /**
 +   * Get a collection of all the indexes. If the IndexType is specified
 +   * returns only the matching indexes.   
 +   * @param indexType the type of indexes to get. Currently must be
 +   *          Indexable.FUNCTIONAL_SORTED
 +   * @return the collection of indexes for the specified region and type
 +   */
 +  public Collection getIndexes(IndexType indexType) {
 +    ArrayList list = new ArrayList();
 +    Iterator it = this.indexes.values().iterator();
 +    while (it.hasNext()) {
 +      Object ind = it.next();
 +      // Check if the value is instance of FutureTask, this means
 +      // the index is in create phase.
 +      if (ind instanceof FutureTask){
 +        continue;
 +      }
 +      Index index = (Index)ind;
 +
 +      // Check if indexType needs to be matched.
 +      if (indexType == null) { // No type check.
 +        list.add(index);
 +      } else if(index.getType() == indexType) {
 +        list.add(index);
 +      }
 +    }
 +    return list;
 +  }  
 +  
 +  /**
 +   * Get a collection of all the indexes managed by IndexManager
 +   * 
 +   * @return the collection of indexes on the specified region
 +   */
 +  public Collection getIndexes() {
 +    return getIndexes(null);
 +  }
 +
 +  // @todo need more specific list of exceptions
 +  /**
 +   * Remove the specified index.
 +   * 
 +   * @param index the Index to remove
 +   */
 +  public void removeIndex(Index index) {
 +    if (index.getRegion() != this.region) 
 +    { 
 +      throw new IllegalArgumentException(LocalizedStrings.
 +          IndexManager_INDEX_DOES_NOT_BELONG_TO_THIS_INDEXMANAGER.toLocalizedString()); 
 +    }
 +    //Asif: We will just remove the Index from the map. Since the
 +    // TreeMap is synchronized & the operation of adding a newly created
 +    // index is in synch there will not be any situation where the unintended
 +    // Index gets removed( in case of same Index Name scenario).
 +    // If query obtains the Index handle which is getting removed , that
 +    // is OK as we are not clearing data maps . The destroy though marks
 +    // the index invalid , that is OK. Because of this flag a query
 +    // may or may not use the Index
 +    IndexTask indexTask = new IndexTask(index.getName());
 +    if (this.indexes.remove(indexTask) != null) {
 +      AbstractIndex indexHandle = (AbstractIndex) index;
 +      indexHandle.destroy();
 +    }
 +  }
 +
 +  // @todo need more specific list of exceptions
 +  /**
 +   * Remove all the indexes managed by IndexManager
 +   */
 +  public int removeIndexes() {
 +    // Remove indexes which are available (create completed).
 +    int numIndexes = 0;
 +    Iterator it = this.indexes.entrySet().iterator();
 +    while (it.hasNext()) {
 +      Map.Entry entry = (Map.Entry)it.next();
 +      Object ind = entry.getValue();
 +      // Check if the returned value is instance of Index (this means
 +      // the index is not in create phase, its created successfully).
 +      if (!(ind instanceof Index)){
 +        continue;
 +      }
 +      numIndexes++;
 +      IndexTask indexTask = (IndexTask)entry.getKey();
 +      this.indexes.remove(indexTask);
 +    }
 +    return numIndexes;
 +  }
 +  
 +
 +  /**
 +   * Asif : This function is invoked during clear operation on Region. It causes
 +   * re execution of Index Initialization query on the region & before doing
 +   * this it makes theexisting data maps null. This is needed so that index does
 +   * not miss any entry being put in the region when the Region.clear is in
 +   * progress
 +   * 
 +   * @throws QueryException
 +   */
 +  public void rerunIndexCreationQuery() throws QueryException {
 +    try {
 +      QueryObserver observer = QueryObserverHolder.getInstance();
 +      observer.beforeRerunningIndexCreationQuery();
 +    }
 +    catch (Exception e) {
 +      //Asif Ignore any exception as this should not hamper normal code flow
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("IndexMananger::rerunIndexCreationQuery: Exception in callback beforeRerunningIndexcreationQuery", e);
 +      }
 +    }
 +    if (isIndexMaintenanceTypeSynchronous()) {
 +      recreateAllIndexesForRegion();
 +    }
 +    else {
 +      //System.out.println("Aynchronous update");
 +      updater.addTask(RECREATE_INDEX, null, IndexProtocol.OTHER_OP);
 +    }
 +  }
 +
 +  /**
 +   * populates all the indexes in the region
 +   */
 +  public void populateIndexes(Collection<Index> indexSet) throws MultiIndexCreationException {
 +    waitBeforeUpdate();
 +    if(region.getCache().getLogger().infoEnabled()) {
 +      region.getCache().getLogger().info("Populating indexes for region " + region.getName());
 +    }
 +    boolean throwException = false;
 +    HashMap<String, Exception> exceptionsMap = new HashMap<String, Exception>();
 +    try {
 +      Iterator entryIter = ((LocalRegion) region).getBestIterator(true);
 +      while (entryIter.hasNext()) {
 +        RegionEntry entry = (RegionEntry) entryIter.next();
 +        if (entry == null || entry.isInvalidOrRemoved()) {
 +          continue;
 +        }
 +        // Fault in the value once before index update so that every index
 +        // update does not have
 +        // to read the value from disk every time.
 +        // TODO OFFHEAP: this optimization (calling getValue to make sure it is faulted in to disk) has a performance problem.
 +        // It also decompresses and deserializes the value and then throws that away. In the case of a heap region the deserialized
 +        // value would be cached in a VMCachedDeserializable. But for compression and/or off-heap the decompression and/or deserialization
 +        // this call does is lost and has to be done again. We could just add a method to RegionEntry that faults the value in without returning it.
 +        // Even better (but more work): could we create a wrapper around RegionEntry that we create here to wrap "entry" and pass the wrapper to addIndexMapping?
 +        // Any indexes that store a reference to the RegionEntry would need to ask the wrapper for the real one but any of them
 +        // that want the value could get it from the wrapper. The first time the wrapper is asked for the value it could get it from
 +        // the real RegionEntry it wraps and cache a reference to that value. I think that gives us the best of both worlds.
 +        entry.getValue((LocalRegion)this.region);
 +        Iterator<Index> indexSetIterator = indexSet.iterator();
 +        while(indexSetIterator.hasNext()) {
 +          AbstractIndex index = (AbstractIndex) indexSetIterator.next();
 +          if (!index.isPopulated() && index.getType() != IndexType.PRIMARY_KEY) {
 +            if (logger.isDebugEnabled()) {
 +              logger.debug("Adding to index :{}{} value :{}", index.getName(), this.region.getFullPath(), entry.getKey());
 +            }
 +            long start = ((AbstractIndex) index).updateIndexUpdateStats();
 +            try {
 +              index.addIndexMapping(entry);
 +            } catch (IMQException e) {
 +              if(logger.isDebugEnabled()) {
 +                logger.debug("Adding to index failed for: {}, {}", index.getName(), e.getMessage(), e);
 +              }
 +              exceptionsMap.put(index.indexName, e);
 +              indexSetIterator.remove();
 +              throwException = true;
 +            }
 +            ((AbstractIndex) index).updateIndexUpdateStats(start);
 +          }
 +        }
 +      }
 +      setPopulateFlagForIndexes(indexSet);
 +      if (throwException) {
 +        throw new MultiIndexCreationException(exceptionsMap);
 +      }
 +    } finally {
 +      notifyAfterUpdate();
 +    }
 +  }
 +  
 +  /**
 +   * Sets the {@link AbstractIndex#isPopulated} after 
 +   * populating all the indexes in this region
 +   */
 +  public void setPopulateFlagForIndexes(Collection<Index> indexSet) {
 +    for (Object ind : indexSet) {
 +      AbstractIndex index = (AbstractIndex) ind;
-       if (!index.isPopulated() && index.getType() != IndexType.PRIMARY_KEY) {
++      if (!index.isPopulated()) {
 +        index.setPopulated(true);
 +      }
 +    }
 +  }
 +  
 +  public void updateIndexes(RegionEntry entry, int action, int opCode) throws QueryException {
 +    updateIndexes(entry, action, opCode, false);
 +  }
 +
 +  // @todo need more specific list of exceptions
 +  /**
 +   * Callback for IndexManager to update indexes Called from AbstractRegionMap.
 +   * 
 +   * @param entry the RegionEntry being updated
 +   * @param action action to be taken (IndexManager.ADD_ENTRY,
 +   *          IndexManager.UPDATE_ENTRY, IndexManager.REMOVE_ENTRY)
 +   * @param opCode one of IndexProtocol.OTHER_OP, BEFORE_UPDATE_OP, AFTER_UPDATE_OP.
 +   * @throws com.gemstone.gemfire.cache.query.IndexMaintenanceException
 +   */
 +  public void updateIndexes(RegionEntry entry, int action, int opCode, boolean isDiskRecoveryInProgress)
 +      throws QueryException {
 +    if(isDiskRecoveryInProgress) {
 +      assert !((LocalRegion)this.region).isInitialized();
 +    } else {
 +      assert Assert.assertHoldsLock(entry,true);  
 +    }
 +    if(logger.isDebugEnabled()) {
 +      logger.debug("IndexManager.updateIndexes {} + action: {}", entry.getKey(), action);
 +    }
 +    if (entry == null) return;
 +    if (isIndexMaintenanceTypeSynchronous()) {
 +      //System.out.println("Synchronous update");
 +      processAction(entry, action, opCode);
 +    }
 +    else {
 +      //System.out.println("Aynchronous update");
 +      updater.addTask(action, entry, opCode);
 +    }
 +  }
 +
 +  /**
 +   * @param opCode one of IndexProtocol.OTHER_OP, BEFORE_UPDATE_OP, AFTER_UPDATE_OP.
 +   */
 +  private void processAction(RegionEntry entry, int action, int opCode)
 +      throws QueryException {
 +    final long startPA = getCachePerfStats().startIndexUpdate();
 +    DefaultQuery.setPdxReadSerialized(this.region.getCache(), true);
 +    TXStateProxy tx = null;
 +    if (!((GemFireCacheImpl)this.region.getCache()).isClient()) {
 +      tx = ((TXManagerImpl) this.region.getCache().getCacheTransactionManager()).internalSuspend();
 +    }
 +
 +    try {
 +      //Asif: Allow the thread to update iff there is no current index
 +      //creator thread in progress. There will not be any issue if
 +      // allow the updater thread to proceed if there is any index
 +      //creator thread in waiting , but that can cause starvation
 +      //for index creator thread. So we will give priorityto index
 +      //creation thread
 +      if (IndexManager.testHook != null) {
 +        if (logger.isDebugEnabled()) {
 +          logger.debug("IndexManager TestHook is set.");
 +        }
 +        testHook.hook(6); //ConcurrentIndexInitOnOverflowRegionDUnitTest
 +      }
 +      
 +      long start = 0;
 +      boolean indexLockAcquired = false;
 +      switch (action) {
 +        case ADD_ENTRY: {
 +          if (IndexManager.testHook != null) {
 +            if (logger.isDebugEnabled()) {
 +              logger.debug("IndexManager TestHook in ADD_ENTRY.");
 +            }
 +            testHook.hook(5);
 +          }
 +          // this action is only called after update
 +          assert opCode == IndexProtocol.OTHER_OP;
 +          
 +          //Asif The behaviour can arise if an index creation has already
 +          // acted upon a newly added entry , but by the time callback
 +          // occurs , the index is added to the map & thus
 +          // the add operation will now have an effect of update.
 +          // so we need to remove the mapping even if it is an Add action
 +          // as otherwise the new results will get added into the
 +          // old results instead of replacement
 +          Iterator iter = this.indexes.values().iterator();
 +          while (iter.hasNext()) {
 +            Object ind = iter.next();
 +            // Check if the value is instance of FutureTask, this means
 +            // the index is in create phase.
 +            if (ind instanceof FutureTask){
 +              continue;
 +            }
 +            IndexProtocol index = (IndexProtocol)ind;            
 +            
 +            if (((AbstractIndex) index).isPopulated() && index.getType() != IndexType.PRIMARY_KEY) {
 +              // Asif : If the current Index contains an entry inspite
 +              // of add operation , this can only mean that Index
 +              // has already acted on it during creation, so do not
 +              // apply IMQ on it
 +              if (!index.containsEntry(entry)) {
 +                if (logger.isDebugEnabled()) {
 +                  logger.debug("Adding to index: {}{} value: {}", index.getName(), this.region.getFullPath(), entry.getKey());
 +                }
 +                start = ((AbstractIndex) index).updateIndexUpdateStats();
 +
 +                index.addIndexMapping(entry);
 +
 +                ((AbstractIndex) index).updateIndexUpdateStats(start);
 +              }
 +            }
 +          }
 +          break;
 +        }
 +        case UPDATE_ENTRY: {
 +
 +          if (IndexManager.testHook != null) {
 +            if (logger.isDebugEnabled()) {
 +              logger.debug("IndexManager TestHook in UPDATE_ENTRY.");
 +            }
 +            testHook.hook(5);
 +            testHook.hook(9); //QueryDataInconsistencyDUnitTest
 +          }
 +
 +          // this action is only called with opCode AFTER_UPDATE_OP
 +          assert opCode == IndexProtocol.AFTER_UPDATE_OP;
 +          Iterator iter = this.indexes.values().iterator();
 +          while (iter.hasNext()) {
 +            Object ind = iter.next();
 +            // Check if the value is instance of FutureTask, this means
 +            // the index is in create phase.
 +            if (ind instanceof FutureTask){
 +              continue;
 +            }
 +            IndexProtocol index = (IndexProtocol)ind;
 +            
 +            if (((AbstractIndex) index).isPopulated() && index.getType() != IndexType.PRIMARY_KEY) {
 +                if (logger.isDebugEnabled()) {
 +                  logger.debug("Updating index: {}{} value: ", index.getName(), this.region.getFullPath(), entry.getKey());
 +                }
 +                start = ((AbstractIndex) index).updateIndexUpdateStats();
 +
 +                index.addIndexMapping(entry);
 +
 +                ((AbstractIndex) index).updateIndexUpdateStats(start);
 +            }
 +          }
 +          break;
 +        }
 +        case REMOVE_ENTRY: {
 +          
 +          if (IndexManager.testHook != null) {
 +            if (logger.isDebugEnabled()) {
 +              logger.debug("IndexManager TestHook in REMOVE_ENTRY.");
 +            }
 +            testHook.hook(5);
 +            testHook.hook(10);
 +          }
 +          Iterator iter = this.indexes.values().iterator();
 +          while (iter.hasNext()) {
 +            Object ind = iter.next();
 +            // Check if the value is instance of FutureTask, this means
 +            // the index is in create phase.
 +            if (ind instanceof FutureTask) {
 +              continue;
 +            }
 +            IndexProtocol index = (IndexProtocol) ind;
 +  
 +            if (((AbstractIndex) index).isPopulated() && index.getType() != IndexType.PRIMARY_KEY) {
 +              AbstractIndex abstractIndex = (AbstractIndex) index;
 +              if (logger.isDebugEnabled()) {
 +                logger.debug("Removing from index: {}{} value: {}", index.getName(), this.region.getFullPath(), entry.getKey());
 +              }
 +              start = ((AbstractIndex) index).updateIndexUpdateStats();
 +              
 +              index.removeIndexMapping(entry, opCode);
 +
 +              ((AbstractIndex) index).updateIndexUpdateStats(start);
 +            }
 +          }
 +          break;
 +        }
 +        default: {
 +          throw new IndexMaintenanceException(LocalizedStrings.IndexManager_INVALID_ACTION.toLocalizedString());
 +        }
 +      }
 +    }
 +    finally {
 +      DefaultQuery.setPdxReadSerialized(this.region.getCache(), false);
 +      if (tx != null) {
 +        ((TXManagerImpl) this.region.getCache().getCacheTransactionManager())
 +          .resume(tx);
 +      }
 +      getCachePerfStats().endIndexUpdate(startPA);
 +    }
 +  }
 +
 +  private void waitBeforeUpdate() {
 +    synchronized (indexes) {
 +      ++numCreators;
 +      // Asif : If there exists any updater thread in progress
 +      // we should not allow index creation to proceed.
 +      while (numUpdatersInProgress > 0) {
 +        ((LocalRegion) getRegion()).getCancelCriterion()
 +            .checkCancelInProgress(null);
 +        boolean interrupted = Thread.interrupted();
 +        try {
 +          indexes.wait();
 +        } catch (InterruptedException ie) {
 +          interrupted = true;
 +        } finally {
 +          if (interrupted) {
 +            Thread.currentThread().interrupt();
 +          }
 +        }
 +      } // while
 +    }
 +  }
 +  
 +  private void notifyAfterUpdate() {
 +    synchronized (indexes) {
 +      --numCreators;
 +      // ASIF: If the creator is in progress , this itself
 +      // means that there is no Update active. The updater threads
 +      // are either in wait state or there are no threads at all.
 +      // Since we do not want any update to progress , if there is
 +      // any creator thread in lock seeking mode ( meaning that it has
 +      // entered the previous synch block) . We will not issue
 +      // any notify till the creator count drops to zero &
 +      // also unless there is any updater thread in waiting
 +      if (numCreators == 0 && numUpdatersInWaiting > 0) {
 +        indexes.notifyAll();
 +      }
 +    }
 +  }
 +  
 +  /**
 +   * Recreates all indexes for this region. This operation blocks all updates
 +   * on all indexes while recreate is in progress. This is required as recreate
 +   * does NOT lock region entries before index update and hence might cause
 +   * inconsistencies in index if concurrent region entry operations are
 +   * going on.
 +   *  
 +   */
 +  private void recreateAllIndexesForRegion() {
 +
 +    long start = 0;
 +    waitBeforeUpdate();
 +    try {
 +      // opCode is ignored for this operation
 +      Iterator iter = this.indexes.values().iterator();
 +      while (iter.hasNext()) {
 +        Object ind = iter.next();
 +        // Check if the value is instance of FutureTask, this means
 +        // the index is in create phase.
 +        if (ind instanceof FutureTask) {
 +          continue;
 +        }
 +        IndexProtocol index = (IndexProtocol) ind;
 +        if (index.getType() == IndexType.FUNCTIONAL || index.getType() == IndexType.HASH) {
 +          AbstractIndex aIndex = ((AbstractIndex) index);
 +          start = ((AbstractIndex) index).updateIndexUpdateStats();
 +          ((AbstractIndex) index).recreateIndexData();
 +          ((AbstractIndex) index).updateIndexUpdateStats(start);
 +
 +        }
 +      }
 +    } catch (Exception e) {
 +      throw new IndexInvalidException(e);
 +    } finally {
 +      notifyAfterUpdate();
 +    }
 +  }
 +
 +  /**
 +   * Wait for index initialization before entry create, update, invalidate or destroy
 +   * operation.
 +   * 
 +   * Note: If the region has a disk region then we should wait for index
 +   * initialization before getting region entry lock to avoid deadlock (#44431).
 +   */
 +  public void waitForIndexInit() {
 +    synchronized (this.indexes) {
 +      ++this.numUpdatersInWaiting;
 +      while (this.numCreators > 0) {
 +        ((LocalRegion)this.getRegion()).getCancelCriterion().checkCancelInProgress(null);
 +        boolean interrupted = Thread.interrupted();
 +        try {
 +          this.indexes.wait();
 +        }
 +        catch (InterruptedException ie) {
 +          interrupted = true;
 +        }
 +        finally {
 +          if (interrupted) {
 +            Thread.currentThread().interrupt();
 +          }
 +        }
 +      } // while
 +      --this.numUpdatersInWaiting;
 +      ++this.numUpdatersInProgress;
 +    }
 +  }
 +
 +  /**
 +   * Necessary finally block call for above method.
 +   */
 +  public void countDownIndexUpdaters() {
 +    synchronized (this.indexes) {
 +       --this.numUpdatersInProgress;
 +      //Asif: Since Index creator threads can progress only if
 +      // there is no update threads in progress, thus we need to issue
 +      // notify all iff there are any creator threads in action & also
 +      // if the upDateInProgress Count has dipped to 0
 +      if (this.numUpdatersInProgress == 0 && this.numCreators > 0) {
 +        this.indexes.notifyAll();
 +      }
 +    }
 +  }
 +
 +  private CachePerfStats getCachePerfStats() {
 +    return ((LocalRegion)this.region).getCachePerfStats();
 +  }
 +  /**
 +   * Callback for destroying IndexManager Called after Region.destroy() called
 +   */
 +  public void destroy() throws QueryException {
 +    this.indexes.clear();
 +    if (!isIndexMaintenanceTypeSynchronous()) updater.shutdown();
 +  }
 +  
 +  /**
 +   * Removes indexes for a destroyed bucket region from the list of bucket indexes 
 +   * in the {@link PartitionedIndex}.
 +   * @param prRegion the partition region that this bucket belongs to
 +   * @throws QueryException
 +   */
 +  public void removeBucketIndexes(PartitionedRegion prRegion) throws QueryException {
 +    IndexManager parentManager = prRegion.getIndexManager();
 +    if (parentManager != null) {
 +      Iterator bucketIndexIterator = indexes.values().iterator();
 +      while (bucketIndexIterator.hasNext()) {
 +        Index bucketIndex = (Index)bucketIndexIterator.next();
 +        Index prIndex = parentManager.getIndex(bucketIndex.getName());
 +        if (prIndex instanceof PartitionedIndex) {
 +          ((PartitionedIndex)prIndex).removeFromBucketIndexes(this.region, bucketIndex);
 +        }
 +      }
 +    }
 +  }
 +
 +  @Override
 +  public String toString() {
 +    StringBuffer sb = new StringBuffer();
 +    Iterator iter = this.indexes.values().iterator();
 +    while (iter.hasNext()) {
 +      Object ind = iter.next();
 +      // Check if the value is instance of FutureTask, this means
 +      // the index is in create phase.
 +      if (ind instanceof FutureTask){
 +        continue;
 +      }
 +      sb.append(((Index)ind).toString()).append('\n');    
 +    }
 +    return sb.toString();
 +  }
 +
 +  public boolean isIndexMaintenanceTypeSynchronous() {
 +    return this.indexMaintenanceSynchronous;
 +  }
 +
 +  public boolean isOverFlowRegion() {
 +    return this.isOverFlowToDisk;
 +  }
 +  public boolean isOffHeap() {
 +    return this.offHeap;
 +  }
 +
 +  public static boolean isObjectModificationInplace() {
 +    return (INPLACE_OBJECT_MODIFICATION || INPLACE_OBJECT_MODIFICATION_FOR_TEST);
 +  }
 +  /**
 +   * Asif : This function is used exclusively by Index Manager. It gets the
 +   * unique Iterator name for a Iterator definition, if it already exists, else
 +   * creates a unqiue name & also stores it in a map for subsequent use
 +   * 
 +   * @param definition String containing definition of the iterator
 +   * @return String containing the name of the Iterator
 +   */
 +  public String putCanonicalizedIteratorNameIfAbsent(String definition) {
 +    String str = null;
 +    synchronized(canonicalizedIteratorNameMap) {
 +      if ((str = (String) this.canonicalizedIteratorNameMap.get(definition)) == null) {
 +        str = new StringBuffer("index_iter").append(this.getIncrementedCounter())
 +            .toString();
 +        String temp;
 +        if ((temp = (String) this.canonicalizedIteratorNameMap.putIfAbsent(
 +            definition, str)) != null) {
 +          str = temp;
 +        }
 +      }
 +    }
 +    return str;
 +  }
 +
 +  public void putCanonicalizedIteratorName(String definition, String name) {
 +    synchronized(canonicalizedIteratorNameMap) {
 +      this.canonicalizedIteratorNameMap.put(definition, name);
 +    }
 +  }
 +  
 +  private synchronized int getIncrementedCounter() {
 +    return ++this.iternameCounter;
 +  }
 +
 +  /**
 +   * Asif : Given a definition returns the canonicalized iterator name for the
 +   * definition. If the definition does not exist , null is returned
 +   * 
 +   * @param definition
 +   * @return String
 +   */
 +  public String getCanonicalizedIteratorName(String definition) {
 +    return ((String) (this.canonicalizedIteratorNameMap.get(definition)));
 +  }
 +
 +  ////////////////////// Inner Classes //////////////////////
 + 
 +  public class IndexUpdaterThread extends Thread  {
 +
 +    private volatile boolean running = true;
 +
 +    private volatile boolean shutdownRequested = false;
 +
 +    private volatile BlockingQueue pendingTasks;
 +
 +    /**
 +     * Creates instance of IndexUpdaterThread
 +     * @param updateThreshold
 +     * @param threadName
 +     */
 +    IndexUpdaterThread(ThreadGroup group, int updateThreshold, String threadName) {
 +      super(group, threadName);
 +      // Check if threshold is set.
 +      if (updateThreshold > 0){
 +        // Create a bounded queue.
 +        pendingTasks = new ArrayBlockingQueue(updateThreshold);
 +      } else {
 +        // Create non-bounded queue.
 +        pendingTasks = new LinkedBlockingQueue();
 +      }
 +      this.setDaemon(true);
 +    }
 +
 +    public void addTask(int action, RegionEntry entry, int opCode) {
 +      Object[] task = new Object[3];
 +      task[0] = Integer.valueOf(action);
 +      task[1] = entry;
 +      task[2] = Integer.valueOf(opCode);  // !!!:ezoerner:20081029 change to valueOf jdk 1.5+
 +      pendingTasks.add(task);
 +    }
 +
 +    /**
 +     * Stops this thread. Does not return until it has stopped.
 +     */
 +    public void shutdown() {
 +      if (!this.running) {
 +        return;
 +      }
 +      this.shutdownRequested = true;
 +      this.interrupt();
 +      try {
 +        this.join();
 +      } catch (InterruptedException ignore) {
 +        Thread.currentThread().interrupt();
 +        // just return, we're done
 +      }
 +    }
 +
 +    @Override
 +    public void run() {
 +      // async writers main loop
 +      // logger.debug("DiskRegion writer started (writer=" + this + ")");
 +      com.gemstone.gemfire.CancelCriterion stopper = ((LocalRegion)region).getCancelCriterion();
 +      try {
 +        while (!this.shutdownRequested) {
 +          // Termination checks
 +          SystemFailure.checkFailure();
 +          if (stopper.cancelInProgress() != null) {
 +            break;
 +          }
 +          try {
 +            Object[] task = (Object[])pendingTasks.take();
 +            if (this.shutdownRequested) {
 +              break;
 +            }
 +            updateIndexes(task);
 +          }
 +          catch (InterruptedException ignore) {
 +            return; // give up (exit the thread)
 +          }
 +        }
 +      }
 +      finally {
 +        this.running = false;
 +      }
 +    }
 +
 +    private void updateIndexes(Object[] task) {
 +      int action = ((Integer)task[0]).intValue();
 +      RegionEntry entry = (RegionEntry)task[1];
 +      int opCode = ((Integer)task[2]).intValue();
 +      //System.out.println("entry = "+entry.getKey());
 +      if (entry != null || action == RECREATE_INDEX) {
 +        try {
 +          if (action == RECREATE_INDEX) {
 +            recreateAllIndexesForRegion();
 +          } else {
 +            if (entry != null) {
 +              entry.setUpdateInProgress(true);
 +            }
 +            processAction(entry, action, opCode);
 +          }
 +        }
 +        catch (Exception e) {
 +          e.printStackTrace();
 +        } finally {
 +          if (entry != null && action != RECREATE_INDEX) {
 +            entry.setUpdateInProgress(false);
 +          }
 +        }
 +      }
 +    }
 +
 +    /**
 +     * Used by tests to determine if the updater thread has finished updating
 +     * its indexes. The list is cleared without synchronization, which makes
 +     * this methods somewhat unsafe from a threading point of view.
 +     */
 +    public synchronized boolean isDone() {
 +      return this.pendingTasks.size() == 0;
 +    }
 +
 +  }
 +  
 +  /**
 +   * Index Task used to create the index. This is used along with the
 +   * FutureTask to take care of, same index creation request from multiple
 +   * threads. At any time only one thread succeeds and other threads waits
 +   * for the completion of the index creation. This avoids usage of
 +   * synchronization which could block any index creation.
 +   */
 +  public class IndexTask implements Callable<Index> {
 +
 +    public String indexName;
 +
 +    public IndexType indexType;
 +
 +    public IndexCreationHelper helper;
 +
 +    public String origFromClause;
 +
 +    public String  origIndexedExpression;
 +
 +    public boolean isCompactOrHash = false;
 +    
 +    public boolean isLDM = false;
 +    
 +    public PartitionedIndex prIndex;
 +    
 +    public boolean loadEntries;
 +    
 +    IndexTask (String indexName, IndexType type, String origFromClause, String origIndexedExpression, IndexCreationHelper helper, boolean isCompactOrHash, PartitionedIndex prIndex, boolean loadEntries){
 +      this.indexName = indexName;
 +      this.indexType = type;
 +      this.origFromClause = origFromClause;
 +      this.origIndexedExpression = origIndexedExpression;
 +      this.helper = helper;
 +      this.isCompactOrHash = isCompactOrHash;
 +      this.prIndex = prIndex;
 +      this.loadEntries = loadEntries;
 +    }
 +
 +    /* For name based index search */
 +    IndexTask (String indexName){
 +      this.indexName = indexName;
 +    }
 +
 +    @Override
 +    public boolean equals (Object other){
 +      if (other == null) {
 +        return false;
 +      }
 +      IndexTask otherIndexTask = (IndexTask) other;
 +      // compare indexName.
 +      if (this.indexName.equals(otherIndexTask.indexName)){
 +        return true;
 +      }
 +
 +      if (otherIndexTask.helper == null || this.helper == null) {
 +        return false;
 +      }
 +
 +      String[] indexDefinitions = this.helper.getCanonicalizedIteratorDefinitions();
 +      int[] mapping = new int[indexDefinitions.length];
 +      // compare index based on its type, expression and definition.
 +      if(compareIndexData(this.indexType, indexDefinitions,
 +          this.helper.getCanonicalizedIndexedExpression(), otherIndexTask.indexType,
 +          otherIndexTask.helper.getCanonicalizedIteratorDefinitions(),
 +          otherIndexTask.helper.getCanonicalizedIndexedExpression(), mapping) == 0)
 +      {
 +        return true;
 +      }
 +      return false;
 +    }
 +
 +    public int hashCode(){
 +      // It returns a constant number as the equality check is based on
 +      // the OR condition between the indexName and its characteristics
 +      // (involving type, expression and definition), because of this
 +      // its not possible to come-up with an accurate hash code.
 +      return 99;
 +    }
 +
 +    /*
 +     * Creates and initializes the index.
 +     */
 +    public Index call() {
 +      Index index = null;
 +      String indexedExpression = helper.getCanonicalizedIndexedExpression();
 +      String fromClause = helper.getCanonicalizedFromClause();
 +      String projectionAttributes = helper.getCanonicalizedProjectionAttributes();
 +      String[] definitions = helper.getCanonicalizedIteratorDefinitions();
 +      IndexStatistics stats = null;    
 +      this.isLDM = IndexManager.IS_TEST_LDM;
 +
 +      if (this.prIndex != null) {
 +        stats = this.prIndex.getStatistics();
 +      }
 +      //Hash index not supported for overflow but we "thought" we were so let's maintain backwards compatibility
 +      //and create a regular compact range index instead
 +      if (indexType == IndexType.HASH && isOverFlowRegion()) {
 +        indexType = IndexType.FUNCTIONAL;
 +      }
 +      if (indexType == IndexType.PRIMARY_KEY) {
 +        index = new PrimaryKeyIndex(indexName, region, fromClause,indexedExpression,
 +             projectionAttributes, origFromClause,
 +            origIndexedExpression, definitions, stats);
 +        logger.info("Using Primary Key index implementation for '{}' on region {}", indexName, region.getFullPath());
 +      } else if (indexType == IndexType.HASH){
 +        index = new HashIndex(indexName, region, fromClause,
 +            indexedExpression,  projectionAttributes, origFromClause,
 +            origIndexedExpression, definitions, stats);
 +        
 +        logger.info("Using Hash index implementation for '{}' on region {}", indexName, region.getFullPath());
 +      }
 +      else {
 +        //boolean isCompact = !helper.isMapTypeIndex() && shouldCreateCompactIndex((FunctionalIndexCreationHelper)helper);
 +        if (this.isCompactOrHash || this.isLDM) {
 +          if (indexType == IndexType.FUNCTIONAL && !helper.isMapTypeIndex()) {
 +            index = new CompactRangeIndex(indexName, region, fromClause,
 +                indexedExpression,  projectionAttributes, origFromClause,
 +                origIndexedExpression, definitions, stats);
 +            logger.info("Using Compact Range index implementation for '{}' on region {}", indexName, region.getFullPath());       
 +          }
 +          else {
 +            FunctionalIndexCreationHelper fich = (FunctionalIndexCreationHelper)helper;            
 +            index = new CompactMapRangeIndex(indexName, region, fromClause, indexedExpression,
 +                projectionAttributes, origFromClause,
 +               origIndexedExpression, definitions, fich.isAllKeys(), fich.multiIndexKeysPattern,
 +               fich.mapKeys, stats);
 +            logger.info("Using Compact Map Range index implementation for '{}' on region {}", indexName, region.getFullPath());   
 +          }
 +        }
 +        else {
 +          assert indexType == IndexType.FUNCTIONAL;
 +          if ( !helper.isMapTypeIndex() ) {
 +            index = new RangeIndex(indexName, region, fromClause, 
 +                indexedExpression,   projectionAttributes, origFromClause,
 +                origIndexedExpression, definitions, stats);
 +            logger.info("Using Non-Compact index implementation for '{}' on region {}", indexName, region.getFullPath());   
 +          }
 +          else {
 +            FunctionalIndexCreationHelper fich = (FunctionalIndexCreationHelper)helper;            
 +            index = new MapRangeIndex(indexName, region, fromClause, indexedExpression,
 +                 projectionAttributes, origFromClause,
 +                origIndexedExpression, definitions, fich.isAllKeys(), fich.multiIndexKeysPattern,
 +                fich.mapKeys, stats);
 +            logger.info("Using Non-Compact Map index implementation for '{}' on region {}", indexName, region.getFullPath());
 +          }
 +        }
 +      }
 +      ((AbstractIndex)index).setPRIndex(prIndex);
 +     
 +      if (index.getType() != IndexType.PRIMARY_KEY) {
 +        AbstractIndex aIndex = ((AbstractIndex) index);
 +        aIndex.instantiateEvaluator(helper);
 +        waitBeforeUpdate();
 +        boolean indexCreatedSuccessfully = false;
 +        try {
 +          ((LocalRegion)region).setFlagForIndexCreationThread(true);
 +          aIndex.initializeIndex(loadEntries);
 +          logger.info((loadEntries ? "Initialized and loaded entries into the index "
 +                        : "Initialized but entries not yet loaded into the index "
 +                            + indexName + " on region: " + region.getFullPath()));
 +          aIndex.markValid(true);
 +          indexCreatedSuccessfully = true;
 +          if(loadEntries) {
 +            aIndex.setPopulated(true);
 +            if (this.prIndex != null) {
 +              ((AbstractIndex)this.prIndex).setPopulated(true);
 +            }
 +          }
 +          indexes.put(this, index);
 +          if (region instanceof BucketRegion && prIndex != null) {
 +            prIndex.addToBucketIndexes(region, index);
 +            prIndex.incNumBucketIndexes();
 +          }
 +        } catch (Exception e) {
 +          throw new IndexInvalidException(e);
 +        }
 +        finally {
 +          notifyAfterUpdate();
 +          ((LocalRegion)region).setFlagForIndexCreationThread(false);
 +          if (!indexCreatedSuccessfully){
 +            ((InternalIndexStatistics)index.getStatistics()).close();
 +          }
 +        }
 +      } else {
 +        // For PrimaryKey index
 +        ((AbstractIndex)index).setPopulated(true);
 +        indexes.put(this, index);
 +        if (region instanceof BucketRegion && prIndex != null) {
 +          prIndex.addToBucketIndexes(region, index);
 +        }
++        if (this.prIndex != null) {
++          ((AbstractIndex)this.prIndex).setPopulated(true);
++        }
 +      }
 +      return index;
 +    }
 +  }   
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/PrimaryKeyIndexCreationHelper.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/PrimaryKeyIndexCreationHelper.java
index b43f491,0000000..8c54f19
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/PrimaryKeyIndexCreationHelper.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/PrimaryKeyIndexCreationHelper.java
@@@ -1,124 -1,0 +1,135 @@@
 +/*
 + * 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.
 + */
 +/*
 + * PrimaryIndexCreationHelper.java
 + *
 + * Created on March 20, 2005, 7:21 PM
 + */
 +package com.gemstone.gemfire.cache.query.internal.index;
 +
- import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
++import java.util.List;
++
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.query.IndexInvalidException;
- import com.gemstone.gemfire.cache.query.internal.*;
++import com.gemstone.gemfire.cache.query.internal.CompiledIteratorDef;
++import com.gemstone.gemfire.cache.query.internal.CompiledValue;
++import com.gemstone.gemfire.cache.query.internal.ExecutionContext;
++import com.gemstone.gemfire.cache.query.internal.RuntimeIterator;
 +import com.gemstone.gemfire.cache.query.internal.parse.OQLLexerTokenTypes;
- import java.util.List;
++import com.gemstone.gemfire.internal.cache.PartitionedRegion;
++import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +
 +/**
 + * 
 + * @author vaibhav
 + */
 +public class PrimaryKeyIndexCreationHelper extends IndexCreationHelper  {
 +
 +  ExecutionContext context = null;
 +  
 +  final Region region;
 +
 +  public PrimaryKeyIndexCreationHelper(String fromClause,
 +      String indexedExpression, String projectionAttributes, Cache cache,
 +      ExecutionContext  externalContext, IndexManager imgr) throws IndexInvalidException {
 +    super(fromClause, projectionAttributes, cache);
 +    if( externalContext == null) {
 +      context = new ExecutionContext(null, cache);
 +    }else {
 +      this.context = externalContext;
 +    }
 +    context.newScope(1);
 +    this.region = imgr.region;
 +    prepareFromClause( imgr);
 +    prepareIndexExpression(indexedExpression);
 +    prepareProjectionAttributes(projectionAttributes);
 +  }
 +
 +  private void prepareFromClause(IndexManager imgr)
 +      throws IndexInvalidException {
 +    List list = this.compiler.compileFromClause(fromClause);
 +    if (list.size() > 1) { throw new IndexInvalidException(LocalizedStrings.PrimaryKeyIndexCreationHelper_THE_FROMCLAUSE_FOR_A_PRIMARY_KEY_INDEX_SHOULD_ONLY_HAVE_ONE_ITERATOR_AND_THE_COLLECTION_MUST_BE_A_REGION_PATH_ONLY.toLocalizedString()); }
 +    try {
 +      CompiledIteratorDef iterDef = (CompiledIteratorDef) list.get(0);
 +      if (iterDef.getCollectionExpr().getType() != OQLLexerTokenTypes.RegionPath) { throw new IndexInvalidException(LocalizedStrings.PrimaryKeyIndexCreationHelper_THE_FROMCLAUSE_FOR_A_PRIMARY_KEY_INDEX_SHOULD_BE_A_REGION_PATH_ONLY.toLocalizedString()); }
 +      iterDef.computeDependencies(this.context);
 +      RuntimeIterator rIter = (iterDef.getRuntimeIterator(this.context));
 +      String definition = rIter.getDefinition();
 +      this.canonicalizedIteratorDefinitions = new String[1];
 +      this.canonicalizedIteratorDefinitions[0] = definition;
-       //    Asif: Bind the Index_Internal_ID to the RuntimeIterator
-       String name = imgr.putCanonicalizedIteratorNameIfAbsent(definition);
++      //    Asif: Bind the Index_Internal_ID to the RuntimeIterator      
++      PartitionedRegion pr = this.context.getPartitionedRegion();
++      this.canonicalizedIteratorNames = new String[1];
++      String name = null;
++      if (pr != null) {
++        name = pr.getIndexManager().putCanonicalizedIteratorNameIfAbsent(definition);
++      } else {
++        name = imgr.putCanonicalizedIteratorNameIfAbsent(definition);
++      }
 +      rIter.setIndexInternalID(name);
 +      this.canonicalizedIteratorNames = new String[1];
 +      this.canonicalizedIteratorNames[0] = name;
-       this.fromClause = new StringBuffer(definition).append(' ').append(name)
-           .toString();
++      this.fromClause = new StringBuffer(definition).append(' ').append(name).toString();
 +      context.bindIterator(rIter);
 +    }
 +    catch (IndexInvalidException e) {
 +      throw e; //propagate
 +    }
 +    catch (Exception e) {
 +      throw new IndexInvalidException(e); // wrap any other exceptions
 +    }
 +  }
 +
 +  private void prepareIndexExpression(String indexedExpression)
 +      throws IndexInvalidException {
 +    List indexedExprs = this.compiler
 +        .compileProjectionAttributes(indexedExpression);
 +    if (indexedExprs == null || indexedExprs.size() != 1) { throw new IndexInvalidException(LocalizedStrings.PrimaryKeyIndexCreationHelper_INVALID_INDEXED_EXPRESSOION_0.toLocalizedString(indexedExpression)); }
 +    CompiledValue expr = (CompiledValue) ((Object[]) indexedExprs.get(0))[1];
 +    if (expr.getType() == CompiledValue.LITERAL)
 +        throw new IndexInvalidException(LocalizedStrings.PrimaryKeyIndexCreationHelper_INVALID_INDEXED_EXPRESSOION_0.toLocalizedString(indexedExpression));
 +    try {
 +      StringBuffer sb = new StringBuffer();
 +      expr.generateCanonicalizedExpression(sb, context);
 +      this.indexedExpression = sb.toString();
 +    }
 +    catch (Exception e) {
 +      //e.printStackTrace();
 +      throw new IndexInvalidException(LocalizedStrings.PrimaryKeyIndexCreationHelper_INVALID_INDEXED_EXPRESSOION_0_N_1.toLocalizedString(new Object[] {indexedExpression, e.getMessage()}));
 +    }
 +  }
 +
 +  private void prepareProjectionAttributes(String projectionAttributes)
 +      throws IndexInvalidException {
 +    if (projectionAttributes != null && !projectionAttributes.equals("*")) { throw new IndexInvalidException(LocalizedStrings.PrimaryKeyIndexCreationHelper_INVALID_PROJECTION_ATTRIBUTES_0.toLocalizedString(projectionAttributes)); }
 +    this.projectionAttributes = projectionAttributes;
 +  }
 +
 +  public Region getRegion() {
 +    return region;
 +  }
 +
 +  public List getIterators() {
 +    return null;
 +  }
 +
 +  public CompiledValue getCompiledIndexedExpression() {
 +    return null;
 +  }
 +  
 +}


[10/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache/query/internal/index/PutAllWithIndexPerfDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache/query/internal/index/PutAllWithIndexPerfDUnitTest.java
index fd50e63,0000000..960bd4e
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache/query/internal/index/PutAllWithIndexPerfDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache/query/internal/index/PutAllWithIndexPerfDUnitTest.java
@@@ -1,216 -1,0 +1,216 @@@
 +/*
 + * 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.cache.query.internal.index;
 +
 +import java.io.IOException;
 +import java.util.HashMap;
 +import java.util.Map;
 +import java.util.Properties;
 +
 +import org.junit.Ignore;
 +import org.junit.experimental.categories.Category;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheFactory;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.client.ClientCache;
 +import com.gemstone.gemfire.cache.client.ClientCacheFactory;
 +import com.gemstone.gemfire.cache.client.ClientRegionShortcut;
 +import com.gemstone.gemfire.cache.query.data.PortfolioPdx;
 +import com.gemstone.gemfire.cache.query.dunit.RemoteQueryDUnitTest;
 +import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.cache30.CacheSerializableRunnable;
 +import com.gemstone.gemfire.cache30.CacheTestCase;
 +import com.gemstone.gemfire.internal.AvailablePort;
 +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.NetworkUtils;
 +import com.gemstone.gemfire.test.dunit.VM;
 +import com.gemstone.gemfire.test.junit.categories.DistributedTest;
 +
 +/**
 + * @author shobhit
 + *
 + */
 +@Category(DistributedTest.class)
 +@Ignore("Test was disabled by renaming to DisabledTest")
 +public class PutAllWithIndexPerfDUnitTest extends CacheTestCase {
 +
 +  /** The port on which the bridge server was started in this VM */
 +  private static int bridgeServerPort;
 +  static long timeWithoutStructTypeIndex = 0;
 +  static long timeWithStructTypeIndex = 0;
 +  
 +  public PutAllWithIndexPerfDUnitTest(String name) {
 +    super(name);
 +  }
 +
 +  public void setUp() throws Exception {
 +    super.setUp();
 +    disconnectAllFromDS();
 +  }
 +
 +  @Override
 +  protected final void postTearDownCacheTestCase() throws Exception {
 +    disconnectAllFromDS();
 +  }
 +
 +  public void testPutAllWithIndexes() {
 +    final String name = "testRegion";
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    final int numberOfEntries = 10000;
 +
 +    // Start server
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +        public void run2() throws CacheException {
 +          Properties config = new Properties();
 +          config.put("locators", "localhost["+DistributedTestUtils.getDUnitLocatorPort()+"]");
 +          Cache cache = new CacheFactory(config).create();
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.LOCAL);
 +          cache.createRegionFactory(factory.create()).create(name);
 +          try {
 +            startBridgeServer(0, false);
 +          } catch (Exception ex) {
 +            Assert.fail("While starting CacheServer", ex);
 +          }
 +          //Create Index on empty region
 +          try {
 +            cache.getQueryService().createIndex("idIndex", "ID", "/"+name);
 +          } catch (Exception e) {
 +            Assert.fail("index creation failed", e);
 +          }
 +        }
 +      });
 +
 +    // Create client region
-     final int port = vm0.invokeInt(PutAllWithIndexPerfDUnitTest.class, "getCacheServerPort");
++    final int port = vm0.invoke(() -> PutAllWithIndexPerfDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +    vm1.invoke(new CacheSerializableRunnable("Create region") {
 +        public void run2() throws CacheException {
 +          Properties config = new Properties();
 +          config.setProperty("mcast-port", "0");
 +          ClientCache cache = new ClientCacheFactory().addPoolServer(host0, port).create();
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.LOCAL);
 +          cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(name);
 +        }
 +      });
 +
 +    vm1.invoke(new CacheSerializableRunnable("putAll() test") {
 +      
 +      @Override
 +      public void run2() throws CacheException {
 +        Region exampleRegion = ClientCacheFactory.getAnyInstance().getRegion(name);
 +
 +        Map warmupMap = new HashMap();
 +        Map data =  new HashMap();
 +        for(int i=0; i<10000; i++){
 +          Object p = new PortfolioPdx(i);
 +          if (i < 1000) warmupMap.put(i, p);
 +          data.put(i, p);
 +        }
 +        
 +        for (int i=0; i<10; i++) {
 +          exampleRegion.putAll(warmupMap);
 +        }
 +        
 +        long start = System.currentTimeMillis();
 +        for (int i=0; i<10; i++) {
 +          exampleRegion.putAll(data);
 +        }
 +        long end = System.currentTimeMillis();
 +        timeWithoutStructTypeIndex = ((end-start)/10);
 +        System.out.println("Total putall time for 10000 objects is: "+ ((end-start)/10) + "ms");
 + 
 +      }
 +    });
 +    
 +    vm0.invoke(new CacheSerializableRunnable("Remove Index and create new one") {
 +      
 +      @Override
 +      public void run2() throws CacheException {
 +        try {
 +          Cache cache = CacheFactory.getAnyInstance();
 +          cache.getQueryService().removeIndexes();
 +          cache.getRegion(name).clear();
 +          cache.getQueryService().createIndex("idIndex", "p.ID", "/"+name+" p");
 +        } catch (Exception e) {
 +          Assert.fail("index creation failed", e);
 +        }
 +      }
 +    });
 +
 +    vm1.invoke(new CacheSerializableRunnable("putAll() test") {
 +      
 +      @Override
 +      public void run2() throws CacheException {
 +        Region exampleRegion = ClientCacheFactory.getAnyInstance().getRegion(name);
 +        exampleRegion.clear();
 +        Map warmupMap = new HashMap();
 +        Map data =  new HashMap();
 +        for(int i=0; i<10000; i++){
 +          Object p = new PortfolioPdx(i);
 +          if (i < 1000) warmupMap.put(i, p);
 +          data.put(i, p);
 +        }
 +        
 +        for (int i=0; i<10; i++) {
 +          exampleRegion.putAll(warmupMap);
 +        }
 +        
 +        long start = System.currentTimeMillis();
 +        for (int i=0; i<10; i++) {
 +          exampleRegion.putAll(data);
 +        }
 +        long end = System.currentTimeMillis();
 +        timeWithStructTypeIndex  = ((end-start)/10);
 +        System.out.println("Total putall time for 10000 objects is: "+ ((end-start)/10) + "ms");
 + 
 +      }
 +    });
 +    
 +    if (timeWithoutStructTypeIndex > timeWithStructTypeIndex) {
 +      fail("putAll took more time without struct type index than simple index");
 +    }
 +  }
 +
 +  /**
 +   * Starts a bridge server on the given port, using the given
 +   * deserializeValues and notifyBySubscription to serve up the
 +   * given region.
 +   */
 +  protected void startBridgeServer(int port, boolean notifyBySubscription)
 +    throws IOException {
 +
 +    Cache cache = CacheFactory.getAnyInstance();
 +    CacheServer bridge = cache.addCacheServer();
 +    bridge.setPort(port);
 +    bridge.start();
 +    bridgeServerPort = bridge.getPort();
 +  }
 +
 +  private static int getCacheServerPort() {
 +    return bridgeServerPort;
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/Bug34387DUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/Bug34387DUnitTest.java
index 9c35b1b,0000000..879600d
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/Bug34387DUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/Bug34387DUnitTest.java
@@@ -1,174 -1,0 +1,174 @@@
 +/*
 + * 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.cache30;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheListener;
 +import com.gemstone.gemfire.cache.CacheTransactionManager;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.EntryEvent;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.UnsupportedOperationInTransactionException;
 +import com.gemstone.gemfire.cache.util.CacheListenerAdapter;
 +import com.gemstone.gemfire.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.VM;
 +
 +/**
 + * Test create + localDestroy for bug 34387
 + *
 + * @author darrel
 + * @since 5.0
 + */
 +public class Bug34387DUnitTest extends CacheTestCase {
 +
 +//  private transient Region r;
 +//  private transient DistributedMember otherId;
 +  protected transient int invokeCount;
 +  
 +  static volatile boolean callbackFailure;
 +  
 +  public Bug34387DUnitTest(String name) {
 +    super(name);
 +  }
 +
 +  protected static void callbackAssertEquals(String message, Object expected, 
 +      Object actual) {
 +    if (expected == null && actual == null)
 +      return;
 +    if (expected != null && expected.equals(actual))
 +      return;
 +    callbackFailure = true;
 +    // Throws an error that is ignored, but...
 +    assertEquals(message, expected, actual);
 +  }
 +  
 +
 +  private VM getOtherVm() {
 +    Host host = Host.getHost(0);
 +    return host.getVM(0);
 +  }
 +    
 +  private void initOtherId() {
 +    VM vm = getOtherVm();
 +    vm.invoke(new CacheSerializableRunnable("Connect") {
 +        public void run2() throws CacheException {
 +          getCache();
 +        }
 +      });
-     vm.invoke(Bug34387DUnitTest.class, "getVMDistributedMember");
++    vm.invoke(() -> Bug34387DUnitTest.getVMDistributedMember());
 +  }
 +  private void doCommitOtherVm(final boolean doDestroy) {
 +    VM vm = getOtherVm();
 +    vm.invoke(new CacheSerializableRunnable("create root") {
 +        public void run2() throws CacheException {
 +          AttributesFactory af = new AttributesFactory();
 +          af.setScope(Scope.DISTRIBUTED_ACK);
 +          af.setConcurrencyChecksEnabled(true);
 +          Region r1 = createRootRegion("r1", af.create());
 +          CacheTransactionManager ctm =  getCache().getCacheTransactionManager();
 +          ctm.begin();
 +          r1.create("createKey", "createValue");
 +          if (doDestroy) {
 +            try {
 +              r1.localDestroy("createKey");
 +              fail("expected exception not thrown");
 +            } catch (UnsupportedOperationInTransactionException e) {
 +              assertEquals(e.getMessage(), LocalizedStrings.TXStateStub_LOCAL_DESTROY_NOT_ALLOWED_IN_TRANSACTION.toLocalizedString());
 +            }
 +          } else {
 +            try {
 +              r1.localInvalidate("createKey");
 +              fail("expected exception not thrown");
 +            } catch (UnsupportedOperationInTransactionException e) {
 +              assertEquals(e.getMessage(), LocalizedStrings.TXStateStub_LOCAL_INVALIDATE_NOT_ALLOWED_IN_TRANSACTION.toLocalizedString());
 +            }
 +          }
 +          ctm.commit();
 +        }
 +      });
 +  }
 +
 +  public static DistributedMember getVMDistributedMember() {
 +    return InternalDistributedSystem.getAnyInstance().getDistributedMember();
 +  }
 +  
 +  //////////////////////  Test Methods  //////////////////////
 +
 +  /**
 +   * test create followed by localDestroy
 +   */
 +  public void testCreateAndLD() throws CacheException {
 +    initOtherId();
 +    AttributesFactory af = new AttributesFactory();
 +    af.setDataPolicy(DataPolicy.REPLICATE);
 +    af.setScope(Scope.DISTRIBUTED_ACK);
 +    af.setConcurrencyChecksEnabled(true);
 +    callbackFailure = false;
 +    
 +    CacheListener cl1 = new CacheListenerAdapter() {
 +        public void afterCreate(EntryEvent e) {
 +          callbackAssertEquals("Keys not equal", "createKey", e.getKey());
 +          callbackAssertEquals("Values not equal", "createValue", e.getNewValue());
 +          Bug34387DUnitTest.this.invokeCount++;
 +        }
 +      };
 +    af.addCacheListener(cl1);
 +    Region r1 = createRootRegion("r1", af.create());
 +
 +    this.invokeCount = 0;
 +    assertNull(r1.getEntry("createKey"));
 +    doCommitOtherVm(true);
 +    assertNotNull(r1.getEntry("createKey"));
 +    assertEquals("createValue", r1.getEntry("createKey").getValue());
 +    assertEquals(1, this.invokeCount);
 +    assertFalse("Errors in callbacks; check logs for details", callbackFailure);
 +  }
 +  /**
 +   * test create followed by localInvalidate
 +   */
 +  public void testCreateAndLI() throws CacheException {
 +    initOtherId();
 +    AttributesFactory af = new AttributesFactory();
 +    af.setDataPolicy(DataPolicy.REPLICATE);
 +    af.setScope(Scope.DISTRIBUTED_ACK);
 +    af.setConcurrencyChecksEnabled(true);
 +    callbackFailure = false;
 +    
 +    CacheListener cl1 = new CacheListenerAdapter() {
 +        public void afterCreate(EntryEvent e) {
 +          callbackAssertEquals("key not equal", "createKey", e.getKey());
 +          callbackAssertEquals("value not equal", "createValue", e.getNewValue());
 +          Bug34387DUnitTest.this.invokeCount++;
 +        }
 +      };
 +    af.addCacheListener(cl1);
 +    Region r1 = createRootRegion("r1", af.create());
 +
 +    this.invokeCount = 0;
 +    assertNull(r1.getEntry("createKey"));
 +    doCommitOtherVm(false);
 +    assertNotNull(r1.getEntry("createKey"));
 +    assertEquals("createValue", r1.getEntry("createKey").getValue());
 +    assertEquals(1, this.invokeCount);
 +    assertFalse("Errors in callbacks; check logs for details", callbackFailure);
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/CacheMapTxnDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/CacheMapTxnDUnitTest.java
index b889bc7,0000000..f7e1739
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/CacheMapTxnDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/CacheMapTxnDUnitTest.java
@@@ -1,534 -1,0 +1,534 @@@
 +/*
 + * 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.
 + */
 +
 +/*
 + * CacheMapTxnDUnitTest.java
 + *
 + * Created on August 9, 2005, 11:18 AM
 + */
 +package com.gemstone.gemfire.cache30;
 +
 +import java.util.Properties;
 +import java.util.Set;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheFactory;
 +import com.gemstone.gemfire.cache.CacheTransactionManager;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionAttributes;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.UnsupportedOperationInTransactionException;
 +import com.gemstone.gemfire.distributed.DistributedSystem;
 +import com.gemstone.gemfire.test.dunit.Assert;
 +import com.gemstone.gemfire.test.dunit.AsyncInvocation;
 +import com.gemstone.gemfire.test.dunit.DistributedTestCase;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.ThreadUtils;
 +import com.gemstone.gemfire.test.dunit.VM;
 +
 +public class CacheMapTxnDUnitTest extends DistributedTestCase{
 +    
 +    protected static Cache cache;
 +    protected static Properties props = new Properties();
 +    static DistributedSystem ds = null;
 +    static Region region;
 +    static Region mirroredRegion;
 +    protected static CacheTransactionManager cacheTxnMgr;
 +    
 +    /** Creates a new instance of CacheMapTxnDUnitTest */
 +    public CacheMapTxnDUnitTest(String name) {
 +        super(name);
 +    }
 +    
 +    @Override
 +    public void setUp() throws Exception {
 +      super.setUp();
 +      Host host = Host.getHost(0);
 +      VM vm0 = host.getVM(0);
 +      VM vm1 = host.getVM(1);
-       vm0.invoke(CacheMapTxnDUnitTest.class, "createCache");
-       vm1.invoke(CacheMapTxnDUnitTest.class, "createCache");
++      vm0.invoke(() -> CacheMapTxnDUnitTest.createCache());
++      vm1.invoke(() -> CacheMapTxnDUnitTest.createCache());
 +    }
 +    
 +    @Override
 +    protected final void preTearDown() throws Exception {
 +      Host host = Host.getHost(0);
 +      VM vm0 = host.getVM(0);
 +      VM vm1 = host.getVM(1);
-       vm0.invoke(CacheMapTxnDUnitTest.class, "closeCache");
-       vm1.invoke(CacheMapTxnDUnitTest.class, "closeCache");
++      vm0.invoke(() -> CacheMapTxnDUnitTest.closeCache());
++      vm1.invoke(() -> CacheMapTxnDUnitTest.closeCache());
 +    }
 +    
 +    public static void createCache(){
 +        try{
 +            //            props.setProperty("mcast-port", "1234");
 +            //            ds = DistributedSystem.connect(props);
 +            ds = (new CacheMapTxnDUnitTest("temp")).getSystem(props);
 +            cache = CacheFactory.create(ds);
 +            AttributesFactory factory  = new AttributesFactory();
 +            factory.setScope(Scope.DISTRIBUTED_ACK);
 +            factory.setDataPolicy(DataPolicy.REPLICATE);
 +            RegionAttributes attr = factory.create();
 +            region = cache.createRegion("map", attr);
 +            
 +        } catch (Exception ex){
 +            ex.printStackTrace();
 +        }
 +    }
 +    
 +    public static void closeCache(){
 +        try{            
 +            cache.close();
 +            ds.disconnect();            
 +        } catch (Exception ex){
 +            ex.printStackTrace();
 +        }
 +    }
 +    
 +    public void testCommitTxn() {
 +        //this is to test single VM region transactions
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
-         vm0.invoke(CacheMapTxnDUnitTest.class, "commitTxn");
++        vm0.invoke(() -> CacheMapTxnDUnitTest.commitTxn());
 +    }//end of testCommitTxn
 +    
 +    public void testRollbackTxn() {
 +        //this is to test single VM region transactions
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
-         vm0.invoke(CacheMapTxnDUnitTest.class, "rollbackTxn");
++        vm0.invoke(() -> CacheMapTxnDUnitTest.rollbackTxn());
 +    }//end of testRollbackTxn
 +    
 +    public void testRollbackTxnClear() {
 +        //this is to test single VM region transactions
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +        VM vm1 = host.getVM(1);
 +        int i=0;
 +        Object ob2;
 +        Object [] objArr = new Object[1];
 +        
 +        for (i=0;i<5; i++){
 +            objArr [0] = ""+i;
 +            vm1.invoke(CacheMapTxnDUnitTest.class, "putMethod", objArr);
 +        }
 +        
-         vm0.invoke(CacheMapTxnDUnitTest.class, "rollbackTxnClear");
++        vm0.invoke(() -> CacheMapTxnDUnitTest.rollbackTxnClear());
 +        
 +        i=3;
 +        objArr [0] = ""+i;
 +        ob2 = vm1.invoke(CacheMapTxnDUnitTest.class, "getMethod", objArr);
 +        
 +        if(ob2 != null){
 +            fail("failed in testRollbackTxnClear");
 +        }
 +    }//end of testRollbackTxnClear
 +    
 +    public void testMiscMethods() throws Throwable{
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +        VM vm1 = host.getVM(1);
-         vm0.invoke(CacheMapTxnDUnitTest.class, "miscMethodsOwner");
-         AsyncInvocation o2 = vm0.invokeAsync(CacheMapTxnDUnitTest.class, "miscMethodsNotOwner");//invoke in same vm but in seperate thread
-         AsyncInvocation o3 = vm1.invokeAsync(CacheMapTxnDUnitTest.class, "miscMethodsNotOwner");//invoke in another vm
++        vm0.invoke(() -> CacheMapTxnDUnitTest.miscMethodsOwner());
++        AsyncInvocation o2 = vm0.invokeAsync(() -> CacheMapTxnDUnitTest.miscMethodsNotOwner());//invoke in same vm but in seperate thread
++        AsyncInvocation o3 = vm1.invokeAsync(() -> CacheMapTxnDUnitTest.miscMethodsNotOwner());//invoke in another vm
 +        ThreadUtils.join(o2, 30 * 1000);
 +        ThreadUtils.join(o3, 30 * 1000);
 +        
 +        if(o2.exceptionOccurred()){
 +          Assert.fail("o2 failed", o2.getException());
 +        }
 +        
 +        if(o3.exceptionOccurred()){
 +          Assert.fail("o3 failed", o3.getException());
 +        }
 +        
 +    }//end of testMiscMethods
 +    
 +    
 +    //methods to be executed in remote vms...and called through vm.invoke
 +    
 +    public static void commitTxn(){
 +        try{
 +            cacheTxnMgr = cache.getCacheTransactionManager();
 +            int [] i = {0,1,2,3,4,5,6,7,8,9};
 +            Object o3;
 +            o3="init";
 +            region.put(""+i[0], "zero");
 +            region.create(""+i[5], "fifth");
 +            //begin transaction
 +            cacheTxnMgr.begin();
 +            region.put(""+i[1], "first");
 +            //test get
 +            o3 = region.get(""+i[1]);
 +            if( !(o3.toString().equals("first"))){
 +                fail("get inside transaction returns incorrect object");
 +            }
 +            
 +            //test remove
 +            region.put(""+i[2], "second");
 +            o3 = region.remove(""+i[2]);
 +            if( !(o3.toString().equals("second"))){
 +                fail("remove inside transaction returns incorrect object");
 +            }
 +            
 +            boolean flag = region.containsKey(""+i[2]);
 +            if (flag){
 +                fail("region.containsKey after region.remove inside txn returns incorrect value");
 +            }
 +            
 +            region.put(""+i[3], "third");
 +            region.put(""+i[0], "updatedZero");
 +
 +            // test putIfAbsent
 +            region.putIfAbsent(""+i[4], "fourth");
 +            if (!region.get(""+i[4]).toString().equals("fourth")) {
 +              fail("putIfAbsent inside transaction returns incorrect object");
 +            }
 +            
 +            // test replace
 +            region.replace(""+i[4], "fourth2");
 +            if (!region.get(""+i[4]).toString().equals("fourth2")) {
 +              fail("replace inside transaction returns incorrect object)");
 +            }
 +
 +            // test replace
 +            region.replace(""+i[4], "fourth2", "fourth3");
 +            if (!region.get(""+i[4]).toString().equals("fourth3")) {
 +              fail("replace inside transaction returns incorrect object)");
 +            }
 +            
 +            // test a failed removal
 +            boolean succeeded = region.remove(""+i[5], new Object());
 +            assertTrue(!succeeded);
 +            assertTrue(region.get(""+i[5]).equals("fifth"));
 +            
 +            // test remove
 +            region.remove(""+i[5]);
 +            
 +            //commit the transaction
 +            cacheTxnMgr.commit();
 +            
 +            //verify for persistent data now
 +            o3 = region.get(""+i[1]);
 +            if( !(o3.toString().equals("first"))){
 +                fail("get after committed transaction returns incorrect object");
 +            }
 +            
 +            o3 = region.get(""+i[2]);
 +            if(o3 != null){
 +                fail("get after committed transaction returns incorrect object");
 +            }
 +            
 +            o3 = region.get(""+i[3]);
 +            if( !(o3.toString().equals("third"))){
 +                fail("get after committed transaction returns incorrect object");
 +            }
 +            
 +            if (!region.get(""+i[4]).toString().equals("fourth3")) {
 +              fail("get after committed transaction returns incorrect object");
 +            }
 +            
 +            if (region.containsKey(""+i[5])) {
 +              fail("containsKey after committed transaction was true");
 +            }
 +            
 +            boolean val = region.containsValue("updatedZero");
 +            if(! val){
 +                fail("containsValue after committed transaction returns incorrect result");
 +            }
 +            
 +        }
 +        catch(Exception ex){
 +            ex.printStackTrace();
 +            fail("failed in commitTxn");
 +        }
 +        finally{
 +            if(cacheTxnMgr.exists()){
 +                try {cacheTxnMgr.commit();} catch (Exception cce){cce.printStackTrace();}
 +            }
 +        }
 +        
 +    }//end of commitTxn
 +    
 +    
 +    public static void rollbackTxn(){
 +        try{
 +            cacheTxnMgr = cache.getCacheTransactionManager();
 +            int [] i = {0,1,2,3,4,5,6,7,8,9};
 +            Object o3;
 +            o3="init";
 +            region.put(""+i[0], "zero");
 +            region.put(""+i[5], "fifth");
 +            cacheTxnMgr.begin();
 +            region.put(""+i[1], "first");
 +            //test get
 +            o3 = region.get(""+i[1]);
 +            if( !(o3.toString().equals("first"))){
 +                fail("get inside transaction returns incorrect object");
 +            }
 +            
 +            //test containsValue
 +            boolean flag = region.containsValue(new String("first"));
 +            //assertEquals(true, flag);
 +            
 +            //test remove
 +            region.put(""+i[2], "second");
 +            o3 = region.remove(""+i[2]);
 +            if( !(o3.toString().equals("second"))){
 +                fail("remove inside transaction returns incorrect object");
 +            }
 +            
 +            region.put(""+i[3], "third");
 +            region.put(""+i[0], "updatedZero");
 +
 +            
 +            // test putIfAbsent
 +            region.putIfAbsent(""+i[4], "fourth");
 +            if (!region.get(""+i[4]).toString().equals("fourth")) {
 +              fail("putIfAbsent inside transaction returns incorrect object");
 +            }
 +            
 +            // test replace
 +            region.replace(""+i[4], "fourth2");
 +            if (!region.get(""+i[4]).toString().equals("fourth2")) {
 +              fail("replace inside transaction returns incorrect object)");
 +            }
 +
 +            // test replace
 +            region.replace(""+i[4], "fourth2", "fourth3");
 +            if (!region.get(""+i[4]).toString().equals("fourth3")) {
 +              fail("replace inside transaction returns incorrect object)");
 +            }
 +            
 +            // test a failed removal
 +            boolean succeeded = region.remove(""+i[5], new Object());
 +            assertTrue(!succeeded);
 +            assertTrue(region.get(""+i[5]).equals("fifth"));
 +
 +            // test remove
 +            region.remove(""+i[5]);
 +            
 +            //rollback the transaction
 +            cacheTxnMgr.rollback();
 +            
 +            //verify for persistent data now
 +            o3 = region.get(""+i[1]);
 +            if( o3 != null){ //null?
 +                fail("get after rolled back transaction returns incorrect object");
 +            }
 +            
 +            o3 = region.get(""+i[2]);
 +            if( o3 != null){ //null?
 +                fail("get after rolled back transaction returns incorrect object");
 +            }
 +            
 +            o3 = region.get(""+i[3]);
 +            if( o3 != null){ //null?
 +                fail("get after rolled back transaction returns incorrect object");
 +            }
 +            
 +            o3 = region.get(""+i[4]);
 +            if( o3 != null){ //null?
 +                fail("get after rolled back transaction returns incorrect object");
 +            }
 +            
 +            o3 = region.get(""+i[5]);
 +            if (o3 == null) {
 +              fail("get after rolled back transaction returns incorrect object");
 +            }
 +            
 +            //            boolean val = region.containsValue("zero");
 +            //            if( !val){
 +            //                fail("containsValue after rolled back transaction returns incorrect result");
 +            //            }
 +            
 +        }
 +        catch(Exception ex){
 +            ex.printStackTrace();
 +            fail("failed in rollbackTxn");
 +        }
 +        finally{
 +            if(cacheTxnMgr.exists()){
 +                try {cacheTxnMgr.commit();} catch (Exception cce){cce.printStackTrace();}
 +            }
 +        }
 +        
 +    }//end of rollbackTxn
 +    
 +    public static void rollbackTxnClear(){
 +        try{
 +            cacheTxnMgr = cache.getCacheTransactionManager();
 +            int [] i = {0,1,2,3,4,5,6,7,8,9};
 +            region.put(""+i[0], "zero");
 +            //begin transaction
 +            cacheTxnMgr.begin();
 +            region.put(""+i[1], "first");
 +            region.put(""+i[0], "updatedZero");
 +            
 +            try {
 +              region.clear(); //clear is not transactional operation
 +              fail("excpected exception not thrown");
 +            } catch (UnsupportedOperationInTransactionException e) {
 +            }
 +            
 +            //rollback the transaction
 +            cacheTxnMgr.rollback();
 +            
 +            //verify for persistent data now
 +            
 +            boolean val = region.containsValue("updatedZero");
 +            if(val){
 +                fail("containsValue after region.clear & rolled back transaction returns incorrect result");
 +            }
 +            region.clear();
 +            val = region.containsValue("first");
 +            if(val){
 +                fail("containsValue after region.clear & rolled back transaction returns incorrect result");
 +            }
 +            
 +        }
 +        catch(Exception ex){
 +            cacheTxnMgr = null;
 +            ex.printStackTrace();
 +            fail("failed in rollbackTxnClear");
 +        }
 +        
 +    }//end of rollbackTxnClear
 +    
 +    public static void miscMethodsOwner(){
 +        try{
 +            cacheTxnMgr = cache.getCacheTransactionManager();
 +            int [] i = {0,1,2,3,4,5,6,7,8,9};
 +            region.clear();
 +            region.put(""+i[0], "zero");
 +            region.put(""+i[1], "first");
 +            region.put(""+i[2], "second");
 +            region.put(""+i[3], "third");
 +            
 +            cacheTxnMgr.begin();
 +            region.put(""+i[4], "forth");
 +            region.put(""+i[5], "fifth");
 +            
 +            //test size method
 +            int j = region.size();
 +            if(j != 6){
 +                fail("region.size inside transaction returns incorrect results");
 +            }
 +            
 +            //test entrySet method
 +            Set set = region.entrySet();
 +            int k = set.size();
 +            if(j != 6){
 +                fail("region.entrySet inside transaction returns incorrect results");
 +            }
 +            
 +            //test keySet method
 +            set = region.keySet();
 +            k = set.size();
 +            if(j != 6){
 +                fail("region.keySet inside transaction returns incorrect results");
 +            }
 +            
 +            boolean val = region.containsValue("forth");
 +            if( !val){
 +                fail("containsValue inside transaction returns incorrect result");
 +            }
 +            
 +            //commit the transaction
 +            cacheTxnMgr.rollback();
 +            
 +            //verify for persistent data now
 +            
 +        }
 +        catch(Exception ex){
 +            ex.printStackTrace();
 +            fail("failed in miscMethodsOwner");
 +        }
 +        finally{
 +            if(cacheTxnMgr.exists()){
 +                try {cacheTxnMgr.commit();} catch (Exception cce){cce.printStackTrace();}
 +            }
 +        }
 +        
 +    }//end of miscMethodsOwner
 +    
 +    public static void miscMethodsNotOwner(){
 +        try{
 +//            int [] i = {0,1,2,3,4,5,6,7,8,9};
 +            //it is assumed that there are already four committed entried inside region
 +            //test size method
 +            int j = region.size();
 +            if(j != 4){
 +                fail("region.size for not owner of transaction returns incorrect results, size is "+j+ " but expected 4");
 +            }
 +            
 +            //test entrySet method
 +            Set set = region.entrySet();
 +            int k = set.size();
 +            if(j != 4){
 +                fail("region.entrySet for not owner of transaction returns incorrect results");
 +            }
 +            
 +            //test keySet method
 +            set = region.keySet();
 +            k = set.size();
 +            if(j != 4){
 +                fail("region.keySet for not owner of transaction returns incorrect results");
 +            }
 +            boolean val = region.containsKey("forth");
 +            if(val){
 +                fail("containsValue for not owner of transaction returns incorrect result");
 +            }
 +            
 +        }
 +        catch(Exception ex){
 +            ex.printStackTrace();
 +            fail("failed in miscMethodsNotOwner");
 +        }
 +    }//end of miscMethodsNotOwner
 +    
 +    
 +    
 +    //helper methods
 +    public static Object putMethod(Object ob){
 +        Object obj=null;
 +        try{
 +            if(ob != null){
 +                String str = "first";
 +                obj = region.put(ob, str);
 +            }
 +        }catch(Exception ex){
 +            ex.printStackTrace();
 +            fail("Failed while region.put");
 +        }
 +        return obj;
 +    }
 +    
 +    public static Object getMethod(Object ob){
 +        Object obj=null;
 +        try{
 +            obj = region.get(ob);
 +        } catch(Exception ex){
 +            fail("Failed while region.get");
 +        }
 +        return obj;
 +    }
 +    
 +    
 +}//end of test class

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/CachedAllEventsDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/CachedAllEventsDUnitTest.java
index c0a1fc4,0000000..c3f97f7
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/CachedAllEventsDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/CachedAllEventsDUnitTest.java
@@@ -1,111 -1,0 +1,111 @@@
 +/*
 + * 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.cache30;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.InterestPolicy;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.SubscriptionAttributes;
 +import com.gemstone.gemfire.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.VM;
 +
 +/**
 + * Make sure that create are distributed and done in
 + * remote regions that are CACHED_ALL_EVENTS*.
 + *
 + * @author darrel
 + * @since 5.0
 + */
 +public class CachedAllEventsDUnitTest extends CacheTestCase {
 +
 +//  private transient Region r;
 +//  private transient DistributedMember otherId;
 +//  private transient int invokeCount;
 +  
 +  public CachedAllEventsDUnitTest(String name) {
 +    super(name);
 +  }
 +
 +  private VM getOtherVm() {
 +    Host host = Host.getHost(0);
 +    return host.getVM(0);
 +  }
 +    
 +  private void initOtherId() {
 +    VM vm = getOtherVm();
 +    vm.invoke(new CacheSerializableRunnable("Connect") {
 +        public void run2() throws CacheException {
 +          getCache();
 +        }
 +      });
-     vm.invoke(CachedAllEventsDUnitTest.class, "getVMDistributedMember");
++    vm.invoke(() -> CachedAllEventsDUnitTest.getVMDistributedMember());
 +  }
 +  private void doCreateOtherVm() {
 +    VM vm = getOtherVm();
 +    vm.invoke(new CacheSerializableRunnable("create root") {
 +        public void run2() throws CacheException {
 +          AttributesFactory af = new AttributesFactory();
 +          af.setScope(Scope.DISTRIBUTED_ACK);
 +          Region r1 = createRootRegion("r1", af.create());
 +          r1.create("key", "value");
 +        }
 +      });
 +  }
 +
 +  public static DistributedMember getVMDistributedMember() {
 +    return InternalDistributedSystem.getAnyInstance().getDistributedMember();
 +  }
 +  
 +  //////////////////////  Test Methods  //////////////////////
 +
 +  /**
 +   * make sure a remote create will be done in a NORMAL+ALL region
 +   * @param rmtCreate is true if create should happen in remote region
 +   */
 +  private void remoteCreate(DataPolicy dp, InterestPolicy ip, boolean rmtCreate) throws CacheException {
 +    initOtherId();
 +    AttributesFactory af = new AttributesFactory();
 +    af.setDataPolicy(dp);
 +    af.setSubscriptionAttributes(new SubscriptionAttributes(ip));
 +    af.setScope(Scope.DISTRIBUTED_ACK);
 +    Region r1 = createRootRegion("r1", af.create());
 +
 +    assertEquals(false, r1.containsKey("key"));
 +    doCreateOtherVm();
 +    if (rmtCreate) {
 +      assertEquals(true, r1.containsKey("key"));
 +      assertEquals("value", r1.getEntry("key").getValue());
 +    } else {
 +      assertEquals(false, r1.containsKey("key"));
 +    }
 +  }
 +  // TODO these are never used
 +  public void testRemoteCreate_CAE() throws CacheException {
 +    remoteCreate(DataPolicy.NORMAL, InterestPolicy.ALL, true);
 +  }
 +  public void testRemoteCreate_CAER() throws CacheException {
 +    remoteCreate(DataPolicy.REPLICATE, InterestPolicy.CACHE_CONTENT, true);
 +  }
 +  public void testRemoteCreate_C() throws CacheException {
 +    remoteCreate(DataPolicy.NORMAL, InterestPolicy.CACHE_CONTENT, false);
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/CallbackArgDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/CallbackArgDUnitTest.java
index 49e38c0,0000000..f0bae93
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/CallbackArgDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/CallbackArgDUnitTest.java
@@@ -1,185 -1,0 +1,185 @@@
 +/*
 + * 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.cache30;
 +
 +import java.util.ArrayList;
 +import java.util.Arrays;
 +import java.util.Iterator;
 +import java.util.List;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheListener;
 +import com.gemstone.gemfire.cache.CacheTransactionManager;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.EntryEvent;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.TransactionEvent;
 +import com.gemstone.gemfire.cache.TransactionListener;
 +import com.gemstone.gemfire.cache.util.CacheListenerAdapter;
 +import com.gemstone.gemfire.cache.util.TransactionListenerAdapter;
 +import com.gemstone.gemfire.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.SerializableCallable;
 +import com.gemstone.gemfire.test.dunit.VM;
 +
 +/**
 + * Test the getCallbackArgument in light of bug 34075.
 + *
 + * @author darrel
 + * @since 5.0
 + */
 +public class CallbackArgDUnitTest extends CacheTestCase {
 +
 +//  private transient Region r;
 +//  private transient DistributedMember otherId;
 +  protected transient int invokeCount;
 +  protected static String callbackArg;
 +  
 +  public CallbackArgDUnitTest(String name) {
 +    super(name);
 +  }
 +
 +  private VM getOtherVm() {
 +    Host host = Host.getHost(0);
 +    return host.getVM(0);
 +  }
 +    
 +  private void initOtherId() {
 +    VM vm = getOtherVm();
 +    vm.invoke(new CacheSerializableRunnable("Connect") {
 +        public void run2() throws CacheException {
 +          getCache();
 +        }
 +      });
-     vm.invoke(CallbackArgDUnitTest.class, "getVMDistributedMember");
++    vm.invoke(() -> CallbackArgDUnitTest.getVMDistributedMember());
 +  }
 +  private void doCommitOtherVm() {
 +    VM vm = getOtherVm();
 +    vm.invoke(new CacheSerializableRunnable("create root") {
 +        public void run2() throws CacheException {
 +          AttributesFactory af = new AttributesFactory();
 +          CacheListener cl1 = new CacheListenerAdapter() {
 +              public void afterCreate(EntryEvent e) {
 +                assertEquals(callbackArg, e.getCallbackArgument());
 +              }
 +            };
 +          af.addCacheListener(cl1);
 +          af.setScope(Scope.DISTRIBUTED_ACK);
 +          Region r1 = createRootRegion("r1", af.create());
 +          Region r2 = r1.createSubregion("r2", af.create());
 +          Region r3 = r2.createSubregion("r3", af.create());
 +          CacheTransactionManager ctm =  getCache().getCacheTransactionManager();
 +          TransactionListener tl1 = new TransactionListenerAdapter() {
 +              public void afterCommit(TransactionEvent e) {
 +                assertEquals(6, e.getEvents().size());
 +                Iterator it = e.getEvents().iterator();
 +                while (it.hasNext()) {
 +                  EntryEvent ee = (EntryEvent)it.next();
 +                  assertEquals(callbackArg, ee.getCallbackArgument());
 +                  assertEquals(true, ee.isCallbackArgumentAvailable());
 +                }
 +              }
 +            };
 +          ctm.addListener(tl1);
 +          ctm.begin();
 +          r2.put("b", "value1", callbackArg);
 +          r3.put("c", "value2", callbackArg);
 +          r1.put("a", "value3", callbackArg);
 +          r1.put("a2", "value4", callbackArg);
 +          r3.put("c2", "value5", callbackArg);
 +          r2.put("b2", "value6", callbackArg);
 +          ctm.commit();
 +        }
 +      });
 +  }
 +
 +  public static DistributedMember getVMDistributedMember() {
 +    return InternalDistributedSystem.getAnyInstance().getDistributedMember();
 +  }
 +  
 +  //////////////////////  Test Methods  //////////////////////
 +
 +  List expectedKeys;
 +  int clCount = 0;
 +
 +  Object getCurrentExpectedKey() {
 +    Object result = this.expectedKeys.get(this.clCount);
 +    this.clCount += 1;
 +    return result;
 +  }
 +  /**
 +   * make sure callback arg is NOT_AVAILABLE in all the places it should be
 +   */
 +  public void testForNA_CA() throws CacheException {
 +    doTest();
 +  }
 +  public void testForCA() throws Exception {
 +    callbackArg = "cbArg";
 +    getOtherVm().invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        callbackArg = "cbArg";
 +        return null;
 +      }
 +    });
 +    doTest();
 +  }
 +  private void doTest() throws CacheException {
 +    initOtherId();
 +    AttributesFactory af = new AttributesFactory();
 +    af.setDataPolicy(DataPolicy.REPLICATE);
 +    af.setScope(Scope.DISTRIBUTED_ACK);
 +    CacheListener cl1 = new CacheListenerAdapter() {
 +        public void afterCreate(EntryEvent e) {
 +          assertEquals(getCurrentExpectedKey(), e.getKey());
 +          assertEquals(callbackArg, e.getCallbackArgument());
 +          assertEquals(true, e.isCallbackArgumentAvailable());
 +        }
 +      };
 +    af.addCacheListener(cl1);
 +    Region r1 = createRootRegion("r1", af.create());
 +    Region r2 = r1.createSubregion("r2", af.create());
 +    r2.createSubregion("r3", af.create());
 +
 +    TransactionListener tl1 = new TransactionListenerAdapter() {
 +        public void afterCommit(TransactionEvent e) {
 +          assertEquals(6, e.getEvents().size());
 +          ArrayList keys = new ArrayList();
 +          Iterator it = e.getEvents().iterator();
 +          while (it.hasNext()) {
 +            EntryEvent ee = (EntryEvent)it.next();
 +            keys.add(ee.getKey());
 +            assertEquals(callbackArg, ee.getCallbackArgument());
 +            assertEquals(true, ee.isCallbackArgumentAvailable());
 +          }
 +          assertEquals(CallbackArgDUnitTest.this.expectedKeys, keys);
 +          CallbackArgDUnitTest.this.invokeCount = 1;
 +        }
 +      };
 +    CacheTransactionManager ctm =  getCache().getCacheTransactionManager();
 +    ctm.addListener(tl1);
 +
 +    this.invokeCount = 0;
 +    this.clCount = 0;
 +    this.expectedKeys = Arrays.asList(new String[]{"b", "c", "a", "a2", "c2", "b2"});
 +    doCommitOtherVm();
 +    assertEquals(1, this.invokeCount);
 +    assertEquals(6, this.clCount);
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/ClearMultiVmCallBkDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/ClearMultiVmCallBkDUnitTest.java
index 1d5b621,0000000..e748a42
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/ClearMultiVmCallBkDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/ClearMultiVmCallBkDUnitTest.java
@@@ -1,249 -1,0 +1,249 @@@
 +/*
 + * 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.
 + */
 +
 +/*
 + * ClearMultiVmCallBkDUnitTest.java
 + *
 + * Created on August 11, 2005, 7:37 PM
 + */
 +package com.gemstone.gemfire.cache30;
 +
 +import java.util.Properties;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheFactory;
 +import com.gemstone.gemfire.cache.CacheListener;
 +import com.gemstone.gemfire.cache.CacheTransactionManager;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionAttributes;
 +import com.gemstone.gemfire.cache.RegionEvent;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.util.CacheListenerAdapter;
 +import com.gemstone.gemfire.distributed.DistributedSystem;
 +import com.gemstone.gemfire.test.dunit.DistributedTestCase;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.LogWriterUtils;
 +import com.gemstone.gemfire.test.dunit.VM;
 +
 +/**
 + *
 + * @author  prafulla/vjadhav
 + */
 +public class ClearMultiVmCallBkDUnitTest extends DistributedTestCase{
 +    
 +    /** Creates a new instance of ClearMultiVmCallBkDUnitTest */
 +    public ClearMultiVmCallBkDUnitTest(String name) {
 +        super(name);
 +    }
 +    
 +    static Cache cache;
 +    static Properties props = new Properties();
 +    static Properties propsWork = new Properties();
 +    static DistributedSystem ds = null;
 +    static Region region;
 +    static Region paperWork;
 +    static CacheTransactionManager cacheTxnMgr;
 +    static boolean afterClear=false;
 +    
 +    
 +    @Override
 +    public void setUp() throws Exception {
 +      super.setUp();
 +      Host host = Host.getHost(0);
 +      VM vm0 = host.getVM(0);
 +      VM vm1 = host.getVM(1);
-       vm0.invoke(ClearMultiVmCallBkDUnitTest.class, "createCache");
-       vm1.invoke(ClearMultiVmCallBkDUnitTest.class, "createCache");
++      vm0.invoke(() -> ClearMultiVmCallBkDUnitTest.createCache());
++      vm1.invoke(() -> ClearMultiVmCallBkDUnitTest.createCache());
 +      LogWriterUtils.getLogWriter().fine("Cache created in successfully");
 +    }
 +    
 +    @Override
 +    protected final void preTearDown() throws Exception {
 +      Host host = Host.getHost(0);
 +      VM vm0 = host.getVM(0);
 +      VM vm1 = host.getVM(1);
-       vm0.invoke(ClearMultiVmCallBkDUnitTest.class, "closeCache");
-       vm1.invoke(ClearMultiVmCallBkDUnitTest.class, "closeCache");
++      vm0.invoke(() -> ClearMultiVmCallBkDUnitTest.closeCache());
++      vm1.invoke(() -> ClearMultiVmCallBkDUnitTest.closeCache());
 +    }
 +    
 +    public static void createCache(){
 +        try{
 +            CacheListener aListener = new ListenerCallBk();
 +//            props.setProperty("mcast-port", "1234");
 +//            ds = DistributedSystem.connect(props);
 +            ds = (new ClearMultiVmCallBkDUnitTest("temp")).getSystem(props);            
 +            
 +            cache = CacheFactory.create(ds);
 +            AttributesFactory factory  = new AttributesFactory();
 +            factory.setScope(Scope.DISTRIBUTED_ACK);
 +            
 +            // Set Cachelisterner : aListener
 +            
 +            factory.setCacheListener(aListener);
 +            RegionAttributes attr = factory.create();
 +            
 +            region = cache.createRegion("map", attr);
 +            
 +            
 +        } catch (Exception ex){
 +            ex.printStackTrace();
 +        }
 +    }
 +    
 +    public static void closeCache(){
 +        try{
 +            
 +            cache.close();
 +            ds.disconnect();
 +            
 +        } catch (Exception ex){
 +            ex.printStackTrace();
 +        }
 +    }
 +    
 +    //test methods
 +    
 +    public void testClearSingleVM(){
 +        
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +        //VM vm1 = host.getVM(1);
 +        
 +//        Object obj0;
 +//        Object obj1;
 +        Object[] objArr = new Object[1];
 +        for (int i=1; i<4; i++){
 +            objArr[0] = ""+i;
 +            vm0.invoke(ClearMultiVmCallBkDUnitTest.class, "putMethod", objArr);
 +            
 +        }
 +        LogWriterUtils.getLogWriter().fine("Did all puts successfully");
 +        
-         vm0.invoke(ClearMultiVmCallBkDUnitTest.class,"clearMethod");
++        vm0.invoke(() -> ClearMultiVmCallBkDUnitTest.clearMethod());
 +        LogWriterUtils.getLogWriter().fine("Did clear successfully");
 +        
 +        while(afterClear){
 +        }       
 +        
-         int Regsize = vm0.invokeInt(ClearMultiVmCallBkDUnitTest.class, "sizeMethod");
++        int Regsize = vm0.invoke(() -> ClearMultiVmCallBkDUnitTest.sizeMethod());
 +        assertEquals(1, Regsize);
 +        
 +        
 +    }//end of test case1
 +    
 +     public void testClearMultiVM(){
 +        
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +        VM vm1 = host.getVM(1);
 +        
 +        Object[] objArr = new Object[1];
 +        for (int i=1; i<4; i++){
 +            objArr[0] = ""+i;
 +            vm0.invoke(ClearMultiVmCallBkDUnitTest.class, "putMethod", objArr);
 +            vm1.invoke(ClearMultiVmCallBkDUnitTest.class, "getMethod", objArr);
 +        }
 +        LogWriterUtils.getLogWriter().fine("Did all puts successfully");
-         //vm0.invoke(ClearMultiVmCallBkDUnitTest.class,"putMethod");
-         vm1.invoke(ClearMultiVmCallBkDUnitTest.class,"clearMethod");
++        //vm0.invoke(() -> ClearMultiVmCallBkDUnitTest.putMethod());
++        vm1.invoke(() -> ClearMultiVmCallBkDUnitTest.clearMethod());
 +        LogWriterUtils.getLogWriter().fine("Did clear successfully");
 +        
 +        while(afterClear){
 +        }       
 +        
-         int Regsize = vm0.invokeInt(ClearMultiVmCallBkDUnitTest.class, "sizeMethod");
++        int Regsize = vm0.invoke(() -> ClearMultiVmCallBkDUnitTest.sizeMethod());
 +        assertEquals(1, Regsize);
 +        
 +        
 +    }//end of test case2
 +    
 +    public static Object putMethod(Object ob){
 +        Object obj=null;
 +        try{
 +            if(ob != null){
 +                String str = "first";
 +                obj = region.put(ob, str);
 +            }
 +        }catch(Exception ex){
 +            ex.printStackTrace();
 +            fail("Failed while region.put");
 +        }
 +        return obj;
 +    }
 +    
 +    public static Object getMethod(Object ob){
 +        Object obj=null;
 +        try{
 +            obj = region.get(ob);
 +        } catch(Exception ex){
 +            fail("Failed while region.get");
 +        }
 +        return obj;
 +    }
 +    
 +    public static boolean containsValueMethod(Object ob){
 +        boolean flag = false;
 +        try{
 +            flag = region.containsValue(ob);
 +        }catch(Exception ex){
 +            fail("Failed while region.containsValueMethod");
 +        }
 +        return flag;
 +    }
 +    
 +    public static int sizeMethod(){
 +        int i=0;
 +        try{
 +            i = region.size();
 +        }catch(Exception ex){
 +            fail("Failed while region.size");
 +        }
 +        return i;
 +    }
 +    
 +    public static void clearMethod(){
 +        try{
 +            region.clear();
 +        } catch(Exception ex){
 +            ex.printStackTrace();
 +        }
 +    }
 +    
 +    public static boolean getBoolean(){
 +        return afterClear;
 +    }
 +    
 +    static class ListenerCallBk extends CacheListenerAdapter {
 +  
 +        public void afterRegionClear(RegionEvent event){
 +            LogWriterUtils.getLogWriter().fine("In afterClear:: CacheListener Callback");
 +            try {
 +                int i = 7;
 +                region.put(""+i, "inAfterClear");
 +                afterClear = true;
 +            }catch (Exception e){
 +                //
 +            }
 +            
 +        }
 +        
 +    }
 +    
 +    
 +    
 +}//end of class

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/ClearMultiVmDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/ClearMultiVmDUnitTest.java
index 8553fe6,0000000..5f9450b
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/ClearMultiVmDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/ClearMultiVmDUnitTest.java
@@@ -1,469 -1,0 +1,467 @@@
 +/*
 + * 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.
 + */
 +
 +/*
 + * ClearMultiVmDUnitTest.java
 + *
 + * Created on August 11, 2005, 7:37 PM
 + */
 +package com.gemstone.gemfire.cache30;
 +
 +import java.util.Properties;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheFactory;
 +import com.gemstone.gemfire.cache.CacheTransactionManager;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionAttributes;
 +import com.gemstone.gemfire.cache.RegionDestroyedException;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.UnsupportedOperationInTransactionException;
 +import com.gemstone.gemfire.distributed.DistributedSystem;
 +import com.gemstone.gemfire.test.dunit.Assert;
 +import com.gemstone.gemfire.test.dunit.AsyncInvocation;
 +import com.gemstone.gemfire.test.dunit.DistributedTestCase;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.Invoke;
 +import com.gemstone.gemfire.test.dunit.SerializableRunnable;
 +import com.gemstone.gemfire.test.dunit.ThreadUtils;
 +import com.gemstone.gemfire.test.dunit.VM;
 +
 +/**
 + *
 + * @author  prafulla
 + */
 +public class ClearMultiVmDUnitTest extends DistributedTestCase{
 +    
 +    /** Creates a new instance of ClearMultiVmDUnitTest */
 +    public ClearMultiVmDUnitTest(String name) {
 +        super(name);
 +    }
 +    
 +    static Cache cache;
 +    static Properties props = new Properties();
 +    static Properties propsWork = new Properties();
 +    static DistributedSystem ds = null;
 +    static Region region;
 +    static Region paperWork;
 +    static Region mirroredRegion;
 +    static CacheTransactionManager cacheTxnMgr;
 +    
 +    @Override
 +    public void setUp() throws Exception {
 +      super.setUp();
 +      Host host = Host.getHost(0);
 +      VM vm0 = host.getVM(0);
 +      VM vm1 = host.getVM(1);
-       vm0.invoke(ClearMultiVmDUnitTest.class, "createCache");
-       vm1.invoke(ClearMultiVmDUnitTest.class, "createCache");
++      vm0.invoke(() -> ClearMultiVmDUnitTest.createCache());
++      vm1.invoke(() -> ClearMultiVmDUnitTest.createCache());
 +    }
 +    
 +    @Override
 +    protected final void preTearDown() throws Exception {
 +      Host host = Host.getHost(0);
 +      VM vm0 = host.getVM(0);
 +      VM vm1 = host.getVM(1);
-       vm0.invoke(ClearMultiVmDUnitTest.class, "closeCache");
-       vm1.invoke(ClearMultiVmDUnitTest.class, "closeCache");
++      vm0.invoke(() -> ClearMultiVmDUnitTest.closeCache());
++      vm1.invoke(() -> ClearMultiVmDUnitTest.closeCache());
 +      cache = null;
 +      Invoke.invokeInEveryVM(new SerializableRunnable() { public void run() { cache = null; } });
 +    }
 +    
 +    public static void createCache(){
 +        try{            
 +            ds = (new ClearMultiVmDUnitTest("temp")).getSystem(props);
 +            cache = CacheFactory.create(ds);
 +            AttributesFactory factory  = new AttributesFactory();
 +            factory.setScope(Scope.DISTRIBUTED_ACK);
 +            factory.setConcurrencyChecksEnabled(true);
 +            RegionAttributes attr = factory.create();
 +            region = cache.createRegion("map", attr);
 +            
 +            AttributesFactory factory1  = new AttributesFactory();
 +            factory1.setScope(Scope.DISTRIBUTED_ACK);
 +            factory.setConcurrencyChecksEnabled(true);
 +            factory1.setDataPolicy(DataPolicy.REPLICATE);
 +            paperWork = cache.createRegion("paperWork", factory1.create());
 +            
 +        } catch (Exception ex){
 +            ex.printStackTrace();
 +        }
 +    }
 +    
 +    public static void closeCache(){
 +        try{
 +            cache.close();
 +            ds.disconnect();
 +        } catch (Exception ex){
 +            ex.printStackTrace();
 +        }
 +    }
 +    
 +    //test methods
 +    
 +    public void testClearSimpleScenarios(){
 +        
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +        VM vm1 = host.getVM(1);
 +        
 +        //verifying Single VM clear functionalities
 +        vm0.invoke(new CacheSerializableRunnable("temp1"){
 +            public void run2()throws CacheException{
 +                region.put(new Integer(1), new String("first"));
 +                region.put(new Integer(2), new String("second"));
 +                region.put(new Integer(3), new String("third"));
 +                region.clear();
 +                assertEquals(0, region.size());
 +            }
 +        }
 +        );
 +        
 +        vm1.invoke(new CacheSerializableRunnable("temp1vm1"){
 +            public void run2()throws CacheException{
 +                assertEquals(0, region.size());
 +            }
 +        }
 +        );
 +        
 +        //verifying Single VM and single transaction clear functionalities
 +        vm1.invoke(new CacheSerializableRunnable("temp2"){
 +            public void run2() throws CacheException{
 +                try{
 +                    region.put(new Integer(1), new String("first"));
 +                    region.put(new Integer(2), new String("second"));
 +                    region.put(new Integer(3), new String("third"));
 +                    cacheTxnMgr = cache.getCacheTransactionManager();
 +                    cacheTxnMgr.begin();
 +                    region.put(new Integer(4), new String("forth"));
 +                    try {
 +                      region.clear();
 +                      fail("expected exception not thrown");
 +                    } catch (UnsupportedOperationInTransactionException e) {
 +                      // expected
 +                    }
 +                    region.put(new Integer(5), new String("fifth"));
 +                    cacheTxnMgr.commit();
 +                    assertEquals(5, region.size());
 +                    assertEquals("fifth", region.get(new Integer(5)).toString());
 +                }
 +                catch(CacheException ce){
 +                    ce.printStackTrace();
 +                }
 +                finally{
 +                    if(cacheTxnMgr.exists()){
 +                        try {cacheTxnMgr.commit();} catch (Exception cce){cce.printStackTrace();}
 +                    }
 +                }
 +                
 +            }
 +        }
 +        );
 +        
 +        //verifying that region.clear does not clear the entries from sub region
 +        vm0.invoke(new CacheSerializableRunnable("temp3"){
 +            public void run2()throws CacheException{
 +                region.put(new Integer(1), new String("first"));
 +                region.put(new Integer(2), new String("second"));
 +                AttributesFactory factory  = new AttributesFactory();
 +                factory.setScope(Scope.DISTRIBUTED_ACK);
 +                RegionAttributes attr = factory.create();
 +                Region subRegion = region.createSubregion("subr", attr);
 +                subRegion.put(new Integer(3), new String("third"));
 +                subRegion.put(new Integer(4), new String("forth"));
 +                region.clear();
 +                assertEquals(0, region.size());
 +                assertEquals(2, subRegion.size());
 +            }
 +        }
 +        );
 +        
 +    }//end of test case
 +    
 +    public void testClearMultiVM() throws Throwable{
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +        VM vm1 = host.getVM(1);
 +        
 +        //put 3 key/values in vm0 and get from vm1
 +        Object[] objArr = new Object[1];
 +        //Integer in = new Integer(i);
 +        //objArr[0] = (Object) in;
 +        for (int i=1; i<4; i++){
 +            objArr[0] = ""+i;
 +            vm0.invoke(ClearMultiVmDUnitTest.class, "putMethod", objArr);
 +            vm1.invoke(ClearMultiVmDUnitTest.class, "getMethod", objArr);
 +        }
 +        
-         AsyncInvocation as1 = vm0.invokeAsync(ClearMultiVmDUnitTest.class, "firstVM");
-         AsyncInvocation as2 = vm1.invokeAsync(ClearMultiVmDUnitTest.class, "secondVM");
++        AsyncInvocation as1 = vm0.invokeAsync(() -> ClearMultiVmDUnitTest.firstVM());
++        AsyncInvocation as2 = vm1.invokeAsync(() -> ClearMultiVmDUnitTest.secondVM());
 +        ThreadUtils.join(as1, 30 * 1000);
 +        ThreadUtils.join(as2, 30 * 1000);
 +        
 +        if(as1.exceptionOccurred()){
 +          Assert.fail("as1 failed", as1.getException());
 +        }
 +        
 +        if(as2.exceptionOccurred()){
 +          Assert.fail("as2 failed", as2.getException());
 +        }
 +        
-         int j = vm0.invokeInt(ClearMultiVmDUnitTest.class, "sizeMethod");
++        int j = vm0.invoke(() -> ClearMultiVmDUnitTest.sizeMethod());
 +        assertEquals(0, j);
 +        
-         j = vm1.invokeInt(ClearMultiVmDUnitTest.class, "sizeMethod");
++        j = vm1.invoke(() -> ClearMultiVmDUnitTest.sizeMethod());
 +        assertEquals(1, j);
 +        
 +        
 +        
 +        int i=6;
 +        objArr[0] = ""+i;
 +        vm1.invoke(ClearMultiVmDUnitTest.class, "getMethod", objArr);
 +        
-         Object ob[] = new Object[1];
-         ob[0] = "secondVM";
-         boolean val = vm1.invokeBoolean(ClearMultiVmDUnitTest.class, "containsValueMethod", ob);
++        boolean val = vm1.invoke(() -> containsValueMethod("secondVM"));
 +        assertEquals(true, val);
 +        
 +    }//end of testClearMultiVM
 +    
 +    public void testClearExceptions(){
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +        VM vm1 = host.getVM(1);
 +        
-         vm1.invoke(ClearMultiVmDUnitTest.class, "localDestroyRegionMethod");
++        vm1.invoke(() -> ClearMultiVmDUnitTest.localDestroyRegionMethod());
 +        vm0.invoke(new CacheSerializableRunnable ("exception in vm0"){
 +            public void run2() throws CacheException {
 +               try{
 +                    region.clear();                    
 +                } catch(RegionDestroyedException rdex){
 +                    fail("Should NOT have thrown RegionDestroyedException");
 +                } 
 +            }
 +        }        
 +        );
 +        
 +        vm1.invoke(new CacheSerializableRunnable ("exception in vm1"){
 +            public void run2() throws CacheException {
 +               try{
 +                    region.clear();
 +                    fail("Should have thrown RegionDestroyedException");
 +                } catch(RegionDestroyedException rdex){
 +                    //pass
 +                } 
 +            }
 +        }        
 +        );
 +        
 +    }//end of testClearExceptions
 +
 +    public void testGiiandClear() throws Throwable{
 +      if (false) {
 +        getSystem().getLogWriter().severe("testGiiandClear skipped because of bug 34963");
 +      } else {
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +        VM vm1 = host.getVM(1);
 +        
 +        SerializableRunnable create = new
 +        CacheSerializableRunnable("create mirrored region") {
 +            public void run2() throws CacheException {
 +                AttributesFactory factory1  = new AttributesFactory();
 +                factory1.setScope(Scope.DISTRIBUTED_ACK);
 +                factory1.setDataPolicy(DataPolicy.REPLICATE);
 +                RegionAttributes attr1 = factory1.create();
 +                mirroredRegion = cache.createRegion("mirrored", attr1);
 +                // reset slow
 +                com.gemstone.gemfire.internal.cache.InitialImageOperation.slowImageProcessing = 0;
 +            }
 +        };
 +        
 +        
 +        vm0.invoke(create);
 +        
 +        vm0.invoke(new CacheSerializableRunnable("put initial data"){
 +            public void run2() throws CacheException {
 +                for(int i=0; i<1000; i++){
 +                    mirroredRegion.put(new Integer(i), (new Integer(i)).toString());
 +                }
 +            }
 +        }
 +        );
 +        
 +        // slow down image processing to make it more likely to get async updates
 +        vm1.invoke(new SerializableRunnable("set slow image processing") {
 +            public void run() {
 +                // if this is a no_ack test, then we need to slow down more because of the
 +                // pauses in the nonblocking operations
 +                int pause = 50;
 +                com.gemstone.gemfire.internal.cache.InitialImageOperation.slowImageProcessing = pause;
 +            }
 +        });
 +        
 +        // now do the get initial image in vm1
 +        AsyncInvocation async1 = vm1.invokeAsync(create);
 +
 +        // try to time a distributed clear to happen in the middle of gii
 +        vm0.invoke(new SerializableRunnable("call clear when gii") {
 +            public void run() {
 +                try{Thread.sleep(3*1000);}catch(InterruptedException ex){fail("interrupted");}
 +                mirroredRegion.clear();                
 +                assertEquals(0, mirroredRegion.size());
 +            }
 +        });
 +        
 +        ThreadUtils.join(async1, 30 * 1000);
 +        if(async1.exceptionOccurred()){
 +          Assert.fail("async1 failed", async1.getException());
 +        }
 +        
 +        SerializableRunnable validate = new
 +        CacheSerializableRunnable("validate for region size") {
 +            public void run2() throws CacheException {
 +                assertEquals(0, mirroredRegion.size());
 +            }
 +        };
 +        
 +        vm0.invoke(validate);
 +        vm1.invoke(validate);
 +      }
 +        
 +    }//end of testGiiandClear
 +    
 +    
 +    //remote vm methods
 +    
 +    public static void firstVM(){
 +          //put one entry
 +          int i=5;
 +          region.put(i, "firstVM");
 +            
 +          //test localClear
 +          region.localClear();
 +          assertFalse(region.containsKey(i));
 +          
 +          paperWork.put("localClear","true");
 +          
 +          // wait unit clear on 2nd VM
 +          boolean val=false;
 +          while (!val) {
 +            val = paperWork.containsKey("localClearVerified");
 +          }
 +          region.put("key10", "value");
 +          region.put("key11", "value");
 +          
 +          // test distributed clear
 +          region.clear();
 +          paperWork.put("clear", "value");
 +          
 +          val = false;
 +          while (!val) {
 +            val = paperWork.containsKey("clearVerified");
 +          }
 +          
 +    }//end of firstVM
 +    
 +    public static void secondVM(){
 +          // verify that localClear on the other VM does not affect size on this VM
 +          boolean val=false;
 +          while (!val) {
 +            val = paperWork.containsKey("localClear");
 +          }
 +          assertEquals(3, region.size());
 +          
 +          paperWork.put("localClearVerified", "value");
 +          
 +          // wait for clear
 +          val = false;
 +          while (!val) {
 +            val = paperWork.containsKey("clear");
 +          }
 +          assertEquals(0, region.size());
 +          
 +          paperWork.put("clearVerified", "value");
 +          region.put(""+6, "secondVM");
 +    }//end of secondVM
 +    
 +    //helper methods
 +    
 +    
 +    public static Object putMethod(Object ob){
 +        Object obj=null;
 +        try{
 +            if(ob != null){
 +                String str = "first";
 +                obj = region.put(ob, str);
 +            }
 +        }catch(Exception ex){
 +            ex.printStackTrace();
 +            fail("Failed while region.put");
 +        }
 +        return obj;
 +    }
 +    
 +    public static Object getMethod(Object ob){
 +        Object obj=null;
 +        try{
 +            obj = region.get(ob);
 +        } catch(Exception ex){
 +            fail("Failed while region.get");
 +        }
 +        return obj;
 +    }
 +    
 +    public static boolean containsValueMethod(Object ob){
 +        boolean flag = false;
 +        try{
 +            flag = region.containsValue(ob);
 +        }catch(Exception ex){
 +            fail("Failed while region.containsValueMethod");
 +        }
 +        return flag;
 +    }
 +    
 +    public static int sizeMethod(){
 +        int i=0;
 +        try{
 +            i = region.size();
 +        }catch(Exception ex){
 +            fail("Failed while region.size");
 +        }
 +        return i;
 +    }
 +    
 +    public static void localDestroyRegionMethod(){
 +        try{
 +            region.localDestroyRegion();
 +        } catch (Exception ex){
 +            ex.printStackTrace();
 +        }
 +    }//end of localDestroyRegionMethod
 +    
 +    public static void clearExceptions(){
 +        try{
 +            region.clear();
 +            //fail("Should have thrown RegionDestroyedException");
 +        } catch(RegionDestroyedException rdex){
 +            //pass
 +        }
 +    }//end of clearExceptions
 +    
 +}//end of class


[03/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/SearchAndLoadDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/SearchAndLoadDUnitTest.java
index 01aa205,0000000..f26f94a
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/SearchAndLoadDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/SearchAndLoadDUnitTest.java
@@@ -1,1017 -1,0 +1,1017 @@@
 +/*
 + * 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.cache30;
 +
 +import java.util.concurrent.CountDownLatch;
 +import java.util.concurrent.TimeUnit;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheLoader;
 +import com.gemstone.gemfire.cache.CacheLoaderException;
 +import com.gemstone.gemfire.cache.CacheWriter;
 +import com.gemstone.gemfire.cache.CacheWriterException;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.EntryEvent;
 +import com.gemstone.gemfire.cache.LoaderHelper;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionAttributes;
 +import com.gemstone.gemfire.cache.RegionEvent;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.TimeoutException;
 +import com.gemstone.gemfire.test.dunit.Assert;
 +import com.gemstone.gemfire.test.dunit.AsyncInvocation;
 +import com.gemstone.gemfire.test.dunit.DistributedTestCase;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.IgnoredException;
 +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;
 +
 +/**
 + * This class tests various search load and write scenarios for distributed regions
 + */
 +@SuppressWarnings({"deprecation", "unchecked", "rawtypes", "serial"})
 +public class SearchAndLoadDUnitTest extends CacheTestCase {
 +
 +  static boolean loaderInvoked;
 +  static boolean  remoteLoaderInvoked;
 +  static int remoteLoaderInvokedCount;
 +  static boolean netSearchCalled;
 +  static boolean netSearchHit;
 +  static boolean netWriteInvoked;
 +  static boolean operationWasCreate;
 +  static boolean originWasRemote;
 +  static int writerInvocationCount;
 +
 +  /** A <code>CacheListener</code> used by a test */
 +  protected static TestCacheListener listener;
 +
 +  /** A <code>CacheLoader</code> used by a test */
 +  protected static TestCacheLoader loader;
 +
 +  /** A <code>CacheWriter</code> used by a test */
 +  protected static TestCacheWriter writer;
 +
 +  static boolean exceptionThrown;
 +  static final CountDownLatch readyForExceptionLatch = new CountDownLatch(1);
 +  static final CountDownLatch loaderInvokedLatch = new CountDownLatch(1);
 +
 +  public SearchAndLoadDUnitTest(String name) {
 +    super(name);
 +  }
 +
 +  @Override
 +  protected final void preTearDownCacheTestCase() throws Exception {
 +    for (int h = 0; h < Host.getHostCount(); h++) {
 +      Host host = Host.getHost(h);
 +      for (int v = 0; v < host.getVMCount(); v++) {
 +        host.getVM(v).invoke(new SerializableRunnable("Clean up") {
 +            public void run() {
 +              cleanup();
 +            }
 +          });
 +      }
 +    }
 +    cleanup();
 +  }
 +
 +  /**
 +   * Clears fields used by a test
 +   */
 +  protected static void cleanup() {
 +    listener = null;
 +    loader = null;
 +    writer = null;
 +  }
 +
 +  /**
 +   * Returns region attributes for a <code>GLOBAL</code> region
 +   */
 +  protected RegionAttributes getRegionAttributes() {
 +    AttributesFactory factory = new AttributesFactory();
 +    factory.setScope(Scope.DISTRIBUTED_ACK);
 +    factory.setEarlyAck(false);
 +    return factory.create();
 +  }
 +
 +  public void testNetSearch()
 +  throws CacheException, InterruptedException {
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    final String name = this.getUniqueName() + "-ACK";
 +    final String objectName = "NetSearchKey";
 +    final Integer value = new Integer(440);
 +    vm0.invoke(new SerializableRunnable("Create ACK Region") {
 +      public void run() {
 +        try {
 +
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.DISTRIBUTED_ACK);
 +          factory.setEarlyAck(false);
 +          factory.setStatisticsEnabled(true);
 +          Region region = createRegion(name,factory.create());
 +          region.create(objectName,null);
 +        }
 +        catch (CacheException ex) {
 +          Assert.fail("While creating ACK region", ex);
 +        }
 +      }
 +    });
 +
 +    vm1.invoke(new SerializableRunnable("Create ACK Region") {
 +      public void run() {
 +        try {
 +
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.DISTRIBUTED_ACK);
 +          factory.setEarlyAck(false);
 +          factory.setStatisticsEnabled(true);
 +          Region region = createRegion(name,factory.create());
 +          region.put(objectName,value);
 +
 +        }
 +        catch (CacheException ex) {
 +          Assert.fail("While creating ACK region", ex);
 +        }
 +      }
 +    });
 +
 +    vm2.invoke(new SerializableRunnable("Create ACK Region") {
 +      public void run() {
 +        try {
 +
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.DISTRIBUTED_ACK);
 +          factory.setEarlyAck(false);
 +          factory.setStatisticsEnabled(true);
 +          Region region = createRegion(name,factory.create());
 +          region.create(objectName,null);
 +
 +        }
 +        catch (CacheException ex) {
 +          Assert.fail("While creating ACK region", ex);
 +        }
 +      }
 +    });
 +
 +    vm0.invoke(new SerializableRunnable("Get a value") {
 +      public void run() {
 +        try {
 +          Object result = null;
 +          result = getRootRegion().getSubregion(name).get(objectName);
 +          assertEquals(value,result);
 +
 +//         System.err.println("Results is " + result.toString() + " Key is " + objectName.toString());
 +        }
 +        catch(CacheLoaderException cle) {
 +          Assert.fail("While Get a value", cle);
 +        }
 +        catch(TimeoutException te) {
 +          Assert.fail("While Get a value", te);
 +        }
 +      }
 +
 +    });
 +  }
 +
 +
 +  /**
 +   * This test is for a bug in which a cache loader threw an exception
 +   * that caused the wrong value to be put in a Future in nonTxnFindObject.  This
 +   * in turn caused a concurrent search for the object to not invoke the loader a
 +   * second time.
 +   * 
 +   * VM0 is used to create a cache and a region having a loader that simulates the
 +   * conditions that caused the bug.  One async thread then does a get() which invokes
 +   * the loader.  Another async thread does a get() which reaches nonTxnFindObject
 +   * and blocks waiting for the first thread's load to complete.  The loader then
 +   * throws an exception that is sent back to the first thread.  The second thread
 +   * should then cause the loader to be invoked again, and this time the loader will
 +   * return a value.  Both threads then validate that they received the expected
 +   * result.
 +   */
 +  public void testConcurrentLoad() throws Throwable {
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    
 +    final String name = this.getUniqueName() + "Region";
 +    final String objectName = "theKey";
 +    final Integer value = new Integer(44);
 +    final String exceptionString = "causing first cache-load to fail";
 +
 +    remoteLoaderInvoked = false;
 +    loaderInvoked = false;
 +    
 +    vm0.invoke(new CacheSerializableRunnable("create region " + name + " in vm0") {
 +      public void run2() {
 +        remoteLoaderInvoked = false;
 +        loaderInvoked = false;
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.DISTRIBUTED_ACK);
 +        factory.setConcurrencyChecksEnabled(true);
 +        factory.setCacheLoader(new CacheLoader() {
 +          boolean firstInvocation = true;
 +          public synchronized Object load(LoaderHelper helper) {
 +            System.out.println("invoked cache loader for " + helper.getKey());
 +            loaderInvoked = true;
 +            loaderInvokedLatch.countDown();
 +            if (firstInvocation) {
 +              firstInvocation = false;
 +              try { 
 +                // wait for both threads to be ready for the exception to be thrown
 +                System.out.println("waiting for vm0t2 to be ready before throwing exception");
 +                readyForExceptionLatch.await(30, TimeUnit.SECONDS);
 +                // give the second thread time to get into loader code
 +                Thread.sleep(5000);
 +              } catch (InterruptedException e) {
 +                fail("interrupted");
 +              }
 +              System.out.println("throwing exception");
 +              exceptionThrown = true;
 +              throw new RuntimeException(exceptionString);
 +            }
 +            System.out.println("returning value="+value);
 +            return value;
 +          }
 +
 +          public void close() {
 +
 +          }
 +        });
 +
 +        Region region = createRegion(name,factory.create());
 +        region.create(objectName, null);
 +        IgnoredException.addIgnoredException(exceptionString);
 +      }
 +    });
 +
 +    AsyncInvocation async1 = null;
 +    try {
 +      async1 = vm0.invokeAsync(new CacheSerializableRunnable("Concurrently invoke the remote loader on the same key - t1") {
 +        public void run2() {
 +          Region region = getCache().getRegion("root/"+name);
 +  
 +          LogWriterUtils.getLogWriter().info("t1 is invoking get("+objectName+")");
 +          try {
 +            LogWriterUtils.getLogWriter().info("t1 retrieved value " + region.get(objectName));
 +            fail("first load should have triggered an exception");
 +          } catch (RuntimeException e) {
 +            if (!e.getMessage().contains(exceptionString)) {
 +              throw e;
 +            }
 +          }
 +        }
 +      });
 +      vm0.invoke(new CacheSerializableRunnable("Concurrently invoke the loader on the same key - t2") {
 +        public void run2() {
 +          final Region region = getCache().getRegion("root/"+name);
 +          final Object[] valueHolder = new Object[1];
 +  
 +          // wait for vm1 to cause the loader to be invoked
 +          LogWriterUtils.getLogWriter().info("t2 is waiting for loader to be invoked by t1");
 +          try {
 +            loaderInvokedLatch.await(30, TimeUnit.SECONDS);
 +          } catch (InterruptedException e) {
 +            fail("interrupted");
 +          }
 +          assertTrue(loaderInvoked);
 +          
 +          Thread t = new Thread("invoke get()") {
 +            public void run() {
 +              try {
 +                valueHolder[0] = region.get(objectName);
 +              } catch (RuntimeException e) {
 +                valueHolder[0] = e;
 +              }
 +            }
 +          };
 +          
 +          t.setDaemon(true);
 +          t.start();
 +          try {
 +            // let the thread get to the point of blocking on vm1's Future
 +            // in LocalRegion.nonTxnFindObject()
 +            Thread.sleep(5000);
 +          } catch (InterruptedException e) {
 +            fail("interrupted");
 +          }
 +          
 +          readyForExceptionLatch.countDown();
 +          try {
 +            t.join(30000);
 +          } catch (InterruptedException e) {
 +            fail("interrupted");
 +          }
 +          if (t.isAlive()) {
 +            t.interrupt();
 +            fail("get() operation blocked for too long - test needs some work");
 +          }
 +          
 +          LogWriterUtils.getLogWriter().info("t2 is invoking get("+objectName+")");
 +          Object value = valueHolder[0];
 +          if (value instanceof RuntimeException) {
 +            if ( ((Exception)value).getMessage().contains(exceptionString) ) {
 +              fail("second load should not have thrown an exception");
 +            } else {
 +              throw (RuntimeException)value;
 +            }
 +          } else {
 +            LogWriterUtils.getLogWriter().info("t2 retrieved value " + value);
 +            assertNotNull(value);
 +          }
 +        }
 +      });
 +    } finally {
 +      if (async1 != null) {
 +        async1.join();
 +        if (async1.exceptionOccurred()) {
 +          throw async1.getException();
 +        }
 +      }
 +    }
 +  }
 +  
 +  
 +  public void testNetLoadNoLoaders() throws CacheException, InterruptedException {
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    final String name = this.getUniqueName() + "-ACK";
 +    final String objectName = "B";
 +    SerializableRunnable create =
 +          new CacheSerializableRunnable("Create Region") {
 +      public void run2() throws CacheException {
 +        AttributesFactory factory =
 +          new AttributesFactory();
 +        factory.setScope(Scope.DISTRIBUTED_ACK);
 +        factory.setEarlyAck(false);
 +        createRegion(name,factory.create());
 +
 +      }
 +    };
 +
 +    vm0.invoke(create);
 +    vm1.invoke(create);
 +
 +    vm0.invoke(new SerializableRunnable("Get with No Loaders defined") {
 +      public void run() {
 +        try {
 +          Object result = getRootRegion().getSubregion(name).get(objectName);
 +          assertNull(result);
 +        }
 +        catch(CacheLoaderException cle) {
 +          Assert.fail("While getting value for ACK region", cle);
 +        }
 +        catch(TimeoutException te) {
 +          Assert.fail("While getting value for ACK region", te);
 +        }
 +
 +      }
 +    });
 +
 +
 +  }
 +
 +  public void testNetLoad()
 +  throws CacheException, InterruptedException {
 +    Invoke.invokeInEveryVM(DistributedTestCase.class,
 +        "disconnectFromDS");
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    final String name = this.getUniqueName() + "-ACK";
 +    final String objectName = "B";
 +    final Integer value = new Integer(43);
 +    loaderInvoked = false;
 +    remoteLoaderInvoked = false;
 +    vm0.invoke(new SerializableRunnable("Create ACK Region") {
 +      public void run() {
 +        try {
 +          loaderInvoked = false;
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.DISTRIBUTED_ACK);
 +          factory.setEarlyAck(false);
 +      //  factory.setCacheLoader(new CacheLoader() {
 +      //      public Object load(LoaderHelper helper) {
 +      ///        loaderInvoked = true;
 +      //        return value;
 +      //      }
 +//
 +      //      public void close() {
 +//
 +      //      }
 +      //    });
 +
 +          Region region = createRegion(name,factory.create());
 +          region.create(objectName,null);
 +
 +        }
 +        catch (CacheException ex) {
 +          Assert.fail("While creating ACK region", ex);
 +        }
 +      }
 +    });
 +
 +    vm1.invoke(new SerializableRunnable("Create ACK Region") {
 +      public void run() {
 +        try {
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.DISTRIBUTED_ACK);
 +          factory.setEarlyAck(false);
 +          factory.setCacheLoader(new CacheLoader() {
 +            public Object load(LoaderHelper helper) {
 +              remoteLoaderInvoked = true;
 +              return value;
 +            }
 +            public void close() {
 +
 +            }
 +          });
 +          createRegion(name,factory.create());
 +        }
 +        catch (CacheException ex) {
 +          Assert.fail("While creating ACK region", ex);
 +        }
 +      }
 +    });
 +    vm0.invoke(new SerializableRunnable("Get a value from remote loader") {
 +      public void run() {
 +        for (int i=0;i< 1;i++) {
 +          try {
 +            Object result = getRootRegion().getSubregion(name).get(objectName);
 +            assertEquals(value,result);
 +            assertEquals(new Boolean(loaderInvoked),Boolean.FALSE);
 +           // getRootRegion().getSubregion(name).invalidate(objectName);
 +
 +          }
 +          catch(CacheLoaderException cle) {
 +            Assert.fail("While getting value for ACK region", cle);
 +
 +          }
 +/*        catch(EntryNotFoundException enfe) {
 +            fail("While getting value for ACK region", enfe);
 +
 +          }*/
 +          catch(TimeoutException te) {
 +            Assert.fail("While getting value for ACK region", te);
 +
 +          }
 +        }
 +      }
 +
 +    });
 +  }
 +
 +  /**
 +   * Confirm that a netLoad that returns null will NOT allow other netLoad methods
 +   * to be called.
 +   */
 +  public void testEmptyNetLoad()
 +  throws CacheException, InterruptedException {
 +    Invoke.invokeInEveryVM(DistributedTestCase.class,
 +        "disconnectFromDS");
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    final String name = this.getUniqueName() + "-ACK";
 +    final String objectName = "B";
 +    loaderInvoked = false;
 +    remoteLoaderInvoked = false;
 +    remoteLoaderInvokedCount = 0;
 +    vm0.invoke(new SerializableRunnable("Create ACK Region") {
 +      public void run() {
 +        loaderInvoked = false;
 +        remoteLoaderInvoked = false;
 +        remoteLoaderInvokedCount = 0;
 +        try {
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.DISTRIBUTED_ACK);
 +          factory.setEarlyAck(false);
 +      //  factory.setCacheLoader(new CacheLoader() {
 +      //      public Object load(LoaderHelper helper) {
 +      ///        loaderInvoked = true;
 +      //        return value;
 +      //      }
 +//
 +      //      public void close() {
 +//
 +      //      }
 +      //    });
 +          Region region = createRegion(name,factory.create());
 +          region.create(objectName,null);
 +        }
 +        catch (CacheException ex) {
 +          Assert.fail("While creating ACK region", ex);
 +        }
 +      }
 +    });
 +
 +    SerializableRunnable installLoader = new SerializableRunnable("Create ACK Region") {
 +        public void run() {
 +          loaderInvoked = false;
 +          remoteLoaderInvoked = false;
 +          remoteLoaderInvokedCount = 0;
 +          try {
 +            AttributesFactory factory = new AttributesFactory();
 +            factory.setScope(Scope.DISTRIBUTED_ACK);
 +            factory.setEarlyAck(false);
 +            factory.setCacheLoader(new CacheLoader() {
 +                public Object load(LoaderHelper helper) {
 +                  remoteLoaderInvoked = true;
 +                  remoteLoaderInvokedCount++;
 +                  return null;
 +                }
 +                public void close() {
 +
 +                }
 +              });
 +            createRegion(name,factory.create());
 +          }
 +          catch (CacheException ex) {
 +            Assert.fail("While creating ACK region", ex);
 +          }
 +        }
 +      };
 +    vm1.invoke(installLoader);
 +    vm2.invoke(installLoader);
 +    vm0.invoke(new SerializableRunnable("Get a value from remote loader") {
 +      public void run() {
 +        for (int i=0;i< 1;i++) {
 +          try {
 +            Object result = getRootRegion().getSubregion(name).get(objectName);
 +            assertEquals(null,result);
 +            assertEquals(false, loaderInvoked);
 +           // getRootRegion().getSubregion(name).invalidate(objectName);
 +
 +          }
 +          catch(CacheLoaderException cle) {
 +            Assert.fail("While getting value for ACK region", cle);
 +
 +          }
 +/*        catch(EntryNotFoundException enfe) {
 +            fail("While getting value for ACK region", enfe);
 +
 +          }*/
 +          catch(TimeoutException te) {
 +            Assert.fail("While getting value for ACK region", te);
 +
 +          }
 +        }
 +      }
 +
 +    });
 +    // we only invoke one netLoad loader even when they return null.
 +    boolean xor = vmRemoteLoaderInvoked(vm1) ^ vmRemoteLoaderInvoked(vm2);
 +    assertEquals("vm1=" + vmRemoteLoaderInvoked(vm1) + " vm2=" + vmRemoteLoaderInvoked(vm2)
 +                 + " vm1Count=" + vmRemoteLoaderInvokedCount(vm1) + " vm2Count=" + vmRemoteLoaderInvokedCount(vm2), true, xor);
 +    int total = vmRemoteLoaderInvokedCount(vm1) + vmRemoteLoaderInvokedCount(vm2);
 +    assertEquals("vm1=" + vmRemoteLoaderInvokedCount(vm1) + " vm2=" + vmRemoteLoaderInvokedCount(vm2), 1, total);
 +  }
 +
 +  public static boolean vmRemoteLoaderInvoked(VM vm) {
-     Boolean v = (Boolean)vm.invoke(SearchAndLoadDUnitTest.class, "fetchRemoteLoaderInvoked");
++    Boolean v = (Boolean)vm.invoke(() -> SearchAndLoadDUnitTest.fetchRemoteLoaderInvoked());
 +    return v.booleanValue();
 +  }
 +  public static int vmRemoteLoaderInvokedCount(VM vm) {
-     Integer v = (Integer)vm.invoke(SearchAndLoadDUnitTest.class, "fetchRemoteLoaderInvokedCount");
++    Integer v = (Integer)vm.invoke(() -> SearchAndLoadDUnitTest.fetchRemoteLoaderInvokedCount());
 +    return v.intValue();
 +  }
 +
 +  public static Boolean fetchRemoteLoaderInvoked() {
 +    return Boolean.valueOf(remoteLoaderInvoked);
 +  }
 +  public static Integer fetchRemoteLoaderInvokedCount() {
 +    return new Integer(remoteLoaderInvokedCount);
 +  }
 +  
 +  public void testLocalLoad()
 +  throws CacheException, InterruptedException {
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    final String name = this.getUniqueName() + "-ACK";
 +    final String objectName = "C";
 +    final Integer value = new Integer(44);
 +    remoteLoaderInvoked = false;
 +    loaderInvoked = false;
 +    vm0.invoke(new SerializableRunnable("Create ACK Region") {
 +      public void run() {
 +        remoteLoaderInvoked = false;
 +        loaderInvoked = false;
 +        try {
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.DISTRIBUTED_ACK);
 +          factory.setEarlyAck(false);
 +          factory.setCacheLoader(new CacheLoader() {
 +            public Object load(LoaderHelper helper) {
 +              loaderInvoked = true;
 +              return value;
 +            }
 +
 +            public void close() {
 +
 +            }
 +          });
 +
 +          Region region = createRegion(name,factory.create());
 +          region.create(objectName,null);
 +
 +        }
 +        catch (CacheException ex) {
 +          Assert.fail("While creating ACK region", ex);
 +        }
 +      }
 +    });
 +
 +    vm1.invoke(new SerializableRunnable("Create ACK Region") {
 +      public void run() {
 +        remoteLoaderInvoked = false;
 +        loaderInvoked = false;
 +        try {
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.DISTRIBUTED_ACK);
 +          factory.setEarlyAck(false);
 +          factory.setCacheLoader(new CacheLoader() {
 +            public Object load(LoaderHelper helper) {
 +              remoteLoaderInvoked = true;
 +              return value;
 +            }
 +            public void close() {
 +
 +            }
 +          });
 +          createRegion(name,factory.create());
 +        }
 +        catch (CacheException ex) {
 +          Assert.fail("While creating ACK region", ex);
 +        }
 +      }
 +    });
 +    vm0.invoke(new SerializableRunnable("Get a value from local loader") {
 +      public void run() {
 +        try {
 +          Object result = getRootRegion().getSubregion(name).get(objectName);
 +          assertEquals(value,result);
 +          assertEquals(new Boolean(loaderInvoked),Boolean.TRUE);
 +          assertEquals(new Boolean(remoteLoaderInvoked),Boolean.FALSE);
 +
 +        }
 +        catch(CacheLoaderException cle) {
 +
 +        }
 +        catch(TimeoutException te) {
 +        }
 +      }
 +
 +    });
 +  }
 +
 +
 +  public void testNetWrite()
 +  throws CacheException, InterruptedException {
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    final String name = this.getUniqueName() + "-ACK";
 +    final String objectName = "Gemfire7";
 +    final Integer value = new Integer(483);
 +
 +    vm0.invoke(new SerializableRunnable("Create ACK Region with cacheWriter") {
 +      public void run() {
 +        netWriteInvoked = false;
 +        try {
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.DISTRIBUTED_ACK);
 +          factory.setCacheWriter(new CacheWriter() {
 +            public void beforeCreate(EntryEvent e) throws CacheWriterException {
 +              netWriteInvoked = true;
 +              return;
 +            }
 +            public void beforeUpdate(EntryEvent e) throws CacheWriterException {
 +              netWriteInvoked = true;
 +              return;
 +            }
 +            public void beforeDestroy(EntryEvent e) throws CacheWriterException {
 +              return;
 +            }
 +            public void beforeRegionDestroy(RegionEvent e) throws CacheWriterException {
 +              return;
 +            }
 +            public void beforeRegionClear(RegionEvent e) throws CacheWriterException {
 +              return;
 +            }
 +            public void close() {
 +            }
 +          });
 +
 +          createRegion(name,factory.create());
 +
 +        }
 +        catch (CacheException ex) {
 +          Assert.fail("While creating ACK region", ex);
 +        }
 +      }
 +    });
 +    vm1.invoke(new SerializableRunnable("Create ACK Region") {
 +        public void run() {
 +          loaderInvoked = false;
 +          remoteLoaderInvoked = false;
 +          netWriteInvoked = false;
 +          try {
 +            AttributesFactory factory = new AttributesFactory();
 +            factory.setScope(Scope.DISTRIBUTED_ACK);
 +            createRegion(name,factory.create());
 +          }
 +          catch (CacheException ex) {
 +            Assert.fail("While creating ACK region", ex);
 +          }
 +        }
 +    });
 +
 +    vm1.invoke(new SerializableRunnable("Do a put operation resulting in cache writer notification in other vm") {
 +      public void run() {
 +        try {
 +          getRootRegion().getSubregion(name).put(objectName,value);
 +          try {
 +            Object result = getRootRegion().getSubregion(name).get(objectName);
 +            assertEquals(result,value);
 +           }
 +           catch(CacheLoaderException cle) {
 +           }
 +           catch(TimeoutException te) {
 +           }
 +        }
 +        catch(CacheWriterException cwe) {
 +
 +        }
 +        catch(TimeoutException te) {
 +        }
 +      }
 +
 +    });
 +
 +    vm0.invoke(new SerializableRunnable("ensure that cache writer was invoked") {
 +      public void run() {
 +        assertTrue("expected cache writer to be invoked", netWriteInvoked);
 +      }
 +    });
 +  }
 +
 +  
 +  public void testOneHopNetWrite() throws CacheException, InterruptedException {
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    final String name = this.getUniqueName() + "Region";
 +    final String objectName = "Object7";
 +    final Integer value = new Integer(483);
 +    final Integer updateValue = new Integer(484);
 +
 +    vm0.invoke(new SerializableRunnable("Create replicated region with cacheWriter") {
 +      public void run() {
 +        netWriteInvoked = false;
 +        operationWasCreate = false;
 +        originWasRemote = false;
 +        writerInvocationCount = 0;
 +        try {
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.DISTRIBUTED_ACK);
 +          factory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
 +          factory.setCacheWriter(new CacheWriter() {
 +            public void beforeCreate(EntryEvent e) throws CacheWriterException {
 +              e.getRegion().getCache().getLogger().info("cache writer beforeCreate invoked for " + e);
 +              netWriteInvoked = true;
 +              operationWasCreate = true;
 +              originWasRemote = e.isOriginRemote();
 +              writerInvocationCount++;
 +              return;
 +            }
 +            public void beforeUpdate(EntryEvent e) throws CacheWriterException {
 +              e.getRegion().getCache().getLogger().info("cache writer beforeUpdate invoked for " + e);
 +              netWriteInvoked = true;
 +              operationWasCreate = false;
 +              originWasRemote = e.isOriginRemote();
 +              writerInvocationCount++;
 +              return;
 +            }
 +            public void beforeDestroy(EntryEvent e) throws CacheWriterException {  }
 +            public void beforeRegionDestroy(RegionEvent e) throws CacheWriterException {   }
 +            public void beforeRegionClear(RegionEvent e) throws CacheWriterException {   }
 +            public void close() { }
 +          });
 +
 +          createRegion(name,factory.create());
 +
 +        }
 +        catch (CacheException ex) {
 +          Assert.fail("While creating replicated region", ex);
 +        }
 +      }
 +    });
 +    vm1.invoke(new SerializableRunnable("Create empty Region") {
 +        public void run() {
 +          try {
 +            AttributesFactory factory = new AttributesFactory();
 +            factory.setScope(Scope.DISTRIBUTED_ACK);
 +            factory.setDataPolicy(DataPolicy.EMPTY);
 +            createRegion(name,factory.create());
 +          }
 +          catch (CacheException ex) {
 +            Assert.fail("While creating empty region", ex);
 +          }
 +        }
 +    });
 +
 +    vm1.invoke(new SerializableRunnable("do a put that should be proxied in the other vm and invoke its cache writer") {
 +      public void run() {
 +        try {
 +          getRootRegion().getSubregion(name).put(objectName,value);
 +        } catch(CacheWriterException cwe) {
 +        } catch(TimeoutException te) {
 +        }
 +      }
 +    });
 +
 +    vm0.invoke(new SerializableRunnable("ensure that cache writer was invoked with correct settings in event") {
 +      public void run() {
 +        assertTrue("expected cache writer to be invoked", netWriteInvoked);
 +        assertTrue("expected originRemote to be true", originWasRemote);
 +        assertTrue("expected event to be create", operationWasCreate);
 +        assertEquals("expected only one cache writer invocation", 1, writerInvocationCount);
 +        // set flags for the next test - updating the same key
 +        netWriteInvoked = false;
 +        writerInvocationCount = 0;
 +      }
 +    });
 +
 +    vm1.invoke(new SerializableRunnable("do an update that should be proxied in the other vm and invoke its cache writer") {
 +      public void run() {
 +        try {
 +          getRootRegion().getSubregion(name).put(objectName,updateValue);
 +        } catch(CacheWriterException cwe) {
 +        } catch(TimeoutException te) {
 +        }
 +      }
 +    });
 +
 +    vm0.invoke(new SerializableRunnable("ensure that cache writer was invoked with correct settings in event") {
 +      public void run() {
 +        assertTrue("expected cache writer to be invoked", netWriteInvoked);
 +        assertTrue("expected originRemote to be true", originWasRemote);
 +        assertTrue("expected event to be create", operationWasCreate);
 +        assertEquals("expected only one cache writer invocation", 1, writerInvocationCount);
 +      }
 +    });
 +  }
 +
 +
 +  /** same as the previous test but the cache writer is in a third, non-replicated, vm */
 +  public void testOneHopNetWriteRemoteWriter() throws CacheException, InterruptedException {
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    final String name = this.getUniqueName() + "Region";
 +    final String objectName = "Object7";
 +    final Integer value = new Integer(483);
 +    final Integer updateValue = new Integer(484);
 +
 +    vm0.invoke(new SerializableRunnable("Create replicate Region") {
 +      public void run() {
 +        try {
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.DISTRIBUTED_ACK);
 +          factory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
 +          createRegion(name,factory.create());
 +        }
 +        catch (CacheException ex) {
 +          Assert.fail("While creating empty region", ex);
 +        }
 +      }
 +    });
 +    vm1.invoke(new SerializableRunnable("Create empty Region") {
 +        public void run() {
 +          try {
 +            AttributesFactory factory = new AttributesFactory();
 +            factory.setScope(Scope.DISTRIBUTED_ACK);
 +            factory.setDataPolicy(DataPolicy.EMPTY);
 +            createRegion(name,factory.create());
 +          }
 +          catch (CacheException ex) {
 +            Assert.fail("While creating empty region", ex);
 +          }
 +        }
 +    });
 +    vm2.invoke(new SerializableRunnable("Create replicated region with cacheWriter") {
 +      public void run() {
 +        netWriteInvoked = false;
 +        operationWasCreate = false;
 +        originWasRemote = false;
 +        writerInvocationCount = 0;
 +        try {
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.DISTRIBUTED_ACK);
 +          factory.setDataPolicy(DataPolicy.EMPTY);
 +          factory.setCacheWriter(new CacheWriter() {
 +            public void beforeCreate(EntryEvent e) throws CacheWriterException {
 +              e.getRegion().getCache().getLogger().info("cache writer beforeCreate invoked for " + e);
 +              netWriteInvoked = true;
 +              operationWasCreate = true;
 +              originWasRemote = e.isOriginRemote();
 +              writerInvocationCount++;
 +              return;
 +            }
 +            public void beforeUpdate(EntryEvent e) throws CacheWriterException {
 +              e.getRegion().getCache().getLogger().info("cache writer beforeUpdate invoked for " + e);
 +              netWriteInvoked = true;
 +              operationWasCreate = false;
 +              originWasRemote = e.isOriginRemote();
 +              writerInvocationCount++;
 +              return;
 +            }
 +            public void beforeDestroy(EntryEvent e) throws CacheWriterException {  }
 +            public void beforeRegionDestroy(RegionEvent e) throws CacheWriterException {   }
 +            public void beforeRegionClear(RegionEvent e) throws CacheWriterException {   }
 +            public void close() { }
 +          });
 +
 +          createRegion(name,factory.create());
 +
 +        }
 +        catch (CacheException ex) {
 +          Assert.fail("While creating replicated region", ex);
 +        }
 +      }
 +    });
 +
 +    vm1.invoke(new SerializableRunnable("do a put that should be proxied in the other vm and invoke its cache writer") {
 +      public void run() {
 +        try {
 +          getRootRegion().getSubregion(name).put(objectName,value);
 +        } catch(CacheWriterException cwe) {
 +        } catch(TimeoutException te) {
 +        }
 +      }
 +    });
 +
 +    vm2.invoke(new SerializableRunnable("ensure that cache writer was invoked with correct settings in event") {
 +      public void run() {
 +        assertTrue("expected cache writer to be invoked", netWriteInvoked);
 +        assertTrue("expected originRemote to be true", originWasRemote);
 +        assertTrue("expected event to be create", operationWasCreate);
 +        assertEquals("expected only one cache writer invocation", 1, writerInvocationCount);
 +        // set flags for the next test - updating the same key
 +        netWriteInvoked = false;
 +        writerInvocationCount = 0;
 +      }
 +    });
 +
 +    vm1.invoke(new SerializableRunnable("do an update that should be proxied in the other vm and invoke its cache writer") {
 +      public void run() {
 +        try {
 +          getRootRegion().getSubregion(name).put(objectName,updateValue);
 +        } catch(CacheWriterException cwe) {
 +        } catch(TimeoutException te) {
 +        }
 +      }
 +    });
 +
 +    vm2.invoke(new SerializableRunnable("ensure that cache writer was invoked with correct settings in event") {
 +      public void run() {
 +        assertTrue("expected cache writer to be invoked", netWriteInvoked);
 +        assertTrue("expected originRemote to be true", originWasRemote);
 +        assertTrue("expected event to be create", operationWasCreate);
 +        assertEquals("expected only one cache writer invocation", 1, writerInvocationCount);
 +      }
 +    });
 +  }
 +}
 +
 +



[50/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/AbstractOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/AbstractOp.java
index 0ae0437,0000000..28c1e0f
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/AbstractOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/AbstractOp.java
@@@ -1,436 -1,0 +1,436 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import java.io.ByteArrayInputStream;
 +import java.io.DataInputStream;
 +import java.net.SocketTimeoutException;
 +
 +import org.apache.logging.log4j.Logger;
 +
 +import com.gemstone.gemfire.InternalGemFireError;
 +import com.gemstone.gemfire.cache.client.ServerConnectivityException;
 +import com.gemstone.gemfire.cache.client.ServerOperationException;
 +import com.gemstone.gemfire.distributed.internal.DistributionManager;
 +import com.gemstone.gemfire.internal.HeapDataOutputStream;
 +import com.gemstone.gemfire.internal.Version;
 +import com.gemstone.gemfire.internal.cache.CacheServerImpl;
 +import com.gemstone.gemfire.internal.cache.PutAllPartialResultException;
 +import com.gemstone.gemfire.internal.cache.TXManagerImpl;
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.ChunkedMessage;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Part;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.ServerConnection;
 +import com.gemstone.gemfire.internal.logging.LogService;
 +import com.gemstone.gemfire.internal.logging.log4j.LogMarker;
 +
 +/**
 + * Represents an operation that can be performed in a client by sending
 + * a message to a server.
 + * @since 5.7
 + */
 +public abstract class AbstractOp implements Op {
 +  
 +  private static final Logger logger = LogService.getLogger();
 +  
 +  private final Message msg;
 +
 +  protected AbstractOp(int msgType, int msgParts) {
 +    this.msg = new Message(msgParts, Version.CURRENT);
 +    getMessage().setMessageType(msgType);
 +  }
 +
 +  /**
 +   * Returns the message that this op will send to the server
 +   */
 +  protected Message getMessage() {
 +    return this.msg;
 +  }
 +  protected void initMessagePart() {
 +    
 +  }
 +  /**
 +   * Sets the transaction id on the message
 +   */
 +  private void setMsgTransactionId() {
 +    if (participateInTransaction()
 +        && getMessage().getTransactionId() == TXManagerImpl.NOTX) {
 +      getMessage().setTransactionId(TXManagerImpl.getCurrentTXUniqueId());
 +    }
 +  }
 +
 +  /**
 +   * Attempts to send this operation's message out on the
 +   * given connection
 +   * @param cnx the connection to use when sending
 +   * @throws Exception if the send fails
 +   */
 +  protected void attemptSend(Connection cnx) throws Exception {
 +    setMsgTransactionId();
 +    if (logger.isTraceEnabled(LogMarker.DISTRIBUTION_BRIDGE_SERVER)) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("Sending op={} using {}", getShortClassName(), cnx);
 +      }
 +    }
 +    getMessage().setComms(cnx.getSocket(), cnx.getInputStream(),
 +        cnx.getOutputStream(), cnx.getCommBuffer(), cnx.getStats());
 +    try {
 +      sendMessage(cnx);
 +    } finally {
 +      getMessage().unsetComms();
 +    }
 +  }
 +
 +  /** returns the class name w/o package information.  useful in logging */
 +  public String getShortClassName() {
 +    String cname = getClass().getName();
 +    return cname.substring(getClass().getPackage().getName().length()+1);
 +  }
 +
 +  /**
 +   * New implementations of AbstractOp should override this method if the
 +   * implementation should be excluded from client authentication. e.g.
 +   * PingOp#sendMessage(Connection cnx)
 +   * 
 +   * @see AbstractOp#needsUserId()
 +   * @see AbstractOp#processSecureBytes(Connection, Message)
 +   * @see ServerConnection#updateAndGetSecurityPart()
 +   */
 +  protected void sendMessage(Connection cnx) throws Exception {
 +    if (cnx.getServer().getRequiresCredentials()) {
 +      // Security is enabled on client as well as on server
-       getMessage().setEarlyAck(Message.MESSAGE_HAS_SECURE_PART);
++      getMessage().setMessageHasSecurePartFlag();
 +      HeapDataOutputStream hdos = new HeapDataOutputStream(Version.CURRENT);
 +      long userId = -1;
 +
 +      if (UserAttributes.userAttributes.get() == null) { // single user mode
 +        userId = cnx.getServer().getUserId();
 +      } else { // multi user mode
 +        Object id = UserAttributes.userAttributes.get().getServerToId().get(
 +            cnx.getServer());
 +        if (id == null) {
 +          // This will ensure that this op is retried on another server, unless
 +          // the retryCount is exhausted. Fix for Bug 41501
 +          throw new ServerConnectivityException(
 +              "Connection error while authenticating user"); // TODO:LOG hdos is not closed??
 +        }
 +        userId = (Long)id;
 +      }
 +      try {
 +        hdos.writeLong(cnx.getConnectionID());
 +        hdos.writeLong(userId);
 +        getMessage().setSecurePart(
 +            ((ConnectionImpl)cnx).getHandShake().encryptBytes(
 +                hdos.toByteArray()));
 +      } finally {
 +        hdos.close();
 +      }
 +    }
 +    getMessage().send(false);
 +  }
 +
 +  /**
 +   * Attempts to read a response to this operation by reading it from the
 +   * given connection, and returning it.
 +   * @param cnx the connection to read the response from
 +   * @return the result of the operation
 +   *         or <code>null</code> if the operation has no result.
 +   * @throws Exception if the execute failed
 +   */
 +  protected Object attemptReadResponse(Connection cnx) throws Exception {
 +    Message msg = createResponseMessage();
 +    if (msg != null) {
 +      msg.setComms(cnx.getSocket(), cnx.getInputStream(),
 +          cnx.getOutputStream(), cnx.getCommBuffer(), cnx.getStats());
 +      if (msg instanceof ChunkedMessage) {
 +        try {
 +          return processResponse(msg, cnx);
 +        } finally {
 +          msg.unsetComms();
 +          // TODO (ashetkar) Handle the case when we fail to read the connection id.
 +          processSecureBytes(cnx, msg);
 +        }
 +      } else {
 +        try {
 +          msg.recv();
 +        } finally {
 +          msg.unsetComms();
 +          processSecureBytes(cnx, msg);
 +        }
 +        return processResponse(msg, cnx);
 +      }
 +    } else {
 +      return null;
 +    }
 +  }
 +
 +  /**
 +   * New implementations of AbstractOp should override this method if the
 +   * implementation should be excluded from client authentication. e.g.
 +   * PingOp#processSecureBytes(Connection cnx, Message message)
 +   * 
 +   * @see AbstractOp#sendMessage(Connection)
 +   * @see AbstractOp#needsUserId()
 +   * @see ServerConnection#updateAndGetSecurityPart()
 +   */
 +  protected void processSecureBytes(Connection cnx, Message message)
 +      throws Exception {
 +    if (cnx.getServer().getRequiresCredentials()) {
 +      if (!message.isSecureMode()) {
 +        // This can be seen during shutdown
 +        if (logger.isDebugEnabled()) {
 +          logger.trace(LogMarker.BRIDGE_SERVER, "Response message from {} for {} has no secure part.", cnx, this);
 +        }
 +        return;
 +      }
 +      byte[] partBytes = message.getSecureBytes();
 +      if (partBytes == null) {
 +        if (logger.isDebugEnabled()) {
 +          logger.debug("Response message for {} has no bytes in secure part.", this);
 +        }
 +        return;
 +      }
 +      byte[] bytes = ((ConnectionImpl)cnx).getHandShake().decryptBytes(
 +          partBytes);
 +      DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes));
 +      cnx.setConnectionID(dis.readLong());
 +    }
 +  }
 +
 +  /**
 +   * By default just create a normal one part msg.
 +   * Subclasses can override this.
 +   */
 +  protected Message createResponseMessage() {
 +    return new Message(1, Version.CURRENT);
 +  }
 +  
 +  protected Object processResponse(Message m, Connection con) throws Exception {
 +    return processResponse(m);
 +  }
 +  
 +  /**
 +   * Processes the given response message returning the result, if any,
 +   * of the processing.
 +   * @return the result of processing the response; null if no result
 +   * @throws Exception if response could not be processed or
 +   * we received a response with a server exception.
 +   */
 +  protected abstract Object processResponse(Message msg) throws Exception;
 +
 +  /**
 +   * Return true of <code>msgType</code> indicates the operation
 +   * had an error on the server.
 +   */
 +  protected abstract boolean isErrorResponse(int msgType);
 +  /**
 +   * Process a response that contains an ack.
 +   * @param msg the message containing the response
 +   * @param opName text describing this op
 +   * @throws Exception if response could not be processed or
 +   * we received a response with a server exception.
 +   */
 +  protected void processAck(Message msg, String opName)
 +    throws Exception
 +  {
 +    final int msgType = msg.getMessageType();
 +    if (msgType == MessageType.REPLY) {
 +      return;
 +    } else {
 +      Part part = msg.getPart(0);
 +      if (msgType == MessageType.EXCEPTION) {
 +        String s = ": While performing a remote " + opName;
 +        Throwable t = (Throwable) part.getObject();
 +        if (t instanceof PutAllPartialResultException) {
 +          throw (PutAllPartialResultException)t;
 +        } else {
 +          throw new ServerOperationException(s, t);
 +        }
 +        // Get the exception toString part.
 +        // This was added for c++ thin client and not used in java
 +        // Part exceptionToStringPart = msg.getPart(1);
 +      } else if (isErrorResponse(msgType)) {
 +        throw new ServerOperationException(part.getString());
 +      } else {
 +        throw new InternalGemFireError("Unexpected message type "
 +                                       + MessageType.getString(msgType));
 +      }
 +    }
 +  }
 +  /**
 +   * Process a response that contains a single Object result.
 +   * @param msg the message containing the response
 +   * @param opName text describing this op
 +   * @return the result of the response
 +   * @throws Exception if response could not be processed or
 +   * we received a response with a server exception.
 +   */
 +  protected final Object processObjResponse(Message msg, String opName)
 +    throws Exception
 +  {
 +    Part part = msg.getPart(0);
 +    final int msgType = msg.getMessageType();
 +    if (msgType == MessageType.RESPONSE) {
 +      return part.getObject();
 +    } else {
 +      if (msgType == MessageType.EXCEPTION) {
 +        String s = "While performing a remote " + opName;
 +        throw new ServerOperationException(s, (Throwable) part.getObject());
 +        // Get the exception toString part.
 +        // This was added for c++ thin client and not used in java
 +        // Part exceptionToStringPart = msg.getPart(1);
 +      } else if (isErrorResponse(msgType)) {
 +        throw new ServerOperationException(part.getString());
 +      } else {
 +        throw new InternalGemFireError("Unexpected message type "
 +                                       + MessageType.getString(msgType));
 +      }
 +    }
 +  }
 +  /**
 +   * Used by subclasses who get chunked responses.
 +   */
 +  public interface ChunkHandler {
 +    /**
 +     * This method will be called once for every incoming chunk
 +     * @param msg the current chunk to handle
 +     */
 +    public void handle(ChunkedMessage msg) throws Exception;
 +  }
 +  /**
 +   * Process a chunked response that contains a single Object result.
 +   * @param msg the message containing the response
 +   * @param opName text describing this op
 +   * @param callback used to handle each chunks data
 +   * @throws Exception if response could not be processed or
 +   * we received a response with a server exception.
 +   */
 +  protected final void processChunkedResponse(ChunkedMessage msg, String opName, ChunkHandler callback)
 +    throws Exception
 +  {
 +    msg.readHeader();
 +    final int msgType = msg.getMessageType();
 +    if (msgType == MessageType.RESPONSE) {
 +      do {
 +        msg.receiveChunk();
 +        callback.handle(msg);
 +      } while (!msg.isLastChunk());
 +    } else {
 +      if (msgType == MessageType.EXCEPTION) {
 +        msg.receiveChunk();
 +        Part part = msg.getPart(0);
 +        String s = "While performing a remote " + opName;
 +        throw new ServerOperationException(s, (Throwable) part.getObject());
 +        // Get the exception toString part.
 +        // This was added for c++ thin client and not used in java
 +        // Part exceptionToStringPart = msg.getPart(1);
 +      } else if (isErrorResponse(msgType)) {
 +        msg.receiveChunk();
 +        Part part = msg.getPart(0);
 +        throw new ServerOperationException(part.getString());
 +      } else {
 +        throw new InternalGemFireError("Unexpected message type "
 +                                       + MessageType.getString(msgType));
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Set to true if this attempt failed
 +   */
 +  protected boolean failed;
 +  /**
 +   * Set to true if this attempt timed out
 +   */
 +  protected boolean timedOut;
 +
 +  /* (non-Javadoc)
 +   * @see com.gemstone.gemfire.cache.client.internal.Op#attempt(com.gemstone.gemfire.cache.client.internal.Connection)
 +   */
 +  public Object attempt(Connection cnx) throws Exception {
 +    this.failed = true;
 +    this.timedOut = false;
 +    long start = startAttempt(cnx.getStats());
 +    try {
 +      try {
 +        attemptSend(cnx);
 +        this.failed = false;
 +      } finally {
 +        endSendAttempt(cnx.getStats(), start);
 +      }
 +      this.failed = true;
 +      try {
 +        Object result = attemptReadResponse(cnx);
 +        this.failed = false;
 +        return result;
 +      } catch (SocketTimeoutException ste) {
 +        this.failed = false;
 +        this.timedOut = true;
 +        throw ste;
 +      } catch(Exception e) {
 +        throw e;
 +      }
 +    } finally {
 +      endAttempt(cnx.getStats(), start);
 +    }
 +  }
 +  protected final boolean hasFailed() {
 +    return this.failed;
 +  }
 +  protected final boolean hasTimedOut() {
 +    return this.timedOut;
 +  }
 +  protected abstract long startAttempt(ConnectionStats stats);
 +  protected abstract void endSendAttempt(ConnectionStats stats, long start);
 +  protected abstract void endAttempt(ConnectionStats stats, long start);
 +
 +  /**
 +   * New implementations of AbstractOp should override this method to return
 +   * false if the implementation should be excluded from client authentication.
 +   * e.g. PingOp#needsUserId()
 +   * <P/>
 +   * Also, such an operation's <code>MessageType</code> must be added in the
 +   * 'if' condition in {@link ServerConnection#updateAndGetSecurityPart()}
 +   * 
 +   * @return boolean
 +   * @see AbstractOp#sendMessage(Connection)
 +   * @see AbstractOp#processSecureBytes(Connection, Message)
 +   * @see ServerConnection#updateAndGetSecurityPart()
 +   */
 +  protected boolean needsUserId() {
 +    return true;
 +  }
 +  
 +  /**
 +   * Subclasses for AbstractOp should override this method to return
 +   * false in this message should not participate in any existing transaction
 +   * @return true if the message should participate in transaction
 +   */
 +  protected boolean participateInTransaction() {
 +    return true;
 +  }
 +
 +  @Override
 +  public boolean useThreadLocalConnection() {
 +    return true;
 +  }
 +  
 +  public boolean isGatewaySenderOp() {
 +    return false;
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/AddPDXEnumOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/AddPDXEnumOp.java
index b88948d,0000000..ff2bf1c
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/AddPDXEnumOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/AddPDXEnumOp.java
@@@ -1,98 -1,0 +1,98 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +import com.gemstone.gemfire.pdx.internal.EnumInfo;
 +
 +/**
 + * Push a PDX Enum id to other servers.
 + * @author darrel
 + * @since 6.6.2
 + */
 +public class AddPDXEnumOp {
 +  /**
 +   * Register a bunch of instantiators on a server
 +   * using connections from the given pool
 +   * to communicate with the server.
 +   * @param pool the pool to use to communicate with the server.
 +   */
 +  public static void execute(ExecutablePool pool, int id,
 +                             EnumInfo ei)
 +  {
 +    AbstractOp op = new AddPdxEnumOpImpl(id, ei);
 +    pool.execute(op);;
 +  }
 +                                                               
 +  private AddPDXEnumOp() {
 +    // no instances allowed
 +  }
 +  
 +  private static class AddPdxEnumOpImpl extends AbstractOp {
 +    /**
 +     * @throws com.gemstone.gemfire.SerializationException if serialization fails
 +     */
 +    public AddPdxEnumOpImpl(int id, EnumInfo ei) {
 +      super(MessageType.ADD_PDX_ENUM, 2);
 +      getMessage().addObjPart(ei);
 +      getMessage().addIntPart(id);
 +    }
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      processAck(msg, "addPDXEnum");
 +      return null;
 +    }
 +    @Override
 +    protected boolean isErrorResponse(int msgType) {
 +      return false;
 +    }
 +    @Override
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startAddPdxType(); /* use the addPdxType stats instead of adding more stats */
 +    }
 +    @Override
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endAddPdxTypeSend(start, hasFailed());
 +    }
 +    @Override
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endAddPdxType(start, hasTimedOut(), hasFailed());
 +    }
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +    //Don't send the transaction id for this message type.
 +    @Override
 +    protected boolean participateInTransaction() {
 +      return false;
 +    }
 +    
 +    //TODO - no idea what this mumbo jumbo means, but it's on
 +    //most of the other messages like this.
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/AddPDXTypeOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/AddPDXTypeOp.java
index 92dd246,0000000..9fbc674
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/AddPDXTypeOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/AddPDXTypeOp.java
@@@ -1,98 -1,0 +1,96 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +import com.gemstone.gemfire.pdx.internal.PdxType;
 +
 +/**
 + * Add a PdxType to a server.
 + * @author dsmith
 + * @since 6.6
 + */
 +public class AddPDXTypeOp {
 +  /**
 +   * Register a bunch of instantiators on a server
 +   * using connections from the given pool
 +   * to communicate with the server.
 +   * @param pool the pool to use to communicate with the server.
 +   */
 +  public static void execute(ExecutablePool pool, int id,
 +                             PdxType type)
 +  {
 +    AbstractOp op = new AddPDXTypeOpImpl(id, type);
 +    pool.execute(op);
 +  }
 +                                                               
 +  private AddPDXTypeOp() {
 +    // no instances allowed
 +  }
 +  
 +  private static class AddPDXTypeOpImpl extends AbstractOp {
 +    /**
 +     * @throws com.gemstone.gemfire.SerializationException if serialization fails
 +     */
 +    public AddPDXTypeOpImpl(int id, PdxType type) {
 +      super(MessageType.ADD_PDX_TYPE, 2);
 +      getMessage().addObjPart(type);
 +      getMessage().addIntPart(id);
 +    }
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      processAck(msg, "addPDXType");
 +      return null;
 +    }
 +    @Override
 +    protected boolean isErrorResponse(int msgType) {
 +      return false;
 +    }
 +    @Override
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startAddPdxType();
 +    }
 +    @Override
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endAddPdxTypeSend(start, hasFailed());
 +    }
 +    @Override
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endAddPdxType(start, hasTimedOut(), hasFailed());
 +    }
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +    //Don't send the transaction id for this message type.
 +    @Override
 +    protected boolean participateInTransaction() {
 +      return false;
 +    }
 +    
-     //TODO - no idea what this mumbo jumbo means, but it's on
-     //most of the other messages like this.
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/AuthenticateUserOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/AuthenticateUserOp.java
index b0f0cec,0000000..b03c7b9
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/AuthenticateUserOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/AuthenticateUserOp.java
@@@ -1,312 -1,0 +1,312 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import java.io.ByteArrayInputStream;
 +import java.io.DataInputStream;
 +import java.util.Properties;
 +
 +import com.gemstone.gemfire.DataSerializer;
 +import com.gemstone.gemfire.InternalGemFireError;
 +import com.gemstone.gemfire.cache.client.ServerOperationException;
 +import com.gemstone.gemfire.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.DistributedSystem;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.distributed.internal.ServerLocation;
 +import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
 +import com.gemstone.gemfire.internal.HeapDataOutputStream;
 +import com.gemstone.gemfire.internal.Version;
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.ChunkedMessage;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.HandShake;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Part;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.command.PutUserCredentials;
 +import com.gemstone.gemfire.internal.logging.InternalLogWriter;
 +import com.gemstone.gemfire.security.AuthenticationFailedException;
 +import com.gemstone.gemfire.security.AuthenticationRequiredException;
 +import com.gemstone.gemfire.security.NotAuthorizedException;
 +
 +/**
 + * Authenticates this client (or a user) on a server. This op ideally should get
 + * executed once-per-server.
 + * 
 + * When multiuser-authentication is set to false, this op gets executed
 + * immedialtely after a client-to-server connection is established.
 + * 
 + * When multiuser-authentication is set to true, this op gets executed
 + * before the user attempts to perform an op whose
 + * {@link AbstractOp#needsUserId()} returns true.
 + * 
 + * @author ashetkar
 + * @see PutUserCredentials
 + * @see ProxyCache
 + * @since 6.5
 + */
 +public class AuthenticateUserOp {
 +
 +  /**
 +   * Sends the auth credentials to the server. Used in single user mode of
 +   * authentication.
 +   * 
 +   * @param con
 +   *          The connection to use for this operation.
 +   * @param pool
 +   *          The connection pool to use for this operation.
 +   * @return Object unique user-id.
 +   */
 +  public static Object executeOn(Connection con, ExecutablePool pool) {
 +    AbstractOp op = new AuthenticateUserOpImpl(con, pool);
 +    return pool.executeOn(con, op);
 +  }
 +
 +  /**
 +   * Sends the auth credentials to the server for a particular user. Used in
 +   * multiple user mode of authentication.
 +   * 
 +   * @param location
 +   *          The ServerLocation instance whose connection instance will be used
 +   *          to perform the operation.
 +   * @param pool
 +   *          The connection pool to use for this operation.
 +   * @param securityProps
 +   * @return Object unique user-id.
 +   */
 +  public static Object executeOn(ServerLocation location, ExecutablePool pool,
 +      Properties securityProps) {
 +    AbstractOp op = new AuthenticateUserOpImpl(pool, securityProps);
 +    return pool.executeOn(location, op);
 +  }
 +
 +  private AuthenticateUserOp() {
 +    // no instances allowed
 +  }
 +
 +  static class AuthenticateUserOpImpl extends AbstractOp {
 +
 +    private Properties securityProperties = null;
 +    private boolean needsServerLocation = false;
 +
 +    public AuthenticateUserOpImpl(Connection con, ExecutablePool pool) {
 +      super(MessageType.USER_CREDENTIAL_MESSAGE, 1);
 +      byte[] credentialBytes = null;
 +      // TODO this is not a valid way to create a member ID
 +      DistributedMember server = new InternalDistributedMember(con.getSocket()
 +          .getInetAddress(), con.getSocket().getPort(), false);
 +      DistributedSystem sys = InternalDistributedSystem.getConnectedInstance();
 +      String authInitMethod = sys.getProperties().getProperty(
 +          DistributionConfig.SECURITY_CLIENT_AUTH_INIT_NAME);
 +      Properties tmpSecurityProperties = sys.getSecurityProperties();
 +
 +      // LOG: following passes the DS API LogWriters into the security API
 +      Properties credentials = HandShake.getCredentials(authInitMethod,
 +          tmpSecurityProperties, server, false, (InternalLogWriter)sys.getLogWriter(), (InternalLogWriter)sys
 +              .getSecurityLogWriter());
 +      
-       getMessage().setEarlyAck(Message.MESSAGE_HAS_SECURE_PART);
++      getMessage().setMessageHasSecurePartFlag();
 +      HeapDataOutputStream heapdos = new HeapDataOutputStream(Version.CURRENT);
 +      try {
 +        DataSerializer.writeProperties(credentials, heapdos);
 +        credentialBytes = ((ConnectionImpl)con).getHandShake()
 +            .encryptBytes(heapdos.toByteArray());
 +      } catch (Exception e) {
 +        throw new ServerOperationException(e);
 +      } finally {
 +        heapdos.close();
 +      }
 +      getMessage().addBytesPart(credentialBytes);
 +    }
 +
 +    public AuthenticateUserOpImpl(ExecutablePool pool, Properties securityProps) {
 +      this(pool, securityProps, false);
 +    }
 +
 +    public AuthenticateUserOpImpl(ExecutablePool pool, Properties securityProps, boolean needsServer) {
 +      super(MessageType.USER_CREDENTIAL_MESSAGE, 1);
 +      this.securityProperties = securityProps;
 +      this.needsServerLocation = needsServer;
 +
-       getMessage().setEarlyAck(Message.MESSAGE_HAS_SECURE_PART);
++      getMessage().setMessageHasSecurePartFlag();
 +    }
 +
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
 +      HeapDataOutputStream hdos = new HeapDataOutputStream(Version.CURRENT);
 +      byte[] secureBytes = null;
 +      hdos.writeLong(cnx.getConnectionID());
 +      if (this.securityProperties != null) {
 +        byte[] credentialBytes = null;
 +        // TODO this is not a valid way to create a member ID
 +        DistributedMember server = new InternalDistributedMember(cnx
 +            .getSocket().getInetAddress(), cnx.getSocket().getPort(), false);
 +        DistributedSystem sys = InternalDistributedSystem
 +            .getConnectedInstance();
 +        String authInitMethod = sys.getProperties().getProperty(
 +            DistributionConfig.SECURITY_CLIENT_AUTH_INIT_NAME);
 +
 +        Properties credentials = HandShake.getCredentials(authInitMethod,
 +            this.securityProperties, server, false, (InternalLogWriter)sys.getLogWriter(), (InternalLogWriter)sys
 +                .getSecurityLogWriter());
 +        HeapDataOutputStream heapdos = new HeapDataOutputStream(Version.CURRENT);
 +        try {
 +          DataSerializer.writeProperties(credentials, heapdos);
 +          credentialBytes = ((ConnectionImpl)cnx).getHandShake().encryptBytes(
 +              heapdos.toByteArray());
 +        } finally {
 +          heapdos.close();
 +        }
 +        getMessage().addBytesPart(credentialBytes);
 +      }
 +      try {
 +        secureBytes = ((ConnectionImpl)cnx).getHandShake().encryptBytes(
 +            hdos.toByteArray());
 +      } finally {
 +        hdos.close();
 +      }
 +      getMessage().setSecurePart(secureBytes);
 +      getMessage().send(false);
 +    }
 +
 +    @Override
 +    public Object attempt(Connection cnx) throws Exception {
 +      if (cnx.getServer().getRequiresCredentials()) {
 +        return super.attempt(cnx);
 +      } else {
 +        return null;
 +      }
 +    }
 +
 +    @Override
 +    protected Object attemptReadResponse(Connection cnx) throws Exception {
 +      Message msg = createResponseMessage();
 +      if (msg != null) {
 +        msg.setComms(cnx.getSocket(), cnx.getInputStream(),
 +            cnx.getOutputStream(), cnx.getCommBuffer(), cnx.getStats());
 +        if (msg instanceof ChunkedMessage) {
 +          try {
 +            return processResponse(cnx, msg);
 +          } finally {
 +            msg.unsetComms();
 +            processSecureBytes(cnx, msg);
 +          }
 +        } else {
 +          try {
 +            msg.recv();
 +          } finally {
 +            msg.unsetComms();
 +            processSecureBytes(cnx, msg);
 +          }
 +          return processResponse(cnx, msg);
 +        }
 +      } else {
 +        return null;
 +      }
 +    }
 +
 +    protected Object processResponse(Connection cnx, Message msg) throws Exception {
 +      byte[] bytes = null;
 +      Part part = msg.getPart(0);
 +      final int msgType = msg.getMessageType();
 +      long userId = -1;
 +      if (msgType == MessageType.RESPONSE) {
 +        bytes = (byte[])part.getObject();
 +        if (bytes.length == 0) {
 +          cnx.getServer().setRequiresCredentials(false);
 +        } else {
 +          cnx.getServer().setRequiresCredentials(true);
 +          byte[] decrypted = ((ConnectionImpl)cnx).getHandShake().decryptBytes(bytes);
 +          DataInputStream dis = new DataInputStream(new ByteArrayInputStream(decrypted));
 +          userId = dis.readLong();
 +        }
 +        if (this.needsServerLocation) {
 +          return new Object[] {cnx.getServer(), userId};
 +       } else {
 +         return userId;
 +       }
 +      }
 +      else if (msgType == MessageType.EXCEPTION) {
 +        Object result = part.getObject();
 +        String s = "While performing a remote authenticate";
 +        if (result instanceof AuthenticationFailedException) {
 +          final AuthenticationFailedException afe =
 +            (AuthenticationFailedException)result;
 +          if ("REPLY_REFUSED".equals(afe.getMessage())) {
 +            throw new AuthenticationFailedException(s, afe.getCause());
 +          }
 +          else {
 +            throw new AuthenticationFailedException(s, afe);
 +          }
 +        }
 +        else if (result instanceof AuthenticationRequiredException) {
 +          throw new AuthenticationRequiredException(s,
 +              (AuthenticationRequiredException)result);
 +        }
 +        else if (result instanceof NotAuthorizedException) {
 +          throw new NotAuthorizedException(s, (NotAuthorizedException)result);
 +        }
 +        else {
 +          throw new ServerOperationException(s, (Throwable)result);
 +        }
 +        // Get the exception toString part.
 +        // This was added for c++ thin client and not used in java
 +        // Part exceptionToStringPart = msg.getPart(1);
 +      }
 +      else if (isErrorResponse(msgType)) {
 +        throw new ServerOperationException(part.getString());
 +      }
 +      else {
 +        throw new InternalGemFireError("Unexpected message type "
 +            + MessageType.getString(msgType));
 +      }
 +    }
 +
 +    @Override
 +    protected boolean isErrorResponse(int msgType) {
 +      return msgType == MessageType.REQUESTDATAERROR;
 +    }
 +
 +    @Override
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startGet();
 +    }
 +
 +    @Override
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endGetSend(start, hasFailed());
 +    }
 +
 +    @Override
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endGet(start, hasTimedOut(), hasFailed());
 +    }
 +
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      return null;
 +    }
 +
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +  }
 +
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/CloseConnectionOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/CloseConnectionOp.java
index 943f0a3,0000000..7de38fe
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/CloseConnectionOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/CloseConnectionOp.java
@@@ -1,95 -1,0 +1,95 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +
 +/**
 + * Tell a server that a connection is being closed
 + * @author darrel
 + * @since 5.7
 + */
 +public class CloseConnectionOp {
 +  /**
 +   * Tell a server that a connection is being closed
 +   * @param con the connection that is being closed
 +   * @param keepAlive whether to keep the proxy alive on the server
 +   */
 +  public static void execute(Connection con, boolean keepAlive)
 +    throws Exception
 +  {
 +    AbstractOp op = new CloseConnectionOpImpl(keepAlive);
 +    con.execute(op);
 +  }
 +                                                               
 +  private CloseConnectionOp() {
 +    // no instances allowed
 +  }
 +  
 +  private static class CloseConnectionOpImpl extends AbstractOp {
 +    /**
 +     * @throws com.gemstone.gemfire.SerializationException if serialization fails
 +     */
 +    public CloseConnectionOpImpl(boolean keepAlive)  {
 +      super(MessageType.CLOSE_CONNECTION, 1);
 +      getMessage().addRawPart(new byte[]{(byte)(keepAlive?1:0)}, false);
 +    }
 +    @Override  
 +    protected Message createResponseMessage() {
 +      // no response is sent
 +      return null;
 +    }
 +
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }
 +
 +    @Override  
 +    protected Object processResponse(Message msg) throws Exception {
 +      throw new IllegalStateException("should never be called");
 +    }
 +    @Override  
 +    protected boolean isErrorResponse(int msgType) {
 +      return false;
 +    }
 +    @Override  
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startCloseCon();
 +    }
 +    @Override  
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endCloseConSend(start, hasFailed());
 +    }
 +    @Override  
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endCloseCon(start, hasTimedOut(), hasFailed());
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/CommitOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/CommitOp.java
index e04a466,0000000..c9c6dd7
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/CommitOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/CommitOp.java
@@@ -1,109 -1,0 +1,109 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import com.gemstone.gemfire.internal.cache.TXCommitMessage;
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +
 +/**
 + * Does a commit on a server
 + * @author gregp
 + * @since 6.6
 + */
 +public class CommitOp {
 +  /**
 +   * Does a commit on a server using connections from the given pool
 +   * to communicate with the server.
 +   * @param pool the pool to use to communicate with the server.
 +   */
 +  public static TXCommitMessage execute(ExecutablePool pool,int txId)
 +  {
 +    CommitOpImpl op = new CommitOpImpl(txId);
 +    pool.execute(op);
 +    return op.getTXCommitMessageResponse();
 +  }
 +                                                               
 +  private CommitOp() {
 +    // no instances allowed
 +  }
 +  
 +    
 +  private static class CommitOpImpl extends AbstractOp {
 +    private int txId;
 +    
 +    private TXCommitMessage tXCommitMessageResponse = null;
 +    /**
 +     * @throws com.gemstone.gemfire.SerializationException if serialization fails
 +     */
 +    public CommitOpImpl(int txId) {
 +      super(MessageType.COMMIT, 1);
 +      getMessage().setTransactionId(txId);
 +      this.txId = txId;
 +    }
 +
 +    public TXCommitMessage getTXCommitMessageResponse() {
 +      return tXCommitMessageResponse;
 +    }
 +    
 +    @Override
 +    public String toString() {
 +      return "TXCommit(txId="+this.txId+")";
 +    }
 +
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      TXCommitMessage rcs = (TXCommitMessage)processObjResponse(msg, "commit");
 +      assert rcs != null : "TxCommit response was null";
 +      this.tXCommitMessageResponse = rcs;
 +      return rcs;
 +    }
 +     
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }    
 +    
 +    @Override  
 +    protected boolean isErrorResponse(int msgType) {
 +      return msgType == MessageType.EXCEPTION;
 +    }
 +    @Override  
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startCommit();
 +    }
 +    @Override  
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endCommitSend(start, hasFailed());
 +    }
 +    @Override  
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endCommit(start, hasTimedOut(), hasFailed());
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetClientPRMetaDataOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetClientPRMetaDataOp.java
index f88d1e9,0000000..8bae6ff
mode 100755,000000..100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetClientPRMetaDataOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetClientPRMetaDataOp.java
@@@ -1,171 -1,0 +1,171 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import java.util.List;
 +import java.util.Set;
 +
 +import org.apache.logging.log4j.Logger;
 +
 +import com.gemstone.gemfire.InternalGemFireError;
 +import com.gemstone.gemfire.cache.client.ServerOperationException;
 +import com.gemstone.gemfire.internal.cache.BucketServerLocation66;
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Part;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.logging.LogService;
 +
 +/**
 + * Retrieves {@link ClientPartitionAdvisor} for the specified PartitionedRegion from
 + * one of the servers
 + * 
 + * @author Suranjan Kumar
 + * @author Yogesh Mahajan
 + * 
 + * @since 6.5
 + */
 +public class GetClientPRMetaDataOp {
 +
 +  private static final Logger logger = LogService.getLogger();
 +  
 +  private GetClientPRMetaDataOp() {
 +    // no instances allowed
 +  }
 +
 +  public static void execute(ExecutablePool pool, String regionFullPath,
 +      ClientMetadataService cms) {
 +    AbstractOp op = new GetClientPRMetaDataOpImpl(regionFullPath, cms);
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("GetClientPRMetaDataOp#execute : Sending GetClientPRMetaDataOp Message: {} to server using pool: {}", op.getMessage(), pool);
 +    }
 +    pool.execute(op);
 +  }
 +
 +  static class GetClientPRMetaDataOpImpl extends AbstractOp {
 +
 +    String regionFullPath = null;
 +
 +    ClientMetadataService cms = null;
 +
 +    public GetClientPRMetaDataOpImpl(String regionFullPath, ClientMetadataService cms) {
 +      super(MessageType.GET_CLIENT_PR_METADATA, 1);
 +      this.regionFullPath = regionFullPath;
 +      this.cms = cms;
 +      getMessage().addStringPart(regionFullPath);
 +    }
 +
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }
 +
 +    @SuppressWarnings("unchecked")
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      switch (msg.getMessageType()) {
 +        case MessageType.GET_CLIENT_PR_METADATA_ERROR:
 +          String errorMsg = msg.getPart(0).getString();
 +          if (logger.isDebugEnabled()) {
 +            logger.debug(errorMsg);
 +          }
 +          throw new ServerOperationException(errorMsg);
 +        case MessageType.RESPONSE_CLIENT_PR_METADATA:
 +          final boolean isDebugEnabled = logger.isDebugEnabled();
 +          if (isDebugEnabled) {
 +            logger.debug("GetClientPRMetaDataOpImpl#processResponse: received message of type : {}" + MessageType.getString(msg.getMessageType()));
 +          }
 +          int numParts = msg.getNumberOfParts();
 +          ClientPartitionAdvisor advisor = cms
 +              .getClientPartitionAdvisor(regionFullPath);
 +          for (int i = 0; i < numParts; i++) {
 +            Object result = msg.getPart(i).getObject();
 +            List<BucketServerLocation66> locations = (List<BucketServerLocation66>)result;
 +          if (!locations.isEmpty()) {
 +            int bucketId = locations.get(0).getBucketId();
 +            if (isDebugEnabled) {
 +              logger.debug("GetClientPRMetaDataOpImpl#processResponse: for bucketId : {} locations are {}", bucketId, locations);
 +            }
 +            advisor.updateBucketServerLocations(bucketId, locations, cms);
 +            
 +            Set<ClientPartitionAdvisor> cpas = cms
 +                .getColocatedClientPartitionAdvisor(regionFullPath);
 +            if (cpas != null && !cpas.isEmpty()) {
 +              for (ClientPartitionAdvisor colCPA : cpas) {
 +                colCPA.updateBucketServerLocations(bucketId, locations, cms);
 +              }
 +            }
 +          }
 +          }
 +          if (isDebugEnabled) {
 +            logger.debug("GetClientPRMetaDataOpImpl#processResponse: received ClientPRMetadata from server successfully.");
 +          }
 +          cms.setMetadataStable(true);
 +          return null;
 +        case MessageType.EXCEPTION:
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("GetClientPRMetaDataOpImpl#processResponse: received message of type EXCEPTION");
 +          }
 +          Part part = msg.getPart(0);
 +          Object obj = part.getObject();
 +          String s = "While performing  GetClientPRMetaDataOp "
 +              + ((Throwable)obj).getMessage();
 +          throw new ServerOperationException(s, (Throwable)obj);
 +        default:
 +          throw new InternalGemFireError(
 +              LocalizedStrings.Op_UNKNOWN_MESSAGE_TYPE_0
 +                  .toLocalizedString(Integer.valueOf(msg.getMessageType())));
 +      }
 +    }
 +
 +    @Override
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startGetClientPRMetadata();
 +    }
 +
 +    protected String getOpName() {
 +      return "GetClientPRMetaDataOp";
 +    }
 +
 +    @Override
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endGetClientPRMetadataSend(start, hasFailed());
 +    }
 +
 +    @Override
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endGetClientPRMetadata(start, hasTimedOut(), hasFailed());
 +    }
 +
 +    @Override
 +    protected boolean isErrorResponse(int msgType) {
 +      return false;
 +    }
 +  }
 +
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetClientPartitionAttributesOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetClientPartitionAttributesOp.java
index 004872c,0000000..e1a8870
mode 100755,000000..100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetClientPartitionAttributesOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetClientPartitionAttributesOp.java
@@@ -1,177 -1,0 +1,177 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import java.util.Set;
 +
 +import org.apache.logging.log4j.Logger;
 +
 +import com.gemstone.gemfire.InternalGemFireError;
 +import com.gemstone.gemfire.cache.FixedPartitionAttributes;
 +import com.gemstone.gemfire.cache.client.ServerOperationException;
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Part;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.logging.LogService;
 +
 +/**
 + * 
 + * Retrieves {@link ClientPartitionAdvisor} related information for the
 + * specified PartitionedRegion from one of the servers
 + * 
 + * @author Suranjan Kumar
 + * @author Yogesh Mahajan
 + * 
 + * @since 6.5
 + * 
 + */
 +public class GetClientPartitionAttributesOp {
 +
 +  private static final Logger logger = LogService.getLogger();
 +  
 +  private GetClientPartitionAttributesOp() {
 +    // no instances allowed
 +  }
 +
 +  @SuppressWarnings("unchecked")
 +  public static ClientPartitionAdvisor execute(ExecutablePool pool, String regionFullPath) {
 +    AbstractOp op = new GetClientPartitionAttributesOpImpl(regionFullPath);
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("GetClientPartitionAttributesOp#execute : Sending GetClientPartitionAttributesOp Message: {} for region: {} to server using pool: {}", op.getMessage(), regionFullPath, pool);
 +    }
 +    
 +    ClientPartitionAdvisor advisor = (ClientPartitionAdvisor)pool.execute(op);
 +
 +    if (advisor != null) {
 +      advisor.setServerGroup(((PoolImpl)pool).getServerGroup());
 +    }
 +    
 +    return advisor;
 +  }
 +
 +  static class GetClientPartitionAttributesOpImpl extends AbstractOp {
 +
 +    String regionFullPath = null;
 +
 +    public GetClientPartitionAttributesOpImpl(String regionFullPath) {
 +      super(MessageType.GET_CLIENT_PARTITION_ATTRIBUTES, 1);
 +      this.regionFullPath = regionFullPath;
 +      getMessage().addStringPart(regionFullPath);
 +    }
 +
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }
 +
 +    @SuppressWarnings("unchecked")
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      switch (msg.getMessageType()) {
 +        case MessageType.GET_CLIENT_PARTITION_ATTRIBUTES_ERROR:
 +          String errorMsg = msg.getPart(0).getString();
 +          if (logger.isDebugEnabled()) {
 +            logger.debug(errorMsg);
 +          }
 +          throw new ServerOperationException(errorMsg);
 +        case MessageType.RESPONSE_CLIENT_PARTITION_ATTRIBUTES:
 +          final boolean isDebugEnabled = logger.isDebugEnabled();
 +          if (isDebugEnabled) {
 +            logger.debug("GetClientPartitionAttributesOpImpl#processResponse: received message of type : {}", MessageType.getString(msg.getMessageType()));
 +          }
 +          int bucketCount;
 +          String colocatedWith;
 +          String partitionResolverName = null;
 +          Set<FixedPartitionAttributes> fpaSet = null; 
 +          bucketCount = (Integer)msg.getPart(0).getObject();
 +          colocatedWith = (String)msg.getPart(1).getObject();
 +          if (msg.getNumberOfParts() == 4) {
 +            partitionResolverName = (String)msg.getPart(2).getObject();
 +            fpaSet = (Set<FixedPartitionAttributes>)msg.getPart(3).getObject();
 +          }
 +          else if (msg.getNumberOfParts() == 3) {
 +            Object obj = msg.getPart(2).getObject();
 +            if(obj instanceof String){
 +              partitionResolverName = (String)obj;
 +            }else{
 +              fpaSet = (Set<FixedPartitionAttributes>)obj;
 +            }
 +          }
 +          else if(bucketCount==-1){              
 +              return null;
 +          }
 +          if (isDebugEnabled) {
 +            logger.debug("GetClientPartitionAttributesOpImpl#processResponse: received all the results from server successfully.");
 +          }
 +          ClientPartitionAdvisor advisor = new ClientPartitionAdvisor(bucketCount, colocatedWith,
 +                partitionResolverName, fpaSet);
 +          return advisor;
 +
 +        case MessageType.EXCEPTION:
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("GetClientPartitionAttributesOpImpl#processResponse: received message of type EXCEPTION");
 +          }
 +          Part part = msg.getPart(0);
 +          Object obj = part.getObject();
 +          String s = "While performing  GetClientPartitionAttributesOp "+  ((Throwable)obj).getMessage();
 +          throw new ServerOperationException(s, (Throwable) obj);
 +        default:
 +          throw new InternalGemFireError(
 +              LocalizedStrings.Op_UNKNOWN_MESSAGE_TYPE_0
 +                  .toLocalizedString(Integer.valueOf(msg.getMessageType())));
 +      }
 +    }
 +
 +    @Override
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startGetClientPartitionAttributes();
 +    }
 +
 +    protected String getOpName() {
 +      return "GetClientPartitionAttributesOp";
 +    }
 +
 +    @Override
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endGetClientPartitionAttributesSend(start, hasFailed());
 +    }
 +
 +    @Override
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endGetClientPartitionAttributes(start, hasTimedOut(), hasFailed());
 +    }
 +
 +    @Override
 +    protected boolean isErrorResponse(int msgType) {
 +      return false;
 +    }
 +
 +  }
 +
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetEventValueOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetEventValueOp.java
index 7dfe9af,0000000..1038ede
mode 100755,000000..100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetEventValueOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetEventValueOp.java
@@@ -1,121 -1,0 +1,121 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import com.gemstone.gemfire.InternalGemFireError;
 +import com.gemstone.gemfire.cache.client.ServerOperationException;
 +import com.gemstone.gemfire.internal.cache.EventID;
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Part;
 +
 +/**
 + * Gets (full) value (unlike GetOp, which may get either a full value or a delta
 + * depending upon delta flag) of a given event from the ha container on server.
 + * 
 + * @since 6.1
 + */
 +public class GetEventValueOp {
 +  /**
 +   * Does a get on the primary server using connections from the given pool
 +   * @param pool the pool to use to communicate with the server.
 +   * @param event the eventid to do the get on
 +   * @param callbackArg an optional callback arg to pass to any cache callbacks
 +   * @return the entry value found by the get if any
 +   */
 +  public static Object executeOnPrimary(ExecutablePool pool, EventID event,
 +      Object callbackArg) {
 +    AbstractOp op = new GetEventValueOpImpl(event, callbackArg);
 +    return pool.executeOnPrimary(op);
 +  }
 +
 +
 +  private GetEventValueOp() {
 +    // no instances allowed
 +  }
 +
 +  static class GetEventValueOpImpl extends AbstractOp {
 +    /**
 +     * @throws com.gemstone.gemfire.SerializationException if serialization fails
 +     */
 +    public GetEventValueOpImpl(EventID event, Object callbackArg) {
 +      super(MessageType.REQUEST_EVENT_VALUE, callbackArg != null ? 2 : 1);
 +      getMessage().addObjPart(event);
 +      if (callbackArg != null) {
 +        getMessage().addObjPart(callbackArg);
 +      }
 +    }
 +
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }
 +
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      Part part = msg.getPart(0);
 +      final int msgType = msg.getMessageType();
 +      if (msgType == MessageType.RESPONSE) {
 +        return part;
 +      } else {
 +        if (msgType == MessageType.REQUEST_EVENT_VALUE_ERROR) {
 +          // Value not found in haContainer.
 +          return null;
 +        }
 +        else if (msgType == MessageType.EXCEPTION) {
 +          String s = "While performing a remote " + "getFullValue";
 +          throw new ServerOperationException(s, (Throwable) part.getObject());
 +          // Get the exception toString part.
 +          // This was added for c++ thin client and not used in java
 +          // Part exceptionToStringPart = msg.getPart(1);
 +        } else if (isErrorResponse(msgType)) {
 +          throw new ServerOperationException(part.getString());
 +        } else {
 +          throw new InternalGemFireError("Unexpected message type "
 +                                         + MessageType.getString(msgType));
 +        }
 +      }
 +    }
 +
 +    protected boolean isErrorResponse(int msgType) {
 +      return msgType == MessageType.REQUESTDATAERROR;
 +    }
 +
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startGet();
 +    }
 +
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endGetSend(start, hasFailed());
 +    }
 +
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endGet(start, hasTimedOut(), hasFailed());
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetFunctionAttributeOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetFunctionAttributeOp.java
index 9e63fba,0000000..177ea26
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetFunctionAttributeOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetFunctionAttributeOp.java
@@@ -1,84 -1,0 +1,84 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +
 +public class GetFunctionAttributeOp {
 +
 +  public static Object execute(ExecutablePool pool, String functionId) {
 +    AbstractOp op = new GetFunctionAttributeOpImpl(functionId);
 +    return pool.execute(op);
 +  }
 +
 +  private GetFunctionAttributeOp() {
 +    // no instances allowed
 +  }
 +
 +  static class GetFunctionAttributeOpImpl extends AbstractOp {
 +
 +    private String functionId = null;
 +
 +    public GetFunctionAttributeOpImpl(String functionId) {
 +      super(MessageType.GET_FUNCTION_ATTRIBUTES, 1);
 +      this.functionId = functionId;
 +      getMessage().addStringPart(this.functionId);
 +    }
 +
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      return processObjResponse(msg, "getFunctionAttribute");
 +    }
 +
 +    @Override
 +    protected boolean isErrorResponse(int msgType) {
 +      return msgType == MessageType.REQUESTDATAERROR;
 +    }
 +
 +    @Override
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startGet();
 +    }
 +
 +    @Override
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endGetSend(start, hasFailed());
 +    }
 +
 +    @Override
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endGet(start, hasTimedOut(), hasFailed());
 +    }
 +
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXEnumByIdOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXEnumByIdOp.java
index d3e9efb,0000000..1e22d81
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXEnumByIdOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXEnumByIdOp.java
@@@ -1,94 -1,0 +1,92 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +import com.gemstone.gemfire.pdx.internal.EnumInfo;
 +
 +/**
 + * Retrieve the PDXType, given an integer PDX id, from a server.
 + * @author darrel
 + * @since 6.6.2
 + */
 +public class GetPDXEnumByIdOp {
 +  /**
 +   * Get a enum from the given pool.
 +   * @param pool the pool to use to communicate with the server.
 +   */
 +  public static EnumInfo execute(ExecutablePool pool,
 +                             int enumId)
 +  {
 +    AbstractOp op = new GetPDXEnumByIdOpImpl(enumId);
 +    return (EnumInfo) pool.execute(op);
 +  }
 +                                                               
 +  private GetPDXEnumByIdOp() {
 +    // no instances allowed
 +  }
 +  
 +  private static class GetPDXEnumByIdOpImpl extends AbstractOp {
 +    /**
 +     * @throws com.gemstone.gemfire.SerializationException if serialization fails
 +     */
 +    public GetPDXEnumByIdOpImpl(int enumId) {
 +      super(MessageType.GET_PDX_ENUM_BY_ID, 1);
 +      getMessage().addIntPart(enumId);
 +    }
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      return processObjResponse(msg, "getPDXEnumById");
 +    }
 +    @Override
 +    protected boolean isErrorResponse(int msgType) {
 +      return false;
 +    }
 +    @Override
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startGetPDXTypeById(); // reuse PDXType stats instead of adding new enum ones
 +    }
 +    @Override
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endGetPDXTypeByIdSend(start, hasFailed());
 +    }
 +    @Override
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endGetPDXTypeById(start, hasTimedOut(), hasFailed());
 +    }
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +    //Don't send the transaction id for this message type.
 +    @Override
 +    protected boolean participateInTransaction() {
 +      return false;
 +    }
 +    
-     //TODO - no idea what this mumbo jumbo means, but it's on
-     //most of the other messages like this.
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXEnumsOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXEnumsOp.java
index 0cb5d33,0000000..0590caf
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXEnumsOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXEnumsOp.java
@@@ -1,112 -1,0 +1,112 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import java.util.Map;
 +
 +import com.gemstone.gemfire.InternalGemFireError;
 +import com.gemstone.gemfire.cache.client.ServerOperationException;
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Part;
 +import com.gemstone.gemfire.pdx.internal.EnumInfo;
 +
 +/**
 + * Retrieve all known PDX types.
 + * 
 + * @author bakera
 + * @since 7.0
 + */
 +public class GetPDXEnumsOp {
 +
 +  public static Map<Integer, EnumInfo> execute(ExecutablePool pool) {
 +    AbstractOp op = new GetPDXEnumsOpImpl();
 +    return (Map<Integer, EnumInfo>) pool.execute(op);
 +  }
 +                                                               
 +  private GetPDXEnumsOp() {
 +    // no instances allowed
 +  }
 +  
 +  private static class GetPDXEnumsOpImpl extends AbstractOp {
 +    public GetPDXEnumsOpImpl() {
 +      super(MessageType.GET_PDX_ENUMS, 1);
 +      getMessage().addIntPart(0); // must have at least one part
 +    }
 +
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      Part part = msg.getPart(0);
 +      int msgType = msg.getMessageType();
 +      if (msgType == MessageType.RESPONSE) {
 +        return (Map<Integer, EnumInfo>) part.getObject();
 +
 +      } else {
 +        if (msgType == MessageType.EXCEPTION) {
 +          String s = "While performing a remote " + "getPdxEnums";
 +          throw new ServerOperationException(s, (Throwable) part.getObject());
 +
 +        } else if (isErrorResponse(msgType)) {
 +          throw new ServerOperationException(part.getString());
 +
 +        } else {
 +          throw new InternalGemFireError("Unexpected message type "
 +              + MessageType.getString(msgType));
 +        }
 +      }
 +    }
 +
 +    @Override
 +    protected boolean isErrorResponse(int msgType) {
 +      return false;
 +    }
 +
 +    @Override
 +    protected long startAttempt(ConnectionStats stats) {
 +      return 0;
 +    }
 +
 +    @Override
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +    }
 +
 +    @Override
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +    }
 +    
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +
 +    @Override
 +    protected boolean participateInTransaction() {
 +      return false;
 +    }
 +
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXIdForEnumOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXIdForEnumOp.java
index 81a2b1b,0000000..bac2e80
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXIdForEnumOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXIdForEnumOp.java
@@@ -1,115 -1,0 +1,113 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import com.gemstone.gemfire.InternalGemFireError;
 +import com.gemstone.gemfire.cache.client.ServerOperationException;
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Part;
 +import com.gemstone.gemfire.pdx.internal.EnumInfo;
 +
 +/**
 + * Retrieve the PDXType, given an integer PDX id, from a server.
 + * @author darrel
 + * @since 6.6.2
 + */
 +public class GetPDXIdForEnumOp {
 +  /**
 +   * Register a bunch of instantiators on a server
 +   * using connections from the given pool
 +   * to communicate with the server.
 +   * @param pool the pool to use to communicate with the server.
 +   */
 +  public static int execute(ExecutablePool pool,
 +                             EnumInfo ei)
 +  {
 +    AbstractOp op = new GetPDXIdForEnumOpImpl(ei);
 +    return ((Integer) pool.execute(op)).intValue();
 +  }
 +                                                               
 +  private GetPDXIdForEnumOp() {
 +    // no instances allowed
 +  }
 +  
 +  private static class GetPDXIdForEnumOpImpl extends AbstractOp {
 +    /**
 +     * @throws com.gemstone.gemfire.SerializationException if serialization fails
 +     */
 +    public GetPDXIdForEnumOpImpl(EnumInfo ei) {
 +      super(MessageType.GET_PDX_ID_FOR_ENUM, 1);
 +      getMessage().addObjPart(ei);
 +    }
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      Part part = msg.getPart(0);
 +      final int msgType = msg.getMessageType();
 +      if (msgType == MessageType.RESPONSE) {
 +        return Integer.valueOf(part.getInt());
 +      } else {
 +        if (msgType == MessageType.EXCEPTION) {
 +          String s = "While performing a remote " + "getPdxIdForEnum";
 +          throw new ServerOperationException(s, (Throwable) part.getObject());
 +          // Get the exception toString part.
 +          // This was added for c++ thin client and not used in java
 +          // Part exceptionToStringPart = msg.getPart(1);
 +        } else if (isErrorResponse(msgType)) {
 +          throw new ServerOperationException(part.getString());
 +        } else {
 +          throw new InternalGemFireError("Unexpected message type "
 +                                         + MessageType.getString(msgType));
 +        }
 +      }
 +    }
 +    @Override
 +    protected boolean isErrorResponse(int msgType) {
 +      return false;
 +    }
 +    @Override
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startGetPDXTypeById();
 +    }
 +    @Override
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endGetPDXTypeByIdSend(start, hasFailed()); /* reusing type stats instead of adding enum ones */
 +    }
 +    @Override
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endGetPDXTypeById(start, hasTimedOut(), hasFailed());
 +    }
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +    //Don't send the transaction id for this message type.
 +    @Override
 +    protected boolean participateInTransaction() {
 +      return false;
 +    }
-     //TODO - no idea what this mumbo jumbo means, but it's on
-     //most of the other messages like this.
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXIdForTypeOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXIdForTypeOp.java
index d771cb6,0000000..1b71f71
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXIdForTypeOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXIdForTypeOp.java
@@@ -1,115 -1,0 +1,113 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import com.gemstone.gemfire.InternalGemFireError;
 +import com.gemstone.gemfire.cache.client.ServerOperationException;
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Part;
 +import com.gemstone.gemfire.pdx.internal.PdxType;
 +
 +/**
 + * Retrieve the PDXType, given an integer PDX id, from a server.
 + * @author dsmith
 + * @since 6.6
 + */
 +public class GetPDXIdForTypeOp {
 +  /**
 +   * Register a bunch of instantiators on a server
 +   * using connections from the given pool
 +   * to communicate with the server.
 +   * @param pool the pool to use to communicate with the server.
 +   */
 +  public static int execute(ExecutablePool pool,
 +                             PdxType type)
 +  {
 +    AbstractOp op = new GetPDXIdForTypeOpImpl(type);
 +    return ((Integer) pool.execute(op)).intValue();
 +  }
 +                                                               
 +  private GetPDXIdForTypeOp() {
 +    // no instances allowed
 +  }
 +  
 +  private static class GetPDXIdForTypeOpImpl extends AbstractOp {
 +    /**
 +     * @throws com.gemstone.gemfire.SerializationException if serialization fails
 +     */
 +    public GetPDXIdForTypeOpImpl(PdxType type) {
 +      super(MessageType.GET_PDX_ID_FOR_TYPE, 1);
 +      getMessage().addObjPart(type);
 +    }
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      Part part = msg.getPart(0);
 +      final int msgType = msg.getMessageType();
 +      if (msgType == MessageType.RESPONSE) {
 +        return Integer.valueOf(part.getInt());
 +      } else {
 +        if (msgType == MessageType.EXCEPTION) {
 +          String s = "While performing a remote " + "getPdxIdForType";
 +          throw new ServerOperationException(s, (Throwable) part.getObject());
 +          // Get the exception toString part.
 +          // This was added for c++ thin client and not used in java
 +          // Part exceptionToStringPart = msg.getPart(1);
 +        } else if (isErrorResponse(msgType)) {
 +          throw new ServerOperationException(part.getString());
 +        } else {
 +          throw new InternalGemFireError("Unexpected message type "
 +                                         + MessageType.getString(msgType));
 +        }
 +      }
 +    }
 +    @Override
 +    protected boolean isErrorResponse(int msgType) {
 +      return false;
 +    }
 +    @Override
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startGetPDXTypeById();
 +    }
 +    @Override
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endGetPDXTypeByIdSend(start, hasFailed());
 +    }
 +    @Override
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endGetPDXTypeById(start, hasTimedOut(), hasFailed());
 +    }
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +    //Don't send the transaction id for this message type.
 +    @Override
 +    protected boolean participateInTransaction() {
 +      return false;
 +    }
-     //TODO - no idea what this mumbo jumbo means, but it's on
-     //most of the other messages like this.
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }
 +  }
 +}



[12/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache/query/internal/index/ConcurrentIndexInitOnOverflowRegionDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache/query/internal/index/ConcurrentIndexInitOnOverflowRegionDUnitTest.java
index 5528299,0000000..ca90f00
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache/query/internal/index/ConcurrentIndexInitOnOverflowRegionDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache/query/internal/index/ConcurrentIndexInitOnOverflowRegionDUnitTest.java
@@@ -1,468 -1,0 +1,467 @@@
 +/*
 + * 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.cache.query.internal.index;
 +
 +import java.io.IOException;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.DiskStore;
 +import com.gemstone.gemfire.cache.EvictionAction;
 +import com.gemstone.gemfire.cache.EvictionAlgorithm;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionFactory;
 +import com.gemstone.gemfire.cache.RegionShortcut;
 +import com.gemstone.gemfire.cache.client.ClientCache;
 +import com.gemstone.gemfire.cache.client.ClientCacheFactory;
 +import com.gemstone.gemfire.cache.client.ClientRegionShortcut;
 +import com.gemstone.gemfire.cache.query.Index;
 +import com.gemstone.gemfire.cache.query.QueryService;
 +import com.gemstone.gemfire.cache.query.data.Portfolio;
 +import com.gemstone.gemfire.cache.query.data.PortfolioData;
 +import com.gemstone.gemfire.cache.query.internal.index.IndexManager.TestHook;
 +import com.gemstone.gemfire.cache.query.partitioned.PRQueryDUnitHelper;
 +import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.cache30.CacheSerializableRunnable;
 +import com.gemstone.gemfire.cache30.CacheTestCase;
 +import com.gemstone.gemfire.internal.cache.EvictionAttributesImpl;
 +import com.gemstone.gemfire.test.dunit.AsyncInvocation;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.IgnoredException;
 +import com.gemstone.gemfire.test.dunit.LogWriterUtils;
 +import com.gemstone.gemfire.test.dunit.NetworkUtils;
 +import com.gemstone.gemfire.test.dunit.ThreadUtils;
 +import com.gemstone.gemfire.test.dunit.VM;
 +import com.gemstone.gemfire.test.dunit.Wait;
 +
 +/**
 + * @author shobhit
 + * 
 + */
 +public class ConcurrentIndexInitOnOverflowRegionDUnitTest extends CacheTestCase {
 +
 +  PRQueryDUnitHelper PRQHelp = new PRQueryDUnitHelper("");
 +
 +  String name;
 +
 +  final int redundancy = 0;
 +
 +  private int cnt = 0;
 +
 +  private int cntDest = 1;
 +
 +  public static volatile boolean hooked = false;
 +
 +  private static int bridgeServerPort;
 +  
 +  /**
 +   * @param name
 +   */
 +  public ConcurrentIndexInitOnOverflowRegionDUnitTest(String name) {
 +    super(name);
 +  }
 +
 +  /**
 +  *
 +  */
 +  public void testAsyncIndexInitDuringEntryDestroyAndQueryOnRR() {
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +
 +    name = "PartionedPortfoliosPR";
 +    // Create Overflow Persistent Partition Region
 +    vm0.invoke(new CacheSerializableRunnable(
 +        "Create local region with synchronous index maintenance") {
 +      @Override
 +      public void run2() throws CacheException {
 +        Cache cache = PRQHelp.getCache();
 +        Region partitionRegion = null;
 +        IndexManager.testHook = null;
 +        try {
 +          DiskStore ds = cache.findDiskStore("disk");
 +          if (ds == null) {
 +            ds = cache.createDiskStoreFactory().setDiskDirs(getDiskDirs())
 +                .create("disk");
 +          }
 +          AttributesFactory attr = new AttributesFactory();
 +          attr.setValueConstraint(PortfolioData.class);
 +          attr.setIndexMaintenanceSynchronous(true);
 +          EvictionAttributesImpl evicAttr = new EvictionAttributesImpl()
 +              .setAction(EvictionAction.OVERFLOW_TO_DISK);
 +          evicAttr.setAlgorithm(EvictionAlgorithm.LRU_ENTRY).setMaximum(1);
 +          attr.setEvictionAttributes(evicAttr);
 +          attr.setDataPolicy(DataPolicy.REPLICATE);
 +          // attr.setPartitionAttributes(new
 +          // PartitionAttributesFactory().setTotalNumBuckets(1).create());
 +          attr.setDiskStoreName("disk");
 +          RegionFactory regionFactory = cache.createRegionFactory(attr.create());
 +          partitionRegion = regionFactory.create(name);
 +        } catch (IllegalStateException ex) {
 +          LogWriterUtils.getLogWriter().warning("Creation caught IllegalStateException", ex);
 +        }
 +        assertNotNull("Region " + name + " not in cache", cache.getRegion(name));
 +        assertNotNull("Region ref null", partitionRegion);
 +        assertTrue("Region ref claims to be destroyed",
 +            !partitionRegion.isDestroyed());
 +        // Create Indexes
 +        try {
 +          Index index = cache.getQueryService().createIndex("statusIndex",
 +              "p.status", "/" + name + " p");
 +          assertNotNull(index);
 +        } catch (Exception e1) {
 +          e1.printStackTrace();
 +          fail("Index creation failed");
 +        }
 +      }
 +    });
 +
 +    // Start changing the value in Region which should turn into a deadlock if
 +    // the fix is not there
 +    AsyncInvocation asyncInv1 = vm0.invokeAsync(new CacheSerializableRunnable(
 +        "Change value in region") {
 +
 +      @Override
 +      public void run2() throws CacheException {
 +        Cache cache = PRQHelp.getCache();
 +
 +        // Do a put in region.
 +        Region r = PRQHelp.getCache().getRegion(name);
 +
 +        for (int i = 0; i < 100; i++) {
 +          r.put(i, new PortfolioData(i));
 +        }
 +
 +        assertNull(IndexManager.testHook);
 +        IndexManager.testHook = new IndexManagerTestHook();
 +
 +        // Destroy one of the values.
 +        PRQHelp.getCache().getLogger().fine("Destroying the value");
 +        r.destroy(1);
 +
 +        IndexManager.testHook = null;
 +      }
 +    });
 +
 +    AsyncInvocation asyncInv2 = vm0.invokeAsync(new CacheSerializableRunnable(
 +        "Run query on region") {
 +
 +      @Override
 +      public void run2() throws CacheException {
 +        Cache cache = PRQHelp.getCache();
 +
 +        while (!hooked) {
 +          Wait.pause(100);
 +        }
 +        // Create and hence initialize Index
 +        try {
 +          Index index = cache.getQueryService().createIndex("idIndex",
 +              "p.ID", "/" + name + " p");
 +          assertNotNull(index);
 +        } catch (Exception e1) {
 +          e1.printStackTrace();
 +          fail("Index creation failed");
 +        }
 +      }
 +    });
 +
 +    // If we take more than 30 seconds then its a deadlock.
 +    ThreadUtils.join(asyncInv2, 30 * 1000);
 +    ThreadUtils.join(asyncInv1, 30 * 1000);
 +  }
 +
 +  /**
 +  *
 +  */
 +  public void testAsyncIndexInitDuringEntryPutUsingClientOnRR() {
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    
 +    IgnoredException.addIgnoredException("Unexpected IOException:");
 +    IgnoredException.addIgnoredException("java.net.SocketException");
 +
 +    name = "PartionedPortfoliosPR";
 +    // Create Overflow Persistent Partition Region
 +    vm0.invoke(new CacheSerializableRunnable(
 +        "Create local region with synchronous index maintenance") {
 +      @Override
 +      public void run2() throws CacheException {
 +        Cache cache = PRQHelp.getCache();
 +        
 +        Region partitionRegion = null;
 +        IndexManager.testHook = null;
 +        try {
 +          CacheServer bridge = cache.addCacheServer();
 +          bridge.setPort(0);
 +          bridge.start();
 +          bridgeServerPort = bridge.getPort();
 +          
 +          DiskStore ds = cache.findDiskStore("disk");
 +          if (ds == null) {
 +            ds = cache.createDiskStoreFactory().setDiskDirs(getDiskDirs())
 +                .create("disk");
 +          }
 +          AttributesFactory attr = new AttributesFactory();
 +          attr.setValueConstraint(PortfolioData.class);
 +          attr.setIndexMaintenanceSynchronous(true);
 +          EvictionAttributesImpl evicAttr = new EvictionAttributesImpl()
 +              .setAction(EvictionAction.OVERFLOW_TO_DISK);
 +          evicAttr.setAlgorithm(EvictionAlgorithm.LRU_ENTRY).setMaximum(1);
 +          attr.setEvictionAttributes(evicAttr);
 +          attr.setDataPolicy(DataPolicy.REPLICATE);
 +          // attr.setPartitionAttributes(new
 +          // PartitionAttributesFactory().setTotalNumBuckets(1).create());
 +          attr.setDiskStoreName("disk");
 +          RegionFactory regionFactory = cache.createRegionFactory(attr.create());
 +          partitionRegion = regionFactory.create(name);
 +        } catch (IllegalStateException ex) {
 +          LogWriterUtils.getLogWriter().warning("Creation caught IllegalStateException", ex);
 +        } catch (IOException e) {
 +          e.printStackTrace();
 +        }
 +        assertNotNull("Region " + name + " not in cache", cache.getRegion(name));
 +        assertNotNull("Region ref null", partitionRegion);
 +        assertTrue("Region ref claims to be destroyed", !partitionRegion.isDestroyed());
 +        // Create Indexes
 +        try {
 +          Index index = cache.getQueryService().createIndex("idIndex",
 +              "p.ID", "/" + name + " p");
 +          assertNotNull(index);
 +        } catch (Exception e1) {
 +          e1.printStackTrace();
 +          fail("Index creation failed");
 +        }
 +      }
 +    });
 +
 +    
-     final int port = vm0.invokeInt(ConcurrentIndexInitOnOverflowRegionDUnitTest.class,
-     "getCacheServerPort");
++    final int port = vm0.invoke(() -> ConcurrentIndexInitOnOverflowRegionDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +
 +    // Start changing the value in Region which should turn into a deadlock if
 +    // the fix is not there
 +    vm1.invoke(new CacheSerializableRunnable(
 +        "Change value in region") {
 +
 +      @Override
 +      public void run2() throws CacheException {
 +        disconnectFromDS();
 +        ClientCache clientCache = new ClientCacheFactory().addPoolServer(host0, port).create();
 +
 +        // Do a put in region.
 +        Region r = clientCache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(name);
 +
 +        for (int i = 0; i < 100; i++) {
 +          r.put(i, new PortfolioData(i));
 +        }
 +      }
 +    });
 +
 +    vm0.invoke(new CacheSerializableRunnable("Set Test Hook") {
 +      
 +      @Override
 +      public void run2() throws CacheException {
 +        // Set test hook before client operation
 +        assertNull(IndexManager.testHook);
 +        IndexManager.testHook = new IndexManagerTestHook();
 +      }
 +    });
 +
 +    AsyncInvocation asyncInv1 = vm1.invokeAsync(new CacheSerializableRunnable("Change value in region") {
 +
 +      @Override
 +      public void run2() throws CacheException {
 +        ClientCache clientCache = ClientCacheFactory.getAnyInstance();
 +
 +        // Do a put in region.
 +        Region r = clientCache.getRegion(name);
 +
 +        // Destroy one of the values.
 +        clientCache.getLogger().fine("Destroying the value");
 +        r.destroy(1);
 +      }
 +    });
 +
 +    AsyncInvocation asyncInv2 = vm0.invokeAsync(new CacheSerializableRunnable(
 +        "Run query on region") {
 +
 +      @Override
 +      public void run2() throws CacheException {
 +        Cache cache = PRQHelp.getCache();
 +
 +        while (!hooked) {
 +          Wait.pause(100);
 +        }
 +        // Create Indexes
 +        try {
 +          Index index = cache.getQueryService().createIndex("statusIndex",
 +              "p.status", "/" + name + " p");
 +          assertNotNull(index);
 +        } catch (Exception e1) {
 +          e1.printStackTrace();
 +          fail("Index creation failed");
 +        }
 +      }
 +    });
 +
 +    // If we take more than 30 seconds then its a deadlock.
 +    ThreadUtils.join(asyncInv2, 30 * 1000);
 +    ThreadUtils.join(asyncInv1, 30 * 1000);
 +    
 +    vm0.invoke(new CacheSerializableRunnable("Set Test Hook") {
 +      
 +      @Override
 +      public void run2() throws CacheException {
 +        assertNotNull(IndexManager.testHook);
 +        IndexManager.testHook = null;
 +      }
 +    });
 +
 +  }
 +
 +  /**
 +   * This tests if index updates are blocked while region.clear() is
 +   * called and indexes are being reinitialized.
 +   */
 +  public void testIndexUpdateWithRegionClear() {
 +    
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +
 +    final String regionName = "portfolio";
 +    
 +    hooked = false;
 +    
 +    // Create region and an index on it
 +    vm0.invoke(new CacheSerializableRunnable("Create region and index") {
 +      
 +      @Override
 +      public void run2() throws CacheException {
 +        Cache cache = PRQHelp.getCache();
 +        Region region = cache.createRegionFactory(RegionShortcut.LOCAL).create(regionName);
 +        QueryService qService = cache.getQueryService();
 +        
 +        try {
 +          qService.createIndex("idIndex", "ID", "/"+regionName);
 +          qService.createIndex("secIdIndex", "pos.secId", "/" + regionName + " p, p.positions.values pos");
 +        } catch (Exception e) {
 +          fail("Index creation failed." + e);
 +        }
 +      }
 +    });
 +
 +    final class LocalTestHook implements TestHook {
 +
 +      @Override
 +      public void hook(int spot) throws RuntimeException {
 +        switch (spot) {
 +        case 6: // processAction in IndexManager
 +          hooked = true;
 +          //wait untill some thread unhooks.
 +          while (hooked) { Wait.pause(20); }
 +          break;
 +        default:
 +          break;
 +        }
 +      }
 +      
 +    }
 +    
 +    // Asynch invocation for continuous index updates
 +    AsyncInvocation indexUpdateAsysnch = vm0.invokeAsync(new CacheSerializableRunnable("index updates") {
 +      
 +      @Override
 +      public void run2() throws CacheException {
 +        
 +        Region region = PRQHelp.getCache().getRegion(regionName);
 +        for (int i=0; i<100; i++) {
 +          if (i == 50) IndexManager.testHook = new LocalTestHook();
 +          region.put(i, new Portfolio(i));
 +          if (i == 50) Wait.pause(20);
 +        }
 +      }
 +    });
 + 
 +    // Region.clear() which should block other region updates.
 +    vm0.invoke(new CacheSerializableRunnable("Clear the region") {
 +      
 +      @Override
 +      public void run2() throws CacheException {
 +        Region region = PRQHelp.getCache().getRegion(regionName);
 +        
 +        while(!hooked) {
 +          Wait.pause(100);
 +        }
 +        if (hooked) {
 +          hooked = false;
 +          IndexManager.testHook = null;
 +          region.clear();
 +        }
 +
 +        try {
 +            QueryService qservice = PRQHelp.getCache().getQueryService();
 +            Index index = qservice.getIndex(region, "idIndex");
 +            if (((CompactRangeIndex)index).getIndexStorage().size() > 1) {
 +              fail("After clear region size is supposed to be zero as all index updates are blocked. Current region size is: "+ region.size());
 +            }
 +        } finally {
 +          IndexManager.testHook = null;  
 +        }
 +      }
 +    });
 +
 +    // Kill asynch thread
 +    ThreadUtils.join(indexUpdateAsysnch, 20000);
 +
 +    //Verify region size which must be 50
 +    vm0.invoke(new CacheSerializableRunnable("Check region size") {
 +      
 +      @Override
 +      public void run2() throws CacheException {
 +        Region region = PRQHelp.getCache().getRegion(regionName);
 +        if (region.size() > 50) {
 +          fail("After clear region size is supposed to be 50 as all index updates are blocked " + region.size());
 +        }
 +      }
 +    });
 +  }
 +
 +  public class IndexManagerTestHook implements
 +      com.gemstone.gemfire.cache.query.internal.index.IndexManager.TestHook {
 +    public void hook(final int spot) throws RuntimeException {
 +      switch (spot) {
 +      case 6: // Before Index update and after region entry lock.
 +        hooked = true;
 +        LogWriterUtils.getLogWriter().fine("IndexManagerTestHook is hooked.");
 +        Wait.pause(10000);
 +        hooked = false;
 +        break;
 +      default:
 +        break;
 +      }
 +    }
 +  }
 +
 +  private static int getCacheServerPort() {
 +    return bridgeServerPort;
 +  }
 +}


[24/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/hll/Bits.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/hll/Bits.java
index 0000000,0000000..351adbb
new file mode 100755
--- /dev/null
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/hll/Bits.java
@@@ -1,0 -1,0 +1,48 @@@
++/*
++ * Copyright (C) 2011 Clearspring Technologies, Inc.
++ *
++ * Licensed 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.hll;
++
++import java.io.ByteArrayInputStream;
++import java.io.DataInput;
++import java.io.DataInputStream;
++import java.io.IOException;
++
++public class Bits {
++
++    public static int[] getBits(byte[] mBytes) throws IOException {
++        int bitSize = mBytes.length / 4;
++        int[] bits = new int[bitSize];
++        DataInputStream dis = new DataInputStream(new ByteArrayInputStream(mBytes));
++        for (int i = 0; i < bitSize; i++) {
++            bits[i] = dis.readInt();
++        }
++        return bits;
++    }
++
++    /**
++     * This method might be better described as
++     * "byte array to int array" or "data input to int array"
++     */
++    public static int[] getBits(DataInput dataIn, int byteLength) throws IOException {
++        int bitSize = byteLength / 4;
++        int[] bits = new int[bitSize];
++        for (int i = 0; i < bitSize; i++) {
++            bits[i] = dataIn.readInt();
++        }
++        return bits;
++    }
++
++}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/hll/CardinalityMergeException.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/hll/CardinalityMergeException.java
index 0000000,0000000..e8ae3be
new file mode 100755
--- /dev/null
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/hll/CardinalityMergeException.java
@@@ -1,0 -1,0 +1,26 @@@
++/*
++ * Copyright (C) 2011 Clearspring Technologies, Inc.
++ *
++ * Licensed 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.hll;
++
++
++
++@SuppressWarnings("serial")
++public abstract class CardinalityMergeException extends Exception {
++
++    public CardinalityMergeException(String message) {
++        super(message);
++    }
++}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/hll/HyperLogLog.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/hll/HyperLogLog.java
index 0000000,0000000..7e88198
new file mode 100755
--- /dev/null
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/hll/HyperLogLog.java
@@@ -1,0 -1,0 +1,345 @@@
++/*
++ * Copyright (C) 2012 Clearspring Technologies, Inc.
++ *
++ * Licensed 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.hll;
++
++import com.gemstone.gemfire.internal.redis.executor.hll.HllExecutor;
++
++import java.io.ByteArrayInputStream;
++import java.io.ByteArrayOutputStream;
++import java.io.DataInput;
++import java.io.DataInputStream;
++import java.io.DataOutput;
++import java.io.DataOutputStream;
++import java.io.Externalizable;
++import java.io.IOException;
++import java.io.ObjectInput;
++import java.io.ObjectOutput;
++import java.io.Serializable;
++
++/**
++ * Java implementation of HyperLogLog (HLL) algorithm from this paper:
++ * <p/>
++ * http://algo.inria.fr/flajolet/Publications/FlFuGaMe07.pdf
++ * <p/>
++ * HLL is an improved version of LogLog that is capable of estimating
++ * the cardinality of a set with accuracy = 1.04/sqrt(m) where
++ * m = 2^b.  So we can control accuracy vs space usage by increasing
++ * or decreasing b.
++ * <p/>
++ * The main benefit of using HLL over LL is that it only requires 64%
++ * of the space that LL does to get the same accuracy.
++ * <p/>
++ * This implementation implements a single counter.  If a large (millions)
++ * number of counters are required you may want to refer to:
++ * <p/>
++ * http://dsiutils.dsi.unimi.it/
++ * <p/>
++ * It has a more complex implementation of HLL that supports multiple counters
++ * in a single object, drastically reducing the java overhead from creating
++ * a large number of objects.
++ * <p/>
++ * This implementation leveraged a javascript implementation that Yammer has
++ * been working on:
++ * <p/>
++ * https://github.com/yammer/probablyjs
++ * <p>
++ * Note that this implementation does not include the long range correction function
++ * defined in the original paper.  Empirical evidence shows that the correction
++ * function causes more harm than good.
++ * </p>
++ * <p/>
++ * <p>
++ * Users have different motivations to use different types of hashing functions.
++ * Rather than try to keep up with all available hash functions and to remove
++ * the concern of causing future binary incompatibilities this class allows clients
++ * to offer the value in hashed int or long form.  This way clients are free
++ * to change their hash function on their own time line.  We recommend using Google's
++ * Guava Murmur3_128 implementation as it provides good performance and speed when
++ * high precision is required.  In our tests the 32bit MurmurHash function included
++ * in this project is faster and produces better results than the 32 bit murmur3
++ * implementation google provides.
++ * </p>
++ */
++public class HyperLogLog implements ICardinality, Serializable {
++
++  private static final long serialVersionUID = -4661220245111112301L;
++  private final RegisterSet registerSet;
++  private final int log2m;
++  private final double alphaMM;
++
++
++  /**
++   * Create a new HyperLogLog instance using the specified standard deviation.
++   *
++   * @param rsd - the relative standard deviation for the counter.
++   *            smaller values create counters that require more space.
++   */
++  public HyperLogLog(double rsd) {
++    this(log2m(rsd));
++  }
++
++  private static int log2m(double rsd) {
++    return (int) (Math.log((1.106 / rsd) * (1.106 / rsd)) / Math.log(2));
++  }
++
++  /**
++   * Create a new HyperLogLog instance.  The log2m parameter defines the accuracy of
++   * the counter.  The larger the log2m the better the accuracy.
++   * <p/>
++   * accuracy = 1.04/sqrt(2^log2m)
++   *
++   * @param log2m - the number of bits to use as the basis for the HLL instance
++   */
++  public HyperLogLog(int log2m) {
++    this(log2m, new RegisterSet(1 << log2m));
++  }
++
++  /**
++   * Creates a new HyperLogLog instance using the given registers.  Used for unmarshalling a serialized
++   * instance and for merging multiple counters together.
++   *
++   * @param registerSet - the initial values for the register set
++   */
++  @Deprecated
++  public HyperLogLog(int log2m, RegisterSet registerSet) {
++    if (log2m < 0 || log2m > 30) {
++      throw new IllegalArgumentException("log2m argument is "
++          + log2m + " and is outside the range [0, 30]");
++    }
++    this.registerSet = registerSet;
++    this.log2m = log2m;
++    int m = 1 << this.log2m;
++
++    alphaMM = getAlphaMM(log2m, m);
++  }
++
++  @Override
++  public boolean offerHashed(long hashedValue) {
++    // j becomes the binary address determined by the first b log2m of x
++    // j will be between 0 and 2^log2m
++    final int j = (int) (hashedValue >>> (Long.SIZE - log2m));
++    final int r = Long.numberOfLeadingZeros((hashedValue << this.log2m) | (1 << (this.log2m - 1)) + 1) + 1;
++    return registerSet.updateIfGreater(j, r);
++  }
++
++  @Override
++  public boolean offerHashed(int hashedValue) {
++    // j becomes the binary address determined by the first b log2m of x
++    // j will be between 0 and 2^log2m
++    final int j = hashedValue >>> (Integer.SIZE - log2m);
++    final int r = Integer.numberOfLeadingZeros((hashedValue << this.log2m) | (1 << (this.log2m - 1)) + 1) + 1;
++    return registerSet.updateIfGreater(j, r);
++  }
++
++  @Override
++  public boolean offer(Object o) {
++    final int x = MurmurHash.hash(o);
++    return offerHashed(x);
++  }
++
++
++  @Override
++  public long cardinality() {
++    double registerSum = 0;
++    int count = registerSet.count;
++    double zeros = 0.0;
++    for (int j = 0; j < registerSet.count; j++) {
++      int val = registerSet.get(j);
++      registerSum += 1.0 / (1 << val);
++      if (val == 0) {
++        zeros++;
++      }
++    }
++
++    double estimate = alphaMM * (1 / registerSum);
++
++    if (estimate <= (5.0 / 2.0) * count) {
++      // Small Range Estimate
++      return Math.round(linearCounting(count, zeros));
++    } else {
++      return Math.round(estimate);
++    }
++  }
++
++  @Override
++  public int sizeof() {
++    return registerSet.size * 4;
++  }
++
++  @Override
++  public byte[] getBytes() throws IOException {
++    ByteArrayOutputStream baos = new ByteArrayOutputStream();
++    DataOutput dos = new DataOutputStream(baos);
++    writeBytes(dos);
++
++    return baos.toByteArray();
++  }
++
++  private void writeBytes(DataOutput serializedByteStream) throws IOException {
++    serializedByteStream.writeInt(log2m);
++    serializedByteStream.writeInt(registerSet.size * 4);
++    for (int x : registerSet.readOnlyBits()) {
++      serializedByteStream.writeInt(x);
++    }
++  }
++
++  /**
++   * Add all the elements of the other set to this set.
++   * <p/>
++   * This operation does not imply a loss of precision.
++   *
++   * @param other A compatible Hyperloglog instance (same log2m)
++   * @throws CardinalityMergeException if other is not compatible
++   */
++  public void addAll(HyperLogLog other) throws CardinalityMergeException {
++    if (this.sizeof() != other.sizeof()) {
++      throw new HyperLogLogMergeException("Cannot merge estimators of different sizes");
++    }
++
++    registerSet.merge(other.registerSet);
++  }
++
++  @Override
++  public ICardinality merge(ICardinality... estimators) throws CardinalityMergeException {
++    HyperLogLog merged = new HyperLogLog(HllExecutor.DEFAULT_HLL_STD_DEV);//new HyperLogLog(log2m, new RegisterSet(this.registerSet.count));
++    merged.addAll(this);
++
++    if (estimators == null) {
++      return merged;
++    }
++
++    for (ICardinality estimator : estimators) {
++      if (!(estimator instanceof HyperLogLog)) {
++        throw new HyperLogLogMergeException("Cannot merge estimators of different class");
++      }
++      HyperLogLog hll = (HyperLogLog) estimator;
++      merged.addAll(hll);
++    }
++
++    return merged;
++  }
++
++  private Object writeReplace() {
++    return new SerializationHolder(this);
++  }
++
++  /**
++   * This class exists to support Externalizable semantics for
++   * HyperLogLog objects without having to expose a public
++   * constructor, public write/read methods, or pretend final
++   * fields aren't final.
++   *
++   * In short, Externalizable allows you to skip some of the more
++   * verbose meta-data default Serializable gets you, but still
++   * includes the class name. In that sense, there is some cost
++   * to this holder object because it has a longer class name. I
++   * imagine people who care about optimizing for that have their
++   * own work-around for long class names in general, or just use
++   * a custom serialization framework. Therefore we make no attempt
++   * to optimize that here (eg. by raising this from an inner class
++   * and giving it an unhelpful name).
++   */
++  private static class SerializationHolder implements Externalizable {
++
++    HyperLogLog hyperLogLogHolder;
++
++    public SerializationHolder(HyperLogLog hyperLogLogHolder) {
++      this.hyperLogLogHolder = hyperLogLogHolder;
++    }
++
++    /**
++     * required for Externalizable
++     */
++    @SuppressWarnings("unused")
++    public SerializationHolder() {
++
++    }
++
++    @Override
++    public void writeExternal(ObjectOutput out) throws IOException {
++      hyperLogLogHolder.writeBytes(out);
++    }
++
++    @Override
++    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
++      hyperLogLogHolder = Builder.build(in);
++    }
++
++    private Object readResolve() {
++      return hyperLogLogHolder;
++    }
++  }
++
++  public static class Builder implements IBuilder<ICardinality>, Serializable {
++
++    private static final long serialVersionUID = -979314356097156719L;
++    private double rsd;
++
++    public Builder(double rsd) {
++      this.rsd = rsd;
++    }
++
++    @Override
++    public HyperLogLog build() {
++      return new HyperLogLog(rsd);
++    }
++
++    @Override
++    public int sizeof() {
++      int log2m = log2m(rsd);
++      int k = 1 << log2m;
++      return RegisterSet.getBits(k) * 4;
++    }
++
++    public static HyperLogLog build(byte[] bytes) throws IOException {
++      ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
++      return build(new DataInputStream(bais));
++    }
++
++    public static HyperLogLog build(DataInput serializedByteStream) throws IOException {
++      int log2m = serializedByteStream.readInt();
++      int byteArraySize = serializedByteStream.readInt();
++      return new HyperLogLog(log2m,
++          new RegisterSet(1 << log2m, Bits.getBits(serializedByteStream, byteArraySize)));
++    }
++  }
++
++  @SuppressWarnings("serial")
++  protected static class HyperLogLogMergeException extends CardinalityMergeException {
++
++    public HyperLogLogMergeException(String message) {
++      super(message);
++    }
++  }
++
++  protected static double getAlphaMM(final int p, final int m) {
++    // See the paper.
++    switch (p) {
++    case 4:
++      return 0.673 * m * m;
++    case 5:
++      return 0.697 * m * m;
++    case 6:
++      return 0.709 * m * m;
++    default:
++      return (0.7213 / (1 + 1.079 / m)) * m * m;
++    }
++  }
++
++  protected static double linearCounting(int m, double V) {
++    return m * Math.log(m / V);
++  }
++}


[19/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/CacheServerSSLConnectionDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/CacheServerSSLConnectionDUnitTest.java
index 9bdf56c,0000000..5fa4fc4
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/CacheServerSSLConnectionDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/CacheServerSSLConnectionDUnitTest.java
@@@ -1,424 -1,0 +1,424 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import java.io.IOException;
 +import java.io.PrintWriter;
 +import java.io.StringWriter;
 +import java.util.Properties;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheFactory;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionFactory;
 +import com.gemstone.gemfire.cache.RegionShortcut;
 +import com.gemstone.gemfire.cache.client.ClientCache;
 +import com.gemstone.gemfire.cache.client.ClientCacheFactory;
 +import com.gemstone.gemfire.cache.client.ClientRegionFactory;
 +import com.gemstone.gemfire.cache.client.ClientRegionShortcut;
 +import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 +import com.gemstone.gemfire.internal.AvailablePortHelper;
 +import com.gemstone.gemfire.security.AuthenticationRequiredException;
 +import com.gemstone.gemfire.test.dunit.DistributedTestCase;
 +import com.gemstone.gemfire.test.dunit.IgnoredException;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.VM;
 +import com.gemstone.gemfire.util.test.TestUtil;
 +
 +/**
 + * Tests cacheserver ssl support added. See https://svn.gemstone.com/trac/gemfire/ticket/48995 for details
 + * @author tushark
 + *
 + */
 +public class CacheServerSSLConnectionDUnitTest extends DistributedTestCase {
 +  
 +  private static final long serialVersionUID = 1L;
 +  private Cache cache;
 +  private CacheServer cacheServer;
 +  private ClientCache clientCache;
 +  private int cacheServerPort;
 +  private String hostName;
 +  
 +  private static final String TRUSTED_STORE = "trusted.keystore";
 +  private static final String CLIENT_KEY_STORE = "client.keystore";
 +  private static final String CLIENT_TRUST_STORE = "client.truststore";
 +  private static final String SERVER_KEY_STORE = "cacheserver.keystore";
 +  private static final String SERVER_TRUST_STORE = "cacheserver.truststore";
 +  
 +  private static CacheServerSSLConnectionDUnitTest instance = new CacheServerSSLConnectionDUnitTest("CacheServerSSLConnectionDUnit");
 +  
 +  
 +  public void setUp() throws Exception {
 +    disconnectAllFromDS();
 +    super.setUp();
 +  }
 +
 +  public CacheServerSSLConnectionDUnitTest(String name) {
 +    super(name);
 +  }  
 +
 +  public Cache createCache(Properties props) throws Exception
 +  {
 +    props.setProperty("mcast-port", "0");
 +    props.setProperty("locators", "");
 +    cache = new CacheFactory(props).create();
 +    if (cache == null) {
 +      throw new Exception("CacheFactory.create() returned null ");
 +    }
 +    return cache;
 +  }
 +  
 +  private void createServer() throws IOException{
 +    cacheServerPort = AvailablePortHelper.getRandomAvailableTCPPort();
 +    cacheServer = cache.addCacheServer();
 +    cacheServer.setPort(cacheServerPort);
 +    cacheServer.start();
 +    hostName = cacheServer.getHostnameForClients();
 +  }
 +  
 +  public int getCacheServerPort(){
 +    return cacheServerPort;
 +  }
 +  
 +  public String getCacheServerHost(){
 +    return hostName;
 +  }
 +  
 +  public void stopCacheServer(){
 +    this.cacheServer.stop();
 +  }
 +  
 +  
 +  @SuppressWarnings("rawtypes")
 +  public void setUpServerVM(boolean cacheServerSslenabled) throws Exception {
 +    Properties gemFireProps = new Properties();
 +
 +    String cacheServerSslprotocols = "any";
 +    String cacheServerSslciphers = "any";
 +    boolean cacheServerSslRequireAuth = true;
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_ENABLED_NAME,
 +        String.valueOf(cacheServerSslenabled));
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_PROTOCOLS_NAME,
 +        cacheServerSslprotocols);
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_CIPHERS_NAME,
 +        cacheServerSslciphers);
 +    gemFireProps.put(
 +        DistributionConfig.SERVER_SSL_REQUIRE_AUTHENTICATION_NAME,
 +        String.valueOf(cacheServerSslRequireAuth));
 +
 +    String keyStore = TestUtil.getResourcePath(CacheServerSSLConnectionDUnitTest.class, SERVER_KEY_STORE);
 +    String trustStore = TestUtil.getResourcePath(CacheServerSSLConnectionDUnitTest.class, SERVER_TRUST_STORE);
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_KEYSTORE_TYPE_NAME, "jks");
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_KEYSTORE_NAME, keyStore);
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_KEYSTORE_PASSWORD_NAME, "password");
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_TRUSTSTORE_NAME, trustStore);
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_TRUSTSTORE_PASSWORD_NAME, "password");
 +    
 +    StringWriter sw = new StringWriter();
 +    PrintWriter writer = new PrintWriter(sw);
 +    gemFireProps.list(writer);
 +    System.out.println("Starting cacheserver ds with following properties \n" + sw);
 +    createCache(gemFireProps);
 +    
 +    RegionFactory factory = cache.createRegionFactory(RegionShortcut.REPLICATE);
 +    Region r = factory.create("serverRegion");
 +    r.put("serverkey", "servervalue");
 +  }
 +  
 +  public void setUpClientVM(String host, int port,
 +      boolean cacheServerSslenabled, boolean cacheServerSslRequireAuth,
 +      String keyStore, String trustStore, boolean subscription) {
 +
 +    Properties gemFireProps = new Properties();
 +
 +    String cacheServerSslprotocols = "any";
 +    String cacheServerSslciphers = "any";
 +
 +    String keyStorePath = TestUtil.getResourcePath(CacheServerSSLConnectionDUnitTest.class, keyStore);
 +    String trustStorePath = TestUtil.getResourcePath(CacheServerSSLConnectionDUnitTest.class, trustStore);
 +    //using new server-ssl-* properties
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_ENABLED_NAME,
 +        String.valueOf(cacheServerSslenabled));
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_PROTOCOLS_NAME,
 +        cacheServerSslprotocols);
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_CIPHERS_NAME,
 +        cacheServerSslciphers);
 +    gemFireProps.put(
 +        DistributionConfig.SERVER_SSL_REQUIRE_AUTHENTICATION_NAME,
 +        String.valueOf(cacheServerSslRequireAuth));
 +
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_KEYSTORE_TYPE_NAME, "jks");
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_KEYSTORE_NAME, keyStorePath);
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_KEYSTORE_PASSWORD_NAME, "password");
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_TRUSTSTORE_NAME, trustStorePath);
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_TRUSTSTORE_PASSWORD_NAME, "password");
 +
 +    StringWriter sw = new StringWriter();
 +    PrintWriter writer = new PrintWriter(sw);
 +    gemFireProps.list(writer);
 +    System.out.println("Starting client ds with following properties \n" + sw.getBuffer());
 +    
 +    ClientCacheFactory clientCacheFactory = new ClientCacheFactory(gemFireProps);
 +    clientCacheFactory.setPoolSubscriptionEnabled(subscription).addPoolServer(host, port);
 +    clientCache = clientCacheFactory.create();
 +    
 +    ClientRegionFactory<String,String> regionFactory = clientCache.createClientRegionFactory(ClientRegionShortcut.PROXY);
 +    Region<String, String> region = regionFactory.create("serverRegion");  
 +    assertNotNull(region);
 +  }
 +  
 +  public void doClientRegionTest(){
 +    Region<String, String> region = clientCache.getRegion("serverRegion");
 +    assertEquals("servervalue",region.get("serverkey"));
 +    region.put("clientkey", "clientvalue");
 +    assertEquals("clientvalue",region.get("clientkey"));
 +  }
 +  
 +  public void doServerRegionTest(){
 +    Region<String, String> region = cache.getRegion("serverRegion");
 +    assertEquals("servervalue",region.get("serverkey"));    
 +    assertEquals("clientvalue",region.get("clientkey"));
 +  }
 +  
 +  
 +  public static void setUpServerVMTask(boolean cacheServerSslenabled) throws Exception{
 +    instance.setUpServerVM(cacheServerSslenabled);
 +  }
 +  
 +  public static void createServerTask() throws Exception {
 +    instance.createServer();
 +  }
 +  
 +  public static void setUpClientVMTask(String host, int port,
 +      boolean cacheServerSslenabled, boolean cacheServerSslRequireAuth, String keyStore, String trustStore)
 +      throws Exception {
 +    instance.setUpClientVM(host, port, cacheServerSslenabled,
 +        cacheServerSslRequireAuth, keyStore, trustStore, true);
 +  }
 +  public static void setUpClientVMTaskNoSubscription(String host, int port,
 +      boolean cacheServerSslenabled, boolean cacheServerSslRequireAuth, String keyStore, String trustStore)
 +      throws Exception {
 +    instance.setUpClientVM(host, port, cacheServerSslenabled,
 +        cacheServerSslRequireAuth, keyStore, trustStore, false);
 +  }
 +  
 +  public static void doClientRegionTestTask() {
 +    instance.doClientRegionTest();
 +  }
 +  
 +  public static void doServerRegionTestTask() {
 +    instance.doServerRegionTest();
 +  }
 +  
 +  public static Object[] getCacheServerEndPointTask() {
 +    Object[] array = new Object[2];
 +    array[0] = instance.getCacheServerHost();
 +    array[1] = instance.getCacheServerPort();
 +    return array;
 +  }
 +  
 +  public static void closeCacheTask(){
 +    if (instance != null && instance.cache != null) {
 +      instance.cache.close();
 +    }
 +  }
 +  
 +  public static void closeClientCacheTask(){
 +    if (instance != null && instance.clientCache != null) {
 +      instance.clientCache.close();
 +    }
 +  }
 +  
 +  public void testCacheServerSSL() throws Exception {
 +    final Host host = Host.getHost(0);
 +    VM serverVM = host.getVM(1);
 +    VM clientVM = host.getVM(2);
 +    
 +    boolean cacheServerSslenabled = true;
 +    boolean cacheClientSslenabled = true;
 +    boolean cacheClientSslRequireAuth = true;
 +    
-     serverVM.invoke(CacheServerSSLConnectionDUnitTest.class, "setUpServerVMTask", new Object[]{cacheServerSslenabled});
-     serverVM.invoke(CacheServerSSLConnectionDUnitTest.class, "createServerTask");
++    serverVM.invoke(() -> CacheServerSSLConnectionDUnitTest.setUpServerVMTask(cacheServerSslenabled));
++    serverVM.invoke(() -> CacheServerSSLConnectionDUnitTest.createServerTask());
 +    
-     Object array[] = (Object[])serverVM.invoke(CacheServerSSLConnectionDUnitTest.class, "getCacheServerEndPointTask"); 
++    Object array[] = (Object[])serverVM.invoke(() -> CacheServerSSLConnectionDUnitTest.getCacheServerEndPointTask()); 
 +    String hostName = (String)array[0];
 +    int port = (Integer) array[1];
 +    Object params[] = new Object[6];
 +    params[0] = hostName;
 +    params[1] = port;
 +    params[2] = cacheClientSslenabled;
 +    params[3] = cacheClientSslRequireAuth;
 +    params[4] = CLIENT_KEY_STORE;
 +    params[5] = CLIENT_TRUST_STORE;
 +    //getLogWriter().info("Starting client with server endpoint " + hostName + ":" + port);
 +    clientVM.invoke(CacheServerSSLConnectionDUnitTest.class, "setUpClientVMTask", params);
-     clientVM.invoke(CacheServerSSLConnectionDUnitTest.class, "doClientRegionTestTask");
-     serverVM.invoke(CacheServerSSLConnectionDUnitTest.class, "doServerRegionTestTask");
++    clientVM.invoke(() -> CacheServerSSLConnectionDUnitTest.doClientRegionTestTask());
++    serverVM.invoke(() -> CacheServerSSLConnectionDUnitTest.doServerRegionTestTask());
 +    
 +  }
 +  
 +  
 +  public void testNonSSLClient() throws Exception {
 +    final Host host = Host.getHost(0);
 +    VM serverVM = host.getVM(1);
 +    VM clientVM = host.getVM(2);
 +    
 +    boolean cacheServerSslenabled = true;
 +    boolean cacheClientSslenabled = false;
 +    boolean cacheClientSslRequireAuth = true;
 +    
-     serverVM.invoke(CacheServerSSLConnectionDUnitTest.class, "setUpServerVMTask", new Object[]{cacheServerSslenabled});
-     serverVM.invoke(CacheServerSSLConnectionDUnitTest.class, "createServerTask");
++    serverVM.invoke(() -> CacheServerSSLConnectionDUnitTest.setUpServerVMTask(cacheServerSslenabled));
++    serverVM.invoke(() -> CacheServerSSLConnectionDUnitTest.createServerTask());
 +    
-     Object array[] = (Object[])serverVM.invoke(CacheServerSSLConnectionDUnitTest.class, "getCacheServerEndPointTask"); 
++    Object array[] = (Object[])serverVM.invoke(() -> CacheServerSSLConnectionDUnitTest.getCacheServerEndPointTask()); 
 +    String hostName = (String)array[0];
 +    int port = (Integer) array[1];
 +    Object params[] = new Object[6];
 +    params[0] = hostName;
 +    params[1] = port;
 +    params[2] = cacheClientSslenabled;
 +    params[3] = cacheClientSslRequireAuth;
 +    params[4] = TRUSTED_STORE;
 +    params[5] = TRUSTED_STORE;
 +    IgnoredException expect = IgnoredException.addIgnoredException("javax.net.ssl.SSLException", serverVM);
 +    IgnoredException expect2 = IgnoredException.addIgnoredException("IOException", serverVM);
 +    try{
 +      //getLogWriter().info("Starting client with server endpoint " + hostName + ":" + port);    
 +      clientVM.invoke(CacheServerSSLConnectionDUnitTest.class, "setUpClientVMTaskNoSubscription", params);
-       clientVM.invoke(CacheServerSSLConnectionDUnitTest.class, "doClientRegionTestTask");
-       serverVM.invoke(CacheServerSSLConnectionDUnitTest.class, "doServerRegionTestTask");
++      clientVM.invoke(() -> CacheServerSSLConnectionDUnitTest.doClientRegionTestTask());
++      serverVM.invoke(() -> CacheServerSSLConnectionDUnitTest.doServerRegionTestTask());
 +      fail("Test should fail as non-ssl client is trying to connect to ssl configured server");
 +    } catch (Exception rmiException) {
 +      Throwable e = rmiException.getCause();
 +      //getLogWriter().info("ExceptionCause at clientVM " + e);
 +      if (e instanceof com.gemstone.gemfire.cache.client.ServerOperationException) {
 +        Throwable t = e.getCause();
 +        //getLogWriter().info("Cause is " + t);
 +        assertTrue(t instanceof com.gemstone.gemfire.security.AuthenticationRequiredException);
 +      } else {
 +        //getLogWriter().error("Unexpected exception ", e);
 +        fail("Unexpected Exception: " + e + " expected: "
 +            + AuthenticationRequiredException.class);
 +      }
 +    } finally {
 +      expect.remove();
 +      expect2.remove();
 +    }
 +  }
 +  
 +  public void testSSLClientWithNoAuth() throws Exception {
 +    final Host host = Host.getHost(0);
 +    VM serverVM = host.getVM(1);
 +    VM clientVM = host.getVM(2);
 +
 +    boolean cacheServerSslenabled = true;
 +    boolean cacheClientSslenabled = true;
 +    boolean cacheClientSslRequireAuth = false;
 +
-     serverVM.invoke(CacheServerSSLConnectionDUnitTest.class, "setUpServerVMTask", new Object[]{cacheServerSslenabled});
-     serverVM.invoke(CacheServerSSLConnectionDUnitTest.class, "createServerTask");
++    serverVM.invoke(() -> CacheServerSSLConnectionDUnitTest.setUpServerVMTask(cacheServerSslenabled));
++    serverVM.invoke(() -> CacheServerSSLConnectionDUnitTest.createServerTask());
 +
-     Object array[] = (Object[])serverVM.invoke(CacheServerSSLConnectionDUnitTest.class, "getCacheServerEndPointTask"); 
++    Object array[] = (Object[])serverVM.invoke(() -> CacheServerSSLConnectionDUnitTest.getCacheServerEndPointTask()); 
 +    String hostName = (String)array[0];
 +    int port = (Integer) array[1];
 +    Object params[] = new Object[6];
 +    params[0] = hostName;
 +    params[1] = port;
 +    params[2] = cacheClientSslenabled;
 +    params[3] = cacheClientSslRequireAuth;
 +    params[4] = CLIENT_KEY_STORE;
 +    params[5] = CLIENT_TRUST_STORE;
 +    //getLogWriter().info("Starting client with server endpoint " + hostName + ":" + port);
 +    try {
 +      clientVM.invoke(CacheServerSSLConnectionDUnitTest.class, "setUpClientVMTask", params);
-       clientVM.invoke(CacheServerSSLConnectionDUnitTest.class, "doClientRegionTestTask");
-       serverVM.invoke(CacheServerSSLConnectionDUnitTest.class, "doServerRegionTestTask");
++      clientVM.invoke(() -> CacheServerSSLConnectionDUnitTest.doClientRegionTestTask());
++      serverVM.invoke(() -> CacheServerSSLConnectionDUnitTest.doServerRegionTestTask());
 +    } catch (Exception rmiException) {
 +      Throwable e = rmiException.getCause();
 +      //getLogWriter().info("ExceptionCause at clientVM " + e);
 +      if (e instanceof com.gemstone.gemfire.cache.client.ServerOperationException) {
 +        Throwable t = e.getCause();
 +        //getLogWriter().info("Cause is " + t);
 +        assertTrue(t instanceof com.gemstone.gemfire.security.AuthenticationRequiredException);
 +      } else {
 +        //getLogWriter().error("Unexpected exception ", e);
 +        fail("Unexpected Exception...expected "
 +            + AuthenticationRequiredException.class);
 +      }
 +    }
 +  }
 +  
 +  public void testSSLClientWithNonSSLServer() throws Exception {
 +    final Host host = Host.getHost(0);
 +    VM serverVM = host.getVM(1);
 +    VM clientVM = host.getVM(2);
 +    
 +    boolean cacheServerSslenabled = false;
 +    boolean cacheClientSslenabled = true;
 +    boolean cacheClientSslRequireAuth = true;
 +    
-     serverVM.invoke(CacheServerSSLConnectionDUnitTest.class, "setUpServerVMTask", new Object[]{cacheServerSslenabled});
-     serverVM.invoke(CacheServerSSLConnectionDUnitTest.class, "createServerTask");
++    serverVM.invoke(() -> CacheServerSSLConnectionDUnitTest.setUpServerVMTask(cacheServerSslenabled));
++    serverVM.invoke(() -> CacheServerSSLConnectionDUnitTest.createServerTask());
 +    
-     Object array[] = (Object[])serverVM.invoke(CacheServerSSLConnectionDUnitTest.class, "getCacheServerEndPointTask"); 
++    Object array[] = (Object[])serverVM.invoke(() -> CacheServerSSLConnectionDUnitTest.getCacheServerEndPointTask()); 
 +    String hostName = (String)array[0];
 +    int port = (Integer) array[1];
 +    Object params[] = new Object[6];
 +    params[0] = hostName;
 +    params[1] = port;
 +    params[2] = cacheClientSslenabled;
 +    params[3] = cacheClientSslRequireAuth;
 +    params[4] = TRUSTED_STORE;
 +    params[5] = TRUSTED_STORE;
 +    IgnoredException expect = IgnoredException.addIgnoredException("javax.net.ssl.SSLHandshakeException", serverVM);
 +    try{
 +      //getLogWriter().info("Starting client with server endpoint " + hostName + ":" + port);    
 +      clientVM.invoke(CacheServerSSLConnectionDUnitTest.class, "setUpClientVMTask", params);
-       clientVM.invoke(CacheServerSSLConnectionDUnitTest.class, "doClientRegionTestTask");
-       serverVM.invoke(CacheServerSSLConnectionDUnitTest.class, "doServerRegionTestTask");
++      clientVM.invoke(() -> CacheServerSSLConnectionDUnitTest.doClientRegionTestTask());
++      serverVM.invoke(() -> CacheServerSSLConnectionDUnitTest.doServerRegionTestTask());
 +      fail("Test should fail as ssl client with ssl enabled is trying to connect to server with ssl disabled");
 +    }catch (Exception rmiException) {
 +      
 +      //ignore
 +      
 +      /*Throwable e = rmiException.getCause();
 +      getLogWriter().info("ExceptionCause at clientVM " + e);
 +      if (e instanceof com.gemstone.gemfire.cache.client.ServerOperationException) {
 +        Throwable t = e.getCause();
 +        getLogWriter().info("Cause is " + t);
 +        assertTrue(t instanceof com.gemstone.gemfire.security.AuthenticationRequiredException);
 +      } else {
 +        getLogWriter().error("Unexpected exception ", e);
 +        fail("Unexpected Exception...expected "
 +            + AuthenticationRequiredException.class);
 +      }*/
 +    } finally {
 +      expect.remove();
 +    }
 +  }
 +  
 +  @Override
 +  protected final void preTearDown() throws Exception {
 +    final Host host = Host.getHost(0);
 +    VM serverVM = host.getVM(1);
 +    VM clientVM = host.getVM(2);
-     clientVM.invoke(CacheServerSSLConnectionDUnitTest.class, "closeClientCacheTask");
-     serverVM.invoke(CacheServerSSLConnectionDUnitTest.class, "closeCacheTask");
++    clientVM.invoke(() -> CacheServerSSLConnectionDUnitTest.closeClientCacheTask());
++    serverVM.invoke(() -> CacheServerSSLConnectionDUnitTest.closeCacheTask());
 +  }
 +}
 +

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/LocatorLoadBalancingDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/LocatorLoadBalancingDUnitTest.java
index 3c6a980,0000000..6f4d404
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/LocatorLoadBalancingDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/LocatorLoadBalancingDUnitTest.java
@@@ -1,499 -1,0 +1,502 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import java.io.IOException;
 +import java.io.Serializable;
 +import java.net.InetAddress;
 +import java.net.UnknownHostException;
 +import java.util.Collections;
 +import java.util.HashMap;
 +import java.util.List;
 +import java.util.Map;
 +
 +import org.junit.Assert;
 +
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.client.PoolManager;
 +import com.gemstone.gemfire.cache.client.internal.locator.ClientConnectionRequest;
 +import com.gemstone.gemfire.cache.client.internal.locator.ClientConnectionResponse;
 +import com.gemstone.gemfire.cache.client.internal.locator.QueueConnectionRequest;
 +import com.gemstone.gemfire.cache.client.internal.locator.QueueConnectionResponse;
 +import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.cache.server.ServerLoad;
 +import com.gemstone.gemfire.cache.server.ServerLoadProbeAdapter;
 +import com.gemstone.gemfire.cache.server.ServerMetrics;
 +import com.gemstone.gemfire.distributed.Locator;
 +import com.gemstone.gemfire.distributed.internal.InternalLocator;
 +import com.gemstone.gemfire.distributed.internal.ServerLocation;
 +import com.gemstone.gemfire.distributed.internal.ServerLocator;
 +import com.gemstone.gemfire.distributed.internal.tcpserver.TcpClient;
 +import com.gemstone.gemfire.internal.AvailablePort;
 +import com.gemstone.gemfire.internal.cache.CacheServerImpl;
 +import com.gemstone.gemfire.internal.cache.PoolFactoryImpl;
 +import com.gemstone.gemfire.internal.logging.InternalLogWriter;
 +import com.gemstone.gemfire.internal.logging.LocalLogWriter;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.LogWriterUtils;
 +import com.gemstone.gemfire.test.dunit.NetworkUtils;
 +import com.gemstone.gemfire.test.dunit.SerializableRunnable;
 +import com.gemstone.gemfire.test.dunit.SerializableRunnableIF;
 +import com.gemstone.gemfire.test.dunit.VM;
 +import com.gemstone.gemfire.test.dunit.Wait;
 +import com.gemstone.gemfire.test.dunit.WaitCriterion;
 +
 +/**
 + * @author dsmith
 + *
 + */
 +public class LocatorLoadBalancingDUnitTest extends LocatorTestBase {
 +  
 +  /**
 +   * The number of connections that we can be off by in the balancing tests
 +   * We need this little fudge factor, because the locator can receive an update
 +   * from the bridge server after it has made incremented its counter for a client
 +   * connection, but the client hasn't connected yet. This wipes out the estimation
 +   * on the locator.  This means that we may be slighly off in our balance.
 +   * 
 +   * TODO grid fix this hole in the locator.
 +   */
 +  private static final int ALLOWABLE_ERROR_IN_COUNT = 1;
 +  protected static final long MAX_WAIT = 60000;
 +
 +  public LocatorLoadBalancingDUnitTest(String name) {
 +    super(name);
 +  }
 +
 +  /**
 +   * Test the locator discovers a bridge server and is initialized with 
 +   * the correct load for that bridge server.
 +   */
 +  public void testDiscovery() {
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +//    vm0.invoke(new SerializableRunnable() {
 +//      public void run() {
 +//        System.setProperty("gemfire.DistributionAdvisor.VERBOSE", "true");
 +//      }
 +//    });
 +    
 +    int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    startLocatorInVM(vm0, locatorPort, "");
 +    
 +    String locators = getLocatorString(host, locatorPort);
 +    
 +    int serverPort = startBridgeServerInVM(vm1, new String[] {"a", "b"},  locators);
 +    
 +    ServerLoad expectedLoad = new ServerLoad(0f, 1 / 800.0f, 0f, 1f);
 +    ServerLocation expectedLocation = new ServerLocation(NetworkUtils.getServerHostName(vm0
 +        .getHost()), serverPort);
 +    Map expected = new HashMap();
 +    expected.put(expectedLocation, expectedLoad);
 +    
 +    checkLocatorLoad(vm0, expected);
 +    
 +    int serverPort2 = startBridgeServerInVM(vm2, new String[] {"a", "b"},  locators);
 +    
 +    ServerLocation expectedLocation2 = new ServerLocation(NetworkUtils.getServerHostName(vm0
 +        .getHost()), serverPort2);
 +    
 +    expected.put(expectedLocation2, expectedLoad);
 +    checkLocatorLoad(vm0, expected);
 +  }
 +  
 +  /**
 +   * Test that the locator will properly estimate the load for servers when
 +   * it receives connection requests. 
 +   */
 +  public void testEstimation() throws UnknownHostException, IOException, ClassNotFoundException {
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    
 +    int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    startLocatorInVM(vm0, locatorPort, "");
 +    String locators = getLocatorString(host, locatorPort);
 +    
 +    int serverPort = startBridgeServerInVM(vm1, new String[] {"a", "b"},  locators);
 +    
 +    ServerLoad expectedLoad = new ServerLoad(2/800f, 1 / 800.0f, 0f, 1f);
 +    ServerLocation expectedLocation = new ServerLocation(NetworkUtils.getServerHostName(host), serverPort);
 +    Map expected = new HashMap();
 +    expected.put(expectedLocation, expectedLoad);
 +    
 +    ClientConnectionResponse response;
 +    response = (ClientConnectionResponse) TcpClient.requestToServer(InetAddress
 +        .getByName(NetworkUtils.getServerHostName(host)), locatorPort,
 +        new ClientConnectionRequest(Collections.EMPTY_SET, null), 10000);
 +    Assert.assertEquals(expectedLocation, response.getServer());
 +    
 +    response = (ClientConnectionResponse) TcpClient.requestToServer(InetAddress
 +        .getByName(NetworkUtils.getServerHostName(host)), locatorPort,
 +        new ClientConnectionRequest(Collections.EMPTY_SET, null), 10000, true);
 +    Assert.assertEquals(expectedLocation, response.getServer());
 +    
 +    //we expect that the connection load load will be 2 * the loadPerConnection
 +    checkLocatorLoad(vm0, expected);
 +    
 +    QueueConnectionResponse response2;
 +    response2 = (QueueConnectionResponse) TcpClient.requestToServer(InetAddress
 +        .getByName(NetworkUtils.getServerHostName(host)), locatorPort,
 +        new QueueConnectionRequest(null, 2,
 +            Collections.EMPTY_SET, null, false), 10000, true);
 +    Assert.assertEquals(Collections.singletonList(expectedLocation), response2.getServers());
 +    
 +    response2 = (QueueConnectionResponse) TcpClient
 +        .requestToServer(InetAddress.getByName(NetworkUtils.getServerHostName(host)),
 +            locatorPort, new QueueConnectionRequest(null, 5, Collections.EMPTY_SET, null,
 +                false), 10000, true);
 +    
 +    Assert.assertEquals(Collections.singletonList(expectedLocation), response2.getServers());
 +
 +    //we expect that the queue load will increase by 2
 +    expectedLoad.setSubscriptionConnectionLoad(2f);
 +    checkLocatorLoad(vm0, expected);
 +  }
 +  
 +  /**
 +   * Test to make sure the bridge servers communicate
 +   * their updated load to the controller when the load
 +   * on the bridge server changes.
++   * @throws Exception 
 +   */
-   public void testLoadMessaging() {
++  public void testLoadMessaging() throws Exception {
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    
 +    int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    startLocatorInVM(vm0, locatorPort, "");
 +    String locators = getLocatorString(host, locatorPort);
 +    
 +    final int serverPort = startBridgeServerInVM(vm1, new String[] {"a", "b"},  locators);
 +    
 +    //We expect 0 load
 +    Map expected = new HashMap();
 +    ServerLocation expectedLocation = new ServerLocation(NetworkUtils.getServerHostName(host), serverPort);
 +    ServerLoad expectedLoad = new ServerLoad(0f, 1 / 800.0f, 0f, 1f);
 +    expected.put(expectedLocation, expectedLoad);
 +    checkLocatorLoad(vm0, expected);
 +    
 +    PoolFactoryImpl pf = new PoolFactoryImpl(null);
 +    pf.addServer(NetworkUtils.getServerHostName(host), serverPort);
 +    pf.setMinConnections(8);
 +    pf.setMaxConnections(8);
 +    pf.setSubscriptionEnabled(true);
 +    startBridgeClientInVM(vm2, pf.getPoolAttributes(), new String[] {REGION_NAME});
 +    
 +    //We expect 8 client to server connections. The queue requires
 +    //an additional client to server connection, but that shouldn't show up here.
 +    expectedLoad = new ServerLoad(8/800f, 1 / 800.0f, 1f, 1f);
 +    expected.put(expectedLocation, expectedLoad);
 +    
 +    
 +    checkLocatorLoad(vm0, expected);
 +    
 +    stopBridgeMemberVM(vm2);
 +    
 +    //Now we expect 0 load
 +    expectedLoad = new ServerLoad(0f, 1 / 800.0f, 0f, 1f);
 +    expected.put(expectedLocation, expectedLoad);
 +    checkLocatorLoad(vm0, expected);
 +  }
 +  
 +  /**
 +   * Test to make sure that the locator
 +   * balancing load between two servers.
++   * @throws Exception 
 +   */
-   public void testBalancing() {
++  public void testBalancing() throws Exception {
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    VM vm3 = host.getVM(3);
 +    
 +    int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    startLocatorInVM(vm0, locatorPort, "");
 +    String locators = getLocatorString(host, locatorPort);
 +    
 +    startBridgeServerInVM(vm1, new String[] {"a", "b"},  locators);
 +    startBridgeServerInVM(vm2, new String[] {"a", "b"},  locators);
 +    
 +    PoolFactoryImpl pf = new PoolFactoryImpl(null);
 +    pf.addLocator(NetworkUtils.getServerHostName(host), locatorPort);
 +    pf.setMinConnections(80);
 +    pf.setMaxConnections(80);
 +    pf.setSubscriptionEnabled(false);
 +    pf.setIdleTimeout(-1);
 +    startBridgeClientInVM(vm3, pf.getPoolAttributes(), new String[] {REGION_NAME});
 +    
 +    waitForPrefilledConnections(vm3, 80);
 +    
 +    checkConnectionCount(vm1, 40);
 +    checkConnectionCount(vm2, 40);
 +  }
 +
 +  private void checkConnectionCount(VM vm, final int count) {
 +    SerializableRunnableIF checkConnectionCount = new SerializableRunnable("checkConnectionCount") {
 +      public void run() {
 +        Cache cache = (Cache) remoteObjects.get(CACHE_KEY);
 +        final CacheServerImpl server = (CacheServerImpl)
 +            cache.getCacheServers().get(0);
 +        WaitCriterion wc = new WaitCriterion() {
 +          String excuse;
 +          public boolean done() {
 +            int sz = server.getAcceptor().getStats()
 +                .getCurrentClientConnections();
 +            if (Math.abs(sz - count) <= ALLOWABLE_ERROR_IN_COUNT) {
 +              return true;
 +            }
 +            excuse = "Found " + sz + " connections, expected " + count;
 +            return false;
 +          }
 +          public String description() {
 +            return excuse;
 +          }
 +        };
 +        Wait.waitForCriterion(wc, 5 * 60 * 1000, 1000, true);
 +      }
 +    };
 +    
 +    vm.invoke(checkConnectionCount);
 +  }
 +  
-   private void waitForPrefilledConnections(VM vm, final int count) {
++  private void waitForPrefilledConnections(VM vm, final int count) throws Exception {
 +    waitForPrefilledConnections(vm, count, POOL_NAME);
 +  }
 +
-   private void waitForPrefilledConnections(VM vm, final int count, final String poolName) {
++  private void waitForPrefilledConnections(VM vm, final int count, final String poolName) throws Exception {
 +    SerializableRunnable runnable = new SerializableRunnable("waitForPrefilledConnections") {
 +      public void run() {
 +        final PoolImpl pool = (PoolImpl) PoolManager.getAll().get(poolName);
 +        WaitCriterion ev = new WaitCriterion() {
 +          public boolean done() {
 +            return pool.getConnectionCount() >= count; 
 +          }
 +          public String description() {
 +            return "connection count never reached " + count;
 +          }
 +        };
 +        Wait.waitForCriterion(ev, MAX_WAIT, 200, true);
 +      }
 +    };
 +    if(vm == null) {
 +      runnable.run();
 +    } else {
 +      vm.invoke(runnable);
 +    }
 +  }
 +  
 +  /** Test that the locator balances load between
 +   * three servers with intersecting server groups.
 +   * Server:    1       2       3
 +   * Groups:    a       a,b     b
++   * @throws Exception 
 +   */
-   public void testIntersectingServerGroups() {
++  public void testIntersectingServerGroups() throws Exception {
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    VM vm3 = host.getVM(3);
 +    
 +    int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    startLocatorInVM(vm0, locatorPort, "");
 +    String locators = getLocatorString(host, locatorPort);
 +    
 +    int serverPort1 = startBridgeServerInVM(vm1, new String[] {"a"},  locators);
 +    startBridgeServerInVM(vm2, new String[] {"a", "b"},  locators);
 +    startBridgeServerInVM(vm3, new String[] {"b"},  locators);
 +    
 +    PoolFactoryImpl pf = new PoolFactoryImpl(null);
 +    pf.addLocator(NetworkUtils.getServerHostName(host), locatorPort);
 +    pf.setMinConnections(12);
 +    pf.setSubscriptionEnabled(false);
 +    pf.setServerGroup("a");
 +    pf.setIdleTimeout(-1);
 +    startBridgeClientInVM(null, pf.getPoolAttributes(), new String[] {REGION_NAME});
 +    waitForPrefilledConnections(null, 12);
 +    
 +    checkConnectionCount(vm1, 6);
 +    checkConnectionCount(vm2, 6);
 +    checkConnectionCount(vm3, 0);
 +    
 +    LogWriterUtils.getLogWriter().info("pool1 prefilled");
 +    
 +    PoolFactoryImpl pf2 = (PoolFactoryImpl) PoolManager.createFactory();
 +    pf2.init(pf.getPoolAttributes());
 +    pf2.setServerGroup("b");
 +    PoolImpl pool2= (PoolImpl) pf2.create("testPool2");
 +    waitForPrefilledConnections(null, 12, "testPool2");
 +
 +    // The load will not be perfect, because we created all of the connections
 +    //for group A first.
 +    checkConnectionCount(vm1, 6);
 +    checkConnectionCount(vm2, 9);
 +    checkConnectionCount(vm3, 9);
 +    
 +    LogWriterUtils.getLogWriter().info("pool2 prefilled");
 +    
 +    ServerLocation location1 = new ServerLocation(NetworkUtils.getServerHostName(host), serverPort1);
 +    PoolImpl pool1 = (PoolImpl) PoolManager.getAll().get(POOL_NAME);
 +    Assert.assertEquals("a", pool1.getServerGroup());
 +    
 +    //Use up all of the pooled connections on pool1, and acquire 3 more
 +    for(int i = 0; i < 15; i++) {
 +      pool1.acquireConnection();
 +    }
 +    
 +    LogWriterUtils.getLogWriter().info("aquired 15 connections in pool1");
 +    
 +    //now the load should be equal
 +    checkConnectionCount(vm1, 9);
 +    checkConnectionCount(vm2, 9);
 +    checkConnectionCount(vm3, 9);
 +    
 +    //use up all of the pooled connections on pool2
 +    for(int i = 0; i < 12; i++) {
 +      pool2.acquireConnection();
 +    }
 +    
 +    LogWriterUtils.getLogWriter().info("aquired 12 connections in pool2");
 +    
 +    //interleave creating connections in both pools
 +    for(int i = 0; i < 6; i++) {
 +      pool1.acquireConnection();
 +      pool2.acquireConnection();
 +    }
 +    
 +    LogWriterUtils.getLogWriter().info("interleaved 6 connections from pool1 with 6 connections from pool2");
 +    
 +    //The load should still be balanced
 +    checkConnectionCount(vm1, 13);
 +    checkConnectionCount(vm2, 13);
 +    checkConnectionCount(vm3, 13);
 +    
 +  }
 +  
-   public void testCustomLoadProbe() {
++  public void testCustomLoadProbe() throws Exception {
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +//    VM vm3 = host.getVM(3);
 +    
 +    int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    startLocatorInVM(vm0, locatorPort, "");
 +    String locators = getLocatorString(host, locatorPort);
 +    
 +    ServerLoad load1= new ServerLoad(.3f, .01f, .44f, 4564f);
 +    ServerLoad load2= new ServerLoad(23.2f, 1.1f, 22.3f, .3f);
 +    int serverPort1 = startBridgeServerInVM(vm1, null, locators, new String[] {REGION_NAME}, new MyLoadProbe(load1 ));
 +    int serverPort2 = startBridgeServerInVM(vm2, null, locators, new String[] {REGION_NAME}, new MyLoadProbe(load2 ));
 +    
 +    HashMap expected = new HashMap();
 +    ServerLocation l1 = new ServerLocation(NetworkUtils.getServerHostName(host), serverPort1);
 +    ServerLocation l2 = new ServerLocation(NetworkUtils.getServerHostName(host), serverPort2);
 +    expected.put(l1, load1);
 +    expected.put(l2, load2);
 +    checkLocatorLoad(vm0, expected);
 +    
 +    load1.setConnectionLoad(25f);
 +    changeLoad(vm1, load1);
 +    load2.setSubscriptionConnectionLoad(3.5f);
 +    changeLoad(vm2, load2);
 +    checkLocatorLoad(vm0, expected);
 +    
 +    load1 = new ServerLoad(1f, .1f, 0f, 1f);
 +    load2 = new ServerLoad(2f, 5f, 0f, 2f);
 +    expected.put(l1, load1);
 +    expected.put(l2, load2);
 +    changeLoad(vm1, load1);
 +    changeLoad(vm2, load2);
 +    checkLocatorLoad(vm0, expected);
 +    
 +    PoolFactoryImpl pf = new PoolFactoryImpl(null);
 +    pf.addLocator(NetworkUtils.getServerHostName(host), locatorPort);
 +    pf.setMinConnections(20);
 +    pf.setSubscriptionEnabled(true);
 +    pf.setIdleTimeout(-1);
 +    startBridgeClientInVM(null, pf.getPoolAttributes(), new String[] {REGION_NAME});
 +    waitForPrefilledConnections(null, 20);
 +    
 +    //The first 10 connection should to go vm1, then 1 to vm2, then another 9 to vm1
 +    //because have unequal values for loadPerConnection
 +    checkConnectionCount(vm1, 19);
 +    checkConnectionCount(vm2, 1);
 +  }
 +  
 +  public void checkLocatorLoad(VM vm, final Map expected) {
 +    vm.invoke(new SerializableRunnable() {
 +      public void run() {
 +        List locators = Locator.getLocators();
 +        Assert.assertEquals(1, locators.size());
 +        InternalLocator locator = (InternalLocator) locators.get(0);
 +        final ServerLocator sl = locator.getServerLocatorAdvisee();
 +        InternalLogWriter log = new LocalLogWriter(InternalLogWriter.FINEST_LEVEL, System.out);
 +        sl.getDistributionAdvisor().dumpProfiles("PROFILES= ");
 +        WaitCriterion ev = new WaitCriterion() {
 +          public boolean done() {
 +            return expected.equals(sl.getLoadMap());
 +          }
 +          public String description() {
 +            return "load map never became equal to " + expected;
 +          }
 +        };
 +        Wait.waitForCriterion(ev, MAX_WAIT, 200, true);
 +      }
 +    });
 +  }
 +  
 +  private void changeLoad(VM vm, final ServerLoad newLoad) {
 +    vm.invoke(new SerializableRunnable() {
 +
 +      public void run() {
 +        Cache cache = (Cache) remoteObjects.get(CACHE_KEY);
 +        CacheServer server = (CacheServer) cache.getCacheServers().get(0);
 +        MyLoadProbe probe = (MyLoadProbe) server.getLoadProbe();
 +        probe.setLoad(newLoad);
 +      }
 +      
 +    });
 +  }
 +  
 +  private static class MyLoadProbe extends ServerLoadProbeAdapter implements Serializable {
 +    private ServerLoad load;
 +    
 +    public MyLoadProbe(ServerLoad load) {
 +      this.load = load;
 +    }
 +    
 +    public ServerLoad getLoad(ServerMetrics metrics) {
 +      float connectionLoad = load.getConnectionLoad()
 +          + metrics.getConnectionCount() * load.getLoadPerConnection();
 +      float queueLoad = load.getSubscriptionConnectionLoad() + metrics.getSubscriptionConnectionCount()
 +          * load.getLoadPerSubscriptionConnection();
 +      return new ServerLoad(connectionLoad, load.getLoadPerConnection(),
 +          queueLoad, load.getLoadPerSubscriptionConnection());
 +    }
 +    
 +    public void setLoad(ServerLoad load) {
 +      this.load = load;
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/LocatorTestBase.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/LocatorTestBase.java
index f35e2fa,0000000..a89d648
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/LocatorTestBase.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/LocatorTestBase.java
@@@ -1,365 -1,0 +1,365 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import java.io.File;
 +import java.io.IOException;
 +import java.net.InetAddress;
 +import java.net.InetSocketAddress;
 +import java.net.UnknownHostException;
 +import java.util.HashMap;
 +import java.util.HashSet;
 +import java.util.List;
 +import java.util.Properties;
 +import java.util.Set;
 +
 +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.RegionAttributes;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.client.Pool;
 +import com.gemstone.gemfire.cache.client.PoolManager;
 +import com.gemstone.gemfire.cache.server.ServerLoadProbe;
 +import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.distributed.DistributedSystem;
 +import com.gemstone.gemfire.distributed.Locator;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 +import com.gemstone.gemfire.internal.AvailablePortHelper;
 +import com.gemstone.gemfire.internal.cache.PoolFactoryImpl;
 +import com.gemstone.gemfire.test.dunit.Assert;
 +import com.gemstone.gemfire.test.dunit.DistributedTestCase;
 +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.NetworkUtils;
 +import com.gemstone.gemfire.test.dunit.SerializableCallable;
 +import com.gemstone.gemfire.test.dunit.SerializableRunnable;
 +import com.gemstone.gemfire.test.dunit.VM;
 +
 +/**
 + * @author dsmith
 + *
 + */
 +public abstract class LocatorTestBase  extends DistributedTestCase {
 +  protected static final String CACHE_KEY = "CACHE";
 +  protected static final String LOCATOR_KEY = "LOCATOR";
 +  protected static final String REGION_NAME = "A_REGION";
 +  protected static final String POOL_NAME = "daPool";
 +  protected static final Object CALLBACK_KEY = "callback";
 +  /** A map for storing temporary objects in a remote VM so that they can be used
 +   * between calls. Cleared after each test.
 +   */
 +  protected static final HashMap remoteObjects = new HashMap();
 +
 +  public LocatorTestBase(String name) {
 +    super(name);
 +  }
 +  
 +  @Override
 +  protected final void preTearDown() throws Exception {
 +    
 +    SerializableRunnable tearDown = new SerializableRunnable("tearDown") {
 +      public void run() {
 +        Locator locator = (Locator) remoteObjects.get(LOCATOR_KEY);
 +        if(locator != null) {
 +          try {
 +            locator.stop();
 +          } catch(Exception e) {
 +            //do nothing
 +          }
 +        }
 +        
 +        Cache cache = (Cache) remoteObjects.get(CACHE_KEY);
 +        if(cache != null) {
 +          try {
 +            cache.close();
 +          } catch(Exception e) {
 +            //do nothing
 +          }
 +        }
 +        remoteObjects.clear();
 +        
 +      }
 +    };
 +    //We seem to like leaving the DS open if we can for
 +    //speed, but lets at least destroy our cache and locator.
 +    Invoke.invokeInEveryVM(tearDown);
 +    tearDown.run();
 +    
 +    postTearDownLocatorTestBase();
 +  }
 +  
 +  protected void postTearDownLocatorTestBase() throws Exception {
 +  }
 +  
 +  protected void startLocatorInVM(final VM vm, final int locatorPort, final String otherLocators) {
 +    vm.invoke(new SerializableRunnable("Create Locator") {
 +
 +      final String testName= getUniqueName();
 +      public void run() {
 +        disconnectFromDS();
 +        Properties props = new Properties();
 +        props.setProperty(DistributionConfig.MCAST_PORT_NAME, String.valueOf(0));
 +        props.setProperty(DistributionConfig.LOCATORS_NAME, otherLocators);
 +        props.setProperty(DistributionConfig.LOG_LEVEL_NAME, LogWriterUtils.getDUnitLogLevel());
 +        props.setProperty(DistributionConfig.ENABLE_CLUSTER_CONFIGURATION_NAME, "false");
 +        try {
 +          File logFile = new File(testName + "-locator" + locatorPort
 +              + ".log");
 +          InetAddress bindAddr = null;
 +          try {
 +            bindAddr = InetAddress.getByName(NetworkUtils.getServerHostName(vm.getHost()));
 +          } catch (UnknownHostException uhe) {
 +            Assert.fail("While resolving bind address ", uhe);
 +          }
 +          Locator locator = Locator.startLocatorAndDS(locatorPort, logFile, bindAddr, props);
 +          remoteObjects.put(LOCATOR_KEY, locator);
 +        } catch (IOException ex) {
 +          Assert.fail("While starting locator on port " + locatorPort, ex);
 +        }
 +      }
 +    });
 +  }
 +  
 +  
 +  
 +  protected void stopLocatorInVM(VM vm) {
 +    vm.invoke(new SerializableRunnable("Stop Locator") {
 +      public void run() {
 +        Locator locator = (Locator) remoteObjects.remove(LOCATOR_KEY);
 +        locator.stop();
 +      }
 +    });
 +  }
 +  
 +  protected int startBridgeServerInVM(VM vm, String[] groups, String locators) {
 +    return startBridgeServerInVM(vm, groups, locators, new String[] {REGION_NAME});
 +  }
 +  
 +  protected int addCacheServerInVM(VM vm, final String[] groups) {
 +    SerializableCallable connect =
 +      new SerializableCallable("Add Bridge server") {
 +
 +      public Object call() throws Exception {
 +        Cache cache = (Cache) remoteObjects.get(CACHE_KEY);
 +        CacheServer server = cache.addCacheServer();
 +        final int serverPort = AvailablePortHelper.getRandomAvailableTCPPort();
 +        server.setPort(serverPort);
 +        server.setGroups(groups);
 +        server.start();
 +        return new Integer(serverPort);
 +      }
 +    };
 +    Integer port = (Integer) vm.invoke(connect);
 +    return port.intValue();
 +  }
 +  
 +  protected int startBridgeServerInVM(VM vm, final String[] groups, final String locators, final String[] regions) {
 +    return startBridgeServerInVM(vm, groups, locators, regions, CacheServer.DEFAULT_LOAD_PROBE);
 +  }
 +  
 +  protected int startBridgeServerInVM(VM vm, final String[] groups, final String locators, final String[] regions, final ServerLoadProbe probe) {
 +    SerializableCallable connect =
 +      new SerializableCallable("Start bridge server") {
 +          public Object call() throws IOException  {
 +            Properties props = new Properties();
 +            props.setProperty(DistributionConfig.MCAST_PORT_NAME, String.valueOf(0));
 +            props.setProperty(DistributionConfig.LOCATORS_NAME, locators);
 +            DistributedSystem ds = getSystem(props);
 +            Cache cache = CacheFactory.create(ds);
 +            AttributesFactory factory = new AttributesFactory();
 +            factory.setScope(Scope.DISTRIBUTED_ACK);
 +            factory.setEnableBridgeConflation(true);
 +            factory.setDataPolicy(DataPolicy.REPLICATE);
 +            RegionAttributes attrs = factory.create();
 +            for(int i = 0; i < regions.length; i++) {
 +              cache.createRegion(regions[i], attrs);
 +            }
 +            CacheServer server = cache.addCacheServer();
 +            final int serverPort = AvailablePortHelper.getRandomAvailableTCPPort();
 +            server.setPort(serverPort);
 +            server.setGroups(groups);
 +            server.setLoadProbe(probe);
 +            server.start();
 +            
 +            remoteObjects.put(CACHE_KEY, cache);
 +            
 +            return new Integer(serverPort);
 +          }
 +        };
 +    Integer port = (Integer) vm.invoke(connect);
 +    return port.intValue();
 +  }
 +  
 +  protected int startBridgeServerWithEmbeddedLocator(VM vm, final String[] groups, final String locators, final String[] regions, final ServerLoadProbe probe) {
 +    SerializableCallable connect =
 +      new SerializableCallable("Start bridge server") {
 +          public Object call() throws IOException  {
 +            Properties props = new Properties();
 +            props.setProperty(DistributionConfig.MCAST_PORT_NAME, String.valueOf(0));
 +            props.setProperty(DistributionConfig.START_LOCATOR_NAME, locators);
 +            props.setProperty(DistributionConfig.LOCATORS_NAME, locators);
 +            DistributedSystem ds = getSystem(props);
 +            Cache cache = CacheFactory.create(ds);
 +            AttributesFactory factory = new AttributesFactory();
 +            factory.setScope(Scope.DISTRIBUTED_ACK);
 +            factory.setEnableBridgeConflation(true);
 +            factory.setDataPolicy(DataPolicy.REPLICATE);
 +            RegionAttributes attrs = factory.create();
 +            for(int i = 0; i < regions.length; i++) {
 +              cache.createRegion(regions[i], attrs);
 +            }
 +            CacheServer server = cache.addCacheServer();
 +            server.setGroups(groups);
 +            server.setLoadProbe(probe);
 +            final int serverPort = AvailablePortHelper.getRandomAvailableTCPPort();
 +            server.setPort(serverPort);
 +            server.start();
 +            
 +            remoteObjects.put(CACHE_KEY, cache);
 +            
 +            return new Integer(serverPort);
 +          }
 +        };
 +    Integer port = (Integer) vm.invoke(connect);
 +    return port.intValue();
 +  }
 +  
-   protected void  startBridgeClientInVM(VM vm, final String group, final String host, final int port) {
++  protected void  startBridgeClientInVM(VM vm, final String group, final String host, final int port) throws Exception {
 +    startBridgeClientInVM(vm, group, host, port, new String[] {REGION_NAME});
 +  }
 +  
 +
-   protected void startBridgeClientInVM(VM vm, final String group, final String host, final int port, final String[] regions) {
++  protected void startBridgeClientInVM(VM vm, final String group, final String host, final int port, final String[] regions) throws Exception {
 +    PoolFactoryImpl pf = new PoolFactoryImpl(null);
 +    pf.addLocator(host, port)
 +    .setServerGroup(group)
 +    .setPingInterval(200)
 +    .setSubscriptionEnabled(true)
 +    .setSubscriptionRedundancy(-1);
 +    startBridgeClientInVM(vm, pf.getPoolAttributes(), regions);
 +  }
 +  
-   protected void  startBridgeClientInVM(VM vm, final Pool pool, final String[] regions) {
++  protected void  startBridgeClientInVM(VM vm, final Pool pool, final String[] regions) throws Exception {
 +    SerializableRunnable connect =
 +      new SerializableRunnable("Start bridge client") {
 +          public void run() {
 +            Properties props = new Properties();
 +            props.setProperty(DistributionConfig.MCAST_PORT_NAME, String.valueOf(0));
 +            props.setProperty(DistributionConfig.LOCATORS_NAME, "");
 +            DistributedSystem ds = getSystem(props);
 +            Cache cache = CacheFactory.create(ds);
 +            AttributesFactory factory = new AttributesFactory();
 +            factory.setScope(Scope.LOCAL);
 +//            factory.setEnableBridgeConflation(true);
 +//            factory.setDataPolicy(DataPolicy.NORMAL);
 +            factory.setPoolName(POOL_NAME);
 +            PoolFactoryImpl pf= (PoolFactoryImpl) PoolManager.createFactory();
 +            pf.init(pool);
 +            LocatorDiscoveryCallback locatorCallback = new MyLocatorCallback();
 +            remoteObjects.put(CALLBACK_KEY, locatorCallback);
 +            pf.setLocatorDiscoveryCallback(locatorCallback);
 +            pf.create(POOL_NAME);
 +
 +
 +            RegionAttributes attrs = factory.create();
 +            for(int i = 0; i < regions.length; i++) {
 +              cache.createRegion(regions[i], attrs);
 +            }
 +            
 +            remoteObjects.put(CACHE_KEY, cache);
 +          }
 +    };
 +    
 +    if(vm == null) {
 +      connect.run();
 +    } else {
 +      vm.invoke(connect);
 +    }
 +  }
 +  
 +  protected void stopBridgeMemberVM(VM vm) {
 +   vm.invoke(new SerializableRunnable("Stop bridge member") {
 +    public void run() {
 +      Cache cache = (Cache) remoteObjects.remove(CACHE_KEY);
 +      cache.close();
 +      disconnectFromDS();
 +    }
 +   });
 +  }
 +  
 +  public String getLocatorString(Host host, int locatorPort) {
 +    return getLocatorString(host, new int[] {locatorPort});
 +  }
 +  
 +  public String getLocatorString(Host host, int[] locatorPorts) {
 +    StringBuffer str = new StringBuffer();
 +    for(int i = 0; i < locatorPorts.length; i++) {
 +      str.append(NetworkUtils.getServerHostName(host))
 +          .append("[")
 +          .append(locatorPorts[i])
 +          .append("]");
 +      if(i < locatorPorts.length - 1) {
 +        str.append(",");
 +      }
 +    }
 +    
 +    return str.toString();
 +  }
 +  
 +  protected static class MyLocatorCallback extends LocatorDiscoveryCallbackAdapter {
 +
 +    private final Set discoveredLocators = new HashSet();
 +    private final Set removedLocators = new HashSet();
 +    
 +    public synchronized void locatorsDiscovered(List locators) {
 +      discoveredLocators.addAll(locators);
 +      notifyAll();
 +    }
 +
 +    public synchronized void locatorsRemoved(List locators) {
 +      removedLocators.addAll(locators);
 +      notifyAll();
 +    }
 +    
 +    public boolean waitForDiscovery(InetSocketAddress locator, long time) throws InterruptedException {
 +      return waitFor(discoveredLocators, locator, time);
 +    }
 +    
 +    public boolean waitForRemove(InetSocketAddress locator, long time) throws InterruptedException {
 +      return waitFor(removedLocators, locator, time);
 +    }
 +    
 +    private synchronized boolean waitFor(Set set, InetSocketAddress locator, long time) throws InterruptedException {
 +      long remaining = time;
 +      long endTime = System.currentTimeMillis() + time;
 +      while(!set.contains(locator) && remaining >= 0) {
 +        wait(remaining);
 +        remaining = endTime - System.currentTimeMillis(); 
 +      }
 +      return set.contains(locator);
 +    }
 +    
 +    public synchronized Set getDiscovered() {
 +      return new HashSet(discoveredLocators);
 +    }
 +    
 +  }
 +
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/SSLNoClientAuthDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/SSLNoClientAuthDUnitTest.java
index bb1cc09,0000000..e23fb76
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/SSLNoClientAuthDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/SSLNoClientAuthDUnitTest.java
@@@ -1,277 -1,0 +1,277 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import java.io.IOException;
 +import java.io.PrintWriter;
 +import java.io.StringWriter;
 +import java.util.Properties;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheFactory;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionFactory;
 +import com.gemstone.gemfire.cache.RegionShortcut;
 +import com.gemstone.gemfire.cache.client.ClientCache;
 +import com.gemstone.gemfire.cache.client.ClientCacheFactory;
 +import com.gemstone.gemfire.cache.client.ClientRegionFactory;
 +import com.gemstone.gemfire.cache.client.ClientRegionShortcut;
 +import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 +import com.gemstone.gemfire.internal.AvailablePortHelper;
 +import com.gemstone.gemfire.security.AuthenticationRequiredException;
 +import com.gemstone.gemfire.test.dunit.DistributedTestCase;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.VM;
 +import com.gemstone.gemfire.util.test.TestUtil;
 +
 +/**
 + * Test for GEODE-396
 + */
 +public class SSLNoClientAuthDUnitTest extends DistributedTestCase {
 +  
 +  private static final long serialVersionUID = 1L;
 +  private Cache cache;
 +  private CacheServer cacheServer;
 +  private ClientCache clientCache;
 +  private int cacheServerPort;
 +  private String hostName;
 +  
 +  private static final String DEFAULT_STORE = "default.keystore";
 +  
 +  private static SSLNoClientAuthDUnitTest instance = new SSLNoClientAuthDUnitTest("SSLNoClientAuthDUnitTest");
 +  
 +  
 +  public void setUp() throws Exception {
 +    disconnectAllFromDS();
 +    super.setUp();
 +  }
 +
 +  public SSLNoClientAuthDUnitTest(String name) {
 +    super(name);
 +  }  
 +
 +  public Cache createCache(Properties props) throws Exception
 +  {
 +    props.setProperty("mcast-port", "0");
 +    props.setProperty("locators", "");
 +    cache = new CacheFactory(props).create();
 +    if (cache == null) {
 +      throw new Exception("CacheFactory.create() returned null ");
 +    }
 +    return cache;
 +  }
 +  
 +  private void createServer() throws IOException{
 +    cacheServerPort = AvailablePortHelper.getRandomAvailableTCPPort();
 +    cacheServer = cache.addCacheServer();
 +    cacheServer.setPort(cacheServerPort);
 +    cacheServer.start();
 +    hostName = cacheServer.getHostnameForClients();
 +  }
 +  
 +  public int getCacheServerPort(){
 +    return cacheServerPort;
 +  }
 +  
 +  public String getCacheServerHost(){
 +    return hostName;
 +  }
 +  
 +  public void stopCacheServer(){
 +    this.cacheServer.stop();
 +  }
 +  
 +  
 +  @SuppressWarnings("rawtypes")
 +  public void setUpServerVM(boolean cacheServerSslenabled) throws Exception {
 +    Properties gemFireProps = new Properties();
 +
 +    String cacheServerSslprotocols = "any";
 +    String cacheServerSslciphers = "any";
 +    boolean cacheServerSslRequireAuth = false;
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_ENABLED_NAME,
 +        String.valueOf(cacheServerSslenabled));
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_PROTOCOLS_NAME,
 +        cacheServerSslprotocols);
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_CIPHERS_NAME,
 +        cacheServerSslciphers);
 +    gemFireProps.put(
 +        DistributionConfig.SERVER_SSL_REQUIRE_AUTHENTICATION_NAME,
 +        String.valueOf(cacheServerSslRequireAuth));
 +
 +    String keyStore = TestUtil.getResourcePath(SSLNoClientAuthDUnitTest.class, DEFAULT_STORE);
 +    String trustStore = TestUtil.getResourcePath(SSLNoClientAuthDUnitTest.class, DEFAULT_STORE);
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_KEYSTORE_TYPE_NAME, "jks");
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_KEYSTORE_NAME, keyStore);
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_KEYSTORE_PASSWORD_NAME, "password");
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_TRUSTSTORE_NAME, trustStore);
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_TRUSTSTORE_PASSWORD_NAME, "password");
 +    
 +    StringWriter sw = new StringWriter();
 +    PrintWriter writer = new PrintWriter(sw);
 +    gemFireProps.list(writer);
 +    System.out.println("Starting cacheserver ds with following properties \n" + sw);
 +    createCache(gemFireProps);
 +    
 +    RegionFactory factory = cache.createRegionFactory(RegionShortcut.REPLICATE);
 +    Region r = factory.create("serverRegion");
 +    r.put("serverkey", "servervalue");
 +  }
 +  
 +  public void setUpClientVM(String host, int port,
 +      boolean cacheServerSslenabled, boolean cacheServerSslRequireAuth,
 +      String keyStore, String trustStore) {
 +
 +    Properties gemFireProps = new Properties();
 +
 +    String cacheServerSslprotocols = "any";
 +    String cacheServerSslciphers = "any";
 +
 +    String keyStorePath = TestUtil.getResourcePath(SSLNoClientAuthDUnitTest.class, keyStore);
 +    String trustStorePath = TestUtil.getResourcePath(SSLNoClientAuthDUnitTest.class, trustStore);
 +    //using new server-ssl-* properties
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_ENABLED_NAME,
 +        String.valueOf(cacheServerSslenabled));
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_PROTOCOLS_NAME,
 +        cacheServerSslprotocols);
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_CIPHERS_NAME,
 +        cacheServerSslciphers);
 +    gemFireProps.put(
 +        DistributionConfig.SERVER_SSL_REQUIRE_AUTHENTICATION_NAME,
 +        String.valueOf(cacheServerSslRequireAuth));
 +
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_KEYSTORE_TYPE_NAME, "jks");
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_KEYSTORE_NAME, keyStorePath);
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_KEYSTORE_PASSWORD_NAME, "password");
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_TRUSTSTORE_NAME, trustStorePath);
 +    gemFireProps.put(DistributionConfig.SERVER_SSL_TRUSTSTORE_PASSWORD_NAME, "password");
 +
 +    StringWriter sw = new StringWriter();
 +    PrintWriter writer = new PrintWriter(sw);
 +    gemFireProps.list(writer);
 +    System.out.println("Starting client ds with following properties \n" + sw.getBuffer());
 +    
 +    ClientCacheFactory clientCacheFactory = new ClientCacheFactory(gemFireProps);
 +    clientCacheFactory.addPoolServer(host, port);
 +    clientCache = clientCacheFactory.create();
 +    
 +    ClientRegionFactory<String,String> regionFactory = clientCache.createClientRegionFactory(ClientRegionShortcut.PROXY);
 +    Region<String, String> region = regionFactory.create("serverRegion");  
 +    assertNotNull(region);
 +  }
 +  
 +  public void doClientRegionTest(){
 +    Region<String, String> region = clientCache.getRegion("serverRegion");
 +    assertEquals("servervalue",region.get("serverkey"));
 +    region.put("clientkey", "clientvalue");
 +    assertEquals("clientvalue",region.get("clientkey"));
 +  }
 +  
 +  public void doServerRegionTest(){
 +    Region<String, String> region = cache.getRegion("serverRegion");
 +    assertEquals("servervalue",region.get("serverkey"));    
 +    assertEquals("clientvalue",region.get("clientkey"));
 +  }
 +  
 +  
 +  public static void setUpServerVMTask(boolean cacheServerSslenabled) throws Exception{
 +    instance.setUpServerVM(cacheServerSslenabled);
 +  }
 +  
 +  public static void createServerTask() throws Exception {
 +    instance.createServer();
 +  }
 +  
 +  public static void setUpClientVMTask(String host, int port,
 +      boolean cacheServerSslenabled, boolean cacheServerSslRequireAuth, String keyStore, String trustStore)
 +      throws Exception {
 +    instance.setUpClientVM(host, port, cacheServerSslenabled, cacheServerSslRequireAuth, keyStore, trustStore);
 +  }
 +  
 +  public static void doClientRegionTestTask() {
 +    instance.doClientRegionTest();
 +  }
 +  
 +  public static void doServerRegionTestTask() {
 +    instance.doServerRegionTest();
 +  }
 +  
 +  public static Object[] getCacheServerEndPointTask() {
 +    Object[] array = new Object[2];
 +    array[0] = instance.getCacheServerHost();
 +    array[1] = instance.getCacheServerPort();
 +    return array;
 +  }
 +  
 +  public static void closeCacheTask(){
 +    if (instance != null && instance.cache != null) {
 +      instance.cache.close();
 +    }
 +  }
 +  
 +  public static void closeClientCacheTask(){
 +    if (instance != null && instance.clientCache != null) {
 +      instance.clientCache.close();
 +    }
 +  }
 +  
 +  /**
 +   * Test for GEODE-396
 +   */
 +  public void testSSLServerWithNoAuth() throws Exception {
 +    final Host host = Host.getHost(0);
 +    VM serverVM = host.getVM(1);
 +    VM clientVM = host.getVM(2);
 +
 +    boolean cacheServerSslenabled = true;
 +    boolean cacheClientSslenabled = true;
 +    boolean cacheClientSslRequireAuth = true;
 +
-     serverVM.invoke(SSLNoClientAuthDUnitTest.class, "setUpServerVMTask", new Object[]{cacheServerSslenabled});
-     serverVM.invoke(SSLNoClientAuthDUnitTest.class, "createServerTask");
++    serverVM.invoke(() -> SSLNoClientAuthDUnitTest.setUpServerVMTask(cacheServerSslenabled));
++    serverVM.invoke(() -> SSLNoClientAuthDUnitTest.createServerTask());
 +
-     Object array[] = (Object[])serverVM.invoke(SSLNoClientAuthDUnitTest.class, "getCacheServerEndPointTask"); 
++    Object array[] = (Object[])serverVM.invoke(() -> SSLNoClientAuthDUnitTest.getCacheServerEndPointTask()); 
 +    String hostName = (String)array[0];
 +    int port = (Integer) array[1];
 +    Object params[] = new Object[6];
 +    params[0] = hostName;
 +    params[1] = port;
 +    params[2] = cacheClientSslenabled;
 +    params[3] = cacheClientSslRequireAuth;
 +    params[4] = DEFAULT_STORE;
 +    params[5] = DEFAULT_STORE;
 +    //getLogWriter().info("Starting client with server endpoint " + hostName + ":" + port);
 +    try {
 +      clientVM.invoke(SSLNoClientAuthDUnitTest.class, "setUpClientVMTask", params);
-       clientVM.invoke(SSLNoClientAuthDUnitTest.class, "doClientRegionTestTask");
-       serverVM.invoke(SSLNoClientAuthDUnitTest.class, "doServerRegionTestTask");
++      clientVM.invoke(() -> SSLNoClientAuthDUnitTest.doClientRegionTestTask());
++      serverVM.invoke(() -> SSLNoClientAuthDUnitTest.doServerRegionTestTask());
 +    } catch (Exception rmiException) {
 +      Throwable e = rmiException.getCause();
 +      //getLogWriter().info("ExceptionCause at clientVM " + e);
 +      fail("Unexpected Exception " + e);
 +    }
 +  }
 +  
 +  @Override
 +  protected final void preTearDown() throws Exception {
 +    final Host host = Host.getHost(0);
 +    VM serverVM = host.getVM(1);
 +    VM clientVM = host.getVM(2);
-     clientVM.invoke(SSLNoClientAuthDUnitTest.class, "closeClientCacheTask");
-     serverVM.invoke(SSLNoClientAuthDUnitTest.class, "closeCacheTask");
++    clientVM.invoke(() -> SSLNoClientAuthDUnitTest.closeClientCacheTask());
++    serverVM.invoke(() -> SSLNoClientAuthDUnitTest.closeCacheTask());
 +  }
 +}


[05/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/ReconnectDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/ReconnectDUnitTest.java
index 0d80574,0000000..70c7171
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/ReconnectDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/ReconnectDUnitTest.java
@@@ -1,1173 -1,0 +1,1173 @@@
 +/*
 + * 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.cache30;
 +
 +import java.io.File;
 +import java.io.FileWriter;
 +import java.io.IOException;
 +import java.io.PrintWriter;
 +import java.util.Iterator;
 +import java.util.Properties;
 +import java.util.Set;
 +import java.util.concurrent.TimeUnit;
 +
 +import com.gemstone.gemfire.CancelException;
 +import com.gemstone.gemfire.SystemFailure;
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheFactory;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.LossAction;
 +import com.gemstone.gemfire.cache.MembershipAttributes;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionAttributes;
 +import com.gemstone.gemfire.cache.RegionDestroyedException;
 +import com.gemstone.gemfire.cache.RegionExistsException;
 +import com.gemstone.gemfire.cache.ResumptionAction;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.TimeoutException;
 +import com.gemstone.gemfire.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.DistributedSystem;
 +import com.gemstone.gemfire.distributed.Locator;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem.ReconnectListener;
 +import com.gemstone.gemfire.distributed.internal.InternalLocator;
 +import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
 +import com.gemstone.gemfire.distributed.internal.membership.gms.MembershipManagerHelper;
 +import com.gemstone.gemfire.distributed.internal.membership.gms.mgr.GMSMembershipManager;
 +import com.gemstone.gemfire.internal.AvailablePort;
 +import com.gemstone.gemfire.internal.AvailablePortHelper;
 +import com.gemstone.gemfire.internal.cache.xmlcache.CacheXmlGenerator;
 +import com.gemstone.gemfire.test.dunit.Assert;
 +import com.gemstone.gemfire.test.dunit.AsyncInvocation;
 +import com.gemstone.gemfire.test.dunit.DistributedTestCase;
 +import com.gemstone.gemfire.test.dunit.DistributedTestUtils;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.IgnoredException;
 +import com.gemstone.gemfire.test.dunit.Invoke;
 +import com.gemstone.gemfire.test.dunit.LogWriterUtils;
 +import com.gemstone.gemfire.test.dunit.SerializableCallable;
 +import com.gemstone.gemfire.test.dunit.SerializableRunnable;
 +import com.gemstone.gemfire.test.dunit.ThreadUtils;
 +import com.gemstone.gemfire.test.dunit.VM;
 +import com.gemstone.gemfire.test.dunit.Wait;
 +import com.gemstone.gemfire.test.dunit.WaitCriterion;
 +
 +@SuppressWarnings("serial")
 +public class ReconnectDUnitTest extends CacheTestCase
 +{
 +  static int locatorPort;
 +  static Locator locator;
 +  static DistributedSystem savedSystem;
 +  static int locatorVMNumber = 3;
 +  static Thread gfshThread;
 +  
 +  Properties dsProperties;
 +  
 +  public ReconnectDUnitTest(String name) {
 +    super(name);
 +  }
 +  
 +  @Override
 +  public void setUp() throws Exception {
 +    super.setUp();
 +    this.locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    final int locPort = this.locatorPort;
 +    Host.getHost(0).getVM(locatorVMNumber)
 +      .invoke(new SerializableRunnable("start locator") {
 +      public void run() {
 +        try {
 +          InternalDistributedSystem ds = InternalDistributedSystem.getConnectedInstance();
 +          if (ds != null) {
 +            ds.disconnect();
 +          }
 +          locatorPort = locPort;
 +          Properties props = getDistributedSystemProperties();
 +          locator = Locator.startLocatorAndDS(locatorPort, new File(""), props);
 +          IgnoredException.addIgnoredException("com.gemstone.gemfire.ForcedDisconnectException||Possible loss of quorum");
 +//          MembershipManagerHelper.getMembershipManager(InternalDistributedSystem.getConnectedInstance()).setDebugJGroups(true);
 +        } catch (IOException e) {
 +          Assert.fail("unable to start locator", e);
 +        }
 +      }
 +    });
 +
 +    beginCacheXml();
 +    createRegion("myRegion", createAtts());
 +    finishCacheXml("MyDisconnect");
 +    //Cache cache = getCache();
 +    closeCache();
 +    getSystem().disconnect();
 +    LogWriterUtils.getLogWriter().fine("Cache Closed ");
 +  }
 +
 +  @Override
 +  public Properties getDistributedSystemProperties() {
 +    if (dsProperties == null) {
 +      dsProperties = super.getDistributedSystemProperties();
 +      dsProperties.put(DistributionConfig.MAX_WAIT_TIME_FOR_RECONNECT_NAME, "20000");
 +      dsProperties.put(DistributionConfig.ENABLE_NETWORK_PARTITION_DETECTION_NAME, "true");
 +      dsProperties.put(DistributionConfig.DISABLE_AUTO_RECONNECT_NAME, "false");
 +      dsProperties.put(DistributionConfig.LOCATORS_NAME, "localHost["+this.locatorPort+"]");
 +      dsProperties.put(DistributionConfig.MCAST_PORT_NAME, "0");
 +      dsProperties.put(DistributionConfig.MEMBER_TIMEOUT_NAME, "1000");
 +      dsProperties.put(DistributionConfig.LOG_LEVEL_NAME, LogWriterUtils.getDUnitLogLevel());
 +    }
 +    return dsProperties;
 +  }
 +  
 +  @Override
 +  protected final void postTearDownCacheTestCase() throws Exception {
 +    try {
 +      Host.getHost(0).getVM(locatorVMNumber).invoke(new SerializableRunnable("stop locator") {
 +        public void run() {
 +          if (locator != null) {
 +            LogWriterUtils.getLogWriter().info("stopping locator " + locator);
 +            locator.stop();
 +          }
 +        }
 +      });
 +    } finally {
 +      Invoke.invokeInEveryVM(new SerializableRunnable() {
 +        public void run() {
 +          ReconnectDUnitTest.savedSystem = null;
 +        }
 +      });
 +      disconnectAllFromDS();
 +    }
 +  }
 +
 +  /**
 +   * Creates some region attributes for the regions being created.
 +   * */
 +  private RegionAttributes createAtts()
 +  {
 +    AttributesFactory factory = new AttributesFactory();
 +
 +    {
 +      // TestCacheListener listener = new TestCacheListener(){}; // this needs to be serializable
 +      //callbacks.add(listener);
 +      //factory.setDataPolicy(DataPolicy.REPLICATE);
 +      factory.setDataPolicy(DataPolicy.REPLICATE);
 +      factory.setScope(Scope.DISTRIBUTED_ACK);
 +      // factory.setCacheListener(listener);
 +    }
 +
 +    return factory.create();
 +  }
 +
 +  // quorum check fails, then succeeds
 +  public void testReconnectWithQuorum() throws Exception {
 +    IgnoredException.addIgnoredException("killing member's ds");
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    
 +    final int locPort = locatorPort;
 +
 +    final String xmlFileLoc = (new File(".")).getAbsolutePath();
 +    
 +    // disable disconnects in the locator so we have some stability
 +    host.getVM(locatorVMNumber).invoke(new SerializableRunnable("disable force-disconnect") {
 +      public void run() {
 +        GMSMembershipManager mgr = (GMSMembershipManager)MembershipManagerHelper
 +            .getMembershipManager(InternalDistributedSystem.getConnectedInstance());
 +        mgr.disableDisconnectOnQuorumLossForTesting();
 +      }}
 +    );
 +
 +    SerializableCallable create = new SerializableCallable(
 +    "Create Cache and Regions from cache.xml") {
 +      public Object call() throws CacheException
 +      {
 +        //      DebuggerSupport.waitForJavaDebugger(getLogWriter(), " about to create region");
 +        locatorPort = locPort;
 +        Properties props = getDistributedSystemProperties();
 +        props.put("cache-xml-file", xmlFileLoc+"/MyDisconnect-cache.xml");
 +        props.put("max-num-reconnect-tries", "2");
 +        props.put("log-file", "autoReconnectVM"+VM.getCurrentVMNum()+"_"+getPID()+".log");
 +        Cache cache = new CacheFactory(props).create();
 +        IgnoredException.addIgnoredException("com.gemstone.gemfire.ForcedDisconnectException||Possible loss of quorum");
 +        Region myRegion = cache.getRegion("root/myRegion");
 +        ReconnectDUnitTest.savedSystem = cache.getDistributedSystem();
 +        myRegion.put("MyKey1", "MyValue1");
 +//        MembershipManagerHelper.getMembershipManager(cache.getDistributedSystem()).setDebugJGroups(true); 
 +        // myRegion.put("Mykey2", "MyValue2");
 +        return savedSystem.getDistributedMember();
 +      }
 +    };
 +    
 +    System.out.println("creating caches in vm0, vm1 and vm2");
 +    vm0.invoke(create);
 +    vm1.invoke(create);
 +    vm2.invoke(create);
 +    
 +    // view is [locator(3), vm0(15), vm1(10), vm2(10)]
 +    
 +    /* now we want to cause vm0 and vm1 to force-disconnect.  This may cause the other
 +     * non-locator member to also disconnect, depending on the timing
 +     */
 +    System.out.println("disconnecting vm0");
 +    forceDisconnect(vm0);
 +    Wait.pause(10000);
 +    System.out.println("disconnecting vm1");
 +    forceDisconnect(vm1);
 +
 +    /* now we wait for them to auto-reconnect */
 +    try {
 +      System.out.println("waiting for vm0 to reconnect");
 +      waitForReconnect(vm0);
 +      System.out.println("waiting for vm1 to reconnect");
 +      waitForReconnect(vm1);
 +      System.out.println("done reconnecting vm0 and vm1");
 +    } catch (Exception e) {
 +      ThreadUtils.dumpAllStacks();
 +      throw e;
 +    }
 +  }
 +  
 +  public void testReconnectOnForcedDisconnect() throws Exception  {
 +    doTestReconnectOnForcedDisconnect(false);
 +  }
 +  
 +  /** bug #51335 - customer is also trying to recreate the cache */
 +  // this test is disabled due to a high failure rate during CI test runs.
 +  // see bug #52160
 +  public void disabledtestReconnectCollidesWithApplication() throws Exception  {
 +    doTestReconnectOnForcedDisconnect(true);
 +  }
 +  
 +  public void doTestReconnectOnForcedDisconnect(final boolean createInAppToo) throws Exception {
 +
 +    IgnoredException.addIgnoredException("killing member's ds");
 +//    getSystem().disconnect();
 +//    getLogWriter().fine("Cache Closed ");
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    
 +    final int locPort = locatorPort;
 +    final int secondLocPort = AvailablePortHelper.getRandomAvailableTCPPort();
 +
 +    DistributedTestUtils.deleteLocatorStateFile(locPort, secondLocPort);
 +
 +
 +    final String xmlFileLoc = (new File(".")).getAbsolutePath();
 +
 +    SerializableCallable create1 = new SerializableCallable(
 +    "Create Cache and Regions from cache.xml") {
 +      public Object call() throws CacheException
 +      {
 +        //      DebuggerSupport.waitForJavaDebugger(getLogWriter(), " about to create region");
 +        locatorPort = locPort;
 +        Properties props = getDistributedSystemProperties();
 +        props.put("cache-xml-file", xmlFileLoc+"/MyDisconnect-cache.xml");
 +        props.put("max-wait-time-reconnect", "1000");
 +        props.put("max-num-reconnect-tries", "2");
 +//        props.put("log-file", "autoReconnectVM"+VM.getCurrentVMNum()+"_"+getPID()+".log");
 +        Cache cache = new CacheFactory(props).create();
 +        Region myRegion = cache.getRegion("root/myRegion");
 +        ReconnectDUnitTest.savedSystem = cache.getDistributedSystem();
 +        myRegion.put("MyKey1", "MyValue1");
 +        // myRegion.put("Mykey2", "MyValue2");
 +        return savedSystem.getDistributedMember();
 +      }
 +    };
 +
 +    SerializableCallable create2 = new SerializableCallable(
 +    "Create Cache and Regions from cache.xml") {
 +      public Object call() throws CacheException
 +      {
 +        //            DebuggerSupport.waitForJavaDebugger(getLogWriter(), " about to create region");
 +        locatorPort = locPort;
 +        final Properties props = getDistributedSystemProperties();
 +        props.put("cache-xml-file", xmlFileLoc+"/MyDisconnect-cache.xml");
 +        props.put("max-wait-time-reconnect", "5000");
 +        props.put("max-num-reconnect-tries", "2");
 +        props.put("start-locator", "localhost["+secondLocPort+"]");
 +        props.put("locators", props.get("locators")+",localhost["+secondLocPort+"]");
 +//        props.put("log-file", "autoReconnectVM"+VM.getCurrentVMNum()+"_"+getPID()+".log");
 +        getSystem(props);
 +//        MembershipManagerHelper.getMembershipManager(system).setDebugJGroups(true);
 +        final Cache cache = getCache();
 +        ReconnectDUnitTest.savedSystem = cache.getDistributedSystem();
 +        Region myRegion = cache.getRegion("root/myRegion");
 +        //myRegion.put("MyKey1", "MyValue1");
 +        myRegion.put("Mykey2", "MyValue2");
 +        assertNotNull(myRegion.get("MyKey1"));
 +        //getLogWriter().fine("MyKey1 value is : "+myRegion.get("MyKey1"));
 +        if (createInAppToo) {
 +          Thread recreateCacheThread = new Thread("ReconnectDUnitTest.createInAppThread") {
 +            public void run() {
 +              while (!cache.isClosed()) {
 +                Wait.pause(100);
 +              }
 +              try {
 +                new CacheFactory(props).create();
 +                LogWriterUtils.getLogWriter().error("testReconnectCollidesWithApplication failed - application thread was able to create a cache");
 +              } catch (IllegalStateException cacheExists) {
 +                // expected
 +              }
 +            }
 +          };
 +          recreateCacheThread.setDaemon(true);
 +          recreateCacheThread.start();
 +        }
 +        return cache.getDistributedSystem().getDistributedMember();
 +      }
 +    };
 +
 +    vm0.invoke(create1);
 +    DistributedMember dm = (DistributedMember)vm1.invoke(create2);
 +    forceDisconnect(vm1);
 +    DistributedMember newdm = (DistributedMember)vm1.invoke(new SerializableCallable("wait for reconnect(1)") {
 +      public Object call() {
 +        final DistributedSystem ds = ReconnectDUnitTest.savedSystem;
 +        ReconnectDUnitTest.savedSystem = null;
 +        Wait.waitForCriterion(new WaitCriterion() {
 +          public boolean done() {
 +            return ds.isReconnecting();
 +          }
 +          public String description() {
 +            return "waiting for ds to begin reconnecting";
 +          }
 +        }, 30000, 1000, true);
 +        LogWriterUtils.getLogWriter().info("entering reconnect wait for " + ds);
 +        LogWriterUtils.getLogWriter().info("ds.isReconnecting() = " + ds.isReconnecting());
 +        boolean failure = true;
 +        try {
 +          ds.waitUntilReconnected(60, TimeUnit.SECONDS);
 +          ReconnectDUnitTest.savedSystem = ds.getReconnectedSystem();
 +          InternalLocator locator = (InternalLocator)Locator.getLocator();
 +          assertTrue("Expected system to be restarted", ds.getReconnectedSystem() != null);
 +          assertTrue("Expected system to be running", ds.getReconnectedSystem().isConnected());
 +          assertTrue("Expected there to be a locator", locator != null);
 +          assertTrue("Expected locator to be restarted", !locator.isStopped());
 +          failure = false;
 +          return ds.getReconnectedSystem().getDistributedMember();
 +        } catch (InterruptedException e) {
 +          LogWriterUtils.getLogWriter().warning("interrupted while waiting for reconnect");
 +          return null;
 +        } finally {
 +          if (failure) {
 +            ds.disconnect();
 +          }
 +        }
 +      }
 +    });
 +    assertNotSame(dm, newdm);
 +    // force another reconnect and show that stopReconnecting works
 +    forceDisconnect(vm1);
 +    boolean stopped = (Boolean)vm1.invoke(new SerializableCallable("wait for reconnect and stop") {
 +      public Object call() {
 +        final DistributedSystem ds = ReconnectDUnitTest.savedSystem;
 +        ReconnectDUnitTest.savedSystem = null;
 +        Wait.waitForCriterion(new WaitCriterion() {
 +          public boolean done() {
 +            return ds.isReconnecting() || ds.getReconnectedSystem() != null;
 +          }
 +          public String description() {
 +            return "waiting for reconnect to commence in " + ds;
 +          }
 +          
 +        }, 10000, 1000, true);
 +        ds.stopReconnecting();
 +        assertFalse(ds.isReconnecting());
 +        DistributedSystem newDs = InternalDistributedSystem.getAnyInstance();
 +        if (newDs != null) {
 +          LogWriterUtils.getLogWriter().warning("expected distributed system to be disconnected: " + newDs);
 +          return false;
 +        }
 +        return true;
 +      }
 +    });
 +    assertTrue("expected DistributedSystem to disconnect", stopped);
 +
 +    // recreate the system in vm1 without a locator and crash it 
 +    dm = (DistributedMember)vm1.invoke(create1);
 +    forceDisconnect(vm1);
 +    newdm = waitForReconnect(vm1);
 +    assertNotSame("expected a reconnect to occur in member", dm, newdm);
 +    DistributedTestUtils.deleteLocatorStateFile(locPort);
 +    DistributedTestUtils.deleteLocatorStateFile(secondLocPort);
 +  }
 +  
 +  private DistributedMember getDMID(VM vm) {
 +    return (DistributedMember)vm.invoke(new SerializableCallable("get ID") {
 +      public Object call() {
 +        ReconnectDUnitTest.savedSystem = InternalDistributedSystem.getAnyInstance();
 +        return ReconnectDUnitTest.savedSystem.getDistributedMember();
 +      }
 +    });
 +  }
 +  
 +  private DistributedMember waitForReconnect(VM vm) {
 +    return (DistributedMember)vm.invoke(new SerializableCallable("wait for Reconnect and return ID") {
 +      public Object call() {
 +    	System.out.println("waitForReconnect invoked");
 +        final DistributedSystem ds = ReconnectDUnitTest.savedSystem;
 +        ReconnectDUnitTest.savedSystem = null;
 +        Wait.waitForCriterion(new WaitCriterion() {
 +          public boolean done() {
 +            return ds.isReconnecting();
 +          }
 +          public String description() {
 +            return "waiting for ds to begin reconnecting";
 +          }
 +        }, 30000, 1000, true);
 +        long waitTime = 120;
 +        LogWriterUtils.getLogWriter().info("VM"+VM.getCurrentVMNum() + " waiting up to "+waitTime+" seconds for reconnect to complete");
 +        try {
 +          ds.waitUntilReconnected(waitTime, TimeUnit.SECONDS);
 +        } catch (InterruptedException e) {
 +          fail("interrupted while waiting for reconnect");
 +        }
 +        assertTrue("expected system to be reconnected", ds.getReconnectedSystem() != null);
 +        int oldViewId = MembershipManagerHelper.getMembershipManager(ds).getLocalMember().getVmViewId();
 +        int newViewId = ((InternalDistributedMember)ds.getReconnectedSystem().getDistributedMember()).getVmViewId();
 +        if ( !(newViewId > oldViewId) ) {
 +          fail("expected a new ID to be assigned.  oldViewId="+oldViewId + "; newViewId=" + newViewId);
 +        }
 +        return ds.getReconnectedSystem().getDistributedMember();
 +      }
 +    });
 +  }
 +  
 +  
 +  public void testReconnectALocator() throws Exception {
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm3 = host.getVM(3);
 +    DistributedMember dm, newdm;
 +    
 +    final int locPort = locatorPort;
 +    final int secondLocPort = AvailablePortHelper.getRandomAvailableTCPPort();
 +
 +    DistributedTestUtils.deleteLocatorStateFile(locPort, secondLocPort);
 +
 +    final String xmlFileLoc = (new File(".")).getAbsolutePath();
 +
 +    //This locator was started in setUp.
 +    File locatorViewLog = new File(vm3.getWorkingDirectory(), "locator"+locatorPort+"views.log");
 +    assertTrue("Expected to find " + locatorViewLog.getPath() + " file", locatorViewLog.exists());
 +    long logSize = locatorViewLog.length();
 +
 +    vm0.invoke(new SerializableRunnable("Create a second locator") {
 +      public void run() throws CacheException
 +      {
 +        locatorPort = locPort;
 +        Properties props = getDistributedSystemProperties();
 +        props.put("max-wait-time-reconnect", "1000");
 +        props.put("max-num-reconnect-tries", "2");
 +        props.put("locators", props.get("locators")+",localhost["+locPort+"]");
 +        props.put(DistributionConfig.ENABLE_CLUSTER_CONFIGURATION_NAME, "false");
 +        try {
 +          Locator.startLocatorAndDS(secondLocPort, null, props);
 +        } catch (IOException e) {
 +          Assert.fail("exception starting locator", e);
 +        }
 +      }
 +    });
 +
 +    File locator2ViewLog = new File(vm0.getWorkingDirectory(), "locator"+secondLocPort+"views.log");
 +    assertTrue("Expected to find " + locator2ViewLog.getPath() + " file", locator2ViewLog.exists());
 +    long log2Size = locator2ViewLog.length();
 +
 +    // create a cache in vm1 so there is more weight in the system
 +    SerializableCallable create1 = new SerializableCallable(
 +    "Create Cache and Regions from cache.xml") {
 +      public Object call() throws CacheException
 +      {
 +        //      DebuggerSupport.waitForJavaDebugger(getLogWriter(), " about to create region");
 +        locatorPort = locPort;
 +        Properties props = getDistributedSystemProperties();
 +        props.put("cache-xml-file", xmlFileLoc+"/MyDisconnect-cache.xml");
 +        props.put("max-wait-time-reconnect", "1000");
 +        props.put("max-num-reconnect-tries", "2");
 +        ReconnectDUnitTest.savedSystem = getSystem(props);
 +        Cache cache = getCache();
 +        Region myRegion = cache.getRegion("root/myRegion");
 +        myRegion.put("MyKey1", "MyValue1");
 +        // myRegion.put("Mykey2", "MyValue2");
 +        return savedSystem.getDistributedMember();
 +      }
 +    };
 +    vm1.invoke(create1);
 +
 +    
 +    try {
 +      
 +      dm = getDMID(vm0);
 +      createGfshWaitingThread(vm0);
 +      forceDisconnect(vm0);
 +      newdm = waitForReconnect(vm0);
 +      assertGfshWaitingThreadAlive(vm0);
 +
 +      boolean running = (Boolean)vm0.invoke(new SerializableCallable("check for running locator") {
 +        public Object call() {
 +          WaitCriterion wc = new WaitCriterion() {
 +            public boolean done() {
 +              return Locator.getLocator() != null;
 +            }
 +            public String description() {
 +              return "waiting for locator to restart";
 +            }
 +          };
 +          Wait.waitForCriterion(wc, 30000, 1000, false);
 +          if (Locator.getLocator() == null) {
 +            LogWriterUtils.getLogWriter().error("expected to find a running locator but getLocator() returns null");
 +            return false;
 +          }
 +          if (((InternalLocator)Locator.getLocator()).isStopped()) {
 +            LogWriterUtils.getLogWriter().error("found a stopped locator");
 +            return false;
 +          }
 +          return true;
 +        }
 +      });
 +      if (!running) {
 +        fail("expected the restarted member to be hosting a running locator");
 +      }
 +      
 +      assertNotSame("expected a reconnect to occur in the locator", dm, newdm);
 +
 +      // the log should have been opened and appended with a new view
 +      assertTrue("expected " + locator2ViewLog.getPath() + " to grow in size",
 +          locator2ViewLog.length() > log2Size);
 +      // the other locator should have logged a new view
 +      assertTrue("expected " + locatorViewLog.getPath() + " to grow in size",
 +          locatorViewLog.length() > logSize);
 +
 +    } finally {
 +      vm0.invoke(new SerializableRunnable("stop locator") {
 +        public void run() {
 +          Locator loc = Locator.getLocator();
 +          if (loc != null) {
 +            loc.stop();
 +          }
 +          if (gfshThread != null && gfshThread.isAlive()) {
 +            gfshThread.interrupt();
 +          }
 +          gfshThread = null;
 +        }
 +      });
 +      DistributedTestUtils.deleteLocatorStateFile(locPort);
 +      DistributedTestUtils.deleteLocatorStateFile(secondLocPort);
 +    }
 +  }
 +  
 +  @SuppressWarnings("serial")
 +  private void createGfshWaitingThread(VM vm) {
 +    vm.invoke(new SerializableRunnable("create Gfsh-like waiting thread") {
 +      public void run() {
 +        final Locator loc = Locator.getLocator();
 +        assertNotNull(loc);
 +        gfshThread = new Thread("ReconnectDUnitTest_Gfsh_thread") {
 +          public void run() {
 +            try {
 +              ((InternalLocator)loc).waitToStop();
 +            } catch (InterruptedException e) {
 +              System.out.println(Thread.currentThread().getName() + " interrupted - exiting");
 +            }
 +          }
 +        };
 +        gfshThread.setDaemon(true);
 +        gfshThread.start();
 +        System.out.println("created gfsh thread: " + gfshThread);
 +      }
 +    });
 +  }
 +  
 +  @SuppressWarnings("serial")
 +  private void assertGfshWaitingThreadAlive(VM vm) {
 +    vm.invoke(new SerializableRunnable("assert gfshThread is still waiting") {
 +      public void run() {
 +        assertTrue(gfshThread.isAlive());
 +      }
 +    });
 +  }
 +  
 +  /**
 +   * Test the reconnect behavior when the required roles are missing.
 +   * Reconnect is triggered as a Reliability policy. The test is to
 +   * see if the reconnect is triggered for the configured number of times
 +   */
 +  
 +  public void testReconnectWithRoleLoss() throws TimeoutException,
 +      RegionExistsException  {
 +
 +    final String rr1 = "RoleA";
 +    final String rr2 = "RoleB";
 +    final String[] requiredRoles = { rr1, rr2 };
 +    final int locPort = locatorPort;
 +    final String xmlFileLoc = (new File(".")).getAbsolutePath();
 +
 +
 +    beginCacheXml();
 +
 +    locatorPort = locPort;
 +    Properties config = getDistributedSystemProperties();
 +    config.put(DistributionConfig.ROLES_NAME, "");
 +    config.put(DistributionConfig.LOG_LEVEL_NAME, LogWriterUtils.getDUnitLogLevel());
 +//    config.put("log-file", "roleLossController.log");
 +    //creating the DS
 +    getSystem(config);
 +
 +    MembershipAttributes ra = new MembershipAttributes(requiredRoles,
 +        LossAction.RECONNECT, ResumptionAction.NONE);
 +
 +    AttributesFactory fac = new AttributesFactory();
 +    fac.setMembershipAttributes(ra);
 +    fac.setScope(Scope.DISTRIBUTED_ACK);
 +
 +    RegionAttributes attr = fac.create();
 +    createRootRegion("MyRegion", attr);
 +
 +    //writing the cachexml file.
 +
 +    File file = new File("RoleReconnect-cache.xml");
 +    try {
 +      PrintWriter pw = new PrintWriter(new FileWriter(file), true);
 +      CacheXmlGenerator.generate(getCache(), pw);
 +      pw.close();
 +    }
 +    catch (IOException ex) {
 +      Assert.fail("IOException during cache.xml generation to " + file, ex);
 +    }
 +    closeCache();
 +    getSystem().disconnect();
 +
 +    LogWriterUtils.getLogWriter().info("disconnected from the system...");
 +    Host host = Host.getHost(0);
 +
 +    VM vm0 = host.getVM(0);
 +
 +    // Recreating from the cachexml.
 +
 +    SerializableRunnable roleLoss = new CacheSerializableRunnable(
 +        "ROLERECONNECTTESTS") {
 +      public void run2() throws CacheException, RuntimeException
 +      {
 +        LogWriterUtils.getLogWriter().info("####### STARTING THE REAL TEST ##########");
 +        locatorPort = locPort;
 +        Properties props = getDistributedSystemProperties();
 +        props.put("cache-xml-file", xmlFileLoc+File.separator+"RoleReconnect-cache.xml");
 +        props.put("max-wait-time-reconnect", "200");
 +        final int timeReconnect = 3;
 +        props.put("max-num-reconnect-tries", "3");
 +        props.put(DistributionConfig.LOG_LEVEL_NAME, LogWriterUtils.getDUnitLogLevel());
 +        props.put("log-file", "roleLossVM0.log");
 +
 +        getSystem(props);
 +
 +        addReconnectListener();
 +        
 +        system.getLogWriter().info("<ExpectedException action=add>" 
 +            + "CacheClosedException" + "</ExpectedException");
 +        try{
 +          getCache();
 +          throw new RuntimeException("The test should throw a CancelException ");
 +        }
 +        catch (CancelException ignor){ // can be caused by role loss during intialization.
 +          LogWriterUtils.getLogWriter().info("Got Expected CancelException ");
 +        }
 +        finally {
 +          system.getLogWriter().info("<ExpectedException action=remove>" 
 +              + "CacheClosedException" + "</ExpectedException");
 +        }
 +        LogWriterUtils.getLogWriter().fine("roleLoss Sleeping SO call dumprun.sh");
 +        WaitCriterion ev = new WaitCriterion() {
 +          public boolean done() {
 +            return reconnectTries >= timeReconnect;
 +          }
 +          public String description() {
 +            return "Waiting for reconnect count " + timeReconnect + " currently " + reconnectTries;
 +          }
 +        };
 +        Wait.waitForCriterion(ev, 60 * 1000, 200, true);
 +        LogWriterUtils.getLogWriter().fine("roleLoss done Sleeping");
 +        assertEquals(timeReconnect,
 +            reconnectTries);
 +      }
 +
 +    };
 +
 +    vm0.invoke(roleLoss);
 +
 +
 +  }
 +  
 +     
 +  
 +  public static volatile int reconnectTries;
 +  
 +  public static volatile boolean initialized = false;
 +  
 +  public static volatile boolean initialRolePlayerStarted = false;
 +   
 +  //public static boolean rPut;
 +  public static Integer reconnectTries(){
 +    return new Integer(reconnectTries);
 +  }
 +  
 +  public static Boolean isInitialized(){
 +	  return new Boolean(initialized);
 +  }
 +  
 +  public static Boolean isInitialRolePlayerStarted(){
 +	  return new Boolean (initialRolePlayerStarted);
 +  }
 +  
 +  
 +  // See #50944 before enabling the test.  This ticket has been closed with wontFix
 +  // for the 2014 8.0 release.
 +  public void DISABLED_testReconnectWithRequiredRoleRegained()throws Throwable {
 +
 +    final String rr1 = "RoleA";
 +    //final String rr2 = "RoleB";
 +    final String[] requiredRoles = { rr1 };
 +    //final boolean receivedPut[] = new boolean[1];
 +
 +    final Integer[] numReconnect = new Integer[1];
 +    numReconnect[0] = new Integer(-1);
 +    final String myKey = "MyKey";
 +    final String myValue = "MyValue"; 
 +    final String regionName = "MyRegion";
 +    final int locPort = locatorPort;
 +
 +    // CREATE XML FOR MEMBER THAT WILL SEE ROLE LOSS (in this VM)
 +    beginCacheXml();
 +
 +    locatorPort = locPort;
 +    Properties config = getDistributedSystemProperties();
 +    config.put(DistributionConfig.ROLES_NAME, "");
 +    config.put(DistributionConfig.LOG_LEVEL_NAME, LogWriterUtils.getDUnitLogLevel());
 +    //creating the DS
 +    getSystem(config);
 +
 +    MembershipAttributes ra = new MembershipAttributes(requiredRoles,
 +        LossAction.RECONNECT, ResumptionAction.NONE);
 +
 +    AttributesFactory fac = new AttributesFactory();
 +    fac.setMembershipAttributes(ra);
 +    fac.setScope(Scope.DISTRIBUTED_ACK);
 +    fac.setDataPolicy(DataPolicy.REPLICATE);
 +
 +    RegionAttributes attr = fac.create();
 +    createRootRegion(regionName, attr);
 +
 +    //writing the cachexml file.
 +
 +    File file = new File("RoleRegained.xml");
 +    try {
 +      PrintWriter pw = new PrintWriter(new FileWriter(file), true);
 +      CacheXmlGenerator.generate(getCache(), pw);
 +      pw.close();
 +    }
 +    catch (IOException ex) {
 +      Assert.fail("IOException during cache.xml generation to " + file, ex);
 +    }
 +    closeCache();
 +    //disconnectFromDS();
 +    getSystem().disconnect(); //added
 +
 +    // ################################################################### //
 +    //
 +    Host host = Host.getHost(0);
 +    final VM vm0 = host.getVM(0);
 +    final VM vm1 = host.getVM(1);
 +
 +    vm0.invoke(new CacheSerializableRunnable("reset reconnect count") {
 +      @Override
 +      public void run2() throws CacheException {
 +        reconnectTries = 0;
 +      }
 +    });
 +
 +    SerializableRunnable roleAPlayerForCacheInitialization = 
 +        getRoleAPlayerForCacheInitializationRunnable(vm0, locPort, regionName,
 +            "starting roleAplayer, which will initialize, wait for "
 +                + "vm0 to initialize, and then close its cache to cause role loss");
 +    AsyncInvocation avkVm1 = vm1.invokeAsync(roleAPlayerForCacheInitialization);
 +    
 +    CacheSerializableRunnable roleLoss = getRoleLossRunnable(vm1, locPort, regionName, myKey, myValue,
 +        "starting role loss vm.  When the role is lost it will start"
 +            + " trying to reconnect");
 +    final AsyncInvocation roleLossAsync = vm0.invokeAsync(roleLoss);
 +    
 +    LogWriterUtils.getLogWriter().info("waiting for role loss vm to start reconnect attempts");
 +
 +    WaitCriterion ev = new WaitCriterion() {
 +      public boolean done() {
 +        if (!roleLossAsync.isAlive()) {
 +          return true;
 +        }
-         Object res = vm0.invoke(ReconnectDUnitTest.class, "reconnectTries");
++        Object res = vm0.invoke(() -> ReconnectDUnitTest.reconnectTries());
 +        if (((Integer)res).intValue() != 0) {
 +          return true;
 +        }
 +        return false;
 +      }
 +      public String description() {
 +        return "waiting for event";
 +      }
 +    };
 +    Wait.waitForCriterion(ev, 120 * 1000, 200, true);
 +
 +    VM vm2 = host.getVM(2);
 +    if (roleLossAsync.isAlive()) {
 +
 +      SerializableRunnable roleAPlayer = getRoleAPlayerRunnable(locPort, regionName, myKey, myValue,
 +          "starting roleAPlayer in a different vm."
 +              + "  After this reconnect should succeed in vm0");
 +
 +      vm2.invoke(roleAPlayer);
 +
 +      //      long startTime = System.currentTimeMillis();
 +      /*
 +      while (numReconnect[0].intValue() > 0){
 +        if((System.currentTimeMillis()-startTime )> 120000)
 +          fail("The test failed because the required role not satisfied" +
 +               "and the number of reconnected tried is not set to zero for " +
 +               "more than 2 mins");
 +        try{
 +          Thread.sleep(15);
 +        }catch(Exception ee){
 +          getLogWriter().severe("Exception : "+ee);
 +        }
 +      }*/
 +      LogWriterUtils.getLogWriter().info("waiting for vm0 to finish reconnecting");
 +      ThreadUtils.join(roleLossAsync, 120 * 1000);
 +    }
 +
 +    if (roleLossAsync.getException() != null){
 +      Assert.fail("Exception in Vm0", roleLossAsync.getException());
 +    }
 +
 +    ThreadUtils.join(avkVm1, 30 * 1000);
 +    if (avkVm1.getException() != null){
 +      Assert.fail("Exception in Vm1", avkVm1.getException());
 +    }
 +
 +  }
 +  
 +  private CacheSerializableRunnable getRoleLossRunnable(final VM otherVM, final int locPort,
 +      final String regionName, final String myKey, final Object myValue,
 +      final String startupMessage) {
 +
 +    return new CacheSerializableRunnable("roleloss runnable") {
 +      public void run2()
 +      {
 +        Thread t = null;
 +        try {
 +          //  closeCache();
 +          //  getSystem().disconnect();
 +          LogWriterUtils.getLogWriter().info(startupMessage);
 +          WaitCriterion ev = new WaitCriterion() {
 +            public boolean done() {
-               return ((Boolean)otherVM.invoke(ReconnectDUnitTest.class, "isInitialRolePlayerStarted")).booleanValue();
++              return ((Boolean)otherVM.invoke(() -> ReconnectDUnitTest.isInitialRolePlayerStarted())).booleanValue();
 +            }
 +            public String description() {
 +              return null;
 +            }
 +          };
 +          Wait.waitForCriterion(ev, 10 * 1000, 200, true);
 +
 +          LogWriterUtils.getLogWriter().info("Starting the test and creating the cache and regions etc ...");
 +          locatorPort = locPort;
 +          Properties props = getDistributedSystemProperties();
 +          props.put("cache-xml-file", "RoleRegained.xml");
 +          props.put("max-wait-time-reconnect", "3000");
 +          props.put("max-num-reconnect-tries", "8");
 +          props.put(DistributionConfig.LOG_LEVEL_NAME, LogWriterUtils.getDUnitLogLevel());
 +
 +          getSystem(props);
 +          system.getLogWriter().info("<ExpectedException action=add>" 
 +              + "CacheClosedException" + "</ExpectedException");
 +
 +          try {
 +            getCache();
 +          } catch (CancelException e) {
 +            // can happen if RoleA goes away during initialization
 +            LogWriterUtils.getLogWriter().info("cache threw CancelException while creating the cache");      
 +          }
 +          
 +          initialized = true;
 +          
 +          addReconnectListener();
 +
 +          ev = new WaitCriterion() {
 +            public boolean done() {
 +              LogWriterUtils.getLogWriter().info("ReconnectTries=" + reconnectTries);
 +              return reconnectTries != 0;
 +            }
 +            public String description() {
 +              return null;
 +            }
 +          };
 +          Wait.waitForCriterion(ev, 30 * 1000, 200, true);
 +
 +          //        long startTime = System.currentTimeMillis();
 +
 +          ev = new WaitCriterion() {
 +            String excuse;
 +            public boolean done() {
 +              if (InternalDistributedSystem.getReconnectCount() != 0) {
 +                excuse = "reconnectCount is " + reconnectTries
 +                    + " waiting for it to be zero";
 +                return false;
 +              }
 +              Object key = null;
 +              Object value= null;
 +              Region.Entry keyValue = null;
 +              try {
 +                Cache cache = CacheFactory.getAnyInstance();
 +                if (cache == null) {
 +                  excuse = "no cache";
 +                  return false;
 +                }
 +                Region myRegion = cache.getRegion(regionName);
 +                if (myRegion == null) {
 +                  excuse = "no region";
 +                  return false;
 +                }
 +
 +                Set keyValuePair = myRegion.entrySet();
 +                Iterator it = keyValuePair.iterator();
 +                while (it.hasNext()) {
 +                  keyValue = (Region.Entry)it.next();
 +                  key = keyValue.getKey();
 +                  value = keyValue.getValue();
 +                }
 +                if (key == null) {
 +                  excuse = "key is null";
 +                  return false;
 +                }
 +                if (!myKey.equals(key)) {
 +                  excuse = "key is wrong";
 +                  return false;
 +                }
 +                if (value == null) {
 +                  excuse = "value is null";
 +                  return false;
 +                }
 +                if (!myValue.equals(value)) {
 +                  excuse = "value is wrong";
 +                  return false;
 +                }
 +                LogWriterUtils.getLogWriter().info("All assertions passed");
 +                LogWriterUtils.getLogWriter().info("MyKey : "+key+" and myvalue : "+value);
 +                return true;
 +              }
 +              catch (CancelException ecc){ 
 +                // ignor the exception because the cache can be closed/null some times 
 +                // while in reconnect.
 +              }
 +              catch(RegionDestroyedException rex){
 +
 +              }
 +              finally {
 +                LogWriterUtils.getLogWriter().info("waiting for reconnect.  Current status is '"+excuse+"'");
 +              }
 +              return false;
 +            }
 +            public String description() {
 +              return excuse;
 +            }
 +          };
 +
 +          Wait.waitForCriterion(ev,  60 * 1000, 200, true); // was 5 * 60 * 1000
 +
 +          Cache cache = CacheFactory.getAnyInstance();
 +          if (cache != null) {
 +            cache.getDistributedSystem().disconnect();
 +          }
 +        } 
 +        catch (VirtualMachineError e) {
 +          SystemFailure.initiateFailure(e);
 +          throw e;
 +        }
 +        catch (Error th) {
 +          LogWriterUtils.getLogWriter().severe("DEBUG", th);
 +          throw th;
 +        } finally {
 +          if (t != null) {
 +            ThreadUtils.join(t, 2 * 60 * 1000);
 +          }
 +          // greplogs won't care if you remove an exception that was never added,
 +          // and this ensures that it gets removed.
 +          system.getLogWriter().info("<ExpectedException action=remove>" 
 +              + "CacheClosedException" + "</ExpectedException");
 +        }
 +
 +      }
 +
 +    }; // roleloss runnable
 +  }
 +
 +  private CacheSerializableRunnable getRoleAPlayerRunnable(
 +      final int locPort, final String regionName, final String myKey, final String myValue,
 +      final String startupMessage) {
 +    return new CacheSerializableRunnable(
 +        "second RoleA player") {
 +      public void run2() throws CacheException
 +      {
 +        LogWriterUtils.getLogWriter().info(startupMessage);
 +        //closeCache();
 +        // getSystem().disconnect();
 +        locatorPort = locPort;
 +        Properties props = getDistributedSystemProperties();
 +        props.put(DistributionConfig.LOG_LEVEL_NAME, LogWriterUtils.getDUnitLogLevel());
 +        props.put(DistributionConfig.ROLES_NAME, "RoleA");
 +
 +        getSystem(props);
 +        getCache();
 +        AttributesFactory fac = new AttributesFactory();
 +        fac.setScope(Scope.DISTRIBUTED_ACK);
 +        fac.setDataPolicy(DataPolicy.REPLICATE);
 +
 +        RegionAttributes attr = fac.create();
 +        Region region = createRootRegion(regionName, attr);
 +        LogWriterUtils.getLogWriter().info("STARTED THE REQUIREDROLES CACHE");
 +        try{
 +          Thread.sleep(120);
 +        }
 +        catch (Exception ee) {
 +          fail("interrupted");
 +        }
 +
 +        region.put(myKey,myValue);
 +        try {
 +          Thread.sleep(5000); // why are we sleeping for 5 seconds here?
 +          // if it is to give time to avkVm0 to notice us we should have
 +          // him signal us that he has seen us and then we can exit.
 +        }
 +        catch(InterruptedException ee){
 +          fail("interrupted");
 +        }
 +        LogWriterUtils.getLogWriter().info("RolePlayer is done...");
 +
 +
 +      }
 +
 +
 +    };
 +  }
 +  
 +  
 +  private CacheSerializableRunnable getRoleAPlayerForCacheInitializationRunnable(
 +      final VM otherVM, final int locPort, final String regionName,
 +      final String startupMessage) {
 +    return new CacheSerializableRunnable(
 +        "first RoleA player") {
 +      public void run2() throws CacheException
 +      {
 +        //  closeCache();
 +        // getSystem().disconnect();
 +        LogWriterUtils.getLogWriter().info(startupMessage);
 +        locatorPort = locPort;
 +        Properties props = getDistributedSystemProperties();
 +        props.put(DistributionConfig.LOG_LEVEL_NAME, LogWriterUtils.getDUnitLogLevel());
 +        props.put(DistributionConfig.ROLES_NAME, "RoleA");
 +
 +        getSystem(props);
 +        getCache();
 +        AttributesFactory fac = new AttributesFactory();
 +        fac.setScope(Scope.DISTRIBUTED_ACK);
 +        fac.setDataPolicy(DataPolicy.REPLICATE);
 +
 +        RegionAttributes attr = fac.create();
 +        createRootRegion(regionName, attr);
 +        LogWriterUtils.getLogWriter().info("STARTED THE REQUIREDROLES CACHE");
 +        initialRolePlayerStarted = true;
 +
-         while(!((Boolean)otherVM.invoke(ReconnectDUnitTest.class, "isInitialized")).booleanValue()){
++        while(!((Boolean)otherVM.invoke(() -> ReconnectDUnitTest.isInitialized())).booleanValue()){
 +          try{
 +            Thread.sleep(15);
 +          }catch(InterruptedException ignor){
 +            fail("interrupted");
 +          }
 +        }
 +        LogWriterUtils.getLogWriter().info("RoleAPlayerInitializer is done...");
 +        closeCache();
 +
 +      }
 +    };
 +  }
 +  
 +  
 +  void addReconnectListener() {
 +    reconnectTries = 0; // reset the count for this listener
 +    LogWriterUtils.getLogWriter().info("adding reconnect listener");
 +    ReconnectListener reconlis = new ReconnectListener() {
 +      public void reconnecting(InternalDistributedSystem oldSys) {
 +        LogWriterUtils.getLogWriter().info("reconnect listener invoked");
 +        reconnectTries++;
 +      }
 +      public void onReconnect(InternalDistributedSystem system1, InternalDistributedSystem system2) {}
 +    };
 +    InternalDistributedSystem.addReconnectListener(reconlis);
 +  }
 +  
 +  private void waitTimeout() throws InterruptedException
 +  {
 +    Thread.sleep(500);
 +
 +  }
 +  public boolean forceDisconnect(VM vm) {
 +    return (Boolean)vm.invoke(new SerializableCallable("crash distributed system") {
 +      public Object call() throws Exception {
 +        // since the system will disconnect and attempt to reconnect
 +        // a new system the old reference to DTC.system can cause
 +        // trouble, so we first null it out.
 +        DistributedTestCase.system = null;
 +        final DistributedSystem msys = InternalDistributedSystem.getAnyInstance();
 +        final Locator oldLocator = Locator.getLocator();
 +        MembershipManagerHelper.crashDistributedSystem(msys);
 +        if (oldLocator != null) {
 +          WaitCriterion wc = new WaitCriterion() {
 +            public boolean done() {
 +              return msys.isReconnecting();
 +            }
 +            public String description() {
 +              return "waiting for locator to start reconnecting: " + oldLocator;
 +            }
 +          };
 +          Wait.waitForCriterion(wc, 10000, 50, true);
 +        }
 +        return true;
 +      }
 +    });
 +  }
 +  
 +  private static int getPID() {
 +    String name = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();            
 +    int idx = name.indexOf('@'); 
 +    try {             
 +      return Integer.parseInt(name.substring(0,idx));
 +    } catch(NumberFormatException nfe) {
 +      //something changed in the RuntimeMXBean name
 +    }                         
 +    return 0;
 +  }
 +
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/ReconnectedCacheServerDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/ReconnectedCacheServerDUnitTest.java
index 2b97a9a,0000000..6daf213
mode 100755,000000..100755
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/ReconnectedCacheServerDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/ReconnectedCacheServerDUnitTest.java
@@@ -1,68 -1,0 +1,95 @@@
 +/*
 + * 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.cache30;
 +
++import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.distributed.internal.membership.gms.MembershipManagerHelper;
 +import com.gemstone.gemfire.distributed.internal.membership.gms.mgr.GMSMembershipManager;
++import com.gemstone.gemfire.internal.cache.CacheServerLauncher;
 +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
 +
 +
 +public class ReconnectedCacheServerDUnitTest extends CacheTestCase {
 +
 +  public ReconnectedCacheServerDUnitTest(String name) {
 +    super(name);
 +  }
 +
 +  private static final long serialVersionUID = 1L;
 +  
 +  private boolean addedCacheServer = false;
 +  
 +  @Override
 +  public void setUp() {
 +    getCache();
 +    if (cache.getCacheServers().isEmpty()) {
 +      cache.addCacheServer();
 +      addedCacheServer = true;
 +    }
 +  }
 +  
 +  @Override
 +  protected final void preTearDownCacheTestCase() throws Exception {
 +    if (addedCacheServer && cache != null && !cache.isClosed()) {
 +      // since I polluted the cache I should shut it down in order
 +      // to avoid affecting other tests
 +      cache.close();
 +    }
 +  }
 +
 +  public void testCacheServerConfigRetained() {
 +    // make sure the environment isn't polluted
 +    assertFalse(Boolean.getBoolean("gemfire.autoReconnect-useCacheXMLFile"));
 +
 +    GemFireCacheImpl gc = (GemFireCacheImpl)cache;
 +    
 +    // fool the system into thinking cluster-config is being used
 +    GMSMembershipManager mgr = (GMSMembershipManager)MembershipManagerHelper
 +           .getMembershipManager(gc.getDistributedSystem());
 +    mgr.saveCacheXmlForReconnect(true);
 +    
 +    // the cache server config should now be stored in the cache's config
 +    assertFalse(gc.getCacheServers().isEmpty());
 +    assertNotNull(gc.getCacheConfig().getCacheServerCreation());
 +  }
 +
++  public void testDefaultCacheServerNotCreatedOnReconnect() {
++    
++    assertFalse(Boolean.getBoolean("gemfire.autoReconnect-useCacheXMLFile"));
++    
++    GemFireCacheImpl gc = (GemFireCacheImpl)cache;
++
++    // fool the system into thinking cluster-config is being used
++    GMSMembershipManager mgr = (GMSMembershipManager)MembershipManagerHelper
++        .getMembershipManager(gc.getDistributedSystem());
++    final boolean sharedConfigEnabled = true;
++    mgr.saveCacheXmlForReconnect(sharedConfigEnabled);
++
++    // the cache server config should now be stored in the cache's config
++    assertFalse(gc.getCacheServers().isEmpty());
++    int numServers = gc.getCacheServers().size();
++
++    assertNotNull(gc.getCacheConfig().getCacheServerCreation());
++
++    InternalDistributedSystem system = gc.getDistributedSystem();
++    system.createAndStartCacheServers(gc.getCacheConfig().getCacheServerCreation(), gc);
++
++    assertEquals("found these cache servers:" + gc.getCacheServers(),
++        numServers, gc.getCacheServers().size());
++      
++  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/RegionMembershipListenerDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/RegionMembershipListenerDUnitTest.java
index d539b82,0000000..0572c76
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/RegionMembershipListenerDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/RegionMembershipListenerDUnitTest.java
@@@ -1,420 -1,0 +1,420 @@@
 +/*
 + * 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.cache30;
 +
 +import java.util.ArrayList;
 +import java.util.Arrays;
 +import java.util.List;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheListener;
 +import com.gemstone.gemfire.cache.Operation;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionAttributes;
 +import com.gemstone.gemfire.cache.RegionEvent;
 +import com.gemstone.gemfire.cache.RegionMembershipListener;
 +import com.gemstone.gemfire.cache.util.RegionMembershipListenerAdapter;
 +import com.gemstone.gemfire.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.internal.DistributionAdvisor.Profile;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.distributed.internal.membership.gms.MembershipManagerHelper;
 +import com.gemstone.gemfire.internal.cache.CacheDistributionAdvisor.CacheProfile;
 +import com.gemstone.gemfire.internal.cache.DistributedRegion;
 +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.LogWriterUtils;
 +import com.gemstone.gemfire.test.dunit.VM;
 +import com.gemstone.gemfire.test.dunit.Wait;
 +import com.gemstone.gemfire.test.dunit.WaitCriterion;
 +
 +/**
 + * Test {@link RegionMembershipListener}
 + *
 + * @author darrel
 + * @since 5.0
 + */
 +public class RegionMembershipListenerDUnitTest extends CacheTestCase {
 +
 +  private transient MyRML myListener;
 +  private transient MyRML mySRListener;
 +  private transient Region r; // root region
 +  private transient Region sr; // subregion
 +  protected transient DistributedMember otherId;
 +  
 +  public RegionMembershipListenerDUnitTest(String name) {
 +    super(name);
 +  }
 +  
 +  @Override
 +  public void setUp() throws Exception {
 +    super.setUp();
 +    DistributedRegion.TEST_HOOK_ADD_PROFILE = true;
 +  }
 +  
 +  @Override
 +  protected final void postTearDownCacheTestCase() throws Exception {
 +    DistributedRegion.TEST_HOOK_ADD_PROFILE = false;
 +  }
 +
 +  protected VM getOtherVm() {
 +    Host host = Host.getHost(0);
 +    return host.getVM(0);
 +  }
 +    
 +  private void initOtherId() {
 +    VM vm = getOtherVm();
 +    vm.invoke(new CacheSerializableRunnable("Connect") {
 +        public void run2() throws CacheException {
 +          getCache();
 +        }
 +      });
-     this.otherId = (DistributedMember)vm.invoke(RegionMembershipListenerDUnitTest.class, "getVMDistributedMember");
++    this.otherId = (DistributedMember)vm.invoke(() -> RegionMembershipListenerDUnitTest.getVMDistributedMember());
 +  }
 +  protected void createRootOtherVm(final String rName) {
 +    VM vm = getOtherVm();
 +    vm.invoke(new CacheSerializableRunnable("create root") {
 +        public void run2() throws CacheException {
 +          Region r= createRootRegion(rName, createRootRegionAttributes(null));
 +          r.createSubregion("mysub", createSubRegionAttributes(null));
 +        }
 +      });
 +  }
 +  protected RegionAttributes createRootRegionAttributes(CacheListener[] cacheListeners) {
 +    AttributesFactory af = new AttributesFactory();
 +    if (cacheListeners != null) {
 +      af.initCacheListeners(cacheListeners);
 +    }
 +    return af.create();   
 +  }
 +  protected RegionAttributes createSubRegionAttributes(CacheListener[] cacheListeners) {
 +    return createRootRegionAttributes(cacheListeners);
 +  }
 +
 +  protected void destroyRootOtherVm(final String rName) {
 +    VM vm = getOtherVm();
 +    vm.invoke(new CacheSerializableRunnable("local destroy root") {
 +        public void run2() throws CacheException {
 +          getRootRegion(rName).localDestroyRegion();
 +        }
 +      });
 +  }
 +  protected void closeRootOtherVm(final String rName) {
 +    VM vm = getOtherVm();
 +    vm.invoke(new CacheSerializableRunnable("close root") {
 +        public void run2() throws CacheException {
 +          getRootRegion(rName).close();
 +        }
 +      });
 +  }
 +  private void closeCacheOtherVm() {
 +    VM vm = getOtherVm();
 +    vm.invoke(new CacheSerializableRunnable("close cache") {
 +        public void run2() throws CacheException {
 +          getCache().close();
 +        }
 +      });
 +  }
 +
 +  private void crashCacheOtherVm() {
 +    VM vm = getOtherVm();
 +    vm.invoke(new CacheSerializableRunnable("crash cache") {
 +        public void run2() throws CacheException {
 +          // shut down the gms before the distributed system to simulate
 +          // a crash.  In post-5.1.x, this could use SystemFailure.initFailure()
 +          GemFireCacheImpl cache = (GemFireCacheImpl)getCache();
 +          InternalDistributedSystem sys = (InternalDistributedSystem)cache.getDistributedSystem();
 +          MembershipManagerHelper.crashDistributedSystem(sys);
 +        }
 +      });
 +  }
 +
 +  public static DistributedMember getVMDistributedMember() {
 +    return InternalDistributedSystem.getAnyInstance().getDistributedMember();
 +  }
 +  
 +  protected void createRootRegionWithListener(String rName) throws CacheException {
 +    int to = getOpTimeout();
 +    this.myListener = new MyRML(to);
 +    this.r = createRootRegion(rName, createRootRegionAttributes(new CacheListener[]{this.myListener}));
 +    this.mySRListener = new MyRML(to);
 +    this.sr = this.r.createSubregion("mysub", createSubRegionAttributes(new CacheListener[]{this.mySRListener}));
 +  }
 +  
 +  public int getOpTimeout() {
 +    return getSystem().getDistributionManager().getConfig().getMemberTimeout() * 3;    
 +  }
 +  
 +  //////////////////////  Test Methods  //////////////////////
 +
 +  /**
 +   * tests {@link RegionMembershipListener#initialMembers}
 +   */
 +  public void testInitialMembers() throws CacheException {
 +    final String rName = getUniqueName();
 +    initOtherId();
 +    createRootRegionWithListener(rName);
 +    assertInitialMembers(null);
 +    createRootOtherVm(rName);
 +
 +    // now close the region in the controller
 +    // and recreate it and see if initMembers includes otherId
 +    closeRoots();
 +    
 +    createRootRegionWithListener(rName);
 +    assertInitialMembers(this.otherId);
 +  }
 +  protected void closeRoots() {
 +    this.r.close();
 +  }
 +  protected List<DistributedMember> assertInitialMembers(final DistributedMember expectedId) {
 +    final List<DistributedMember> l;
 +    if (expectedId == null) {
 +      l = Arrays.asList(new DistributedMember[]{});
 +    } else {
 +      l = Arrays.asList(new DistributedMember[]{expectedId});
 +    }
 +    assertTrue(this.myListener.lastOpWasInitialMembers());
 +    assertEquals(l, this.myListener.getInitialMembers());
 +    assertTrue(this.mySRListener.lastOpWasInitialMembers());
 +    assertEquals(l, this.mySRListener.getInitialMembers());
 +    // test new methods added for #43098
 +    if (expectedId != null) {
 +      Cache cache = (Cache)this.r.getRegionService();
 +      //assertEquals(l, new ArrayList(cache.getMembers()));
 +      assertEquals(l, new ArrayList(cache.getMembers(this.r)));
 +      assertEquals(l, new ArrayList(cache.getMembers(this.sr)));
 +    }
 +    return l;
 +  }
 +
 +  /**
 +   * tests {@link RegionMembershipListener#afterRemoteRegionCreate}
 +   */
 +  public void testCreate() throws CacheException {
 +    final String rName = getUniqueName();
 +    initOtherId();
 +    createRootRegionWithListener(rName);
 +    createRootOtherVm(rName);
 +    assertTrue(this.myListener.lastOpWasCreate());
 +    {
 +      RegionEvent e = this.myListener.getLastEvent();
 +      assertEquals(this.otherId, e.getDistributedMember());
 +      assertEquals(Operation.REGION_CREATE, e.getOperation());
 +      assertEquals(true, e.isOriginRemote());
 +      assertEquals(false, e.isDistributed());
 +      assertEquals(this.r, e.getRegion());
 +      // the test now uses a hook to get the member's DistributionAdvisor profile in the callback argument
 +      assertTrue(e.getCallbackArgument() instanceof Profile);
 +//      assertEquals(null, e.getCallbackArgument());
 +    }
 +    assertTrue(this.mySRListener.lastOpWasCreate());
 +    {
 +      RegionEvent e = this.mySRListener.getLastEvent();
 +      assertEquals(this.otherId, e.getDistributedMember());
 +      assertEquals(Operation.REGION_CREATE, e.getOperation());
 +      assertEquals(true, e.isOriginRemote());
 +      assertEquals(false, e.isDistributed());
 +      assertEquals(this.sr, e.getRegion());
 +      // the test now uses a hook to get the member's DistributionAdvisor profile in the callback argument
 +      assertTrue(e.getCallbackArgument() instanceof Profile);
 +//      assertEquals(null, e.getCallbackArgument());
 +    }
 +  }
 +  /**
 +   * tests {@link RegionMembershipListener#afterRemoteRegionDeparture}
 +   */
 +  public void testDeparture() throws CacheException {
 +    final String rName = getUniqueName();
 +    initOtherId();
 +    createRootRegionWithListener(rName);
 +    createRootOtherVm(rName);
 +    assertOpWasCreate();
 +
 +    destroyRootOtherVm(rName);
 +    assertOpWasDeparture();
 +    
 +    createRootOtherVm(rName);
 +    assertOpWasCreate();
 +    
 +    closeRootOtherVm(rName);
 +    assertOpWasDeparture();
 +
 +    createRootOtherVm(rName);
 +    assertOpWasCreate();
 +
 +    closeCacheOtherVm();
 +    assertOpWasDeparture();
 +  }
 +  protected void assertOpWasDeparture() {
 +    assertTrue(this.myListener.lastOpWasDeparture());
 +    assertEventStuff(this.myListener.getLastEvent(), this.otherId, this.r);
 +    assertTrue(this.mySRListener.lastOpWasDeparture());
 +    assertEventStuff(this.mySRListener.getLastEvent(), this.otherId, this.sr);
 +  }
 +  public static void assertEventStuff(RegionEvent e, DistributedMember em, Region er) {
 +    assertEquals(em, e.getDistributedMember());
 +    assertEquals(Operation.REGION_CLOSE, e.getOperation());
 +    assertEquals(true, e.isOriginRemote());
 +    assertEquals(false, e.isDistributed());
 +    assertEquals(er, e.getRegion());
 +    assertEquals(null, e.getCallbackArgument());
 +  }
 +  protected void assertOpWasCreate() {
 +    assertTrue(this.myListener.lastOpWasCreate());
 +    assertTrue(this.mySRListener.lastOpWasCreate());
 +  }
 +
 +  /**
 +   * tests {@link RegionMembershipListener#afterRemoteRegionCrash}
 +   */
 +  public void testCrash() throws CacheException {
 +    final String rName = getUniqueName();
 +    initOtherId();
 +    createRootRegionWithListener(rName);
 +    createRootOtherVm(rName);
 +    try {
 +      assertTrue(this.myListener.lastOpWasCreate()); // root region
 +      assertTrue(this.mySRListener.lastOpWasCreate()); // subregion
 +      MembershipManagerHelper.inhibitForcedDisconnectLogging(true);
 +      
 +      crashCacheOtherVm();
 +      int to = getOpTimeout();
 +      MembershipManagerHelper.waitForMemberDeparture(system, this.otherId, to);
 +      this.myListener.waitForCrashOp();
 +      {
 +        RegionEvent e = this.myListener.getLastEvent();
 +        assertEquals(this.otherId, e.getDistributedMember());
 +        assertEquals(Operation.REGION_CLOSE, e.getOperation());
 +        assertEquals(true, e.isOriginRemote());
 +        assertEquals(false, e.getOperation().isDistributed());
 +        assertEquals(this.r, e.getRegion());
 +        assertEquals(null, e.getCallbackArgument());
 +      }
 +      this.mySRListener.waitForCrashOp();
 +      {
 +        RegionEvent e = this.mySRListener.getLastEvent();
 +        assertEquals(this.otherId, e.getDistributedMember());
 +        assertEquals(Operation.REGION_CLOSE, e.getOperation());
 +        assertEquals(true, e.isOriginRemote());
 +        assertEquals(false, e.getOperation().isDistributed());
 +        assertEquals(this.sr, e.getRegion());
 +        assertEquals(null, e.getCallbackArgument());
 +      }
 +    } finally {
 +      MembershipManagerHelper.inhibitForcedDisconnectLogging(false);
 +      disconnectAllFromDS();
 +    }
 +  }
 +  enum Op {Initial, Create, Departure, Crash};  
 +  public class MyRML extends RegionMembershipListenerAdapter {
 +    private final int timeOut; 
 +    volatile Op lastOp;  
 +    private volatile RegionEvent lastEvent;
 +    private volatile DistributedMember[] initialMembers;
 +    private volatile boolean memberInitialized; // was the member initialized when afterRemoteRegionCreate was called?
 +    
 +    public MyRML(int to) {  this.timeOut = to;  }
 +    
 +    public boolean lastOpWasInitialMembers() {
 +      return waitForOp(Op.Initial);
 +    }
 +    public boolean lastOpWasCreate() {
 +      boolean result =  waitForOp(Op.Create);
 +      if (result) {
 +        // bug #44684 - afterRemoteRegionCreate should not be invoked before the remote region is initialized
 +        assertTrue("bug #44684 - expected remote member to be initialized when afterRemoteRegionCreate was invoked",
 +            this.memberInitialized);
 +      }
 +      return result;
 +    }
 +    public boolean lastOpWasDeparture() {
 +      return waitForOp(Op.Departure);
 +    }
 +    public String getOpName(Op op) {
 +      if (op == null) {
 +        return "null";
 +      }
 +      switch (op) {
 +      case Initial: return "Initial";
 +      case Create: return "Create";
 +      case Departure: return "Departure";
 +      case Crash: return "Crash";
 +      default: return "Unknown";
 +      }
 +    }
 +    private boolean waitForOp(final Op op) {
 +      WaitCriterion ev = new WaitCriterion() {
 +        public boolean done() {
 +          return MyRML.this.lastOp == op;
 +        }
 +        public String description() {
 +          return MyRML.this.toString() + " waiting for Op " + op + " when lastOp was " + getOpName(MyRML.this.lastOp);
 +        }
 +      };
 +      LogWriterUtils.getLogWriter().info(this.toString() + " waiting for Op " + getOpName(op)
 +          + " when lastOp was " + getOpName(this.lastOp));
 +      Wait.waitForCriterion(ev, this.timeOut, 200, true);
 +      assertEquals(op, this.lastOp);
 +      return true;
 +    }
 +    
 +    public void waitForCrashOp() {
 +      waitForOp(Op.Crash);
 +    }
 +    public RegionEvent getLastEvent() {
 +      return this.lastEvent;
 +    }
 +    public List getInitialMembers() {
 +      return Arrays.asList(this.initialMembers);
 +    }
 +    
 +    public void initialMembers(Region r, DistributedMember[] initialMembers) {
 +      this.lastOp = Op.Initial;
 +      this.lastEvent = null;
 +      this.initialMembers = initialMembers;
 +      LogWriterUtils.getLogWriter().info(this.toString() + " received initialMembers notification for region " + r
 +          + " with members " + Arrays.deepToString(initialMembers));
 +    }
 +    public void afterRemoteRegionCreate(RegionEvent event) {
 +      this.lastOp = Op.Create;
 +      this.lastEvent = event;
 +      CacheProfile cacheProfile = (CacheProfile)event.getCallbackArgument();
 +      if (cacheProfile != null) {
 +        this.memberInitialized = cacheProfile.regionInitialized;
 +        if (!this.memberInitialized) {
 +          LogWriterUtils.getLogWriter().warning("afterRemoteRegionCreate invoked when member is not done initializing!", new Exception("stack trace"));
 +        }
 +        LogWriterUtils.getLogWriter().info(this.toString() + " received afterRemoteRegionCreate notification for event " + event);
 +      } else {
 +        LogWriterUtils.getLogWriter().warning("afterRemoteRegionCreate was expecting a profile in the event callback but there was none. " +
 +        		" This indicates a problem with the test hook DistributedRegion.TEST_HOOK_ADD_PROFILE");
 +      }
 +    }
 +    public void afterRemoteRegionDeparture(RegionEvent event) {
 +      this.lastOp = Op.Departure;
 +      this.lastEvent = event;
 +      LogWriterUtils.getLogWriter().info(this.toString() + " received afterRemoteRegionDeparture notification for event " + event);
 +    }
 +    public void afterRemoteRegionCrash(RegionEvent event) {
 +      this.lastOp = Op.Crash;
 +      this.lastEvent = event;
 +      LogWriterUtils.getLogWriter().info(this.toString() + " received afterRemoteRegionCrash notification for event " + event);
 +    }
 +  }
 +}


[06/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/PartitionedRegionDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/PartitionedRegionDUnitTest.java
index 0408eb7,0000000..bc631bd
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/PartitionedRegionDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/PartitionedRegionDUnitTest.java
@@@ -1,554 -1,0 +1,554 @@@
 +/*
 + * 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.cache30;
 +
 +import java.io.Serializable;
 +import java.util.Collection;
 +import java.util.Iterator;
 +import java.util.Random;
 +import java.util.Set;
 +
 +import com.gemstone.gemfire.LogWriter;
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.EntryEvent;
 +import com.gemstone.gemfire.cache.EntryExistsException;
 +import com.gemstone.gemfire.cache.InterestPolicy;
 +import com.gemstone.gemfire.cache.PartitionAttributes;
 +import com.gemstone.gemfire.cache.PartitionAttributesFactory;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionAttributes;
 +import com.gemstone.gemfire.cache.RegionFactory;
 +import com.gemstone.gemfire.cache.RegionShortcut;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.SubscriptionAttributes;
 +import com.gemstone.gemfire.cache.util.CacheListenerAdapter;
 +import com.gemstone.gemfire.internal.cache.PartitionedRegionException;
 +import com.gemstone.gemfire.internal.logging.InternalLogWriter;
 +import com.gemstone.gemfire.internal.logging.PureLogWriter;
 +import com.gemstone.gemfire.test.dunit.Assert;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.SerializableCallable;
 +import com.gemstone.gemfire.test.dunit.SerializableRunnable;
 +import com.gemstone.gemfire.test.dunit.VM;
 +
 +/**
 + * This class tests the functionality of a cache {@link Region region}
 + * that has a scope of {@link Scope#DISTRIBUTED_ACK distributed ACK}
 + * and {@link PartitionAttributes partition-attributes}.
 + *
 + * @author Bruce Schuchardt
 + * @since 5.1
 + */
 +public class PartitionedRegionDUnitTest extends MultiVMRegionTestCase {
 +  
 +  static int oldLogLevel;
 +
 +  public PartitionedRegionDUnitTest(String name) {
 +    super(name);
 +  }
 +  
 +  /*
 +   * (non-Javadoc)
 +   * @see com.gemstone.gemfire.cache30.RegionTestCase#supportsSubregions()
 +   */
 +  protected boolean supportsSubregions() { return false; }
 +  
 +  /*
 +   * (non-Javadoc)
 +   * @see com.gemstone.gemfire.cache30.MultiVMRegionTestCase#supportsNetLoad()
 +   */
 +  protected boolean supportsNetLoad() { return false; }
 +
 +  /*
 +   * (non-Javadoc)
 +   * @see com.gemstone.gemfire.cache30.MultiVMRegionTestCase#supportsReplication()
 +   */
 +  protected boolean supportsReplication() { return false; }
 +  
 +  /*
 +   * (non-Javadoc)
 +   * @see com.gemstone.gemfire.cache30.MultiVMRegionTestCase#supportsTransactions()
 +   */
 +  protected boolean supportsTransactions() { return false; }
 +  
 +  /*
 +   * (non-Javadoc)
 +   * @see com.gemstone.gemfire.cache30.RegionTestCase#supportsLocalDestroyAndLocalInvalidate()
 +   */
 +  protected boolean supportsLocalDestroyAndLocalInvalidate() { return false; }
 +  
 +  public void testCacheLoaderModifyingArgument() throws InterruptedException {
 +    // TODO, implement a specific PR related test that properly reflects primary allocation
 +    // and event deliver based on that allocation
 +  }
 +
 +  public void testLocalAndRemoteCacheWriters() throws InterruptedException {
 +    // TODO, implement a specific PR related test that properly reflects primary allocation
 +    // and event deliver based on that allocation
 +  }
 +
 +  public void testLocalCacheLoader() {
 +    // TODO, implement a specific PR related test that properly reflects primary allocation
 +    // and event deliver based on that allocation
 +  }
 +  
 +
 +  /**
 +   * Returns region attributes for a partitioned region with distributed-ack scope
 +   */
 +  protected RegionAttributes getRegionAttributes() {
 +    AttributesFactory factory = new AttributesFactory();
 +    factory.setEarlyAck(false);
 +    factory.setPartitionAttributes((new PartitionAttributesFactory()).create());
 +    return factory.create();
 +  }
 +
 +  /**
 +   * Returns region attributes with a distributed-ack scope
 +   */
 +  protected RegionAttributes getNonPRRegionAttributes() {
 +    AttributesFactory factory = new AttributesFactory();
 +    factory.setScope(Scope.DISTRIBUTED_ACK);
 +    factory.setEarlyAck(false);
 +    return factory.create();
 +  }
 +
 +  public static int setLogLevel(LogWriter l, int logLevl) {
 +    int ret = -1;
 +    if (l instanceof PureLogWriter) {
 +        PureLogWriter pl = (PureLogWriter) l;
 +        ret = pl.getLogWriterLevel();
 +        pl.setLevel(logLevl);
 +    }
 +    return ret;
 +  }
 +
 +  void setVMInfoLogLevel() {
 +    SerializableRunnable runnable = new SerializableRunnable() {
 +      public void run() {
 +        oldLogLevel = setLogLevel(getCache().getLogger(), InternalLogWriter.INFO_LEVEL);
 +      }
 +    };
 +    for (int i=0; i<4; i++) {
 +      Host.getHost(0).getVM(i).invoke(runnable);
 +    }
 +  }
 +    
 +  void resetVMLogLevel() {
 +    SerializableRunnable runnable = new SerializableRunnable() {
 +      public void run() {
 +        setLogLevel(getCache().getLogger(), oldLogLevel);
 +      }
 +    };
 +    for (int i=0; i<4; i++) {
 +      Host.getHost(0).getVM(i).invoke(runnable);
 +    }
 +  }
 +    
 +  //////////////////////  Test Methods  //////////////////////
 +
 +  public static boolean InvalidateInvoked = false;
 +  
 +  /**
 +   * Bug #47235 concerns assertion failures being thrown when there is a
 +   * member that receives adjunct messages (as in a WAN gateway, a peer
 +   * with clients, etc).
 +   * 
 +   * @throws Exception
 +   */
 +  public void testRegionInvalidationWithAdjunctMessages() throws Exception {
 +    final String name = getUniqueName();
 +    VM vm1 = Host.getHost(0).getVM(1);
 +    Cache cache = getCache();
 +    RegionFactory fact = getCache().createRegionFactory(RegionShortcut.PARTITION);
 +    Region pr = fact.create(name+"Region");
 +    pr.put("Object1", "Value1");
 +    
 +    vm1.invoke(new SerializableRunnable("create PR") {
 +      @Override
 +      public void run() {
 +        RegionFactory fact = getCache().createRegionFactory(RegionShortcut.PARTITION);
 +        fact.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
 +        fact.addCacheListener(new CacheListenerAdapter(){
 +          @Override
 +          public void afterInvalidate(EntryEvent event) {
 +            com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("afterInvalidate invoked with " + event);
 +            InvalidateInvoked = true;
 +          }
 +        });
 +        fact.create(name+"Region");
 +      }
 +    });
 +    try {
 +      pr.invalidateRegion();
 +      assertTrue("vm1 should have invoked the listener for an invalidateRegion operation",
 +          (Boolean)vm1.invoke(new SerializableCallable("getStatus") {
 +            public Object call() {
 +              return InvalidateInvoked;
 +            }
 +          }));
 +    } finally {
 +      disconnectAllFromDS();
 +    }
 +  }
 +
 +  /**
 +   * Tests the compatibility of creating certain kinds of subregions
 +   * of a local region.
 +   *
 +   * @see Region#createSubregion
 +   */
 +  public void testIncompatibleSubregions()
 +    throws CacheException, InterruptedException {
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +
 +    final String name = this.getUniqueName() + "-PR";
 +    vm0.invoke(new SerializableRunnable("Create partitioned Region") {
 +        public void run() {
 +          try {
 +            
 +            createRegion(name, "INCOMPATIBLE_ROOT", getRegionAttributes());
 +          } catch (CacheException ex) {
 +            Assert.fail("While creating Partitioned region", ex);
 +          }
 +        }
 +      });
 +
 +    vm1.invoke(new SerializableRunnable("Create non-partitioned Region") {
 +        public void run() {
 +          try {
 +            AttributesFactory factory =
 +              new AttributesFactory(getNonPRRegionAttributes());
 +            try {
 +              createRegion(name, "INCOMPATIBLE_ROOT", factory.create());
 +              fail("Should have thrown an IllegalStateException");
 +            } catch (IllegalStateException ex) {
 +              // pass...
 +            }
 +
 +          } catch (CacheException ex) {
 +            Assert.fail("While creating Partitioned Region", ex);
 +          }
 +        }
 +      });
 +  } 
 +  
 +
 +  private void setupExtendedTest(final String regionName, final int numVals) {
 +    Host host = Host.getHost(0);
 +    SerializableRunnable createPR = new SerializableRunnable("createPartitionedRegion") {
 +        public void run() {
 +          try {
 +            createRegion(regionName, "root", getRegionAttributes());
 +          } catch (CacheException ex) {
 +            Assert.fail("While creating Partitioned region", ex);
 +          }
 +        }
 +    };
 +    for (int i=1; i<4; i++) {
 +      host.getVM(i).invoke(createPR);
 +    }
 +    VM vm0 = host.getVM(0);
 +    vm0.invoke(new SerializableRunnable("Populate Partitioned Region") {
 +        public void run() {
 +          Region region = null;
 +          try {
 +            region = createRegion(regionName, "root", getRegionAttributes());
 +            // since random keys are being used, we might hit duplicates
 +            region.getCache().getLogger().info("<ExpectedException action=add>"
 +                + "com.gemstone.gemfire.cache.EntryExistsException"
 +                + "</ExpectedException>");
 +            java.util.Random rand = new java.util.Random(System.currentTimeMillis());
 +            for (int i=0; i<numVals; i++) {
 +              boolean created = false;
 +              while (!created) {
 +                try {
 +                  int val = rand.nextInt(100000000);
 +                  String key = String.valueOf(val);
 +                  region.create(key, new Integer(val));
 +                  created = true;
 +                }
 +                catch (EntryExistsException eee) {
 +                  // loop to try again
 +                }
 +              }
 +            }
 +          }
 +          catch (Exception ex) {
 +            Assert.fail("while creating or populating partitioned region", ex);
 +          }
 +          finally {
 +            if (region != null) {
 +              region.getCache().getLogger().info("<ExpectedException action=remove>"
 +                + "com.gemstone.gemfire.cache.EntryExistsException"
 +                + "</ExpectedException>");
 +            }
 +          }
 +        }
 +    });
 +  }
 +  
 +  /**
 +   * test with multiple vms and a decent spread of keys
 +   */
 +  public void testExtendedKeysValues() {
 +    final String regionName = getUniqueName();
 +    final int numEntries = 20000;
 +
 +    // since this test has to create a lot of entries, info log level is used.
 +    // comment out the setting of this and rerun if there are problems
 +    setVMInfoLogLevel();
 +    try {
 +      setupExtendedTest(regionName, numEntries);
 +      
 +      Host host = Host.getHost(0);
 +      VM vm0 = host.getVM(0);
 +      vm0.invoke(new SerializableRunnable("exercise Region.values") {
 +          public void run() {
 +            try {
 +              Region region = getRootRegion().getSubregion(regionName);
 +              Collection values = region.values();
 +              Set keys = region.keySet();
 +              Set entries = region.entrySet();
 +              assertEquals("value collection size was not the expected value", numEntries, values.size());
 +              assertEquals("key set size was not the expected value", numEntries, keys.size());
 +              assertEquals("entry set size was not the expected value", numEntries, entries.size());
 +              assertEquals("region size was not the expected value", numEntries, region.size());
 +              Iterator valuesIt = values.iterator();
 +              Iterator keysIt = keys.iterator();
 +              Iterator entriesIt = entries.iterator();
 +              for (int i=0; i<numEntries; i++) {
 +                assertTrue(valuesIt.hasNext());
 +                Integer value = (Integer)valuesIt.next();
 +                assertNotNull("value was null", value);
 +  
 +                assertTrue(keysIt.hasNext());
 +                String key = (String)keysIt.next();
 +                assertNotNull("key was null", key);
 +  
 +                assertTrue(entriesIt.hasNext());
 +                Region.Entry entry = (Region.Entry)entriesIt.next();
 +                assertNotNull("entry was null", entry);
 +                assertNotNull("entry key was null", entry.getKey());
 +                assertNotNull("entry value was null", entry.getValue());
 +              }
 +              assertTrue("should have been end of values iteration", !valuesIt.hasNext());
 +              assertTrue("should have been end of keys iteration", !keysIt.hasNext());
 +              assertTrue("should have been end of entries iteration", !entriesIt.hasNext());
 +            }
 +            catch (Exception ex) {
 +              try {
 +                getRootRegion().getSubregion(regionName).destroyRegion();
 +              }
 +              catch (Exception ex2) {
 +              }
 +              Assert.fail("Unexpected exception", ex);
 +            }
 +          }
 +      });
 +    }
 +    finally {
 +      resetVMLogLevel();
 +    }
 +  }
 +  
 +  // these tests make no sense for partitioned regions
 +  public void testDefinedEntryUpdated() {
 +    unimplemented();
 +  }
 +  public void testRemoteCacheListener() {
 +    unimplemented();
 +  }
 +  
 +  
 +  // these tests require getEntry support - need an alternative way of checking
 +  // the results that can be overridden here
 +//  public void testDistributedUpdate() {
 +//    unimplemented();
 +//  }
 +//  public void testDistributedPutNoUpdate() {
 +//    unimplemented();
 +//  }
 +//  public void testDistributedInvalidate() {
 +//    unimplemented();
 +//  }
 +//  public void testDistributedInvalidate4() {
 +//    unimplemented();
 +//  }
 +//  public void testContainsKey() {
 +//    unimplemented();
 +//  }
 +//  public void testBadRegionAccess() {
 +//    unimplemented();
 +//  }
 +//  public void testPutNonExistentEntry() {
 +//    unimplemented();
 +//  }
 +//  public void testDestroyEntry() {
 +//    unimplemented();
 +//  }
 +//  public void testInvalidateEntry() {
 +//    unimplemented();
 +//  }
 +//  public void testDistributedDestroy() {
 +//    unimplemented();
 +//  }
 +  
 +
 +  // user attributes aren't supported in partitioned regions at this time (5.1)
 +  public void testEntryUserAttribute() {
 +    unimplemented();
 +  }
 +
 +  
 +  // these tests require misc Region operations not currently supported by PRs
 +  public void testInvalidateRegion() {
 +    unimplemented();
 +  }
 +  public void testLocalDestroyRegion() {
 +    unimplemented();
 +  }
 +  public void testLocalInvalidateRegion() {
 +    unimplemented();
 +  }
 +  public void testSnapshot() {
 +    unimplemented();
 +  }
 +  public void testRootSnapshot() {
 +    unimplemented();
 +  }
 +
 +  private void unimplemented() {
 +//    StackTraceElement stack[] = new Exception("dummy").getStackTrace();
 +//    getLogWriter().info(stack[1].getClassName() + "." + stack[1].getMethodName()
 +//        + ": this test is not implemented for PartitionedRegions at this time");
 +  }
 +  static class PoisonedKey implements Serializable {
 +    static volatile boolean poisoned = false;
 +    static volatile boolean poisonDetected = false;
 +    
 +    /**
 +     * Accessed via reflection
 +     * @return true if poison found
 +     */
 +    public static boolean poisonFound() {
 +      boolean result = poisonDetected;
 +      poisonDetected = false; // restore default static value
 +      return result;
 +    }
 +    
 +    public int hashCode() {
 +      int result = k.hashCode();
 +      synchronized (PoisonedKey.class) {
 +        if (poisoned) {
 +          result += (new Random()).nextInt();
 +        }
 +      }
 +      return result;
 +    }
 +
 +    final String k;
 +    
 +    PoisonedKey(String s) {
 +      this.k = s;
 +    }
 +    
 +    public boolean equals(Object o) {
 +      if (o == null) {
 +        return false;
 +      }
 +      if (!(o instanceof PoisonedKey)) {
 +        return false;
 +      }
 +      PoisonedKey po = (PoisonedKey)o;
 +      if (k == null) {
 +        return po.k == null;
 +      }
 +      return k.equals(po.k);
 +    }
 +  }
 +  
 +  public void testBadHash() {
 +    final String regionName = getUniqueName();
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    SerializableRunnable createPR = new SerializableRunnable("createPartitionedRegion") {
 +        public void run() {
 +          try {
 +            createRegion(regionName, "root", getRegionAttributes());
 +          } catch (CacheException ex) {
 +            Assert.fail("While creating Partitioned region", ex);
 +          }
 +        }
 +    };
 +    vm0.invoke(createPR);
 +    vm1.invoke(createPR);
 +
 +    vm0.invoke(new SerializableRunnable("Populate 1") { 
 +      public void run() {
 +        Region region = getRootRegion().getSubregion(regionName);
 +        for (int i = 0; i < 10; i ++) {
 +          String st = Integer.toString(i);
 +          PoisonedKey pk = new PoisonedKey(st);
 +          region.create(pk, st);
 +        }
 +      }
 +    });
 +    
 +    // Verify values are readily accessible
 +    vm1.invoke(new SerializableRunnable("Read 1") { 
 +      public void run() {
 +        Region region = getRootRegion().getSubregion(regionName);
 +        for (int i = 0; i < 10; i ++) {
 +          String st = Integer.toString(i);
 +          PoisonedKey pk = new PoisonedKey(st);
 +          assertTrue("Keys screwed up too early", region.get(pk).equals(st));
 +        }
 +      }
 +    });
 +    
 +    // Bucket ID's will be screwed up with these creates.
 +    vm0.invoke(new SerializableRunnable("Populate 2") { 
 +      public void run() {
 +        Region region = getRootRegion().getSubregion(regionName);
 +        PoisonedKey.poisoned = true;
 +        try {
 +          for (int i = 10; i < 20; i ++) {
 +            String st = Integer.toString(i);
 +            PoisonedKey pk = new PoisonedKey(st);
 +            region.create(pk, st);
 +          }
 +        }
 +        catch (PartitionedRegionException e) {
 +          PoisonedKey.poisonDetected = true;
 +        } finally {
 +          PoisonedKey.poisoned = false; // restore default static value
 +        }
 +      }
 +    });
 +    
-     boolean success = vm0.invokeBoolean(PoisonedKey.class, "poisonFound");
++    boolean success = vm0.invoke(() -> PoisonedKey.poisonFound());
 +    assertTrue("Hash mismatch not found", success);
 +  }
 +}
 +

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/ProxyDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/ProxyDUnitTest.java
index 54554ab,0000000..8d6c14c
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/ProxyDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/ProxyDUnitTest.java
@@@ -1,546 -1,0 +1,546 @@@
 +/*
 + * 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.cache30;
 +
 +import java.util.HashMap;
 +import java.util.Map;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.AttributesMutator;
 +import com.gemstone.gemfire.cache.CacheEvent;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheListener;
 +import com.gemstone.gemfire.cache.CacheLoader;
 +import com.gemstone.gemfire.cache.CacheLoaderException;
 +import com.gemstone.gemfire.cache.CacheWriter;
 +import com.gemstone.gemfire.cache.CacheWriterException;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.EntryEvent;
 +import com.gemstone.gemfire.cache.InterestPolicy;
 +import com.gemstone.gemfire.cache.LoaderHelper;
 +import com.gemstone.gemfire.cache.Operation;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionEvent;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.SubscriptionAttributes;
 +import com.gemstone.gemfire.cache.util.CacheWriterAdapter;
 +import com.gemstone.gemfire.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.internal.DMStats;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.VM;
 +
 +/**
 + * Make sure that operations are distributed and done in
 + * regions remote from a PROXY
 + *
 + * @author darrel
 + * @since 5.0
 + */
 +public class ProxyDUnitTest extends CacheTestCase {
 +
 +  private transient Region r;
 +  private transient DistributedMember otherId;
 +  protected transient int clInvokeCount;
 +  protected transient CacheEvent clLastEvent;
 +  
 +  public ProxyDUnitTest(String name) {
 +    super(name);
 +  }
 +
 +  private VM getOtherVm() {
 +    Host host = Host.getHost(0);
 +    return host.getVM(0);
 +  }
 +    
 +  private void initOtherId() {
 +    VM vm = getOtherVm();
 +    vm.invoke(new CacheSerializableRunnable("Connect") {
 +        public void run2() throws CacheException {
 +          getCache();
 +        }
 +      });
-     this.otherId = (DistributedMember)vm.invoke(ProxyDUnitTest.class, "getVMDistributedMember");
++    this.otherId = (DistributedMember)vm.invoke(() -> ProxyDUnitTest.getVMDistributedMember());
 +  }
 +  private void doCreateOtherVm() {
 +    VM vm = getOtherVm();
 +    vm.invoke(new CacheSerializableRunnable("create root") {
 +        public void run2() throws CacheException {
 +          AttributesFactory af = new AttributesFactory();
 +          af.setDataPolicy(DataPolicy.REPLICATE);
 +          af.setScope(Scope.DISTRIBUTED_ACK);
 +          createRootRegion("ProxyDUnitTest", af.create());
 +        }
 +      });
 +  }
 +
 +  public static DistributedMember getVMDistributedMember() {
 +    return InternalDistributedSystem.getAnyInstance().getDistributedMember();
 +  }
 +  
 +  //////////////////////  Test Methods  //////////////////////
 +
 +  /**
 +   * check distributed ops that originate in a PROXY are correctly distributed
 +   * to non-proxy regions.
 +   */
 +  private void distributedOps(DataPolicy dp, InterestPolicy ip) throws CacheException {
 +    initOtherId();
 +    AttributesFactory af = new AttributesFactory();
 +    af.setDataPolicy(dp);
 +    af.setSubscriptionAttributes(new SubscriptionAttributes(ip));
 +    af.setScope(Scope.DISTRIBUTED_ACK);
 +    Region r = createRootRegion("ProxyDUnitTest", af.create());
 +
 +    doCreateOtherVm();
 +
 +    r.put("putkey", "putvalue1");
 +    
 +    getOtherVm().invoke(new CacheSerializableRunnable("check put") {
 +        public void run2() throws CacheException {
 +          Region r = getRootRegion("ProxyDUnitTest");
 +          assertEquals(true, r.containsKey("putkey"));
 +          assertEquals("putvalue1", r.getEntry("putkey").getValue());
 +          r.put("putkey", "putvalue2");
 +        }
 +      });
 +    
 +    assertEquals(false, r.containsKey("putkey"));
 +    assertEquals("putvalue2", r.get("putkey")); // netsearch
 +
 +    r.invalidate("putkey");
 +
 +    getOtherVm().invoke(new CacheSerializableRunnable("check invalidate") {
 +        public void run2() throws CacheException {
 +          Region r = getRootRegion("ProxyDUnitTest");
 +          assertEquals(true, r.containsKey("putkey"));
 +          assertEquals(null, r.getEntry("putkey").getValue());
 +        }
 +      });
 +    assertEquals(null, r.get("putkey")); // invalid so total miss
 +
 +    r.destroy("putkey");
 +
 +    getOtherVm().invoke(new CacheSerializableRunnable("check destroy") {
 +        public void run2() throws CacheException {
 +          Region r = getRootRegion("ProxyDUnitTest");
 +          assertEquals(false, r.containsKey("putkey"));
 +        }
 +      });
 +    
 +    assertEquals(null, r.get("putkey")); // total miss
 +
 +    r.create("createKey", "createValue1");
 +    getOtherVm().invoke(new CacheSerializableRunnable("check create") {
 +        public void run2() throws CacheException {
 +          Region r = getRootRegion("ProxyDUnitTest");
 +          assertEquals(true, r.containsKey("createKey"));
 +          assertEquals("createValue1", r.getEntry("createKey").getValue());
 +        }
 +      });
 +    {
 +      Map m = new HashMap();
 +      m.put("putAllKey1", "putAllValue1");
 +      m.put("putAllKey2", "putAllValue2");
 +      r.putAll(m, "putAllCallback");
 +    }
 +    getOtherVm().invoke(new CacheSerializableRunnable("check putAll") {
 +        public void run2() throws CacheException {
 +          Region r = getRootRegion("ProxyDUnitTest");
 +          assertEquals(true, r.containsKey("putAllKey1"));
 +          assertEquals("putAllValue1", r.getEntry("putAllKey1").getValue());
 +          assertEquals(true, r.containsKey("putAllKey2"));
 +          assertEquals("putAllValue2", r.getEntry("putAllKey2").getValue());
 +        }
 +      });
 +    r.clear();
 +    getOtherVm().invoke(new CacheSerializableRunnable("check clear") {
 +        public void run2() throws CacheException {
 +          Region r = getRootRegion("ProxyDUnitTest");
 +          assertEquals(0, r.size());
 +        }
 +      });
 +
 +    getOtherVm().invoke(new CacheSerializableRunnable("install CacheWriter") {
 +        public void run2() throws CacheException {
 +          Region r = getRootRegion("ProxyDUnitTest");
 +          AttributesMutator am = r.getAttributesMutator();
 +          CacheWriter cw = new CacheWriterAdapter() {
 +              public void beforeCreate(EntryEvent event) throws CacheWriterException {
 +                throw new CacheWriterException("expected");
 +              }
 +            };
 +          am.setCacheWriter(cw);
 +        }
 +      });
 +    try {
 +      r.put("putkey", "putvalue");
 +      fail("expected CacheWriterException");
 +    } catch (CacheWriterException expected) {
 +    }
 +    getOtherVm().invoke(new CacheSerializableRunnable("check clear") {
 +        public void run2() throws CacheException {
 +          Region r = getRootRegion("ProxyDUnitTest");
 +          assertEquals(0, r.size());
 +        }
 +      });
 +
 +    assertEquals(null, r.get("loadkey")); // total miss
 +    getOtherVm().invoke(new CacheSerializableRunnable("install CacheLoader") {
 +        public void run2() throws CacheException {
 +          Region r = getRootRegion("ProxyDUnitTest");
 +          AttributesMutator am = r.getAttributesMutator();
 +          am.setCacheWriter(null); // clear csche writer
 +          CacheLoader cl = new CacheLoader() {
 +              public Object load(LoaderHelper helper) throws CacheLoaderException {
 +                if (helper.getKey().equals("loadkey")) {
 +                  return "loadvalue";
 +                } else if (helper.getKey().equals("loadexception")) {
 +                  throw new CacheLoaderException("expected");
 +                } else {
 +                  return null;
 +                }
 +              }
 +              public void close() {
 +              }
 +            };
 +          am.setCacheLoader(cl);
 +        }
 +      });
 +    assertEquals("loadvalue", r.get("loadkey")); // net load
 +    assertEquals(null, r.get("foobar")); // total miss
 +    try {
 +      r.get("loadexception");
 +      fail("expected CacheLoaderException");
 +    } catch (CacheLoaderException expected) {
 +    }
 +    r.destroyRegion();
 +    getOtherVm().invoke(new CacheSerializableRunnable("check clear") {
 +        public void run2() throws CacheException {
 +          Region r = getRootRegion("ProxyDUnitTest");
 +          assertEquals(null, r);
 +        }
 +      });
 +  }
 +  /**
 +   * Gets the DMStats for the vm's DM
 +   */
 +  private DMStats getDMStats() {
 +    return ((InternalDistributedSystem)getCache().getDistributedSystem())
 +      .getDistributionManager().getStats();
 +  }
 +  /**
 +   * check remote ops done in a normal vm are correctly distributed to PROXY regions
 +   */
 +  private void remoteOriginOps(DataPolicy dp, InterestPolicy ip) throws CacheException {
 +    initOtherId();
 +    AttributesFactory af = new AttributesFactory();
 +    af.setDataPolicy(dp);
 +    af.setSubscriptionAttributes(new SubscriptionAttributes(ip));
 +    af.setScope(Scope.DISTRIBUTED_ACK);
 +    CacheListener cl1 = new CacheListener() {
 +        public void afterUpdate(EntryEvent e) {
 +          clLastEvent = e;
 +          clInvokeCount++;
 +        }
 +        public void afterCreate(EntryEvent e) {
 +          clLastEvent = e;
 +          clInvokeCount++;
 +        }
 +        public void afterInvalidate(EntryEvent e) {
 +          clLastEvent = e;
 +          clInvokeCount++;
 +        }
 +        public void afterDestroy(EntryEvent e) {
 +          clLastEvent = e;
 +          clInvokeCount++;
 +        }
 +        public void afterRegionInvalidate(RegionEvent e) {
 +          clLastEvent = e;
 +          clInvokeCount++;
 +        }
 +        public void afterRegionDestroy(RegionEvent e) {
 +          clLastEvent = e;
 +          clInvokeCount++;
 +        }
 +        public void afterRegionClear(RegionEvent e) {
 +          clLastEvent = e;
 +          clInvokeCount++;
 +        }
 +        public void afterRegionCreate(RegionEvent e) {
 +        }
 +        public void afterRegionLive(RegionEvent e) {
 +        }
 +        public void close() {
 +        }
 +      };
 +    af.addCacheListener(cl1);
 +    Region r = createRootRegion("ProxyDUnitTest", af.create());
 +    this.clInvokeCount = 0;
 +
 +    doCreateOtherVm();
 +
 +    DMStats stats = getDMStats();
 +    long receivedMsgs = stats.getReceivedMessages();
 +
 +    if (ip.isAll()) {
 +      getOtherVm().invoke(new CacheSerializableRunnable("do put") {
 +          public void run2() throws CacheException {
 +            Region r = getRootRegion("ProxyDUnitTest");
 +            r.put("p", "v");
 +          }
 +        });
 +      assertEquals(1, this.clInvokeCount);
 +      assertEquals(Operation.CREATE, this.clLastEvent.getOperation());
 +      assertEquals(true, this.clLastEvent.isOriginRemote());
 +      assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
 +      assertEquals(null, ((EntryEvent)this.clLastEvent).getOldValue());
 +      assertEquals(false, ((EntryEvent)this.clLastEvent).isOldValueAvailable()); // failure
 +      assertEquals("v", ((EntryEvent)this.clLastEvent).getNewValue());
 +      assertEquals("p", ((EntryEvent)this.clLastEvent).getKey());
 +      this.clInvokeCount = 0;
 +      
 +      getOtherVm().invoke(new CacheSerializableRunnable("do create") {
 +          public void run2() throws CacheException {
 +            Region r = getRootRegion("ProxyDUnitTest");
 +            r.create("c", "v");
 +          }
 +        });
 +      assertEquals(1, this.clInvokeCount);
 +      assertEquals(Operation.CREATE, this.clLastEvent.getOperation());
 +      assertEquals(true, this.clLastEvent.isOriginRemote());
 +      assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
 +      assertEquals(null, ((EntryEvent)this.clLastEvent).getOldValue());
 +      assertEquals(false, ((EntryEvent)this.clLastEvent).isOldValueAvailable());
 +      assertEquals("v", ((EntryEvent)this.clLastEvent).getNewValue());
 +      assertEquals("c", ((EntryEvent)this.clLastEvent).getKey());
 +      this.clInvokeCount = 0;
 +      
 +      getOtherVm().invoke(new CacheSerializableRunnable("do update") {
 +          public void run2() throws CacheException {
 +            Region r = getRootRegion("ProxyDUnitTest");
 +            r.put("c", "v2");
 +          }
 +        });
 +      assertEquals(1, this.clInvokeCount);
 +      assertEquals(Operation.UPDATE, this.clLastEvent.getOperation());
 +      assertEquals(true, this.clLastEvent.isOriginRemote());
 +      assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
 +      assertEquals(null, ((EntryEvent)this.clLastEvent).getOldValue());
 +      assertEquals(false, ((EntryEvent)this.clLastEvent).isOldValueAvailable());
 +      assertEquals("v2", ((EntryEvent)this.clLastEvent).getNewValue());
 +      assertEquals("c", ((EntryEvent)this.clLastEvent).getKey());
 +      this.clInvokeCount = 0;
 +
 +      getOtherVm().invoke(new CacheSerializableRunnable("do invalidate") {
 +          public void run2() throws CacheException {
 +            Region r = getRootRegion("ProxyDUnitTest");
 +            r.invalidate("c");
 +          }
 +        });
 +      assertEquals(1, this.clInvokeCount);
 +      assertEquals(Operation.INVALIDATE, this.clLastEvent.getOperation());
 +      assertEquals(true, this.clLastEvent.isOriginRemote());
 +      assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
 +      assertEquals(null, ((EntryEvent)this.clLastEvent).getOldValue());
 +      assertEquals(false, ((EntryEvent)this.clLastEvent).isOldValueAvailable());
 +      assertEquals(null, ((EntryEvent)this.clLastEvent).getNewValue());
 +      assertEquals("c", ((EntryEvent)this.clLastEvent).getKey());
 +      this.clInvokeCount = 0;
 +      
 +      getOtherVm().invoke(new CacheSerializableRunnable("do destroy") {
 +          public void run2() throws CacheException {
 +            Region r = getRootRegion("ProxyDUnitTest");
 +            r.destroy("c");
 +          }
 +        });
 +      assertEquals(1, this.clInvokeCount);
 +      assertEquals(Operation.DESTROY, this.clLastEvent.getOperation());
 +      assertEquals(true, this.clLastEvent.isOriginRemote());
 +      assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
 +      assertEquals(null, ((EntryEvent)this.clLastEvent).getOldValue());
 +      assertEquals(false, ((EntryEvent)this.clLastEvent).isOldValueAvailable());
 +      assertEquals(null, ((EntryEvent)this.clLastEvent).getNewValue());
 +      assertEquals("c", ((EntryEvent)this.clLastEvent).getKey());
 +      this.clInvokeCount = 0;
 +      
 +      getOtherVm().invoke(new CacheSerializableRunnable("do putAll") {
 +          public void run2() throws CacheException {
 +            Region r = getRootRegion("ProxyDUnitTest");
 +            Map m = new HashMap();
 +            m.put("putAllKey1", "putAllValue1");
 +            m.put("putAllKey2", "putAllValue2");
 +            r.putAll(m);
 +          }
 +        });
 +      assertEquals(2, this.clInvokeCount);
 +      // @todo darrel; check putAll events
 +      this.clInvokeCount = 0;
 +
 +      getOtherVm().invoke(new CacheSerializableRunnable("do netsearch") {
 +          public void run2() throws CacheException {
 +            Region r = getRootRegion("ProxyDUnitTest");
 +            assertEquals(null, r.get("loadkey")); // total miss
 +          }
 +        });
 +      assertEquals(0, this.clInvokeCount);
 +      
 +    } else {
 +      getOtherVm().invoke(new CacheSerializableRunnable("do entry ops") {
 +          public void run2() throws CacheException {
 +            Region r = getRootRegion("ProxyDUnitTest");
 +            r.put("p", "v");
 +            r.create("c", "v");
 +            r.put("c", "v"); // update
 +            r.invalidate("c");
 +            r.destroy("c");
 +            {
 +              Map m = new HashMap();
 +              m.put("putAllKey1", "putAllValue1");
 +              m.put("putAllKey2", "putAllValue2");
 +              r.putAll(m);
 +            }
 +            assertEquals(null, r.get("loadkey")); // total miss
 +          }
 +        });
 +
 +      assertEquals(0, this.clInvokeCount);
 +      assertEquals(0, r.size());
 +      // check the stats to make sure none of the above sent up messages
 +      assertEquals(receivedMsgs, stats.getReceivedMessages());
 +    }
 +
 +    {
 +      AttributesMutator am = r.getAttributesMutator();
 +      CacheLoader cl = new CacheLoader() {
 +          public Object load(LoaderHelper helper) throws CacheLoaderException {
 +            if (helper.getKey().equals("loadkey")) {
 +              return "loadvalue";
 +            } else if (helper.getKey().equals("loadexception")) {
 +              throw new CacheLoaderException("expected");
 +            } else {
 +              return null;
 +            }
 +          }
 +          public void close() {
 +          }
 +        };
 +      am.setCacheLoader(cl);
 +    }
 +
 +    receivedMsgs = stats.getReceivedMessages();
 +    getOtherVm().invoke(new CacheSerializableRunnable("check net loader") {
 +        public void run2() throws CacheException {
 +          Region r = getRootRegion("ProxyDUnitTest");
 +          assertEquals("loadvalue", r.get("loadkey")); // net load
 +          assertEquals(null, r.get("foobar")); // total miss
 +          try {
 +            r.get("loadexception");
 +            fail("expected CacheLoaderException");
 +          } catch (CacheLoaderException expected) {
 +          }
 +        }
 +      });
 +    assertTrue(stats.getReceivedMessages() > receivedMsgs);
 +    if (ip.isAll()) {
 +      assertEquals(1, this.clInvokeCount);
 +      assertEquals(Operation.NET_LOAD_CREATE, this.clLastEvent.getOperation());
 +      assertEquals(true, this.clLastEvent.isOriginRemote());
 +      assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
 +      assertEquals(null, ((EntryEvent)this.clLastEvent).getOldValue());
 +      assertEquals(false, ((EntryEvent)this.clLastEvent).isOldValueAvailable());
 +      this.clInvokeCount = 0;
 +    } else {
 +      assertEquals(0, this.clInvokeCount);
 +    }
 +
 +    {
 +      AttributesMutator am = r.getAttributesMutator();
 +      am.setCacheLoader(null);
 +      CacheWriter cw = new CacheWriterAdapter() {
 +          public void beforeCreate(EntryEvent event) throws CacheWriterException {
 +            throw new CacheWriterException("expected");
 +          }
 +        };
 +      am.setCacheWriter(cw);
 +    }
 +    receivedMsgs = stats.getReceivedMessages();
 +    getOtherVm().invoke(new CacheSerializableRunnable("check net write") {
 +        public void run2() throws CacheException {
 +          Region r = getRootRegion("ProxyDUnitTest");
 +          try {
 +            r.put("putkey", "putvalue");
 +            fail("expected CacheWriterException");
 +          } catch (CacheWriterException expected) {
 +          }
 +        }
 +      });
 +    assertTrue(stats.getReceivedMessages() > receivedMsgs);
 +    {
 +      AttributesMutator am = r.getAttributesMutator();
 +      am.setCacheWriter(null);
 +    }
 +    assertEquals(0, this.clInvokeCount);
 +    this.clLastEvent = null;
 +    getOtherVm().invoke(new CacheSerializableRunnable("check region invalidate") {
 +        public void run2() throws CacheException {
 +          Region r = getRootRegion("ProxyDUnitTest");
 +          r.invalidateRegion();
 +        }
 +      });
 +    assertEquals(1, this.clInvokeCount);
 +    assertEquals(Operation.REGION_INVALIDATE, this.clLastEvent.getOperation());
 +    assertEquals(true, this.clLastEvent.isOriginRemote());
 +    assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
 +
 +    this.clLastEvent = null;
 +    getOtherVm().invoke(new CacheSerializableRunnable("check region clear") {
 +        public void run2() throws CacheException {
 +          Region r = getRootRegion("ProxyDUnitTest");
 +          r.clear();
 +        }
 +      });
 +    assertEquals(2, this.clInvokeCount);
 +    assertEquals(Operation.REGION_CLEAR, this.clLastEvent.getOperation());
 +    assertEquals(true, this.clLastEvent.isOriginRemote());
 +    assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
 +
 +    this.clLastEvent = null;
 +    getOtherVm().invoke(new CacheSerializableRunnable("check region destroy") {
 +        public void run2() throws CacheException {
 +          Region r = getRootRegion("ProxyDUnitTest");
 +          r.destroyRegion();
 +        }
 +      });
 +    assertEquals(3, this.clInvokeCount);
 +    assertEquals(Operation.REGION_DESTROY, this.clLastEvent.getOperation());
 +    assertEquals(true, this.clLastEvent.isOriginRemote());
 +    assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
 +    assertTrue(r.isDestroyed());
 +  }
 +  
 +  public void testDistributedOpsPROXY() throws CacheException {
 +    distributedOps(DataPolicy.EMPTY, InterestPolicy.CACHE_CONTENT);
 +  }
 +
 +  public void testRemoteOriginOpsPROXY() throws CacheException {
 +    remoteOriginOps(DataPolicy.EMPTY, InterestPolicy.CACHE_CONTENT);
 +  }
 +  public void testRemoteOriginOpsPROXY_ALL() throws CacheException {
 +    remoteOriginOps(DataPolicy.EMPTY, InterestPolicy.ALL);
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/PutAllCallBkRemoteVMDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/PutAllCallBkRemoteVMDUnitTest.java
index caa8333,0000000..01a1f42
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/PutAllCallBkRemoteVMDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/PutAllCallBkRemoteVMDUnitTest.java
@@@ -1,401 -1,0 +1,401 @@@
 +/*
 + * 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.
 + */
 +
 +/*
 + * PutAllCallBkRemoteVMDUnitTest.java
 + *
 + * Created on September 2, 2005, 2:49 PM
 +*/
 +package com.gemstone.gemfire.cache30;
 +
 +import java.util.HashMap;
 +import java.util.Map;
 +import java.util.Properties;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheFactory;
 +import com.gemstone.gemfire.cache.CacheListener;
 +import com.gemstone.gemfire.cache.CacheWriter;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.EntryEvent;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionAttributes;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.util.CacheListenerAdapter;
 +import com.gemstone.gemfire.cache.util.CacheWriterAdapter;
 +import com.gemstone.gemfire.distributed.DistributedSystem;
 +import com.gemstone.gemfire.test.dunit.DistributedTestCase;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.LogWriterUtils;
 +import com.gemstone.gemfire.test.dunit.VM;
 +import com.gemstone.gemfire.test.dunit.Wait;
 +import com.gemstone.gemfire.test.dunit.WaitCriterion;
 +
 +/**
 + *
 + * @author vjadhav
 + */
 +public class PutAllCallBkRemoteVMDUnitTest extends DistributedTestCase {
 +    
 +    /** Creates a new instance of PutAllCallBkRemoteVMDUnitTest */
 +    public PutAllCallBkRemoteVMDUnitTest(String name) {
 +        super(name);
 +    }
 +    
 +    static volatile Cache cache;
 +    static Properties props = new Properties();
 +    static Properties propsWork = new Properties();
 +    static volatile DistributedSystem ds = null;
 +    static volatile Region region;
 +    static volatile Region paperRegion;
 +    static boolean afterCreate=false;
 +    static boolean afterUpdate=false;
 +    static int putAllcounter = 0;
 +    static int afterUpdateputAllcounter = 0;
 +    static boolean beforeCreate=false;
 +    static boolean beforeUpdate=false;
 +    static int forCreate = 0;
 +    static int forUpdate = 0;
 +    static int beforeCreateputAllcounter = 0;
 +    static int beforeUpdateputAllcounter = 0;
 +    static boolean notified = false;
 +    
 +    @Override
 +    public void setUp() throws Exception {
 +      super.setUp();
 +      Host host = Host.getHost(0);
 +      VM vm0 = host.getVM(0);
 +      VM vm1 = host.getVM(1);
-       vm0.invoke(PutAllCallBkRemoteVMDUnitTest.class, "createCacheForVM0");
-       vm1.invoke(PutAllCallBkRemoteVMDUnitTest.class, "createCacheForVM1");
++      vm0.invoke(() -> PutAllCallBkRemoteVMDUnitTest.createCacheForVM0());
++      vm1.invoke(() -> PutAllCallBkRemoteVMDUnitTest.createCacheForVM1());
 +      LogWriterUtils.getLogWriter().info("Cache created successfully");
 +    }
 +    
 +    @Override
 +    protected final void preTearDown() throws Exception {
 +      Host host = Host.getHost(0);
 +      VM vm0 = host.getVM(0);
 +      VM vm1 = host.getVM(1);
-       vm0.invoke(PutAllCallBkRemoteVMDUnitTest.class, "closeCache");
-       vm1.invoke(PutAllCallBkRemoteVMDUnitTest.class, "closeCache");
++      vm0.invoke(() -> PutAllCallBkRemoteVMDUnitTest.closeCache());
++      vm1.invoke(() -> PutAllCallBkRemoteVMDUnitTest.closeCache());
 +    }
 +    
 +    public static synchronized void createCacheForVM0(){
 +        try{
 +            ds = (new PutAllCallBkRemoteVMDUnitTest("temp")).getSystem(props);
 +            cache = CacheFactory.create(ds);
 +            AttributesFactory factory  = new AttributesFactory();
 +            factory.setDataPolicy(DataPolicy.REPLICATE);
 +            factory.setScope(Scope.DISTRIBUTED_ACK);
 +            RegionAttributes attr1 = factory.create();
 +            paperRegion = cache.createRegion("paper", attr1);
 +            RegionAttributes attr = factory.create();
 +            region = cache.createRegion("map", attr);
 +              
 +        } catch (CacheException ex){
 +            throw new RuntimeException("vm0 cache creation exception", ex);
 +        }
 +    }
 +    
 +    public static void createCacheForVM1(){
 +        try{
 +            CacheListener aListener = new AfterCreateCallback();
 +            CacheWriter aWriter = new BeforeCreateCallback();
 +            
 +            ds = (new PutAllCallBkRemoteVMDUnitTest("temp")).getSystem(props);
 +            cache = CacheFactory.create(ds);
 +            AttributesFactory factory  = new AttributesFactory();
 +            factory.setDataPolicy(DataPolicy.REPLICATE);
 +            factory.setScope(Scope.DISTRIBUTED_ACK);
 +            RegionAttributes attr1 = factory.create();
 +            paperRegion = cache.createRegion("paper", attr1);
 +            factory.setCacheWriter(aWriter);
 +            factory.addCacheListener(aListener);
 +            RegionAttributes attr = factory.create();
 +            region = cache.createRegion("map", attr);
 +        } catch (CacheException ex){
 +            throw new RuntimeException("vm1 cache creation exception", ex);
 +        }
 +    }
 +    
 +    
 +    public static synchronized void closeCache(){
 +      paperRegion = null;
 +      region = null;
 +      if (cache != null) {
 +        cache.close();
 +        cache = null;
 +      }
 +      if (ds != null) {
 +        ds.disconnect();
 +        ds = null;
 +      }
 +    }
 +    
 +    //test methods
 +    
 +    public void testputAllRemoteVM(){
 +        
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +        VM vm1 = host.getVM(1);
 +        
 +        ////////////////testing create call backs//////////////
 +        
 +        vm0.invoke(new CacheSerializableRunnable("put entries"){
 +            public void run2() throws CacheException{
 +                Map m = new HashMap();
 +                paperRegion.put("callbackCame","false");
 +                try{
 +                    for(int i=1; i<21; i++){
 +                        m.put(new Integer(i), java.lang.Integer.toString(i));
 +                    }
 +                    region.putAll(m);
 +                    
 +                }catch (Exception ex){
 +                    throw new RuntimeException("exception putting entries", ex);
 +                }
 +                LogWriterUtils.getLogWriter().info("****************paperRegion.get(afterCreate)***************"+paperRegion.get("afterCreate"));
 +
 +                WaitCriterion ev = new WaitCriterion() {
 +                  public boolean done() {
 +                    int size = region.size();
 +                    if (size != ((Integer)paperRegion.get("afterCreate")).intValue()-1) {
 +                      return false;
 +                    }
 +                    if (size != ((Integer)paperRegion.get("beforeCreate")).intValue()-1) {
 +                      return false;
 +                    }
 +                    return true;
 +                  }
 +                  public String description() {
 +                    return "Waiting for event";
 +                  }
 +                };
 +                Wait.waitForCriterion(ev, 3000, 200, true);
 +            }
 +        });
 +        
 +        
 +        vm1.invoke(new CacheSerializableRunnable("validate callbacks"){
 +            public void run2() throws CacheException{
 +                if(!notified){
 +                    try{
 +                        synchronized(PutAllCallBkRemoteVMDUnitTest.class){
 +                            this.wait();
 +                        }
 +                    } catch(Exception e){
 +                        
 +                    }
 +                }
 +                if(!paperRegion.get("callbackCame").equals("true")){
 +                    fail("Failed in aftercreate call back :: PutAllCallBkRemoteVMDUnitTest ");
 +                }
 +    
 +            }
 +        });
 +        
 +        
 +        //to test afterUpdate
 +        
 +    }
 +    
 +    public void testPutAllAfterUpdateCallbacks(){
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +        VM vm1 = host.getVM(1);
 +        
 +        vm0.invoke(new CacheSerializableRunnable("put and then update"){
 +            public void run2() throws CacheException{
 +                paperRegion.put("callbackCame","false");             
 +                //to invoke afterUpdate we should make sure that entries are already present
 +                for(int i=0; i<5; i++){
 +                    region.put(new Integer(i), new String("region"+i));
 +                }
 +                
 +                Map m = new HashMap();
 +                for(int i=0; i<5; i++){
 +                    m.put(new Integer(i), new String("map"+i));
 +                }
 +                
 +                region.putAll(m);
 +                
 +//                 try{
 +//                     Thread.sleep(3000);
 +//                 }catch(InterruptedException ex){
 +//                     //
 +//                 }
 +                
 +                assertEquals(region.size(), ((Integer)paperRegion.get("beforeUpdate")).intValue()-1);
 +                assertEquals(region.size(), ((Integer)paperRegion.get("afterUpdate")).intValue()-1);
 +            }
 +        }
 +        );
 +        
 +        vm1.invoke(new CacheSerializableRunnable("validate callbacks"){
 +            public void run2() throws CacheException{
 +                
 +                if(!notified){
 +                    try{
 +                        synchronized(PutAllCallBkRemoteVMDUnitTest.class){
 +                            this.wait();
 +                        }
 +                    } catch(Exception e){
 +                        
 +                    }
 +                }
 +                
 +                if(!paperRegion.get("callbackCame").equals("true")){
 +                    fail("Failed in afterUpdate call back :: PutAllCallBkRemoteVMDUnitTest");
 +                }
 +                
 +            }
 +        }
 +        );
 +        
 +    }//end of test case1
 +    
 +    
 +    public static Object putMethod(Object ob){
 +        Object obj=null;
 +        try{
 +            if(ob != null){
 +                String str = "first";
 +                obj = region.put(ob, str);
 +            }
 +        }catch(Exception ex){
 +            ex.printStackTrace();
 +            fail("Failed while region.put");
 +        }
 +        return obj;
 +    }//end of putMethod
 +    
 +    public static void putAllMethod(){
 +        Map m = new HashMap();
 +        int i = 5, cntr = 0;
 +        try{
 +            while(cntr<20){
 +                m.put(new Integer(i), new String("map"+i));
 +                i++;
 +                cntr++;
 +            }
 +            
 +            region.putAll(m);
 +            
 +        }catch(Exception ex){
 +            ex.printStackTrace();
 +            fail("Failed while region.putAll");
 +        }
 +    }//end of putAllMethod
 +    
 +    
 +    public static Object getMethod(Object ob){
 +        Object obj=null;
 +        try{
 +            obj = region.get(ob);
 +        } catch(Exception ex){
 +            fail("Failed while region.get");
 +        }
 +        return obj;
 +    }
 +    
 +    
 +    public static int sizeMethod(){
 +        int i=0;
 +        try{
 +            i = region.size();
 +        }catch(Exception ex){
 +            fail("Failed while region.size");
 +        }
 +        return i;
 +    }
 +    
 +    public static void clearMethod(){
 +        try{
 +            region.clear();
 +        } catch(Exception ex){
 +            ex.printStackTrace();
 +        }
 +    }
 +    
 +    static class AfterCreateCallback extends CacheListenerAdapter {
 +        public void afterCreate(EntryEvent event){
 +            paperRegion.put("callbackCame","true");
 +            Integer counter = (Integer)paperRegion.get("afterCreate");
 +            if(counter==null) counter = new Integer(1);
 +            paperRegion.put("afterCreate",new Integer(counter.intValue()+1));
 +            
 +            LogWriterUtils.getLogWriter().info("In afterCreate"+putAllcounter);
 +            if(putAllcounter == forCreate){
 +                LogWriterUtils.getLogWriter().info("performingtrue");
 +                afterCreate = true;
 +            }
 +            try{
 +                synchronized(PutAllCallBkRemoteVMDUnitTest.class){
 +                    this.notify();
 +                }
 +            } catch(Exception e){
 +                
 +            }
 +            notified = true;
 +            LogWriterUtils.getLogWriter().info("*******afterCreate***** Key :"+event.getKey()+ " Value :"+event.getNewValue());
 +        }
 +        
 +        public void afterUpdate(EntryEvent event){
 +            paperRegion.put("callbackCame","true");
 +            Integer counter = (Integer)paperRegion.get("afterUpdate");
 +            if(counter==null) counter = new Integer(1);
 +            paperRegion.put("afterUpdate",new Integer(counter.intValue()+1));
 +            LogWriterUtils.getLogWriter().info("In afterUpdate"+afterUpdateputAllcounter);
 +            if(afterUpdateputAllcounter == forUpdate){
 +                LogWriterUtils.getLogWriter().info("performingtrue afterUpdate");
 +                afterUpdate = true;
 +            }
 +            try{
 +                synchronized(PutAllCallBkRemoteVMDUnitTest.class){
 +                    this.notify();
 +                }
 +            } catch(Exception e){
 +                
 +            }
 +            
 +            notified = true;
 +            
 +            LogWriterUtils.getLogWriter().info("*******afterUpdate***** Key :"+event.getKey()+ " Value :"+event.getNewValue());
 +            
 +        }
 +    }
 +    static class BeforeCreateCallback extends CacheWriterAdapter {
 +        //    static class BeforeCreateCallback extends CapacityControllerAdapter {
 +        public void beforeCreate(EntryEvent event){
 +            Integer counter = (Integer)paperRegion.get("beforeCreate");
 +            if(counter==null) counter = new Integer(1);
 +            paperRegion.put("beforeCreate",new Integer(counter.intValue()+1));
 +            LogWriterUtils.getLogWriter().info("*******BeforeCreate***** event="+event);
 +        }
 +        
 +        public void beforeUpdate(EntryEvent event) {
 +            Integer counter = (Integer)paperRegion.get("beforeUpdate");
 +            if(counter==null) counter = new Integer(1);
 +            paperRegion.put("beforeUpdate",new Integer(counter.intValue()+1));
 +            LogWriterUtils.getLogWriter().info("In beforeUpdate"+beforeUpdateputAllcounter);
 +            LogWriterUtils.getLogWriter().info("*******BeforeUpdate***** event="+event);
 +        }
 +    }
 +}// end of test class

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/PutAllCallBkSingleVMDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/PutAllCallBkSingleVMDUnitTest.java
index 8c85ebd,0000000..12337cd
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/PutAllCallBkSingleVMDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/PutAllCallBkSingleVMDUnitTest.java
@@@ -1,321 -1,0 +1,321 @@@
 +/*
 + * 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.
 + */
 +
 +/*
 + * PutAllCallBkSingleVMDUnitTest.java
 + *
 + * Created on August 31, 2005, 4:17 PM
 + */
 +package com.gemstone.gemfire.cache30;
 +
 +import java.util.HashMap;
 +import java.util.Map;
 +import java.util.Properties;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheFactory;
 +import com.gemstone.gemfire.cache.CacheListener;
 +import com.gemstone.gemfire.cache.CacheWriter;
 +import com.gemstone.gemfire.cache.EntryEvent;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionAttributes;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.util.CacheListenerAdapter;
 +import com.gemstone.gemfire.cache.util.CacheWriterAdapter;
 +import com.gemstone.gemfire.distributed.DistributedSystem;
 +import com.gemstone.gemfire.test.dunit.Assert;
 +import com.gemstone.gemfire.test.dunit.DistributedTestCase;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.LogWriterUtils;
 +import com.gemstone.gemfire.test.dunit.VM;
 +
 +public class PutAllCallBkSingleVMDUnitTest extends DistributedTestCase{
 +    
 +    /** Creates a new instance of PutAllCallBkSingleVMDUnitTest */
 +    public PutAllCallBkSingleVMDUnitTest(String name) {
 +        super(name);
 +    }
 +    
 +    static volatile Cache cache;
 +    static Properties props = new Properties();
 +    static Properties propsWork = new Properties();
 +    static volatile DistributedSystem ds = null;
 +    static volatile Region region;
 +    static boolean afterCreate=false;
 +    static boolean afterUpdate=false;
 +    static int putAllcounter = 0;
 +    static int afterUpdateputAllcounter = 0;
 +    static boolean beforeCreate=false;
 +    static boolean beforeUpdate=false;
 +    static int beforeCreateputAllcounter = 0;
 +    static int beforeUpdateputAllcounter = 0;
 +    
 +    @Override
 +    public void setUp() throws Exception {
 +      super.setUp();
 +      Host host = Host.getHost(0);
 +      VM vm0 = host.getVM(0);
 +      VM vm1 = host.getVM(1);
-       vm0.invoke(PutAllCallBkSingleVMDUnitTest.class, "createCache");
-       vm1.invoke(PutAllCallBkSingleVMDUnitTest.class, "createCache");
++      vm0.invoke(() -> PutAllCallBkSingleVMDUnitTest.createCache());
++      vm1.invoke(() -> PutAllCallBkSingleVMDUnitTest.createCache());
 +      LogWriterUtils.getLogWriter().fine("Cache created in successfully");
 +    }
 +    
 +    @Override
 +    protected final void preTearDown() throws Exception {
 +      Host host = Host.getHost(0);
 +      VM vm0 = host.getVM(0);
 +      VM vm1 = host.getVM(1);
-       vm0.invoke(PutAllCallBkSingleVMDUnitTest.class, "closeCache");
-       vm1.invoke(PutAllCallBkSingleVMDUnitTest.class, "closeCache");
++      vm0.invoke(() -> PutAllCallBkSingleVMDUnitTest.closeCache());
++      vm1.invoke(() -> PutAllCallBkSingleVMDUnitTest.closeCache());
 +    }
 +    
 +    public static synchronized void createCache(){
 +        try{
 +            CacheListener aListener = new AfterCreateCallback();
 +            CacheWriter aWriter = new BeforeCreateCallback();            
 +            ds = (new PutAllCallBkSingleVMDUnitTest("temp")).getSystem(props);
 +            cache = CacheFactory.create(ds);
 +            AttributesFactory factory  = new AttributesFactory();
 +            factory.setScope(Scope.DISTRIBUTED_ACK);
 +            factory.setCacheWriter(aWriter);
 +            factory.setCacheListener(aListener);
 +            RegionAttributes attr = factory.create();
 +            
 +            region = cache.createRegion("map", attr);
 +            
 +            
 +        } catch (Exception ex){
 +            ex.printStackTrace();
 +        }
 +    }
 +    
 +    public static synchronized void closeCache(){
 +      region = null;
 +      if (cache != null) {
 +        cache.close();
 +        cache = null;
 +      }
 +      if (ds != null) {
 +        ds.disconnect();
 +        ds = null;
 +      }
 +    }
 +
 +    //test methods
 +    public void testputAllSingleVM(){
 +        
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +        
 +//        Object obj2;
 +        Object[] objArr = new Object[1];
 +        for (int i=0; i<5; i++){
 +            objArr[0] = ""+i;
 +            vm0.invoke(PutAllCallBkSingleVMDUnitTest.class, "putMethod", objArr);
 +            
 +        }
 +        
-         vm0.invoke(PutAllCallBkSingleVMDUnitTest.class, "putAllMethod");
++        vm0.invoke(() -> PutAllCallBkSingleVMDUnitTest.putAllMethod());
 +        
 +        vm0.invoke(new CacheSerializableRunnable("temp1"){
 +            public void run2() throws CacheException{
 +                if(!afterCreate){
 +                    fail("FAILED in aftercreate call back");
 +                }
 +                assertEquals(region.size(), putAllcounter); 
 +                assertEquals(region.size(), beforeCreateputAllcounter);
 +            }
 +        });
 +        
 +        vm0.invoke(new CacheSerializableRunnable("abc"){
 +            public void run2() throws CacheException{
 +                CacheListener bListener = new AfterUpdateCallback();  
 +                CacheWriter bWriter =new BeforeUpdateCallback();
 +                AttributesFactory factory  = new AttributesFactory();
 +                factory.setScope(Scope.DISTRIBUTED_ACK);
 +                factory.setCacheWriter(bWriter);
 +                factory.setCacheListener(bListener);
 +                RegionAttributes attr = factory.create();                
 +                Region tempRegion = cache.createRegion("temp", attr);                
 +                
 +                //to invoke afterUpdate we should make sure that entries are already present
 +                for(int i=0; i<5; i++){
 +                    tempRegion.put(new Integer(i), new String("region"+i));
 +                }
 +                
 +                Map m = new HashMap();
 +                for(int i=0; i<5; i++){
 +                    m.put(new Integer(i), new String("map"+i));
 +                }
 +                
 +                tempRegion.putAll(m, "putAllAfterUpdateCallback");
 +                
 +                //now, verifying callbacks
 +                if(!afterUpdate){
 +                    fail("FAILED in afterupdate call back");
 +                }
 +                assertEquals(tempRegion.size(), afterUpdateputAllcounter);
 +                assertEquals(tempRegion.size(), beforeUpdateputAllcounter); 
 +            }
 +        }
 +        );
 +        
 +    }//end of test case1
 +    
 +   
 +    public static Object putMethod(Object ob){
 +        Object obj=null;
 +        try{
 +            if(ob != null){
 +                String str = "first";
 +                obj = region.put(ob, str);
 +            }
 +        }catch(Exception ex){
 +            Assert.fail("Failed while region.put", ex);
 +        }
 +        return obj;
 +    }//end of putMethod
 +    
 +    public static void putAllMethod(){
 +        Map m = new HashMap();
 +        int i = 5, cntr = 0;
 +        try{
 +            while(cntr<21){
 +                m.put(new Integer(i), new String("map"+i));
 +                i++;
 +                cntr++;
 +            }
 +            
 +            region.putAll(m, "putAllCreateCallback");
 +            
 +        }catch(Exception ex){
 +            ex.printStackTrace();
 +            fail("Failed while region.putAll");
 +        }
 +    }//end of putAllMethod
 +    
 +    public static void putAllAfterUpdate(){
 +        Map m = new HashMap();
 +        int cntr = 0;
 +        try{
 +            for(int i=0; i<5; i++){
 +                m.put(""+i, new String("map_AfterUpdate"+i));
 +                cntr++;
 +            }
 +            region.putAll(m, "putAllAfterUpdateCallback");
 +        }catch(Exception ex){
 +            ex.printStackTrace();
 +            fail("Failed while region.putAll");
 +        }
 +    }//end of putAllAfterUpdate
 +    
 +    public static Object getMethod(Object ob){
 +        Object obj=null;
 +        try{
 +            obj = region.get(ob);
 +        } catch(Exception ex){
 +            fail("Failed while region.get");
 +        }
 +        return obj;
 +    }
 +    
 +    public static boolean containsValueMethod(Object ob){
 +        boolean flag = false;
 +        try{
 +            flag = region.containsValue(ob);
 +        }catch(Exception ex){
 +            fail("Failed while region.containsValueMethod");
 +        }
 +        return flag;
 +    }
 +    
 +    public static int sizeMethod(){
 +        int i=0;
 +        try{
 +            i = region.size();
 +        }catch(Exception ex){
 +            fail("Failed while region.size");
 +        }
 +        return i;
 +    }
 +    
 +    public static void clearMethod(){
 +        try{
 +            region.clear();
 +        } catch(Exception ex){
 +            ex.printStackTrace();
 +        }
 +    }
 +    
 +    static class AfterCreateCallback extends CacheListenerAdapter {
 +        public void afterCreate(EntryEvent event){            
 +            putAllcounter++;
 +            LogWriterUtils.getLogWriter().fine("In afterCreate"+putAllcounter);
 +            if (event.getOperation().isPutAll()) {
 +              assertEquals("putAllCreateCallback", event.getCallbackArgument());
 +            }
 +            if(putAllcounter == 25){
 +                LogWriterUtils.getLogWriter().fine("performingtrue");
 +                afterCreate = true;
 +            }            
 +        }
 +    }
 +    
 +    static class AfterUpdateCallback extends CacheListenerAdapter {
 +        public void afterUpdate(EntryEvent event){            
 +            afterUpdateputAllcounter++;
 +            LogWriterUtils.getLogWriter().fine("In afterUpdate"+afterUpdateputAllcounter);
 +            if (event.getOperation().isPutAll()) {
 +              assertEquals("putAllAfterUpdateCallback", event.getCallbackArgument());
 +            }
 +            if(afterUpdateputAllcounter == 5){
 +                LogWriterUtils.getLogWriter().fine("performingtrue afterUpdate");
 +                afterUpdate = true;
 +            }            
 +        }
 +    }
 +    static class BeforeCreateCallback extends CacheWriterAdapter {
 +          public void beforeCreate(EntryEvent event){            
 +            beforeCreateputAllcounter++;
 +            LogWriterUtils.getLogWriter().fine("In beforeCreate"+beforeCreateputAllcounter);
 +            if (event.getOperation().isPutAll()) {
 +              assertEquals("putAllCreateCallback", event.getCallbackArgument());
 +            }
 +            if(beforeCreateputAllcounter == 25){
 +                LogWriterUtils.getLogWriter().fine("performingtrue beforeCreateputAll");
 +                beforeCreate = true;
 +            }            
 +        }
 +     }   
 +      static class BeforeUpdateCallback extends CacheWriterAdapter {
 +        public void beforeUpdate(EntryEvent event){            
 +            beforeUpdateputAllcounter++;
 +            LogWriterUtils.getLogWriter().fine("In beforeUpdate"+beforeUpdateputAllcounter);
 +            if (event.getOperation().isPutAll()) {
 +              assertEquals("putAllAfterUpdateCallback", event.getCallbackArgument());
 +            }
 +            if(beforeUpdateputAllcounter == 5){
 +                LogWriterUtils.getLogWriter().fine("performingtrue beforeUpdate");
 +                beforeUpdate = true;
 +            }            
 +        }
 +    }
 +    
 +}//end of class

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/PutAllMultiVmDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/PutAllMultiVmDUnitTest.java
index 5a37ebf,0000000..4ec8054
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/PutAllMultiVmDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/PutAllMultiVmDUnitTest.java
@@@ -1,366 -1,0 +1,366 @@@
 +/*
 + * 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.
 + */
 +
 +/*
 + * PutAllMultiVmDUnitTest.java
 + *
 + * Created on September 1, 2005, 12:19 PM
 + */
 +package com.gemstone.gemfire.cache30;
 +
 +import java.util.HashMap;
 +import java.util.Map;
 +import java.util.Properties;
 +import java.util.TreeMap;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheFactory;
 +import com.gemstone.gemfire.cache.CacheTransactionManager;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionAttributes;
 +import com.gemstone.gemfire.cache.RegionDestroyedException;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.distributed.DistributedSystem;
 +import com.gemstone.gemfire.test.dunit.DistributedTestCase;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.Invoke;
 +import com.gemstone.gemfire.test.dunit.SerializableRunnable;
 +import com.gemstone.gemfire.test.dunit.VM;
 +
 +/**
 + *
 + * @author  prafulla
 + */
 +public class PutAllMultiVmDUnitTest extends DistributedTestCase{
 +    
 +    /** Creates a new instance of PutAllMultiVmDUnitTest */
 +    public PutAllMultiVmDUnitTest(String name) {
 +        super(name);
 +    }
 +    
 +    static Cache cache;
 +    static Properties props = new Properties();
 +    static Properties propsWork = new Properties();
 +    static DistributedSystem ds = null;
 +    static Region region;
 +    static Region mirroredRegion;
 +    static CacheTransactionManager cacheTxnMgr;
 +    
 +    @Override
 +    public void setUp() throws Exception {
 +      super.setUp();
 +      Host host = Host.getHost(0);
 +      VM vm0 = host.getVM(0);
 +      VM vm1 = host.getVM(1);
-       vm0.invoke(PutAllMultiVmDUnitTest.class, "createCache");
-       vm1.invoke(PutAllMultiVmDUnitTest.class, "createCache");
++      vm0.invoke(() -> PutAllMultiVmDUnitTest.createCache());
++      vm1.invoke(() -> PutAllMultiVmDUnitTest.createCache());
 +    }
 +    
 +    @Override
 +    protected final void preTearDown() throws Exception {
 +      Host host = Host.getHost(0);
 +      VM vm0 = host.getVM(0);
 +      VM vm1 = host.getVM(1);
-       vm0.invoke(PutAllMultiVmDUnitTest.class, "closeCache");
-       vm1.invoke(PutAllMultiVmDUnitTest.class, "closeCache");
++      vm0.invoke(() -> PutAllMultiVmDUnitTest.closeCache());
++      vm1.invoke(() -> PutAllMultiVmDUnitTest.closeCache());
 +      cache = null;
 +      Invoke.invokeInEveryVM(new SerializableRunnable() { public void run() { cache = null; } });
 +    }
 +    
 +    public static void createCache(){
 +        try{
 +            ds = (new PutAllMultiVmDUnitTest("temp")).getSystem(props);
 +            cache = CacheFactory.create(ds);
 +            AttributesFactory factory  = new AttributesFactory();
 +            factory.setScope(Scope.DISTRIBUTED_ACK);
 +            RegionAttributes attr = factory.create();
 +            region = cache.createRegion("map", attr);
 +            
 +        } catch (Exception ex){
 +            ex.printStackTrace();
 +        }
 +    }//end of createCache
 +    
 +    public static void createMirroredRegion(){
 +        try{
 +            AttributesFactory factory  = new AttributesFactory();
 +            factory.setDataPolicy(DataPolicy.REPLICATE);
 +            factory.setScope(Scope.DISTRIBUTED_ACK);
 +            RegionAttributes attr = factory.create();
 +            mirroredRegion = cache.createRegion("mirrored", attr);
 +        } catch (Exception ex){
 +            ex.printStackTrace();
 +        }
 +    }//end of createCache
 +    
 +    public static void closeCache(){
 +        try{
 +            //System.out.println("closing cache cache cache cache cache 33333333");
 +            cache.close();
 +            ds.disconnect();
 +            //System.out.println("closed cache cache cache cache cache 44444444");
 +        } catch (Exception ex){
 +            ex.printStackTrace();
 +        }
 +    }//end of closeCache
 +    
 +    
 +    //tests methods
 +    
 +    public void testSimplePutAll(){
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +        VM vm1 = host.getVM(1);
 +        
 +        SerializableRunnable clear = new CacheSerializableRunnable("clear"){
 +            public void run2() throws CacheException {
 +                try{
 +                    region.clear();
 +                }catch(Exception ex){
 +                    ex.printStackTrace();
 +                }
 +            }
 +        };//end of clear
 +        
 +        vm0.invoke(new CacheSerializableRunnable("testSimplePutAll1"){
 +            public void run2() throws CacheException {
 +                int cntr = 0, cntr1 = 0;
 +                for(int i=1; i<6; i++) {
 +                    region.put(new Integer(i), new String("testSimplePutAll"+i));
 +                    cntr++;
 +                }
 +                
 +                int size1 = region.size();
 +                Map m = new HashMap();
 +                for(int i=6; i<27; i++) {
 +                    m.put(new Integer(i), new String("map"+i));
 +                    cntr++;
 +                    cntr1++;
 +                }
 +                
 +                region.putAll(m);
 +                int size2 = region.size();
 +                
 +                assertEquals(cntr, region.size());
 +                assertEquals(cntr1, (size2 - size1));
 +                assertEquals(true, region.containsKey(new Integer(10)));
 +                assertEquals(true, region.containsValue(new String("map12")));
 +            }
 +        } );
 +        
 +        vm0.invoke(clear);
 +        
 +        vm1.invoke(new CacheSerializableRunnable("create mirrored region"){
 +            public void run2() throws CacheException {
 +                createMirroredRegion();
 +            }
 +        }
 +        );
 +        
 +        vm0.invoke(new CacheSerializableRunnable("testSimplePutAll2"){
 +            public void run2() throws CacheException {
 +                //assertEquals(0, region.size());
 +                createMirroredRegion();
 +                cacheTxnMgr = cache.getCacheTransactionManager();
 +                int cntr = 0;
 +                for(int i=1; i<6; i++) {
 +                    mirroredRegion.put(new Integer(i), new String("testSimplePutAll"+i));
 +                    cntr++;
 +                }
 +                
 +                int size1 = mirroredRegion.size();
 +                Map m = new HashMap();
 +                for(int i=6; i<27; i++) {
 +                    m.put(new Integer(i), new String("map"+i));
 +                    cntr++;
 +                }
 +                
 +                //Disabled until putAll works in tx
 +                //cacheTxnMgr.begin();
 +                //mirroredRegion.putAll(m);
 +                //cacheTxnMgr.rollback();
 +                
 +                assertEquals(size1, mirroredRegion.size());
 +                assertEquals(false, mirroredRegion.containsKey(new Integer(10)));
 +                assertEquals(false, mirroredRegion.containsValue(new String("map12")));
 +                
 +                //cacheTxnMgr.begin();
 +                mirroredRegion.putAll(m);
 +                //cacheTxnMgr.commit();
 +                
 +//                int size2 = mirroredRegion.size();
 +                
 +                assertEquals(cntr, mirroredRegion.size());
 +                assertEquals(true, mirroredRegion.containsKey(new Integer(10)));
 +                assertEquals(true, mirroredRegion.containsValue(new String("map12")));
 +                
 +                //sharing the size of region of vm0 in vm1
 +                mirroredRegion.put("size", new Integer(mirroredRegion.size()));
 +            }
 +        } );
 +        
 +        vm1.invoke(new CacheSerializableRunnable("testSimplePutAll3"){
 +            public void run2() throws CacheException {
 +                Integer i = (Integer) mirroredRegion.get("size");
 +                int cntr = i.intValue();
 +                assertEquals(cntr, (mirroredRegion.size()-1));
 +                assertEquals(true, mirroredRegion.containsKey(new Integer(10)));
 +                assertEquals(true, mirroredRegion.containsValue(new String("map12")));
 +            }
 +        } );
 +        
 +    }//end of testSimplePutAll
 +    
 +    public void testPutAllExceptions(){
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +        VM vm1 = host.getVM(1);
 +        
 +        vm0.invoke(new CacheSerializableRunnable("testPutAllExceptions1"){
 +            public void run2() throws CacheException {
 +                int cntr = 0;
 +//                int cntr1 = 0;
 +                for(int i=1; i<6; i++) {
 +                    region.put(new Integer(i), new String("testSimplePutAll"+i));
 +                    cntr++;
 +                }
 +                
 +                Map m = new TreeMap();//to verify the assertions
 +                for(int i=6; i<27; i++) {
 +                    if(i == 16){
 +                        m.put(new Integer(i), null);
 +                    }
 +                    else {
 +                        m.put(new Integer(i), new String("map"+i));
 +                    }
 +                }
 +                
 +                try{
 +                    region.putAll(m);
 +                    fail("Expect NullPointerException");
 +                } catch (NullPointerException ex){
 +                    //do nothing
 +                }
 +                
 +                assertEquals(5, region.size());
 +                assertEquals(false, region.containsKey(new Integer(10)));
 +                assertEquals(false, region.containsValue(new String("map12")));
 +                assertEquals(false, region.containsKey(new Integer(20)));
 +                assertEquals(false, region.containsValue(new String("map21")));
 +            }
 +        } );
 +        
 +        
 +        vm1.invoke(new CacheSerializableRunnable("create mirrored region"){
 +            public void run2() throws CacheException {
 +                createMirroredRegion();
 +            }
 +        }
 +        );
 +        
 +        
 +        vm0.invoke(new CacheSerializableRunnable("testPutAllExceptions2"){
 +            public void run2() throws CacheException {
 +                //assertEquals(0, region.size());
 +                createMirroredRegion();
 +                
 +                for(int i=1; i<6; i++) {
 +                    mirroredRegion.put(new Integer(i), new String("testSimplePutAll"+i));
 +                }
 +                
 +                Map m = new TreeMap();//to verify the assertions
 +                for(int i=6; i<27; i++) {
 +                    if(i == 16){
 +                        m.put(new Integer(i), null);
 +                    }
 +                    else {
 +                        m.put(new Integer(i), new String("map"+i));
 +                    }
 +                }
 +                
 +                try{
 +                    mirroredRegion.putAll(m);
 +                    fail("Expect NullPointerException");
 +                } catch (NullPointerException ex){
 +                    //do nothing
 +                }
 +               
 +                assertEquals(5, mirroredRegion.size());
 +                assertEquals(false, mirroredRegion.containsKey(new Integer(10)));
 +                assertEquals(false, mirroredRegion.containsValue(new String("map12")));
 +                assertEquals(false, region.containsKey(new Integer(20)));
 +                assertEquals(false, region.containsValue(new String("map21")));
 +
 +                //sharing the size of region of vm0 in vm1
 +                mirroredRegion.put("size", new Integer(mirroredRegion.size()));
 +            }
 +        } );
 +        
 +        vm1.invoke(new CacheSerializableRunnable("testPutAllExceptions3"){
 +            public void run2() throws CacheException {
 +                Integer i = (Integer) mirroredRegion.get("size");
 +                int cntr = i.intValue();
 +                assertEquals(cntr, (mirroredRegion.size()-1));
 +                assertEquals(false, mirroredRegion.containsKey(new Integer(10)));
 +                assertEquals(false, mirroredRegion.containsValue(new String("map12")));
 +                assertEquals(false, mirroredRegion.containsKey(new Integer(20)));
 +                assertEquals(false, mirroredRegion.containsValue(new String("map21")));
 +            }
 +        } );
 +        
 +        
 +    }//end of testPutAllExceptions
 +    
 +    public void testPutAllExceptionHandling(){
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +//        VM vm1 = host.getVM(1);
 +        
 +        vm0.invoke(new CacheSerializableRunnable("testPutAllExceptionHandling1"){
 +            public void run2() throws CacheException {
 +                Map m = new HashMap();
 +                m = null;
 +                try{
 +                    region.putAll(m);
 +                    fail("Should have thrown NullPointerException");
 +                }catch (NullPointerException ex){
 +                    //pass
 +                }
 +                
 +                region.localDestroyRegion();
 +                try{
 +                    Map m1 = new HashMap();
 +                    for(int i=1; i<21; i++) {
 +                        m1.put(new Integer(i), Integer.toString(i));
 +                    }
 +                    
 +                    region.putAll(m1);
 +                    fail("Should have thrown RegionDestroyedException");
 +                }catch (RegionDestroyedException ex){
 +                    //pass
 +                }
 +                
 +            }
 +        });
 +        
 +    }//testPutAllExceptionHandling
 +    
 +    
 +}//end of PutAllMultiVmDUnitTest


[15/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache/query/dunit/QueryDataInconsistencyDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache/query/dunit/QueryDataInconsistencyDUnitTest.java
index aeb4343,0000000..0227836
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache/query/dunit/QueryDataInconsistencyDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache/query/dunit/QueryDataInconsistencyDUnitTest.java
@@@ -1,580 -1,0 +1,576 @@@
 +/*
 + * 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.cache.query.dunit;
 +
 +import java.util.Properties;
 +
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheFactory;
 +import com.gemstone.gemfire.cache.PartitionAttributesFactory;
 +import com.gemstone.gemfire.cache.PartitionResolver;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionShortcut;
 +import com.gemstone.gemfire.cache.client.ClientCache;
 +import com.gemstone.gemfire.cache.client.ClientCacheFactory;
 +import com.gemstone.gemfire.cache.client.ClientRegionShortcut;
 +import com.gemstone.gemfire.cache.query.Index;
 +import com.gemstone.gemfire.cache.query.IndexType;
 +import com.gemstone.gemfire.cache.query.QueryService;
 +import com.gemstone.gemfire.cache.query.SelectResults;
 +import com.gemstone.gemfire.cache.query.data.Portfolio;
 +import com.gemstone.gemfire.cache.query.data.Position;
 +import com.gemstone.gemfire.cache.query.internal.QueryObserverHolder;
 +import com.gemstone.gemfire.cache.query.internal.index.IndexManager;
 +import com.gemstone.gemfire.cache.query.partitioned.PRQueryDUnitHelper;
 +import com.gemstone.gemfire.cache30.CacheSerializableRunnable;
 +import com.gemstone.gemfire.cache30.CacheTestCase;
 +import com.gemstone.gemfire.internal.cache.execute.PRClientServerTestBase;
 +import com.gemstone.gemfire.test.dunit.Assert;
 +import com.gemstone.gemfire.test.dunit.AsyncInvocation;
 +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.ThreadUtils;
 +import com.gemstone.gemfire.test.dunit.VM;
 +import com.gemstone.gemfire.test.dunit.Wait;
 +
 +/**
 + * This tests the data inconsistency during update on an index and querying the
 + * same UNLOCKED index.
 + * 
 + * @author shobhit
 + * 
 + */
 +public class QueryDataInconsistencyDUnitTest extends CacheTestCase {
 +
 +  private static final int cnt = 0;
 +
 +  private static final int cntDest = 10;
 +
 +  static VM server = null;
 +
 +  static VM client = null;
 +
 +  static Cache cache = null;
 +
 +  static String PartitionedRegionName1 = "TestPartitionedRegion1"; // default
 +                                                                   // name
 +  static String repRegionName = "TestRepRegion"; // default name
 +  
 +  static Integer serverPort1 = null;
 +
 +  public static int numOfBuckets = 20;
 +
 +  public static String[] queries = new String[] {
 +      "select * from /" + PartitionedRegionName1 + " where ID=1",
 +      };
 +
 +  public static String[] queriesForRR = new String[] { "<trace> select * from /"
 +      + repRegionName + " where ID=1" };
 +
 +  private static PRQueryDUnitHelper PRQHelp = new PRQueryDUnitHelper("");
 +
 +  public static volatile boolean hooked = false;
 +  /**
 +   * @param name
 +   */
 +  public QueryDataInconsistencyDUnitTest(String name) {
 +    super(name);
 +  }
 +
 +  @Override
 +  protected final void preTearDownCacheTestCase() throws Exception {
 +    Invoke.invokeInEveryVM(CacheTestCase.class, "disconnectFromDS");
 +  }
 +  
 +  @Override
 +  protected final void postTearDownCacheTestCase() throws Exception {
 +    Invoke.invokeInEveryVM(QueryObserverHolder.class, "reset");
 +  }
 +
 +  @Override
 +  public void setUp() throws Exception {
 +    super.setUp();
 +    Host host = Host.getHost(0);
 +    server = host.getVM(0);
 +  }
 +
 +  public void testCompactRangeIndex() {
 +    // Create caches
 +    Properties props = new Properties();
-     server.invoke(PRClientServerTestBase.class, "createCacheInVm",
-         new Object[] { props });
++    server.invoke(() -> PRClientServerTestBase.createCacheInVm( props ));
 +
 +    server.invoke(new CacheSerializableRunnable("create indexes") {
 +
 +      @Override
 +      public void run2() throws CacheException {
 +        cache = CacheFactory.getAnyInstance();
 +        Region region = cache.createRegionFactory(RegionShortcut.REPLICATE).create(repRegionName);
 +        
 +        // Create common Portflios and NewPortfolios
 +        for (int j = cnt; j < cntDest; j++) {
 +          region.put(new Integer(j), new Portfolio(j));
 +        }
 +        
 +        QueryService qs = CacheFactory.getAnyInstance().getQueryService();
 +        try {
 +          Index index = qs.createIndex("idIndex", "ID", "/"+repRegionName);
 +          assertEquals(10, index.getStatistics().getNumberOfKeys());
 +        } catch (Exception e) {
 +          fail("Index creation failed");
 +        }
 +      }
 +    });
 +    //Invoke update from client and stop in updateIndex
 +    //first before updating the RegionEntry and second after updating
 +    //the RegionEntry.
 +    AsyncInvocation putThread = server.invokeAsync(new CacheSerializableRunnable("update a Region Entry") {
 +      
 +      @Override
 +      public void run2() throws CacheException {
 +        Region repRegion = CacheFactory.getAnyInstance().getRegion(repRegionName);
 +        IndexManager.testHook = new IndexManagerTestHook();
 +        repRegion.put(new Integer("1"), new Portfolio(cntDest+1));
 +        //above call must be hooked in BEFORE_UPDATE_OP call.
 +      }
 +    });
 +    server.invoke(new CacheSerializableRunnable("query on server") {
 +      
 +      @Override
 +      public void run2() throws CacheException {
 +        QueryService qs = CacheFactory.getAnyInstance().getQueryService();
 +        while (!hooked){Wait.pause(100);}
 +        Object rs = null;
 +        try {
 +          rs = qs.newQuery("<trace> select * from /"+repRegionName+" where ID = 1").execute();          
 +        } catch (Exception e) {
 +          e.printStackTrace();
 +          fail("Query execution failed on server.");
 +          IndexManager.testHook = null;
 +        }
 +        assertTrue(rs instanceof SelectResults);
 +        assertEquals(1, ((SelectResults)rs).size());
 +        Portfolio p1 = (Portfolio) ((SelectResults)rs).asList().get(0);
 +        if (p1.getID() != 1) {
 +          fail("Query thread did not verify index results even when RE is under update");
 +          IndexManager.testHook = null;
 +        }
 +        hooked = false;//Let client put go further.
 +      }
 +    });
 +
 +    //Client put is again hooked in AFTER_UPDATE_OP call in updateIndex.
 +    server.invoke(new CacheSerializableRunnable("query on server") {
 +      
 +      @Override
 +      public void run2() throws CacheException {
 +        QueryService qs = CacheFactory.getAnyInstance().getQueryService();
 +        while (!hooked){Wait.pause(100);}
 +        Object rs = null;
 +        try {
 +          rs = qs.newQuery("<trace> select * from /"+repRegionName+" where ID = 1").execute();          
 +        } catch (Exception e) {
 +          e.printStackTrace();
 +          fail("Query execution failed on server."+e.getMessage());
 +        } finally {
 +          IndexManager.testHook = null;
 +        }
 +        assertTrue(rs instanceof SelectResults);
 +        if (((SelectResults)rs).size() > 0) {
 +          Portfolio p1 = (Portfolio) ((SelectResults)rs).iterator().next();
 +          if (p1.getID() != 1) {
 +            fail("Query thread did not verify index results even when RE is under update and " +
 +                      "RegionEntry value has been modified before releasing the lock");
 +            IndexManager.testHook = null;
 +          }
 +        }
 +        hooked = false;//Let client put go further.
 +      }
 +    });
 +    ThreadUtils.join(putThread, 200);
 +  }
 +
 +  public void testRangeIndex() {
 +    // Create caches
 +    Properties props = new Properties();
-     server.invoke(PRClientServerTestBase.class, "createCacheInVm",
-         new Object[] { props });
++    server.invoke(() -> PRClientServerTestBase.createCacheInVm( props ));
 +
 +    server.invoke(new CacheSerializableRunnable("create indexes") {
 +
 +      @Override
 +      public void run2() throws CacheException {
 +        cache = CacheFactory.getAnyInstance();
 +        Region region = cache.createRegionFactory(RegionShortcut.REPLICATE).create(repRegionName);
 +        IndexManager.testHook = null;
 +        // Create common Portfolios and NewPortfolios
 +        Position.cnt = 0;
 +        for (int j = cnt; j < cntDest; j++) {
 +          Portfolio p = new Portfolio(j);
 +          CacheFactory.getAnyInstance().getLogger().fine("Shobhit: portfolio "+j+ " : "+p);
 +          region.put(new Integer(j), p);
 +        }
 +        
 +        QueryService qs = CacheFactory.getAnyInstance().getQueryService();
 +        try {
 +          Index index = qs.createIndex("posIndex", "pos.secId", "/"+repRegionName+" p, p.positions.values pos");
 +          assertEquals(12, index.getStatistics().getNumberOfKeys());
 +        } catch (Exception e) {
 +          fail("Index creation failed");
 +        }
 +      }
 +    });
 +    //Invoke update from client and stop in updateIndex
 +    //first before updating the RegionEntry and second after updating
 +    //the RegionEntry.
 +    AsyncInvocation putThread = server.invokeAsync(new CacheSerializableRunnable("update a Region Entry") {
 +      
 +      @Override
 +      public void run2() throws CacheException {
 +        Region repRegion = CacheFactory.getAnyInstance().getRegion(repRegionName);
 +        IndexManager.testHook = new IndexManagerTestHook();
 +        Portfolio newPort = new Portfolio(cntDest+1);
 +        CacheFactory.getAnyInstance().getLogger().fine("Shobhit: New Portfolio"+newPort);
 +        repRegion.put(new Integer("1"), newPort);
 +        //above call must be hooked in BEFORE_UPDATE_OP call.
 +      }
 +    });
 +
 +    server.invoke(new CacheSerializableRunnable("query on server") {
 +      
 +      @Override
 +      public void run2() throws CacheException {
 +        QueryService qs = CacheFactory.getAnyInstance().getQueryService();
 +        Position pos1 = null;
 +        while (!hooked){Wait.pause(100);}
 +        try {
 +          Object rs = qs.newQuery("<trace> select pos from /"+repRegionName+" p, p.positions.values pos where pos.secId = 'APPL' AND p.ID = 1").execute();
 +          CacheFactory.getAnyInstance().getLogger().fine("Shobhit: "+rs);
 +          assertTrue(rs instanceof SelectResults);
 +          pos1 = (Position) ((SelectResults)rs).iterator().next();
 +          if (!pos1.secId.equals("APPL")) {
 +            fail("Query thread did not verify index results even when RE is under update");
 +            IndexManager.testHook = null;       
 +          }          
 +        } catch (Exception e) {
 +          e.printStackTrace();
 +          Assert.fail("Query execution failed on server.", e);
 +          IndexManager.testHook = null;
 +        } finally {
 +          hooked = false;//Let client put go further.
 +        }
 +        while (!hooked) {
 +          Wait.pause(100);
 +        }
 +        try {
 +          Object rs = qs.newQuery("<trace> select pos from /"+repRegionName+" p, p.positions.values pos where pos.secId = 'APPL' AND p.ID = 1").execute();
 +          CacheFactory.getAnyInstance().getLogger().fine("Shobhit: "+rs);
 +          assertTrue(rs instanceof SelectResults);
 +          if (((SelectResults)rs).size() > 0) {
 +            Position pos2 = (Position) ((SelectResults)rs).iterator().next();
 +            if (pos2.equals(pos1)) {
 +              fail("Query thread did not verify index results even when RE is under update and " +
 +              "RegionEntry value has been modified before releasing the lock");
 +            }
 +          }
 +        } catch (Exception e) {
 +          e.printStackTrace();
 +          fail("Query execution failed on server.");
 +        } finally {
 +          hooked = false;//Let client put go further.
 +          IndexManager.testHook = null;
 +        }
 +      }
 +    });
 +    ThreadUtils.join(putThread, 200);
 +  }
 +  
 +  public void testRangeIndexWithIndexAndQueryFromCluaseMisMatch() {
 +    // Create caches
 +    Properties props = new Properties();
-     server.invoke(PRClientServerTestBase.class, "createCacheInVm",
-         new Object[] { props });
++    server.invoke(() -> PRClientServerTestBase.createCacheInVm( props ));
 +
 +    server.invoke(new CacheSerializableRunnable("create indexes") {
 +
 +      @Override
 +      public void run2() throws CacheException {
 +        cache = CacheFactory.getAnyInstance();
 +        Region region = cache.createRegionFactory(RegionShortcut.REPLICATE).create(repRegionName);
 +        IndexManager.testHook = null;
 +        // Create common Portfolios and NewPortfolios
 +        Position.cnt = 0;
 +        for (int j = cnt; j < cntDest; j++) {
 +          region.put(new Integer(j), new Portfolio(j));
 +        }
 +        
 +        QueryService qs = CacheFactory.getAnyInstance().getQueryService();
 +        try {
 +          Index index = qs.createIndex("posIndex", "pos.secId", "/"+repRegionName+" p, p.collectionHolderMap.values coll, p.positions.values pos");
 +          assertEquals(12, index.getStatistics().getNumberOfKeys());
 +        } catch (Exception e) {
 +          fail("Index creation failed");
 +        }
 +      }
 +    });
 +    //Invoke update from client and stop in updateIndex
 +    //first before updating the RegionEntry and second after updating
 +    //the RegionEntry.
 +    AsyncInvocation putThread = server.invokeAsync(new CacheSerializableRunnable("update a Region Entry") {
 +      
 +      @Override
 +      public void run2() throws CacheException {
 +        Region repRegion = CacheFactory.getAnyInstance().getRegion(repRegionName);
 +        IndexManager.testHook = new IndexManagerTestHook();
 +        // This portfolio with same ID must have different positions.
 +        repRegion.put(new Integer("1"), new Portfolio(1));
 +        //above call must be hooked in BEFORE_UPDATE_OP call.
 +      }
 +    });
 +
 +    server.invoke(new CacheSerializableRunnable("query on server") {
 +      
 +      @Override
 +      public void run2() throws CacheException {
 +        QueryService qs = CacheFactory.getAnyInstance().getQueryService();
 +        Position pos1 = null;
 +        while (!hooked){Wait.pause(100);}
 +        try {
 +          Object rs = qs.newQuery("<trace> select pos from /"+repRegionName+" p, p.positions.values pos where pos.secId = 'APPL' AND p.ID = 1").execute();
 +          CacheFactory.getAnyInstance().getLogger().fine("Shobhit: "+rs);
 +          assertTrue(rs instanceof SelectResults);
 +          pos1 = (Position) ((SelectResults)rs).iterator().next();
 +          if (!pos1.secId.equals("APPL")) {
 +            fail("Query thread did not verify index results even when RE is under update");
 +            IndexManager.testHook = null;       
 +          }          
 +        } catch (Exception e) {
 +          e.printStackTrace();
 +          Assert.fail("Query execution failed on server.", e);
 +          IndexManager.testHook = null;
 +        } finally {
 +          hooked = false;//Let client put go further.
 +        }
 +        while (!hooked) {
 +          Wait.pause(100);
 +        }
 +        try {
 +          Object rs = qs.newQuery("select pos from /"+repRegionName+" p, p.positions.values pos where pos.secId = 'APPL' AND p.ID = 1").execute();
 +          assertTrue(rs instanceof SelectResults);
 +          if (((SelectResults)rs).size() > 0) {
 +            Position pos2 = (Position) ((SelectResults)rs).iterator().next();
 +            if (pos2.equals(pos1)) {
 +              fail("Query thread did not verify index results even when RE is under update and " +
 +              "RegionEntry value has been modified before releasing the lock");
 +            }
 +          }
 +        } catch (Exception e) {
 +          e.printStackTrace();
 +          fail("Query execution failed on server.");
 +        } finally {
 +          hooked = false;//Let client put go further.
 +          IndexManager.testHook = null;
 +        }
 +      }
 +    });
 +    ThreadUtils.join(putThread, 200);
 +  }
 +
 +  public void testRangeIndexWithIndexAndQueryFromCluaseMisMatch2() {
 +    // Create caches
 +    Properties props = new Properties();
-     server.invoke(PRClientServerTestBase.class, "createCacheInVm",
-         new Object[] { props });
++    server.invoke(() -> PRClientServerTestBase.createCacheInVm( props ));
 +
 +    server.invoke(new CacheSerializableRunnable("create indexes") {
 +
 +      @Override
 +      public void run2() throws CacheException {
 +        cache = CacheFactory.getAnyInstance();
 +        Region region = cache.createRegionFactory(RegionShortcut.REPLICATE).create(repRegionName);
 +        IndexManager.testHook = null;
 +        // Create common Portfolios and NewPortfolios
 +        Position.cnt = 0;
 +        for (int j = cnt; j < cntDest; j++) {
 +          region.put(new Integer(j), new Portfolio(j));
 +        }
 +        
 +        QueryService qs = CacheFactory.getAnyInstance().getQueryService();
 +        try {
 +          Index index = qs.createIndex("posIndex", "pos.secId", "/"+repRegionName+" p, p.positions.values pos");
 +          assertEquals(12, index.getStatistics().getNumberOfKeys());
 +        } catch (Exception e) {
 +          fail("Index creation failed");
 +        }
 +      }
 +    });
 +    //Invoke update from client and stop in updateIndex
 +    //first before updating the RegionEntry and second after updating
 +    //the RegionEntry.
 +    AsyncInvocation putThread = server.invokeAsync(new CacheSerializableRunnable("update a Region Entry") {
 +      
 +      @Override
 +      public void run2() throws CacheException {
 +        Region repRegion = CacheFactory.getAnyInstance().getRegion(repRegionName);
 +        IndexManager.testHook = new IndexManagerTestHook();
 +        // This portfolio with same ID must have different positions.
 +        repRegion.put(new Integer("1"), new Portfolio(1));
 +        //above call must be hooked in BEFORE_UPDATE_OP call.
 +      }
 +    });
 +
 +    server.invoke(new CacheSerializableRunnable("query on server") {
 +      
 +      @Override
 +      public void run2() throws CacheException {
 +        QueryService qs = CacheFactory.getAnyInstance().getQueryService();
 +        Position pos1 = null;
 +        while (!hooked){Wait.pause(100);}
 +        try {
 +          Object rs = qs.newQuery("<trace> select pos from /"+repRegionName+" p, p.collectionHolderMap.values coll, p.positions.values pos where pos.secId = 'APPL' AND p.ID = 1").execute();
 +          CacheFactory.getAnyInstance().getLogger().fine("Shobhit: "+rs);
 +          assertTrue(rs instanceof SelectResults);
 +          pos1 = (Position) ((SelectResults)rs).iterator().next();
 +          if (!pos1.secId.equals("APPL")) {
 +            fail("Query thread did not verify index results even when RE is under update");
 +            IndexManager.testHook = null;       
 +          }          
 +        } catch (Exception e) {
 +          e.printStackTrace();
 +          Assert.fail("Query execution failed on server.", e);
 +          IndexManager.testHook = null;
 +        } finally {
 +          hooked = false;//Let client put go further.
 +        }
 +        while (!hooked) {
 +          Wait.pause(100);
 +        }
 +        try {
 +          Object rs = qs.newQuery("select pos from /"+repRegionName+" p, p.collectionHolderMap.values coll, p.positions.values pos where pos.secId = 'APPL' AND p.ID = 1").execute();
 +          assertTrue(rs instanceof SelectResults);
 +          if (((SelectResults)rs).size() > 0) {
 +            Position pos2 = (Position) ((SelectResults)rs).iterator().next();
 +            if (pos2.equals(pos1)) {
 +              fail("Query thread did not verify index results even when RE is under update and " +
 +              "RegionEntry value has been modified before releasing the lock");
 +            }
 +          }
 +        } catch (Exception e) {
 +          e.printStackTrace();
 +          fail("Query execution failed on server.");
 +        } finally {
 +          IndexManager.testHook = null;
 +          hooked = false;//Let client put go further.
 +        }
 +      }
 +    });
 +    ThreadUtils.join(putThread, 200);
 +  }
 +  
 +  public static void createProxyRegions() {
 +    new QueryDataInconsistencyDUnitTest("temp").createProxyRegs();
 +  }
 +
 +  private void createProxyRegs() {
 +    ClientCache cache = (ClientCache) CacheFactory.getAnyInstance();
 +    cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(
 +        repRegionName);
 +
 +    /*cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(
 +        PartitionedRegionName1);*/
 +  }
 +
 +  public static void createNewPR() {
 +    new QueryDataInconsistencyDUnitTest("temp").createPR();
 +  }
 +  public void createPR() {
 +    PartitionResolver testKeyBasedResolver = new QueryAPITestPartitionResolver();
 +    cache = CacheFactory.getAnyInstance();
 +    cache
 +        .createRegionFactory(RegionShortcut.PARTITION_REDUNDANT)
 +        .setPartitionAttributes(
 +            new PartitionAttributesFactory()
 +                .setTotalNumBuckets(numOfBuckets)
 +                .setPartitionResolver(testKeyBasedResolver)
 +                .create())
 +        .create(PartitionedRegionName1);
 +  }
 +
 +  public static void createCacheClientWithoutRegion(String host, Integer port1) {
 +    new QueryDataInconsistencyDUnitTest("temp").createCacheClientWithoutReg(
 +        host, port1);
 +  }
 +
 +  private void createCacheClientWithoutReg(String host, Integer port1) {
 +    this.disconnectFromDS();
 +    ClientCache cache = new ClientCacheFactory().addPoolServer(host, port1)
 +        .create();
 +  }
 +
 +  /**
 +   * This function puts portfolio objects into the created Region (PR or Local)
 +   * *
 +   * 
 +   * @param regionName
 +   * @param portfolio
 +   * @param to
 +   * @param from
 +   * @return cacheSerializable object
 +   */
 +  public CacheSerializableRunnable getCacheSerializableRunnableForPRPuts(
 +      final String regionName, final Object[] portfolio, final int from,
 +      final int to) {
 +    SerializableRunnable puts = new CacheSerializableRunnable("Region Puts") {
 +      @Override
 +      public void run2() throws CacheException {
 +        Cache cache = CacheFactory.getAnyInstance();
 +        Region region = cache.getRegion(repRegionName);
 +        for (int j = from; j < to; j++)
 +          region.put(new Integer(j), portfolio[j]);
 +          LogWriterUtils.getLogWriter()
 +            .info(
 +                "PRQueryDUnitHelper#getCacheSerializableRunnableForPRPuts: Inserted Portfolio data on Region "
 +                    + regionName);
 +      }
 +    };
 +    return (CacheSerializableRunnable) puts;
 +  }
 +
 +  public class IndexManagerTestHook implements com.gemstone.gemfire.cache.query.internal.index.IndexManager.TestHook{
 +    public void hook(final int spot) throws RuntimeException {
 +      switch (spot) {
 +      case 9: //Before Index update and after region entry lock.
 +        hooked  = true;
 +        LogWriterUtils.getLogWriter().info("QueryDataInconsistency.IndexManagerTestHook is hooked in Update Index Entry.");
 +        while(hooked) {
 +          Wait.pause(100);
 +        }
 +        assertEquals(hooked, false);
 +        break;
 +      case 10: //Before Region update and after Index Remove call.
 +        hooked  = true;
 +        LogWriterUtils.getLogWriter().info("QueryDataInconsistency.IndexManagerTestHook is hooked in Remove Index Entry.");
 +        while(hooked) {
 +          Wait.pause(100);
 +        }
 +        assertEquals(hooked, false);
 +        break;
 +      default:
 +        break;
 +      }
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache/query/dunit/QueryUsingFunctionContextDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache/query/dunit/QueryUsingFunctionContextDUnitTest.java
index fcc96dc,0000000..1e3e973
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache/query/dunit/QueryUsingFunctionContextDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache/query/dunit/QueryUsingFunctionContextDUnitTest.java
@@@ -1,1048 -1,0 +1,1041 @@@
 +/*
 + * 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.cache.query.dunit;
 +
 +import java.util.ArrayList;
 +import java.util.HashSet;
 +import java.util.List;
 +import java.util.Properties;
 +import java.util.Set;
 +
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheFactory;
 +import com.gemstone.gemfire.cache.PartitionAttributesFactory;
 +import com.gemstone.gemfire.cache.PartitionResolver;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionShortcut;
 +import com.gemstone.gemfire.cache.client.ClientCache;
 +import com.gemstone.gemfire.cache.client.ClientCacheFactory;
 +import com.gemstone.gemfire.cache.client.ClientRegionShortcut;
 +import com.gemstone.gemfire.cache.client.ServerConnectivityException;
 +import com.gemstone.gemfire.cache.execute.Function;
 +import com.gemstone.gemfire.cache.execute.FunctionAdapter;
 +import com.gemstone.gemfire.cache.execute.FunctionContext;
 +import com.gemstone.gemfire.cache.execute.FunctionException;
 +import com.gemstone.gemfire.cache.execute.FunctionService;
 +import com.gemstone.gemfire.cache.execute.RegionFunctionContext;
 +import com.gemstone.gemfire.cache.execute.ResultCollector;
 +import com.gemstone.gemfire.cache.query.Query;
 +import com.gemstone.gemfire.cache.query.QueryInvocationTargetException;
 +import com.gemstone.gemfire.cache.query.QueryService;
 +import com.gemstone.gemfire.cache.query.SelectResults;
 +import com.gemstone.gemfire.cache.query.data.Portfolio;
 +import com.gemstone.gemfire.cache.query.functional.StructSetOrResultsSet;
 +import com.gemstone.gemfire.cache.query.internal.DefaultQuery;
 +import com.gemstone.gemfire.cache.query.internal.IndexTrackingQueryObserver;
 +import com.gemstone.gemfire.cache.query.internal.QueryObserverHolder;
 +import com.gemstone.gemfire.cache.query.partitioned.PRQueryDUnitHelper;
 +import com.gemstone.gemfire.cache30.CacheSerializableRunnable;
 +import com.gemstone.gemfire.cache30.CacheTestCase;
 +import com.gemstone.gemfire.internal.cache.LocalDataSet;
 +import com.gemstone.gemfire.internal.cache.PartitionedRegion;
 +import com.gemstone.gemfire.internal.cache.execute.PRClientServerTestBase;
 +import com.gemstone.gemfire.test.dunit.Assert;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.IgnoredException;
 +import com.gemstone.gemfire.test.dunit.Invoke;
 +import com.gemstone.gemfire.test.dunit.LogWriterUtils;
 +import com.gemstone.gemfire.test.dunit.NetworkUtils;
 +import com.gemstone.gemfire.test.dunit.SerializableRunnable;
 +import com.gemstone.gemfire.test.dunit.VM;
 +
 +/**
 + * This tests the querying using a RegionFunctionContext which provides a filter
 + * (routing keys) to run the query on subset of buckets "locally". If query
 + * includes buckets
 + *
 + * @author shobhit
 + *
 + */
 +public class QueryUsingFunctionContextDUnitTest extends CacheTestCase {
 +
 +  private static final int cnt = 0;
 +
 +  private static final int cntDest = 100;
 +
-   static VM server1 = null;
++  VM server1 = null;
 +
 +  static VM server2 = null;
 +
 +  static VM server3 = null;
 +
 +  static VM client = null;
 +
 +  static Cache cache = null;
 +
 +  static Function function = null;
 +  // PR 2 is co-located with 1 and 3 is co-located with 2
 +  // PR 5 is co-located with 4
 +  static String PartitionedRegionName1 = "TestPartitionedRegion1"; //default name
 +  static String PartitionedRegionName2 = "TestPartitionedRegion2"; //default name
 +  static String PartitionedRegionName3 = "TestPartitionedRegion3"; //default name
 +  static String PartitionedRegionName4 = "TestPartitionedRegion4"; //default name
 +  static String PartitionedRegionName5 = "TestPartitionedRegion5"; //default name
 +
 +  static String repRegionName = "TestRepRegion"; //default name
 +  static String localRegionName = "TestLocalRegion"; //default name
 +
 +  static Integer serverPort1 = null;
 +
 +  static Integer serverPort2 = null;
 +
 +  static Integer serverPort3 = null;
 +
 +  public static int numOfBuckets = 20;
 +
 +  public static String[] queries = new String[] {
 +      "select * from /" + PartitionedRegionName1 + " where ID>=0",
 +      "Select * from /" + PartitionedRegionName1 + " r1, /" + PartitionedRegionName2 + " r2 where r1.ID = r2.ID",
 +      "Select * from /" + PartitionedRegionName1 + " r1, /" + PartitionedRegionName2 + " r2 where r1.ID = r2.ID AND r1.status = r2.status",
 +      "Select * from /" + PartitionedRegionName1 + " r1, /" + PartitionedRegionName2 + " r2, /" + PartitionedRegionName3 + " r3 where r1.ID = r2.ID and r2.ID = r3.ID",
 +      "Select * from /" + PartitionedRegionName1 + " r1, /" + PartitionedRegionName2 + " r2, /" + PartitionedRegionName3
 +          + " r3  , /" + repRegionName + " r4 where r1.ID = r2.ID and r2.ID = r3.ID and r3.ID = r4.ID",
 +     // "Select * from /" + PartitionedRegionName4 + " r4 , /" + PartitionedRegionName5 + " r5 where r4.ID = r5.ID"
 +      };
 +
 +  public static String[] nonColocatedQueries = new String[]{
 +    "Select * from /" + PartitionedRegionName1 + " r1, /" + PartitionedRegionName4 + " r4 where r1.ID = r4.ID",
 +    "Select * from /" + PartitionedRegionName1 + " r1, /" + PartitionedRegionName4 + " r4 , /" + PartitionedRegionName5 + " r5 where r1.ID = r4.ID and r4.ID = r5.ID"
 +  };
 +  
 +  public static String[] queriesForRR = new String[]{"<trace> select * from /"+repRegionName+" where ID>=0"};
 +
 +  private static PRQueryDUnitHelper PRQHelp = new PRQueryDUnitHelper("");
 +  /**
 +   * @param name
 +   */
 +  public QueryUsingFunctionContextDUnitTest(String name) {
 +    super(name);
 +
 +  }
 +
 +  @Override
 +  protected final void preTearDownCacheTestCase() throws Exception {
 +    Invoke.invokeInEveryVM(CacheTestCase.class, "disconnectFromDS");
 +  }
 +  
 +  @Override
 +  protected final void postTearDownCacheTestCase() throws Exception {
 +    Invoke.invokeInEveryVM(QueryObserverHolder.class, "reset");
 +    cache = null;
 +    Invoke.invokeInEveryVM(new SerializableRunnable() { public void run() { cache = null; } });
 +  }
 +
 +  @Override
 +  public void setUp() throws Exception {
 +    super.setUp();
 +    Host host = Host.getHost(0);
 +    server1 = host.getVM(0);
 +    server2 = host.getVM(1);
 +    server3 = host.getVM(2);
 +    client = host.getVM(3);
 +    createServersWithRegions();
 +    fillValuesInRegions();
 +    registerFunctionOnServers();
 +  }
 +
 +
 +  /**
 +   * Test on Replicated Region.
 +   */
 +  public void testQueriesWithFilterKeysOnReplicatedRegion() {
 +    IgnoredException.addIgnoredException("IllegalArgumentException");
 +
 +    Object[][] r = new Object[queriesForRR.length][2];
 +
 +    client.invoke(new CacheSerializableRunnable("Run function on RR") {
 +      @Override
 +      public void run2() throws CacheException {
 +
 +        ResultCollector rcollector = null;
 +
 +        for (int i = 0; i < queriesForRR.length; i++) {
 +
 +          try {
 +            function = new TestQueryFunction("queryFunctionOnRR");
 +
 +            rcollector = FunctionService
 +                .onRegion(
 +                    CacheFactory.getAnyInstance().getRegion(repRegionName))
 +                .withArgs(queriesForRR[i]).execute(function);
 +
 +            //Should not come here, an exception is expected from above function call.
 +            fail("Function call did not fail for query with function context");
 +          } catch (FunctionException ex) {
 +            if (!(ex.getCause() instanceof IllegalArgumentException)) {
 +              fail("Should have received an IllegalArgumentException");
 +            }
 +          }
 +        }//For loop ends here.
 +      }
 +    });
 +  }
 +
 +  /**
 +   * Test on PR on one server only using filter.
 +   */
 +  public void testQueriesWithFilterKeysOnPRLocal() {
 +    
 +    client.invoke(new CacheSerializableRunnable("Test query on client and server") {
 +      
 +      @Override
 +      public void run2() throws CacheException {
 +        Set filter =  new HashSet();
 +        filter.add(0);
 +
 +        for (int i=0; i< queries.length; i++) {
 +          Object[][] r = new Object[1][2];
 +
 +          TestServerQueryFunction func = new TestServerQueryFunction("LDS Server function-1");
 +          function = new TestQueryFunction("queryFunction-1");
 +          QueryUsingFunctionContextDUnitTest test = new QueryUsingFunctionContextDUnitTest("test");
 +          ArrayList queryResults2 = test.runQueryOnClientUsingFunc(function, PartitionedRegionName1, filter, queries[i]);  
 +          if (queryResults2 == null)
 +            fail(queries[i] +"result is null from client function");
 +          
 +          ArrayList queryResults1 = test.runLDSQueryOnClientUsingFunc(func, filter, queries[i]);
 +          if (queryResults1 == null)
 +            fail(queries[i] +"result is null from LDS function");
 +          
 +          r[0][0] = queryResults1;
 +          r[0][1] = queryResults2;
 +          StructSetOrResultsSet ssORrs = new  StructSetOrResultsSet();
 +          ssORrs.CompareQueryResultsAsListWithoutAndWithIndexes(r, 1,false, new String[]{queries[i]});
 +        }   
 +      }
 +    });
 +    
 +  }
 +
 +  public void testInvalidQueries() {
 +    
 +    IgnoredException.addIgnoredException("Syntax error");
 +    client.invoke(new CacheSerializableRunnable("Test query on client and server") {
 +      
 +      @Override
 +      public void run2() throws CacheException {
 +        Set filter =  new HashSet();
 +        filter.add(0);
 +        String query = "select * from / " + repRegionName + " where ID>=0";
 +        TestServerQueryFunction func = new TestServerQueryFunction("LDS Server function-1");
 +        function = new TestQueryFunction("queryFunction-1");
 +        QueryUsingFunctionContextDUnitTest test = new QueryUsingFunctionContextDUnitTest("test");
 +        try {
 +          test.runQueryOnClientUsingFunc(function, repRegionName, filter, query);  
 +          fail("Query execution should have failed.");
 +        } catch (FunctionException ex) {
 +          assertTrue("The exception message should mention QueryInvalidException. ", ex.getLocalizedMessage().contains("QueryInvalidException"));
 +        }
 +        
 +        query = "select * from / " + PartitionedRegionName1 + " where ID>=0";
 +        func = new TestServerQueryFunction("LDS Server function-1");
 +        function = new TestQueryFunction("queryFunction-1");
 +        test = new QueryUsingFunctionContextDUnitTest("test");
 +        try {
 +          test.runQueryOnClientUsingFunc(function, PartitionedRegionName1, filter, query);  
 +          fail("Query execution should have failed.");
 +        } catch (FunctionException ex) {
 +          assertTrue("The exception message should mention QueryInvalidException. ", ex.getLocalizedMessage().contains("QueryInvalidException"));
 +        }
 +      }
 +    });
 +    
 +  }
 +  
 +  /**
 +   *
 +   */
 +  public void testQueriesWithFilterKeysOnPRLocalAndRemote() {
 +    
 +    client.invoke(new CacheSerializableRunnable("Test query on client and server") {
 +      
 +      @Override
 +      public void run2() throws CacheException {
 +        Set filter = getFilter(0, 1);
 +
 +        TestServerQueryFunction func = new TestServerQueryFunction("LDS Server function-1");
 +        function = new TestQueryFunction("queryFunction-2");
 +
 +        for (int i=0; i< queries.length; i++) {
 +          Object[][] r = new Object[1][2];
 +
 +          QueryUsingFunctionContextDUnitTest test = new QueryUsingFunctionContextDUnitTest("test");
 +          ArrayList queryResults2 = test.runQueryOnClientUsingFunc(function, PartitionedRegionName1, filter, queries[i]);
 +          if (queryResults2 == null)
 +            fail(queries[i] +"result is null from client function");
 +          ArrayList queryResults1 = test.runLDSQueryOnClientUsingFunc(func, filter, queries[i]);
 +          if (queryResults1 == null)
 +            fail(queries[i] +"result is null from LDS function");
 +          
 +
 +          r[0][0] = queryResults1;
 +          r[0][1] = queryResults2;
 +          StructSetOrResultsSet ssORrs = new  StructSetOrResultsSet();
 +          ssORrs.CompareQueryResultsAsListWithoutAndWithIndexes(r, 1,false, new String[]{queries[i]});
 +        }    
 +      }
 +    });
 +  }
 +
 +  /**
 +  *
 +  */
 + public void testQueriesWithFilterKeysOnPRLocalAndRemoteWithBucketDestroy() {
 +   
 +   // Set Query Observer in cache on server1
 +   server1.invoke(new CacheSerializableRunnable("Set QueryObserver in cache on server1") {
 +     @Override
 +     public void run2() throws CacheException {
 +
 +       class MyQueryObserver extends IndexTrackingQueryObserver {
 +
 +         @Override
 +         public void startQuery(Query query) {
 +           //Destroy only for first query.
 +           if (query.getQueryString().contains("ID>=0")){
 +             Region pr = CacheFactory.getAnyInstance().getRegion(PartitionedRegionName1);
 +             Region KeyRegion = null;
 +             for (int i=3; i<7; i++) {
 +               KeyRegion = ((PartitionedRegion)pr).getBucketRegion(i/*key*/);
 +               if (KeyRegion != null)
 +               KeyRegion.destroyRegion();
 +             }
 +           }
 +         }
 +       };
 +
 +       QueryObserverHolder.setInstance(new MyQueryObserver());
 +     }
 +   });
 +   
 +   client.invoke(new CacheSerializableRunnable("Test query on client and server") {
 +     
 +     @Override
 +     public void run2() throws CacheException {
 +       Set filter = getFilter(0, 2);
 +       
 +       TestServerQueryFunction func = new TestServerQueryFunction("LDS Server function-2");
 +       function = new TestQueryFunction("queryFunction");
 +
 +       for (int i=0; i< queries.length; i++) {
 +         
 +         QueryUsingFunctionContextDUnitTest test = new QueryUsingFunctionContextDUnitTest("test");
 +         ArrayList queryResults2 = test.runQueryOnClientUsingFunc(function, PartitionedRegionName1, filter, queries[i]);  
 +         
 +         // The Partition Region has 20 buckets with 100 values and key i goes in bucket j=(i%20)
 +         // So each bucket has 5 keys so for 3 buckets 3*5.
 +         if (i==0 && queryResults2.size() != 3*5) {
 +           fail("Result size should have been 15 but was"+queryResults2.size());
 +         }
 +       }    
 +     }
 +   });
 +   // Reset Query Observer in cache on server1
 +   server1.invoke(new CacheSerializableRunnable("Reset Query Observer on server1") {
 +     @Override
 +     public void run2() throws CacheException {
 +       QueryObserverHolder.reset();
 +     }
 +   });
 +
 + }
 +
 +  /**
 +   *
 +   */
 +  public void testQueriesWithFilterKeysOnPRWithBucketDestroy() {
 +    IgnoredException.addIgnoredException("QueryInvocationTargetException");
 +    Object[][] r = new Object[queries.length][2];
 +    Set filter =  new HashSet();
 +
 +    // Close cache on server1
 +    server1.invoke(new CacheSerializableRunnable("Set QueryObserver in cache on server1") {
 +      @Override
 +      public void run2() throws CacheException {
 +
 +        class MyQueryObserver extends IndexTrackingQueryObserver {
 +
 +          @Override
 +          public void startQuery(Query query) {
 +            Region pr = CacheFactory.getAnyInstance().getRegion(PartitionedRegionName1);
 +            Region KeyRegion = null;
 +            for (int i=0; i<7; i++) {
 +              KeyRegion = ((PartitionedRegion)pr).getBucketRegion(i/*key*/);
 +              if (KeyRegion != null)
 +              KeyRegion.destroyRegion();
 +            }
 +          }
 +        };
 +
 +        QueryObserverHolder.setInstance(new MyQueryObserver());
 +      }
 +    });
 +
 +    client.invoke(new CacheSerializableRunnable("Run function on PR") {
 +      @Override
 +      public void run2() throws CacheException {
 +        Set filter =  new HashSet();
 +        ResultCollector rcollector = null;
 +        filter.addAll(getFilter(0, 19));
 +
 +        for (int i = 0; i < queries.length; i++) {
 +
 +          try {
 +            function = new TestQueryFunction("queryFunctionBucketDestroy");
 +
 +            rcollector = FunctionService
 +                .onRegion(
 +                    CacheFactory.getAnyInstance().getRegion(PartitionedRegionName1))
 +                .withArgs(queries[i])
 +                .withFilter(filter)
 +                .execute(function);
 +
 +            //Should not come here, an exception is expected from above function call.
 +            fail("Function call did not fail for query with function context");
 +          } catch (FunctionException ex) {
 +            //ex.printStackTrace();
 +            if (!(ex.getCause() instanceof QueryInvocationTargetException)) {
 +              fail("Should have received an QueryInvocationTargetException but recieved"+ex.getMessage());
 +            }
 +          }
 +        }//For loop ends here.
 +      }
 +    });
 +
 + // Close cache on server1
 +    server1.invoke(new CacheSerializableRunnable("Reset Query Observer on server1") {
 +      @Override
 +      public void run2() throws CacheException {
 +        QueryObserverHolder.reset();
 +      }
 +    });
 +
 +  }
 +
 +  /**
 +  *
 +  */
 + public void testQueriesWithFilterKeysOnPRWithRebalancing() {
 +   IgnoredException.addIgnoredException("QueryInvocationTargetException");
 +   IgnoredException.addIgnoredException("java.net.SocketException");
 +   IgnoredException.addIgnoredException("ServerConnectivityException");
 +   IgnoredException.addIgnoredException("FunctionException");
 +   IgnoredException.addIgnoredException("IOException");
 +
 +   // Close cache on server1
 +   server1.invoke(new CacheSerializableRunnable("Set QueryObserver in cache on server1") {
 +     @Override
 +     public void run2() throws CacheException {
 +
 +       class MyQueryObserver extends IndexTrackingQueryObserver {
 +
 +         @Override
 +         public void startQuery(Query query) {
 +           Region pr = CacheFactory.getAnyInstance().getRegion(PartitionedRegionName1);
 +           Region KeyRegion = null;
 +           for (int i=6; i<9; i++) {
 +             KeyRegion = ((PartitionedRegion)pr).getBucketRegion(i/*key*/);
 +             if (KeyRegion != null)
 +             KeyRegion.destroyRegion();
 +           }
 +         }
 +       };
 +
 +       QueryObserverHolder.setInstance(new MyQueryObserver());
 +     }
 +   });
 +
 +   client.invoke(new CacheSerializableRunnable("Run function on PR") {
 +     @Override
 +     public void run2() throws CacheException {
 +       Set filter =  new HashSet();
 +       ResultCollector rcollector = null;
 +       filter.addAll(getFilter(6, 9));
 +
 +       for (int i = 0; i < queries.length; i++) {
 +
 +         try {
 +           function = new TestQueryFunction("queryFunction");
 +
 +           rcollector = FunctionService
 +               .onRegion(
 +                   CacheFactory.getAnyInstance().getRegion(PartitionedRegionName1))
 +               .withArgs(queries[i])
 +               .withFilter(filter)
 +               .execute(function);
 +
 +           //Should not come here, an exception is expected from above function call.
 +           fail("Function call did not fail for query with function context");
 +         } catch (FunctionException ex) {
 +           if (!((ex.getCause() instanceof QueryInvocationTargetException) || (ex.getCause() instanceof ServerConnectivityException))) {
 +             if (ex.getCause() instanceof FunctionException) {
 +               FunctionException fe = (FunctionException)ex.getCause();
 +               if (!fe.getMessage().startsWith("IOException")) {
 +                 fail("Should have received an QueryInvocationTargetException but recieved"+ex.getMessage());
 +               }
 +             }
 +             else {
 +               fail("Should have received an QueryInvocationTargetException but recieved"+ex.getMessage());
 +             }
 +           }
 +         }
 +       }//For loop ends here.
 +     }
 +   });
 +
 +// Close cache on server1
 +   server1.invoke(new CacheSerializableRunnable("Reset Query Observer on server1") {
 +     @Override
 +     public void run2() throws CacheException {
 +       QueryObserverHolder.reset();
 +     }
 +   });
 +
 + }
 +
 + 
 + public void testNonColocatedRegionQueries() {
 +   IgnoredException.addIgnoredException("UnsupportedOperationException");
 +   client.invoke(new CacheSerializableRunnable("Test query on non-colocated regions on server") {
 +     @Override
 +     public void run2() throws CacheException {
 +       Set filter =  new HashSet();
 +       filter.add(0);
 +
 +       for (int i=0; i< nonColocatedQueries.length; i++) {
 +         function = new TestQueryFunction("queryFunction-1");
 +         QueryUsingFunctionContextDUnitTest test = new QueryUsingFunctionContextDUnitTest("test");
 +         try {
 +          ArrayList queryResults2 = test.runQueryOnClientUsingFunc(function, PartitionedRegionName1, filter, nonColocatedQueries[i]);
 +          fail("Function call did not fail for query with function context");
 +         } catch (FunctionException e) {
 +          if (!(e.getCause() instanceof UnsupportedOperationException)) {
 +            Assert.fail("Should have received an UnsupportedOperationException but received", e);
 +          }
 +         }  
 +        }   
 +     }
 +   });
 +   
 + }
 + 
 + public void testJoinQueryPRWithMultipleIndexes(){
 +   
 +   server1.invoke(new CacheSerializableRunnable("Test query with indexes") {
 +     
 +     @Override
 +     public void run2() throws CacheException {
 +       Set filter = getFilter(0, 1);
 +       function = new TestQueryFunction("queryFunction-2");
 +       Object[][] r = new Object[2][2];
 +       QueryUsingFunctionContextDUnitTest test = new QueryUsingFunctionContextDUnitTest("test");
 +       int j = 0;
 +       for (int i=3; i< 5; i++) {
 +         ArrayList queryResults2 = test.runQueryOnClientUsingFunc(function, PartitionedRegionName1, filter, queries[i]);
 +         if (queryResults2 == null)
 +           fail(queries[i] +"result is null from client function");
 +         r[j++][1] = queryResults2;
 +       }
 +       createIndex();
 +       j = 0;
 +       for (int i=3; i< 5; i++) {
 +         ArrayList queryResults1 = test.runQueryOnClientUsingFunc(function, PartitionedRegionName1, filter, queries[i]);
 +         if (queryResults1 == null)
 +           fail(queries[i] +"result is null from client function");
 +         r[j++][0] = queryResults1;
 +       }
 +       
 +       StructSetOrResultsSet ssORrs = new  StructSetOrResultsSet();
 +       ssORrs.CompareQueryResultsAsListWithoutAndWithIndexes(r, 2,false, queries);
 +
 +     }
 +   });
 + }
 + 
 +
 +  //Helper classes and function
 +  public class TestQueryFunction extends FunctionAdapter {
 +
 +    @Override
 +    public boolean hasResult() {
 +      return true;
 +    }
 +
 +    @Override
 +    public boolean isHA() {
 +      return false;
 +    }
 +
 +    private final String id;
 +
 +
 +    public TestQueryFunction(String id) {
 +      super();
 +      this.id = id;
 +    }
 +
 +    @Override
 +    public void execute(FunctionContext context) {
 +      Cache cache = CacheFactory.getAnyInstance();
 +      QueryService queryService = cache.getQueryService();
 +      ArrayList allQueryResults = new ArrayList();
 +      String qstr = (String) context.getArguments();
 +      try {
 +        Query query = queryService.newQuery(qstr);
 +        context.getResultSender().lastResult((ArrayList) ((SelectResults) query
 +                .execute((RegionFunctionContext) context)).asList());
 +      } catch (Exception e) {
 +        throw new FunctionException(e);
 +      }
 +    }
 +
 +    @Override
 +    public String getId() {
 +      return this.id;
 +    }
 +  }
 +  
 +  public class TestServerQueryFunction extends FunctionAdapter {
 +
 +    @Override
 +    public boolean hasResult() {
 +      return true;
 +    }
 +
 +    @Override
 +    public boolean isHA() {
 +      return false;
 +    }
 +
 +    private final String id;
 +
 +
 +    public TestServerQueryFunction(String id) {
 +      super();
 +      this.id = id;
 +    }
 +
 +    @Override
 +    public void execute(FunctionContext context) {
 +      Cache cache = CacheFactory.getAnyInstance();
 +      QueryService queryService = cache.getQueryService();
 +      ArrayList allQueryResults = new ArrayList();
 +      Object[] args = (Object[])context.getArguments();
 +      Set buckets = getBucketsForFilter((Set)args[1]);
 +      Region localDataSet = new LocalDataSet((PartitionedRegion)CacheFactory.getAnyInstance().getRegion(PartitionedRegionName1), buckets);
 +
 +      try {
 +        Query query = queryService.newQuery((String)args[0]);
 +        context.getResultSender().lastResult((ArrayList) ((SelectResults) ((LocalDataSet) localDataSet).executeQuery(
 +            (DefaultQuery) query, null, buckets)).asList());
 +      } catch (Exception e) {
 +        throw new FunctionException(e);
 +      }
 +    }
 +
 +    @Override
 +    public String getId() {
 +      return this.id;
 +    }
 +    
 +    private Set getBucketsForFilter(Set filter) {
 +      Set bucketids = new HashSet();
 +      for (Object key: filter) {
 +        int intKey = ((Integer)key).intValue();
 +        bucketids.add(intKey%numOfBuckets);
 +      }
 +      return bucketids;
 +    }
 +  }
 +
 +  public void fillValuesInRegions() {
 +    //Create common Portflios and NewPortfolios
 +    final Portfolio[] portfolio = PRQHelp.createPortfoliosAndPositions(cntDest);
 +
 +    //Fill local region
 +    server1.invoke(getCacheSerializableRunnableForPRPuts(localRegionName,
 +        portfolio, cnt, cntDest));
 +
 +    //Fill replicated region
 +    server1.invoke(getCacheSerializableRunnableForPRPuts(repRegionName,
 +        portfolio, cnt, cntDest));
 +
 +    //Fill Partition Region
 +    server1.invoke(getCacheSerializableRunnableForPRPuts(PartitionedRegionName1,
 +        portfolio, cnt, cntDest));
 +    server1.invoke(getCacheSerializableRunnableForPRPuts(PartitionedRegionName2,
 +        portfolio, cnt, cntDest));
 +    server1.invoke(getCacheSerializableRunnableForPRPuts(PartitionedRegionName3,
 +        portfolio, cnt, cntDest));
 +    server1.invoke(getCacheSerializableRunnableForPRPuts(PartitionedRegionName4,
 +        portfolio, cnt, cntDest));
 +    server1.invoke(getCacheSerializableRunnableForPRPuts(PartitionedRegionName5,
 +        portfolio, cnt, cntDest));
 +  }
 +
 +  private void registerFunctionOnServers() {
 +    function = new TestQueryFunction("queryFunction");
 +    server1.invoke(PRClientServerTestBase.class,
 +        "registerFunction", new Object []{function});
 +
 +    server2.invoke(PRClientServerTestBase.class,
 +        "registerFunction", new Object []{function});
 +
 +    server3.invoke(PRClientServerTestBase.class,
 +        "registerFunction", new Object []{function});
 +  }
 +
 +  private void createServersWithRegions() {
 +    //Create caches
 +    Properties props = new Properties();
-     server1.invoke(PRClientServerTestBase.class, "createCacheInVm",
-         new Object[] { props });
-     server2.invoke(PRClientServerTestBase.class, "createCacheInVm",
-         new Object[] { props });
-     server3.invoke(PRClientServerTestBase.class, "createCacheInVm",
-         new Object[] { props });
++    server1.invoke(() -> PRClientServerTestBase.createCacheInVm( props ));
++    server2.invoke(() -> PRClientServerTestBase.createCacheInVm( props ));
++    server3.invoke(() -> PRClientServerTestBase.createCacheInVm( props ));
 +
 +    //Create Cache Servers
-     Integer port1 = (Integer)server1.invoke(PRClientServerTestBase.class,
-     "createCacheServer");
-     Integer port2 = (Integer)server2.invoke(PRClientServerTestBase.class,
-         "createCacheServer");
-     Integer port3 = (Integer)server3.invoke(PRClientServerTestBase.class,
-         "createCacheServer");
++    Integer port1 = (Integer)server1.invoke(() -> PRClientServerTestBase.createCacheServer());
++    Integer port2 = (Integer)server2.invoke(() -> PRClientServerTestBase.createCacheServer());
++    Integer port3 = (Integer)server3.invoke(() -> PRClientServerTestBase.createCacheServer());
 +    serverPort1 = port1;
 +    serverPort2 = port2;
 +    serverPort3 = port3;
 +
 +    //Create client cache without regions
-     client.invoke(QueryUsingFunctionContextDUnitTest.class, "createCacheClientWithoutRegion",
-         new Object[] { NetworkUtils.getServerHostName(server1.getHost()), port1, port2,
-             port3 });
++    client.invoke(() -> QueryUsingFunctionContextDUnitTest.createCacheClientWithoutRegion( NetworkUtils.getServerHostName(server1.getHost()), port1, port2,
++            port3 ));
 +
 +    //Create proxy regions on client.
-     client.invoke(QueryUsingFunctionContextDUnitTest.class, "createProxyRegions");
++    client.invoke(() -> QueryUsingFunctionContextDUnitTest.createProxyRegions());
 +
 +    //Create local Region on servers
-     server1.invoke(QueryUsingFunctionContextDUnitTest.class, "createLocalRegion");
++    server1.invoke(() -> QueryUsingFunctionContextDUnitTest.createLocalRegion());
 +
 +    //Create ReplicatedRegion on servers
-     server1.invoke(QueryUsingFunctionContextDUnitTest.class, "createReplicatedRegion");
-     server2.invoke(QueryUsingFunctionContextDUnitTest.class, "createReplicatedRegion");
-     server3.invoke(QueryUsingFunctionContextDUnitTest.class, "createReplicatedRegion");
++    server1.invoke(() -> QueryUsingFunctionContextDUnitTest.createReplicatedRegion());
++    server2.invoke(() -> QueryUsingFunctionContextDUnitTest.createReplicatedRegion());
++    server3.invoke(() -> QueryUsingFunctionContextDUnitTest.createReplicatedRegion());
 +
 +    //Create two colocated PartitionedRegions On Servers.
-     server1.invoke(QueryUsingFunctionContextDUnitTest.class, "createColocatedPR");
-     server2.invoke(QueryUsingFunctionContextDUnitTest.class, "createColocatedPR");
-     server3.invoke(QueryUsingFunctionContextDUnitTest.class, "createColocatedPR");
++    server1.invoke(() -> QueryUsingFunctionContextDUnitTest.createColocatedPR());
++    server2.invoke(() -> QueryUsingFunctionContextDUnitTest.createColocatedPR());
++    server3.invoke(() -> QueryUsingFunctionContextDUnitTest.createColocatedPR());
 +
 +  }
 +
 +  public static void createProxyRegions() {
 +    new QueryUsingFunctionContextDUnitTest("temp").createProxyRegs();
 +  }
 +
 +  private void createProxyRegs() {
 +    ClientCache cache = (ClientCache)CacheFactory.getAnyInstance();
 +    cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(repRegionName);
 +
 +    cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(localRegionName);
 +
 +    cache.createClientRegionFactory(ClientRegionShortcut.PROXY)
 +      .create(PartitionedRegionName1);
 +    cache.createClientRegionFactory(ClientRegionShortcut.PROXY)
 +      .create(PartitionedRegionName2);
 +    cache.createClientRegionFactory(ClientRegionShortcut.PROXY)
 +    .create(PartitionedRegionName3);
 +    cache.createClientRegionFactory(ClientRegionShortcut.PROXY)
 +    .create(PartitionedRegionName4);
 +    cache.createClientRegionFactory(ClientRegionShortcut.PROXY)
 +    .create(PartitionedRegionName5);
 +  }
 +
 +  public static void createLocalRegion() {
 +    new QueryUsingFunctionContextDUnitTest("temp").createLocalReg();
 +  }
 +  public void createLocalReg() {
 +    cache = CacheFactory.getAnyInstance();
 +    cache.createRegionFactory(RegionShortcut.LOCAL).create(localRegionName);
 +  }
 +
 +  public static void createReplicatedRegion() {
 +    new QueryUsingFunctionContextDUnitTest("temp").createRR();
 +  }
 +  public void createRR() {
 +    cache = CacheFactory.getAnyInstance();
 +    cache.createRegionFactory(RegionShortcut.REPLICATE).create(repRegionName);
 +  }
 +
 +  public static void createColocatedPR() {
 +    new QueryUsingFunctionContextDUnitTest("temp").createColoPR();
 +  }
 +  public void createColoPR() {
 +    PartitionResolver testKeyBasedResolver = new QueryAPITestPartitionResolver();
 +    cache = CacheFactory.getAnyInstance();
 +    cache
 +        .createRegionFactory(RegionShortcut.PARTITION)
 +        .setPartitionAttributes(
 +            new PartitionAttributesFactory()
 +                .setTotalNumBuckets(numOfBuckets)
 +                .setPartitionResolver(testKeyBasedResolver)
 +                .create())
 +        .create(PartitionedRegionName1);
 +
 +    cache
 +        .createRegionFactory(RegionShortcut.PARTITION)
 +        .setPartitionAttributes(
 +            new PartitionAttributesFactory()
 +                .setTotalNumBuckets(numOfBuckets)
 +                .setPartitionResolver(testKeyBasedResolver)
 +                .setColocatedWith(PartitionedRegionName1)
 +                .create())
 +        .create(PartitionedRegionName2);
 +    
 +    cache
 +    .createRegionFactory(RegionShortcut.PARTITION)
 +    .setPartitionAttributes(
 +        new PartitionAttributesFactory()
 +            .setTotalNumBuckets(numOfBuckets)
 +            .setPartitionResolver(testKeyBasedResolver)
 +            .setColocatedWith(PartitionedRegionName2)
 +            .create())
 +    .create(PartitionedRegionName3);
 +    
 +    cache
 +    .createRegionFactory(RegionShortcut.PARTITION)
 +    .setPartitionAttributes(
 +        new PartitionAttributesFactory()
 +            .setTotalNumBuckets(numOfBuckets)
 +            .setPartitionResolver(testKeyBasedResolver)
 +             .create())
 +    .create(PartitionedRegionName4); // not collocated
 +    
 +    cache
 +    .createRegionFactory(RegionShortcut.PARTITION)
 +    .setPartitionAttributes(
 +        new PartitionAttributesFactory()
 +            .setTotalNumBuckets(numOfBuckets)
 +            .setPartitionResolver(testKeyBasedResolver)
 +            .setColocatedWith(PartitionedRegionName4)
 +             .create())
 +    .create(PartitionedRegionName5); // collocated with 4 
 +  }
 +
 +  public static void createCacheClientWithoutRegion(String host, Integer port1, Integer port2, Integer port3) {
 +    new QueryUsingFunctionContextDUnitTest("temp").createCacheClientWithoutReg(host, port1, port2, port3);
 +  }
 +  private void createCacheClientWithoutReg(String host, Integer port1, Integer port2, Integer port3) {
 +    this.disconnectFromDS();
 +    ClientCache cache = new ClientCacheFactory()
 +      .addPoolServer(host, port1)
 +      .addPoolServer(host, port2)
 +      .addPoolServer(host, port3)
 +      .create();
 +  }
 +
 +  /**
 +   * Run query on server using LocalDataSet.executeQuery() to compare results
 +   * received from client function execution.
 +   */
 +  public static ArrayList runQueryOnServerLocalDataSet(String query, Set filter) {
 +    return new QueryUsingFunctionContextDUnitTest("temp").runQueryOnServerLDS(query, filter);
 +  }
 +
 +  protected ArrayList runQueryOnServerLDS(String queryStr, Set filter) {
 +
 +    Set buckets = getBucketsForFilter(filter);
 +    Region localDataSet = new LocalDataSet((PartitionedRegion)CacheFactory.getAnyInstance().getRegion(PartitionedRegionName1), buckets);
 +
 +    QueryService qservice = CacheFactory.getAnyInstance().getQueryService();
 +
 +    Query query = qservice.newQuery(queryStr);
 +    SelectResults results;
 +    try {
 +      results = (SelectResults) ((LocalDataSet) localDataSet).executeQuery(
 +          (DefaultQuery) query, null, buckets);
 +
 +      return (ArrayList)results.asList();
 +    } catch (Exception e) {
 +      e.printStackTrace();
 +    }
 +    return null;
 +  }
 +
 +  /**
 +   * Run query on server to compare the results received from client function execution.
 +   */
 +  public static ArrayList runQueryOnServerRegion(String query) {
 +    return new QueryUsingFunctionContextDUnitTest("temp").runQueryOnServerReg(query);
 +  }
 +  protected ArrayList runQueryOnServerReg(String queryStr) {
 +
 +    QueryService qservice = CacheFactory.getAnyInstance().getQueryService();
 +
 +    Query query = qservice.newQuery(queryStr);
 +    SelectResults results = null;
 +    try {
 +      results = (SelectResults) query.execute();
 +    } catch (Exception e) {
 +      e.printStackTrace();
 +    }
 +    return results!= null ? (ArrayList)results.asList():null;
 +  }
 +
 +  /**
 +   * Run query using a function executed by client on a region on server with filter.
 +   * @param function
 +   * @param regionName
 +   * @param filter
 +   * @return ArrayList of results
 +   */
 +  public static ArrayList runQueryOnClientUsingFunction(Function function, String regionName, Set filter, String query) {
 +    return new QueryUsingFunctionContextDUnitTest("temp").runQueryOnClientUsingFunc(function, regionName, filter, query);
 +  }
 +
 +  private ArrayList runQueryOnClientUsingFunc(Function func, String regionName, Set filter, String query) {
 +    ResultCollector rcollector = null;
 +
 +    // Filter can not be set as null if withFilter() is called.
 +    if (filter != null) {
 +      rcollector = FunctionService.onRegion(CacheFactory.getAnyInstance().getRegion(regionName))
 +        .withArgs(query)
 +        .withFilter(filter)
 +        .execute(func);
 +    } else {
 +      rcollector = FunctionService.onRegion(CacheFactory.getAnyInstance().getRegion(regionName))
 +      .withArgs(query)
 +      .execute(func);
 +    }
 +    Object result = rcollector.getResult();
 +    assertTrue(result instanceof ArrayList);
 +
 +    //Results from multiple nodes.
 +    ArrayList resultList = (ArrayList)result;
 +    resultList.trimToSize();
 +    List queryResults = null;
 +
 +    if (resultList.size()!=0 && resultList.get(0) instanceof ArrayList) {
 +      queryResults = new ArrayList();
 +      for (Object obj: resultList) {
 +        if (obj != null) {
 +          queryResults.addAll((ArrayList)obj);
 +        }
 +      }
 +    }
 +
 +    return (ArrayList) queryResults;
 +  }
 +
 +  /**
 +   * Runs a {@link LocalDataSet} query on a single server. 
 +   * @param func
 +   * @param filter
 +   * @param query
 +   * @return results in a List
 +   */
 +  private ArrayList runLDSQueryOnClientUsingFunc(Function func, Set filter, String query) {
 +    ResultCollector rcollector = null;
 +
 +    // Filter can not be set as null if withFilter() is called.
 +    rcollector = FunctionService
 +        .onServer(ClientCacheFactory.getAnyInstance())
 +        .withArgs(new Object[]{query, filter}).execute(func);
 +    Object result = rcollector.getResult();
 +    assertTrue(result instanceof ArrayList);
 +
 +    //Results from multiple nodes.
 +    ArrayList resultList = (ArrayList)result;
 +    resultList.trimToSize();
 +    List queryResults = new ArrayList();
 +    
 +    if (resultList.size()!=0 && resultList.get(0) instanceof ArrayList) {
 +      for (Object obj: resultList) {
 +        if (obj != null) {
 +          queryResults.addAll((ArrayList)obj);
 +        }
 +      }
 +    }
 +
 +    return (ArrayList) queryResults;
 +  }
 +
 +  private Set getFilter(int start, int end) {
 +    Set filter = new HashSet();
 +    for (int j=start; j<=end; j++) {
 +      filter.add(j);
 +    }
 +    return filter;
 +  }
 +
 +
 +  private Set getBucketsForFilter(Set filter) {
 +    Set bucketids = new HashSet();
 +    for (Object key: filter) {
 +      int intKey = ((Integer)key).intValue();
 +      bucketids.add(intKey%numOfBuckets);
 +    }
 +    return bucketids;
 +  }
 +
 +  /**
 +   * This function puts portfolio objects into the created Region (PR or Local) *
 +   *
 +   * @param regionName
 +   * @param portfolio
 +   * @param to
 +   * @param from
 +   * @return cacheSerializable object
 +   */
 +  public CacheSerializableRunnable getCacheSerializableRunnableForPRPuts(
 +      final String regionName, final Object[] portfolio, final int from,
 +      final int to)
 +  {
 +    SerializableRunnable puts = new CacheSerializableRunnable("Region Puts") {
 +      @Override
 +      public void run2() throws CacheException
 +      {
 +        Cache cache = CacheFactory.getAnyInstance();
 +        Region region = cache.getRegion(regionName);
 +        for (int j = from; j < to; j++)
 +          region.put(new Integer(j), portfolio[j]);
 +        LogWriterUtils.getLogWriter()
 +            .info(
 +                "PRQueryDUnitHelper#getCacheSerializableRunnableForPRPuts: Inserted Portfolio data on Region "
 +                    + regionName);
 +      }
 +    };
 +    return (CacheSerializableRunnable)puts;
 +  }
 +  
 + 
 +  public void createIndex() {
 +    QueryService qs = CacheFactory.getAnyInstance().getQueryService();
 +    try {
 +      qs.createIndex("ID1", "ID", "/"+ PartitionedRegionName1);
 +      qs.createIndex("ID2", "ID", "/"+ PartitionedRegionName2);
 +      qs.createIndex("ID3", "ID", "/"+ PartitionedRegionName3);
 +    } catch (Exception e) {
 +        fail("Index creation failed " + e);
 +    }
 +  }
 +
 +}


[22/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/hll/IBuilder.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/hll/IBuilder.java
index 0000000,0000000..0e74fe6
new file mode 100755
--- /dev/null
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/hll/IBuilder.java
@@@ -1,0 -1,0 +1,24 @@@
++/*
++ * Copyright (C) 2011 Clearspring Technologies, Inc.
++ *
++ * Licensed 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.hll;
++
++
++public interface IBuilder<T> {
++
++    T build();
++
++    int sizeof();
++}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/hll/ICardinality.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/hll/ICardinality.java
index 0000000,0000000..fd32154
new file mode 100755
--- /dev/null
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/hll/ICardinality.java
@@@ -1,0 -1,0 +1,72 @@@
++/*
++ * Copyright (C) 2011 Clearspring Technologies, Inc.
++ *
++ * Licensed 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.hll;
++
++
++import java.io.IOException;
++
++
++public interface ICardinality {
++
++    /**
++     * @param o stream element
++     * @return false if the value returned by cardinality() is unaffected by the appearance of o in the stream.
++     */
++    boolean offer(Object o);
++
++    /**
++     * Offer the value as a hashed long value
++     *
++     * @param hashedLong - the hash of the item to offer to the estimator
++     * @return false if the value returned by cardinality() is unaffected by the appearance of hashedLong in the stream
++     */
++    boolean offerHashed(long hashedLong);
++
++    /**
++     * Offer the value as a hashed long value
++     *
++     * @param hashedInt - the hash of the item to offer to the estimator
++     * @return false if the value returned by cardinality() is unaffected by the appearance of hashedInt in the stream
++     */
++    boolean offerHashed(int hashedInt);
++
++    /**
++     * @return the number of unique elements in the stream or an estimate thereof
++     */
++    long cardinality();
++
++    /**
++     * @return size in bytes needed for serialization
++     */
++    int sizeof();
++
++    /**
++     * @return byte[]
++     * @throws IOException
++     */
++    byte[] getBytes() throws IOException;
++
++    /**
++     * Merges estimators to produce a new estimator for the combined streams
++     * of this estimator and those passed as arguments.
++     * <p/>
++     * Nor this estimator nor the one passed as parameters are modified.
++     *
++     * @param estimators Zero or more compatible estimators
++     * @throws CardinalityMergeException If at least one of the estimators is not compatible with this one
++     */
++    ICardinality merge(ICardinality... estimators) throws CardinalityMergeException;
++}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/hll/MurmurHash.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/hll/MurmurHash.java
index 0000000,0000000..139e029
new file mode 100755
--- /dev/null
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/hll/MurmurHash.java
@@@ -1,0 -1,0 +1,245 @@@
++/*
++ * 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.hll;
++
++
++/**
++ * This is a very fast, non-cryptographic hash suitable for general hash-based
++ * lookup. See http://murmurhash.googlepages.com/ for more details.
++ * <p/>
++ * <p>
++ * The C version of MurmurHash 2.0 found at that site was ported to Java by
++ * Andrzej Bialecki (ab at getopt org).
++ * </p>
++ */
++public class MurmurHash
++{
++    public static int hash(Object o)
++    {
++        if (o == null)
++        {
++            return 0;
++        }
++        if (o instanceof Long)
++        {
++            return hashLong((Long) o);
++        }
++        if (o instanceof Integer)
++        {
++            return hashLong((Integer) o);
++        }
++        if (o instanceof Double)
++        {
++            return hashLong(Double.doubleToRawLongBits((Double) o));
++        }
++        if (o instanceof Float)
++        {
++            return hashLong(Float.floatToRawIntBits((Float) o));
++        }
++        if (o instanceof String)
++        {
++            return hash(((String) o).getBytes());
++        }
++        if (o instanceof byte[])
++        {
++            return hash((byte[]) o);
++        }
++        return hash(o.toString());
++    }
++
++    public static int hash(byte[] data)
++    {
++        return hash(data, 0, data.length, -1);
++    }
++
++    public static int hash(byte[] data, int seed)
++    {
++        return hash(data, 0, data.length, seed);
++    }
++
++    public static int hash(byte[] data, int offset, int length, int seed)
++    {
++        int m = 0x5bd1e995;
++        int r = 24;
++
++        int h = seed ^ length;
++
++        int len_4 = length >> 2;
++
++        for (int i = 0; i < len_4; i++)
++        {
++            int i_4 = i << 2;
++            int k = data[offset + i_4 + 3];
++            k = k << 8;
++            k = k | (data[offset + i_4 + 2] & 0xff);
++            k = k << 8;
++            k = k | (data[offset + i_4 + 1] & 0xff);
++            k = k << 8;
++            k = k | (data[offset + i_4 + 0] & 0xff);
++            k *= m;
++            k ^= k >>> r;
++            k *= m;
++            h *= m;
++            h ^= k;
++        }
++
++        // avoid calculating modulo
++        int len_m = len_4 << 2;
++        int left = length - len_m;
++
++        if (left != 0)
++        {
++            if (left >= 3)
++            {
++                h ^= (int) data[offset + length - 3] << 16;
++            }
++            if (left >= 2)
++            {
++                h ^= (int) data[offset + length - 2] << 8;
++            }
++            if (left >= 1)
++            {
++                h ^= (int) data[offset + length - 1];
++            }
++
++            h *= m;
++        }
++
++        h ^= h >>> 13;
++        h *= m;
++        h ^= h >>> 15;
++
++        return h;
++    }
++
++    public static int hashLong(long data)
++    {
++        int m = 0x5bd1e995;
++        int r = 24;
++
++        int h = 0;
++
++        int k = (int) data * m;
++        k ^= k >>> r;
++        h ^= k * m;
++
++        k = (int) (data >> 32) * m;
++        k ^= k >>> r;
++        h *= m;
++        h ^= k * m;
++
++        h ^= h >>> 13;
++        h *= m;
++        h ^= h >>> 15;
++
++        return h;
++    }
++
++    public static long hash64(Object o)
++    {
++        if (o == null)
++        {
++            return 0l;
++        }
++        else if (o instanceof String)
++        {
++            final byte[] bytes = ((String) o).getBytes();
++            return hash64(bytes, bytes.length);
++        }
++        else if (o instanceof byte[])
++        {
++            final byte[] bytes = (byte[]) o;
++            return hash64(bytes, bytes.length);
++        }
++        return hash64(o.toString());
++    }
++
++    // 64 bit implementation copied from here:  https://github.com/tnm/murmurhash-java
++
++    /**
++     * Generates 64 bit hash from byte array with default seed value.
++     *
++     * @param data   byte array to hash
++     * @param length length of the array to hash
++     * @return 64 bit hash of the given string
++     */
++    public static long hash64(final byte[] data, int length)
++    {
++        return hash64(data, length, 0xe17a1465);
++    }
++
++
++    /**
++     * Generates 64 bit hash from byte array of the given length and seed.
++     *
++     * @param data   byte array to hash
++     * @param length length of the array to hash
++     * @param seed   initial seed value
++     * @return 64 bit hash of the given array
++     */
++    public static long hash64(final byte[] data, int length, int seed)
++    {
++        final long m = 0xc6a4a7935bd1e995L;
++        final int r = 47;
++
++        long h = (seed & 0xffffffffl) ^ (length * m);
++
++        int length8 = length / 8;
++
++        for (int i = 0; i < length8; i++)
++        {
++            final int i8 = i * 8;
++            long k = ((long) data[i8 + 0] & 0xff) + (((long) data[i8 + 1] & 0xff) << 8)
++                    + (((long) data[i8 + 2] & 0xff) << 16) + (((long) data[i8 + 3] & 0xff) << 24)
++                    + (((long) data[i8 + 4] & 0xff) << 32) + (((long) data[i8 + 5] & 0xff) << 40)
++                    + (((long) data[i8 + 6] & 0xff) << 48) + (((long) data[i8 + 7] & 0xff) << 56);
++
++            k *= m;
++            k ^= k >>> r;
++            k *= m;
++
++            h ^= k;
++            h *= m;
++        }
++
++        switch (length % 8)
++        {
++            case 7:
++                h ^= (long) (data[(length & ~7) + 6] & 0xff) << 48;
++            case 6:
++                h ^= (long) (data[(length & ~7) + 5] & 0xff) << 40;
++            case 5:
++                h ^= (long) (data[(length & ~7) + 4] & 0xff) << 32;
++            case 4:
++                h ^= (long) (data[(length & ~7) + 3] & 0xff) << 24;
++            case 3:
++                h ^= (long) (data[(length & ~7) + 2] & 0xff) << 16;
++            case 2:
++                h ^= (long) (data[(length & ~7) + 1] & 0xff) << 8;
++            case 1:
++                h ^= (long) (data[length & ~7] & 0xff);
++                h *= m;
++        }
++        ;
++
++        h ^= h >>> r;
++        h *= m;
++        h ^= h >>> r;
++
++        return h;
++    }
++}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/hll/RegisterSet.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/hll/RegisterSet.java
index 0000000,0000000..e35340f
new file mode 100755
--- /dev/null
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/hll/RegisterSet.java
@@@ -1,0 -1,0 +1,110 @@@
++/*
++ * Copyright (C) 2012 Clearspring Technologies, Inc.
++ *
++ * Licensed 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.hll;
++
++
++public class RegisterSet {
++
++    public final static int LOG2_BITS_PER_WORD = 6;
++    public final static int REGISTER_SIZE = 5;
++
++    public final int count;
++    public final int size;
++
++    private final int[] M;
++
++    public RegisterSet(int count) {
++        this(count, null);
++    }
++
++    public RegisterSet(int count, int[] initialValues) {
++        this.count = count;
++
++        if (initialValues == null) {
++            this.M = new int[getSizeForCount(count)];
++        } else {
++            this.M = initialValues;
++        }
++        this.size = this.M.length;
++    }
++
++    public static int getBits(int count) {
++        return count / LOG2_BITS_PER_WORD;
++    }
++
++    public static int getSizeForCount(int count) {
++        int bits = getBits(count);
++        if (bits == 0) {
++            return 1;
++        } else if (bits % Integer.SIZE == 0) {
++            return bits;
++        } else {
++            return bits + 1;
++        }
++    }
++
++    public void set(int position, int value) {
++        int bucketPos = position / LOG2_BITS_PER_WORD;
++        int shift = REGISTER_SIZE * (position - (bucketPos * LOG2_BITS_PER_WORD));
++        this.M[bucketPos] = (this.M[bucketPos] & ~(0x1f << shift)) | (value << shift);
++    }
++
++    public int get(int position) {
++        int bucketPos = position / LOG2_BITS_PER_WORD;
++        int shift = REGISTER_SIZE * (position - (bucketPos * LOG2_BITS_PER_WORD));
++        return (this.M[bucketPos] & (0x1f << shift)) >>> shift;
++    }
++
++    public boolean updateIfGreater(int position, int value) {
++        int bucket = position / LOG2_BITS_PER_WORD;
++        int shift = REGISTER_SIZE * (position - (bucket * LOG2_BITS_PER_WORD));
++        int mask = 0x1f << shift;
++
++        // Use long to avoid sign issues with the left-most shift
++        long curVal = this.M[bucket] & mask;
++        long newVal = value << shift;
++        if (curVal < newVal) {
++            this.M[bucket] = (int) ((this.M[bucket] & ~mask) | newVal);
++            return true;
++        } else {
++            return false;
++        }
++    }
++
++    public void merge(RegisterSet that) {
++        for (int bucket = 0; bucket < M.length; bucket++) {
++            int word = 0;
++            for (int j = 0; j < LOG2_BITS_PER_WORD; j++) {
++                int mask = 0x1f << (REGISTER_SIZE * j);
++
++                int thisVal = (this.M[bucket] & mask);
++                int thatVal = (that.M[bucket] & mask);
++                word |= (thisVal < thatVal) ? thatVal : thisVal;
++            }
++            this.M[bucket] = word;
++        }
++    }
++
++    int[] readOnlyBits() {
++        return M;
++    }
++
++    public int[] bits() {
++        int[] copy = new int[size];
++        System.arraycopy(M, 0, copy, 0, M.length);
++        return copy;
++    }
++}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/redis/RegionProvider.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/redis/RegionProvider.java
index f9539e5,0000000..dcd83cb
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/redis/RegionProvider.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/redis/RegionProvider.java
@@@ -1,553 -1,0 +1,553 @@@
 +/*
 + * 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.redis;
 +
 +import java.io.Closeable;
 +import java.util.HashMap;
 +import java.util.Map;
 +import java.util.Map.Entry;
 +import java.util.Set;
 +import java.util.concurrent.ConcurrentHashMap;
 +import java.util.concurrent.ConcurrentMap;
 +import java.util.concurrent.ScheduledExecutorService;
 +import java.util.concurrent.ScheduledFuture;
 +import java.util.concurrent.TimeUnit;
 +import java.util.concurrent.locks.Lock;
 +import java.util.concurrent.locks.ReentrantLock;
 +
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheTransactionManager;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionShortcut;
 +import com.gemstone.gemfire.cache.TransactionId;
 +import com.gemstone.gemfire.cache.query.IndexExistsException;
 +import com.gemstone.gemfire.cache.query.IndexInvalidException;
 +import com.gemstone.gemfire.cache.query.IndexNameConflictException;
 +import com.gemstone.gemfire.cache.query.Query;
 +import com.gemstone.gemfire.cache.query.QueryInvalidException;
 +import com.gemstone.gemfire.cache.query.QueryService;
 +import com.gemstone.gemfire.cache.query.RegionNotFoundException;
 +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
 +import com.gemstone.gemfire.internal.redis.executor.ExpirationExecutor;
 +import com.gemstone.gemfire.internal.redis.executor.ListQuery;
 +import com.gemstone.gemfire.internal.redis.executor.SortedSetQuery;
- import com.gemstone.gemfire.internal.redis.executor.hll.HyperLogLogPlus;
++import com.gemstone.gemfire.internal.hll.HyperLogLogPlus;
 +import com.gemstone.gemfire.management.cli.Result;
 +import com.gemstone.gemfire.management.cli.Result.Status;
 +import com.gemstone.gemfire.management.internal.cli.commands.CreateAlterDestroyRegionCommands;
 +import com.gemstone.gemfire.redis.GemFireRedisServer;
 +
 +/**
 + * This class stands between {@link Executor} and {@link Cache#getRegion(String)}.
 + * This is needed because some keys for Redis represented as a {@link Region} in
 + * {@link GemFireRedisServer} come with additional state. Therefore getting, creating,
 + * or destroying a {@link Region} needs to be synchronized, which is done away with
 + * and abstracted by this class.
 + * 
 + * @author Vitaly Gavrilov
 + *
 + */
 +public class RegionProvider implements Closeable {
 +
 +  private final ConcurrentHashMap<ByteArrayWrapper, Region<?, ?>> regions;
 +
 +  /**
 +   * This is the Redis meta data {@link Region} that holds the {@link RedisDataType}
 +   * information for all Regions created. The mapping is a {@link String} key which is the name
 +   * of the {@link Region} created to hold the data to the RedisDataType it contains.
 +   */
 +  private final Region<String, RedisDataType> redisMetaRegion;
 +
 +  /**
 +   * This is the {@link RedisDataType#REDIS_STRING} {@link Region}. This is the Region
 +   * that stores all string contents
 +   */
 +  private final Region<ByteArrayWrapper, ByteArrayWrapper> stringsRegion;
 +
 +  /**
 +   * This is the {@link RedisDataType#REDIS_HLL} {@link Region}. This is the Region
 +   * that stores all HyperLogLog contents
 +   */
 +  private final Region<ByteArrayWrapper, HyperLogLogPlus> hLLRegion;
 +
 +  private final Cache cache;
 +  private final QueryService queryService;
 +  private final ConcurrentMap<ByteArrayWrapper, Map<Enum<?>, Query>> preparedQueries = new ConcurrentHashMap<ByteArrayWrapper, Map<Enum<?>, Query>>();
 +  private final ConcurrentMap<ByteArrayWrapper, ScheduledFuture<?>> expirationsMap;
 +  private final ScheduledExecutorService expirationExecutor;
 +  private final RegionShortcut defaultRegionType;
 +  private static final CreateAlterDestroyRegionCommands cliCmds = new CreateAlterDestroyRegionCommands();
 +  private final ConcurrentHashMap<String, Lock> locks;
 +
 +  public RegionProvider(Region<ByteArrayWrapper, ByteArrayWrapper> stringsRegion, Region<ByteArrayWrapper, HyperLogLogPlus> hLLRegion, Region<String, RedisDataType> redisMetaRegion, ConcurrentMap<ByteArrayWrapper, ScheduledFuture<?>> expirationsMap, ScheduledExecutorService expirationExecutor, RegionShortcut defaultShortcut) {
 +    if (stringsRegion == null || hLLRegion == null || redisMetaRegion == null)
 +      throw new NullPointerException();
 +    this.regions = new ConcurrentHashMap<ByteArrayWrapper, Region<?, ?>>();
 +    this.stringsRegion = stringsRegion;
 +    this.hLLRegion = hLLRegion;
 +    this.redisMetaRegion = redisMetaRegion;
 +    this.cache = GemFireCacheImpl.getInstance();
 +    this.queryService = cache.getQueryService();
 +    this.expirationsMap = expirationsMap;
 +    this.expirationExecutor = expirationExecutor;
 +    this.defaultRegionType = defaultShortcut;
 +    this.locks = new ConcurrentHashMap<String, Lock>();
 +  }
 +
 +  public boolean existsKey(ByteArrayWrapper key) {
 +    return this.redisMetaRegion.containsKey(key.toString());
 +  }
 +
 +  public Set<String> metaKeySet() {
 +    return this.redisMetaRegion.keySet();
 +  }
 +
 +  public Set<Map.Entry<String, RedisDataType>> metaEntrySet() {
 +    return this.redisMetaRegion.entrySet();
 +  }
 +
 +  public int getMetaSize() {
 +    return this.redisMetaRegion.size() - RedisConstants.NUM_DEFAULT_KEYS;
 +  }
 +
 +  private boolean metaRemoveEntry(ByteArrayWrapper key) {
 +    return this.redisMetaRegion.remove(key.toString()) != null;
 +  }
 +
 +  public RedisDataType metaPutIfAbsent(ByteArrayWrapper key, RedisDataType value) {
 +    return this.redisMetaRegion.putIfAbsent(key.toString(), value);
 +  }
 +
 +  public RedisDataType metaPut(ByteArrayWrapper key, RedisDataType value) {
 +    return this.redisMetaRegion.put(key.toString(), value);
 +  }
 +
 +  public RedisDataType metaGet(ByteArrayWrapper key) {
 +    return this.redisMetaRegion.get(key.toString());
 +  }
 +
 +  public Region<?, ?> getRegion(ByteArrayWrapper key) {
 +    return this.regions.get(key);
 +  }
 +
 +  public void removeRegionReferenceLocally(ByteArrayWrapper key, RedisDataType type) {
 +    Lock lock = this.locks.get(key.toString());
 +    boolean locked = false;
 +    try {
 +      locked = lock.tryLock();
 +      // If we cannot get the lock we ignore this remote event, this key has local event
 +      // that started independently, ignore this event to prevent deadlock
 +      if (locked) {
 +        cancelKeyExpiration(key);
 +        removeRegionState(key, type);
 +      }
 +    } finally {
 +      if (locked) {
 +        lock.unlock();
 +      }
 +    }
 +  }
 +
 +  public boolean removeKey(ByteArrayWrapper key) {
 +    RedisDataType type = getRedisDataType(key);
 +    return removeKey(key, type);
 +  }
 +
 +  public boolean removeKey(ByteArrayWrapper key, RedisDataType type) {
 +    return removeKey(key, type, true);
 +  }
 +
 +  public boolean removeKey(ByteArrayWrapper key, RedisDataType type, boolean cancelExpiration) {
 +    if (type == null || type == RedisDataType.REDIS_PROTECTED)
 +      return false;
 +    Lock lock = this.locks.get(key.toString());
 +    try {
 +      if (lock != null)  {// Strings/hlls will not have locks
 +        lock.lock();
 +      }
 +      metaRemoveEntry(key);
 +      try {
 +        if (type == RedisDataType.REDIS_STRING) {
 +          return this.stringsRegion.remove(key) != null;
 +        } else if (type == RedisDataType.REDIS_HLL) {
 +          return this.hLLRegion.remove(key) != null;
 +        } else {
 +          return destroyRegion(key, type);
 +        }
 +      } catch (Exception exc) {
 +        return false;
 +      } finally {
 +        if (cancelExpiration)
 +          cancelKeyExpiration(key);
 +        else
 +          removeKeyExpiration(key);
 +        if (lock != null)
 +          this.locks.remove(key.toString());
 +      }
 +    } finally {
 +      if (lock != null) {
 +        lock.unlock();
 +      }
 +    }
 +  }
 +
 +  public Region<?, ?> getOrCreateRegion(ByteArrayWrapper key, RedisDataType type, ExecutionHandlerContext context) {
 +    return getOrCreateRegion0(key, type, context, true);
 +  }
 +
 +  public void createRemoteRegionReferenceLocally(ByteArrayWrapper key, RedisDataType type) {
 +    if (type == null || type == RedisDataType.REDIS_STRING || type == RedisDataType.REDIS_HLL)
 +      return;
 +    Region<?, ?> r = this.regions.get(key);
 +    if (r != null)
 +      return;
 +    if (!this.regions.contains(key)) {
 +      String stringKey = key.toString();
 +      Lock lock = this.locks.get(stringKey);
 +      if (lock == null) {
 +        this.locks.putIfAbsent(stringKey, new ReentrantLock());
 +        lock = this.locks.get(stringKey);
 +      }
 +      boolean locked = false;
 +      try {
 +        locked = lock.tryLock();
 +        // If we cannot get the lock then this remote event may have been initialized
 +        // independently on this machine, so if we wait on the lock it is more than
 +        // likely we will deadlock just to do the same task. This event can be ignored
 +        if (locked) {
 +          r = cache.getRegion(key.toString());
 +          // If r is null, this implies that we are after a create/destroy
 +          // simply ignore. Calls to getRegion or getOrCreate will work correctly
 +          if (r == null)
 +            return;
 +
 +          if (type == RedisDataType.REDIS_LIST) {
 +            doInitializeList(key, r);
 +          } else if (type == RedisDataType.REDIS_SORTEDSET) {
 +            try {
 +              doInitializeSortedSet(key, r);
 +            } catch (RegionNotFoundException | IndexInvalidException e) {
 +              //ignore
 +            }
 +          }
 +          this.regions.put(key, r);
 +        }
 +      } finally {
 +        if (locked) {
 +          lock.unlock();
 +        }
 +      }
 +    }
 +  }
 +
 +  private Region<?, ?> getOrCreateRegion0(ByteArrayWrapper key, RedisDataType type, ExecutionHandlerContext context, boolean addToMeta) {
 +    checkDataType(key, type);
 +    Region<?, ?> r = this.regions.get(key);
 +    if (r != null && r.isDestroyed()) {
 +      removeKey(key, type);
 +      r = null;
 +    }
 +    if (r == null) {
 +      String stringKey = key.toString();
 +      Lock lock = this.locks.get(stringKey);
 +      if (lock == null) {
 +        this.locks.putIfAbsent(stringKey, new ReentrantLock());
 +        lock = this.locks.get(stringKey);
 +      }
 +
 +      try {
 +        lock.lock();
 +        r = regions.get(key);
 +        if (r == null) {
 +          boolean hasTransaction = context != null && context.hasTransaction(); // Can create without context
 +          CacheTransactionManager txm = null;
 +          TransactionId transactionId = null;
 +          try {
 +            if (hasTransaction) {
 +              txm = cache.getCacheTransactionManager();
 +              transactionId = txm.suspend();
 +            }
 +            Exception concurrentCreateDestroyException = null;
 +            do {
 +              concurrentCreateDestroyException = null;
 +              r = createRegionGlobally(stringKey);
 +              try {
 +                if (type == RedisDataType.REDIS_LIST) {
 +                  doInitializeList(key, r);
 +                } else if (type == RedisDataType.REDIS_SORTEDSET) {
 +                  try {
 +                    doInitializeSortedSet(key, r);
 +                  } catch (RegionNotFoundException | IndexInvalidException e) {
 +                    concurrentCreateDestroyException = e;
 +                  }
 +                }
 +              } catch (QueryInvalidException e) {
 +                if (e.getCause() instanceof RegionNotFoundException) {
 +                  concurrentCreateDestroyException = e;
 +                }
 +              }
 +            } while(concurrentCreateDestroyException != null);
 +            this.regions.put(key, r);            
 +            if (addToMeta) {
 +              RedisDataType existingType = metaPutIfAbsent(key, type);
 +              if (existingType != null && existingType != type)
 +                throw new RedisDataTypeMismatchException("The key name \"" + key + "\" is already used by a " + existingType.toString());
 +            }
 +          } finally {
 +            if (hasTransaction)
 +              txm.resume(transactionId);
 +          }
 +        }
 +      } finally {
 +        lock.unlock();
 +      }
 +    }
 +    return r;
 +  }
 +
 +  /**
 +   * SYNCHRONIZE EXTERNALLY OF this.locks.get(key.toString())!!!!!
 +   * 
 +   * @param key Key of region to destroy
 +   * @param type Type of region to destroyu
 +   * @return Flag if destroyed
 +   */
 +  private boolean destroyRegion(ByteArrayWrapper key, RedisDataType type) {
 +    Region<?, ?> r = this.regions.get(key);
 +    if (r != null) {
 +      try {
 +        r.destroyRegion();
 +      } catch (Exception e) {
 +        return false;
 +      } finally {
 +        removeRegionState(key, type);
 +      }
 +    }
 +    return true;
 +  }
 +
 +  /**
 +   * Do not call this method if you are not synchronized on the lock associated with this key
 +   * 
 +   * @param key Key of region to remove
 +   * @param type Type of key to remove all state
 +   */
 +  private void removeRegionState(ByteArrayWrapper key, RedisDataType type) {
 +    this.preparedQueries.remove(key);
 +    this.regions.remove(key);
 +  }
 +
 +  private void doInitializeSortedSet(ByteArrayWrapper key, Region<?, ?> r) throws RegionNotFoundException, IndexInvalidException {
 +    String fullpath = r.getFullPath();
 +    try {
 +      queryService.createIndex("scoreIndex", "entry.value.score", r.getFullPath() + ".entrySet entry");
 +      queryService.createIndex("scoreIndex2", "value.score", r.getFullPath() + ".values value");
 +    } catch (IndexNameConflictException | IndexExistsException | UnsupportedOperationException e) {
 +      // ignore, these indexes already exist or unsupported but make sure prepared queries are made
 +    }
 +    HashMap<Enum<?>, Query> queryList = new HashMap<Enum<?>, Query>();
 +    for (SortedSetQuery lq: SortedSetQuery.values()) {
 +      String queryString = lq.getQueryString(fullpath);
 +      Query query = this.queryService.newQuery(queryString);
 +      queryList.put(lq, query);
 +    }
 +    this.preparedQueries.put(key, queryList);
 +  }
 +
 +  private void doInitializeList(ByteArrayWrapper key, Region r) {
 +    r.put("head", Integer.valueOf(0));
 +    r.put("tail", Integer.valueOf(0));
 +    String fullpath = r.getFullPath();
 +    HashMap<Enum<?>, Query> queryList = new HashMap<Enum<?>, Query>();
 +    for (ListQuery lq: ListQuery.values()) {
 +      String queryString = lq.getQueryString(fullpath);
 +      Query query = this.queryService.newQuery(queryString);
 +      queryList.put(lq, query);
 +    }
 +    this.preparedQueries.put(key, queryList);
 +  }
 +
 +  /**
 +   * This method creates a Region globally with the given name. If
 +   * there is an error in the creation, a runtime exception will
 +   * be thrown.
 +   * 
 +   * @param key Name of Region to create
 +   * @return Region Region created globally
 +   */
 +  private Region<?, ?> createRegionGlobally(String key) {
 +    Region<?, ?> r = null;
 +    r = cache.getRegion(key);
 +    if (r != null) return r;
 +    do {
 +      Result result = cliCmds.createRegion(key, defaultRegionType, null, null, true, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
 +      r = cache.getRegion(key);
 +      if (result.getStatus() == Status.ERROR && r == null) {
 +        String err = "";
 +        while(result.hasNextLine())
 +          err += result.nextLine();
 +        throw new RegionCreationException(err);
 +      }
 +    } while(r == null); // The region can be null in the case that it is concurrently destroyed by
 +    // a remote even triggered internally by Geode
 +    return r;
 +  }
 +
 +  public Query getQuery(ByteArrayWrapper key, Enum<?> query) {
 +    return this.preparedQueries.get(key).get(query);
 +    /*
 +    if (query instanceof ListQuery) {
 +      return this.queryService.newQuery(((ListQuery)query).getQueryString(this.regions.get(key).getFullPath()));
 +    } else {
 +      return this.queryService.newQuery(((SortedSetQuery)query).getQueryString(this.regions.get(key).getFullPath()));
 +    }
 +     */
 +  }
 +
 +  /**
 +   * Checks if the given key is associated with the passed data type.
 +   * If there is a mismatch, a {@link RuntimeException} is thrown
 +   * 
 +   * @param key Key to check
 +   * @param type Type to check to
 +   */
 +  protected void checkDataType(ByteArrayWrapper key, RedisDataType type) {
 +    RedisDataType currentType = redisMetaRegion.get(key.toString());
 +    if (currentType == null)
 +      return;
 +    if (currentType == RedisDataType.REDIS_PROTECTED)
 +      throw new RedisDataTypeMismatchException("The key name \"" + key + "\" is protected");
 +    if (currentType != type)
 +      throw new RedisDataTypeMismatchException("The key name \"" + key + "\" is already used by a " + currentType.toString());
 +  }
 +
 +  public boolean regionExists(ByteArrayWrapper key) {
 +    return this.regions.containsKey(key);
 +  }
 +
 +  public Region<ByteArrayWrapper, ByteArrayWrapper> getStringsRegion() {
 +    return this.stringsRegion;
 +  }
 +
 +  public Region<ByteArrayWrapper, HyperLogLogPlus> gethLLRegion() {
 +    return this.hLLRegion;
 +  }
 +
 +  private RedisDataType getRedisDataType(String key) {
 +    return this.redisMetaRegion.get(key);
 +  }
 +
 +  public RedisDataType getRedisDataType(ByteArrayWrapper key) {
 +    return getRedisDataType(key.toString());
 +  }
 +
 +  /**
 +   * Sets the expiration for a key. The setting and modifying of a key expiration can only be set by a delay,
 +   * which means that both expiring after a time and at a time can be done but
 +   * the delay to expire at a time must be calculated before these calls. It is
 +   * also important to note that the delay is always handled in milliseconds
 +   * 
 +   * @param key The key to set the expiration for
 +   * @param delay The delay in milliseconds of the expiration
 +   * @return True is expiration set, false otherwise
 +   */
 +  public final boolean setExpiration(ByteArrayWrapper key, long delay) {
 +    RedisDataType type = getRedisDataType(key);
 +    if (type == null)
 +      return false;
 +    ScheduledFuture<?> future = this.expirationExecutor.schedule(new ExpirationExecutor(key, type, this), delay, TimeUnit.MILLISECONDS);
 +    this.expirationsMap.put(key, future);
 +    return true;
 +  }
 +
 +  /**
 +   * Modifies an expiration on a key
 +   * 
 +   * @param key String key to modify expiration on
 +   * @param delay Delay in milliseconds to reset the expiration to
 +   * @return True if reset, false if not
 +   */
 +  public final boolean modifyExpiration(ByteArrayWrapper key, long delay) {
 +    /*
 +     * Attempt to cancel future task
 +     */
 +    boolean canceled = cancelKeyExpiration(key);
 +
 +    if (!canceled)
 +      return false;
 +
 +    RedisDataType type = getRedisDataType(key);
 +    if (type == null)
 +      return false;
 +
 +    ScheduledFuture<?> future = this.expirationExecutor.schedule(new ExpirationExecutor(key, type, this), delay, TimeUnit.MILLISECONDS);
 +    this.expirationsMap.put(key, future);
 +    return true;
 +  }
 +
 +  /**
 +   * Removes an expiration from a key
 +   * 
 +   * @param key Key
 +   * @return True is expiration cancelled on the key, false otherwise
 +   */
 +  public final boolean cancelKeyExpiration(ByteArrayWrapper key) {
 +    ScheduledFuture<?> future = expirationsMap.remove(key);
 +    if (future == null)
 +      return false;
 +    return future.cancel(false);
 +  }
 +
 +  private boolean removeKeyExpiration(ByteArrayWrapper key) {
 +    return expirationsMap.remove(key) != null;
 +  }
 +
 +  /**
 +   * Check method if key has expiration
 +   * 
 +   * @param key Key
 +   * @return True if key has expiration, false otherwise
 +   */
 +  public boolean hasExpiration(ByteArrayWrapper key) {
 +    return this.expirationsMap.containsKey(key);
 +  }
 +
 +  /**
 +   * Get remaining expiration time
 +   * 
 +   * @param key Key
 +   * @return Remaining time in milliseconds or 0 if no delay or key doesn't exist
 +   */
 +  public final long getExpirationDelayMillis(ByteArrayWrapper key) {
 +    ScheduledFuture<?> future = this.expirationsMap.get(key);
 +    return future != null ? future.getDelay(TimeUnit.MILLISECONDS) : 0L;
 +  }
 +
 +  @Override
 +  public void close() {
 +    this.preparedQueries.clear();
 +  }
 +
 +  public String dumpRegionsCache() {
 +    StringBuilder builder = new StringBuilder();
 +    for (Entry<ByteArrayWrapper, Region<?, ?>> e : this.regions.entrySet()) {
 +      builder.append(e.getKey() + " --> {" + e.getValue() + "}\n");
 +    }
 +    return builder.toString();
 +  }
 +
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/redis/executor/hll/PFAddExecutor.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/redis/executor/hll/PFAddExecutor.java
index 95af34a,0000000..150078a
mode 100755,000000..100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/redis/executor/hll/PFAddExecutor.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/redis/executor/hll/PFAddExecutor.java
@@@ -1,65 -1,0 +1,66 @@@
 +/*
 + * 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.redis.executor.hll;
 +
 +import java.util.List;
 +
 +import com.gemstone.gemfire.cache.Region;
++import com.gemstone.gemfire.internal.hll.HyperLogLogPlus;
 +import com.gemstone.gemfire.internal.redis.ByteArrayWrapper;
 +import com.gemstone.gemfire.internal.redis.Command;
 +import com.gemstone.gemfire.internal.redis.Coder;
 +import com.gemstone.gemfire.internal.redis.ExecutionHandlerContext;
 +import com.gemstone.gemfire.internal.redis.RedisConstants.ArityDef;
 +
 +public class PFAddExecutor extends HllExecutor {
 +
 +  @Override
 +  public void executeCommand(Command command, ExecutionHandlerContext context) {
 +    List<byte[]> commandElems = command.getProcessedCommand();
 +
 +    if (commandElems.size() < 2) {
 +      command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.PFADD));
 +      return;
 +    }
 +
 +    ByteArrayWrapper key = command.getKey();
 +    checkAndSetDataType(key, context);
 +    Region<ByteArrayWrapper, HyperLogLogPlus> keyRegion = context.getRegionProvider().gethLLRegion();
 +
 +    HyperLogLogPlus hll = keyRegion.get(key);
 +
 +    boolean changed = false;
 +
 +    if (hll == null)
 +      hll = new HyperLogLogPlus(DEFAULT_HLL_DENSE);
 +
 +    for (int i = 2; i < commandElems.size(); i++) {
 +      byte[] bytes = commandElems.get(i);
 +      boolean offerChange = hll.offer(bytes);
 +      if (offerChange)
 +        changed = true;
 +    }
 +
 +    keyRegion.put(key, hll);
 +
 +    if (changed)
 +      command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), 1));
 +    else
 +      command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), 0));
 +  }
 +
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/redis/executor/hll/PFCountExecutor.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/redis/executor/hll/PFCountExecutor.java
index e081022,0000000..625a934
mode 100755,000000..100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/redis/executor/hll/PFCountExecutor.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/redis/executor/hll/PFCountExecutor.java
@@@ -1,68 -1,0 +1,70 @@@
 +/*
 + * 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.redis.executor.hll;
 +
 +import java.util.ArrayList;
 +import java.util.List;
 +
 +import com.gemstone.gemfire.cache.Region;
++import com.gemstone.gemfire.internal.hll.CardinalityMergeException;
++import com.gemstone.gemfire.internal.hll.HyperLogLogPlus;
 +import com.gemstone.gemfire.internal.redis.ByteArrayWrapper;
 +import com.gemstone.gemfire.internal.redis.Coder;
 +import com.gemstone.gemfire.internal.redis.Command;
 +import com.gemstone.gemfire.internal.redis.ExecutionHandlerContext;
 +import com.gemstone.gemfire.internal.redis.RedisConstants.ArityDef;
 +import com.gemstone.gemfire.internal.redis.RedisDataType;
 +
 +public class PFCountExecutor extends HllExecutor {
 +
 +  @Override
 +  public void executeCommand(Command command, ExecutionHandlerContext context) {
 +    List<byte[]> commandElems = command.getProcessedCommand();
 +
 +    if (commandElems.size() < 2) {
 +      command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.PFCOUNT));
 +      return;
 +    }
 +
 +    Region<ByteArrayWrapper, HyperLogLogPlus> keyRegion = context.getRegionProvider().gethLLRegion();
 +
 +    List<HyperLogLogPlus> hlls = new ArrayList<HyperLogLogPlus>();
 +
 +    for (int i = 1; i < commandElems.size(); i++) {
 +      ByteArrayWrapper k = new ByteArrayWrapper(commandElems.get(i));
 +      checkDataType(k, RedisDataType.REDIS_HLL, context);
 +      HyperLogLogPlus h = keyRegion.get(k);
 +      if (h != null)
 +        hlls.add(h);
 +    }
 +    if (hlls.isEmpty()) {
 +      command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), 0));
 +      return;
 +    }
 +
 +    HyperLogLogPlus tmp = hlls.remove(0);
 +    HyperLogLogPlus[] estimators = hlls.toArray(new HyperLogLogPlus[hlls.size()]);
 +    try {
 +      tmp = (HyperLogLogPlus) tmp.merge(estimators);
 +    } catch (CardinalityMergeException e) {
 +      throw new RuntimeException(e);
 +    }
 +    long cardinality = tmp.cardinality();
 +    command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), cardinality));
 +  }
 +
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/redis/executor/hll/PFMergeExecutor.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/redis/executor/hll/PFMergeExecutor.java
index dd9afbc,0000000..f059cc2
mode 100755,000000..100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/redis/executor/hll/PFMergeExecutor.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/redis/executor/hll/PFMergeExecutor.java
@@@ -1,72 -1,0 +1,74 @@@
 +/*
 + * 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.redis.executor.hll;
 +
 +import java.util.ArrayList;
 +import java.util.List;
 +
 +import com.gemstone.gemfire.cache.Region;
++import com.gemstone.gemfire.internal.hll.CardinalityMergeException;
++import com.gemstone.gemfire.internal.hll.HyperLogLogPlus;
 +import com.gemstone.gemfire.internal.redis.ByteArrayWrapper;
 +import com.gemstone.gemfire.internal.redis.Coder;
 +import com.gemstone.gemfire.internal.redis.Command;
 +import com.gemstone.gemfire.internal.redis.ExecutionHandlerContext;
 +import com.gemstone.gemfire.internal.redis.RedisDataType;
 +import com.gemstone.gemfire.internal.redis.RedisConstants.ArityDef;
 +
 +public class PFMergeExecutor extends HllExecutor {
 +
 +  @Override
 +  public void executeCommand(Command command, ExecutionHandlerContext context) {
 +    List<byte[]> commandElems = command.getProcessedCommand();
 +
 +    if (commandElems.size() < 3) {
 +      command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.PFMERGE));
 +      return;
 +    }
 +
 +    ByteArrayWrapper destKey = command.getKey();
 +    checkAndSetDataType(destKey, context);
 +    Region<ByteArrayWrapper, HyperLogLogPlus> keyRegion = context.getRegionProvider().gethLLRegion();
 +    HyperLogLogPlus mergedHLL = keyRegion.get(destKey);
 +    if (mergedHLL == null)
 +      mergedHLL = new HyperLogLogPlus(DEFAULT_HLL_DENSE);
 +    List<HyperLogLogPlus> hlls = new ArrayList<HyperLogLogPlus>();
 +
 +    for (int i = 2; i < commandElems.size(); i++) {
 +      ByteArrayWrapper k = new ByteArrayWrapper(commandElems.get(i));
 +      checkDataType(k, RedisDataType.REDIS_HLL, context);
 +      HyperLogLogPlus h = keyRegion.get(k);
 +      if (h != null)
 +        hlls.add(h);
 +    }
 +    if (hlls.isEmpty()) {
 +      context.getRegionProvider().removeKey(destKey);
 +      command.setResponse(Coder.getSimpleStringResponse(context.getByteBufAllocator(), "OK"));
 +      return;
 +    }
 +
 +    HyperLogLogPlus[] estimators = hlls.toArray(new HyperLogLogPlus[hlls.size()]);
 +    try {
 +      mergedHLL = (HyperLogLogPlus) mergedHLL.merge(estimators);
 +    } catch (CardinalityMergeException e) {
 +      throw new RuntimeException(e);
 +    }
 +    keyRegion.put(destKey, mergedHLL);
 +    command.setResponse(Coder.getSimpleStringResponse(context.getByteBufAllocator(), "OK"));
 +  }
 +
 +}



[33/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/DistributedRegion.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/cache/DistributedRegion.java
index 92b585a,0000000..f3e730a
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/DistributedRegion.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/DistributedRegion.java
@@@ -1,4303 -1,0 +1,4311 @@@
 +/*
 + * 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;
 +
 +import static com.gemstone.gemfire.internal.offheap.annotations.OffHeapIdentifier.ABSTRACT_REGION_ENTRY_FILL_IN_VALUE;
 +
 +import java.io.IOException;
 +import java.io.InputStream;
 +import java.util.ArrayList;
 +import java.util.Collection;
 +import java.util.Collections;
 +import java.util.HashMap;
 +import java.util.HashSet;
 +import java.util.Iterator;
 +import java.util.List;
 +import java.util.Map;
 +import java.util.Set;
 +import java.util.concurrent.CopyOnWriteArraySet;
 +import java.util.concurrent.RejectedExecutionException;
 +import java.util.concurrent.TimeUnit;
 +import java.util.concurrent.atomic.AtomicBoolean;
 +import java.util.concurrent.locks.Condition;
 +import java.util.concurrent.locks.Lock;
 +
 +import org.apache.logging.log4j.Logger;
 +
 +import com.gemstone.gemfire.CancelException;
 +import com.gemstone.gemfire.InternalGemFireError;
 +import com.gemstone.gemfire.InvalidDeltaException;
 +import com.gemstone.gemfire.SystemFailure;
 +import com.gemstone.gemfire.cache.CacheClosedException;
 +import com.gemstone.gemfire.cache.CacheListener;
 +import com.gemstone.gemfire.cache.CacheLoader;
 +import com.gemstone.gemfire.cache.CacheLoaderException;
 +import com.gemstone.gemfire.cache.CacheWriter;
 +import com.gemstone.gemfire.cache.CacheWriterException;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.DiskAccessException;
 +import com.gemstone.gemfire.cache.EntryNotFoundException;
 +import com.gemstone.gemfire.cache.LossAction;
 +import com.gemstone.gemfire.cache.MembershipAttributes;
 +import com.gemstone.gemfire.cache.Operation;
 +import com.gemstone.gemfire.cache.RegionAccessException;
 +import com.gemstone.gemfire.cache.RegionAttributes;
 +import com.gemstone.gemfire.cache.RegionDestroyedException;
 +import com.gemstone.gemfire.cache.RegionDistributionException;
 +import com.gemstone.gemfire.cache.RegionMembershipListener;
 +import com.gemstone.gemfire.cache.ResumptionAction;
 +import com.gemstone.gemfire.cache.RoleException;
 +import com.gemstone.gemfire.cache.TimeoutException;
 +import com.gemstone.gemfire.cache.TransactionId;
 +import com.gemstone.gemfire.cache.asyncqueue.internal.AsyncEventQueueImpl;
 +import com.gemstone.gemfire.cache.execute.Function;
 +import com.gemstone.gemfire.cache.execute.FunctionException;
 +import com.gemstone.gemfire.cache.execute.ResultCollector;
 +import com.gemstone.gemfire.cache.execute.ResultSender;
 +import com.gemstone.gemfire.cache.persistence.PersistentReplicatesOfflineException;
 +import com.gemstone.gemfire.cache.query.internal.IndexUpdater;
 +import com.gemstone.gemfire.cache.wan.GatewaySender;
 +import com.gemstone.gemfire.distributed.DistributedLockService;
 +import com.gemstone.gemfire.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.LockServiceDestroyedException;
 +import com.gemstone.gemfire.distributed.Role;
 +import com.gemstone.gemfire.distributed.internal.DM;
 +import com.gemstone.gemfire.distributed.internal.DistributionAdvisee;
 +import com.gemstone.gemfire.distributed.internal.DistributionAdvisor;
 +import com.gemstone.gemfire.distributed.internal.DistributionAdvisor.Profile;
 +import com.gemstone.gemfire.distributed.internal.DistributionAdvisor.ProfileVisitor;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 +import com.gemstone.gemfire.distributed.internal.MembershipListener;
 +import com.gemstone.gemfire.distributed.internal.ReplyProcessor21;
 +import com.gemstone.gemfire.distributed.internal.locks.DLockRemoteToken;
 +import com.gemstone.gemfire.distributed.internal.locks.DLockService;
 +import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
 +import com.gemstone.gemfire.internal.Assert;
 +import com.gemstone.gemfire.internal.cache.CacheDistributionAdvisor.CacheProfile;
 +import com.gemstone.gemfire.internal.cache.InitialImageOperation.GIIStatus;
 +import com.gemstone.gemfire.internal.cache.RemoteFetchVersionMessage.FetchVersionResponse;
 +import com.gemstone.gemfire.internal.cache.control.InternalResourceManager.ResourceType;
 +import com.gemstone.gemfire.internal.cache.control.MemoryEvent;
 +import com.gemstone.gemfire.internal.cache.execute.DistributedRegionFunctionExecutor;
 +import com.gemstone.gemfire.internal.cache.execute.DistributedRegionFunctionResultSender;
 +import com.gemstone.gemfire.internal.cache.execute.DistributedRegionFunctionResultWaiter;
 +import com.gemstone.gemfire.internal.cache.execute.FunctionStats;
 +import com.gemstone.gemfire.internal.cache.execute.LocalResultCollector;
 +import com.gemstone.gemfire.internal.cache.execute.RegionFunctionContextImpl;
 +import com.gemstone.gemfire.internal.cache.execute.ServerToClientFunctionResultSender;
 +import com.gemstone.gemfire.internal.cache.lru.LRUEntry;
 +import com.gemstone.gemfire.internal.cache.persistence.CreatePersistentRegionProcessor;
 +import com.gemstone.gemfire.internal.cache.persistence.PersistenceAdvisor;
 +import com.gemstone.gemfire.internal.cache.persistence.PersistenceAdvisorImpl;
 +import com.gemstone.gemfire.internal.cache.persistence.PersistentMemberID;
 +import com.gemstone.gemfire.internal.cache.persistence.PersistentMemberManager;
 +import com.gemstone.gemfire.internal.cache.persistence.PersistentMemberView;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.ClientProxyMembershipID;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.VersionedObjectList;
 +import com.gemstone.gemfire.internal.cache.versions.ConcurrentCacheModificationException;
 +import com.gemstone.gemfire.internal.cache.versions.RegionVersionVector;
 +import com.gemstone.gemfire.internal.cache.versions.VersionSource;
 +import com.gemstone.gemfire.internal.cache.versions.VersionTag;
 +import com.gemstone.gemfire.internal.cache.wan.AbstractGatewaySender;
 +import com.gemstone.gemfire.internal.cache.wan.AbstractGatewaySenderEventProcessor;
 +import com.gemstone.gemfire.internal.cache.wan.AsyncEventQueueConfigurationException;
 +import com.gemstone.gemfire.internal.cache.wan.GatewaySenderConfigurationException;
 +import com.gemstone.gemfire.internal.cache.wan.parallel.ConcurrentParallelGatewaySenderQueue;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.logging.LogService;
 +import com.gemstone.gemfire.internal.logging.log4j.LocalizedMessage;
 +import com.gemstone.gemfire.internal.offheap.Chunk;
 +import com.gemstone.gemfire.internal.offheap.OffHeapHelper;
 +import com.gemstone.gemfire.internal.offheap.annotations.Released;
 +import com.gemstone.gemfire.internal.offheap.annotations.Retained;
 +import com.gemstone.gemfire.internal.sequencelog.RegionLogger;
 +import com.gemstone.gemfire.internal.util.concurrent.StoppableCountDownLatch;
 +import com.gemstone.gemfire.i18n.StringId;
 +/**
 + * 
 + * @author Eric Zoerner
 + * @author Sudhir Menon
 + */
 +@SuppressWarnings("deprecation")
 +public class DistributedRegion extends LocalRegion implements 
 +    CacheDistributionAdvisee
 +{
 +  private static final Logger logger = LogService.getLogger();
 +  
 +  /** causes cache profile to be added to afterRemoteRegionCreate notification for testing */
 +  public static boolean TEST_HOOK_ADD_PROFILE = false;
 +  
 +  /** Used to sync accesses to this.dlockService to allow lazy construction */
 +  private final Object dlockMonitor = new Object();
 +
 +  final CacheDistributionAdvisor distAdvisor;
 +
 +  /**
 +   * @guarded.By {@link #dlockMonitor}
 +   */
 +  private DistributedLockService dlockService;
 +
 +  protected final AdvisorListener advisorListener = new AdvisorListener();
 +
 +  /** Set of currently missing required roles */
 +  protected final HashSet missingRequiredRoles = new HashSet();
 +
 +  /** True if this region is currently missing any required roles */
 +  protected volatile boolean isMissingRequiredRoles = false;
 +  
 +  /**
 +   * True if this region is has any required roles defined and the LossAction is
 +   * either NO_ACCESS or LIMITED_ACCESS. Reliability checks will only happen if
 +   * this is true.
 +   */
 +  private final boolean requiresReliabilityCheck;
 +
 +  /**
 +   * Provides a queue for reliable message delivery
 +   * 
 +   * @since 5.0
 +   */
 +  protected final ReliableMessageQueue rmq;
 +
 +  /**
 +   * Latch that is opened after initialization waits for required roles up to
 +   * the <a href="DistributedSystem#member-timeout">member-timeout </a>.
 +   */
 +  protected final StoppableCountDownLatch initializationLatchAfterMemberTimeout; 
 +
 +  private final PersistenceAdvisor persistenceAdvisor;
 +  
 +  private final PersistentMemberID persistentId;
 +
 +  /**
 +   * This boolean is set to false when this region
 +   * is non-persistent, but there are persistent members in the distributed system
 +   * to which all region modifications should be forwarded
 +   * see bug 45186
 +   */
 +  private volatile boolean generateVersionTag = true;
 +
 +  /** Tests can set this to true and ignore reliability triggered reconnects */
 +  public static boolean ignoreReconnect = false;
 +  
 +  /**
 +   * Lock to prevent multiple threads on this member from performing
 +   * a clear at the same time.
 +   */
 +  private final Object clearLock = new Object();
 +
 +  private static AtomicBoolean loggedNetworkPartitionWarning = new AtomicBoolean(false);
 +
 +  /** Creates a new instance of DistributedRegion */
 +  protected DistributedRegion(String regionName, RegionAttributes attrs,
 +      LocalRegion parentRegion, GemFireCacheImpl cache,
 +      InternalRegionArguments internalRegionArgs) {
 +    super(regionName, attrs, parentRegion, cache, internalRegionArgs);
 +    this.initializationLatchAfterMemberTimeout = new StoppableCountDownLatch(
 +        getCancelCriterion(), 1);
 +    this.distAdvisor = createDistributionAdvisor(internalRegionArgs);
 +    
 +    if (getDistributionManager().getConfig().getEnableNetworkPartitionDetection()
 +        && !isInternalRegion() && !attrs.getScope().isAck() && !doesNotDistribute() && attrs.getDataPolicy().withStorage()) {
 +      logger.warn(LocalizedMessage.create(LocalizedStrings.DistributedRegion_REGION_0_1_SPLITBRAIN_CONFIG_WARNING,
 +          new Object[] { regionName, attrs.getScope() })); 
 +    }
 +    if (!getDistributionManager().getConfig().getEnableNetworkPartitionDetection() 
 +        && attrs.getDataPolicy().withPersistence() && !loggedNetworkPartitionWarning.getAndSet(true)) {
 +      logger.warn(LocalizedMessage.create(
 +          LocalizedStrings.DistributedRegion_REGION_0_ENABLE_NETWORK_PARTITION_WARNING,
 +          new Object[] { regionName, attrs.getScope() }));
 +    }
 +
 +    boolean setRequiresReliabilityCheck = attrs.getMembershipAttributes()
 +        .hasRequiredRoles()
 +        &&
 +        // note that the following includes NO_ACCESS, LIMITED_ACCESS,
 +        !attrs.getMembershipAttributes().getLossAction().isAllAccess()
 +        && !attrs.getMembershipAttributes().getLossAction().isReconnect();
 +
 +    // this optimization is safe for as long as Roles and Required Roles are
 +    // immutable
 +    // if this VM fulfills all required roles, make requiresReliabilityCheck
 +    // false
 +    Set reqRoles = new HashSet(attrs.getMembershipAttributes()
 +        .getRequiredRoles());
 +    reqRoles.removeAll(getSystem().getDistributedMember().getRoles());
 +    if (reqRoles.isEmpty()) {
 +      setRequiresReliabilityCheck = false;
 +    }
 +
 +    this.requiresReliabilityCheck = setRequiresReliabilityCheck;
 +
 +    {
 +      ReliableMessageQueue tmp = null;
 +      if (this.requiresReliabilityCheck) {
 +        //         if
 +        // (attrs.getMembershipAttributes().getLossAction().isAllAccessWithQueuing())
 +        // {
 +        //           tmp = cache.getReliableMessageQueueFactory().create(this);
 +        //         }
 +      }
 +      this.rmq = tmp;
 +    }
 +
 +    if(internalRegionArgs.isUsedForPartitionedRegionBucket()) {
 +      this.persistenceAdvisor = internalRegionArgs.getPersistenceAdvisor();
 +    } else if (this.allowsPersistence()){
 +      //TODO prpersist - using this lock service is a hack. Maybe? Or maybe
 +      //it's ok if we have one (rarely used) lock service for many operations?
 +      //What does the resource manager do?
 +      DistributedLockService dl = cache.getPartitionedRegionLockService();
 +      try {
 +        //TODO prpersist - this is just a quick and dirty storage mechanism so that
 +        //I can test the storage.
 +        DiskRegionStats diskStats;
 +        PersistentMemberView storage;
 +        if(getDataPolicy().withPersistence()) {
 +          storage = getDiskRegion();
 +          diskStats = getDiskRegion().getStats();
 +        } else {
 +          storage = new InMemoryPersistentMemberView();
 +          diskStats = null;
 +        }
 +        PersistentMemberManager memberManager = cache.getPersistentMemberManager();
 +        this.persistenceAdvisor = new PersistenceAdvisorImpl(distAdvisor, dl, storage, this.getFullPath(), diskStats, memberManager);
 +      } catch (Exception e) {
 +        throw new InternalGemFireError("Couldn't recover persistence");
 +      }
 +    } else {
 +      this.persistenceAdvisor = null;
 +    }
 +    if(this.persistenceAdvisor != null) {
 +      this.persistentId = persistenceAdvisor.generatePersistentID();
 +    } else {
 +      this.persistentId = null;
 +    }
 +    
 +  }
 +  
 +  @Override
 +  public void createEventTracker() {
 +    this.eventTracker = new EventTracker(this);
 +    this.eventTracker.start();
 +  }
 +  
 +  /**
 +   * Intended for used during construction of a DistributedRegion
 +   *  
 +   * @return the advisor to be used by the region
 +   */
 +  protected CacheDistributionAdvisor createDistributionAdvisor(InternalRegionArguments internalRegionArgs) {
 +    return CacheDistributionAdvisor.createCacheDistributionAdvisor(this);  // Warning: potential early escape of object before full construction
 +  }
 +  
 +  /**
 +   * Does this region support persistence?
 +   */
 +  public boolean allowsPersistence() {
 +    return true;
 +  }
 +
 +  @Override
 +  public boolean requiresOneHopForMissingEntry(EntryEventImpl event) {
 +    // received from another member - don't use one-hop
 +    if (event.isOriginRemote()) {
 +      return false; 
 +    }
 +    // local ops aren't distributed
 +    if (event.getOperation().isLocal()) {
 +      return false;
 +    }
 +    // if it already has a valid version tag it can go out with a DistributedCacheOperation
 +    if (event.getVersionTag() != null && event.getVersionTag().getRegionVersion() > 0) {
 +      return false;
 +    }
 +    // if we're not allowed to generate a version tag we need to send it to someone who can
 +    if (!this.generateVersionTag) {
 +      return true;
 +    }
 +    return this.concurrencyChecksEnabled &&
 +        (this.srp == null) &&
 +        !isTX() &&
 +        this.scope.isDistributed() &&
 +        !this.dataPolicy.withReplication();
 +  }
 +  
 +  
 +  /**
 +   * @see LocalRegion#virtualPut(EntryEventImpl, boolean, boolean, Object, 
 +   * boolean, long, boolean)
 +   */
 +  @Override
 +  protected
 +  boolean virtualPut(EntryEventImpl event,
 +                     boolean ifNew,
 +                     boolean ifOld,
 +                     Object expectedOldValue,
 +                     boolean requireOldValue,
 +                     long lastModified,
 +                     boolean overwriteDestroyed)
 +  throws TimeoutException,
 +         CacheWriterException {
 +    final boolean isTraceEnabled = logger.isTraceEnabled();
 +    
 +    Lock dlock = null;
 +    if (this.scope.isGlobal() && // lock only applies to global scope
 +        !event.isOriginRemote() && // only if operation originating locally
 +        !event.isNetSearch() && // search and load processor handles own locking
 +        !event.isNetLoad() &&
 +        // @todo darrel/kirk: what about putAll?
 +        !event.isLocalLoad() &&
 +        !event.isSingleHopPutOp()) { // Single Hop Op means dlock is already taken at origin node.
 +      dlock = this.getDistributedLockIfGlobal(event.getKey());
 +    }
 +    if (isTraceEnabled) {
 +      logger.trace("virtualPut invoked for event {}", event);
 +    }
 +    try {
 +      if (!hasSeenEvent(event)) {
 +        if (this.requiresOneHopForMissingEntry(event)) {
 +          // bug #45704: see if a one-hop must be done for this operation
 +          RegionEntry re = getRegionEntry(event.getKey());
 +          if (re == null /*|| re.isTombstone()*/ || !this.generateVersionTag) {
 +            if (!event.isBulkOpInProgress() || this.dataPolicy.withStorage()) {
 +              // putAll will send a single one-hop for empty regions.  for other missing entries
 +              // we need to get a valid version number before modifying the local cache 
 +              boolean didDistribute = RemotePutMessage.distribute(event, lastModified,
 +                  false, false, expectedOldValue, requireOldValue, !this.generateVersionTag);
 +
 +              if (!didDistribute && isTraceEnabled) {
 +                logger.trace("Unable to perform one-hop messaging");
 +              }
 +              if (!this.generateVersionTag && !didDistribute) {
 +                throw new PersistentReplicatesOfflineException();
 +              }
 +              if (didDistribute) {
 +                if (isTraceEnabled) {
 +                  logger.trace("Event after remotePut operation: {}", event);
 +                }
 +                if (event.getVersionTag() == null) {
 +                  // if the event wasn't applied by the one-hop replicate it will not have a version tag
 +                  // and so should not be applied to this cache
 +                  return false;
 +                }
 +              }
 +            }
 +          }
 +        }
 +        return super.virtualPut(event,
 +                                ifNew,
 +                                ifOld,
 +                                expectedOldValue,
 +                                requireOldValue,
 +                                lastModified,
 +                                overwriteDestroyed);
 +      }
 +      else {
 +        if (event.getDeltaBytes() != null && event.getRawNewValue() == null) {
 +          // This means that this event has delta bytes but no full value.
 +          // Request the full value of this event.
 +          // The value in this vm may not be same as this event's value.
 +          throw new InvalidDeltaException(
 +              "Cache encountered replay of event containing delta bytes for key "
 +                  + event.getKey());
 +        }
 +        // if the listeners have already seen this event, then it has already
 +        // been successfully applied to the cache.  Distributed messages and
 +        // return
 +        if (isTraceEnabled) {
 +          logger.trace("DR.virtualPut: this cache has already seen this event {}", event);
 +        }
 +        
 +        // Gester, Fix 39014: when hasSeenEvent, put will still distribute
 +        // event, but putAll did not. We add the logic back here, not to put 
 +        // back into DR.distributeUpdate() because we moved this part up into 
 +        // LR.basicPutPart3 in purpose. Reviewed by Bruce.  
 +        if (event.isBulkOpInProgress() && !event.isOriginRemote()) {
 +          event.getPutAllOperation().addEntry(event, true);
 +        }
 +
 +        /* doing this so that other VMs will apply this no matter what. If it 
 +         * is an "update" they will not apply it if they don't have the key.
 +         * Because this is probably a retry, it will never get applied to this 
 +         * local AbstractRegionMap, and so will never be flipped to a 'create'
 +         */
 +        event.makeCreate();
-         distributeUpdate(event, lastModified, ifNew, ifOld, expectedOldValue, requireOldValue);
-         event.invokeCallbacks(this,true, true);
++        if (!getConcurrencyChecksEnabled() || event.hasValidVersionTag()) {
++          distributeUpdate(event, lastModified, ifNew, ifOld, expectedOldValue, requireOldValue);
++          event.invokeCallbacks(this,true, true);
++        }
 +        return true;
 +      }
 +    }
 +    finally {
 +      if (dlock != null) {
 +        dlock.unlock();
 +      }
 +    }
 +  }
 +  
 +  @Override
 +  protected RegionEntry basicPutEntry(EntryEventImpl event, long lastModified)
 +      throws TimeoutException, CacheWriterException {
 +    
 +    final boolean isTraceEnabled = logger.isTraceEnabled();
 +    
 +    if (isTraceEnabled) {
 +      logger.trace("basicPutEntry invoked for event {}", event);
 +    }
 +    if (this.requiresOneHopForMissingEntry(event)) {
 +      // bug #45704: see if a one-hop must be done for this operation
 +      RegionEntry re = getRegionEntry(event.getKey());
 +      if (re == null /*|| re.isTombstone()*/ || !this.generateVersionTag) {
 +        final boolean ifNew = false;
 +        final boolean ifOld = false;
 +        boolean didDistribute = RemotePutMessage.distribute(event, lastModified,
 +            ifNew, ifOld, null, false, !this.generateVersionTag);
 +        if (!this.generateVersionTag && !didDistribute) {
 +          throw new PersistentReplicatesOfflineException();
 +        }
 +        if (didDistribute && isTraceEnabled) {
 +          logger.trace("Event after remotePut for basicPutEntry: {}", event);
 +        }
 +      }
 +    }
 +    return super.basicPutEntry(event, lastModified);
 +  }
 +
 +  @Override
 +  public void performPutAllEntry(EntryEventImpl event) {
 +	  /*
 +	   * force shared data view so that we just do the virtual op, accruing things in the put all operation for later
 +	   */
 +	if(isTX()) {
 +		event.getPutAllOperation().addEntry(event);
 +	} else {
 +		getSharedDataView().putEntry(event, false, false, null, false, 0L, false);
 +	}
 +  }
 +  
 +  @Override
 +  public void performRemoveAllEntry(EntryEventImpl event) {
 +    // force shared data view so that we just do the virtual op, accruing things in the bulk operation for later
 +    if(isTX()) {
 +      event.getRemoveAllOperation().addEntry(event);
 +    } else {
 +      basicDestroy(event, true, null);
 +      //getSharedDataView().destroyExistingEntry(event, true, null);
 +    }
 +  }
 +  
 +  /**
 +   * distribution and listener notification
 +   */
 +  @Override
 +  public void basicPutPart3(EntryEventImpl event, RegionEntry entry,
 +      boolean isInitialized, long lastModified, boolean invokeCallbacks,
 +      boolean ifNew, boolean ifOld, Object expectedOldValue,
 +      boolean requireOldValue) {
 +    
 +    distributeUpdate(event, lastModified, false, false, null, false);
 +    super.basicPutPart3(event, entry, isInitialized, lastModified,
 +        invokeCallbacks, ifNew, ifOld, expectedOldValue, requireOldValue);
 +  }
 +
 +  /** distribute an update operation */
 +  protected void distributeUpdate(EntryEventImpl event, long lastModified, boolean ifNew, boolean ifOld, Object expectedOldValue, boolean requireOldValue) {
 +    // an update from a netSearch is not distributed
 +    if (!event.isOriginRemote() && !event.isNetSearch() && !event.isBulkOpInProgress()) {
 +      boolean distribute = true;
 +        if (event.getInhibitDistribution()) {
 +          // this has already been distributed by a one-hop operation
 +          distribute = false;
 +        }
 +        if (distribute) {
 +          UpdateOperation op = new UpdateOperation(event, lastModified);
 +          if (logger.isTraceEnabled()) {
 +            logger.trace("distributing operation for event : {} : for region : {}", event, this.getName());
 +          }
 +          op.distribute();
 +        }
 +    }
 +  }
 +
 +  protected void setGeneratedVersionTag(boolean generateVersionTag) {
 +    // there is at-least one other persistent member, so turn on concurrencyChecks
 +    enableConcurrencyChecks();
 +    
 +    this.generateVersionTag = generateVersionTag;
 +  }
 +
 +  protected boolean getGenerateVersionTag() {
 +    return this.generateVersionTag;
 +  }
 +
 +  @Override
 +  protected boolean shouldGenerateVersionTag(RegionEntry entry, EntryEventImpl event) {
 +    if (logger.isTraceEnabled()) {
 +      logger.trace("shouldGenerateVersionTag this.generateVersionTag={} ccenabled={} dataPolicy={} event:{}",
 +          this.generateVersionTag, this.concurrencyChecksEnabled, this.dataPolicy, event);
 +    }
 +    if (!this.concurrencyChecksEnabled || this.dataPolicy == DataPolicy.EMPTY || !this.generateVersionTag) {
 +      return false;
 +    }
 +    if (this.srp != null) { // client
 +      return false;
 +    }
 +    if (event.getVersionTag() != null && !event.getVersionTag().isGatewayTag()) {
 +      return false;
 +    }
 +    if (event.getOperation().isLocal()) { // bug #45402 - localDestroy generated a version tag
 +      return false;
 +    }
 +    if (!event.isOriginRemote() && this.dataPolicy.withReplication()) {
 +      return true;
 +    }
 +    if (!this.dataPolicy.withReplication() && !this.dataPolicy.withPersistence()) {
 +      if (!entry.getVersionStamp().hasValidVersion()) {
 +        // do not generate a version stamp in a region that has no replication if it's not based
 +        // on an existing version from a replicate region
 +        return false;
 +      }
 +      return true;
 +    }
 +    if (!event.isOriginRemote() && event.getDistributedMember() != null) {
 +      if (!event.getDistributedMember().equals(this.getMyId())) {
 +        return event.getVersionTag() == null; // one-hop remote message
 +      }
 +    }
 +    return false;
 +  }
 +  /**
 +   * Throws RegionAccessException if required roles are missing and the
 +   * LossAction is NO_ACCESS
 +   * 
 +   * @throws RegionAccessException
 +   *           if required roles are missing and the LossAction is NO_ACCESS
 +   */
 +  @Override
 +  protected void checkForNoAccess()
 +  {
 +    if (this.requiresReliabilityCheck && this.isMissingRequiredRoles) {
 +      if (getMembershipAttributes().getLossAction().isNoAccess()) {
 +        synchronized (this.missingRequiredRoles) {
 +          if (!this.isMissingRequiredRoles)
 +            return;
 +          Set roles = Collections.unmodifiableSet(new HashSet(
 +              this.missingRequiredRoles));
 +          throw new RegionAccessException(LocalizedStrings.DistributedRegion_OPERATION_IS_DISALLOWED_BY_LOSSACTION_0_BECAUSE_THESE_REQUIRED_ROLES_ARE_MISSING_1.toLocalizedString(new Object[] {getMembershipAttributes().getLossAction(), roles}), getFullPath(), roles);
 +        }
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Throws RegionAccessException is required roles are missing and the
 +   * LossAction is either NO_ACCESS or LIMITED_ACCESS.
 +   * 
 +   * @throws RegionAccessException
 +   *           if required roles are missing and the LossAction is either
 +   *           NO_ACCESS or LIMITED_ACCESS
 +   */
 +  @Override
 +  protected void checkForLimitedOrNoAccess()
 +  {
 +    if (this.requiresReliabilityCheck && this.isMissingRequiredRoles) {
 +      if (getMembershipAttributes().getLossAction().isNoAccess()
 +          || getMembershipAttributes().getLossAction().isLimitedAccess()) {
 +        synchronized (this.missingRequiredRoles) {
 +          if (!this.isMissingRequiredRoles)
 +            return;
 +          Set roles = Collections.unmodifiableSet(new HashSet(
 +              this.missingRequiredRoles));
 +          Assert.assertTrue(!roles.isEmpty());
 +          throw new RegionAccessException(LocalizedStrings.DistributedRegion_OPERATION_IS_DISALLOWED_BY_LOSSACTION_0_BECAUSE_THESE_REQUIRED_ROLES_ARE_MISSING_1
 +              .toLocalizedString(new Object[] { getMembershipAttributes().getLossAction(), roles}), getFullPath(), roles);
 +        }
 +      }
 +    }
 +  }
 +  
 +  @Override
 +  protected void handleReliableDistribution(ReliableDistributionData data,
 +      Set successfulRecipients) {
 +    handleReliableDistribution(data, successfulRecipients,
 +        Collections.EMPTY_SET, Collections.EMPTY_SET);
 +  }
 +
 +  protected void handleReliableDistribution(ReliableDistributionData data,
 +      Set successfulRecipients, Set otherRecipients1, Set otherRecipients2)
 +  {
 +    if (this.requiresReliabilityCheck) {
 +      MembershipAttributes ra = getMembershipAttributes();
 +      Set recipients = successfulRecipients;
 +      // determine the successful roles
 +      Set roles = new HashSet();
 +      for (Iterator iter = recipients.iterator(); iter.hasNext();) {
 +        InternalDistributedMember mbr = (InternalDistributedMember)iter.next();
 +        if (mbr != null) {
 +          roles.addAll(mbr.getRoles());
 +        }
 +      }
 +      for (Iterator iter = otherRecipients1.iterator(); iter.hasNext();) {
 +        InternalDistributedMember mbr = (InternalDistributedMember)iter.next();
 +        if (mbr != null) {
 +          roles.addAll(mbr.getRoles());
 +        }
 +      }
 +      for (Iterator iter = otherRecipients2.iterator(); iter.hasNext();) {
 +        InternalDistributedMember mbr = (InternalDistributedMember)iter.next();
 +        if (mbr != null) {
 +          roles.addAll(mbr.getRoles());
 +        }
 +      }
 +      // determine the missing roles
 +      Set failedRoles = new HashSet(ra.getRequiredRoles());
 +      failedRoles.removeAll(roles);
 +      if (failedRoles.isEmpty())
 +        return;
 +//       if (rp.isAllAccessWithQueuing()) {
 +//         this.rmq.add(data, failedRoles);
 +//       } else {
 +
 +      throw new RegionDistributionException(LocalizedStrings.DistributedRegion_OPERATION_DISTRIBUTION_MAY_HAVE_FAILED_TO_NOTIFY_THESE_REQUIRED_ROLES_0.toLocalizedString(failedRoles), getFullPath(), failedRoles);
 +//       }
 +    }
 +  }
 +
 +  /**
 +   * 
 +   * Called when we do a distributed operation and don't have anyone to
 +   * distributed it too. Since this is only called when no distribution was done
 +   * (i.e. no recipients) we do not check isMissingRequiredRoles because it
 +   * might not longer be true due to race conditions
 +   * 
 +   * @return false if this region has at least one required role and queuing is
 +   *         configured. Returns true if sending to no one is ok.
 +   * @throws RoleException
 +   *           if a required role is missing and the LossAction is either
 +   *           NO_ACCESS or LIMITED_ACCESS.
 +   * @since 5.0
 +   */
 +  protected boolean isNoDistributionOk()
 +  {
 +    if (this.requiresReliabilityCheck) {
 +      MembershipAttributes ra = getMembershipAttributes();
 +      //       if (ra.getLossAction().isAllAccessWithQueuing()) {
 +      //         return !ra.hasRequiredRoles();
 +      //       } else {
 +      Set failedRoles = ra.getRequiredRoles();
 +      throw new RegionDistributionException(LocalizedStrings.DistributedRegion_OPERATION_DISTRIBUTION_WAS_NOT_DONE_TO_THESE_REQUIRED_ROLES_0.toLocalizedString(failedRoles), getFullPath(), failedRoles);
 +      //       }
 +    }
 +    return true;
 +  }
 +  
 +  /**
 +   * returns true if this Region does not distribute its operations to other
 +   * members.
 +   * @since 6.0
 +   * @see HARegion#localDestroyNoCallbacks(Object)
 +   */
 +  public boolean doesNotDistribute() {
 +    return false;
 +  }
 +
 +  
 +  @Override
 +  public boolean shouldSyncForCrashedMember(InternalDistributedMember id) {
 +    return !doesNotDistribute() && super.shouldSyncForCrashedMember(id);
 +  }
 +  
 +  
 +  /**
 +   * Adjust the specified set of recipients by removing any of them that are
 +   * currently having their data queued.
 +   * 
 +   * @param recipients
 +   *          the set of recipients that a message is to be distributed too.
 +   *          Recipients that are currently having their data queued will be
 +   *          removed from this set.
 +   * @return the set, possibly null, of recipients that are currently having
 +   *         their data queued.
 +   * @since 5.0
 +   */
 +  protected Set adjustForQueuing(Set recipients)
 +  {
 +    Set result = null;
 +    //     if (this.requiresReliabilityCheck) {
 +    //       MembershipAttributes ra = getMembershipAttributes();
 +    //       if (ra.getLossAction().isAllAccessWithQueuing()) {
 +    //         Set currentQueuedRoles = this.rmq.getQueuingRoles();
 +    //         if (currentQueuedRoles != null) {
 +    //           // foreach recipient see if any of his roles are queued and if
 +    //           // they are remove him from recipients and add him to result
 +    //           Iterator it = recipients.iterator();
 +    //           while (it.hasNext()) {
 +    //             DistributedMember dm = (DistributedMember)it.next();
 +    //             Set dmRoles = dm.getRoles();
 +    //             if (!dmRoles.isEmpty()) {
 +    //               if (intersects(dmRoles, currentQueuedRoles)) {
 +    //                 it.remove(); // fix for bug 34447
 +    //                 if (result == null) {
 +    //                   result = new HashSet();
 +    //                 }
 +    //                 result.add(dm);
 +    //               }
 +    //             }
 +    //           }
 +    //         }
 +    //       }
 +    //     }
 +    return result;
 +  }
 +
 +  /**
 +   * Returns true if the two sets intersect
 +   * 
 +   * @param a
 +   *          a non-null non-empty set
 +   * @param b
 +   *          a non-null non-empty set
 +   * @return true if sets a and b intersect; false if not
 +   * @since 5.0
 +   */
 +  public static boolean intersects(Set a, Set b)
 +  {
 +    Iterator it;
 +    Set target;
 +    if (a.size() <= b.size()) {
 +      it = a.iterator();
 +      target = b;
 +    }
 +    else {
 +      it = b.iterator();
 +      target = a;
 +    }
 +    while (it.hasNext()) {
 +      if (target.contains(it.next()))
 +        return true;
 +    }
 +    return false;
 +  }
 +
 +  @Override
 +  public boolean requiresReliabilityCheck()
 +  {
 +    return this.requiresReliabilityCheck;
 +  }
 +
 +  /**
 +   * Returns true if the ExpiryTask is currently allowed to expire.
 +   * <p>
 +   * If the region is in NO_ACCESS due to reliability configuration, then no
 +   * expiration actions are allowed.
 +   * <p>
 +   * If the region is in LIMITED_ACCESS due to reliability configuration, then
 +   * only non-distributed expiration actions are allowed.
 +   */
 +  @Override
 +  protected boolean isExpirationAllowed(ExpiryTask expiry)
 +  {
 +    if (this.requiresReliabilityCheck && this.isMissingRequiredRoles) {
 +      if (getMembershipAttributes().getLossAction().isNoAccess()) {
 +        return false;
 +      }
 +      if (getMembershipAttributes().getLossAction().isLimitedAccess()
 +          && expiry.isDistributedAction()) {
 +        return false;
 +      }
 +    }
 +    return true;
 +  }
 +
 +  /**
 +   * Performs the resumption action when reliability is resumed.
 +   * 
 +   * @return true if asynchronous resumption is triggered
 +   */
 +  protected boolean resumeReliability(InternalDistributedMember id,
 +      Set newlyAcquiredRoles)
 +  {
 +    boolean async = false;
 +    try {
 +      ResumptionAction ra = getMembershipAttributes().getResumptionAction();
 +      if (ra.isNone()) {
 +        if (logger.isDebugEnabled()) {
 +          logger.debug("Reliability resumption for action of none");
 +        }
 +        resumeExpiration();
 +      }
 +      else if (ra.isReinitialize()) {
 +        async = true;
 +        asyncResumeReliability(id, newlyAcquiredRoles);
 +      }
 +    }
 +    catch (Exception e) {
 +      logger.fatal(LocalizedMessage.create(LocalizedStrings.DistributedRegion_UNEXPECTED_EXCEPTION), e);
 +    }
 +    return async;
 +  }
 +
 +  /**
 +   * Handles asynchronous ResumptionActions such as region reinitialize.
 +   */
 +  private void asyncResumeReliability(final InternalDistributedMember id,
 +                                      final Set newlyAcquiredRoles)
 +                               throws RejectedExecutionException {
 +    final ResumptionAction ra = getMembershipAttributes().getResumptionAction();
 +    getDistributionManager().getWaitingThreadPool().execute(new Runnable() {
 +      public void run()
 +      {
 +        try {
 +          if (ra.isReinitialize()) {
 +            if (logger.isDebugEnabled()) {
 +              logger.debug("Reliability resumption for action of reinitialize");
 +            }
 +            if (!isDestroyed() && !cache.isClosed()) {
 +              RegionEventImpl event = new RegionEventImpl(
 +                  DistributedRegion.this, Operation.REGION_REINITIALIZE, null,
 +                  false, getMyId(), generateEventID());
 +              reinitialize(null, event);
 +            }
 +            synchronized (missingRequiredRoles) {
 +              // any number of threads may be waiting on missingRequiredRoles
 +              missingRequiredRoles.notifyAll();
 +              if (hasListener() && id != null) {
 +                // fire afterRoleGain event
 +                RoleEventImpl relEvent = new RoleEventImpl(
 +                    DistributedRegion.this, Operation.REGION_CREATE, null,
 +                    true, id, newlyAcquiredRoles);
 +                dispatchListenerEvent(EnumListenerEvent.AFTER_ROLE_GAIN,
 +                    relEvent);
 +              }
 +            }
 +          }
 +        }
 +        catch (Exception e) {
 +          logger.fatal(LocalizedMessage.create(LocalizedStrings.DistributedRegion_UNEXPECTED_EXCEPTION), e);
 +        }
 +      }
 +    });
 +  }
 +
 +  /** Reschedules expiry tasks when reliability is resumed. */
 +  private void resumeExpiration()
 +  {
 +    boolean isNoAccess = getMembershipAttributes().getLossAction().isNoAccess();
 +    boolean isLimitedAccess = getMembershipAttributes().getLossAction()
 +        .isLimitedAccess();
 +    if (!(isNoAccess || isLimitedAccess)) {
 +      return; // early out: expiration was never affected by reliability
 +    }
 +
 +    if (getEntryTimeToLive().getTimeout() > 0
 +        && (isNoAccess || (isLimitedAccess && getEntryTimeToLive().getAction()
 +            .isDistributed()))) {
 +      rescheduleEntryExpiryTasks();
 +    }
 +    else 
 +    if (getEntryIdleTimeout().getTimeout() > 0
 +        && (isNoAccess || (isLimitedAccess && getEntryIdleTimeout().getAction()
 +            .isDistributed()))) {
 +      rescheduleEntryExpiryTasks();
 +    }
 +    else
 +    if (getCustomEntryTimeToLive() != null || getCustomEntryIdleTimeout() != null) {
 +      // Force all entries to be rescheduled
 +      rescheduleEntryExpiryTasks();
 +    }
 +
 +    if (getRegionTimeToLive().getTimeout() > 0
 +        && (isNoAccess || (isLimitedAccess && getRegionTimeToLive().getAction()
 +            .isDistributed()))) {
 +      addTTLExpiryTask();
 +    }
 +    if (getRegionIdleTimeout().getTimeout() > 0
 +        && (isNoAccess || (isLimitedAccess && getRegionIdleTimeout()
 +            .getAction().isDistributed()))) {
 +      addIdleExpiryTask();
 +    }
 +  }
 +
 +  /**
 +   * A boolean used to indicate if its the intialization time i.e the
 +   * distributed Region is created for the first time. The variable is used at
 +   * the time of lost reliablility.
 +   */
 +  private boolean isInitializingThread = false;
 +
 +  /**
 +   * Called when reliability is lost. If MembershipAttributes are configured
 +   * with {@link LossAction#RECONNECT}then DistributedSystem reconnect will be
 +   * called asynchronously.
 +   * 
 +   * @return true if asynchronous resumption is triggered
 +   */
 +  protected boolean lostReliability(final InternalDistributedMember id,
 +      final Set newlyMissingRoles)
 +  {
 +    if (DistributedRegion.ignoreReconnect)
 +      return false;
 +    boolean async = false;
 +    try {
 +      if (getMembershipAttributes().getLossAction().isReconnect()) {
 +        async = true;
 +        if (isInitializingThread) {
 +          doLostReliability(true, id, newlyMissingRoles);
 +        }
 +        else {
 +          doLostReliability(false, id, newlyMissingRoles);
 +        }
 +        // we don't do this in the waiting pool because we're going to
 +        // disconnect
 +        // the distributed system, and it will wait for the pool to empty
 +        /*
 +         * moved to a new method called doLostReliablity. Thread t = new
 +         * Thread("Reconnect Distributed System") { public void run() { try { //
 +         * TODO: may need to check isReconnecting and checkReadiness...
 +         * initializationLatchAfterMemberTimeout.await(); // TODO:
 +         * call reconnect here
 +         * getSystem().tryReconnect((GemFireCache)getCache()); // added for
 +         * reconnect. synchronized (missingRequiredRoles) { // any number of
 +         * threads may be waiting on missingRequiredRoles
 +         * missingRequiredRoles.notifyAll(); // need to fire an event if id is
 +         * not null if (hasListener() && id != null) { RoleEventImpl relEvent =
 +         * new RoleEventImpl( DistributedRegion.this, Operation.CACHE_RECONNECT,
 +         * null, true, id, newlyMissingRoles); dispatchListenerEvent(
 +         * EnumListenerEvent.AFTER_ROLE_LOSS, relEvent); } } } catch (Exception
 +         * e) { } } };
 +         * t.setDaemon(true); t.start();
 +         */
 +      }
 +    }
 +    catch (CancelException cce) {
 +      throw cce;
 +    }
 +    catch (Exception e) {
 +      logger.fatal(LocalizedMessage.create(LocalizedStrings.DistributedRegion_UNEXPECTED_EXCEPTION), e);
 +    }
 +    return async;
 +  }
 +
 +  private void doLostReliability(boolean isInitializing,
 +      final InternalDistributedMember id, final Set newlyMissingRoles)
 +  {
 +    try {
 +      if (!isInitializing) {
 +        // moved code to a new thread.
 +        Thread t = new Thread(LocalizedStrings.DistributedRegion_RECONNECT_DISTRIBUTED_SYSTEM.toLocalizedString()) {
 +          @Override
 +          public void run()
 +          {
 +            try {
 +              // TODO: may need to check isReconnecting and checkReadiness...
 +              if (logger.isDebugEnabled()) {
 +                logger.debug("Reliability loss with policy of reconnect and membership thread doing reconnect");
 +              }
 +              initializationLatchAfterMemberTimeout.await();
 +              getSystem().tryReconnect(false, "Role Loss", getCache());
 +              synchronized (missingRequiredRoles) {
 +                // any number of threads may be waiting on missingRequiredRoles
 +                missingRequiredRoles.notifyAll();
 +                // need to fire an event if id is not null
 +                if (hasListener() && id != null) {
 +                  RoleEventImpl relEvent = new RoleEventImpl(
 +                      DistributedRegion.this, Operation.CACHE_RECONNECT, null,
 +                      true, id, newlyMissingRoles);
 +                  dispatchListenerEvent(EnumListenerEvent.AFTER_ROLE_LOSS,
 +                      relEvent);
 +                }
 +              }
 +            }
 +            catch (Exception e) {
 +              logger.fatal(LocalizedMessage.create(LocalizedStrings.DistributedRegion_UNEXPECTED_EXCEPTION), e);
 +            }
 +          }
 +        };
 +        t.setDaemon(true);
 +        t.start();
 +
 +      }
 +      else {
 +        getSystem().tryReconnect(false, "Role Loss", getCache()); // added for
 +        // reconnect.
 +        synchronized (missingRequiredRoles) {
 +          // any number of threads may be waiting on missingRequiredRoles
 +          missingRequiredRoles.notifyAll();
 +          // need to fire an event if id is not null
 +          if (hasListener() && id != null) {
 +            RoleEventImpl relEvent = new RoleEventImpl(DistributedRegion.this,
 +                Operation.CACHE_RECONNECT, null, true, id, newlyMissingRoles);
 +            dispatchListenerEvent(EnumListenerEvent.AFTER_ROLE_LOSS, relEvent);
 +          }
 +        }
 +        // } catch (CancelException cce){
 +
 +        // }
 +
 +      }
 +    }
 +    catch (CancelException ignor) {
 +      throw ignor;
 +    }
 +    catch (Exception e) {
 +      logger.fatal(LocalizedMessage.create(LocalizedStrings.DistributedRegion_UNEXPECTED_EXCEPTION), e);
 +    }
 +
 +  }
 +
 +  protected void lockCheckReadiness()
 +  {
 +    // fix for bug 32610
 +    cache.getCancelCriterion().checkCancelInProgress(null);
 +    checkReadiness();
 +  }
 +
 +  @Override
 +  public final Object validatedDestroy(Object key, EntryEventImpl event)
 +      throws TimeoutException, EntryNotFoundException, CacheWriterException {
 +    Lock dlock = this.getDistributedLockIfGlobal(key);
 +    try {
 +      return super.validatedDestroy(key, event);
 +    } finally {
 +      if (dlock != null) {
 +        dlock.unlock();
 +      }
 +    }
 +  }
 +
 +  /**
 +   * @see LocalRegion#localDestroyNoCallbacks(Object)
 +   */
 +  @Override
 +  public void localDestroyNoCallbacks(Object key)
 +  {
 +    super.localDestroyNoCallbacks(key);
 +    if (getScope().isGlobal()) {
 +      try {
 +        this.getLockService().freeResources(key);
 +      }
 +      catch (LockServiceDestroyedException ignore) {
 +      }
 +    }
 +  }
 +
 +  /**
 +   * @see LocalRegion#localDestroy(Object, Object)
 +   */
 +  @Override
 +  public void localDestroy(Object key, Object aCallbackArgument)
 +      throws EntryNotFoundException
 +  {
 +    super.localDestroy(key, aCallbackArgument);
 +    if (getScope().isGlobal()) {
 +      try {
 +        this.getLockService().freeResources(key);
 +      }
 +      catch (LockServiceDestroyedException ignore) {
 +      }
 +    }
 +  }
 +
 +  /**
 +   * @see LocalRegion#invalidate(Object, Object)
 +   */
 +  @Override
 +  public void invalidate(Object key, Object aCallbackArgument)
 +      throws TimeoutException, EntryNotFoundException
 +  {
 +    validateKey(key);
 +    validateCallbackArg(aCallbackArgument);
 +    checkReadiness();
 +    checkForLimitedOrNoAccess();
 +    Lock dlock = this.getDistributedLockIfGlobal(key);
 +    try {
 +      super.validatedInvalidate(key, aCallbackArgument);
 +    }
 +    finally {
 +      if (dlock != null)
 +        dlock.unlock();
 +    }
 +  }
 +
 +  @Override
 +  public Lock getRegionDistributedLock() throws IllegalStateException
 +  {
 +    lockCheckReadiness();
 +    checkForLimitedOrNoAccess();
 +    if (!this.scope.isGlobal()) {
 +      throw new IllegalStateException(LocalizedStrings.DistributedRegion_DISTRIBUTION_LOCKS_ARE_ONLY_SUPPORTED_FOR_REGIONS_WITH_GLOBAL_SCOPE_NOT_0.toLocalizedString(this.scope));
 +    }
 +    return new RegionDistributedLock();
 +  }
 +
 +  @Override
 +  public Lock getDistributedLock(Object key) throws IllegalStateException
 +  {
 +    validateKey(key);
 +    lockCheckReadiness();
 +    checkForLimitedOrNoAccess();
 +    if (!this.scope.isGlobal()) {
 +      throw new IllegalStateException(LocalizedStrings.DistributedRegion_DISTRIBUTION_LOCKS_ARE_ONLY_SUPPORTED_FOR_REGIONS_WITH_GLOBAL_SCOPE_NOT_0.toLocalizedString(this.scope));
 +    }
 +    if (isLockingSuspendedByCurrentThread()) {
 +      throw new IllegalStateException(LocalizedStrings.DistributedRegion_THIS_THREAD_HAS_SUSPENDED_ALL_LOCKING_FOR_THIS_REGION.toLocalizedString());
 +    }
 +    return new DistributedLock(key);
 +  }
 +
 +  /**
 +   * Called while NOT holding lock on parent's subregions
 +   * 
 +   * @throws IllegalStateException
 +   *           if region is not compatible with a region in another VM.
 +   * 
 +   * @see LocalRegion#initialize(InputStream, InternalDistributedMember, InternalRegionArguments)
 +   */
 +  @Override
 +  protected void initialize(InputStream snapshotInputStream,
 +      InternalDistributedMember imageTarget, InternalRegionArguments internalRegionArgs) throws TimeoutException,
 +      IOException, ClassNotFoundException
 +  {
 +    Assert.assertTrue(!isInitialized());
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("DistributedRegion.initialize BEGIN: {}", getFullPath());
 +    }
 +
 +    // if we're versioning entries we need a region-level version vector
 +    if (this.scope.isDistributed() && this.concurrencyChecksEnabled) {
 +      createVersionVector();
 +    }
 +
 +    if (this.scope.isGlobal()) {
 +      getLockService(); // create lock service eagerly now
 +    }
 +
 +    final IndexUpdater indexUpdater = getIndexUpdater();
 +    boolean sqlfGIILockTaken = false;
 +    // this try block is to release the SQLF GII lock in finally
 +    // which should be done after bucket status will be set
 +    // properly in LocalRegion#initialize()
 +    try {
 +     try {
 +      try {
 +        // take the GII lock to avoid missing entries while updating the
 +        // index list for SQLFabric (#41330 and others)
 +        if (indexUpdater != null) {
 +          indexUpdater.lockForGII();
 +          sqlfGIILockTaken = true;
 +        }
 +        
 +        PersistentMemberID persistentId = null;
 +        boolean recoverFromDisk = isRecoveryNeeded();
 +        DiskRegion dskRgn = getDiskRegion();
 +        if (recoverFromDisk) {
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("DistributedRegion.getInitialImageAndRecovery: Starting Recovery");
 +          }
 +          dskRgn.initializeOwner(this); // do recovery
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("DistributedRegion.getInitialImageAndRecovery: Finished Recovery");
 +          }
 +          persistentId = dskRgn.getMyPersistentID();
 +        }
 +        
 +        // Create OQL indexes before starting GII.
 +        createOQLIndexes(internalRegionArgs, recoverFromDisk);
 + 
 +        if (getDataPolicy().withReplication()
 +            || getDataPolicy().withPreloaded()) {
 +          getInitialImageAndRecovery(snapshotInputStream, imageTarget,
 +              internalRegionArgs, recoverFromDisk, persistentId);
 +        }
 +        else {
 +          new CreateRegionProcessor(this).initializeRegion();
 +          if (snapshotInputStream != null) {
 +            releaseBeforeGetInitialImageLatch();
 +            loadSnapshotDuringInitialization(snapshotInputStream);
 +          }
 +        }
 +      }
 +      catch (DiskAccessException dae) {
 +        this.handleDiskAccessException(dae, true);
 +        throw dae;
 +      }
 +
 +      initMembershipRoles();
 +      isInitializingThread = false;
 +      super.initialize(null, null, null); // makes sure all latches are released if they haven't been already
 +     } finally {
 +      if (this.eventTracker != null) {
 +        this.eventTracker.setInitialized();
 +      }
 +     }
 +    } finally {
 +      if (sqlfGIILockTaken) {
 +        indexUpdater.unlockForGII();
 +      }
 +    }
 +  }
 +
 +  @Override
 +  public void initialized() {
 +    new UpdateAttributesProcessor(this).distribute(false);
 +  }
 +
 +  /** True if GII was impacted by missing required roles */
 +  private boolean giiMissingRequiredRoles = false;
 +
 +  /**
 +   * A reference counter to protected the memoryThresholdReached boolean
 +   */
 +  private final Set<DistributedMember> memoryThresholdReachedMembers =
 +    new CopyOnWriteArraySet<DistributedMember>();
 +
 +  private ConcurrentParallelGatewaySenderQueue hdfsQueue;
 +
 +  /** Sets and returns giiMissingRequiredRoles */
 +  private boolean checkInitialImageForReliability(
 +      InternalDistributedMember imageTarget,
 +      CacheDistributionAdvisor.InitialImageAdvice advice)
 +  {
 +    // assumption: required roles are interesting to GII only if Reinitialize...
 +//    if (true)
 +      return false;
 +//    if (getMembershipAttributes().hasRequiredRoles()
 +//        && getMembershipAttributes().getResumptionAction().isReinitialize()) {
 +//      // are any required roles missing for GII with Reinitialize?
 +//      Set missingRR = new HashSet(getMembershipAttributes().getRequiredRoles());
 +//      missingRR.removeAll(getSystem().getDistributedMember().getRoles());
 +//      for (Iterator iter = advice.replicates.iterator(); iter.hasNext();) {
 +//        DistributedMember member = (DistributedMember)iter.next();
 +//        missingRR.removeAll(member.getRoles());
 +//      }
 +//      for (Iterator iter = advice.others.iterator(); iter.hasNext();) {
 +//        DistributedMember member = (DistributedMember)iter.next();
 +//        missingRR.removeAll(member.getRoles());
 +//      }
 +//      for (Iterator iter = advice.preloaded.iterator(); iter.hasNext();) {
 +//        DistributedMember member = (DistributedMember)iter.next();
 +//        missingRR.removeAll(member.getRoles());
 +//      }
 +//      if (!missingRR.isEmpty()) {
 +//        // entering immediate loss condition, which will cause reinit on resume
 +//        this.giiMissingRequiredRoles = true;
 +//      }
 +//    }
 +//    return this.giiMissingRequiredRoles;
 +  }
 +
 +  private void getInitialImageAndRecovery(InputStream snapshotInputStream,
 +      InternalDistributedMember imageSrc, InternalRegionArguments internalRegionArgs,
 +      boolean recoverFromDisk, PersistentMemberID persistentId) throws TimeoutException
 +  {
 +    logger.info(LocalizedMessage.create(LocalizedStrings.DistributedRegion_INITIALIZING_REGION_0, this.getName()));
 +  
 +    ImageState imgState = getImageState();
 +    imgState.init();
 +    boolean targetRecreated = internalRegionArgs.getRecreateFlag();
 +    Boolean isCBool = (Boolean)isConversion.get();
 +    boolean isForConversion = isCBool!=null?isCBool.booleanValue():false;
 +
 +    if (recoverFromDisk && snapshotInputStream != null && !isForConversion) {
 +      throw new InternalGemFireError(LocalizedStrings.DistributedRegion_IF_LOADING_A_SNAPSHOT_THEN_SHOULD_NOT_BE_RECOVERING_ISRECOVERING_0_SNAPSHOTSTREAM_1.toLocalizedString(new Object[] {Boolean.valueOf(recoverFromDisk), snapshotInputStream}));
 +    }
 +
 +    ProfileExchangeProcessor targetProvider;
 +    if (dataPolicy.withPersistence()) {
 +      targetProvider = new CreatePersistentRegionProcessor(this,
 +          getPersistenceAdvisor(), recoverFromDisk);
 +    }
 +    else {
 +      // this will go in the advisor profile
 +      targetProvider = new CreateRegionProcessor(this);
 +    }
 +    imgState.setInRecovery(false);
 +    RegionVersionVector recovered_rvv = null;
 +    if (dataPolicy.withPersistence()) {
 +      recovered_rvv = (this.getVersionVector()==null?null:this.getVersionVector().getCloneForTransmission());
 +    }
 +      // initializeRegion will send out our profile
 +    targetProvider.initializeRegion();
 +    
 +    if(persistenceAdvisor != null) {
 +      persistenceAdvisor.initialize();
 +    }
 +    
 +    // Register listener here so that the remote members are known
 +    // since registering calls initializeCriticalMembers (which needs to know about
 +    // remote members
 +    if (!isInternalRegion()) {
 +      if (!this.isDestroyed) {
 +        cache.getResourceManager().addResourceListener(ResourceType.MEMORY, this);
 +      }
 +    }
 +    
 +    releaseBeforeGetInitialImageLatch();
 +
 +    // allow GII to invoke test hooks.  Do this just after releasing the
 +    // before-gii latch for bug #48962.  See ConcurrentLeaveDuringGIIDUnitTest
 +    InitialImageOperation.beforeGetInitialImage(this);
 +    
 +    if (snapshotInputStream != null) {
 +      try {
 +        if (logger.isDebugEnabled()) {
 +          logger.debug("DistributedRegion.getInitialImageAndRecovery: About to load snapshot, isInitialized={}; {}",
 +              isInitialized(), getFullPath());
 +        }
 +        loadSnapshotDuringInitialization(snapshotInputStream);
 +      }
 +      catch (IOException e) {
 +        throw new RuntimeException(e); // @todo change this exception?
 +      }
 +      catch (ClassNotFoundException e) {
 +        throw new RuntimeException(e); // @todo change this exception?
 +      }
 +      cleanUpDestroyedTokensAndMarkGIIComplete(GIIStatus.NO_GII);
 +      return;
 +    }
 +    
 +    // No snapshot provided, use the imageTarget(s)
 +
 +    // if we were given a recommended imageTarget, use that first, and
 +    // treat it like it is a replicate (regardless of whether it actually is
 +    // or not)
 +
 +    InitialImageOperation iiop = new InitialImageOperation(this, this.entries);
 +    // [defunct] Special case GII for PR admin regions (which are always
 +    // replicates and always writers
 +    // bruce: this was commented out after adding the GIIAckRequest logic to
 +    // force
 +    //        consistency before the gii operation begins
 +    //      if (isUsedForPartitionedRegionAdmin() ||
 +    // isUsedForPartitionedRegionBucket()) {
 +    //        releaseBeforeGetInitialImageLatch();
 +    //        iiop.getFromAll(this.distAdvisor.adviseGeneric(), false);
 +    //        cleanUpDestroyedTokens();
 +    //        return;
 +    //      }
 +
 +
 +    CacheDistributionAdvisor.InitialImageAdvice advice = null;
 +    boolean done = false;
 +    while(!done && !isDestroyed()) {
 +      advice = targetProvider.getInitialImageAdvice(advice);
 +      checkInitialImageForReliability(imageSrc, advice);
 +      boolean attemptGetFromOne = 
 +        imageSrc != null // we were given a specific member
 +        || this.dataPolicy.withPreloaded()
 +           && !advice.preloaded.isEmpty() // this is a preloaded region
 +        || (!advice.replicates.isEmpty());
 +      // That is: if we have 0 or 1 giiProvider then we can do a getFromOne gii;
 +      // if we have 2 or more giiProviders then we must do a getFromAll gii.
 +
 +      if (attemptGetFromOne) {
 +        if (recoverFromDisk) {
 +          if (LocalRegion.ISSUE_CALLBACKS_TO_CACHE_OBSERVER){
 +            CacheObserverHolder.getInstance().afterMarkingGIIStarted();
 +          }
 +        }
 +        {
 +          // If we have an imageSrc and the target is reinitializing mark the
 +          // getInitialImage so that it will wait until the target region is fully initialized
 +          // before responding to the get image request. Otherwise, the
 +          // source may respond with no data because it is still initializing,
 +          // e.g. loading a snapshot.
 +
 +          // Plan A: use specified imageSrc, if specified
 +          if (imageSrc != null) {
 +            try {
 +              GIIStatus ret = iiop.getFromOne(Collections.singleton(imageSrc),
 +                  targetRecreated, advice, recoverFromDisk, recovered_rvv);
 +              if (GIIStatus.didGII(ret)) {
 +                this.giiMissingRequiredRoles = false;
 +                cleanUpDestroyedTokensAndMarkGIIComplete(ret);
 +                done = true;
 +                return;
 +              }
 +            } finally {
 +              imageSrc = null;
 +            }
 +          }
 +
 +          // Plan C: use a replicate, if one exists
 +          GIIStatus ret = iiop.getFromOne(advice.replicates, false, advice, recoverFromDisk, recovered_rvv);
 +          if (GIIStatus.didGII(ret)) {
 +            cleanUpDestroyedTokensAndMarkGIIComplete(ret);
 +            done = true;
 +            return;
 +          }
 +
 +          // Plan D: if this is a PRELOADED region, fetch from another PRELOADED
 +          if (this.dataPolicy.isPreloaded()) {
 +            GIIStatus ret_preload = iiop.getFromOne(advice.preloaded, false, advice, recoverFromDisk, recovered_rvv);
 +            if (GIIStatus.didGII(ret_preload)) {
 +              cleanUpDestroyedTokensAndMarkGIIComplete(ret_preload);
 +              done = true;
 +              return;
 +            }
 +          } // isPreloaded
 +        }
 +
 +        //If we got to this point, we failed in the GII. Cleanup
 +        //any partial image we received
 +        cleanUpAfterFailedGII(recoverFromDisk);
 +
 +      } // attemptGetFromOne
 +      else {
 +        if(!isDestroyed()) {
 +          if(recoverFromDisk) {
 +            logger.info(LocalizedMessage.create(LocalizedStrings.DistributedRegion_INITIALIZED_FROM_DISK,
 +                new Object[] {this.getFullPath(), persistentId, getPersistentID()}));
 +            if(persistentId != null) {
 +              RegionLogger.logRecovery(this.getFullPath(), persistentId,
 +                  getDistributionManager().getDistributionManagerId());
 +            }
 +          } else {
 +            RegionLogger.logCreate(this.getFullPath(),
 +                getDistributionManager().getDistributionManagerId());
 +            
 +            if (getPersistentID() != null) {
 +              RegionLogger.logPersistence(this.getFullPath(),
 +                  getDistributionManager().getDistributionManagerId(),
 +                  getPersistentID());
 +              logger.info(LocalizedMessage.create(LocalizedStrings.DistributedRegion_NEW_PERSISTENT_REGION_CREATED,
 +                  new Object[] {this.getFullPath(), getPersistentID()}));
 +            }
 +          }
 +          
 +          /* no more union GII
 +            // do union getInitialImage
 +            Set rest = new HashSet();
 +            rest.addAll(advice.others);
 +            rest.addAll(advice.preloaded);
 +            // push profile w/ recovery flag turned off at same time that we
 +            // do a union getInitialImage
 +            boolean pushProfile = recoverFromDisk;
 +            iiop.getFromAll(rest, pushProfile);
 +           */
 +          cleanUpDestroyedTokensAndMarkGIIComplete(GIIStatus.NO_GII);
 +          done = true;
 +          return;
 +        }
 +        break;
 +      }
 +    }
 +
 +    return;
 +  }
 +  
 +  private void synchronizeWith(InternalDistributedMember target, 
 +      VersionSource idToRecover) {
 +    InitialImageOperation op = new InitialImageOperation(this, this.entries);
 +    op.synchronizeWith(target, idToRecover, null);
 +  }
 +  
 +  /**
 +   * If this region has concurrency controls enabled this will pull any missing
 +   * changes from other replicates using InitialImageOperation and a filtered
 +   * chunking protocol.
 +   */
 +  public void synchronizeForLostMember(InternalDistributedMember
 +      lostMember, VersionSource lostVersionID) {
 +    if (this.concurrencyChecksEnabled == false) {
 +      return;
 +    }
 +    CacheDistributionAdvisor advisor = getCacheDistributionAdvisor();
 +    Set<InternalDistributedMember> targets = advisor.adviseInitializedReplicates();
 +    for (InternalDistributedMember target: targets) {
 +      synchronizeWith(target, lostVersionID, lostMember);
 +    }
 +  }
 +  
 +  /**
 +   * synchronize with another member wrt messages from the given "lost" member.
 +   * This can be used when a primary bucket crashes to ensure that interrupted
 +   * message distribution is mended.
 +   */
 +  private void synchronizeWith(InternalDistributedMember target,
 +      VersionSource versionMember, InternalDistributedMember lostMember) {
 +    InitialImageOperation op = new InitialImageOperation(this, this.entries);
 +    op.synchronizeWith(target, versionMember, lostMember);
 +  }
 +
 +  /**
 +   * invoked just before an initial image is requested from another member
 +   */
 +  /** remove any partial entries received in a failed GII */
 +  protected void cleanUpAfterFailedGII(boolean recoverFromDisk) {
 +    DiskRegion dskRgn = getDiskRegion();
 +    //if we have a persistent region, instead of deleting everything on disk,
 +    //we will just reset the "recovered from disk" flag. After
 +    //the next GII we will delete these entries if they do not come
 +    //in as part of the GII.
 +    if (recoverFromDisk && dskRgn != null && dskRgn.isBackup()) {
 +      dskRgn.resetRecoveredEntries(this);
 +      return;
 +    }
 +
 +    if (!this.entries.isEmpty()) {
 +      closeEntries();
 +      if (getDiskRegion() != null) {
 +        getDiskRegion().clear(this, null);
 +      }
 +      // clear the left-members and version-tags sets in imageState
 +      getImageState().getLeftMembers();
 +      getImageState().getVersionTags();
 +      // Clear OQL indexes
 +      if (this.indexManager != null) {
 +        try {
 +          this.indexManager.rerunIndexCreationQuery();
 +        } catch (Exception ex){
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("Exception while clearing indexes after GII failure.", ex);
 +          }
 +        }
 +      }
 +    }
 +  }
 +
 +  private void initMembershipRoles()
 +  {
 +    synchronized (this.advisorListener) {
 +      // hold sync to prevent listener from changing initial members
 +      Set others = this.distAdvisor
 +          .addMembershipListenerAndAdviseGeneric(this.advisorListener);
 +      this.advisorListener.addMembers(others);
 +      // initialize missing required roles with initial member info
 +      if (getMembershipAttributes().hasRequiredRoles()) {
 +        // AdvisorListener will also sync on missingRequiredRoles
 +        synchronized (this.missingRequiredRoles) {
 +          this.missingRequiredRoles.addAll(getMembershipAttributes()
 +              .getRequiredRoles());
 +          // remove all the roles we are playing since they will never be
 +          // missing
 +          this.missingRequiredRoles.removeAll(getSystem()
 +              .getDistributedMember().getRoles());
 +          for (Iterator iter = others.iterator(); iter.hasNext();) {
 +            DistributedMember other = (DistributedMember)iter.next();
 +            this.missingRequiredRoles.removeAll(other.getRoles());
 +          }
 +        }
 +      }
 +    }
 +    if (getMembershipAttributes().hasRequiredRoles()) {
 +      // wait up to memberTimeout for required roles...
 +//      boolean requiredRolesAreMissing = false;
 +      int memberTimeout = getSystem().getConfig().getMemberTimeout();
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("Waiting up to {} for required roles.", memberTimeout);
 +      }
 +      try {
 +        if (this.giiMissingRequiredRoles) {
 +          // force reliability loss and possibly resumption
 +          isInitializingThread = true;
 +          synchronized (this.advisorListener) {
 +            synchronized (this.missingRequiredRoles) {
 +              // forcing state of loss because of bad GII
 +              this.isMissingRequiredRoles = true;
 +              getCachePerfStats().incReliableRegionsMissing(1);
 +              if (getMembershipAttributes().getLossAction().isAllAccess())
 +                getCachePerfStats().incReliableRegionsMissingFullAccess(1); // rahul
 +              else if (getMembershipAttributes().getLossAction()
 +                  .isLimitedAccess())
 +                getCachePerfStats().incReliableRegionsMissingLimitedAccess(1);
 +              else if (getMembershipAttributes().getLossAction().isNoAccess())
 +                getCachePerfStats().incReliableRegionsMissingNoAccess(1);
 +              // pur code to increment the stats.
 +              if (logger.isDebugEnabled()) {
 +                logger.debug("GetInitialImage had missing required roles.");
 +              }
 +              // TODO: will this work with RECONNECT and REINITIALIZE?
 +              isInitializingThread = true;
 +              lostReliability(null, null);
 +              if (this.missingRequiredRoles.isEmpty()) {
 +                // all required roles are present so force resumption
 +                this.isMissingRequiredRoles = false;
 +                getCachePerfStats().incReliableRegionsMissing(-1);
 +                if (getMembershipAttributes().getLossAction().isAllAccess())
 +                  getCachePerfStats().incReliableRegionsMissingFullAccess(-1); // rahul
 +                else if (getMembershipAttributes().getLossAction()
 +                    .isLimitedAccess())
 +                  getCachePerfStats()
 +                      .incReliableRegionsMissingLimitedAccess(-1);
 +                else if (getMembershipAttributes().getLossAction().isNoAccess())
 +                  getCachePerfStats().incReliableRegionsMissingNoAccess(-1);
 +                // pur code to increment the stats.
 +                boolean async = resumeReliability(null, null);
 +                if (async) {
 +                  advisorListener.destroyed = true;
 +                }
 +              }
 +            }
 +          }
 +        }
 +        else {
 +          if (!getSystem().isLoner()) {
 +            waitForRequiredRoles(memberTimeout);
 +          }
 +          synchronized (this.advisorListener) {
 +            synchronized (this.missingRequiredRoles) {
 +              if (this.missingRequiredRoles.isEmpty()) {
 +                Assert.assertTrue(!this.isMissingRequiredRoles);
 +                if (logger.isDebugEnabled()) {
 +                  logger.debug("Initialization completed with all required roles present.");
 +                }
 +              }
 +              else {
 +                // starting in state of loss...
 +                this.isMissingRequiredRoles = true;
 +                getCachePerfStats().incReliableRegionsMissing(1);
 +                if (getMembershipAttributes().getLossAction().isAllAccess())
 +                  getCachePerfStats().incReliableRegionsMissingFullAccess(1); // rahul
 +                else if (getMembershipAttributes().getLossAction()
 +                    .isLimitedAccess())
 +                  getCachePerfStats().incReliableRegionsMissingLimitedAccess(1);
 +                else if (getMembershipAttributes().getLossAction().isNoAccess())
 +                  getCachePerfStats().incReliableRegionsMissingNoAccess(1);
 +                
 +                if (logger.isDebugEnabled()) {
 +                  logger.debug("Initialization completed with missing required roles: {}", this.missingRequiredRoles);
 +                }
 +                isInitializingThread = true;
 +                lostReliability(null, null);
 +              }
 +            }
 +          }
 +        }
 +      }
 +      catch (RegionDestroyedException ignore) {
 +        // ignore to fix bug 34639 may be thrown by waitForRequiredRoles
 +      }
 +      catch (CancelException ignore) {
 +        // ignore to fix bug 34639 may be thrown by waitForRequiredRoles
 +        if (isInitializingThread) {
 +          throw ignore;
 +        }
 +      }
 +      catch (Exception e) {
 +        logger.fatal(LocalizedMessage.create(LocalizedStrings.DistributedRegion_UNEXPECTED_EXCEPTION), e);
 +      }
 +
 +    }
 +    // open latch which will allow any threads in lostReliability to proceed
 +    this.initializationLatchAfterMemberTimeout.countDown();
 +  }
 +  private boolean isRecoveryNeeded() {
 +    return getDataPolicy().withPersistence()
 +      && getDiskRegion().isRecreated();
 +  }
 +
 +  // called by InitialImageOperation to clean up destroyed tokens
 +  // release afterGetInitialImageInitializationLatch before unlocking
 +  // cleanUpLock
 +  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="UL_UNRELEASED_LOCK")
 +  private void cleanUpDestroyedTokensAndMarkGIIComplete(GIIStatus giiStatus)
 +  {
 +    //We need to clean up the disk before we release the after get initial image latch
 +    DiskRegion dskRgn = getDiskRegion();
 +    if (dskRgn != null && dskRgn.isBackup()) {
 +      dskRgn.finishInitializeOwner(this, giiStatus);
 +    }
 +    ImageState is = getImageState();
 +    is.lockGII();
 +    // clear the version tag and left-members sets
 +    is.getVersionTags();
 +    is.getLeftMembers();
 +    // remove DESTROYED tokens
 +    RegionVersionVector rvv = is.getClearRegionVersionVector();
 +    try {
 +      Iterator/*<Object>*/ keysIt = getImageState().getDestroyedEntries();
 +      while (keysIt.hasNext()) {
 +        this.entries.removeIfDestroyed(keysIt.next());
 +      }
 +      if (rvv != null) {
 +        // clear any entries received in the GII that are older than the RVV versions.
 +        // this can happen if entry chunks were received prior to the clear() being
 +        // processed
 +        clearEntries(rvv);
 +      }
 +      //need to do this before we release the afterGetInitialImageLatch
 +      if(persistenceAdvisor != null) {
 +        persistenceAdvisor.setOnline(GIIStatus.didGII(giiStatus), false, getPersistentID());
 +      }
 +    }
 +    finally {
 +      // release after gii lock first so basicDestroy will see isInitialized()
 +      // be true
 +      // when they get the cleanUp lock.
 +        try {
 +          releaseAfterGetInitialImageLatch();
 +        } finally { // make sure unlockGII is done for bug 40001
 +          is.unlockGII();
 +        }
 +    }
 +    
 +    if (LocalRegion.ISSUE_CALLBACKS_TO_CACHE_OBSERVER){
 +      CacheObserverHolder.getInstance().afterMarkingGIICompleted();
 +    }
 +    
 +    //"Initializing region {0}" which is not acompanied by a completed message. Users think thread is stuck in some operation. Hence adding this log
 +    logger.info(LocalizedMessage.create(LocalizedStrings.DistributedRegion_INITIALIZING_REGION_COMPLETED_0, this.getName()));
 +  }
 +
 +  /**
 +   * @see LocalRegion#basicDestroy(EntryEventImpl, boolean, Object)
 +   */
 +  @Override
 +  protected
 +  void basicDestroy(EntryEventImpl event,
 +                       boolean cacheWrite,
 +                       Object expectedOldValue)
 +  throws EntryNotFoundException, CacheWriterException, TimeoutException {
 +    //  disallow local destruction for mirrored keysvalues regions
 +    boolean invokeWriter = cacheWrite;
 +    boolean hasSeen = false;
 +    if (hasSeenEvent(event)) {
 +      hasSeen = true;
 +    }
 +    checkIfReplicatedAndLocalDestroy(event);
 +    
 +    try {
 +      if (this.requiresOneHopForMissingEntry(event)) {
 +        // bug #45704: see if a one-hop must be done for this operation
 +        RegionEntry re = getRegionEntry(event.getKey());
 +        if (re == null /*|| re.isTombstone()*/ || !this.generateVersionTag) {
 +          if (this.srp == null) {
 +            // only assert for non-client regions.
 +            Assert.assertTrue(!this.dataPolicy.withReplication() || !this.generateVersionTag);
 +          }
 +          if (!event.isBulkOpInProgress() || this.dataPolicy.withStorage()) {
 +            // removeAll will send a single one-hop for empty regions.  for other missing entries
 +            // we need to get a valid version number before modifying the local cache 
 +          // TODO: deltaGII: verify that delegating to a peer when this region is also a client is acceptable
 +          boolean didDistribute = RemoteDestroyMessage.distribute(event, expectedOldValue, !this.generateVersionTag);
 +
 +          if (!this.generateVersionTag && !didDistribute) {
 +            throw new PersistentReplicatesOfflineException();
 +          }
 +          
 +          if (didDistribute) {
 +            if (logger.isTraceEnabled()) {
 +              logger.trace("Event after remoteDestroy operation: {}", event);
 +            }
 +            invokeWriter = false; // remote cache invoked the writer
 +            if (event.getVersionTag() == null) {
 +              // if the event wasn't applied by the one-hop replicate it will not have a version tag
 +              // and so should not be applied to this cache
 +              return;
 +            }
 +          }
 +          }
 +        }
 +      }
 +      
 +      super.basicDestroy(event, invokeWriter, expectedOldValue);
 +
 +      // if this is a destroy coming in from remote source, free up lock resources
 +      // if this is a local origin destroy, this will happen after lock is
 +      // released
 +      if (this.scope.isGlobal() && event.isOriginRemote()) {
 +        try {
 +          getLockService().freeResources(event.getKey());
 +        }
 +        catch (LockServiceDestroyedException ignore) {
 +        }
 +      }
 +  
 +      return;
 +    } 
 +    finally {
 +      if (hasSeen) {
 +        if (event.isBulkOpInProgress() && !event.isOriginRemote()) {
 +          event.getRemoveAllOperation().addEntry(event, true);
 +        }
-         distributeDestroy(event, expectedOldValue);
-         event.invokeCallbacks(this,true, false);
++        if (!getConcurrencyChecksEnabled() || event.hasValidVersionTag()) {
++          distributeDestroy(event, expectedOldValue);
++          event.invokeCallbacks(this,true, false);
++        }
 +      }
 +    }
 +  }
 +
 +  @Override
 +  void basicDestroyPart3(RegionEntry re, EntryEventImpl event,
 +      boolean inTokenMode, boolean duringRI, boolean invokeCallbacks, Object expectedOldValue) {
 +  
 +    distributeDestroy(event, expectedOldValue);
 +    super.basicDestroyPart3(re, event, inTokenMode, duringRI, invokeCallbacks, expectedOldValue);
 +  }
 +  
 +  void distributeDestroy(EntryEventImpl event, Object expectedOldValue) {
 +    if (event.isDistributed() && !event.isOriginRemote() && !event.isBulkOpInProgress()) {
 +      boolean distribute = !event.getInhibitDistribution();
 +      if (distribute) {
 +        DestroyOperation op =  new DestroyOperation(event);
 +        op.distribute();
 +      }
 +    }
 +  }
 +  
 +  @Override
 +  boolean evictDestroy(LRUEntry entry) {
 +    boolean evictDestroyWasDone = super.evictDestroy(entry);
 +    if (evictDestroyWasDone) {
 +      if (this.scope.isGlobal()) {
 +        try {
 +          getLockService().freeResources(entry.getKey());
 +        }
 +        catch (LockServiceDestroyedException ignore) {
 +        }
 +      }
 +    }
 +    return evictDestroyWasDone;
 +  }
 +
 +
 +  /**
 +   * @see LocalRegion#basicInvalidateRegion(RegionEventImpl)
 +   */
 +  @Override
 +  void basicInvalidateRegion(RegionEventImpl event)
 +  {
 +    // disallow local invalidation for replicated regions
 +    if (!event.isDistributed() && getScope().isDistributed()
 +        && getDataPolicy().withReplication()) {
 +      throw new IllegalStateException(LocalizedStrings.DistributedRegion_NOT_ALLOWED_TO_DO_A_LOCAL_INVALIDATION_ON_A_REPLICATED_REGION.toLocalizedString());
 +    }
 +    if (shouldDistributeInvalidateRegion(event)) {
 +      distributeInvalidateRegion(event);
 +    }
 +    super.basicInvalidateRegion(event);
 +  }
 +
 +  /**
 +   * decide if InvalidateRegionOperation should be sent to peers. broken out so
 +   * that BucketRegion can override
 +   * @param event
 +   * @return true if {@link InvalidateRegionOperation} should be distributed, false otherwise
 +   */
 +  protected boolean shouldDistributeInvalidateRegion(RegionEventImpl event) {
 +    return event.isDistributed() && !event.isOriginRemote();
 +  }
 +
 +  /**
 +   * Distribute the invalidate of a region given its event.
 +   * This implementation sends the invalidate to peers.
 +   * @since 5.7
 +   */
 +  protected void distributeInvalidateRegion(RegionEventImpl event) {
 +    new InvalidateRegionOperation(event).distribute();
 +  }
 +
 +  /**
 +   * @see LocalRegion#basicDestroyRegion(RegionEventImpl, boolean, boolean,
 +   *      boolean)
 +   */
 +  @Override
 +  void basicDestroyRegion(RegionEventImpl event, boolean cacheWrite,
 +      boolean lock, boolean callbackEvents) throws CacheWriterException,
 +      TimeoutException
 +  {
 +    final String path = getFullPath();
 +    //Keep track of regions that are being destroyed. This helps avoid a race
 +    //when another member concurrently creates this region. See bug 42051.
 +    boolean isClose = event.getOperation().isClose();
 +    if(!isClose) {
 +      cache.beginDestroy(path, this);
 +    }
 +    try {
 +      super.basicDestroyRegion(event, cacheWrite, lock, callbackEvents);
 +      // send destroy region operation even if this is a localDestroyRegion (or
 +      // close)
 +      if (!event.isOriginRemote()) {
 +        distributeDestroyRegion(event, true);
 +      } else {
 +        if(!event.isReinitializing()) {
 +          RegionEventImpl localEvent = new RegionEventImpl(this,
 +              Operation.REGION_LOCAL_DESTROY, event.getCallbackArgument(), false, getMyId(),
 +              generateEventID()/* generate EventID */);
 +          distributeDestroyRegion(localEvent, false/*fixes bug 41111*/);
 +        }
 +      }
 +      notifyBridgeClients(event);
 +    }
 +    catch (CancelException e) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("basicDestroyRegion short-circuited due to cancellation");
 +      }
 +    }
 +    finally {
 +      if(!isClose) {
 +        cache.endDestroy(path, this);
 +      }
 +      RegionLogger.logDestroy(path, getMyId(), getPersistentID(), isClose);
 +    }
 + }
 +
 +
 +  @Override
 +  protected void distributeDestroyRegion(RegionEventImpl event,
 +                                         boolean notifyOfRegionDeparture) {
 +    if(persistenceAdvisor != null) {
 +      persistenceAdvisor.releaseTieLock();
 +    }
 +    new DestroyRegionOperation(event, notifyOfRegionDeparture).distribute();
 +  }
 +
 +  /**
 +   * Return true if invalidation occurred; false if it did not, for example if
 +   * it was already invalidated
 +   * 
 +   * @see LocalRegion#basicInvalidate(EntryEventImpl)
 +   */
 +  @Override
 +  void basicInvalidate(EntryEventImpl event) throws EntryNotFoundException
 +  {
 +    
 +    boolean hasSeen = false;
 +    if (hasSeenEvent(event)) {
 +      hasSeen = true;
 +    }
 +    try {
 +      // disallow local invalidation for replicated regions
 +      if (event.isLocalInvalid() && !event.getOperation().isLocal() && getScope().isDistributed()
 +          && getDataPolicy().withReplication()) {
 +        throw new IllegalStateException(LocalizedStrings.DistributedRegion_NOT_ALLOWED_TO_DO_A_LOCAL_INVALIDATION_ON_A_REPLICATED_REGION.toLocalizedString());
 +      }
 +      if (this.requiresOneHopForMissingEntry(event)) {
 +        // bug #45704: see if a one-hop must be done for this operation
 +        RegionEntry re = getRegionEntry(event.getKey());
 +        if (re == null/* || re.isTombstone()*/ || !this.generateVersionTag) {
 +          if (this.srp == null) {
 +            // only assert for non-client regions.
 +            Assert.assertTrue(!this.dataPolicy.withReplication() || !this.generateVersionTag);
 +          }
 +          // TODO: deltaGII: verify that delegating to a peer when this region is also a client is acceptable
 +          boolean didDistribute = RemoteInvalidateMessage.distribute(event, !this.generateVersionTag);
 +          if (!this.generateVersionTag && !didDistribute) {
 +            throw new PersistentReplicatesOfflineException();
 +          }
 +          if (didDistribute) {
 +            if (logger.isDebugEnabled()) {
 +              logger.debug("Event after remoteInvalidate operation: {}", event);
 +            }
 +            if (event.getVersionTag() == null) {
 +              // if the event wasn't applied by the one-hop replicate it will not have a version tag
 +              // and so should not be applied to this cache
 +              return;
 +            }
 +          }
 +        }
 +      }
 +  
 +      super.basicInvalidate(event);
 +
 +      return;
 +    } finally {
 +      if (hasSeen) {
-         distributeInvalidate(event);
-         event.invokeCallbacks(this,true, false);
++    	if (!getConcurrencyChecksEnabled() || event.hasValidVersionTag()) {
++          distributeInvalidate(event);
++          event.invokeCallbacks(this,true, false);
++    	}
 +      }
 +    }
 +  }
 +
 +  @Override
 +  void basicInvalidatePart3(RegionEntry re, EntryEventImpl event,
 +      boolean invokeCallbacks) {
 +    distributeInvalidate(event);
 +    super.basicInvalidatePart3(re, event, invokeCallbacks);
 +  }
 +  
 +  void distributeInvalidate(EntryEventImpl event) {
 +    if (!this.regionInvalid && event.isDistributed() && !event.isOriginRemote()
 +        && !isTX() /* only distribute if non-tx */) {
 +      if (event.isDistributed() && !event.isOriginRemote()) {
 +        boolean distribute = !event.getInhibitDistribution();
 +        if (distribute) {
 +          InvalidateOperation op = new InvalidateOperation(event);
 +          op.distribute();
 +        }
 +      }
 +    }
 +  }
 +
 +  
 +  @Override
 +  void basicUpdateEntryVersion(EntryEventImpl event)
 +      throws EntryNotFoundException {
 +
 +    try {
 +      if (!hasSeenEvent(event)) {
 +        super.basicUpdateEntryVersion(event);
 +      }
 +      return;
 +    } finally {
-       distributeUpdateEntryVersion(event);
++      if (!getConcurrencyChecksEnabled() || event.hasValidVersionTag()) {
++        distributeUpdateEntryVersion(event);
++      }
 +    }
 +  }
 +
 +  private void distributeUpdateEntryVersion(EntryEventImpl event) {
 +    if (!this.regionInvalid && event.isDistributed() && !event.isOriginRemote()
 +        && !isTX() /* only distribute if non-tx */) {
 +      if (event.isDistributed() && !event.isOriginRemote()) {
 +        UpdateEntryVersionOperation op = new UpdateEntryVersionOperation(event);
 +        op.distribute();
 +      }
 +    }
 +  }
 +
 +  @Override
 +  protected void basicClear(RegionEventImpl ev)
 +  {
 +    Lock dlock = this.getRegionDistributedLockIfGlobal();
 +    try {
 +      super.basicClear(ev);
 +    }
 +    finally {
 +      if (dlock != null)
 +        dlock.unlock();
 +    }
 +  }
 +  
 +  @Override
 +  void basicClear(RegionEventImpl regionEvent, boolean cacheWrite)  {
 +    if (this.concurrencyChecksEnabled && !this.dataPolicy.withReplication()) {
 +      boolean retry = false;
 +      do {
 +        // non-replicate regions must defer to a replicate for clear/invalidate of region
 +        Set<InternalDistributedMember> repls = this.distAdvisor.adviseReplicates();
 +        if (repls.size() > 0) {
 +          InternalDistributedMember mbr = repls.iterator().next();
 +          RemoteRegionOperation op = RemoteRegionOperation.clear(mbr, this);
 +          try {
 +            op.distribute();
 +            return;
 +          } catch (CancelException e) {
 +            this.stopper.checkCancelInProgress(e);
 +            retry = true;
 +          } catch (RemoteOperationException e) {
 +            this.stopper.checkCancelInProgress(e);
 +            retry = true;
 +          }
 +        }
 +      } while (retry);
 +    }
 +    // if no version vector or if no replicates are around, use the default mechanism
 +    super.basicClear(regionEvent, cacheWrite);
 +  }
 +  
 +  
 +  @Override
 +  void cmnClearRegion(RegionEventImpl regionEvent, boolean cacheWrite, boolean useRVV) {
 +    boolean enableRVV = useRVV && this.dataPolicy.withReplication() && this.concurrencyChecksEnabled && !getDistributionManager().isLoner(); 
 +    
 +    //Fix for 46338 - apparently multiple threads from the same VM are allowed
 +    //to suspend locking, which is what distributedLockForClear() does. We don't
 +    //want that to happen, so we'll synchronize to make sure only one thread on
 +    //this member performs a clear.
 +    synchronized(clearLock) {
 +      if (enableRVV) {
 +
 +        distributedLockForClear();
 +        try {
 +          Set<InternalDistributedMember> participants = getCacheDistributionAdvisor().adviseInvalidateRegion();
 +          // pause all generation of versions and flush from the other members to this one
 +          try {
 +            obtainWriteLocksForClear(regionEvent, participants);
 +            clearRegionLocally(regionEvent, cacheWrite, null);
 +            if (!regionEvent.isOriginRemote() && regionEvent.isDistributed()) {
 +              DistributedClearOperation.clear(regionEvent, null, participants);
 +            }
 +          } finally {
 +            releaseWriteLocksForClear(regionEvent, participants);
 +          }
 +        }
 +        finally {
 +          distributedUnlockForClear();
 +        }
 +      } else {
 +        Set<InternalDistributedMember> participants = getCacheDistributionAdvisor().adviseInvalidateRegion();
 +        clearRegionLocally(regionEvent, cacheWrite, null);
 +        if (!regionEvent.isOriginRemote() && regionEvent.isDistributed()) {
 +          DistributedClearOperation.clear(regionEvent, null, participants);
 +        }
 +      }
 +    }
 +    
 +    // since clients do not maintain RVVs except for tombstone GC
 +    // we need to ensure that current ops

<TRUNCATED>


[38/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/gms/messenger/JGroupsMessenger.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/gms/messenger/JGroupsMessenger.java
index 3bd1e83,0000000..f3ba7a2
mode 100755,000000..100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/gms/messenger/JGroupsMessenger.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/gms/messenger/JGroupsMessenger.java
@@@ -1,1109 -1,0 +1,1116 @@@
 +/*
 + * 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.distributed.internal.membership.gms.messenger;
 +
 +import static com.gemstone.gemfire.distributed.internal.membership.gms.GMSUtil.replaceStrings;
 +import static com.gemstone.gemfire.internal.DataSerializableFixedID.JOIN_REQUEST;
 +import static com.gemstone.gemfire.internal.DataSerializableFixedID.JOIN_RESPONSE;
 +
 +import java.io.BufferedReader;
 +import java.io.ByteArrayInputStream;
 +import java.io.DataInputStream;
 +import java.io.IOException;
 +import java.io.InputStream;
 +import java.io.InputStreamReader;
 +import java.lang.reflect.Field;
 +import java.lang.reflect.InvocationTargetException;
 +import java.lang.reflect.Method;
 +import java.net.UnknownHostException;
 +import java.util.ArrayList;
 +import java.util.Arrays;
 +import java.util.Collections;
 +import java.util.HashSet;
 +import java.util.Iterator;
 +import java.util.LinkedList;
 +import java.util.List;
 +import java.util.Map;
 +import java.util.Set;
 +import java.util.concurrent.ConcurrentHashMap;
 +import java.util.concurrent.atomic.AtomicLong;
 +
 +import org.apache.logging.log4j.Logger;
 +import org.jgroups.Address;
 +import org.jgroups.Event;
 +import org.jgroups.JChannel;
 +import org.jgroups.Message;
 +import org.jgroups.Message.Flag;
 +import org.jgroups.Message.TransientFlag;
 +import org.jgroups.ReceiverAdapter;
 +import org.jgroups.View;
 +import org.jgroups.ViewId;
 +import org.jgroups.conf.ClassConfigurator;
 +import org.jgroups.protocols.UDP;
 +import org.jgroups.protocols.pbcast.NAKACK2;
 +import org.jgroups.stack.IpAddress;
 +import org.jgroups.util.Digest;
 +import org.jgroups.util.UUID;
 +
 +import com.gemstone.gemfire.DataSerializer;
 +import com.gemstone.gemfire.ForcedDisconnectException;
 +import com.gemstone.gemfire.GemFireConfigException;
 +import com.gemstone.gemfire.GemFireIOException;
 +import com.gemstone.gemfire.SystemConnectException;
 +import com.gemstone.gemfire.distributed.DistributedSystemDisconnectedException;
 +import com.gemstone.gemfire.distributed.DurableClientAttributes;
 +import com.gemstone.gemfire.distributed.internal.DMStats;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 +import com.gemstone.gemfire.distributed.internal.DistributionManager;
 +import com.gemstone.gemfire.distributed.internal.DistributionMessage;
++import com.gemstone.gemfire.distributed.internal.DistributionStats;
 +import com.gemstone.gemfire.distributed.internal.HighPriorityDistributionMessage;
 +import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
 +import com.gemstone.gemfire.distributed.internal.membership.MemberAttributes;
 +import com.gemstone.gemfire.distributed.internal.membership.NetView;
 +import com.gemstone.gemfire.distributed.internal.membership.QuorumChecker;
 +import com.gemstone.gemfire.distributed.internal.membership.gms.GMSMember;
 +import com.gemstone.gemfire.distributed.internal.membership.gms.Services;
 +import com.gemstone.gemfire.distributed.internal.membership.gms.interfaces.MessageHandler;
 +import com.gemstone.gemfire.distributed.internal.membership.gms.interfaces.Messenger;
 +import com.gemstone.gemfire.distributed.internal.membership.gms.messages.JoinRequestMessage;
 +import com.gemstone.gemfire.distributed.internal.membership.gms.messages.JoinResponseMessage;
 +import com.gemstone.gemfire.internal.ClassPathLoader;
 +import com.gemstone.gemfire.internal.HeapDataOutputStream;
 +import com.gemstone.gemfire.internal.OSProcess;
 +import com.gemstone.gemfire.internal.SocketCreator;
 +import com.gemstone.gemfire.internal.Version;
 +import com.gemstone.gemfire.internal.VersionedDataInputStream;
 +import com.gemstone.gemfire.internal.admin.remote.RemoteTransportConfig;
 +import com.gemstone.gemfire.internal.cache.DirectReplyMessage;
 +import com.gemstone.gemfire.internal.cache.DistributedCacheOperation;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.logging.log4j.LocalizedMessage;
 +import com.gemstone.gemfire.internal.tcp.MemberShunnedException;
 +
 +import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
 +
 +public class JGroupsMessenger implements Messenger {
 +
 +  private static final Logger logger = Services.getLogger();
 +
 +  /**
 +   * The location (in the product) of the locator Jgroups config file.
 +   */
 +  private static final String DEFAULT_JGROUPS_TCP_CONFIG = "com/gemstone/gemfire/distributed/internal/membership/gms/messenger/jgroups-config.xml";
 +
 +  /**
 +   * The location (in the product) of the mcast Jgroups config file.
 +   */
 +  private static final String JGROUPS_MCAST_CONFIG_FILE_NAME = "com/gemstone/gemfire/distributed/internal/membership/gms/messenger/jgroups-mcast.xml";
 +
 +  /** JG magic numbers for types added to the JG ClassConfigurator */
 +  public static final short JGROUPS_TYPE_JGADDRESS = 2000;
 +  public static final short JGROUPS_PROTOCOL_TRANSPORT = 1000;
 +
 +  public static boolean THROW_EXCEPTION_ON_START_HOOK;
 +
 +  String jgStackConfig;
 +  
 +  JChannel myChannel;
 +  InternalDistributedMember localAddress;
 +  JGAddress jgAddress;
 +  Services services;
 +
 +  /** handlers that receive certain classes of messages instead of the Manager */
 +  Map<Class, MessageHandler> handlers = new ConcurrentHashMap<Class, MessageHandler>();
 +  
 +  private volatile NetView view;
 +
 +  private GMSPingPonger pingPonger = new GMSPingPonger();
 +  
 +  protected AtomicLong pongsReceived = new AtomicLong(0);
 +  
 +  /**
 +   * A set that contains addresses that we have logged JGroups IOExceptions for in the
 +   * current membership view and possibly initiated suspect processing.  This
 +   * reduces the amount of suspect processing initiated by IOExceptions and the
 +   * amount of exceptions logged
 +   */
 +  private Set<Address> addressesWithioExceptionsProcessed = Collections.synchronizedSet(new HashSet<Address>());
 +  
 +  static {
 +    // register classes that we've added to jgroups that are put on the wire
 +    // or need a header ID
 +    ClassConfigurator.add(JGROUPS_TYPE_JGADDRESS, JGAddress.class);
 +    ClassConfigurator.addProtocol(JGROUPS_PROTOCOL_TRANSPORT, Transport.class);
 +  }
 +
 +  @Override
 +  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
 +  public void init(Services s) {
 +    this.services = s;
 +
 +    RemoteTransportConfig transport = services.getConfig().getTransport();
 +    DistributionConfig dc = services.getConfig().getDistributionConfig();
 +    
 +
 +    boolean b = dc.getEnableNetworkPartitionDetection();
 +    if (b) {
 +      if (!SocketCreator.FORCE_DNS_USE) {
 +        SocketCreator.resolve_dns = false;
 +      }
 +    }
 +    System.setProperty("jgroups.resolve_dns", String.valueOf(!b));
 +
 +    InputStream is= null;
 +
 +    String r = null;
 +    if (transport.isMcastEnabled()) {
 +      r = JGROUPS_MCAST_CONFIG_FILE_NAME;
 +    } else {
 +      r = DEFAULT_JGROUPS_TCP_CONFIG;
 +    }
 +    is = ClassPathLoader.getLatest().getResourceAsStream(getClass(), r);
 +    if (is == null) {
 +      throw new GemFireConfigException(LocalizedStrings.GroupMembershipService_CANNOT_FIND_0.toLocalizedString(r));
 +    }
 +
 +    String properties;
 +    try {
 +      //PlainConfigurator config = PlainConfigurator.getInstance(is);
 +      //properties = config.getProtocolStackString();
 +      StringBuffer sb = new StringBuffer(3000);
 +      BufferedReader br;
 +      br = new BufferedReader(new InputStreamReader(is, "US-ASCII"));
 +      String input;
 +      while ((input=br.readLine()) != null) {
 +        sb.append(input);
 +      }
 +      br.close();
 +      properties = sb.toString();
 +    }
 +    catch (Exception ex) {
 +      throw new GemFireConfigException(LocalizedStrings.GroupMembershipService_AN_EXCEPTION_WAS_THROWN_WHILE_READING_JGROUPS_CONFIG.toLocalizedString(), ex);
 +    }
 +    
 +    
 +    if (transport.isMcastEnabled()) {
 +      properties = replaceStrings(properties, "MCAST_PORT", String.valueOf(transport.getMcastId().getPort()));
 +      properties = replaceStrings(properties, "MCAST_ADDRESS", dc.getMcastAddress().getHostAddress());
 +      properties = replaceStrings(properties, "MCAST_TTL", String.valueOf(dc.getMcastTtl()));
 +      properties = replaceStrings(properties, "MCAST_SEND_BUFFER_SIZE", String.valueOf(dc.getMcastSendBufferSize()));
 +      properties = replaceStrings(properties, "MCAST_RECV_BUFFER_SIZE", String.valueOf(dc.getMcastRecvBufferSize()));
 +      properties = replaceStrings(properties, "MCAST_RETRANSMIT_INTERVAL", ""+Integer.getInteger("gemfire.mcast-retransmit-interval", 500));
 +      properties = replaceStrings(properties, "RETRANSMIT_LIMIT", String.valueOf(dc.getUdpFragmentSize()-256));
 +    }
 +
 +    if (transport.isMcastEnabled() || transport.isTcpDisabled() ||
 +        (dc.getUdpRecvBufferSize() != DistributionConfig.DEFAULT_UDP_RECV_BUFFER_SIZE) ) {
 +      properties = replaceStrings(properties, "UDP_RECV_BUFFER_SIZE", ""+dc.getUdpRecvBufferSize());
 +    }
 +    else {
 +      properties = replaceStrings(properties, "UDP_RECV_BUFFER_SIZE", ""+DistributionConfig.DEFAULT_UDP_RECV_BUFFER_SIZE_REDUCED);
 +    }
 +    properties = replaceStrings(properties, "UDP_SEND_BUFFER_SIZE", ""+dc.getUdpSendBufferSize());
 +
 +    String str = transport.getBindAddress();
 +    // JGroups UDP protocol requires a bind address
 +    if (str == null || str.length() == 0) {
 +      try {
 +        str = SocketCreator.getLocalHost().getHostAddress();
 +      } catch (UnknownHostException e) {
 +        throw new GemFireConfigException(e.getMessage(), e);
 +      }
 +    }
 +    properties = replaceStrings(properties, "BIND_ADDR_SETTING", "bind_addr=\""+str+"\"");
 +
 +    int port = Integer.getInteger("gemfire.jg-bind-port", 0);
 +    if (port != 0) {
 +      properties = replaceStrings(properties, "MEMBERSHIP_PORT_RANGE_START", ""+port);
 +    } else {
 +      int[] ports = dc.getMembershipPortRange();
 +      properties = replaceStrings(properties, "MEMBERSHIP_PORT_RANGE_START", ""+ports[0]);
 +      properties = replaceStrings(properties, "MEMBERSHIP_PORT_RANGE_END", ""+(ports[1]-ports[0]));
 +    }
 +    
 +    properties = replaceStrings(properties, "UDP_FRAGMENT_SIZE", ""+dc.getUdpFragmentSize());
 +    
 +    properties = replaceStrings(properties, "FC_MAX_CREDITS", ""+dc.getMcastFlowControl().getByteAllowance());
 +    properties = replaceStrings(properties, "FC_THRESHOLD", ""+dc.getMcastFlowControl().getRechargeThreshold());
 +    properties = replaceStrings(properties, "FC_MAX_BLOCK", ""+dc.getMcastFlowControl().getRechargeBlockMs());
 +
 +    this.jgStackConfig = properties;
 +    
 +  }
 +
 +  @Override
 +  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
 +  public void start() {
 +    // create the configuration XML string for JGroups
 +    String properties = this.jgStackConfig;
 +    
 +    long start = System.currentTimeMillis();
 +    
 +    // start the jgroups channel and establish the membership ID
 +    boolean reconnecting = false;
 +    try {
 +      Object oldChannel = services.getConfig().getTransport().getOldDSMembershipInfo();
 +      if (oldChannel != null) {
 +        logger.debug("Reusing JGroups channel from previous system", properties);
 +        
 +        myChannel = (JChannel)oldChannel;
 +        // scrub the old channel
 +        ViewId vid = new ViewId(new JGAddress(), 0);
 +        View jgv = new View(vid, new ArrayList<Address>());
 +        this.myChannel.down(new Event(Event.VIEW_CHANGE, jgv));
 +        UUID logicalAddress = (UUID)myChannel.getAddress();
 +        if (logicalAddress instanceof JGAddress) {
 +          ((JGAddress)logicalAddress).setVmViewId(-1);
 +        }
 +        reconnecting = true;
 +      }
 +      else {
 +        logger.debug("JGroups configuration: {}", properties);
 +        
 +        checkForIPv6();
 +        InputStream is = new ByteArrayInputStream(properties.getBytes("UTF-8"));
 +        myChannel = new JChannel(is);
 +      }
 +    } catch (Exception e) {
 +      throw new GemFireConfigException("unable to create jgroups channel", e);
 +    }
 +    
 +    // give the stats to the jchannel statistics recorder
 +    StatRecorder sr = (StatRecorder)myChannel.getProtocolStack().findProtocol(StatRecorder.class);
 +    if (sr != null) {
 +      sr.setDMStats(services.getStatistics());
 +    }
 +    
 +    Transport transport = (Transport)myChannel.getProtocolStack().getTransport();
 +    transport.setMessenger(this);
 +
 +    try {
 +      myChannel.setReceiver(null);
 +      myChannel.setReceiver(new JGroupsReceiver());
 +      if (!reconnecting) {
 +        myChannel.connect("AG"); // apache g***** (whatever we end up calling it)
 +      }
 +    } catch (Exception e) {
 +      myChannel.close();
 +      throw new SystemConnectException("unable to create jgroups channel", e);
 +    }
 +    
 +    if (JGroupsMessenger.THROW_EXCEPTION_ON_START_HOOK) {
 +      JGroupsMessenger.THROW_EXCEPTION_ON_START_HOOK = false;
 +      throw new SystemConnectException("failing for test");
 +    }
 +    
 +    establishLocalAddress();
 +    
 +    logger.info("JGroups channel {} (took {}ms)", (reconnecting? "reinitialized" : "created"), System.currentTimeMillis()-start);
 +    
 +  }
 +  
 +  /**
 +   * JGroups picks an IPv6 address if preferIPv4Stack is false or not set
 +   * and preferIPv6Addresses is not set or is true.  We want it to use an
 +   * IPv4 address for a dual-IP stack so that both IPv4 and IPv6 messaging work
 +   */
 +  private void checkForIPv6() throws Exception {
 +    boolean preferIpV6Addr = Boolean.getBoolean("java.net.preferIPv6Addresses");
 +    if (!preferIpV6Addr) {
 +      logger.debug("forcing JGroups to think IPv4 is being used so it will choose an IPv4 address");
 +      Field m = org.jgroups.util.Util.class.getDeclaredField("ip_stack_type");
 +      m.setAccessible(true);
 +      m.set(null, org.jgroups.util.StackType.IPv4);
 +    }
 +  }
 +
 +  @Override
 +  public void started() {
 +  }
 +
 +  @Override
 +  public void stop() {
 +    if (this.myChannel != null) {
 +      if ((services.isShutdownDueToForcedDisconnect() && services.isAutoReconnectEnabled()) || services.getManager().isReconnectingDS()) {
 +        // leave the channel open for reconnect attempts
 +      }
 +      else {
 +        this.myChannel.close();
 +      }
 +    }
 +  }
 +
 +  @Override
 +  public void stopped() {
 +  }
 +
 +  @Override
 +  public void memberSuspected(InternalDistributedMember initiator, InternalDistributedMember suspect, String reason) {
 +  }
 +
 +  @Override
 +  public void installView(NetView v) {
 +    this.view = v;
 +    
 +    if (this.jgAddress.getVmViewId() < 0) {
 +      this.jgAddress.setVmViewId(this.localAddress.getVmViewId());
 +    }
 +    List<JGAddress> mbrs = new ArrayList<JGAddress>(v.size());
 +    for (InternalDistributedMember idm: v.getMembers()) {
 +      mbrs.add(new JGAddress(idm));
 +    }
 +    ViewId vid = new ViewId(new JGAddress(v.getCoordinator()), v.getViewId());
 +    View jgv = new View(vid, new ArrayList<Address>(mbrs));
 +    logger.trace("installing JGroups view: {}", jgv);
 +    this.myChannel.down(new Event(Event.VIEW_CHANGE, jgv));
 +
 +    addressesWithioExceptionsProcessed.clear();
 +  }
 +  
 +
 +  /**
 +   * If JGroups is unable to send a message it may mean that the network
 +   * is down.  If so we need to initiate suspect processing on the
 +   * recipient.<p>
 +   * see Transport._send()
 +   */
 +  public void handleJGroupsIOException(IOException e, Address dest) {
 +    if (services.getManager().shutdownInProgress()) { // GEODE-634 - don't log IOExceptions during shutdown
 +      return;
 +    }
 +    if (addressesWithioExceptionsProcessed.contains(dest)) {
 +      return;
 +    }
 +    addressesWithioExceptionsProcessed.add(dest);
 +    NetView v = this.view;
 +    JGAddress jgMbr = (JGAddress)dest;
 +    if (jgMbr != null && v != null) {
 +      List<InternalDistributedMember> members = v.getMembers();
 +      InternalDistributedMember recipient = null;
 +      for (InternalDistributedMember mbr: members) {
 +        GMSMember gmsMbr = ((GMSMember)mbr.getNetMember());
 +        if (jgMbr.getUUIDLsbs() == gmsMbr.getUuidLSBs()
 +            && jgMbr.getUUIDMsbs() == gmsMbr.getUuidMSBs()
 +            && jgMbr.getVmViewId() == gmsMbr.getVmViewId()) {
 +          recipient = mbr;
 +          break;
 +        }
 +      }
 +      if (recipient != null) {
 +        services.getHealthMonitor().suspect(recipient,
 +            "Unable to send messages to this member via JGroups");
 +      }
 +    }
 +  }
 +  
 +  private void establishLocalAddress() {
 +    UUID logicalAddress = (UUID)myChannel.getAddress();
 +    logicalAddress = logicalAddress.copy();
 +    
 +    IpAddress ipaddr = (IpAddress)myChannel.down(new Event(Event.GET_PHYSICAL_ADDRESS));
 +    
 +    if (ipaddr != null) {
 +      this.jgAddress = new JGAddress(logicalAddress, ipaddr);
 +    }
 +    else {
 +      UDP udp = (UDP)myChannel.getProtocolStack().getTransport();
 +
 +      try {
 +        Method getAddress = UDP.class.getDeclaredMethod("getPhysicalAddress");
 +        getAddress.setAccessible(true);
 +        ipaddr = (IpAddress)getAddress.invoke(udp, new Object[0]);
 +        this.jgAddress = new JGAddress(logicalAddress, ipaddr);
 +      } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
 +        logger.info("Unable to find getPhysicallAddress method in UDP - parsing its address instead");
 +      }
 +      
 +//      if (this.jgAddress == null) {
 +//        String addr = udp.getLocalPhysicalAddress();
 +//        int cidx = addr.lastIndexOf(':');  // IPv6 literals might have colons
 +//        String host = addr.substring(0, cidx);
 +//        int jgport = Integer.parseInt(addr.substring(cidx+1, addr.length()));
 +//        try {
 +//          this.jgAddress = new JGAddress(logicalAddress, new IpAddress(InetAddress.getByName(host), jgport));
 +//        } catch (UnknownHostException e) {
 +//          myChannel.disconnect();
 +//          throw new SystemConnectException("unable to initialize jgroups address", e);
 +//        }
 +//      }
 +    }
 +  
 +    // install the address in the JGroups channel protocols
 +    myChannel.down(new Event(Event.SET_LOCAL_ADDRESS, this.jgAddress));
 +
 +    DistributionConfig config = services.getConfig().getDistributionConfig();
 +    boolean isLocator = (services.getConfig().getTransport().getVmKind() == DistributionManager.LOCATOR_DM_TYPE)
 +        || !services.getConfig().getDistributionConfig().getStartLocator().isEmpty();
 +    
 +    // establish the DistributedSystem's address
 +    DurableClientAttributes dca = null;
 +    if (config.getDurableClientId() != null) {
 +      dca = new DurableClientAttributes(config.getDurableClientId(), config
 +          .getDurableClientTimeout());
 +    }
 +    MemberAttributes attr = new MemberAttributes(
 +        -1/*dcPort - not known at this time*/,
 +        OSProcess.getId(),
 +        services.getConfig().getTransport().getVmKind(),
 +        -1/*view id - not known at this time*/,
 +        config.getName(),
 +        MemberAttributes.parseGroups(config.getRoles(), config.getGroups()),
 +        dca);
 +    localAddress = new InternalDistributedMember(jgAddress.getInetAddress(),
 +        jgAddress.getPort(), config.getEnableNetworkPartitionDetection(),
 +        isLocator, attr);
 +
 +    // add the JGroups logical address to the GMSMember
 +    UUID uuid = this.jgAddress;
 +    ((GMSMember)localAddress.getNetMember()).setUUID(uuid);
 +    ((GMSMember)localAddress.getNetMember()).setMemberWeight((byte)(services.getConfig().getMemberWeight() & 0xff));
 +  }
 +  
 +  @Override
 +  public void beSick() {
 +  }
 +
 +  @Override
 +  public void playDead() {
 +  }
 +
 +  @Override
 +  public void beHealthy() {
 +  }
 +
 +  @Override
 +  public void addHandler(Class c, MessageHandler h) {
 +    handlers.put(c, h);
 +  }
 +  
 +  @Override
 +  public boolean testMulticast(long timeout) throws InterruptedException {
 +    long pongsSnapshot = pongsReceived.longValue();
 +    JGAddress dest = null;
 +    try {
 +      pingPonger.sendPingMessage(myChannel, jgAddress, dest);
 +    } catch (Exception e) {
 +      logger.warn("unable to send multicast message: {}", (jgAddress==null? "multicast recipients":jgAddress),
 +          e.getMessage());
 +      return false;
 +    }
 +    long giveupTime = System.currentTimeMillis() + timeout;
 +    while (pongsReceived.longValue() == pongsSnapshot && System.currentTimeMillis() < giveupTime) {
 +      Thread.sleep(100);
 +    }
 +    return pongsReceived.longValue() > pongsSnapshot;
 +  }
 +  
 +  @Override
 +  public void getMessageState(InternalDistributedMember target, Map state, boolean includeMulticast) {
 +    if (includeMulticast) {
 +      NAKACK2 nakack = (NAKACK2)myChannel.getProtocolStack().findProtocol("NAKACK2");
 +      if (nakack != null) {
 +        long seqno = nakack.getCurrentSeqno();
 +        state.put("JGroups.mcastState", Long.valueOf(seqno));
 +      }
 +    }
 +  }
 +  
 +  @Override
 +  public void waitForMessageState(InternalDistributedMember sender, Map state) throws InterruptedException {
 +    NAKACK2 nakack = (NAKACK2)myChannel.getProtocolStack().findProtocol("NAKACK2");
 +    Long seqno = (Long)state.get("JGroups.mcastState");
 +    if (nakack != null && seqno != null) {
 +      waitForMessageState(nakack, sender, seqno);
 +    }
 +  }
 +  
 +  /**
 +   * wait for the mcast state from the given member to reach the given seqno 
 +   */
 +  protected void waitForMessageState(NAKACK2 nakack, InternalDistributedMember sender, Long seqno)
 +    throws InterruptedException {
 +    long timeout = services.getConfig().getDistributionConfig().getAckWaitThreshold() * 1000L;
 +    long startTime = System.currentTimeMillis();
 +    long warnTime = startTime + timeout;
 +    long quitTime = warnTime + timeout - 1000L;
 +    boolean warned = false;
 +    
 +    JGAddress jgSender = new JGAddress(sender);
 +    
 +    for (;;) {
 +      Digest digest = nakack.getDigest(jgSender);
 +      if (digest == null) {
 +        return;
 +      }
 +      String received = "none";
 +      long[] senderSeqnos = digest.get(jgSender);
 +      if (senderSeqnos == null || senderSeqnos[0] >= seqno.longValue()) {
 +        break;
 +      }
 +      long now = System.currentTimeMillis();
 +      if (!warned && now >= warnTime) {
 +        warned = true;
 +        if (senderSeqnos != null) {
 +          received = String.valueOf(senderSeqnos[0]);
 +        }
 +        logger.warn("{} seconds have elapsed while waiting for multicast messages from {}.  Received {} but expecting at least {}.",
 +            Long.toString((warnTime-startTime)/1000L), sender, received, seqno);
 +      }
 +      if (now >= quitTime) {
 +        throw new GemFireIOException("Multicast operations from " + sender + " did not distribute within " + (now - startTime) + " milliseconds");
 +      }
 +      Thread.sleep(50);
 +    }
 +  }
 +
 +  @Override
 +  public Set<InternalDistributedMember> sendUnreliably(DistributionMessage msg) {
 +    return send(msg, false);
 +  }
 +    
 +  @Override
 +  public Set<InternalDistributedMember> send(DistributionMessage msg) {
 +    return send(msg, true);
 +  }
 +    
 +  public Set<InternalDistributedMember> send(DistributionMessage msg, boolean reliably) {
 +      
 +    // perform the same jgroups messaging as in 8.2's GMSMembershipManager.send() method
 +
 +    // BUT: when marshalling messages we need to include the version of the product and
 +    // localAddress at the beginning of the message.  These should be used in the receiver
 +    // code to create a versioned input stream, read the sender address, then read the message
 +    // and set its sender address
 +    DMStats theStats = services.getStatistics();
 +    NetView oldView = this.view;
 +
 +    if (!myChannel.isConnected()) {
 +      logger.info("JGroupsMessenger channel is closed - messaging is not possible");
 +      throw new DistributedSystemDisconnectedException("Distributed System is shutting down");
 +    }
 +    
 +    filterOutgoingMessage(msg);
 +
 +    // JGroupsMessenger does not support direct-replies, so register
 +    // the message's processor if necessary
 +    if ((msg instanceof DirectReplyMessage) && msg.isDirectAck() && msg.getProcessorId() <= 0) {
 +      ((DirectReplyMessage)msg).registerProcessor();
 +    }
 +
 +    InternalDistributedMember[] destinations = msg.getRecipients();
 +    boolean allDestinations = msg.forAll();
 +    
 +    boolean useMcast = false;
 +    if (services.getConfig().getTransport().isMcastEnabled()) {
 +      if (msg.getMulticast() || allDestinations) {
 +        useMcast = services.getManager().isMulticastAllowed();
 +      }
 +    }
 +    
 +    if (logger.isDebugEnabled() && reliably) {
 +      String recips = useMcast? "multicast" : Arrays.toString(msg.getRecipients());
 +      logger.debug("sending via JGroups: [{}] recipients: {}", msg, recips);
 +    }
 +    
 +    JGAddress local = this.jgAddress;
 +    
 +    if (useMcast) {
 +
 +      long startSer = theStats.startMsgSerialization();
 +      Message jmsg = createJGMessage(msg, local, Version.CURRENT_ORDINAL);
 +      theStats.endMsgSerialization(startSer);
 +
 +      Exception problem = null;
 +      try {
 +        jmsg.setTransientFlag(TransientFlag.DONT_LOOPBACK);
 +        if (!reliably) {
 +          jmsg.setFlag(Message.Flag.NO_RELIABILITY);
 +        }
 +        theStats.incSentBytes(jmsg.getLength());
 +        logger.trace("Sending JGroups message: {}", jmsg);
 +        myChannel.send(jmsg);
 +      }
 +      catch (Exception e) {
 +        logger.debug("caught unexpected exception", e);
 +        Throwable cause = e.getCause();
 +        if (cause instanceof ForcedDisconnectException) {
 +          problem = (Exception) cause;
 +        } else {
 +          problem = e;
 +        }
 +        if (services.getShutdownCause() != null) {
 +          Throwable shutdownCause = services.getShutdownCause();
 +          // If ForcedDisconnectException occurred then report it as actual
 +          // problem.
 +          if (shutdownCause instanceof ForcedDisconnectException) {
 +            problem = (Exception) shutdownCause;
 +          } else {
 +            Throwable ne = problem;
 +            while (ne.getCause() != null) {
 +              ne = ne.getCause();
 +            }
 +            ne.initCause(services.getShutdownCause());
 +          }
 +        }
 +        final String channelClosed = LocalizedStrings.GroupMembershipService_CHANNEL_CLOSED.toLocalizedString();
 +//        services.getManager().membershipFailure(channelClosed, problem);
 +        throw new DistributedSystemDisconnectedException(channelClosed, problem);
 +      }
 +    } // useMcast
 +    else { // ! useMcast
 +      int len = destinations.length;
 +      List<GMSMember> calculatedMembers; // explicit list of members
 +      int calculatedLen; // == calculatedMembers.len
 +      if (len == 1 && destinations[0] == DistributionMessage.ALL_RECIPIENTS) { // send to all
 +        // Grab a copy of the current membership
 +        NetView v = services.getJoinLeave().getView();
 +
 +        // Construct the list
 +        calculatedLen = v.size();
 +        calculatedMembers = new LinkedList<GMSMember>();
 +        for (int i = 0; i < calculatedLen; i ++) {
 +          InternalDistributedMember m = (InternalDistributedMember)v.get(i);
 +          calculatedMembers.add((GMSMember)m.getNetMember());
 +        }
 +      } // send to all
 +      else { // send to explicit list
 +        calculatedLen = len;
 +        calculatedMembers = new LinkedList<GMSMember>();
 +        for (int i = 0; i < calculatedLen; i ++) {
 +          calculatedMembers.add((GMSMember)destinations[i].getNetMember());
 +        }
 +      } // send to explicit list
 +      Int2ObjectOpenHashMap<Message> messages = new Int2ObjectOpenHashMap<>();
 +      long startSer = theStats.startMsgSerialization();
 +      boolean firstMessage = true;
 +      for (Iterator<GMSMember> it=calculatedMembers.iterator(); it.hasNext(); ) {
 +        GMSMember mbr = it.next();
 +        short version = mbr.getVersionOrdinal();
 +        if ( !messages.containsKey(version) ) {
 +          Message jmsg = createJGMessage(msg, local, version);
 +          messages.put(version, jmsg);
 +          if (firstMessage) {
 +            theStats.incSentBytes(jmsg.getLength());
 +            firstMessage = false;
 +          }
 +        }
 +      }
 +      theStats.endMsgSerialization(startSer);
 +      Collections.shuffle(calculatedMembers);
 +      int i=0;
 +      for (GMSMember mbr: calculatedMembers) {
 +        JGAddress to = new JGAddress(mbr);
 +        short version = mbr.getVersionOrdinal();
 +        Message jmsg = (Message)messages.get(version);
 +        Exception problem = null;
 +        try {
 +          Message tmp = (i < (calculatedLen-1)) ? jmsg.copy(true) : jmsg;
 +          if (!reliably) {
 +            jmsg.setFlag(Message.Flag.NO_RELIABILITY);
 +          }
 +          tmp.setDest(to);
 +          tmp.setSrc(this.jgAddress);
 +          logger.trace("Unicasting to {}", to);
 +          myChannel.send(tmp);
 +        }
 +        catch (Exception e) {
 +          problem = e;
 +        }
 +        if (problem != null) {
 +          Throwable cause = services.getShutdownCause();
 +          if (cause != null) {
 +            // If ForcedDisconnectException occurred then report it as actual
 +            // problem.
 +            if (cause instanceof ForcedDisconnectException) {
 +              problem = (Exception) cause;
 +            } else {
 +              Throwable ne = problem;
 +              while (ne.getCause() != null) {
 +                ne = ne.getCause();
 +              }
 +              ne.initCause(cause);
 +            }
 +          }
 +          final String channelClosed = LocalizedStrings.GroupMembershipService_CHANNEL_CLOSED.toLocalizedString();
 +          //          services.getManager().membershipFailure(channelClosed, problem);
 +          throw new DistributedSystemDisconnectedException(channelClosed, problem);
 +        }
 +      } // send individually
 +    } // !useMcast
 +
 +    // The contract is that every destination enumerated in the
 +    // message should have received the message.  If one left
 +    // (i.e., left the view), we signal it here.
 +    if (msg.forAll()) {
 +      return Collections.emptySet();
 +    }
 +    Set<InternalDistributedMember> result = new HashSet<InternalDistributedMember>();
 +    NetView newView = this.view;
 +    if (newView != null && newView != oldView) {
 +      for (int i = 0; i < destinations.length; i ++) {
 +        InternalDistributedMember d = destinations[i];
 +        if (!newView.contains(d)) {
 +          logger.debug("messenger: member has left the view: {}  view is now {}", d, newView);
 +          result.add(d);
 +        }
 +      }
 +    }
 +    return result;
 +  }
 +
 +  /**
 +   * This is the constructor to use to create a JGroups message holding a GemFire
 +   * DistributionMessage.  It sets the appropriate flags in the Message and properly
 +   * serializes the DistributionMessage for the recipient's product version
 +   * 
 +   * @param gfmsg the DistributionMessage
 +   * @param src the sender address
 +   * @param version the version of the recipient
 +   * @return the new message
 +   */
 +  Message createJGMessage(DistributionMessage gfmsg, JGAddress src, short version) {
 +    if(gfmsg instanceof DirectReplyMessage) {
 +      ((DirectReplyMessage) gfmsg).registerProcessor();
 +    }
 +    Message msg = new Message();
 +    msg.setDest(null);
 +    msg.setSrc(src);
 +    // GemFire uses its own reply processors so there is no need
 +    // to maintain message order
 +    msg.setFlag(Flag.OOB);
 +    // Bundling is mostly only useful if we're doing no-ack work,
 +    // which is fairly rare
 +    msg.setFlag(Flag.DONT_BUNDLE);
 +
 +    //log.info("Creating message with payload " + gfmsg);
 +    if (gfmsg.getProcessorType() == DistributionManager.HIGH_PRIORITY_EXECUTOR
 +        || gfmsg instanceof HighPriorityDistributionMessage) {
 +      msg.setFlag(Flag.NO_FC);
 +      msg.setFlag(Flag.SKIP_BARRIER);
 +    }
 +    if (gfmsg instanceof DistributedCacheOperation.CacheOperationMessage) {
 +      // we don't want to see our own cache operation messages
 +      msg.setTransientFlag(Message.TransientFlag.DONT_LOOPBACK);
 +    }
 +    try {
 +      long start = services.getStatistics().startMsgSerialization();
 +      HeapDataOutputStream out_stream =
 +        new HeapDataOutputStream(Version.fromOrdinalOrCurrent(version));
 +      Version.CURRENT.writeOrdinal(out_stream, true);
 +      DataSerializer.writeObject(this.localAddress.getNetMember(), out_stream);
 +      DataSerializer.writeObject(gfmsg, out_stream);
 +      msg.setBuffer(out_stream.toByteArray());
 +      services.getStatistics().endMsgSerialization(start);
 +    }
 +    catch(IOException | GemFireIOException ex) {
 +      logger.warn("Error serializing message", ex);
 +      if (ex instanceof GemFireIOException) {
 +        throw (GemFireIOException)ex;
 +      } else {
 +        GemFireIOException ioe = new
 +          GemFireIOException("Error serializing message");
 +        ioe.initCause(ex);
 +        throw ioe;
 +      }
 +    }
 +    return msg;
 +  }
 +
 +
 +  /**
 +   * deserialize a jgroups payload.  If it's a DistributionMessage find
 +   * the ID of the sender and establish it as the message's sender
 +   */
 +  Object readJGMessage(Message jgmsg) {
 +    Object result = null;
 +    
 +    int messageLength = jgmsg.getLength();
 +    
 +    if (logger.isTraceEnabled()) {
 +      logger.trace("deserializing a message of length "+messageLength);
 +    }
 +    
 +    if (messageLength == 0) {
 +      // jgroups messages with no payload are used for protocol interchange, such
 +      // as STABLE_GOSSIP
 +      logger.trace("message length is zero - ignoring");
 +      return null;
 +    }
 +
 +    InternalDistributedMember sender = null;
 +
 +    Exception problem = null;
 +    byte[] buf = jgmsg.getRawBuffer();
 +    try {
 +      long start = services.getStatistics().startMsgDeserialization();
 +      
 +      DataInputStream dis = new DataInputStream(new ByteArrayInputStream(buf, 
 +          jgmsg.getOffset(), jgmsg.getLength()));
 +
 +      short ordinal = Version.readOrdinal(dis);
 +      
 +      if (ordinal < Version.CURRENT_ORDINAL) {
 +        dis = new VersionedDataInputStream(dis, Version.fromOrdinalNoThrow(
 +            ordinal, true));
 +      }
 +      
 +      GMSMember m = DataSerializer.readObject(dis);
 +
 +      result = DataSerializer.readObject(dis);
 +
 +      DistributionMessage dm = (DistributionMessage)result;
 +      
 +      // JoinRequestMessages are sent with an ID that may have been
 +      // reused from a previous life by way of auto-reconnect,
 +      // so we don't want to find a canonical reference for the
 +      // request's sender ID
 +      if (dm.getDSFID() == JOIN_REQUEST) {
 +        sender = ((JoinRequestMessage)dm).getMemberID();
 +      } else {
 +        sender = getMemberFromView(m, ordinal);
 +      }
 +      ((DistributionMessage)result).setSender(sender);
 +      
 +      services.getStatistics().endMsgDeserialization(start);
 +    }
 +    catch (ClassNotFoundException | IOException | RuntimeException e) {
 +      problem = e;
 +    }
 +    if (problem != null) {
 +      logger.error(LocalizedMessage.create(
 +            LocalizedStrings.GroupMembershipService_EXCEPTION_DESERIALIZING_MESSAGE_PAYLOAD_0, jgmsg), problem);
 +      return null;
 +    }
 +
 +    return result;
 +  }
 +  
 +  
 +  /** look for certain messages that may need to be altered before being sent */
 +  void filterOutgoingMessage(DistributionMessage m) {
 +    switch (m.getDSFID()) {
 +    case JOIN_RESPONSE:
 +      JoinResponseMessage jrsp = (JoinResponseMessage)m;
 +      
 +      if (jrsp.getRejectionMessage() == null
 +          &&  services.getConfig().getTransport().isMcastEnabled()) {
 +        // get the multicast message digest and pass it with the join response
 +        Digest digest = (Digest)this.myChannel.getProtocolStack()
 +            .getTopProtocol().down(Event.GET_DIGEST_EVT);
 +        HeapDataOutputStream hdos = new HeapDataOutputStream(500, Version.CURRENT);
 +        try {
 +          digest.writeTo(hdos);
 +        } catch (Exception e) {
 +          logger.fatal("Unable to serialize JGroups messaging digest", e);
 +        }
 +        jrsp.setMessengerData(hdos.toByteArray());
 +      }
 +      break;
 +    default:
 +      break;
 +    }
 +  }
 +  
 +  void filterIncomingMessage(DistributionMessage m) {
 +    switch (m.getDSFID()) {
 +    case JOIN_RESPONSE:
 +      JoinResponseMessage jrsp = (JoinResponseMessage)m;
 +      
 +      if (jrsp.getRejectionMessage() == null
 +          &&  services.getConfig().getTransport().isMcastEnabled()) {
 +        byte[] serializedDigest = jrsp.getMessengerData();
 +        ByteArrayInputStream bis = new ByteArrayInputStream(serializedDigest);
 +        DataInputStream dis = new DataInputStream(bis);
 +        try {
 +          Digest digest = new Digest();
 +          digest.readFrom(dis);
 +          if (digest != null) {
 +            logger.trace("installing JGroups message digest {}", digest);
 +            this.myChannel.getProtocolStack()
 +                .getTopProtocol().down(new Event(Event.SET_DIGEST, digest));
 +            jrsp.setMessengerData(null);
 +          }
 +        } catch (Exception e) {
 +          logger.fatal("Unable to read JGroups messaging digest", e);
 +        }
 +      }
 +      break;
 +    default:
 +      break;
 +    }
 +  }
 +  
 +  @Override
 +  public InternalDistributedMember getMemberID() {
 +    return localAddress;
 +  }
 +  
 +  /**
 +   * returns the JGroups configuration string, for testing
 +   */
 +  public String getJGroupsStackConfig() {
 +    return this.jgStackConfig;
 +  }
 +  
 +  /**
 +   * returns the pinger, for testing
 +   */
 +  public GMSPingPonger getPingPonger() {
 +    return this.pingPonger;
 +  }
 +  
 +  /**
 +   * for unit testing we need to replace UDP with a fake UDP protocol 
 +   */
 +  public void setJGroupsStackConfigForTesting(String config) {
 +    this.jgStackConfig = config;
 +  }
 +
 +  /**
 +   * returns the member ID for the given GMSMember object
 +   */
 +  private InternalDistributedMember getMemberFromView(GMSMember jgId, short version) {
 +    NetView v = services.getJoinLeave().getView();
 +    
 +    if (v != null) {
 +      for (InternalDistributedMember m: v.getMembers()) {
 +        if (((GMSMember)m.getNetMember()).equals(jgId)) {
 +          return m;
 +        }
 +      }
 +    }
 +    return new InternalDistributedMember(jgId);
 +  }
 +
 +
 +  @Override
 +  public void emergencyClose() {
 +    this.view = null;
 +    if (this.myChannel != null) {
 +      if ((services.isShutdownDueToForcedDisconnect() && services.isAutoReconnectEnabled()) || services.getManager().isReconnectingDS()) {
 +      }
 +      else {
 +        this.myChannel.disconnect();
 +      }
 +    }
 +  }
 +  
 +  public QuorumChecker getQuorumChecker() {    
 +    NetView view = this.view;
 +    if (view == null) {
 +      view = services.getJoinLeave().getView();
 +      if (view == null) {
 +        view = services.getJoinLeave().getPreviousView();
 +        if (view == null) {
 +          return null;
 +        }
 +      }
 +    }
 +    GMSQuorumChecker qc = new GMSQuorumChecker(
 +          view, services.getConfig().getLossThreshold(),
 +          this.myChannel);
 +    qc.initialize();
 +    return qc;
 +  }
 +  /**
 +   * JGroupsReceiver receives incoming JGroups messages and passes them to a handler.
 +   * It may be accessed through JChannel.getReceiver().
 +   */
 +  class JGroupsReceiver extends ReceiverAdapter  {
 +  
 +    @Override
 +    public void receive(Message jgmsg) {
-       if (services.getManager().shutdownInProgress()) {
-         return;
-       }
- 
-       if (logger.isTraceEnabled()) {
-         logger.trace("JGroupsMessenger received {} headers: {}", jgmsg, jgmsg.getHeaders());
-       }
-       
-       //Respond to ping messages sent from other systems that are in a auto reconnect state
-       byte[] contents = jgmsg.getBuffer();
-       if (contents == null) {
-         return;
-       }
-       if (pingPonger.isPingMessage(contents)) {
++      long startTime = DistributionStats.getStatTime();
++      try {
++        if (services.getManager().shutdownInProgress()) {
++          return;
++        }
++  
++        if (logger.isTraceEnabled()) {
++          logger.trace("JGroupsMessenger received {} headers: {}", jgmsg, jgmsg.getHeaders());
++        }
++        
++        //Respond to ping messages sent from other systems that are in a auto reconnect state
++        byte[] contents = jgmsg.getBuffer();
++        if (contents == null) {
++          return;
++        }
++        if (pingPonger.isPingMessage(contents)) {
++          try {
++            pingPonger.sendPongMessage(myChannel, jgAddress, jgmsg.getSrc());
++          }
++          catch (Exception e) {
++            logger.info("Failed sending Pong response to " + jgmsg.getSrc());
++          }
++          return;
++        } else if (pingPonger.isPongMessage(contents)) {
++          pongsReceived.incrementAndGet();
++          return;
++        }
++        
++        Object o = readJGMessage(jgmsg);
++        if (o == null) {
++          return;
++        }
++  
++        DistributionMessage msg = (DistributionMessage)o;
++        assert msg.getSender() != null;
++        
++        // admin-only VMs don't have caches, so we ignore cache operations
++        // multicast to them, avoiding deserialization cost and classpath
++        // problems
++        if ( (services.getConfig().getTransport().getVmKind() == DistributionManager.ADMIN_ONLY_DM_TYPE)
++             && (msg instanceof DistributedCacheOperation.CacheOperationMessage)) {
++          return;
++        }
++  
++        msg.resetTimestamp();
++        msg.setBytesRead(jgmsg.getLength());
++              
 +        try {
-           pingPonger.sendPongMessage(myChannel, jgAddress, jgmsg.getSrc());
++          logger.trace("JGroupsMessenger dispatching {} from {}", msg, msg.getSender());
++          filterIncomingMessage(msg);
++          getMessageHandler(msg).processMessage(msg);
 +        }
-         catch (Exception e) {
-           logger.info("Failed sending Pong response to " + jgmsg.getSrc());
++        catch (MemberShunnedException e) {
++          // message from non-member - ignore
 +        }
-         return;
-       } else if (pingPonger.isPongMessage(contents)) {
-         pongsReceived.incrementAndGet();
-         return;
-       }
-       
-       Object o = readJGMessage(jgmsg);
-       if (o == null) {
-         return;
-       }
- 
-       DistributionMessage msg = (DistributionMessage)o;
-       assert msg.getSender() != null;
-       
-       // admin-only VMs don't have caches, so we ignore cache operations
-       // multicast to them, avoiding deserialization cost and classpath
-       // problems
-       if ( (services.getConfig().getTransport().getVmKind() == DistributionManager.ADMIN_ONLY_DM_TYPE)
-            && (msg instanceof DistributedCacheOperation.CacheOperationMessage)) {
-         return;
-       }
- 
-       msg.resetTimestamp();
-       msg.setBytesRead(jgmsg.getLength());
-             
-       try {
-         logger.trace("JGroupsMessenger dispatching {} from {}", msg, msg.getSender());
-         filterIncomingMessage(msg);
-         getMessageHandler(msg).processMessage(msg);
-       }
-       catch (MemberShunnedException e) {
-         // message from non-member - ignore
++      }finally {
++        long delta = DistributionStats.getStatTime() - startTime ;
++        JGroupsMessenger.this.services.getStatistics().incUDPDispatchRequestTime(delta);
 +      }
 +    }
 +    
 +    /**
 +     * returns the handler that should process the given message.
 +     * The default handler is the membership manager
 +     * @param msg
 +     * @return
 +     */
 +    private MessageHandler getMessageHandler(DistributionMessage msg) {
 +      Class<?> msgClazz = msg.getClass();
 +      MessageHandler h = handlers.get(msgClazz);
 +      if (h == null) {
 +        for (Class<?> clazz: handlers.keySet()) {
 +          if (clazz.isAssignableFrom(msgClazz)) {
 +            h = handlers.get(clazz);
 +            handlers.put(msg.getClass(), h);
 +            break;
 +          }
 +        }
 +      }
 +      if (h == null) {
 +        h = (MessageHandler)services.getManager();
 +      }
 +      return h;
 +    }
 +  }
 +  
 +  
 +  
 +}



[14/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache/query/dunit/QueryUsingPoolDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache/query/dunit/QueryUsingPoolDUnitTest.java
index d1e42d3,0000000..4df1f81
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache/query/dunit/QueryUsingPoolDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache/query/dunit/QueryUsingPoolDUnitTest.java
@@@ -1,2588 -1,0 +1,2588 @@@
 +/*
 + * 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.cache.query.dunit;
 +
 +import java.io.DataInput;
 +import java.io.DataOutput;
 +import java.io.IOException;
 +import java.util.Comparator;
 +import java.util.Iterator;
 +import java.util.Properties;
 +import java.util.Set;
 +
 +import cacheRunner.Portfolio;
 +import cacheRunner.Position;
 +
 +import com.gemstone.gemfire.DataSerializable;
 +import com.gemstone.gemfire.DataSerializer;
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.client.PoolFactory;
 +import com.gemstone.gemfire.cache.client.PoolManager;
 +import com.gemstone.gemfire.cache.query.*;
 +import com.gemstone.gemfire.cache.query.internal.DefaultQuery;
 +import com.gemstone.gemfire.cache.query.internal.QueryObserverAdapter;
 +import com.gemstone.gemfire.cache.query.internal.QueryObserverHolder;
 +import com.gemstone.gemfire.cache.query.types.ObjectType;
 +import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.cache30.ClientServerTestCase;
 +import com.gemstone.gemfire.cache30.CacheSerializableRunnable;
 +import com.gemstone.gemfire.cache30.CacheTestCase;
 +import com.gemstone.gemfire.distributed.DistributedSystem;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.internal.AvailablePort;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.CacheClientNotifier;
 +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.IgnoredException;
 +import com.gemstone.gemfire.test.dunit.LogWriterUtils;
 +import com.gemstone.gemfire.test.dunit.NetworkUtils;
 +import com.gemstone.gemfire.test.dunit.SerializableRunnable;
 +import com.gemstone.gemfire.test.dunit.VM;
 +import com.gemstone.gemfire.test.dunit.Wait;
 +
 +/**
 + * Tests remote (client/server) query execution.
 + *
 + * @author Barry Oglesby
 + * @author Asif
 + * @since 5.0.1
 + */
 +public class QueryUsingPoolDUnitTest extends CacheTestCase {
 +
 +  /** The port on which the bridge server was started in this VM */
 +  private static int bridgeServerPort;
 +  
 +  final String rootRegionName = "root";
 +  
 +  private final String regionName = this.getName();
 +  
 +  private final String regName = "/" + rootRegionName + "/" + regionName;
 +  
 +  // Used with compiled queries.
 +  private final String[] queryString = new String[] {
 +      "SELECT itr.value FROM " + regName + ".entries itr where itr.key = $1", // 0
 +      "SELECT DISTINCT * FROM " + regName + " WHERE id < $1 ORDER BY   id", // 1
 +      "SELECT DISTINCT * FROM " + regName + " WHERE id < $1 ORDER BY id", // 2
 +      "(SELECT DISTINCT * FROM " + regName + " WHERE id < $1).size", // 3
 +      "SELECT * FROM " + regName + " WHERE id = $1 and Ticker = $2", // 4
 +      "SELECT * FROM " + regName + " WHERE id < $1 and Ticker = $2", // 5
 +  };
 +
 +  /**
 +   * Creates a new <code>GemFireMemberStatusDUnitTest</code>
 +   */
 +  public QueryUsingPoolDUnitTest(String name) {
 +    super(name);
 +  }
 +
 +  ////////  Test Methods
 +
 +  @Override
 +  public void setUp() throws Exception {
 +    super.setUp();
 +    disconnectAllFromDS();
 +    IgnoredException.addIgnoredException("Connection reset");
 +    IgnoredException.addIgnoredException("Socket input is shutdown");
 +  }
 +
 +  @Override
 +  protected final void postTearDownCacheTestCase() throws Exception {
 +    disconnectAllFromDS();
 +  }
 +
 +  public void createPool(VM vm, String poolName, String server, int port, boolean subscriptionEnabled) {
 +    createPool(vm, poolName, new String[]{server}, new int[]{port}, subscriptionEnabled);  
 +  }
 +
 +  public void createPool(VM vm, String poolName, String server, int port) {
 +    createPool(vm, poolName, new String[]{server}, new int[]{port}, false);  
 +  }
 +
 +  public void createPool(VM vm, final String poolName, final String[] servers, final int[] ports,
 +      final boolean subscriptionEnabled) {
 +    vm.invoke(new CacheSerializableRunnable("createPool :" + poolName) {
 +      public void run2() throws CacheException {
 +        // Create Cache.
 +        getCache();
 +
 +        PoolFactory cpf = PoolManager.createFactory();
 +        cpf.setSubscriptionEnabled(subscriptionEnabled);
 +        for (int i=0; i < servers.length; i++){
 +          LogWriterUtils.getLogWriter().info("### Adding to Pool. ### Server : " + servers[i] + " Port : " + ports[i]);
 +          cpf.addServer(servers[i], ports[i]);
 +        }
 +
 +        cpf.create(poolName);
 +      }
 +    });   
 +  }
 +
 +  public void validateCompiledQuery(VM vm, final long compiledQueryCount) {
 +  vm.invoke(new CacheSerializableRunnable("validate compiled query.") {
 +    public void run2() throws CacheException {
 +      long count = 0;
 +      for (int i=0; i < 100; i++) {
 +        count = CacheClientNotifier.getInstance().getStats().getCompiledQueryCount();
 +        if (count == compiledQueryCount){
 +          break;
 +        } else {
 +          Wait.pause(1 * 100);
 +        }
 +      }
 +      assertEquals(compiledQueryCount, count);
 +    }
 +  });
 + }
 +  
 +  /**
 +   * Tests remote import query execution.
 +   */
 +  public void testRemoteImportQueries() throws CacheException {
 +
 +    final String name = this.getName();
 +    final String rootRegionName = "root";
 +
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    final int numberOfEntries = 100;
 +
 +    // Start server
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        Properties config = new Properties();
 +        config.setProperty("locators", "localhost["+DistributedTestUtils.getDUnitLocatorPort()+"]");
 +        system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        createRegion(name, rootRegionName, factory.create());
 +        Wait.pause(1000);
 +        try {
 +          startBridgeServer(0, false);
 +        } catch (Exception ex) {
 +          Assert.fail("While starting CacheServer", ex);
 +        }
 +      }
 +    });
 +
 +    // Initialize server region
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion().getSubregion(name);
 +        for (int i=0; i<numberOfEntries; i++) {
 +          region.put("key-"+i, new TestObject(i, "ibm"));
 +        }
 +      }
 +    });
 +
 +
-     final int port = vm0.invokeInt(QueryUsingPoolDUnitTest.class, "getCacheServerPort");
++    final int port = vm0.invoke(() -> QueryUsingPoolDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +    final String regionName = "/" + rootRegionName + "/" + name;
 +
 +    // Create client pool.
 +    final String poolName = "testRemoteImportQueries"; 
 +    createPool(vm1, poolName, host0, port);
 +
 +    // Execute client queries
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        String queryString = null;
 +        SelectResults results = null;
 +
 +        QueryService qService = null;
 +
 +        try {
 +          qService = (PoolManager.find(poolName)).getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        }          
 +
 +        queryString = "import com.gemstone.gemfire.admin.QueryUsingPoolDUnitTest.TestObject; select distinct * from " + regionName;
 +
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }          
 +
 +        assertEquals(numberOfEntries, results.size());
 +        assertTrue(!results.getCollectionType().allowsDuplicates());
 +
 +        queryString = "import com.gemstone.gemfire.admin.QueryUsingPoolDUnitTest.TestObject; select distinct * from " + regionName + " where ticker = 'ibm'";
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(numberOfEntries, results.size());
 +        assertTrue(!results.getCollectionType().allowsDuplicates());
 +
 +        queryString = "import com.gemstone.gemfire.admin.QueryUsingPoolDUnitTest.TestObject; select distinct * from " + regionName + " where ticker = 'IBM'";
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(0, results.size());
 +        assertTrue(!results.getCollectionType().allowsDuplicates());
 +
 +        queryString = "import com.gemstone.gemfire.admin.QueryUsingPoolDUnitTest.TestObject; select distinct * from " + regionName + " where price > 49";
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(numberOfEntries/2, results.size());
 +        assertTrue(!results.getCollectionType().allowsDuplicates());
 +
 +        queryString = "import com.gemstone.gemfire.admin.QueryUsingPoolDUnitTest.TestObject; select distinct * from " + regionName + " where price = 50";
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(1, results.size());
 +        assertTrue(!results.getCollectionType().allowsDuplicates());
 +
 +        queryString = "import com.gemstone.gemfire.admin.QueryUsingPoolDUnitTest.TestObject; select distinct * from " + regionName + " where ticker = 'ibm' and price = 50";
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(1, results.size());
 +        assertTrue(!results.getCollectionType().allowsDuplicates());
 +      }
 +    });
 +
 +
 +    // Stop server
 +    vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +      public void run() {
 +        stopBridgeServer(getCache());
 +      }
 +    });
 +  }
 +
 +  /**
 +   * Tests remote struct query execution.
 +   */
 +  public void testRemoteStructQueries() throws CacheException {
 +
 +    final String name = this.getName();
 +    final String rootRegionName = "root";
 +
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    final int numberOfEntries = 100;
 +
 +    // Start server
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        Properties config = new Properties();
 +        config.setProperty("locators", "localhost["+DistributedTestUtils.getDUnitLocatorPort()+"]");
 +        system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        createRegion(name, factory.create());
 +        Wait.pause(1000);
 +        try {
 +          startBridgeServer(0, false);
 +        } catch (Exception ex) {
 +          Assert.fail("While starting CacheServer", ex);
 +        }
 +      }
 +    });
 +
 +    // Initialize server region
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion().getSubregion(name);
 +        for (int i=0; i<numberOfEntries; i++) {
 +          region.put("key-"+i, new TestObject(i, "ibm"));
 +        }
 +      }
 +    });
 +
-     final int port = vm0.invokeInt(QueryUsingPoolDUnitTest.class, "getCacheServerPort");
++    final int port = vm0.invoke(() -> QueryUsingPoolDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +
 +    final String regionName = "/" + rootRegionName + "/" + name;
 +
 +    // Create client pool.
 +    final String poolName = "testRemoteStructQueries"; 
 +    createPool(vm1, poolName, host0, port);
 +
 +    // Execute client queries
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        String queryString = null;
 +        SelectResults results = null;
 +
 +        QueryService qService = null;
 +
 +        try {
 +          qService = (PoolManager.find(poolName)).getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        }          
 +
 +        queryString = "import com.gemstone.gemfire.admin.QueryUsingPoolDUnitTest.TestObject; select distinct ticker, price from " + regionName;
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(numberOfEntries, results.size());
 +        assertTrue(!results.getCollectionType().allowsDuplicates() && results.getCollectionType().getElementType().isStructType());
 +
 +        queryString = "import com.gemstone.gemfire.admin.QueryUsingPoolDUnitTest.TestObject; select distinct ticker, price from " + regionName + " where ticker = 'ibm'";
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(numberOfEntries, results.size());
 +        assertTrue(!results.getCollectionType().allowsDuplicates() && results.getCollectionType().getElementType().isStructType());
 +
 +        queryString = "import com.gemstone.gemfire.admin.QueryUsingPoolDUnitTest.TestObject; select distinct ticker, price from " + regionName + " where ticker = 'IBM'";
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(0, results.size());
 +        assertTrue(!results.getCollectionType().allowsDuplicates() && results.getCollectionType().getElementType().isStructType());
 +
 +        queryString = "import com.gemstone.gemfire.admin.QueryUsingPoolDUnitTest.TestObject; select distinct ticker, price from " + regionName + " where price > 49";
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(numberOfEntries/2, results.size());
 +        assertTrue(!results.getCollectionType().allowsDuplicates() && results.getCollectionType().getElementType().isStructType());
 +
 +        queryString = "import com.gemstone.gemfire.admin.QueryUsingPoolDUnitTest.TestObject; select distinct ticker, price from " + regionName + " where price = 50";
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(1, results.size());
 +        assertTrue(!results.getCollectionType().allowsDuplicates() && results.getCollectionType().getElementType().isStructType());
 +
 +        queryString = "import com.gemstone.gemfire.admin.QueryUsingPoolDUnitTest.TestObject; select distinct ticker, price from " + regionName + " where ticker = 'ibm' and price = 50";
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(1, results.size());
 +        assertTrue(!results.getCollectionType().allowsDuplicates() && results.getCollectionType().getElementType().isStructType());
 +      }
 +    });
 +
 +    // Stop server
 +    vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +      public void run() {
 +        stopBridgeServer(getCache());
 +      }
 +    });
 +  }
 +
 +  /**
 +   * Tests remote complex query execution.
 +   */
 +  public void __testRemoteComplexQueries() throws CacheException {
 +
 +    final String name = this.getName();
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +//    final int numberOfEntries = 100;
 +
 +    // Start server
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        Properties config = new Properties();
 +        config.setProperty("locators", "localhost["+DistributedTestUtils.getDUnitLocatorPort()+"]");
 +        system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        createRegion(name, factory.create());
 +        Wait.pause(1000);
 +        try {
 +          startBridgeServer(0, false);
 +        } catch (Exception ex) {
 +          Assert.fail("While starting CacheServer", ex);
 +        }
 +      }
 +    });
 +
 +    // Initialize server region
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion().getSubregion(name);
 +        Portfolio portfolio = null;
 +        Position position1 = null;
 +        Position position2 = null;
 +        Properties portfolioProperties= null;
 +        Properties position1Properties = null;
 +        Properties position2Properties = null;
 +
 +        // Create portfolio 1
 +        portfolio = new Portfolio();
 +        portfolioProperties = new Properties();
 +        portfolioProperties.put("id", new Integer(1));
 +        portfolioProperties.put("type", "type1");
 +        portfolioProperties.put("status", "active");
 +
 +        position1 = new Position();
 +        position1Properties = new Properties();
 +        position1Properties.put("secId", "SUN");
 +        position1Properties.put("qty", new Double(34000.0));
 +        position1Properties.put("mktValue", new Double(24.42));
 +        position1.init(position1Properties);
 +        portfolioProperties.put("position1", position1);
 +
 +        position2 = new Position();
 +        position2Properties = new Properties();
 +        position2Properties.put("secId", "IBM");
 +        position2Properties.put("qty", new Double(8765.0));
 +        position2Properties.put("mktValue", new Double(34.29));
 +        position2.init(position2Properties);
 +        portfolioProperties.put("position2", position2);
 +
 +        portfolio.init(portfolioProperties);
 +        region.put(new Integer(1), portfolio);
 +
 +        // Create portfolio 2
 +        portfolio = new Portfolio();
 +        portfolioProperties = new Properties();
 +        portfolioProperties.put("id", new Integer(2));
 +        portfolioProperties.put("type", "type2");
 +        portfolioProperties.put("status", "inactive");
 +
 +        position1 = new Position();
 +        position1Properties = new Properties();
 +        position1Properties.put("secId", "YHOO");
 +        position1Properties.put("qty", new Double(9834.0));
 +        position1Properties.put("mktValue", new Double(12.925));
 +        position1.init(position1Properties);
 +        portfolioProperties.put("position1", position1);
 +
 +        position2 = new Position();
 +        position2Properties = new Properties();
 +        position2Properties.put("secId", "GOOG");
 +        position2Properties.put("qty", new Double(12176.0));
 +        position2Properties.put("mktValue", new Double(21.972));
 +        position2.init(position2Properties);
 +        portfolioProperties.put("position2", position2);
 +
 +        portfolio.init(portfolioProperties);
 +        region.put(new Integer(2), portfolio);
 +
 +        // Create portfolio 3
 +        portfolio = new Portfolio();
 +        portfolioProperties = new Properties();
 +        portfolioProperties.put("id", new Integer(3));
 +        portfolioProperties.put("type", "type3");
 +        portfolioProperties.put("status", "active");
 +
 +        position1 = new Position();
 +        position1Properties = new Properties();
 +        position1Properties.put("secId", "MSFT");
 +        position1Properties.put("qty", new Double(98327.0));
 +        position1Properties.put("mktValue", new Double(23.32));
 +        position1.init(position1Properties);
 +        portfolioProperties.put("position1", position1);
 +
 +        position2 = new Position();
 +        position2Properties = new Properties();
 +        position2Properties.put("secId", "AOL");
 +        position2Properties.put("qty", new Double(978.0));
 +        position2Properties.put("mktValue", new Double(40.373));
 +        position2.init(position2Properties);
 +        portfolioProperties.put("position2", position2);
 +
 +        portfolio.init(portfolioProperties);
 +        region.put(new Integer(3), portfolio);
 +
 +        // Create portfolio 4
 +        portfolio = new Portfolio();
 +        portfolioProperties = new Properties();
 +        portfolioProperties.put("id", new Integer(4));
 +        portfolioProperties.put("type", "type1");
 +        portfolioProperties.put("status", "inactive");
 +
 +        position1 = new Position();
 +        position1Properties = new Properties();
 +        position1Properties.put("secId", "APPL");
 +        position1Properties.put("qty", new Double(90.0));
 +        position1Properties.put("mktValue", new Double(67.356572));
 +        position1.init(position1Properties);
 +        portfolioProperties.put("position1", position1);
 +
 +        position2 = new Position();
 +        position2Properties = new Properties();
 +        position2Properties.put("secId", "ORCL");
 +        position2Properties.put("qty", new Double(376.0));
 +        position2Properties.put("mktValue", new Double(101.34));
 +        position2.init(position2Properties);
 +        portfolioProperties.put("position2", position2);
 +
 +        portfolio.init(portfolioProperties);
 +        region.put(new Integer(4), portfolio);
 +      }
 +    });
 +
 +    // Create client region
-     final int port = vm0.invokeInt(QueryUsingPoolDUnitTest.class, "getCacheServerPort");
++    final int port = vm0.invoke(() -> QueryUsingPoolDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +    vm1.invoke(new CacheSerializableRunnable("Create region") {
 +      public void run2() throws CacheException {
 +        Properties config = new Properties();
 +        config.setProperty("mcast-port", "0");
 +        system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +        getCache();
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        ClientServerTestCase.configureConnectionPool(factory, host0, port,-1, true, -1, -1, null);
 +        createRegion(name, factory.create());
 +      }
 +    });
 +
 +    // Execute client queries
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion().getSubregion(name);
 +        String queryString = null;
 +        SelectResults results = null;
 +
 +        queryString =
 +          "IMPORT cacheRunner.Position; " +
 +          "SELECT DISTINCT id, status FROM " + region.getFullPath() +
 +          "WHERE NOT (SELECT DISTINCT * FROM positions.values posnVal TYPE Position " +
 +          "WHERE posnVal.secId='AOL' OR posnVal.secId='SAP').isEmpty";
 +        try {
 +          results = region.query(queryString);
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        LogWriterUtils.getLogWriter().fine("size: " + results.size());
 +        //assertEquals(numberOfEntries, results.size());
 +        assertTrue(!results.getCollectionType().allowsDuplicates() && results.getCollectionType().getElementType().isStructType());
 +      }
 +    });
 +
 +
 +    // Stop server
 +    vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +      public void run() {
 +        stopBridgeServer(getCache());
 +      }
 +    });
 +  }
 +
 +  /**
 +   * Tests remote full region query execution.
 +   */
 +  public void testRemoteFullRegionQueries() throws CacheException {
 +
 +    final String name = this.getName();
 +    final String rootRegionName = "root";
 +
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    final int numberOfEntries = 100;
 +
 +    // Start server
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        Properties config = new Properties();
 +        config.setProperty("locators", "localhost["+DistributedTestUtils.getDUnitLocatorPort()+"]");
 +        system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        createRegion(name, factory.create());
 +        Wait.pause(1000);
 +        try {
 +          startBridgeServer(0, false);
 +        } catch (Exception ex) {
 +          Assert.fail("While starting CacheServer", ex);
 +        }
 +      }
 +    });
 +
 +    // Initialize server region
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion().getSubregion(name);
 +        for (int i=0; i<numberOfEntries; i++) {
 +          region.put("key-"+i, new TestObject(i, "ibm"));
 +        }
 +      }
 +    });
 +
 +    // Create client region
-     final int port = vm0.invokeInt(QueryUsingPoolDUnitTest.class, "getCacheServerPort");
++    final int port = vm0.invoke(() -> QueryUsingPoolDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +
 +    final String regionName = "/" + rootRegionName + "/" + name;
 +
 +    // Create client pool.
 +    final String poolName = "testRemoteFullRegionQueries"; 
 +    createPool(vm1, poolName, host0, port);
 +
 +
 +    // Execute client queries
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        String queryString = null;
 +        SelectResults results = null;
 +        Comparator comparator = null;
 +        Object[] resultsArray = null;
 +        QueryService qService = null;
 +
 +        try {
 +          qService = (PoolManager.find(poolName)).getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        }          
 +
 +        // value query
 +        queryString = "SELECT DISTINCT itr.value FROM " + regionName + ".entries itr where itr.key = 'key-1'";
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(1, results.size());
 +        assertTrue(!results.getCollectionType().allowsDuplicates());
 +        assertTrue(results.asList().get(0) instanceof TestObject);
 +
 +        // key query
 +        queryString = "SELECT DISTINCT itr.key FROM " + regionName + ".entries itr where itr.key = 'key-1'";
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(1, results.size());
 +        assertTrue(!results.getCollectionType().allowsDuplicates());
 +        assertEquals("key-1", results.asList().get(0));
 +
 +        // order by value query
 +        queryString = "SELECT DISTINCT * FROM " + regionName + " WHERE id < 101 ORDER BY id";
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(numberOfEntries, results.size());
 +        // All order-by query results are stored in a ResultsCollectionWrapper
 +        // wrapping a list, so the assertion below is not correct even though
 +        // it should be.
 +        //assertTrue(!results.getCollectionType().allowsDuplicates());
 +        assertTrue(results.getCollectionType().isOrdered());
 +        comparator = new IdComparator();
 +        resultsArray = results.toArray();
 +        for (int i=0; i<resultsArray.length; i++) {
 +          if (i+1 != resultsArray.length) {
 +            // The id of the current element in the result set must be less
 +            // than the id of the next one to pass.
 +            assertTrue("The id for " + resultsArray[i] + " should be less than the id for " + resultsArray[i+1], comparator.compare(resultsArray[i], resultsArray[i+1]) == -1);
 +          }
 +        }
 +
 +        // order by struct query
 +        queryString = "SELECT DISTINCT id, ticker, price FROM " + regionName + " WHERE id < 101 ORDER BY id";
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(numberOfEntries, results.size());
 +        // All order-by query results are stored in a ResultsCollectionWrapper
 +        // wrapping a list, so the assertion below is not correct even though
 +        // it should be.
 +        //assertTrue(!results.getCollectionType().allowsDuplicates());
 +        assertTrue(results.getCollectionType().isOrdered());
 +        comparator = new StructIdComparator();
 +        resultsArray = results.toArray();
 +        for (int i=0; i<resultsArray.length; i++) {
 +          if (i+1 != resultsArray.length) {
 +            // The id of the current element in the result set must be less
 +            // than the id of the next one to pass.
 +            assertTrue("The id for " + resultsArray[i] + " should be less than the id for " + resultsArray[i+1], comparator.compare(resultsArray[i], resultsArray[i+1]) == -1);
 +          }
 +        }
 +
 +        // size query
 +        queryString = "(SELECT DISTINCT * FROM " + regionName + " WHERE id < 101).size";
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(1, results.size());
 +        Object result = results.iterator().next();
 +        assertTrue(result instanceof Integer);
 +        int resultInt = ((Integer) result).intValue();
 +        assertEquals(resultInt, 100);
 +
 +        // query with leading/trailing spaces
 +        queryString = " SELECT DISTINCT itr.key FROM " + regionName + ".entries itr where itr.key = 'key-1' ";
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(1, results.size());
 +        assertEquals("key-1", results.asList().get(0));
 +      }
 +    });
 +
 +    // Stop server
 +    vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +      public void run() {
 +        stopBridgeServer(getCache());
 +      }
 +    });
 +  }
 +
 +  /**
 +   * Tests client-server query using parameters (compiled queries).
 +   */
 +  public void testClientServerQueriesWithParams() throws CacheException {
 +
 +    final String name = this.getName();
 +
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    final int numberOfEntries = 100;
 +
 +    final Object[][] params = new Object[][] {
 +        {"key-1"}, // 0
 +        {101}, // 1
 +        {101}, // 2
 +        {101}, // 3
 +        {50, "ibm"}, // 4
 +        {50, "ibm"}, // 5
 +    };
 +
 +    final int[] expectedResults = new int[] {
 +        1, // 0
 +        100, // 1
 +        100, // 2
 +        1, // 3
 +        1, // 4
 +        50, // 5
 +    };
 +
 +
 +    // Start server
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer();
 +      }
 +    });
 +
 +    // Initialize server region
 +    vm0.invoke(new CacheSerializableRunnable("Create and populate region") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion().getSubregion(regionName);
 +        for (int i=0; i<numberOfEntries; i++) {
 +          region.put("key-"+i, new TestObject(i, "ibm"));
 +        }
 +      }
 +    });
 +
 +    // Create client region
-     final int port = vm0.invokeInt(QueryUsingPoolDUnitTest.class, "getCacheServerPort");
++    final int port = vm0.invoke(() -> QueryUsingPoolDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +
 +    final String regionName = "/" + this.rootRegionName + "/" + this.regionName;
 +
 +    // Create client pool.
 +    final String poolName = "testClientServerQueriesWithParams"; 
 +    createPool(vm1, poolName, host0, port);
 +
 +
 +    // Execute client queries
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        SelectResults results = null;
 +        Comparator comparator = null;
 +        Object[] resultsArray = null;
 +        QueryService qService = null;
 +
 +        try {
 +          qService = (PoolManager.find(poolName)).getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        }          
 +
 +        for (int i=0; i < queryString.length; i++){
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query :" + queryString[i]);
 +            Query query = qService.newQuery(queryString[i]);
 +            results = (SelectResults)query.execute(params[i]);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +          try {
 +            assertEquals(expectedResults[i], results.size());
 +          }catch(Throwable th) {
 +            fail("Result mismatch for query= " + queryString[i] + " expected = "+expectedResults[i] + " actual="+results.size());
 +          }
 +        }        
 +      }
 +    });
 +
 +    final int useMaintainedCompiledQueries = queryString.length;
 +
 +    // Execute the same compiled queries multiple time
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        SelectResults results = null;
 +        Comparator comparator = null;
 +        Object[] resultsArray = null;
 +        QueryService qService = null;
 +
 +        try {
 +          qService = (PoolManager.find(poolName)).getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        }          
 +        for (int x=0; x < useMaintainedCompiledQueries; x++){
 +          for (int i=0; i < queryString.length; i++){
 +            try {
 +              LogWriterUtils.getLogWriter().info("### Executing Query :" + queryString[i]);
 +              Query query = qService.newQuery(queryString[i]);
 +              results = (SelectResults)query.execute(params[i]);
 +            } catch (Exception e) {
 +              Assert.fail("Failed executing " + queryString[i], e);
 +            }
 +            try {
 +              assertEquals(expectedResults[i], results.size());
 +            }catch(Throwable th) {
 +              fail("Result mismatch for query= " + queryString[i] + " expected = "+expectedResults[i] + " actual="+results.size());
 +            }
 +          }        
 +        }
 +      }
 +    });
 +
 +    // Validate maintained compiled queries.
 +    // There should be only queryString.length compiled queries registered.
 +    vm0.invoke(new CacheSerializableRunnable("validate compiled query.") {
 +      public void run2() throws CacheException {
 +        long compiledQueryCount = CacheClientNotifier.getInstance().getStats().getCompiledQueryCount();
 +        assertEquals(queryString.length, compiledQueryCount);
 +      }
 +    });
 +
 +    // Check to see if maintained compiled queries are used.
 +    vm0.invoke(new CacheSerializableRunnable("validate compiled query.") {
 +      public void run2() throws CacheException {
 +        long compiledQueryUsedCount = CacheClientNotifier.getInstance().getStats().getCompiledQueryUsedCount();
 +        int numTimesUsed = (useMaintainedCompiledQueries + 1) * queryString.length;
 +        assertEquals(numTimesUsed, compiledQueryUsedCount);
 +      }
 +    });
 +
 +    // Stop server
 +    vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +      public void run() {
 +        stopBridgeServer(getCache());
 +      }
 +    });
 +  }
 +
 +  /**
 +   * Tests client-server query using parameters (compiled queries).
 +   */
 +  public void testMulitipleClientServerQueriesWithParams() throws CacheException {
 +    final String name = this.getName();
 +
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    VM vm3 = host.getVM(3);
 +    final int numberOfEntries = 100;
 +
 +    final Object[][] params = new Object[][] {
 +        {"key-1"}, // 0
 +        {101}, // 1
 +        {101}, // 2
 +        {101}, // 3
 +        {50, "ibm"}, // 4
 +        {50, "ibm"}, // 5
 +    };
 +
 +    final int[] expectedResults = new int[] {
 +        1, // 0
 +        100, // 1
 +        100, // 2
 +        1, // 3
 +        1, // 4
 +        50, // 5
 +    };
 +
 +
 +    // Start server1
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer();
 +        Region region = getRootRegion().getSubregion(regionName);
 +        for (int i=0; i<numberOfEntries; i++) {
 +          region.put("key-"+i, new TestObject(i, "ibm"));
 +        }
 +      }
 +    });
 +
 +
 +    // Start server2
 +    vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer();
 +        Region region = getRootRegion().getSubregion(regionName);
 +        for (int i=0; i<numberOfEntries; i++) {
 +          region.put("key-"+i, new TestObject(i, "ibm"));
 +        }
 +      }
 +    });
 +
 +    // Create client region
-     final int port0 = vm0.invokeInt(QueryUsingPoolDUnitTest.class, "getCacheServerPort");
-     final int port1 = vm1.invokeInt(QueryUsingPoolDUnitTest.class, "getCacheServerPort");
++    final int port0 = vm0.invoke(() -> QueryUsingPoolDUnitTest.getCacheServerPort());
++    final int port1 = vm1.invoke(() -> QueryUsingPoolDUnitTest.getCacheServerPort());
 +
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +
 +    final String regionName = "/" + this.rootRegionName + "/" + this.regionName;
 +
 +    // Create client pool.
 +    final String poolName = "testClientServerQueriesWithParams"; 
 +    createPool(vm2, poolName, new String[]{host0}, new int[]{port0}, true);
 +    createPool(vm3, poolName, new String[]{host0}, new int[]{port1}, true);
 +
 +    // Execute client queries
 +    SerializableRunnable executeQueries = new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        SelectResults results = null;
 +        Comparator comparator = null;
 +        Object[] resultsArray = null;
 +        QueryService qService = null;
 +
 +        try {
 +          qService = (PoolManager.find(poolName)).getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        }          
 +        for (int j=0; j < queryString.length; j++){
 +          for (int i=0; i < queryString.length; i++){
 +            try {
 +              LogWriterUtils.getLogWriter().info("### Executing Query :" + queryString[i]);
 +              Query query = qService.newQuery(queryString[i]);
 +              results = (SelectResults)query.execute(params[i]);
 +            } catch (Exception e) {
 +              Assert.fail("Failed executing " + queryString[i], e);
 +            }
 +            try {
 +              assertEquals(expectedResults[i], results.size());
 +            }catch(Throwable th) {
 +              fail("Result mismatch for query= " + queryString[i] + " expected = "+expectedResults[i] + " actual="+results.size());
 +            }
 +          }
 +        }
 +      }
 +    };
 +
 +    vm2.invoke(executeQueries);
 +    vm3.invoke(executeQueries);
 +
 +    // Validate maintained compiled queries.
 +    // There should be only queryString.length compiled queries registered.
 +    vm0.invoke(new CacheSerializableRunnable("validate compiled query.") {
 +      public void run2() throws CacheException {
 +        long compiledQueryCount = CacheClientNotifier.getInstance().getStats().getCompiledQueryCount();
 +        assertEquals(queryString.length, compiledQueryCount);
 +      }
 +    });
 +    
 +    this.closeClient(vm2);
 +    this.closeClient(vm3);
 +    
 +    // Validate maintained compiled queries.
 +    // All the queries will be still present in the server.
 +    // They will be cleaned up periodically.
 +    vm0.invoke(new CacheSerializableRunnable("validate compiled query.") {
 +      public void run2() throws CacheException {
 +        long compiledQueryCount = CacheClientNotifier.getInstance().getStats().getCompiledQueryCount();
 +        assertEquals(queryString.length, compiledQueryCount);
 +      }
 +    });
 +    
 +    vm1.invoke(new CacheSerializableRunnable("validate compiled query.") {
 +      public void run2() throws CacheException {
 +        long compiledQueryCount = CacheClientNotifier.getInstance().getStats().getCompiledQueryCount();
 +        assertEquals(queryString.length, compiledQueryCount);
 +      }
 +    });
 + 
 +    // recreate clients and execute queries.
 +    createPool(vm2, poolName, new String[]{host0, host0}, new int[]{port1, port0}, true);
 +    createPool(vm3, poolName, new String[]{host0, host0}, new int[]{port0, port1}, true);
 +
 +    vm2.invoke(executeQueries);
 +    vm3.invoke(executeQueries);
 +    
 +    // Validate maintained compiled queries.
 +    // All the queries will be still present in the server.
 +    vm0.invoke(new CacheSerializableRunnable("validate compiled query.") {
 +      public void run2() throws CacheException {
 +        long compiledQueryCount = CacheClientNotifier.getInstance().getStats().getCompiledQueryCount();
 +        assertEquals(queryString.length, compiledQueryCount);
 +      }
 +    });
 +    
 +    vm1.invoke(new CacheSerializableRunnable("validate compiled query.") {
 +      public void run2() throws CacheException {
 +        long compiledQueryCount = CacheClientNotifier.getInstance().getStats().getCompiledQueryCount();
 +        assertEquals(queryString.length, compiledQueryCount);
 +      }
 +    });
 +    
 +    // Stop server
 +    vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +      public void run() {
 +        stopBridgeServer(getCache());
 +      }
 +    });
 +    vm1.invoke(new SerializableRunnable("Stop CacheServer") {
 +      public void run() {
 +        stopBridgeServer(getCache());
 +      }
 +    });
 +
 +  }
 +
 +  /**
 +   * Tests client-server compiled query register and cleanup.
 +   */
 +  public void testClientServerCompiledQueryRegisterAndCleanup() throws CacheException {
 +
 +    final String name = this.getName();
 +    final String rootRegionName = "root";
 +
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    final int numberOfEntries = 100;
 +
 +    final Object[][] params = new Object[][] {
 +        {"key-1"}, // 0
 +        {101}, // 1
 +        {101}, // 2
 +        {101}, // 3
 +        {50, "ibm"}, // 4
 +        {50, "ibm"}, // 5
 +    };
 +    
 +    // Start server
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer();
 +      }
 +    });
 +
 +    // Initialize server region
 +    vm0.invoke(new CacheSerializableRunnable("Create and populate region.") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion().getSubregion(name);
 +        for (int i=0; i<numberOfEntries; i++) {
 +          region.put("key-"+i, new TestObject(i, "ibm"));
 +        }
 +      }
 +    });
 +
-     final int port = vm0.invokeInt(QueryUsingPoolDUnitTest.class, "getCacheServerPort");
++    final int port = vm0.invoke(() -> QueryUsingPoolDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +
 +    final String regionName = "/" + this.rootRegionName + "/" + this.regionName;
 +
 +    // Create client pool.
 +    final String poolName = "testClientServerQueriesWithParams"; 
 +    createPool(vm1, poolName, host0, port);
 +    createPool(vm2, poolName, host0, port);
 +
 +    // Execute client queries
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        executeCompiledQueries(poolName, params);      }
 +    });
 +
 +    // Execute client queries
 +    vm2.invoke(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        executeCompiledQueries(poolName, params);      }
 +    });
 +    
 +    // Validate maintained compiled queries.
 +    vm0.invoke(new CacheSerializableRunnable("validate compiled query.") {
 +      public void run2() throws CacheException {
 +        long compiledQueryCount = CacheClientNotifier.getInstance().getStats().getCompiledQueryCount();
 +        assertEquals(queryString.length, compiledQueryCount);
 +      }
 +    });
 +
 +    closeClient(vm1);
 +
 +    // Validate maintained compiled queries.
 +    vm0.invoke(new CacheSerializableRunnable("validate compiled query.") {
 +      public void run2() throws CacheException {
 +        long compiledQueryCount = CacheClientNotifier.getInstance().getStats().getCompiledQueryCount();
 +        assertEquals(queryString.length, compiledQueryCount);
 +      }
 +    });
 +
 +    // Stop server
 +    vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +      public void run() {
 +        stopBridgeServer(getCache());
 +      }
 +    });    
 +  }
 +
 +  /**
 +   * Tests client-server compiled query register and cleanup.
 +   */
 +  public void testClientServerCompiledQueryTimeBasedCleanup() throws CacheException {
 +
 +    final String name = this.getName();
 +    final String rootRegionName = "root";
 +
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    final int numberOfEntries = 100;
 +
 +    final Object[][] params = new Object[][] {
 +        {"key-1"}, // 0
 +        {101}, // 1
 +        {101}, // 2
 +        {101}, // 3
 +        {50, "ibm"}, // 4
 +        {50, "ibm"}, // 5
 +    };
 +    
 +    // Start server
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer();
 +      }
 +    });
 +
 +    // Initialize server region
 +    vm0.invoke(new CacheSerializableRunnable("Create and populate region.") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion().getSubregion(name);
 +        for (int i=0; i<numberOfEntries; i++) {
 +          region.put("key-"+i, new TestObject(i, "ibm"));
 +        }
 +        QueryService qs = getCache().getQueryService();
 +        DefaultQuery query = (DefaultQuery)qs.newQuery("Select * from " + regName);
 +        query.setTestCompiledQueryClearTime(2 * 1000); 
 +      }
 +    });
 +
-     final int port = vm0.invokeInt(QueryUsingPoolDUnitTest.class, "getCacheServerPort");
++    final int port = vm0.invoke(() -> QueryUsingPoolDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +
 +    final String regionName = "/" + this.rootRegionName + "/" + this.regionName;
 +
 +    // Create client pool.
 +    final String poolName = "testClientServerQueriesWithParams"; 
 +    createPool(vm1, poolName, host0, port);
 +    createPool(vm2, poolName, host0, port);
 +
 +    // Execute client queries
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        executeCompiledQueries(poolName, params);
 +      }
 +    });
 +
 +    // Execute client queries
 +    vm2.invoke(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        executeCompiledQueries(poolName, params);
 +      }
 +    });
 +        
 +    // Validate maintained compiled queries.
 +    this.validateCompiledQuery(vm0, 0);
 +        
 +    // Recreate compiled queries.
 +    // Execute client queries
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        executeCompiledQueries(poolName, params);
 +      }
 +    });
 +
 +    // Execute client queries
 +    // The client2 will be using the queries.
 +    vm2.invokeAsync(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        for (int i=0; i < 10; i++) {
 +          Wait.pause(200);
 +          executeCompiledQueries(poolName, params);
 +        }
 +      }
 +    });
 +       
 +    // Validate maintained compiled queries.
 +    validateCompiledQuery(vm0, queryString.length);
 +
 +    // Let the compiled queries to be idle (not used).
 +    Wait.pause(2 * 1000);    
 +    
 +    // Validate maintained compiled queries.
 +    this.validateCompiledQuery(vm0, 0);
 +
 +    // Close clients
 +    closeClient(vm2);
 +    closeClient(vm1);
 +    
 +    // Validate maintained compiled queries.
 +    vm0.invoke(new CacheSerializableRunnable("validate compiled query") {
 +      public void run2() throws CacheException {
 +        long compiledQueryCount = CacheClientNotifier.getInstance().getStats().getCompiledQueryCount();
 +        assertEquals(0, compiledQueryCount);
 +      }
 +    });
 +    
 +    // Stop server
 +    vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +      public void run() {
 +        stopBridgeServer(getCache());
 +      }
 +    });    
 +  }
 +
 +  /**
 +   * Tests client-server compiled query register and cleanup.
 +   * It creates the client connections without the subscription 
 +   * enabled. This doesn't create any client proxy on the server.
 +   */
 +  public void testClientServerCompiledQueryCleanup() throws CacheException {
 +
 +    final String name = this.getName();
 +    final String rootRegionName = "root";
 +
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    final int numberOfEntries = 100;
 +
 +    final Object[][] params = new Object[][] {
 +        {"key-1"}, // 0
 +        {101}, // 1
 +        {101}, // 2
 +        {101}, // 3
 +        {50, "ibm"}, // 4
 +        {50, "ibm"}, // 5
 +    };
 +    
 +    // Start server
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer();
 +      }
 +    });
 +
 +    // Initialize server region
 +    vm0.invoke(new CacheSerializableRunnable("Create and populate region.") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion().getSubregion(name);
 +        for (int i=0; i<numberOfEntries; i++) {
 +          region.put("key-"+i, new TestObject(i, "ibm"));
 +        }
 +        QueryService qs = getCache().getQueryService();
 +        DefaultQuery query = (DefaultQuery)qs.newQuery("Select * from " + regName);
 +        query.setTestCompiledQueryClearTime(2 * 1000); 
 +      }
 +    });
 +
-     final int port = vm0.invokeInt(QueryUsingPoolDUnitTest.class, "getCacheServerPort");
++    final int port = vm0.invoke(() -> QueryUsingPoolDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +
 +    final String regionName = "/" + this.rootRegionName + "/" + this.regionName;
 +
 +    // Create client pool.
 +    final String poolName = "testClientServerQueriesWithParams"; 
 +    final boolean subscriptiuonEnabled = false;
 +    createPool(vm1, poolName, host0, port, subscriptiuonEnabled);
 +    createPool(vm2, poolName, host0, port, subscriptiuonEnabled);
 +
 +    // Execute client queries
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        executeCompiledQueries(poolName, params);
 +      }
 +    });
 +
 +    // Execute client queries
 +    vm2.invoke(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        executeCompiledQueries(poolName, params);
 +      }
 +    });
 +    
 +    // Validate maintained compiled queries.
 +    this.validateCompiledQuery(vm0, 0);
 +    
 +    
 +    // Recreate compiled queries.
 +    // Execute client queries
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        executeCompiledQueries(poolName, params);
 +      }
 +    });
 +
 +    // Execute client queries
 +    // The client2 will be using the queries.
 +    vm2.invokeAsync(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        for (int i=0; i < 10; i++) {
 +          Wait.pause(10);
 +          executeCompiledQueries(poolName, params);
 +        }
 +      }
 +    });
 +       
 +    // Validate maintained compiled queries.
 +    this.validateCompiledQuery(vm0, queryString.length);
 +
 +    // Close clients
 +    // Let the compiled queries to be idle (not used).
 +    //pause(2 * 1000);    
 +    
 +    // Validate maintained compiled queries.
 +    this.validateCompiledQuery(vm0, 0);
 +    
 +    // Recreate compiled queries.
 +    // Execute client queries
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        executeCompiledQueries(poolName, params);
 +      }
 +    });
 +    
 +    
 +    // Validate maintained compiled queries.
 +    vm0.invoke(new CacheSerializableRunnable("validate compiled query.") {
 +      public void run2() throws CacheException {
 +        long compiledQueryCount = CacheClientNotifier.getInstance().getStats().getCompiledQueryCount();
 +        assertEquals(queryString.length, compiledQueryCount);
 +      }
 +    });
 +
 +    // Close clients
 +    closeClient(vm2);
 +    closeClient(vm1);
 +
 +    // Validate maintained compiled queries.
 +    // since not used it should get cleaned up after sometime.
 +    this.validateCompiledQuery(vm0, 0);
 +    
 +    
 +    // Stop server
 +    vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +      public void run() {
 +        stopBridgeServer(getCache());
 +      }
 +    });    
 +  }
 +
 +  /**
 +   * Tests client-server query using parameters (compiled queries).
 +   */
 +  public void testBindParamsWithMulitipleClients() throws CacheException {
 +
 +    final String name = this.getName();
 +
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    VM vm3 = host.getVM(3);
 +    final int numberOfEntries = 100;
 +
 +    final Object[][] params = new Object[][] {
 +        {"key-1"}, // 0
 +        {101}, // 1
 +        {101}, // 2
 +        {101}, // 3
 +        {50, "ibm"}, // 4
 +        {50, "ibm"}, // 5
 +    };
 +
 +    final String[] querys = new String[] {
 +        "SELECT itr.value FROM " + regName + ".entries itr where itr.key = 'key-1'", // 0
 +        "SELECT DISTINCT * FROM " + regName + " WHERE id < 101 ORDER BY id", // 1
 +        "SELECT DISTINCT * FROM " + regName + " WHERE id < 101 ORDER BY id", // 2
 +        "(SELECT DISTINCT * FROM " + regName + " WHERE id < 101).size", // 3
 +        "SELECT * FROM " + regName + " WHERE id = 50 and Ticker = 'ibm'", // 4
 +        "SELECT * FROM " + regName + " WHERE id < 50 and Ticker = 'ibm'", // 5
 +    };
 +
 +    final int[] expectedResults = new int[] {
 +        1, // 0
 +        100, // 1
 +        100, // 2
 +        1, // 3
 +        1, // 4
 +        50, // 5
 +    };
 +
 +    // Start server1
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer();
 +        Region region = getRootRegion().getSubregion(regionName);
 +        for (int i=0; i<numberOfEntries; i++) {
 +          region.put("key-"+i, new TestObject(i, "ibm"));
 +        }
 +      }
 +    });
 +
 +    // Create client region
-     final int port0 = vm0.invokeInt(QueryUsingPoolDUnitTest.class, "getCacheServerPort");
++    final int port0 = vm0.invoke(() -> QueryUsingPoolDUnitTest.getCacheServerPort());
 +
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +
 +    final String regionName = "/" + this.rootRegionName + "/" + this.regionName;
 +
 +    // Create client pool.
 +    final String poolName = "testClientServerQueriesWithParams"; 
 +    createPool(vm1, poolName, new String[]{host0}, new int[]{port0}, true);
 +    createPool(vm2, poolName, new String[]{host0}, new int[]{port0}, true);
 +    createPool(vm3, poolName, new String[]{host0}, new int[]{port0}, true);
 +
 +    // Execute client query multiple times and validate results.
 +    SerializableRunnable executeSameQueries = new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        QueryService qService = null;
 +        SelectResults[][] rs = new SelectResults[1][2];
 +        
 +        try {
 +          qService = (PoolManager.find(poolName)).getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        }      
 +        // Use minimal query, so that there will be multiple 
 +        // clients using the same compiled query at server.
 +        for (int j=0; j < 5; j++){
 +          for (int i=0; i < 2; i++){
 +            try {
 +              LogWriterUtils.getLogWriter().info("### Executing Query :" + queryString[i]);
 +              Query query = qService.newQuery(queryString[i]);
 +              rs[0][0] = (SelectResults)query.execute(params[i]);
 +              Query query2 = qService.newQuery(querys[i]);
 +              rs[0][1] = (SelectResults)query2.execute();
 +              // Compare results.
 +            } catch (Exception e) {
 +              Assert.fail("Failed executing " + queryString[i], e);
 +            }
 +            LogWriterUtils.getLogWriter().info("### Comparing results for Query :" + ((i+1) * (j+1)) + " : " + queryString[i]);
 +            compareQueryResultsWithoutAndWithIndexes(rs, 1);
 +            LogWriterUtils.getLogWriter().info("### Done Comparing results for Query :" + ((i+1) * (j+1)) + " : " + queryString[i]); 
 +          }
 +        }
 +      }
 +    };
 +
 +    vm1.invokeAsync(executeSameQueries);
 +    vm2.invokeAsync(executeSameQueries);
 +    vm3.invokeAsync(executeSameQueries);
 +    
 +    // Wait till the query execution completes.
 +    vm0.invoke(new CacheSerializableRunnable("validate compiled query.") {
 +      public void run2() throws CacheException {
 +        long compiledQueryUsedCount = -1;
 +        while (true) {
 +          LogWriterUtils.getLogWriter().info("### CompiledQueryUsedCount :" + compiledQueryUsedCount);
 +          if (compiledQueryUsedCount == CacheClientNotifier.getInstance().getStats().getCompiledQueryUsedCount()) {
 +            LogWriterUtils.getLogWriter().info("### previous and current CompiledQueryUsedCounts are same :" + compiledQueryUsedCount);
 +            break;
 +          } else {
 +            compiledQueryUsedCount = CacheClientNotifier.getInstance().getStats().getCompiledQueryUsedCount();
 +            try {
 +              Thread.currentThread().sleep(3 * 1000);
 +            } catch (Exception ex) {
 +              break;
 +            }
 +          }
 +        }
 +      }
 +    });
 +    
 +    // Execute client queries and validate results.
 +    SerializableRunnable executeQueries = new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        SelectResults results = null;
 +        SelectResults results2 = null;
 +        Comparator comparator = null;
 +        Object[] resultsArray = null;
 +        QueryService qService = null;
 +        SelectResults[][] rs = new SelectResults[1][2];
 +        
 +        try {
 +          qService = (PoolManager.find(poolName)).getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        }          
 +        for (int j=0; j < queryString.length; j++){
 +          for (int i=0; i < queryString.length; i++){
 +            try {
 +              LogWriterUtils.getLogWriter().info("### Executing Query :" + queryString[i]);
 +              Query query = qService.newQuery(queryString[i]);
 +              rs[0][0] = (SelectResults)query.execute(params[i]);
 +              Query query2 = qService.newQuery(querys[i]);
 +              rs[0][1] = (SelectResults)query2.execute();
 +              // Compare results.
 +              compareQueryResultsWithoutAndWithIndexes(rs, 1);
 +            } catch (Exception e) {
 +              Assert.fail("Failed executing " + queryString[i], e);
 +            }
 +          }
 +        }
 +      }
 +    };
 +
 +    vm1.invokeAsync(executeQueries);
 +    vm2.invokeAsync(executeQueries);
 +    vm3.invokeAsync(executeQueries);
 +    
 +    // Wait till the query execution completes.
 +    vm0.invoke(new CacheSerializableRunnable("validate compiled query.") {
 +      public void run2() throws CacheException {
 +        long compiledQueryUsedCount = -1;
 +        while (true) {
 +          LogWriterUtils.getLogWriter().info("### previous CompiledQueryUsedCount :" + compiledQueryUsedCount);
 +          if (compiledQueryUsedCount == CacheClientNotifier.getInstance().getStats().getCompiledQueryUsedCount()) {
 +            LogWriterUtils.getLogWriter().info("### previous and current CompiledQueryUsedCounts are same :" + compiledQueryUsedCount);
 +            break;
 +          } else {
 +            compiledQueryUsedCount = CacheClientNotifier.getInstance().getStats().getCompiledQueryUsedCount();
 +            try {
 +              Thread.currentThread().sleep(3 * 1000);
 +            } catch (Exception ex) {
 +              break;
 +            }
 +          }
 +        }
 +      }
 +    });
 +      
 +    this.closeClient(vm1);
 +    this.closeClient(vm2);
 +    this.closeClient(vm3);
 +    
 +    // Stop server
 +    vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +      public void run() {
 +        stopBridgeServer(getCache());
 +      }
 +    });
 +    
 +  }
 +
 +  /**
 +   * Tests remote join query execution.
 +   */
 +  public void testRemoteJoinRegionQueries() throws CacheException {
 +
 +    final String name = this.getName();
 +    final String rootRegionName = "root";
 +
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    final int numberOfEntries = 100;
 +
 +    // Start server
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        Properties config = new Properties();
 +        config.setProperty("locators", "localhost["+DistributedTestUtils.getDUnitLocatorPort()+"]");
 +        system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        createRegion(name+"1", factory.create());
 +        createRegion(name+"2", factory.create());
 +        Wait.pause(1000);
 +        try {
 +          startBridgeServer(0, false);
 +        } catch (Exception ex) {
 +          Assert.fail("While starting CacheServer", ex);
 +        }
 +      }
 +    });
 +
 +    // Initialize server region
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        Region region1 = getRootRegion().getSubregion(name+"1");
 +        for (int i=0; i<numberOfEntries; i++) {
 +          region1.put("key-"+i, new TestObject(i, "ibm"));
 +        }
 +        Region region2 = getRootRegion().getSubregion(name+"2");
 +        for (int i=0; i<numberOfEntries; i++) {
 +          region2.put("key-"+i, new TestObject(i, "ibm"));
 +        }
 +      }
 +    });
 +
 +    // Create client region
-     final int port = vm0.invokeInt(QueryUsingPoolDUnitTest.class, "getCacheServerPort");
++    final int port = vm0.invoke(() -> QueryUsingPoolDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +    final String regionName1 = "/" + rootRegionName + "/" + name+"1";
 +    final String regionName2 = "/" + rootRegionName + "/" + name+"2";
 +
 +    // Create client pool.
 +    final String poolName = "testRemoteJoinRegionQueries"; 
 +    createPool(vm1, poolName, host0, port);
 +
 +    // Execute client queries
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        String queryString = null;
 +        SelectResults results = null;
 +        QueryService qService = null;
 +
 +        try {
 +          qService = (PoolManager.find(poolName)).getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        }          
 +
 +        queryString =
 +          "select distinct a, b.price from " + regionName1 + " a, " + regionName2 + " b where a.price = b.price";
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(numberOfEntries, results.size());
 +        assertTrue(!results.getCollectionType().allowsDuplicates() && results.getCollectionType().getElementType().isStructType());
 +
 +        queryString =
 +          "select distinct a, b.price from " + regionName1 + " a, " + regionName2 + " b where a.price = b.price and a.price = 50";
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(1, results.size());
 +        assertTrue(!results.getCollectionType().allowsDuplicates() && results.getCollectionType().getElementType().isStructType());
 +      }
 +    });
 +
 +    // Stop server
 +    vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +      public void run() {
 +        stopBridgeServer(getCache());
 +      }
 +    });
 +  }
 +
 +  /**
 +   * Tests remote query execution using a BridgeClient as the CacheWriter
 +   * and CacheLoader.
 +   */
 +  public void testRemoteBridgeClientQueries() throws CacheException {
 +
 +    final String name = this.getName();
 +    final String rootRegionName = "root";
 +
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    final int numberOfEntries = 100;
 +
 +    // Start server
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        Properties config = new Properties();
 +        config.setProperty("locators", "localhost["+DistributedTestUtils.getDUnitLocatorPort()+"]");
 +        system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        createRegion(name, factory.create());
 +        Wait.pause(1000);
 +        try {
 +          startBridgeServer(0, false);
 +        } catch (Exception ex) {
 +          Assert.fail("While starting CacheServer", ex);
 +        }
 +      }
 +    });
 +
 +    // Initialize server region
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion().getSubregion(name);
 +        for (int i=0; i<numberOfEntries; i++) {
 +          region.put("key-"+i, new TestObject(i, "ibm"));
 +        }
 +      }
 +    });
 +
-     final int port = vm0.invokeInt(QueryUsingPoolDUnitTest.class, "getCacheServerPort");
++    final int port = vm0.invoke(() -> QueryUsingPoolDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +
 +    final String regionName = "/" + rootRegionName + "/" + name;
 +
 +    // Create client pool.
 +    final String poolName1 = "testRemoteBridgeClientQueries1"; 
 +    final String poolName2 = "testRemoteBridgeClientQueries2"; 
 +
 +    createPool(vm1, poolName1, host0, port);
 +    createPool(vm2, poolName2, host0, port);
 +
 +    // Execute client queries in VM1
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        String queryString = null;
 +        SelectResults results = null;
 +        QueryService qService = null;
 +
 +        try {
 +          qService = (PoolManager.find(poolName1)).getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        }          
 +
 +        queryString = "SELECT DISTINCT itr.value FROM " + regionName + ".entries itr where itr.key = 'key-1'";
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(1, results.size());
 +        assertTrue(!results.getCollectionType().allowsDuplicates() && !results.getCollectionType().getElementType().isStructType());
 +        assertTrue(results.asList().get(0) instanceof TestObject);
 +
 +        queryString = "SELECT DISTINCT itr.key FROM " + regionName + ".entries itr where itr.key = 'key-1'";
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(1, results.size());
 +        assertTrue(!results.getCollectionType().allowsDuplicates() && !results.getCollectionType().getElementType().isStructType());
 +        assertEquals("key-1", results.asList().get(0));
 +      }
 +    });
 +
 +    // Execute client queries in VM2
 +    vm2.invoke(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        String queryString = null;
 +        SelectResults results = null;
 +        QueryService qService = null;
 +
 +        try {
 +          qService = (PoolManager.find(poolName2)).getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        }          
 +
 +        queryString = "SELECT DISTINCT itr.value FROM " + regionName + ".entries itr where itr.key = 'key-1'";
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(1, results.size());
 +        assertTrue(!results.getCollectionType().allowsDuplicates() && !results.getCollectionType().getElementType().isStructType());
 +        assertTrue(results.asList().get(0) instanceof TestObject);
 +
 +        queryString = "SELECT DISTINCT itr.key FROM " + regionName + ".entries itr where itr.key = 'key-1'";
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          results = (SelectResults)query.execute();
 +        } catch (Exception e) {
 +          Assert.fail("Failed executing " + queryString, e);
 +        }
 +        assertEquals(1, results.size());
 +        assertTrue(!results.getCollectionType().allowsDuplicates() && !results.getCollectionType().getElementType().isStructType());
 +        assertEquals("key-1", results.asList().get(0));
 +      }
 +    });
 +
 +    // Stop server
 +    vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +      public void run() {
 +        stopBridgeServer(getCache());
 +      }
 +    });
 +  }
 +
 +  /**
 +   * This the dunit test for the bug no : 36969
 +   * @throws Exception
 +   */
 +
 +  public void testBug36969() throws Exception
 +  {
 +    final String name = this.getName();
 +    final String rootRegionName = "root";
 +
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +
 +    final int numberOfEntries = 100;
 +
 +    // Start server
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        Properties config = new Properties();
 +        config.setProperty("locators", "localhost["+DistributedTestUtils.getDUnitLocatorPort()+"]");
 +        system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +
 +        Wait.pause(1000);
 +        try {
 +          startBridgeServer(0, false);
 +        } catch (Exception ex) {
 +          Assert.fail("While starting CacheServer", ex);
 +        }
 +      }
 +    });
 +
 +    vm0.invoke(new CacheSerializableRunnable("Create two regions") {
 +      public void run2() throws CacheException {
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        final Region region1 = createRegion(name, factory.createRegionAttributes());
 +        final Region region2 = createRegion(name+"_2", factory.createRegionAttributes());
 +        QueryObserverHolder.setInstance(new QueryObserverAdapter() {
 +          public void afterQueryEvaluation(Object result) {
 +            //Destroy the region in the test
 +            region1.close();
 +          }
 +
 +        });
 +        Wait.pause(1000);
 +        for (int i=0; i<numberOfEntries; i++) {
 +          region1.put("key-"+i, new TestObject(i, "ibm"));
 +          region2.put("key-"+i, new TestObject(i, "ibm"));
 +        }
 +
 +      }
 +    });
 +
-     final int port = vm0.invokeInt(QueryUsingPoolDUnitTest.class, "getCacheServerPort");
++    final int port = vm0.invoke(() -> QueryUsingPoolDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +
 +    final String regionName1 = "/" + rootRegionName + "/" + name;
 +    final String regionName2 = "/" + rootRegionName + "/" + name + "_2";
 +
 +    // Create client pool.
 +    final String poolName = "testBug36969";
 +    createPool(vm1, poolName, host0, port);
 +
 +    // Execute client queries in VM1      
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        String queryString = "select distinct * from " + regionName1 + ", "+regionName2;
 +//        SelectResults results = null;
 +        QueryService qService = null;
 +
 +        try {
 +          qService = (PoolManager.find(poolName)).getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        }
 +
 +        try {
 +          Query query = qService.newQuery(queryString);
 +          query.execute();
 +          fail("The query should have experienced RegionDestroyedException");
 +        } catch(Exception e) {
 +          // OK
 +        }
 +      }
 +    });
 +
 +    // Stop server
 +    vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +      public void run() {
 +        QueryObserverHolder.setInstance(new QueryObserverAdapter());
 +        stopBridgeServer(getCache());
 +      }
 +    });
 +
 +
 +
 +  }
 +
 +  /**
 +   * Tests remote full region query execution.
 +   */
 +  public void testRemoteSortQueriesUsingIndex() throws CacheException {
 +
 +    final String name = this.getName();
 +    final String rootRegionName = "root";
 +
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    final int numberOfEntries = 100;
 +
 +    // Start server
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        Properties config = new Properties();
 +        config.setProperty("locators", "localhost["+DistributedTestUtils.getDUnitLocatorPort()+"]");
 +        system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        createRegion(name, factory.create());
 +        Wait.pause(1000);
 +        try {
 +          startBridgeServer(0, false);
 +        } catch (Exception ex) {
 +          Assert.fail("While starting CacheServer", ex);
 +        }
 +      }
 +    });
 +
 +    // Initialize server region
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion().getSubregion(name);
 +        for (int i=0; i<numberOfEntries; i++) {
 +          region.put("key-"+i, new TestObject(i, "ibm"));
 +        }
 +        // Create index
 +        try {
 +          QueryService qService = region.getCache().getQueryService();
 +          qService.createIndex("idIndex",IndexType.FUNCTIONAL, "id", region.getFullPath());
 +        } catch (Exception e) {
 +          Assert.fail("Failed to create index.", e);
 +        }          
 +
 +      }
 +    });
 +
 +    // Create client region
-     final int port = vm0.invokeInt(QueryUsingPoolDUnitTest.class, "getCacheServerPort");
++    final int port = vm0.invoke(() -> QueryUsingPoolDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +
 +    final String regionName = "/" + rootRegionName + "/" + name;
 +
 +    // Create client pool.
 +    final String poolName = "testRemoteFullRegionQueries"; 
 +    createPool(vm1, poolName, host0, port);
 +
 +
 +    // Execute client queries
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        String queryString = null;
 +        SelectResults results = null;
 +        Comparator comparator = null;
 +        Object[] resultsArray = null;
 +        QueryService qService = null;
 +        Integer v1 = 0;
 +        Integer v2 = 0;
 +        
 +        try {
 +          qService = (PoolManager.find(poolName)).getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        }          
 +
 +        // order by value query
 +        String[] qString = {
 +            "SELECT DISTINCT * FROM " + regionName + " WHERE id < 101 ORDER BY id",
 +            "SELECT DISTINCT id FROM " + regionName + " WHERE id < 101 ORDER BY id",
 +        };
 +        
 +        for (int cnt=0; cnt < qString.length; cnt++) {
 +          queryString = qString[cnt];
 +          try {
 +            Query query = qService.newQuery(queryString);
 +            results = (SelectResults)query.execute();
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(numberOfEntries, results.size());
 +          // All order-by query results are stored in a ResultsCollectionWrapper
 +          // wrapping a list, so the assertion below is not correct even though
 +          // it should be.
 +          //assertTrue(!results.getCollectionType().allowsDuplicates());
 +          assertTrue(results.getCollectionType().isOrdered());
 +          comparator = new IdValueComparator();
 +
 +          resultsArray = results.toArray();
 +          for (int i=0; i<resultsArray.length; i++) {
 +            if (i+1 != resultsArray.length) {
 +              // The id of the current element in the result set must be less
 +              // than the id of the next one to pass.
 +              if (resultsArray[i] instanceof TestObject) {
 +                v1 = ((TestObject)resultsArray[i]).getId();
 +                v2 = ((TestObject)resultsArray[i+1]).getId();
 +              } else {
 +                v1 = (Integer)resultsArray[i];
 +                v2 = (Integer)resultsArray[i + 1];
 +              }
 +              assertTrue("The id for " + resultsArray[i] + " should be less than the id for " + resultsArray[i+1], 
 +                  comparator.compare(v1, v2) == -1);
 +            }
 +          }
 +        }
 +      
 +        // order by struct query
 +        String[] qString2 = {
 +            "SELECT DISTINCT id, ticker, price FROM " + regionName + " WHERE id < 101 ORDER BY id",
 +            "SELECT DISTINCT ticker, id FROM " + regionName + " WHERE id < 101  ORDER BY id",
 +            "SELECT DISTINCT id, ticker FROM " + regionName + " WHERE id < 101  ORDER BY id asc",
 +        };
 +
 +        for (int cnt=0; cnt < qString2.length; cnt++) {
 +          queryString = qString2[cnt];
 +          try {
 +            Query query = qService.newQuery(queryString);
 +            results = (SelectResults)query.execute();
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(numberOfEntries, results.size());
 +          // All order-by query results are stored in a ResultsCollectionWrapper
 +          // wrapping a list, so the assertion below is not correct even though
 +          // it should be.
 +          //assertTrue(!results.getCollectionType().allowsDuplicates());
 +          assertTrue(results.getCollectionType().isOrdered());
 +          comparator = new StructIdComparator();
 +          resultsArray = results.toArray();
 +          for (int i=0; i<resultsArray.length; i++) {
 +            if (i+1 != resultsArray.length) {
 +              // The id of the current element in the result set must be less
 +              // than the id of the next one to pass.
 +              assertTrue("The id for " + resultsArray[i] + " should be less than the id for " + resultsArray[i+1], comparator.compare(resultsArray[i], resultsArray[i+1]) == -1);
 +            }
 +          }
 +        }
 +      }
 +    });
 +
 +    // Stop server
 +    vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +      public void run() {
 +        stopBridgeServer(getCache());
 +      }
 +    });
 +  }
 +
 +
 +  public void testUnSupportedOps() throws Exception
 +  {
 +    final String name = this.getName();
 +    final String rootRegionName = "root";
 +
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +
 +    final int numberOfEntries = 100;
 +
 +    // Start server
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        Properties config = new Properties();
 +        config.setProperty("locators", "localhost["+DistributedTestUtils.getDUnitLocatorPort()+"]");
 +        system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +
 +        Wait.pause(1000);
 +        try {
 +          startBridgeServer(0, false);
 +        } catch (Exception ex) {
 +          Assert.fail("While starting CacheServer", ex);
 +        }
 +      }
 +    });
 +
 +    vm0.invoke(new CacheSerializableRunnable("Create two regions") {
 +      public void run2() throws CacheException {
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        final Region region1 = createRegion(name, factory.createRegionAttributes());
 +        final Region region2 = createRegion(name+"_2", factory.createRegionAttributes());
 +        for (int i=0; i<numberOfEntries; i++) {
 +          region1.put("key-"+i, new TestObject(i, "ibm"));
 +          region2.put("key-"+i, new TestObject(i, "ibm"));
 +        }
 +
 +      }
 +    });
 +
-     final int port = vm0.invokeInt(QueryUsingPoolDUnitTest.class, "getCacheServerPort");
++    final int port = vm0.invoke(() -> QueryUsingPoolDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +
 +    final String regionName1 = "/" + rootRegionName + "/" + name;
 +//    final String regionName2 = "/" + rootRegionName + "/" + name + "_2";
 +
 +    // Create client pool.
 +    final String poolName = "testUnSupportedOps";
 +    createPool(vm1, poolName, host0, port);
 +
 +    // Execute client queries in VM1      
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        final Region region1 = createRegion(name, factory.createRegionAttributes());
 +
 +        String queryString = "select distinct * from " + regionName1 + " where ticker = $1";
 +        Object[] params = new Object[] { new String("ibm")};
 +//        SelectResults results = null;
 +        QueryService qService = null;
 +
 +        try {
 +          qService = (PoolManager.find(poolName)).getQueryService();
 +        } catch (Exception e) {
 +          Asse

<TRUNCATED>


[11/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache/query/internal/index/IndexUseJUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache/query/internal/index/IndexUseJUnitTest.java
index c3223d8,0000000..b171ac1
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache/query/internal/index/IndexUseJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache/query/internal/index/IndexUseJUnitTest.java
@@@ -1,1890 -1,0 +1,1654 @@@
 +/*
 + * 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.
 + */
- /*
-  * IndexTest.java
-  *
-  * Created on February 23, 2005, 3:17 PM
-  */
 +package com.gemstone.gemfire.cache.query.internal.index;
 +
 +import static org.junit.Assert.assertEquals;
 +import static org.junit.Assert.assertFalse;
 +import static org.junit.Assert.assertTrue;
 +import static org.junit.Assert.fail;
 +
 +import java.util.ArrayList;
 +import java.util.Collection;
 +import java.util.HashMap;
 +import java.util.Iterator;
 +import java.util.List;
 +import java.util.Map;
 +
 +import org.junit.After;
 +import org.junit.Before;
 +import org.junit.Test;
 +import org.junit.experimental.categories.Category;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
++import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.PartitionAttributesFactory;
 +import com.gemstone.gemfire.cache.Region;
++import com.gemstone.gemfire.cache.RegionShortcut;
 +import com.gemstone.gemfire.cache.query.CacheUtils;
 +import com.gemstone.gemfire.cache.query.Index;
 +import com.gemstone.gemfire.cache.query.IndexType;
 +import com.gemstone.gemfire.cache.query.Query;
 +import com.gemstone.gemfire.cache.query.QueryInvalidException;
 +import com.gemstone.gemfire.cache.query.QueryService;
 +import com.gemstone.gemfire.cache.query.SelectResults;
 +import com.gemstone.gemfire.cache.query.Struct;
 +import com.gemstone.gemfire.cache.query.data.Portfolio;
 +import com.gemstone.gemfire.cache.query.functional.StructSetOrResultsSet;
 +import com.gemstone.gemfire.cache.query.internal.QueryObserverAdapter;
 +import com.gemstone.gemfire.cache.query.internal.QueryObserverHolder;
 +import com.gemstone.gemfire.cache.query.internal.index.IndexManager.TestHook;
 +import com.gemstone.gemfire.internal.cache.LocalRegion;
 +import com.gemstone.gemfire.test.junit.categories.IntegrationTest;
 +
 +/**
 + * 
 + * @author vaibhav
 + */
 +@Category(IntegrationTest.class)
 +public class IndexUseJUnitTest
 +{
 +
 +  Region region;
 +
 +  QueryService qs;
 +
 +  @Before
 +  public void setUp() throws Exception {
 +    CacheUtils.startCache();
 +    region = CacheUtils.createRegion("pos", Portfolio.class);
 +    region.put("0", new Portfolio(0));
 +    region.put("1", new Portfolio(1));
 +    region.put("2", new Portfolio(2));
 +    region.put("3", new Portfolio(3));
 +
 +    qs = CacheUtils.getQueryService();
 +    qs.createIndex("statusIndex", IndexType.FUNCTIONAL, "status", "/pos");
 +    qs.createIndex("idIndex", IndexType.FUNCTIONAL, "ID", "/pos");
 +    qs.createIndex("secIdIndex", IndexType.FUNCTIONAL, "P1.secId", "/pos");
 +    qs.createIndex("secIdIndex2", IndexType.FUNCTIONAL, "P2.secId", "/pos");
 +    qs.createIndex("p1secindex", "p.position1.secId", " /pos p ");
 +  }
 +
 +  @After
 +  public void tearDown() throws Exception {
 +    CacheUtils.closeCache();
 +    IndexManager indexManager = ((LocalRegion)region).getIndexManager();
 +    if (indexManager != null)
 +      indexManager.destroy();
 +    IndexManager.TEST_RANGEINDEX_ONLY = false;
 +    
 +  }
 +
 +  @Test
 +  public void testIndexUseSingleCondition() throws Exception {
 +    String testData[][] = { { "status", "'active'" }, { "ID", "2" },
 +        { "P1.secId", "'IBM'" }, };
 +    String operators[] = { "=", "<>", "!=", "<", "<=", ">", ">=" };
 +    for (int i = 0; i < operators.length; i++) {
 +      String operator = operators[i];
 +      for (int j = 0; j < testData.length; j++) {
 +        Query q = qs.newQuery("SELECT DISTINCT * FROM /pos where "
 +            + testData[j][0] + " " + operator + " " + testData[j][1]);
 +        QueryObserverImpl observer = new QueryObserverImpl();
 +        QueryObserverHolder.setInstance(observer);
 +        q.execute();
 +        if (!observer.isIndexesUsed) {
-           fail("Index not uesd for operator '" + operator + "'");
++          fail("Index not used for operator '" + operator + "'");
 +        }
 +      }
 +    }
 +  }
 +
 +  @Test
 +  public void testIndexUseMultipleConditions() throws Exception {
 +    String testData[][] = { { "P1.secType = 'a'", "0" },
 +        { "status = 'active' AND ID = 2", "1" },
 +        { "status = 'active' AND ID = 2 AND P1.secId  = 'IBM'", "1" },
 +        { "status = 'active' OR ID = 2", "2" },
 +        { "status = 'active' OR ID = 2 OR P1.secId  = 'IBM'", "3" },
 +        { "status = 'active' AND ID = 2 OR P1.secId  = 'IBM'", "2" },
 +        { "status = 'active' AND ( ID = 2 OR P1.secId  = 'IBM')", "1" },
 +        { "status = 'active' OR ID = 2 AND P1.secId  = 'IBM'", "2" },
 +        { "(status = 'active' OR ID = 2) AND P1.secId  = 'IBM'", "1" },
 +        { "NOT (status = 'active') AND ID = 2", "1" },
 +        { "status = 'active' AND NOT( ID = 2 )", "1" },
 +        { "NOT (status = 'active') OR ID = 2", "2" },
 +        { "status = 'active' OR NOT( ID = 2 )", "2" },
 +        { "status = 'active' AND P1.secType = 'a'", "1" },
 +        { "status = 'active' OR P1.secType = 'a'", "0" },
 +        { "status = 'active' AND ID =1 AND P1.secType = 'a'", "1" },
 +        { "status = 'active' AND ID = 1 OR P1.secType = 'a'", "0" },
 +        { "status = 'active' OR ID = 1 AND P1.secType = 'a'", "2" },
 +        { "P2.secId = null", "1" }, { "IS_UNDEFINED(P2.secId)", "1" },
 +        { "IS_DEFINED(P2.secId)", "1" }, { "P2.secId = UNDEFINED", "0" }, };
 +    for (int j = 0; j < testData.length; j++) {
 +      Query q = qs.newQuery("SELECT DISTINCT * FROM /pos where "
 +          + testData[j][0]);
 +      QueryObserverImpl observer = new QueryObserverImpl();
 +      QueryObserverHolder.setInstance(observer);
 +      q.execute();
 +      if (observer.indexesUsed.size() != Integer.parseInt(testData[j][1])) {
 +        fail("Wrong Index use for " + testData[j][0] + "\n Indexes used "
 +            + observer.indexesUsed);
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Test to check if Region object is passed as bind argument, the index
 +   * utilization occurs or not
 +   * @author ashahid
 +   */
 +  @Test
 +  public void testBug36421_part1() {
 +    try {
 +      String testData[][] = { { "status", "'active'" }, };
 +      String operators[] = { "=" };
 +      for (int i = 0; i < operators.length; i++) {
 +        String operator = operators[i];
 +        for (int j = 0; j < testData.length; j++) {
 +          Query q = qs.newQuery("SELECT DISTINCT * FROM $1 where "
 +              + testData[j][0] + " " + operator + " " + testData[j][1]);
 +          QueryObserverImpl observer = new QueryObserverImpl();
 +          QueryObserverHolder.setInstance(observer);
 +          q.execute(new Object[] { CacheUtils.getRegion("/pos") });
 +          if (!observer.isIndexesUsed) {
-             fail("Index not uesd for operator '" + operator + "'");
++            fail("Index not used for operator '" + operator + "'");
 +          }
 +        }
 +      }
 +    }
 +    catch (Exception e) {
 +      CacheUtils.getLogger().error(e);
-       fail("Test faield due to exception =" + e);
++      fail("Test failed due to exception =" + e);
 +    }
 +  }
 +
 +  /**
 +   * Test to check if Region short cut method is used for querying, the index
 +   * utilization occurs or not
 +   * @author ashahid
 +   */
 +  @Test
 +  public void testBug36421_part2() {
 +    try {
 +      String testData[][] = { { "status", "'active'" }, };
 +      String operators[] = { "=" };
 +      for (int i = 0; i < operators.length; i++) {
 +        String operator = operators[i];
 +        for (int j = 0; j < testData.length; j++) {
 +
 +          QueryObserverImpl observer = new QueryObserverImpl();
 +          QueryObserverHolder.setInstance(observer);
 +          CacheUtils.getRegion("/pos").query(
 +              testData[j][0] + " " + operator + " " + testData[j][1]);
 +          if (!observer.isIndexesUsed) {
-             fail("Index not uesd for operator '" + operator + "'");
++            fail("Index not used for operator '" + operator + "'");
 +          }
 +        }
 +      }
 +    }
 +    catch (Exception e) {
 +      CacheUtils.getLogger().error(e);
 +      fail("Test failed due to exception =" + e);
 +    }
 +  }
 +
 +  /**
 +   * Test to check if a parametrized query when using different bind arguments
 +   * of Region uses the index correctly
 +   * @author ashahid
 +   */
 +  @Test
 +  public void testBug36421_part3() {
 +
 +    Query q = null;
 +    try {
 +      q = qs.newQuery("SELECT DISTINCT * FROM $1 z where z.status = 'active'");
 +      QueryObserverImpl observer = new QueryObserverImpl();
 +      QueryObserverHolder.setInstance(observer);
 +      q.execute(new Object[] { CacheUtils.getRegion("/pos") });
 +      if (!observer.isIndexesUsed) {
 +        fail("Index not uesd for operator '='");
 +      }
 +      assertTrue(observer.indexesUsed.get(0).equals("statusIndex"));
 +      region = CacheUtils.createRegion("pos1", Portfolio.class);
 +      region.put("0", new Portfolio(0));
 +      region.put("1", new Portfolio(1));
 +      region.put("2", new Portfolio(2));
 +      region.put("3", new Portfolio(3));
 +      qs.createIndex("statusIndex1", IndexType.FUNCTIONAL, "pf1.status",
 +          "/pos1 pf1");
 +      region.put("4", new Portfolio(4));
 +      observer = new QueryObserverImpl();
 +      QueryObserverHolder.setInstance(observer);
 +      q.execute(new Object[] { CacheUtils.getRegion("/pos1") });
 +      if (!observer.isIndexesUsed) {
-         fail("Index not uesd for operator'='");
++        fail("Index not used for operator'='");
 +      }
 +      assertTrue(observer.indexesUsed.get(0).equals("statusIndex1"));
 +
 +    }
 +    catch (Exception e) {
 +      CacheUtils.getLogger().error(e);
 +      fail("Test failed due to exception =" + e);
 +    }
 +  }
 +
 +  /**
 +   * Test to check if Region short cut method is used for querying, the Primary
 +   * key index utilization occurs or not 
 +   * @author ashahid
 +   */
 +  @Test
 +  public void testBug36421_part4() {
 +//    Query q = null;
 +    try {
 +      qs.createIndex("pkIndex", IndexType.PRIMARY_KEY, "pk", "/pos");
 +      QueryObserverImpl observer = new QueryObserverImpl();
 +      QueryObserverHolder.setInstance(observer);
 +      SelectResults rs = CacheUtils.getRegion("/pos").query("pk = '2'");
 +      if (!observer.isIndexesUsed) {
 +        fail("Index not uesd for operator '='");
 +      }
 +      assertTrue(rs.size() == 1);
 +      assertTrue(((Portfolio)rs.iterator().next()).pkid.equals("2"));
 +      assertTrue(observer.indexesUsed.get(0).equals("pkIndex"));
 +      CacheUtils.getRegion("/pos").put("7", new Portfolio(7));
 +      observer = new QueryObserverImpl();
 +      QueryObserverHolder.setInstance(observer);
 +      rs = CacheUtils.getRegion("/pos").query("pk = '7'");
 +      if (!observer.isIndexesUsed) {
-         fail("Index not uesd for operator '='");
++        fail("Index not used for operator '='");
 +      }
 +      assertTrue(rs.size() == 1);
 +      assertTrue(((Portfolio)rs.iterator().next()).pkid.equals("7"));
 +      assertTrue(observer.indexesUsed.get(0).equals("pkIndex"));
 +    }
 +    catch (Exception e) {
 +      CacheUtils.getLogger().error(e);
 +      fail("Test failed due to exception =" + e);
 +    }
 +  }
 +  
 +  @Test
 +  public void testMapIndexUsageAllKeys() throws Exception {
++    try {
++      IndexManager.TEST_RANGEINDEX_ONLY = true;
++      evaluateMapTypeIndexUsageAllKeys();
++    }
++    finally {
++      IndexManager.TEST_RANGEINDEX_ONLY = false;
++    }
++  }
++  
++  private void evaluateMapTypeIndexUsageAllKeys() throws Exception {
 +    QueryService qs;
 +    qs = CacheUtils.getQueryService();
 +    LocalRegion testRgn = (LocalRegion)CacheUtils.createRegion("testRgn", null);
 +    int ID =1;
 +    //Add some test data now
 +    //Add 5 main objects. 1 will contain key1, 2 will contain key1 & key2
 +    // and so on
 +    for(; ID <=30; ++ID) {
 +      MapKeyIndexData mkid = new MapKeyIndexData(ID);
 +      for(int j =1; j<= ID;++j) {
 +        mkid.maap.put("key1", j*1);
 +        mkid.maap.put("key2", j*2);
 +        mkid.maap.put("key3", j*3);
 +      }
 +      testRgn.put(ID, mkid);
 +    }
 +    String queries[] = {
 +        "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key2'] >= 16"
 +       /* "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap.get('key2') >= 16" ,*/        
 +    };
 +    String queriesIndexNotUsed[] = {        
 +         "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap.get('key2') >= 16"         
 +     };
 +    Object r[][]= new Object[queries.length][2];
 +    
 +    qs = CacheUtils.getQueryService();
 +    
 +  //Execute Queries without Indexes
 +    for (int i = 0; i < queries.length; i++) {
 +        Query q = null;
 +        try {
 +            q = CacheUtils.getQueryService().newQuery(queries[i]);
 +            CacheUtils.getLogger().info("Executing query: " + queries[i]);              
 +            r[i][0] = q.execute();         
 +          CacheUtils.log("Executed query: " + queries[i] );
 +        } catch (Exception e) {
 +            e.printStackTrace();
 +            fail(q.getQueryString());
 +        }
 +    }  
 +    
 +    Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL, "objs.maap[*]",
 +        "/testRgn objs");
 +    
 +          //Execute Queries with Indexes
 +      for (int i = 0; i < queries.length; i++) {
 +          Query q = null;
 +          try {
 +              q = CacheUtils.getQueryService().newQuery(queries[i]);
 +              CacheUtils.getLogger().info("Executing query: " + queries[i]);
 +              QueryObserverImpl observer = new QueryObserverImpl();
 +              QueryObserverHolder.setInstance(observer);
 +              r[i][1] = q.execute();
 +              CacheUtils.log("Executing query: " + queries[i] + " with index created");
 +              if(!observer.isIndexesUsed){
-                   fail("Index is NOT uesd");
++                  fail("Index is NOT used");
 +              }              
 +              Iterator itr = observer.indexesUsed.iterator();
 +              assertTrue(itr.hasNext());
 +              String temp = itr.next().toString();
 +              assertEquals(temp,"Index1");       
 +              
 +          } catch (Exception e) {
 +              e.printStackTrace();
 +              fail(q.getQueryString());
 +          }
 +      }
 +      StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
 +      ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r,queries.length,queries);
 +      
 +      //Test queries index not used
 +      for (int i = 0; i < queriesIndexNotUsed.length; i++) {
 +        Query q = null;
 +        try {
 +            q = CacheUtils.getQueryService().newQuery(queriesIndexNotUsed[i]);
 +            CacheUtils.getLogger().info("Executing query: " + queriesIndexNotUsed[i]);
 +            QueryObserverImpl observer = new QueryObserverImpl();
 +            QueryObserverHolder.setInstance(observer);            
 +            CacheUtils.log("Executing query: " + queriesIndexNotUsed[i] + " with index created");
 +            q.execute();
 +            assertFalse(observer.isIndexesUsed);                          
 +            Iterator itr = observer.indexesUsed.iterator();
 +            assertFalse(itr.hasNext());                   
 +            
 +        } catch (Exception e) {
 +            e.printStackTrace();
 +            fail(q.getQueryString());
 +        }
 +    }
 +      
 +  }
 +  
 +  @Test
 +  public void testCompactMapIndexUsageWithIndexOnSingleKey() throws Exception
 +  {
-     QueryService qs;
-     qs = CacheUtils.getQueryService();
++    String queries[] = {
++        "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key2'] >= 3"};
++    String queriesIndexNotUsed[] = {
++        "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap.get('key2') >= 3"};
++
++    
 +    LocalRegion testRgn = (LocalRegion)CacheUtils.createRegion("testRgn", null);
 +    int ID = 1;
 +    // Add some test data now
 +    // Add 5 main objects. 1 will contain key1, 2 will contain key1 & key2
 +    // and so on
 +    for (; ID <= 30; ++ID) {
 +      MapKeyIndexData mkid = new MapKeyIndexData(ID);
 +      for (int j = 1; j <= ID; ++j) {
 +        mkid.addKeyValue("key1", j * 1);
 +        mkid.addKeyValue("key2", j * 2);
 +        mkid.addKeyValue("key3", j * 3);
 +      }
 +      testRgn.put(ID, mkid);
 +    }
- 
-     qs = CacheUtils.getQueryService();
-     String queries[] = { "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key2'] >= 3" };
- 
-     String queriesIndexNotUsed[] = {
-         "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap.get('key2') >= 3",
-         "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key3'] >= 3", };
- 
-     Object r[][] = new Object[queries.length][2];
- 
-     // Execute Queries without Indexes
-     for (int i = 0; i < queries.length; i++) {
-       Query q = null;
-       try {
-         q = CacheUtils.getQueryService().newQuery(queries[i]);
-         CacheUtils.getLogger().info("Executing query: " + queries[i]);
-         r[i][0] = q.execute();
-         CacheUtils.log("Executed query: " + queries[i]);
-       }
-       catch (Exception e) {
-         e.printStackTrace();
-         fail(q.getQueryString());
-       }
-     }
- 
-     Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL,
-         "objs.maap['key2']", "/testRgn objs");
-     assertTrue(i1 instanceof CompactRangeIndex);
-     // Execute Queries with Indexes
-     for (int i = 0; i < queries.length; i++) {
-       Query q = null;
-       try {
-         q = CacheUtils.getQueryService().newQuery(queries[i]);
-         CacheUtils.getLogger().info("Executing query: " + queries[i]);
-         QueryObserverImpl observer = new QueryObserverImpl();
-         QueryObserverHolder.setInstance(observer);
-         r[i][1] = q.execute();
-         CacheUtils.log("Executing query: " + queries[i]
-             + " with index created");
-         if (!observer.isIndexesUsed) {
-           fail("Index is NOT uesd");
-         }
-         Iterator itr = observer.indexesUsed.iterator();
-         assertTrue(itr.hasNext());
-         String temp = itr.next().toString();
-         assertEquals(temp, "Index1");
- 
-       }
-       catch (Exception e) {
-         e.printStackTrace();
-         fail(q.getQueryString());
-       }
-     }
-     StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
-     ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r, queries.length,queries);
- 
-     // Test queries index not used
-     for (int i = 0; i < queriesIndexNotUsed.length; i++) {
-       Query q = null;
-       try {
-         q = CacheUtils.getQueryService().newQuery(queriesIndexNotUsed[i]);
-         CacheUtils.getLogger().info(
-             "Executing query: " + queriesIndexNotUsed[i]);
-         QueryObserverImpl observer = new QueryObserverImpl();
-         QueryObserverHolder.setInstance(observer);
-         CacheUtils.log("Executing query: " + queriesIndexNotUsed[i]
-             + " with index created");
-         q.execute();
-         assertFalse(observer.isIndexesUsed);
-         Iterator itr = observer.indexesUsed.iterator();
-         assertFalse(itr.hasNext());
- 
-       }
-       catch (Exception e) {
-         e.printStackTrace();
-         fail(q.getQueryString());
-       }
-     }
++    
++    evaluateMapTypeIndexUsage("objs.maap['key2']", "/testRgn objs", queries, queriesIndexNotUsed, CompactRangeIndex.class);
 +
 +    String query = "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.liist[0] >= 2";
 +    SelectResults withoutIndex, withIndex;
 +    Query q = CacheUtils.getQueryService().newQuery(query);
 +    CacheUtils.getLogger().info("Executing query: " + query);
 +    withoutIndex = (SelectResults)q.execute();
 +    CacheUtils.log("Executed query: " + query);
 +
 +    Index i2 = qs.createIndex("Index2", IndexType.FUNCTIONAL, "objs.liist[0]",
 +        "/testRgn objs");
 +    assertTrue(i2 instanceof CompactRangeIndex);
 +    CacheUtils.getLogger().info("Executing query: " + query);
 +    QueryObserverImpl observer = new QueryObserverImpl();
 +    QueryObserverHolder.setInstance(observer);
 +    withIndex = (SelectResults)q.execute();
 +    CacheUtils.log("Executing query: " + query + " with index created");
 +    if (!observer.isIndexesUsed) {
 +      fail("Index is NOT uesd");
 +    }
 +    Iterator itr = observer.indexesUsed.iterator();
 +    assertTrue(itr.hasNext());
 +    String temp = itr.next().toString();
 +    assertEquals(temp, "Index2");
 +
-     ssOrrs = new StructSetOrResultsSet();
++    StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
 +    ssOrrs.CompareQueryResultsWithoutAndWithIndexes(new Object[][] { {
 +        withoutIndex, withIndex } }, 1,queries);
 +
 +  }
 +  @Test
 +  public void testCompactMapIndexUsageWithIndexOnMultipleKeys() throws Exception
 +  {
-     QueryService qs;
-     qs = CacheUtils.getQueryService();
-     LocalRegion testRgn = (LocalRegion)CacheUtils.createRegion("testRgn", null);
-     int ID = 1;
-     // Add some test data now
-     // Add 5 main objects. 1 will contain key1, 2 will contain key1 & key2
-     // and so on
-     for (; ID <= 30; ++ID) {
-       MapKeyIndexData mkid = new MapKeyIndexData(ID);
-       for (int j = 1; j <= ID; ++j) {
-         mkid.addKeyValue("key1", j * 1);
-         mkid.addKeyValue("key2", j * 2);
-         mkid.addKeyValue("key3", j * 3);
-       }
-       testRgn.put(ID, mkid);
-     }
- 
-     qs = CacheUtils.getQueryService();
 +    String queries[] = {
 +        "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key2'] >= 3",
 +        "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key3'] >= 3" };
- 
 +    String queriesIndexNotUsed[] = {
 +        "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap.get('key2') >= 16",
 +        "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key4'] >= 16", };
 +
-     Object r[][] = new Object[queries.length][2];
- 
-     // Execute Queries without Indexes
-     for (int i = 0; i < queries.length; i++) {
-       Query q = null;
-       try {
-         q = CacheUtils.getQueryService().newQuery(queries[i]);
-         CacheUtils.getLogger().info("Executing query: " + queries[i]);
-         r[i][0] = q.execute();
-         CacheUtils.log("Executed query: " + queries[i]);
-       }
-       catch (Exception e) {
-         e.printStackTrace();
-         fail(q.getQueryString());
-       }
-     }
- 
-     Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL,
-         "objs.maap['key2','key3']", "/testRgn objs");
-     assertTrue(i1 instanceof CompactMapRangeIndex);
-     // Execute Queries with Indexes
-     for (int i = 0; i < queries.length; i++) {
-       Query q = null;
-       try {
-         q = CacheUtils.getQueryService().newQuery(queries[i]);
-         CacheUtils.getLogger().info("Executing query: " + queries[i]);
-         QueryObserverImpl observer = new QueryObserverImpl();
-         QueryObserverHolder.setInstance(observer);
-         r[i][1] = q.execute();
-         CacheUtils.log("Executing query: " + queries[i]
-             + " with index created");
-         if (!observer.isIndexesUsed) {
-           fail("Index is NOT uesd");
-         }
-         Iterator itr = observer.indexesUsed.iterator();
-         assertTrue(itr.hasNext());
-         String temp = itr.next().toString();
-         assertEquals(temp, "Index1");
- 
-       }
-       catch (Exception e) {
-         e.printStackTrace();
-         fail(q.getQueryString());
-       }
-     }
-     StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
-     ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r, queries.length,queries);
- 
-     // Test queries index not used
-     for (int i = 0; i < queriesIndexNotUsed.length; i++) {
-       Query q = null;
-       try {
-         q = CacheUtils.getQueryService().newQuery(queriesIndexNotUsed[i]);
-         CacheUtils.getLogger().info(
-             "Executing query: " + queriesIndexNotUsed[i]);
-         QueryObserverImpl observer = new QueryObserverImpl();
-         QueryObserverHolder.setInstance(observer);
-         CacheUtils.log("Executing query: " + queriesIndexNotUsed[i]
-             + " with index created");
-         q.execute();
-         assertFalse(observer.isIndexesUsed);
-         Iterator itr = observer.indexesUsed.iterator();
-         assertFalse(itr.hasNext());
- 
-       }
-       catch (Exception e) {
-         e.printStackTrace();
-         fail(q.getQueryString());
-       }
-     }
- 
-   }
-       
-   @Test
-   public void testMapIndexUsageWithIndexOnMultipleKeys() throws Exception
-   {
-     IndexManager.TEST_RANGEINDEX_ONLY = true;
-     QueryService qs;
-     qs = CacheUtils.getQueryService();
 +    LocalRegion testRgn = (LocalRegion)CacheUtils.createRegion("testRgn", null);
 +    int ID = 1;
 +    // Add some test data now
 +    // Add 5 main objects. 1 will contain key1, 2 will contain key1 & key2
 +    // and so on
 +    for (; ID <= 30; ++ID) {
 +      MapKeyIndexData mkid = new MapKeyIndexData(ID);
 +      for (int j = 1; j <= ID; ++j) {
 +        mkid.addKeyValue("key1", j * 1);
 +        mkid.addKeyValue("key2", j * 2);
 +        mkid.addKeyValue("key3", j * 3);
 +      }
 +      testRgn.put(ID, mkid);
 +    }
++    evaluateMapTypeIndexUsage("objs.maap['key2','key3']", "/testRgn objs", queries, queriesIndexNotUsed, CompactMapRangeIndex.class);
++  }
++      
++  @Test
++  public void testMapIndexUsageWithIndexOnMultipleKeys() throws Exception
++  {
++    try {
++      IndexManager.TEST_RANGEINDEX_ONLY = true;
++      String queries[] = {
++          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key2'] >= 3",
++          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key3'] >= 3" };
++      String queriesIndexNotUsed[] = {
++          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap.get('key2') >= 16",
++          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key4'] >= 16", };
 +
-     qs = CacheUtils.getQueryService();
-     String queries[] = {
-         "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key2'] >= 3",
-         "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key3'] >= 3" };
- 
-     String queriesIndexNotUsed[] = {
-         "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap.get('key2') >= 16",
-         "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key4'] >= 16", };
- 
++      
++      LocalRegion testRgn = (LocalRegion)CacheUtils.createRegion("testRgn", null);
++      int ID = 1;
++      // Add some test data now
++      // Add 5 main objects. 1 will contain key1, 2 will contain key1 & key2
++      // and so on
++      for (; ID <= 30; ++ID) {
++        MapKeyIndexData mkid = new MapKeyIndexData(ID);
++        for (int j = 1; j <= ID; ++j) {
++          mkid.addKeyValue("key1", j * 1);
++          mkid.addKeyValue("key2", j * 2);
++          mkid.addKeyValue("key3", j * 3);
++        }
++        testRgn.put(ID, mkid);
++      }
++      
++      evaluateMapTypeIndexUsage("objs.maap['key2','key3']", "/testRgn objs", queries, queriesIndexNotUsed, MapRangeIndex.class);
++    }
++    finally {
++      IndexManager.TEST_RANGEINDEX_ONLY = false;
++    }
++  }
++  
++  private void evaluateMapTypeIndexUsage(String indexExpression, String fromClause, String[] queries, String[] queriesIndexNotUsed, Class expectedIndexClass) throws Exception {
++    QueryService qs = CacheUtils.getQueryService();
++   
 +    Object r[][] = new Object[queries.length][2];
 +
 +    // Execute Queries without Indexes
 +    for (int i = 0; i < queries.length; i++) {
 +      Query q = null;
 +      try {
 +        q = CacheUtils.getQueryService().newQuery(queries[i]);
 +        CacheUtils.getLogger().info("Executing query: " + queries[i]);
 +        r[i][0] = q.execute();
 +        CacheUtils.log("Executed query: " + queries[i]);
 +      }
 +      catch (Exception e) {
 +        e.printStackTrace();
 +        fail(q.getQueryString());
 +      }
 +    }
 +
 +    Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL,
-         "objs.maap['key2','key3']", "/testRgn objs");
-     assertTrue(i1 instanceof MapRangeIndex);
++        indexExpression, fromClause);
++    assertTrue(i1.getClass().equals(expectedIndexClass));
++    
 +    // Execute Queries with Indexes
 +    for (int i = 0; i < queries.length; i++) {
 +      Query q = null;
 +      try {
 +        q = CacheUtils.getQueryService().newQuery(queries[i]);
 +        CacheUtils.getLogger().info("Executing query: " + queries[i]);
 +        QueryObserverImpl observer = new QueryObserverImpl();
 +        QueryObserverHolder.setInstance(observer);
 +        r[i][1] = q.execute();
 +        CacheUtils.log("Executing query: " + queries[i]
 +            + " with index created");
 +        if (!observer.isIndexesUsed) {
-           fail("Index is NOT uesd");
++          fail("Index is NOT used");
 +        }
 +        Iterator itr = observer.indexesUsed.iterator();
 +        assertTrue(itr.hasNext());
 +        String temp = itr.next().toString();
 +        assertEquals(temp, "Index1");
 +
 +      }
 +      catch (Exception e) {
 +        e.printStackTrace();
 +        fail(q.getQueryString());
 +      }
 +    }
 +    StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
 +    ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r, queries.length,queries);
 +
 +    // Test queries index not used
 +    for (int i = 0; i < queriesIndexNotUsed.length; i++) {
 +      Query q = null;
 +      try {
 +        q = CacheUtils.getQueryService().newQuery(queriesIndexNotUsed[i]);
 +        CacheUtils.getLogger().info(
 +            "Executing query: " + queriesIndexNotUsed[i]);
 +        QueryObserverImpl observer = new QueryObserverImpl();
 +        QueryObserverHolder.setInstance(observer);
 +        CacheUtils.log("Executing query: " + queriesIndexNotUsed[i]
 +            + " with index created");
 +        q.execute();
 +        assertFalse(observer.isIndexesUsed);
 +        Iterator itr = observer.indexesUsed.iterator();
 +        assertFalse(itr.hasNext());
 +
 +      }
 +      catch (Exception e) {
 +        e.printStackTrace();
 +        fail(q.getQueryString());
 +      }
 +    }
- 
 +  }
 +
 +  @Test
 +  public void testIndexUsageWithOrderBy() throws Exception
 +  {
 +    QueryService qs;
 +    qs = CacheUtils.getQueryService();
 +    LocalRegion testRgn = (LocalRegion)CacheUtils.createRegion("testRgn", null);
 +    
 +    int numObjects = 30;
 +    // Add some test data now
 +    // Add 5 main objects. 1 will contain key1, 2 will contain key1 & key2
 +    // and so on
 +    for (int i=0; i < numObjects; i++) {
 +      Portfolio p = new Portfolio(i);
 +      p.pkid = ("" +(numObjects - i));
 +      testRgn.put("" + i, p);
 +    }
 +
 +    qs = CacheUtils.getQueryService();
 +    String queries[] = {
 +        "SELECT DISTINCT * FROM /testRgn p  WHERE p.ID <= 10 order by p.pkid asc limit 1",
 +        "SELECT DISTINCT * FROM /testRgn p  WHERE p.ID <= 10 order by p.pkid desc limit 1",
 +    };
 +
 +    Object r[][] = new Object[queries.length][2];
 +
 +    // Execute Queries without Indexes
 +    for (int i = 0; i < queries.length; i++) {
 +      Query q = null;
 +      try {
 +        q = CacheUtils.getQueryService().newQuery(queries[i]);
 +        CacheUtils.getLogger().info("Executing query: " + queries[i]);
 +        // verify individual result
 +        SelectResults sr = (SelectResults)q.execute();
 +        List results = sr.asList();
 +        for (int rows=0; rows < results.size(); rows++) {
 +          Portfolio p = (Portfolio)results.get(0);
 +          CacheUtils.getLogger().info("p: " + p);
 +          if (i == 0) {
 +            assertEquals(p.getID(), 10);
 +            assertEquals(p.pkid, "" + (numObjects - 10));
 +          } else if (i == 1) {
 +            assertEquals(p.getID(), 0);
 +            assertEquals(p.pkid, "" + numObjects);
 +          }
 +        }
 +        r[i][0] = sr;
 +        CacheUtils.log("Executed query: " + queries[i]);
 +      }
 +      catch (Exception e) {
 +        e.printStackTrace();
 +        fail(q.getQueryString());
 +      }
 +    }
 +
 +    Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL, "p.ID", "/testRgn p");
 + 
 +    // Execute Queries with Indexes
 +    for (int i = 0; i < queries.length; i++) {
 +      Query q = null;
 +      try {
 +        q = CacheUtils.getQueryService().newQuery(queries[i]);
 +        CacheUtils.getLogger().info("Executing query: " + queries[i]);
 +        QueryObserverImpl observer = new QueryObserverImpl();
 +        QueryObserverHolder.setInstance(observer);
 +        SelectResults sr = (SelectResults)q.execute();
 +        List results = sr.asList();
 +        for (int rows=0; rows < results.size(); rows++) {
 +          Portfolio p = (Portfolio)results.get(0);
 +          CacheUtils.getLogger().info("index p: " + p);
 +          if (i == 0) {
 +            assertEquals(p.getID(), 10);
 +            assertEquals(p.pkid, "" + (numObjects - 10));
 +          } else if (i == 1) {
 +            assertEquals(p.getID(), 0);
 +            assertEquals(p.pkid, "" + numObjects);
 +          }
 +        }
 +        r[i][1] = sr;
 +        //r[i][1] = q.execute();
 +        CacheUtils.log("Executing query: " + queries[i]
 +            + " with index created");
 +        if (!observer.isIndexesUsed) {
 +          fail("Index is NOT uesd");
 +        }
 +        Iterator itr = observer.indexesUsed.iterator();
 +        assertTrue(itr.hasNext());
 +        String temp = itr.next().toString();
 +        assertEquals(temp, "Index1");
 +      }
 +      
 +      catch (Exception e) {
 +        e.printStackTrace();
 +        fail(q.getQueryString());
 +      }
 +    }
 +    StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
 +    ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r, queries.length,queries);
 +  }
 +  
 +  @Test
 +  public void testIndexUsageWithOrderBy3() throws Exception
 +  {
 +    QueryService qs;
 +    qs = CacheUtils.getQueryService();
 +    LocalRegion testRgn = (LocalRegion)CacheUtils.createRegion("testRgn", null);
 +    
 +    int numObjects = 30;
 +    // Add some test data now
 +    // Add 5 main objects. 1 will contain key1, 2 will contain key1 & key2
 +    // and so on
 +    for (int i=0; i < numObjects; i++) {
 +      Portfolio p = new Portfolio(i);
 +      p.pkid = ("" +(numObjects - i));
 +      testRgn.put("" + i, p);
 +    }
 +
 +    qs = CacheUtils.getQueryService();
 +    String queries[] = {
 +        "SELECT DISTINCT * FROM /testRgn p  WHERE p.ID <= 10 order by ID asc limit 1",
 +        "SELECT DISTINCT * FROM /testRgn p  WHERE p.ID <= 10 order by p.ID desc limit 1",
 +    };
 +
 +    Object r[][] = new Object[queries.length][2];
 +
 +    
 +    Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL, "p.ID", "/testRgn p");
 + 
 +    // Execute Queries with Indexes
 +    for (int i = 0; i < queries.length; i++) {
 +      Query q = null;
 +      try {
 +        q = CacheUtils.getQueryService().newQuery(queries[i]);
 +        CacheUtils.getLogger().info("Executing query: " + queries[i]);
 +        QueryObserverImpl observer = new QueryObserverImpl();
 +        QueryObserverHolder.setInstance(observer);
 +        SelectResults sr = (SelectResults)q.execute();
 +        List results = sr.asList();
 +        for (int rows=0; rows < results.size(); rows++) {
 +          Portfolio p = (Portfolio)results.get(0);
 +          CacheUtils.getLogger().info("index p: " + p);
 +          if (i == 0) {
 +            assertEquals(p.getID(), 0);
 +            
 +          } else if (i == 1) {
 +            assertEquals(p.getID(), 10);
 +            
 +          }
 +        }
 +        r[i][1] = sr;
 +        //r[i][1] = q.execute();
 +        CacheUtils.log("Executing query: " + queries[i]
 +            + " with index created");
 +        if (!observer.isIndexesUsed) {
 +          fail("Index is NOT uesd");
 +        }
 +        Iterator itr = observer.indexesUsed.iterator();
 +        assertTrue(itr.hasNext());
 +        String temp = itr.next().toString();
 +        assertEquals(temp, "Index1");
 +      }
 +      
 +      catch (Exception e) {
 +        e.printStackTrace();
 +        fail(q.getQueryString());
 +      }
 +    }
 +   
 +  }
 +
 +  @Test
 +  public void testIndexUsageWithOrderBy2() throws Exception
 +  {
 +    QueryService qs;
 +    qs = CacheUtils.getQueryService();
 +    LocalRegion testRgn = (LocalRegion)CacheUtils.createRegion("testRgn", null);
 +    
 +    int numObjects = 30;
 +    // Add some test data now
 +    // Add 5 main objects. 1 will contain key1, 2 will contain key1 & key2
 +    // and so on
 +    for (int i=0; i < numObjects; i++) {
 +      Portfolio p = new Portfolio(i % 2);
 +      p.createTime = (numObjects - i);
 +      testRgn.put("" + i, p);
 +    }
 +
 +    qs = CacheUtils.getQueryService();
 +    String queries[] = {
 +        "SELECT DISTINCT p.key, p.value FROM /testRgn.entrySet p  WHERE p.value.ID <= 10 order by p.value.createTime asc limit 1",
 +        "SELECT DISTINCT p.key, p.value FROM /testRgn.entrySet p  WHERE p.value.ID <= 10 order by p.value.createTime desc limit 1",
 +    };
 +
 +    Object r[][] = new Object[queries.length][2];
 +
 +    // Execute Queries without Indexes
 +    for (int i = 0; i < queries.length; i++) {
 +      Query q = null;
 +      try {
 +        q = CacheUtils.getQueryService().newQuery(queries[i]);
 +        CacheUtils.getLogger().info("Executing query: " + queries[i]);
 +        // verify individual result
 +        SelectResults sr = (SelectResults)q.execute();
 +        List results = sr.asList();
 +        for (int rows=0; rows < results.size(); rows++) {
 +          Struct s = (Struct)results.get(0);
 +          Portfolio p = (Portfolio)s.get("value");
 +          CacheUtils.getLogger().info("p: " + p);
 +          if (i == 0) {
 +            assertEquals(p.createTime, 1);
 +          } else if (i == 1) {
 +            assertEquals(p.createTime, numObjects);
 +          }
 +        }
 +        r[i][0] = sr;
 +        CacheUtils.log("Executed query: " + queries[i]);
 +      }
 +      catch (Exception e) {
 +        e.printStackTrace();
 +        fail(q.getQueryString());
 +      }
 +    }
 +
 +    Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL, "p.value.ID", "/testRgn.entrySet p");
 + 
 +    // Execute Queries with Indexes
 +    for (int i = 0; i < queries.length; i++) {
 +      Query q = null;
 +      try {
 +        q = CacheUtils.getQueryService().newQuery(queries[i]);
 +        CacheUtils.getLogger().info("Executing query: " + queries[i]);
 +        QueryObserverImpl observer = new QueryObserverImpl();
 +        QueryObserverHolder.setInstance(observer);
 +        SelectResults sr = (SelectResults)q.execute();
 +        List results = sr.asList();
 +        for (int rows=0; rows < results.size(); rows++) {
 +          Struct s = (Struct)results.get(0);
 +          Portfolio p = (Portfolio)s.get("value");
 +          CacheUtils.getLogger().info("index p: " + p);
 +          if (i == 0) {
 +            assertEquals(p.createTime, 1);
 +          } else if (i == 1) {
 +            assertEquals(p.createTime, numObjects);
 +          }
 +        }
 +        r[i][1] = sr;
 +        //r[i][1] = q.execute();
 +        CacheUtils.log("Executing query: " + queries[i]
 +            + " with index created");
 +        if (!observer.isIndexesUsed) {
 +          fail("Index is NOT uesd");
 +        }
 +        Iterator itr = observer.indexesUsed.iterator();
 +        assertTrue(itr.hasNext());
 +        String temp = itr.next().toString();
 +        assertEquals(temp, "Index1");
 +      }
 +      
 +      catch (Exception e) {
 +        e.printStackTrace();
 +        fail(q.getQueryString());
 +      }
 +    }
 +    StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
 +    ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r, queries.length,queries);
 +  }
 +
 +  @Test
 +  public void testIncorrectIndexOperatorSyntax() {
 +    QueryService qs;
 +    qs = CacheUtils.getQueryService();
 +    LocalRegion testRgn = (LocalRegion)CacheUtils.createRegion("testRgn", null);
 +    int ID =1;
 +    //Add some test data now
 +    //Add 5 main objects. 1 will contain key1, 2 will contain key1 & key2
 +    // and so on
 +    for(; ID <=30; ++ID) {
 +      MapKeyIndexData mkid = new MapKeyIndexData(ID);
 +      for(int j =1; j<= ID;++j) {
 +        mkid.maap.put("key1", j*1);
 +        mkid.maap.put("key2", j*2);
 +        mkid.maap.put("key3", j*3);
 +      }
 +      testRgn.put(ID, mkid);
 +    }
 +    
 +    
 +    qs = CacheUtils.getQueryService();
 +    try { 
 +    Query q = CacheUtils.getQueryService().newQuery(
 +        "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap[*] >= 3");
 +    fail("Should have thrown exception");
 +    }catch(QueryInvalidException qe) {
 +      //ok      
 +    }           
 +    
 +    try { 
 +    Query q = CacheUtils.getQueryService().newQuery(
 +        "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key1','key2'] >= 3");
 +    fail("Should have thrown exception");
 +    }catch(QueryInvalidException qe) {
 +      //ok      
 +    }   
 +      
 +  }
 +  
 +  @Test
 +  public void testRangeGroupingBehaviourOfCompactMapIndex() throws Exception {
 +    QueryService qs;
 +    qs = CacheUtils.getQueryService();
 +    LocalRegion testRgn = (LocalRegion)CacheUtils.createRegion("testRgn", null);
 +    int ID = 1;
 +    // Add some test data now
 +    // Add 5 main objects. 1 will contain key1, 2 will contain key1 & key2
 +    // and so on
 +    for (; ID <= 30; ++ID) {
 +      MapKeyIndexData mkid = new MapKeyIndexData(ID);
 +      for (int j = 1; j <= ID; ++j) {
 +        mkid.addKeyValue("key1", j * 1);
 +        mkid.addKeyValue("key2", j * 2);
 +        mkid.addKeyValue("key3", j * 3);
 +      }
 +      testRgn.put(ID, mkid);
 +    }
 +
 +    qs = CacheUtils.getQueryService();
 +    String queries[] = {
 +        "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key2'] >= 3 and itr1.maap['key2'] <=18",
 +        "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key3'] >= 3  and  itr1.maap['key3'] >= 13 ",
 +        "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key2'] >= 3  and  itr1.maap['key3'] >= 13 ",
 +        "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key2'] >= 3  and  itr1.maap['key3'] < 18 "
 +        
 +    };
 +
 +
 +    Object r[][] = new Object[queries.length][2];
 +
 +    // Execute Queries without Indexes
 +    for (int i = 0; i < queries.length; i++) {
 +      Query q = null;
 +      try {
 +        q = CacheUtils.getQueryService().newQuery(queries[i]);
 +        CacheUtils.getLogger().info("Executing query: " + queries[i]);
 +        r[i][0] = q.execute();
 +        CacheUtils.log("Executed query: " + queries[i]);
 +      }
 +      catch (Exception e) {
 +        e.printStackTrace();
 +        fail(q.getQueryString());
 +      }
 +    }
 +
 +    Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL,
 +        "objs.maap['key2','key3']", "/testRgn objs");
 +    assertTrue(i1 instanceof CompactMapRangeIndex);
 +    // Execute Queries with Indexes
 +    for (int i = 0; i < queries.length; i++) {
 +      Query q = null;
 +      try {
 +        q = CacheUtils.getQueryService().newQuery(queries[i]);
 +        CacheUtils.getLogger().info("Executing query: " + queries[i]);
 +        QueryObserverImpl observer = new QueryObserverImpl();
 +        QueryObserverHolder.setInstance(observer);
 +        r[i][1] = q.execute();
 +        CacheUtils.log("Executing query: " + queries[i]
 +            + " with index created");
 +        if (!observer.isIndexesUsed) {
 +          fail("Index is NOT uesd");
 +        }
 +        Iterator itr = observer.indexesUsed.iterator();
 +        assertTrue(itr.hasNext());
 +        String temp = itr.next().toString();
 +        assertEquals(temp, "Index1");
 +
 +      }
 +      catch (Exception e) {
 +        e.printStackTrace();
 +        fail(q.getQueryString());
 +      }
 +    }
 +    StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
 +    ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r, queries.length,queries);
 +    
 +  }
 +  
 +  @Test
 +  public void testRangeGroupingBehaviourOfMapIndex() throws Exception {
 +    IndexManager.TEST_RANGEINDEX_ONLY = true;
 +    QueryService qs;
 +    qs = CacheUtils.getQueryService();
 +    LocalRegion testRgn = (LocalRegion)CacheUtils.createRegion("testRgn", null);
 +    int ID = 1;
 +    // Add some test data now
 +    // Add 5 main objects. 1 will contain key1, 2 will contain key1 & key2
 +    // and so on
 +    for (; ID <= 30; ++ID) {
 +      MapKeyIndexData mkid = new MapKeyIndexData(ID);
 +      for (int j = 1; j <= ID; ++j) {
 +        mkid.addKeyValue("key1", j * 1);
 +        mkid.addKeyValue("key2", j * 2);
 +        mkid.addKeyValue("key3", j * 3);
 +      }
 +      testRgn.put(ID, mkid);
 +    }
 +
 +    qs = CacheUtils.getQueryService();
 +    String queries[] = {
 +        "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key2'] >= 3 and itr1.maap['key2'] <=18",
 +        "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key3'] >= 3  and  itr1.maap['key3'] >= 13 ",
 +        "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key2'] >= 3  and  itr1.maap['key3'] >= 13 ",
 +        "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key2'] >= 3  and  itr1.maap['key3'] < 18 "
 +        
 +    };
 +
 +
 +    Object r[][] = new Object[queries.length][2];
 +
 +    // Execute Queries without Indexes
 +    for (int i = 0; i < queries.length; i++) {
 +      Query q = null;
 +      try {
 +        q = CacheUtils.getQueryService().newQuery(queries[i]);
 +        CacheUtils.getLogger().info("Executing query: " + queries[i]);
 +        r[i][0] = q.execute();
 +        CacheUtils.log("Executed query: " + queries[i]);
 +      }
 +      catch (Exception e) {
 +        e.printStackTrace();
 +        fail(q.getQueryString());
 +      }
 +    }
 +
 +    Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL,
 +        "objs.maap['key2','key3']", "/testRgn objs");
 +    assertTrue(i1 instanceof MapRangeIndex);
 +    // Execute Queries with Indexes
 +    for (int i = 0; i < queries.length; i++) {
 +      Query q = null;
 +      try {
 +        q = CacheUtils.getQueryService().newQuery(queries[i]);
 +        CacheUtils.getLogger().info("Executing query: " + queries[i]);
 +        QueryObserverImpl observer = new QueryObserverImpl();
 +        QueryObserverHolder.setInstance(observer);
 +        r[i][1] = q.execute();
 +        CacheUtils.log("Executing query: " + queries[i]
 +            + " with index created");
 +        if (!observer.isIndexesUsed) {
 +          fail("Index is NOT uesd");
 +        }
 +        Iterator itr = observer.indexesUsed.iterator();
 +        assertTrue(itr.hasNext());
 +        String temp = itr.next().toString();
 +        assertEquals(temp, "Index1");
 +
 +      }
 +      catch (Exception e) {
 +        e.printStackTrace();
 +        fail(q.getQueryString());
 +      }
 +    }
 +    StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
 +    ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r, queries.length,queries);
 +    
 +  }
 +  
 +  @Test
 +  public void testMapIndexUsableQueryOnEmptyRegion() throws Exception
 +  {
- 
++    IndexManager.TEST_RANGEINDEX_ONLY = true;
 +    QueryService qs;
 +    qs = CacheUtils.getQueryService();
 +    LocalRegion testRgn = (LocalRegion)CacheUtils.createRegion("testRgn", null);
 +    Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL,
 +        "objs.maap['key2','key3']", "/testRgn objs");
 +    qs = CacheUtils.getQueryService();
 +    // Execute Queries without Indexes
 +    Query q = CacheUtils.getQueryService().newQuery(
 +        "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key2'] >= 3 ");
 +    SelectResults sr = (SelectResults)q.execute();
 +    assertTrue(sr.isEmpty());
 +
 +  }
 +
 +  @Test
 +  public void testSizeEstimateLTInRangeIndexForNullMap() throws Exception {
 +    QueryService qs = CacheUtils.getQueryService();
 +    LocalRegion testRgn = (LocalRegion)CacheUtils.createRegion("testRgn", null);
 +    //Create indexes
 +    Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL,
 +        "p.status", "/testRgn p, p.positions");
 +    Index i2 = qs.createIndex("Index2", IndexType.FUNCTIONAL,
 +        "p.ID", "/testRgn p, p.positions");
 +    
 +    //put values
 +    testRgn.put(0, new Portfolio(0));
 +    testRgn.put(1, new Portfolio(1));
 +
 +    //Set TestHook in RangeIndex
 +    TestHook hook = new RangeIndexTestHook();
 +    RangeIndex.setTestHook(hook);
 +    // Execute Queries without Indexes
 +    Query q = CacheUtils.getQueryService().newQuery(
 +        "<trace> SELECT * FROM /testRgn p, p.positions where p.status = 'active' AND p.ID > 0 ");
 +    
 +    //Following should throw NullPointerException.
 +    SelectResults sr = (SelectResults)q.execute();
 +    
 +    assertTrue("RangeIndexTestHook was not hooked for spot 2", ((RangeIndexTestHook)hook).isHooked(2));
 +    RangeIndex.setTestHook(null);
 +  }
 +
 +  @Test
 +  public void testSizeEstimateGTInRangeIndexForNullMap() throws Exception {
 +    QueryService qs = CacheUtils.getQueryService();
 +    LocalRegion testRgn = (LocalRegion)CacheUtils.createRegion("testRgn", null);
 +    //Create indexes
 +    Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL,
 +        "p.status", "/testRgn p, p.positions");
 +    Index i2 = qs.createIndex("Index2", IndexType.FUNCTIONAL,
 +        "p.ID", "/testRgn p, p.positions");
 +    
 +    //put values
 +    testRgn.put(0, new Portfolio(0));
 +    testRgn.put(1, new Portfolio(1));
 +
 +    //Set TestHook in RangeIndex
 +    TestHook hook = new RangeIndexTestHook();
 +    RangeIndex.setTestHook(hook);
 +    // Execute Queries without Indexes
 +    Query q = CacheUtils.getQueryService().newQuery(
 +        "<trace> SELECT * FROM /testRgn p, p.positions where p.status = 'active' AND p.ID < 0 ");
 +    
 +    //Following should throw NullPointerException.
 +    SelectResults sr = (SelectResults)q.execute();
 +    
 +    assertTrue("RangeIndexTestHook was not hooked for spot 1", ((RangeIndexTestHook)hook).isHooked(1));
 +    RangeIndex.setTestHook(null);
 +  }
 +
 +  @Test
 +  public void testSizeEstimateLTInCompactRangeIndexForNullMap() throws Exception {
 +    QueryService qs = CacheUtils.getQueryService();
 +    LocalRegion testRgn = (LocalRegion)CacheUtils.createRegion("testRgn", null);
 +    //Create indexes
 +    Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL,
 +        "p.status", "/testRgn p");
 +    Index i2 = qs.createIndex("Index2", IndexType.FUNCTIONAL,
 +        "p.ID", "/testRgn p");
 +    
 +    //put values
 +    testRgn.put(0, new Portfolio(0));
 +    testRgn.put(1, new Portfolio(1));
 +
 +    //Set TestHook in RangeIndex
 +    TestHook hook = new RangeIndexTestHook();
 +    CompactRangeIndex.setTestHook(hook);
 +    // Execute Queries without Indexes
 +    Query q = CacheUtils.getQueryService().newQuery(
 +        "<trace> SELECT * FROM /testRgn p where p.status = 'active' AND p.ID > 0 ");
 +    
 +    //Following should throw NullPointerException.
 +    SelectResults sr = (SelectResults)q.execute();
 +    
 +    assertTrue("RangeIndexTestHook was not hooked for spot 2", ((RangeIndexTestHook)hook).isHooked(2));
 +    CompactRangeIndex.setTestHook(null);
 +  }
 +
 +  @Test
 +  public void testSizeEstimateGTInCompactRangeIndexForNullMap() throws Exception {
 +    QueryService qs = CacheUtils.getQueryService();
 +    LocalRegion testRgn = (LocalRegion)CacheUtils.createRegion("testRgn", null);
 +    //Create indexes
 +    Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL,
 +        "p.status", "/testRgn p");
 +    Index i2 = qs.createIndex("Index2", IndexType.FUNCTIONAL,
 +        "p.ID", "/testRgn p");
 +    
 +    //put values
 +    testRgn.put(0, new Portfolio(0));
 +    testRgn.put(1, new Portfolio(1));
 +
 +    //Set TestHook in RangeIndex
 +    TestHook hook = new RangeIndexTestHook();
 +    CompactRangeIndex.setTestHook(hook);
 +    // Execute Queries without Indexes
 +    Query q = CacheUtils.getQueryService().newQuery(
 +        "<trace> SELECT * FROM /testRgn p where p.status = 'active' AND p.ID < 0 ");
 +    
 +    //Following should throw NullPointerException.
 +    SelectResults sr = (SelectResults)q.execute();
 +    
 +    assertTrue("RangeIndexTestHook was not hooked for spot 1", ((RangeIndexTestHook)hook).isHooked(1));
 +    CompactRangeIndex.setTestHook(null);
 +  }
 +  
 +  @Test
 +  public void testCompactMapIndexUsageAllKeysWithVariousValueTypes() throws Exception {
 +      QueryService qs;
 +      qs = CacheUtils.getQueryService();
 +      LocalRegion testRgn = (LocalRegion)CacheUtils.createRegion("testRgn", null);
 +      int ID =1;
 +      for(; ID <=30; ++ID) {
 +        TestObject object = new TestObject(ID);
 +        testRgn.put(ID, object);
 +      }
 +      String queries[] = {
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields['string'] = '1'",
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields['double'] > 1D",
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields['integer'] > 1",
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields['long'] > 1L"
 +      };
 +      String queriesIndexNotUsed[] = {        
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields.get('string') = '1'",       
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields.get('double') > 1D",         
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields.get('integer') > 1",         
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields.get('long') > 1L"         
 +       };
-       Object r[][]= new Object[queries.length][2];
-       
-       qs = CacheUtils.getQueryService();
-       
-     //Execute Queries without Indexes
-       for (int i = 0; i < queries.length; i++) {
-           Query q = null;
-           try {
-               q = CacheUtils.getQueryService().newQuery(queries[i]);
-               CacheUtils.getLogger().info("Executing query: " + queries[i]);              
-               r[i][0] = q.execute();         
-             CacheUtils.log("Executed query: " + queries[i] );
-           } catch (Exception e) {
-               e.printStackTrace();
-               fail(q.getQueryString());
-           }
-       }  
-       
-       Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL, "itr1.testFields[*]",
-           "/testRgn itr1");
-       
-       assertTrue(i1 instanceof CompactMapRangeIndex);
-       
-             //Execute Queries with Indexes
-         for (int i = 0; i < queries.length; i++) {
-             Query q = null;
-             try {
-                 q = CacheUtils.getQueryService().newQuery(queries[i]);
-                 CacheUtils.getLogger().info("Executing query: " + queries[i]);
-                 QueryObserverImpl observer = new QueryObserverImpl();
-                 QueryObserverHolder.setInstance(observer);
-                 r[i][1] = q.execute();
-                 CacheUtils.log("Executing query: " + queries[i] + " with index created");
-                 if(!observer.isIndexesUsed){
-                     fail("Index is NOT uesd");
-                 }              
-                 Iterator itr = observer.indexesUsed.iterator();
-                 assertTrue(itr.hasNext());
-                 String temp = itr.next().toString();
-                 assertEquals(temp,"Index1");       
-                 
-             } catch (Exception e) {
-                 e.printStackTrace();
-                 fail(q.getQueryString());
-             }
-         }
-         StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
-         ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r,queries.length,queries);
-         
-         //Test queries index not used
-         for (int i = 0; i < queriesIndexNotUsed.length; i++) {
-           Query q = null;
-           try {
-               q = CacheUtils.getQueryService().newQuery(queriesIndexNotUsed[i]);
-               CacheUtils.getLogger().info("Executing query: " + queriesIndexNotUsed[i]);
-               QueryObserverImpl observer = new QueryObserverImpl();
-               QueryObserverHolder.setInstance(observer);            
-               CacheUtils.log("Executing query: " + queriesIndexNotUsed[i] + " with index created");
-               q.execute();
-               assertFalse(observer.isIndexesUsed);                          
-               Iterator itr = observer.indexesUsed.iterator();
-               assertFalse(itr.hasNext());                   
-               
-           } catch (Exception e) {
-               e.printStackTrace();
-               fail(q.getQueryString());
-           }
-       }
++      this.evaluateMapTypeIndexUsage("itr1.testFields[*]", "/testRgn itr1", queries, queriesIndexNotUsed, CompactMapRangeIndex.class);
 +    }
 +    
 +    @Test
 +  public void testCompactMapIndexUsageAllKeysOneIndex() throws Exception {
 +      QueryService qs;
 +      qs = CacheUtils.getQueryService();
 +      LocalRegion testRgn = (LocalRegion)CacheUtils.createRegion("testRgn", null);
 +      int ID =1;
 +      for(; ID <=30; ++ID) {
 +        TestObject object = new TestObject(ID);
 +        testRgn.put(ID, object);
 +      }
 +      String queries[] = {
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields['string'] = '1'",
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields['double'] > 1D",
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields['integer'] > 1",
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields['long'] > 1L"
 +      };
 +      String queriesIndexNotUsed[] = {        
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields.get('string') = '1'",       
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields.get('double') > 1D",         
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields.get('integer') > 1",         
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields.get('long') > 1L"         
 +       };
-       Object r[][]= new Object[queries.length][2];
-       
-       qs = CacheUtils.getQueryService();
-       
-     //Execute Queries without Indexes
-       for (int i = 0; i < queries.length; i++) {
-           Query q = null;
-           try {
-               q = CacheUtils.getQueryService().newQuery(queries[i]);
-               CacheUtils.getLogger().info("Executing query: " + queries[i]);              
-               r[i][0] = q.execute();         
-             CacheUtils.log("Executed query: " + queries[i] );
-           } catch (Exception e) {
-               e.printStackTrace();
-               fail(q.getQueryString());
-           }
-       }  
-       
-       Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL, "itr1.testFields['string','double','integer','long']",
-           "/testRgn itr1");
-       
-       assertTrue(i1 instanceof CompactMapRangeIndex);
-       
-             //Execute Queries with Indexes
-         for (int i = 0; i < queries.length; i++) {
-             Query q = null;
-             try {
-                 q = CacheUtils.getQueryService().newQuery(queries[i]);
-                 CacheUtils.getLogger().info("Executing query: " + queries[i]);
-                 QueryObserverImpl observer = new QueryObserverImpl();
-                 QueryObserverHolder.setInstance(observer);
-                 r[i][1] = q.execute();
-                 CacheUtils.log("Executing query: " + queries[i] + " with index created");
-                 if(!observer.isIndexesUsed){
-                     fail("Index is NOT uesd");
-                 }              
-                 Iterator itr = observer.indexesUsed.iterator();
-                 assertTrue(itr.hasNext());
-                 String temp = itr.next().toString();
-                 assertEquals(temp,"Index1");       
-                 
-             } catch (Exception e) {
-                 e.printStackTrace();
-                 fail(q.getQueryString());
-             }
-         }
-         StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
-         ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r,queries.length,queries);
-         
-         //Test queries index not used
-         for (int i = 0; i < queriesIndexNotUsed.length; i++) {
-           Query q = null;
-           try {
-               q = CacheUtils.getQueryService().newQuery(queriesIndexNotUsed[i]);
-               CacheUtils.getLogger().info("Executing query: " + queriesIndexNotUsed[i]);
-               QueryObserverImpl observer = new QueryObserverImpl();
-               QueryObserverHolder.setInstance(observer);            
-               CacheUtils.log("Executing query: " + queriesIndexNotUsed[i] + " with index created");
-               q.execute();
-               assertFalse(observer.isIndexesUsed);                          
-               Iterator itr = observer.indexesUsed.iterator();
-               assertFalse(itr.hasNext());                   
-               
-           } catch (Exception e) {
-               e.printStackTrace();
-               fail(q.getQueryString());
-           }
-       }
++     evaluateMapTypeIndexUsage("itr1.testFields['string','double','integer','long']", "/testRgn itr1", queries, queriesIndexNotUsed, CompactMapRangeIndex.class);
 +    }
 +    
 +    @Test
 +  public void testCompactMapIndexUsageManyGetKeysWithVariousValueTypes() throws Exception {
 +      QueryService qs;
 +      qs = CacheUtils.getQueryService();
 +      LocalRegion testRgn = (LocalRegion)CacheUtils.createRegion("testRgn", null);
 +      int ID =1;
 +      for(; ID <=30; ++ID) {
 +        TestObject object = new TestObject(ID);
 +        testRgn.put(ID, object);
 +      }
 +      String queries[] = {
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields.get('string') = '1'",       
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields.get('double') > 1D",         
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields.get('integer') > 1",         
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields.get('long') > 1L" 
 +      };
 +      String queriesIndexNotUsed[] = {        
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields['string'] = '1'",
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields['double'] > 1D",
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields['integer'] > 1",
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields['long'] > 1L"
 +       };
 +      Object r[][]= new Object[queries.length][2];
 +      
 +      qs = CacheUtils.getQueryService();
 +      
 +      //Execute Queries without Indexes
 +      for (int i = 0; i < queries.length; i++) {
 +          Query q = null;
 +          try {
 +              q = CacheUtils.getQueryService().newQuery(queries[i]);
 +              CacheUtils.getLogger().info("Executing query: " + queries[i]);              
 +              r[i][0] = q.execute();         
 +            CacheUtils.log("Executed query: " + queries[i] );
 +          } catch (Exception e) {
 +              e.printStackTrace();
 +              fail(q.getQueryString());
 +          }
 +      }  
 +      
 +      Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL, "itr1.testFields.get('string')",
 +          "/testRgn itr1");
 +      Index i2 = qs.createIndex("Index2", IndexType.FUNCTIONAL, "itr1.testFields.get('double')",
 +          "/testRgn itr1");
 +      Index i3 = qs.createIndex("Index3", IndexType.FUNCTIONAL, "itr1.testFields.get('integer')",
 +          "/testRgn itr1");
 +      Index i4 = qs.createIndex("Index4", IndexType.FUNCTIONAL, "itr1.testFields.get('long')",
 +          "/testRgn itr1");  
 +      Index i5 = qs.createIndex("Index5", IndexType.FUNCTIONAL, "itr1.testFields.get('complex')",
 +          "/testRgn itr1");
 +      
 +      assertTrue(i1 instanceof CompactRangeIndex);
 +      
 +            //Execute Queries with Indexes
 +        for (int i = 0; i < queries.length; i++) {
 +            Query q = null;
 +            try {
 +                q = CacheUtils.getQueryService().newQuery(queries[i]);
 +                CacheUtils.getLogger().info("Executing query: " + queries[i]);
 +                QueryObserverImpl observer = new QueryObserverImpl();
 +                QueryObserverHolder.setInstance(observer);
 +                r[i][1] = q.execute();
 +                CacheUtils.log("Executing query: " + queries[i] + " with index created");
 +                if(!observer.isIndexesUsed){
 +                    fail("Index is NOT uesd");
 +                }              
 +                Iterator itr = observer.indexesUsed.iterator();
 +                assertTrue(itr.hasNext());
 +                String temp = itr.next().toString();
 +                assertEquals(temp,"Index" + (i + 1));       
 +                
 +            } catch (Exception e) {
 +                e.printStackTrace();
 +                fail(q.getQueryString());
 +            }
 +        }
 +        StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
 +        ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r,queries.length,queries);
 +        
 +        //Test queries index not used
 +        for (int i = 0; i < queriesIndexNotUsed.length; i++) {
 +          Query q = null;
 +          try {
 +              q = CacheUtils.getQueryService().newQuery(queriesIndexNotUsed[i]);
 +              CacheUtils.getLogger().info("Executing query: " + queriesIndexNotUsed[i]);
 +              QueryObserverImpl observer = new QueryObserverImpl();
 +              QueryObserverHolder.setInstance(observer);            
 +              CacheUtils.log("Executing query: " + queriesIndexNotUsed[i] + " with index created");
 +              q.execute();
 +              assertFalse(observer.isIndexesUsed);                          
 +              Iterator itr = observer.indexesUsed.iterator();
 +              assertFalse(itr.hasNext());                   
 +              
 +          } catch (Exception e) {
 +              e.printStackTrace();
 +              fail(q.getQueryString());
 +          }
 +      }
 +    }
 +    
 +    @Test
 +  public void testCompactMapIndexUsageManyKeysWithVariousValueTypes() throws Exception {
 +      QueryService qs;
 +      qs = CacheUtils.getQueryService();
 +      LocalRegion testRgn = (LocalRegion)CacheUtils.createRegion("testRgn", null);
 +      int ID =1;
 +      for(; ID <=30; ++ID) {
 +        TestObject object = new TestObject(ID);
 +        testRgn.put(ID, object);
 +      }
 +      String queries[] = {
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields['string'] = '1'",
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields['double'] > 1D",
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields['integer'] > 1",
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields['long'] > 1L"
 +      };
 +      String queriesIndexNotUsed[] = {        
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields.get('string') = '1'",       
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields.get('double') > 1D",         
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields.get('integer') > 1",         
 +          "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.testFields.get('long') > 1L"         
 +       };
 +      Object r[][]= new Object[queries.length][2];
 +      
 +      qs = CacheUtils.getQueryService();
 +      
 +    //Execute Queries without Indexes
 +      for (int i = 0; i < queries.length; i++) {
 +          Query q = null;
 +          try {
 +              q = CacheUtils.getQueryService().newQuery(queries[i]);
 +              CacheUtils.getLogger().info("Executing query: " + queries[i]);              
 +              r[i][0] = q.execute();         
 +            CacheUtils.log("Executed query: " + queries[i] );
 +          } catch (Exception e) {
 +              e.printStackTrace();
 +              fail(q.getQueryString());
 +          }
 +      }  
 +      
 +      Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL, "itr1.testFields['string']",
 +          "/testRgn itr1");
 +      Index i2 = qs.createIndex("Index2", IndexType.FUNCTIONAL, "itr1.testFields['double']",
 +          "/testRgn itr1");
 +      Index i3 = qs.createIndex("Index3", IndexType.FUNCTIONAL, "itr1.testFields['integer']",
 +          "/testRgn itr1");
 +      Index i4 = qs.createIndex("Index4", IndexType.FUNCTIONAL, "itr1.testFields['long']",
 +          "/testRgn itr1");
 +      Index i5 = qs.createIndex("Index5", IndexType.FUNCTIONAL, "itr1.testFields['complex']",
 +          "/testRgn itr1");
 +      
 +      assertTrue(i1 instanceof CompactRangeIndex);
 +      
 +            //Execute Queries with Indexes
 +        for (int i = 0; i < queries.length; i++) {
 +            Query q = null;
 +            try {
 +                q = CacheUtils.getQueryService().newQuery(queries[i]);
 +                CacheUtils.getLogger().info("Executing query: " + queries[i]);
 +                QueryObserverImpl observer = new QueryObserverImpl();
 +                QueryObserverHolder.setInstance(observer);
 +                r[i][1] = q.execute();
 +                CacheUtils.log("Executing query: " + queries[i] + " with index created");
 +                if(!observer.isIndexesUsed){
 +                    fail("Index is NOT uesd");
 +                }              
 +                Iterator itr = observer.indexesUsed.iterator();
 +                assertTrue(itr.hasNext());
 +                String temp = itr.next().toString();
 +                assertEquals(temp,"Index" + (i+1));       
 +                
 +            } catch (Exception e) {
 +                e.printStackTrace();
 +                fail(q.getQueryString());
 +            }
 +        }
 +        StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
 +        ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r,queries.length,queries);
 +        
 +        //Test queries index not used
 +        for (int i = 0; i < queriesIndexNotUsed.length; i++) {
 +          Query q = null;
 +          try {
 +              q = CacheUtils.getQueryService().newQuery(queriesIndexNotUsed[i]);
 +              CacheUtils.getLogger().info("Executing query: " + queriesIndexNotUsed[i]);
 +              QueryObserverImpl observer = new QueryObserverImpl();
 +              QueryObserverHolder.setInstance(observer);            
 +              CacheUtils.log("Executing query: " + queriesIndexNotUsed[i] + " with index created");
 +              q.execute();
 +              assertFalse(observer.isIndexesUsed);                          
 +              Iterator itr = observer.indexesUsed.iterator();
 +              assertFalse(itr.hasNext());                   
 +              
 +          } catch (Exception e) {
 +              e.printStackTrace();
 +              fail(q.getQueryString());
 +          }
 +      }
 +    }
 +
 +    @Test
 +  public void testIndexUseSelfJoin() throws Exception {
 +      String[] queries = {"SELECT DISTINCT * FROM /pos p1, /pos p2 where p1.status = p2.status",
 +          "SELECT DISTINCT * FROM /pos p1, /pos p2 where p1.ID = p2.ID",
 +          "SELECT DISTINCT * FROM /pos p1, /pos p2 where p1.P1.secId = p2.P1.secId",
 +          "SELECT DISTINCT * FROM /pos p1, /pos p2 where p1.status = p2.status and p1.status = 'active'",
 +          "SELECT DISTINCT * FROM /pos p1, /pos p2 where p1.ID = p2.ID and p1.ID < 2",
 +          "SELECT * FROM /pos p1, /pos p2 where p1.ID = p2.ID",
 +          "SELECT * FROM /pos p1, /pos p2 where p1.P1.secId = p2.P1.secId",
 +          "SELECT * FROM /pos p1, /pos p2 where p1.status = p2.status and p1.status = 'active'",
 +          "SELECT * FROM /pos p1, /pos p2 where p1.ID = p2.ID and p1.ID < 2"};
 +
 +      SelectResults[][] sr = new SelectResults[queries.length][2];
 +        for (int j = 0; j < queries.length; j++) {
 +          Query q = qs.newQuery(queries[j]);
 +          QueryObserverImpl observer = new QueryObserverImpl();
 +          QueryObserverHolder.setInstance(observer);
 +          sr[j][0] = (SelectResults) q.execute();
 +          if(sr[j][0].size() == 0) {
 +            fail("Query " + q.getQueryString() + " should have returned results");
 +          }
 +          if (!observer.isIndexesUsed) {
 +            fail("Index should have been used for query '" + q.getQueryString() + "'");
 +          }
 +        }
 +        qs.removeIndexes();
 +        for (int j = 0; j < queries.length; j++) {
 +          Query q = qs.newQuery(queries[j]);
 +          QueryObserverImpl observer = new QueryObserverImpl();
 +          QueryObserverHolder.setInstance(observer);
 +          sr[j][1] = (SelectResults) q.execute();
 +          if(sr[j][1].size() == 0) {
 +            fail("Query " + q.getQueryString() + " should have returned results");
 +          }
 +          if (observer.isIndexesUsed) {
 +            fail("Index should not be used for query '" + q.getQueryString() + "'");
 +          }
 +        }
 +        CacheUtils.compareResultsOfWithAndWithoutIndex(sr);
 +    }
 +
 +    @Test
-   public void testUndefinedInResults() throws Exception {
++  public void testUndefinedResultsAreReturnedWhenANotEqualsQueryIsExecuted() throws Exception {
 +      Portfolio p1 = new Portfolio(0);
 +      p1.position1 = null;
 +      region.put("0", p1);
 +      Portfolio p2 = new Portfolio(2);
 +      p2.position1 = null;
 +      region.put("2", p2);
 +
 +      String query = "SELECT p.position1.secId FROM /pos p where p.position1.secId != 'MSFT' ";
 +      SelectResults[][] sr = new SelectResults[1][2];
 +      sr[0][0] = (SelectResults) qs.newQuery(query).execute();
 +      qs.removeIndexes();
 +      sr[0][1] = (SelectResults) qs.newQuery(query).execute();
 +      if(!CacheUtils.compareResultsOfWithAndWithoutIndex(sr)) {
 +        fail("Query results not same with and without index");
 +      }
 +    }
 +
 +    @Test
-     public void testBug52444() throws Exception {
++    public void testIndexesRemainInUseAfterARebalance() throws Exception {
 +        // Create partitioned region
 +        PartitionAttributesFactory paf = new PartitionAttributesFactory();
 +        AttributesFactory af = new AttributesFactory();
 +        af.setPartitionAttributes(paf.create());
-         Region region = CacheUtils.createRegion("testBug52444", af.create(), false);
++        Region region = CacheUtils.createRegion("testIndexesRemainInUseAfterARebalance", af.create(), false);
 +
 +        // Add index
 +        PartitionedIndex index = (PartitionedIndex) qs.createIndex("statusIndex", "status", region.getFullPath());
 +
 +        // Do puts
 +        for (int i=0; i<200; i++) {
 +          region.put(i, new Portfolio(i));
 +        }
 +        
 +        // Initialize query observer
 +        QueryObserverImpl observer = new QueryObserverImpl();
 +        QueryObserverHolder.setInstance(observer);
 +        
 +        // Create and run query
 +        Query query = qs.newQuery("SELECT * FROM " + region.getFullPath() + " where status = 'active'");
 +        query.execute();
 +
 +        // Verify index was used
 +        assertTrue(observer.isIndexesUsed);
 +
 +        // Get the first index entry in the PartitionedIndex bucketIndexes and delete the index from it (to simulate what happens when a bucket is moved)
 +        Map.Entry<Region,List<Index>> firstIndexEntry = index.getFirstBucketIndex();
 +        assertTrue(!firstIndexEntry.getValue().isEmpty());
 +        index.removeFromBucketIndexes(firstIndexEntry.getKey(), firstIndexEntry.getValue().iterator().next());
 +        
 +        // Verify the index was removed from the entry and the entry was removed from the bucket indexes
 +        assertTrue(firstIndexEntry.getValue().isEmpty());      
 +        Map.Entry<Region,List<Index>> nextFirstIndexEntry = index.getFirstBucketIndex();
 +        assertTrue(!nextFirstIndexEntry.getValue().isEmpty());
 +        
 +        // Run query again
 +        observer.reset();
 +        query.execute();
 +        
 +        // Verify index was still used
 +        assertTrue(observer.isIndexesUsed);
 +      }
++    
++  @Test
++  public void testPKIndexUseWithPR() throws Exception {
++    evaluatePKIndexUsetest(RegionShortcut.PARTITION, "pkTest");
++  }
++
++  @Test
++  public void testPKIndexUseWithReplicate() throws Exception {
++    evaluatePKIndexUsetest(RegionShortcut.REPLICATE, "pkTest");
++  }
++    
++  private void evaluatePKIndexUsetest(RegionShortcut regionShortcut, String regionName) throws Exception {
++    Cache cache = CacheUtils.getCache();
++    Region r = cache.createRegionFactory(regionShortcut).create(regionName);
++    Map map0 = new HashMap();
++    Map map1 = new HashMap();
++    Map map2 = new HashMap();
++    r.put("A0", map0);
++    r.put("A1", map1);
++    r.put("A2", map2);
++    qs.createIndex("pkIndex", IndexType.PRIMARY_KEY, "p.pk", "/" + regionName + " p");
++    QueryObserverImpl observer = new QueryObserverImpl();
++    QueryObserverHolder.setInstance(observer);
++    SelectResults sr = (SelectResults) qs.newQuery("select * from /" + regionName + " p where p.pk = 'A1'").execute();
++    assertEquals(1, sr.size());
++
++    if (!observer.isIndexesUsed) {
++      fail("Index not used for operator '='");
++    }
++  }
 +
 +  class QueryObserverImpl extends QueryObserverAdapter
 +  {
 +    boolean isIndexesUsed = false;
 +
 +    ArrayList indexesUsed = new ArrayList();
 +
 +    public void beforeIndexLookup(Index index, int oper, Object key) {
 +      indexesUsed.add(index.getName());
 +    }
 +
 +    public void afterIndexLookup(Collection results) {
 +      if (results != null) {
 +        isIndexesUsed = true;
 +      }
 +    }
 +    
 +    public void reset() {
 +      this.isIndexesUsed = false;
 +      this.indexesUsed.clear();
 +    }
 +  }
 +  
 +  public class RangeIndexTestHook implements TestHook {
 +
 +    int lastHook = -1;
 +    @Override
 +    public void hook(int spot) throws RuntimeException {
 +      lastHook = spot;
 +      CacheUtils.getCache().getLogger().fine("Inside RangeIndexTestHook for spot "+ spot);
 +      if (spot == 1) { //LT size estimate
 +        CacheUtils.getCache().getRegion("testRgn").clear();
 +      } else if (spot == 2) { //GT size estimate
 +        CacheUtils.getCache().getRegion("testRgn").clear();
 +      }
 +    }
 +
 +    public boolean isHooked(int spot) {
 +      return spot==lastHook;
 +    }
 +  }
 +  static  class MapKeyIndexData {
 +    int id;
 +    public Map maap = new HashMap();
 +    public List liist = new ArrayList();
 +    public MapKeyIndexData(int id) {
 +      this.id = id;
 +    }
 +    public void addKeyValue(Object key, Object value) {
 +      this.maap.put(key,value);
 +      this.liist.add(value);
 +    }
 +  }
 +  private class TestObject {
 +      public Map testFields = new HashMap();
 +      TestObject(int i) {
 +        testFields.put("string", "String Value" + i);
 +        testFields.put("double", (double) i);
 +        testFields.put("integer", i);
 +        testFields.put("long", (long) i);
 +        testFields.put("complex", new CompObject(i));
 +      }
 +      public Map getTestFields() {
 +        return testFields;
 +      }
 +    }
 +    
 +    private class CompObject implements Comparable {
 +      int value;
 +      CompObject(int i) {
 +        value = i;
 +      }
 +      
 +      
 +      public int compareTo(Object o) {
 +        if (o instanceof CompObject) {
 +          CompObject other = (CompObject) o;
 +          if (value > other.value) {
 +            return 1;
 +          }
 +          else if (value == other.value){
 +            return 0;
 +          }
 +          else {
 +            return -1;
 +          }
 +        }
 +        throw new ClassCastException("Could not cast " + o.getClass().getName() + " to compObject");
 +      }
 +      
 +    }
 +
 +}


[30/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/BaseCommand.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/BaseCommand.java
index 52ea6f5,0000000..dd13f19
mode 100755,000000..100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/BaseCommand.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/BaseCommand.java
@@@ -1,1625 -1,0 +1,1625 @@@
 +/*
 + * 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.tier.sockets;
 +
 +import java.io.EOFException;
 +import java.io.IOException;
 +import java.io.InterruptedIOException;
 +import java.io.PrintWriter;
 +import java.io.StringWriter;
 +import java.util.ArrayList;
 +import java.util.Collection;
 +import java.util.HashMap;
 +import java.util.HashSet;
 +import java.util.Iterator;
 +import java.util.List;
 +import java.util.Map;
 +import java.util.Set;
 +import java.util.concurrent.Semaphore;
 +import java.util.regex.Pattern;
 +
 +import org.apache.logging.log4j.Logger;
 +
 +import com.gemstone.gemfire.CancelException;
 +import com.gemstone.gemfire.CopyException;
 +import com.gemstone.gemfire.InternalGemFireError;
 +import com.gemstone.gemfire.SerializationException;
 +import com.gemstone.gemfire.SystemFailure;
 +import com.gemstone.gemfire.cache.CacheLoaderException;
 +import com.gemstone.gemfire.cache.CacheWriterException;
 +import com.gemstone.gemfire.cache.InterestResultPolicy;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionDestroyedException;
 +import com.gemstone.gemfire.cache.TransactionException;
 +import com.gemstone.gemfire.cache.persistence.PartitionOfflineException;
 +import com.gemstone.gemfire.cache.query.types.CollectionType;
 +import com.gemstone.gemfire.distributed.DistributedSystemDisconnectedException;
 +import com.gemstone.gemfire.distributed.internal.DistributionStats;
 +import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
 +import com.gemstone.gemfire.internal.Assert;
 +import com.gemstone.gemfire.internal.Version;
 +import com.gemstone.gemfire.internal.cache.CachedDeserializable;
 +import com.gemstone.gemfire.internal.cache.DistributedRegion;
 +import com.gemstone.gemfire.internal.cache.EntryEventImpl;
 +import com.gemstone.gemfire.internal.cache.EntrySnapshot;
 +import com.gemstone.gemfire.internal.cache.EventID;
 +import com.gemstone.gemfire.internal.cache.FindVersionTagOperation;
 +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
 +import com.gemstone.gemfire.internal.cache.LocalRegion;
 +import com.gemstone.gemfire.internal.cache.LocalRegion.NonTXEntry;
 +import com.gemstone.gemfire.internal.cache.PartitionedRegion;
 +import com.gemstone.gemfire.internal.cache.PartitionedRegionHelper;
 +import com.gemstone.gemfire.internal.cache.TXManagerImpl;
 +import com.gemstone.gemfire.internal.cache.TXStateProxy;
 +import com.gemstone.gemfire.internal.cache.Token;
 +import com.gemstone.gemfire.internal.cache.tier.CachedRegionHelper;
 +import com.gemstone.gemfire.internal.cache.tier.Command;
 +import com.gemstone.gemfire.internal.cache.tier.InterestType;
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.versions.VersionStamp;
 +import com.gemstone.gemfire.internal.cache.versions.VersionTag;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.logging.LogService;
 +import com.gemstone.gemfire.internal.logging.log4j.LocalizedMessage;
 +import com.gemstone.gemfire.internal.offheap.OffHeapHelper;
 +import com.gemstone.gemfire.internal.sequencelog.EntryLogger;
 +import com.gemstone.gemfire.security.GemFireSecurityException;
 +
 +/**
 + * @author ashahid
 + *
 + */
 +public abstract class BaseCommand implements Command {
 +  protected static final Logger logger = LogService.getLogger();
 +
 +  /**
 +   * Whether zipped values are being passed to/from the client. Can be modified
 +   * using the system property Message.ZIP_VALUES ? This does not appear to
 +   * happen anywhere
 +   */
 +  protected static final boolean zipValues = false;
 +
 +  protected static final boolean APPLY_RETRIES = Boolean
 +      .getBoolean("gemfire.gateway.ApplyRetries");
 +
 +  public static final byte[] OK_BYTES = new byte[]{0};  
 +
 +  public static final int maximumChunkSize = Integer.getInteger(
 +      "BridgeServer.MAXIMUM_CHUNK_SIZE", 100).intValue();
 +
 +  /** Maximum number of entries in each chunked response chunk */
 +
 +  /** Whether to suppress logging of IOExceptions */
 +  private static boolean suppressIOExceptionLogging = Boolean
 +      .getBoolean("gemfire.bridge.suppressIOExceptionLogging");
 +
 +  /**
 +   * Maximum number of concurrent incoming client message bytes that a bridge
 +   * server will allow. Once a server is working on this number additional
 +   * incoming client messages will wait until one of them completes or fails.
 +   * The bytes are computed based in the size sent in the incoming msg header.
 +   */
 +  private static final int MAX_INCOMING_DATA = Integer.getInteger(
 +      "BridgeServer.MAX_INCOMING_DATA", -1).intValue();
 +
 +  /**
 +   * Maximum number of concurrent incoming client messages that a bridge server
 +   * will allow. Once a server is working on this number additional incoming
 +   * client messages will wait until one of them completes or fails.
 +   */
 +  private static final int MAX_INCOMING_MSGS = Integer.getInteger(
 +      "BridgeServer.MAX_INCOMING_MSGS", -1).intValue();
 +
 +  private static final Semaphore incomingDataLimiter;
 +
 +  private static final Semaphore incomingMsgLimiter;
 +  static {
 +    Semaphore tmp;
 +    if (MAX_INCOMING_DATA > 0) {
 +      // backport requires that this is fair since we inc by values > 1
 +      tmp = new Semaphore(MAX_INCOMING_DATA, true);
 +    }
 +    else {
 +      tmp = null;
 +    }
 +    incomingDataLimiter = tmp;
 +    if (MAX_INCOMING_MSGS > 0) {
 +      tmp = new Semaphore(MAX_INCOMING_MSGS, false); // unfair for best
 +      // performance
 +    }
 +    else {
 +      tmp = null;
 +    }
 +    incomingMsgLimiter = tmp;
 +
 +  }
 +
 +  final public void execute(Message msg, ServerConnection servConn) {
 +    // Read the request and update the statistics
 +    long start = DistributionStats.getStatTime();
 +    //servConn.resetTransientData();
 +    if(EntryLogger.isEnabled() && servConn  != null) {
 +      EntryLogger.setSource(servConn.getMembershipID(), "c2s");
 +    }
 +    boolean shouldMasquerade = shouldMasqueradeForTx(msg, servConn);
 +    try {
 +      if (shouldMasquerade) {
 +        GemFireCacheImpl  cache = (GemFireCacheImpl)servConn.getCache();
 +        InternalDistributedMember member = (InternalDistributedMember)servConn.getProxyID().getDistributedMember();
 +        TXManagerImpl txMgr = cache.getTxManager();
 +        TXStateProxy tx = null;
 +        try {
 +          tx = txMgr.masqueradeAs(msg, member, false);
 +          cmdExecute(msg, servConn, start);
 +        } finally {
 +          txMgr.unmasquerade(tx);
 +        }
 +      } else {
 +        cmdExecute(msg, servConn, start);
 +      }
 +      
 +    }   
 +    catch (EOFException eof) {
 +      BaseCommand.handleEOFException(msg, servConn, eof);
 +      // TODO:Asif: Check if there is any need for explicitly returning
 +      return;
 +    }
 +    catch (InterruptedIOException e) { // Solaris only
 +      BaseCommand.handleInterruptedIOException(msg, servConn, e);
 +      return;
 +    }
 +    catch (IOException e) {
 +      BaseCommand.handleIOException(msg, servConn, e);
 +      return;
 +    }
 +    catch (DistributedSystemDisconnectedException e) {
 +      BaseCommand.handleShutdownException(msg, servConn, e);
 +      return;
 +    }
 +    catch (PartitionOfflineException e) { // fix for bug #42225
 +      handleExceptionNoDisconnect(msg, servConn, e);
 +    }
 +    catch (GemFireSecurityException e) {
 +      handleExceptionNoDisconnect(msg, servConn, e);
 +    }
 +    catch (CacheLoaderException e) {
 +      handleExceptionNoDisconnect(msg, servConn, e);
 +    }
 +    catch (CacheWriterException e) {
 +      handleExceptionNoDisconnect(msg, servConn, e);
 +    } catch (SerializationException e) {
 +      handleExceptionNoDisconnect(msg, servConn, e);
 +    } catch (CopyException e) {
 +      handleExceptionNoDisconnect(msg, servConn, e);
 +    } catch (TransactionException e) {
 +      handleExceptionNoDisconnect(msg, servConn, e);
 +    }
 +    
 +    catch (VirtualMachineError err) {
 +      SystemFailure.initiateFailure(err);
 +      // If this ever returns, rethrow the error.  We're poisoned
 +      // now, so don't let this thread continue.
 +      throw err;
 +    }
 +    catch (Throwable e) {
 +      BaseCommand.handleThrowable(msg, servConn, e);
 +    } finally {
 +      EntryLogger.clearSource();
 +    }
 +    /*
 +     * finally { // Keep track of the fact that a message is no longer being //
 +     * processed. servConn.setNotProcessingMessage();
 +     * servConn.clearRequestMsg(); }
 +     */
 +  }
 +
 +  /**
 +   * checks to see if this thread needs to masquerade as a transactional thread.
 +   * clients after GFE_66 should be able to start a transaction.
 +   * @param msg
 +   * @param servConn
 +   * @return true if thread should masquerade as a transactional thread.
 +   */
 +  protected boolean shouldMasqueradeForTx(Message msg, ServerConnection servConn) {
 +    if (servConn.getClientVersion().compareTo(Version.GFE_66) >= 0
 +        && msg.getTransactionId() > TXManagerImpl.NOTX) {
 +      return true;
 +    }
 +    return false;
 +  }
 +  
 +  /**
 +   * If an operation is retried then some server may have seen it already.
 +   * We cannot apply this operation to the cache without knowing whether a
 +   * version tag has already been created for it.  Otherwise caches that have
 +   * seen the event already will reject it but others will not, but will have
 +   * no version tag with which to perform concurrency checks.
 +   * <p>The client event should have the event identifier from the client and
 +   * the region affected by the operation.
 +   * @param clientEvent
 +   */
 +  public boolean recoverVersionTagForRetriedOperation(EntryEventImpl clientEvent) {
 +    LocalRegion r = clientEvent.getRegion();
 +    VersionTag tag =  null;
 +    if ((clientEvent.getVersionTag() != null) && (clientEvent.getVersionTag().isGatewayTag())) {
 +      tag = r.findVersionTagForGatewayEvent(clientEvent.getEventId());
 +    }
 +    else {
 +      tag = r.findVersionTagForClientEvent(clientEvent.getEventId());
 +    }
 +    if (tag == null) {
 +      if (r instanceof DistributedRegion || r instanceof PartitionedRegion) {
 +        // TODO this could be optimized for partitioned regions by sending the key
 +        // so that the PR could look at an individual bucket for the event
 +        tag = FindVersionTagOperation.findVersionTag(r, clientEvent.getEventId(), false);
 +      }
 +    }
 +    if (tag != null) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("recovered version tag {} for replayed operation {}", tag, clientEvent.getEventId());
 +      }
 +      clientEvent.setVersionTag(tag);
 +    }
 +    return (tag != null);
 +  }
 +  
 +  /**
 +   * If an operation is retried then some server may have seen it already.
 +   * We cannot apply this operation to the cache without knowing whether a
 +   * version tag has already been created for it.  Otherwise caches that have
 +   * seen the event already will reject it but others will not, but will have
 +   * no version tag with which to perform concurrency checks.
 +   * <p>The client event should have the event identifier from the client and
 +   * the region affected by the operation.
 +   */
 +  protected VersionTag findVersionTagsForRetriedBulkOp(LocalRegion r, EventID eventID) {
 +    VersionTag tag = r.findVersionTagForClientBulkOp(eventID);
 +    if(tag != null) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("recovered version tag {} for replayed bulk operation {}", tag, eventID);
 +      }
 +      return tag;
 +    }
 +    if (r instanceof DistributedRegion || r instanceof PartitionedRegion) {
 +      // TODO this could be optimized for partitioned regions by sending the key
 +      // so that the PR could look at an individual bucket for the event
 +      tag = FindVersionTagOperation.findVersionTag(r, eventID, true);
 +    }
 +    if (tag != null) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("recovered version tag {} for replayed bulk operation {}", tag, eventID);
 +      }
 +    }
 +    return tag;
 +  }
 +
 +  abstract public void cmdExecute(Message msg, ServerConnection servConn,
 +      long start) throws IOException, ClassNotFoundException, InterruptedException;
 +
 +  protected void writeReply(Message origMsg, ServerConnection servConn)
 +      throws IOException {
 +    Message replyMsg = servConn.getReplyMessage();
 +    servConn.getCache().getCancelCriterion().checkCancelInProgress(null);
 +    replyMsg.setMessageType(MessageType.REPLY);
 +    replyMsg.setNumberOfParts(1);
 +    replyMsg.setTransactionId(origMsg.getTransactionId());
 +    replyMsg.addBytesPart(OK_BYTES);
 +    replyMsg.send(servConn);
 +    if (logger.isTraceEnabled()) {
 +      logger.trace("{}: rpl tx: {}", servConn.getName(), origMsg.getTransactionId());
 +    }
 +  }
 +  protected void writeReplyWithRefreshMetadata(Message origMsg,
 +      ServerConnection servConn, PartitionedRegion pr, byte nwHop) throws IOException {
 +    Message replyMsg = servConn.getReplyMessage();
 +    servConn.getCache().getCancelCriterion().checkCancelInProgress(null);
 +    replyMsg.setMessageType(MessageType.REPLY);
 +    replyMsg.setNumberOfParts(1);
 +    replyMsg.setTransactionId(origMsg.getTransactionId());
 +    replyMsg.addBytesPart(new byte[]{pr.getMetadataVersion().byteValue(), nwHop});
 +    replyMsg.send(servConn);
 +    pr.getPrStats().incPRMetaDataSentCount();
 +    if (logger.isTraceEnabled()) {
 +      logger.trace("{}: rpl with REFRESH_METADAT tx: {}", servConn.getName(), origMsg.getTransactionId());
 +    }
 +  }
 +
 +  private static void handleEOFException(Message msg,
 +      ServerConnection servConn, Exception eof) {
 +    CachedRegionHelper crHelper = servConn.getCachedRegionHelper();
 +    CacheServerStats stats = servConn.getCacheServerStats();
 +    boolean potentialModification = servConn.getPotentialModification();
 +    if (!crHelper.isShutdown()) {
 +      if (potentialModification) {
 +        stats.incAbandonedWriteRequests();
 +      }
 +      else {
 +        stats.incAbandonedReadRequests();
 +      }
 +      if (!suppressIOExceptionLogging) {
 +        if (potentialModification) {
 +          int transId = (msg != null) ? msg.getTransactionId()
 +              : Integer.MIN_VALUE;
 +          logger.warn(LocalizedMessage.create(
 +            LocalizedStrings.BaseCommand_0_EOFEXCEPTION_DURING_A_WRITE_OPERATION_ON_REGION__1_KEY_2_MESSAGEID_3,
 +            new Object[] {servConn.getName(), servConn.getModRegion(), servConn.getModKey(), Integer.valueOf(transId)}));
 +        }
 +        else {
 +          logger.debug("EOF exception", eof);
 +          logger.info(LocalizedMessage.create(
 +            LocalizedStrings.BaseCommand_0_CONNECTION_DISCONNECT_DETECTED_BY_EOF,
 +            servConn.getName()));
 +        }
 +      }
 +    }
 +    servConn.setFlagProcessMessagesAsFalse();
 +  }
 +
 +  private static void handleInterruptedIOException(Message msg,
 +      ServerConnection servConn, Exception e) {
 +    CachedRegionHelper crHelper = servConn.getCachedRegionHelper();
 +    if (!crHelper.isShutdown() && servConn.isOpen()) {
 +      if (!suppressIOExceptionLogging) {
 +        if (logger.isDebugEnabled())
 +          logger.debug("Aborted message due to interrupt: {}", e.getMessage(), e);
 +      }
 +    }
 +    servConn.setFlagProcessMessagesAsFalse();
 +  }
 +
 +  private static void handleIOException(Message msg, ServerConnection servConn,
 +      Exception e) {
 +    CachedRegionHelper crHelper = servConn.getCachedRegionHelper();
 +    boolean potentialModification = servConn.getPotentialModification();
 +
 +    if (!crHelper.isShutdown() && servConn.isOpen()) {
 +      if (!suppressIOExceptionLogging) {
 +        if (potentialModification) {
 +          int transId = (msg != null) ? msg.getTransactionId()
 +              : Integer.MIN_VALUE;
 +          logger.warn(LocalizedMessage.create(
 +            LocalizedStrings.BaseCommand_0_UNEXPECTED_IOEXCEPTION_DURING_OPERATION_FOR_REGION_1_KEY_2_MESSID_3,
 +            new Object[] {servConn.getName(), servConn.getModRegion(), servConn.getModKey(), Integer.valueOf(transId)}), e);
 +        }
 +        else {
 +          logger.warn(LocalizedMessage.create(
 +            LocalizedStrings.BaseCommand_0_UNEXPECTED_IOEXCEPTION,
 +            servConn.getName()), e);
 +        }
 +      }
 +    }
 +    servConn.setFlagProcessMessagesAsFalse();
 +  }
 +
 +  private static void handleShutdownException(Message msg,
 +      ServerConnection servConn, Exception e) {
 +    CachedRegionHelper crHelper = servConn.getCachedRegionHelper();
 +    boolean potentialModification = servConn.getPotentialModification();
 +
 +    if (!crHelper.isShutdown()) {
 +      if (potentialModification) {
 +        int transId = (msg != null) ? msg.getTransactionId()
 +            : Integer.MIN_VALUE;
 +        logger.warn(LocalizedMessage.create(
 +          LocalizedStrings.BaseCommand_0_UNEXPECTED_SHUTDOWNEXCEPTION_DURING_OPERATION_ON_REGION_1_KEY_2_MESSAGEID_3,
 +          new Object[] {servConn.getName(), servConn.getModRegion(), servConn.getModKey(), Integer.valueOf(transId)}), e);
 +      }
 +      else {
 +        logger.warn(LocalizedMessage.create(
 +          LocalizedStrings.BaseCommand_0_UNEXPECTED_SHUTDOWNEXCEPTION,
 +          servConn.getName()),e);
 +        }
 +    }
 +    servConn.setFlagProcessMessagesAsFalse();
 +  }
 +
 +  // Handle GemfireSecurityExceptions separately since the connection should not
 +  // be terminated (by setting processMessages to false) unlike in
 +  // handleThrowable. Fixes bugs #38384 and #39392.
 +//  private static void handleGemfireSecurityException(Message msg,
 +//      ServerConnection servConn, GemFireSecurityException e) {
 +//
 +//    boolean requiresResponse = servConn.getTransientFlag(REQUIRES_RESPONSE);
 +//    boolean responded = servConn.getTransientFlag(RESPONDED);
 +//    boolean requiresChunkedResponse = servConn
 +//        .getTransientFlag(REQUIRES_CHUNKED_RESPONSE);
 +//    boolean potentialModification = servConn.getPotentialModification();
 +//
 +//    try {
 +//      try {
 +//        if (requiresResponse && !responded) {
 +//          if (requiresChunkedResponse) {
 +//            writeChunkedException(msg, e, false, servConn);
 +//          }
 +//          else {
 +//            writeException(msg, e, false, servConn);
 +//          }
 +//          servConn.setAsTrue(RESPONDED);
 +//        }
 +//      }
 +//      finally { // inner try-finally to ensure proper ordering of logging
 +//        if (potentialModification) {
 +//          int transId = (msg != null) ? msg.getTransactionId()
 +//              : Integer.MIN_VALUE;
 +//        }
 +//      }
 +//    }
 +//    catch (IOException ioe) {
 +//      if (logger.isDebugEnabled()) {
 +//        logger.fine(servConn.getName()
 +//            + ": Unexpected IOException writing security exception: ", ioe);
 +//      }
 +//    }
 +//  }
 +
 +  private static void handleExceptionNoDisconnect(Message msg,
 +      ServerConnection servConn, Exception e) {
 +    boolean requiresResponse = servConn.getTransientFlag(REQUIRES_RESPONSE);
 +    boolean responded = servConn.getTransientFlag(RESPONDED);
 +    boolean requiresChunkedResponse = servConn
 +        .getTransientFlag(REQUIRES_CHUNKED_RESPONSE);
 +    boolean potentialModification = servConn.getPotentialModification();
 +    boolean wroteExceptionResponse = false;
 +
 +    try {
 +      try {
 +        if (requiresResponse && !responded) {
 +          if (requiresChunkedResponse) {
 +            writeChunkedException(msg, e, false, servConn);
 +          }
 +          else {
 +            writeException(msg, e, false, servConn);
 +          }
 +          wroteExceptionResponse = true;
 +          servConn.setAsTrue(RESPONDED);
 +        }
 +      }
 +      finally { // inner try-finally to ensure proper ordering of logging
 +        if (potentialModification) {
 +          int transId = (msg != null) ? msg.getTransactionId()
 +              : Integer.MIN_VALUE;
 +          if (!wroteExceptionResponse) {
 +            logger.warn(LocalizedMessage.create(
 +                LocalizedStrings.BaseCommand_0_UNEXPECTED_EXCEPTION_DURING_OPERATION_ON_REGION_1_KEY_2_MESSAGEID_3,
 +                new Object[] {servConn.getName(),servConn.getModRegion(), servConn.getModKey(), Integer.valueOf(transId)}), e);
 +          } else {
 +            if (logger.isDebugEnabled()) {
 +              logger.debug("{}: Exception during operation on region: {} key: {} messageId: {}", servConn.getName(),
 +                  servConn.getModRegion(), servConn.getModKey(), transId, e);
 +            }
 +          }
 +        }
 +        else {
 +          if (!wroteExceptionResponse) {
 +            logger.warn(LocalizedMessage.create(
 +                LocalizedStrings.BaseCommand_0_UNEXPECTED_EXCEPTION,
 +                servConn.getName()), e);
 +          } else {
 +            if (logger.isDebugEnabled()) {
 +              logger.debug("{}: Exception: {}", servConn.getName(), e.getMessage(), e);
 +            }
 +          }
 +        }
 +      }
 +    }
 +    catch (IOException ioe) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}: Unexpected IOException writing exception: {}", servConn.getName(), ioe.getMessage(), ioe);
 +      }
 +    }
 +  }
 +
 +  private static void handleThrowable(Message msg, ServerConnection servConn,
 +      Throwable th) {
 +    boolean requiresResponse = servConn.getTransientFlag(REQUIRES_RESPONSE);
 +    boolean responded = servConn.getTransientFlag(RESPONDED);
 +    boolean requiresChunkedResponse = servConn
 +        .getTransientFlag(REQUIRES_CHUNKED_RESPONSE);
 +    boolean potentialModification = servConn.getPotentialModification();
 +
 +    try {
 +      try {
 +        if (th instanceof Error) {
 +          logger.fatal(LocalizedMessage.create(LocalizedStrings.BaseCommand_0_UNEXPECTED_ERROR_ON_SERVER,
 +              servConn.getName()), th);
 +        }
 +        if (requiresResponse && !responded) {
 +          if (requiresChunkedResponse) {
 +            writeChunkedException(msg, th, false, servConn);
 +          }
 +          else {
 +            writeException(msg, th, false, servConn);
 +          }
 +          servConn.setAsTrue(RESPONDED);
 +        }
 +      }
 +      finally { // inner try-finally to ensure proper ordering of logging
 +        if (th instanceof Error) {
 +          // log nothing
 +        } else if (th instanceof CancelException) {
 +          // log nothing
 +        } else {
 +          if (potentialModification) {
 +            int transId = (msg != null) ? msg.getTransactionId()
 +                : Integer.MIN_VALUE;
 +            logger.warn(LocalizedMessage.create(
 +              LocalizedStrings.BaseCommand_0_UNEXPECTED_EXCEPTION_DURING_OPERATION_ON_REGION_1_KEY_2_MESSAGEID_3,
 +              new Object[] {servConn.getName(),servConn.getModRegion(), servConn.getModKey(), Integer.valueOf(transId)}), th);
 +          }
 +          else {
 +            logger.warn(LocalizedMessage.create(
 +              LocalizedStrings.BaseCommand_0_UNEXPECTED_EXCEPTION,
 +              servConn.getName()), th);
 +          }
 +        }
 +      }
 +    } catch (IOException ioe) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}: Unexpected IOException writing exception: {}", servConn.getName(), ioe.getMessage(), ioe);
 +      }
 +    } finally {
 +      servConn.setFlagProcessMessagesAsFalse();
 +    }
 +  }
 +  
 +  protected static void writeChunkedException(Message origMsg, Throwable e,
 +      boolean isSevere, ServerConnection servConn) throws IOException {
 +    writeChunkedException(origMsg, e, isSevere, servConn, servConn.getChunkedResponseMessage());
 +  }
 +
 +  protected static void writeChunkedException(Message origMsg, Throwable e,
 +      boolean isSevere, ServerConnection servConn, ChunkedMessage originalReponse) throws IOException {
 +    writeChunkedException(origMsg, e, isSevere, servConn, originalReponse, 2);
 +  }
 +
 +  protected static void writeChunkedException(Message origMsg, Throwable e,
 +      boolean isSevere, ServerConnection servConn, ChunkedMessage originalReponse, int numOfParts) throws IOException {
 +    ChunkedMessage chunkedResponseMsg = servConn.getChunkedResponseMessage();
 +    chunkedResponseMsg.setServerConnection(servConn);
 +    if (originalReponse.headerHasBeenSent()) {
 +      //chunkedResponseMsg = originalReponse;
 +      // fix for bug 35442
 +      chunkedResponseMsg.setNumberOfParts(numOfParts);
 +      chunkedResponseMsg.setLastChunkAndNumParts(true, numOfParts);
 +      chunkedResponseMsg.addObjPart(e); 
 +      if (numOfParts == 2) {
 +        chunkedResponseMsg.addStringPart(getExceptionTrace(e));
 +      }
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}: Sending exception chunk while reply in progress: {}", servConn.getName(), e.getMessage(), e);
 +      }
 +    }
 +    else {
 +      chunkedResponseMsg.setMessageType(MessageType.EXCEPTION);
 +      chunkedResponseMsg.setNumberOfParts(numOfParts);
 +      chunkedResponseMsg.setLastChunkAndNumParts(true, numOfParts);
 +      chunkedResponseMsg.setTransactionId(origMsg.getTransactionId());
 +      chunkedResponseMsg.sendHeader();
 +      chunkedResponseMsg.addObjPart(e);
 +      if (numOfParts == 2) {
 +        chunkedResponseMsg.addStringPart(getExceptionTrace(e));
 +      }
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}: Sending exception chunk: {}", servConn.getName(), e.getMessage(), e);
 +      }
 +    }
 +    chunkedResponseMsg.sendChunk(servConn);
 +  }
 +
 +  // Get the exception stacktrace for native clients
 +  public static String getExceptionTrace(Throwable ex) {
 +    StringWriter sw = new StringWriter();
 +    PrintWriter pw = new PrintWriter(sw);
 +    ex.printStackTrace(pw);
 +    pw.close();
 +    return sw.toString();
 +  }
 +
 +  protected static void writeException(Message origMsg, Throwable e,
 +      boolean isSevere, ServerConnection servConn) throws IOException {
 +    writeException(origMsg, MessageType.EXCEPTION, e, isSevere, servConn);
 +  }
 +
 +  protected static void writeException(Message origMsg, int msgType, Throwable e,
 +      boolean isSevere, ServerConnection servConn) throws IOException {
 +    Message errorMsg = servConn.getErrorResponseMessage();
 +    errorMsg.setMessageType(msgType);
 +    errorMsg.setNumberOfParts(2);
 +    errorMsg.setTransactionId(origMsg.getTransactionId());
 +    if (isSevere) {
 +      String msg = e.getMessage();
 +      if (msg == null) {
 +        msg = e.toString();
 +      }
 +      logger.fatal(LocalizedMessage.create(LocalizedStrings.BaseCommand_SEVERE_CACHE_EXCEPTION_0, msg));
 +    }
 +    errorMsg.addObjPart(e);
 +    errorMsg.addStringPart(getExceptionTrace(e));
 +    errorMsg.send(servConn);
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("{}: Wrote exception: {}", servConn.getName(), e.getMessage(), e);
 +    }
 +  }
 +
 +  protected static void writeErrorResponse(Message origMsg, int messageType,
 +      ServerConnection servConn) throws IOException {
 +    Message errorMsg = servConn.getErrorResponseMessage();
 +    errorMsg.setMessageType(messageType);
 +    errorMsg.setNumberOfParts(1);
 +    errorMsg.setTransactionId(origMsg.getTransactionId());
 +    errorMsg
 +        .addStringPart(LocalizedStrings.BaseCommand_INVALID_DATA_RECEIVED_PLEASE_SEE_THE_CACHE_SERVER_LOG_FILE_FOR_ADDITIONAL_DETAILS.toLocalizedString());
 +    errorMsg.send(servConn);
 +  }
 +
 +  protected static void writeErrorResponse(Message origMsg, int messageType,
 +      String msg, ServerConnection servConn) throws IOException {
 +    Message errorMsg = servConn.getErrorResponseMessage();
 +    errorMsg.setMessageType(messageType);
 +    errorMsg.setNumberOfParts(1);
 +    errorMsg.setTransactionId(origMsg.getTransactionId());
 +    errorMsg.addStringPart(msg);
 +    errorMsg.send(servConn);
 +  }
 +
 +  protected static void writeRegionDestroyedEx(Message msg, String regionName,
 +      String title, ServerConnection servConn) throws IOException {
 +    String reason = servConn.getName() + ": Region named " + regionName + title;
 +    RegionDestroyedException ex = new RegionDestroyedException(reason,
 +        regionName);
 +    if (servConn.getTransientFlag(REQUIRES_CHUNKED_RESPONSE)) {
 +      writeChunkedException(msg, ex, false, servConn);
 +    }
 +    else {
 +      writeException(msg, ex, false, servConn);
 +    }
 +  }
 +
 +  protected static void writeResponse(Object data, Object callbackArg,
 +      Message origMsg, boolean isObject, ServerConnection servConn)
 +      throws IOException {
 +    Message responseMsg = servConn.getResponseMessage();
 +    responseMsg.setMessageType(MessageType.RESPONSE);
 +    responseMsg.setTransactionId(origMsg.getTransactionId());
 +
 +    
 +    if (callbackArg == null) {
 +      responseMsg.setNumberOfParts(1);
 +    }
 +    else {
 +      responseMsg.setNumberOfParts(2);
 +    }
 +    if (data instanceof byte[]) {
 +      responseMsg.addRawPart((byte[])data, isObject);
 +    }
 +    else {
 +      Assert.assertTrue(isObject,
 +          "isObject should be true when value is not a byte[]");
 +      responseMsg.addObjPart(data, zipValues);
 +    }
 +    if (callbackArg != null) {
 +      responseMsg.addObjPart(callbackArg);
 +    }
 +    servConn.getCache().getCancelCriterion().checkCancelInProgress(null);
 +    responseMsg.send(servConn);
 +    origMsg.clearParts();
 +  }
 +  
 +  protected static void writeResponseWithRefreshMetadata(Object data,
 +      Object callbackArg, Message origMsg, boolean isObject,
 +      ServerConnection servConn, PartitionedRegion pr, byte nwHop) throws IOException {
 +    Message responseMsg = servConn.getResponseMessage();
 +    responseMsg.setMessageType(MessageType.RESPONSE);
 +    responseMsg.setTransactionId(origMsg.getTransactionId());
 +
 +    if (callbackArg == null) {
 +      responseMsg.setNumberOfParts(2);
 +    }
 +    else {
 +      responseMsg.setNumberOfParts(3);
 +    }
 +
 +    if (data instanceof byte[]) {
 +      responseMsg.addRawPart((byte[])data, isObject);
 +    }
 +    else {
 +      Assert.assertTrue(isObject,
 +          "isObject should be true when value is not a byte[]");
 +      responseMsg.addObjPart(data, zipValues);
 +    }
 +    if (callbackArg != null) {
 +      responseMsg.addObjPart(callbackArg);
 +    }
 +    responseMsg.addBytesPart(new byte[]{pr.getMetadataVersion().byteValue(),nwHop});
 +    servConn.getCache().getCancelCriterion().checkCancelInProgress(null);
 +    responseMsg.send(servConn);
 +    origMsg.clearParts();
 +  }
 +
 +  protected static void writeResponseWithFunctionAttribute(byte[] data,
 +      Message origMsg, ServerConnection servConn) throws IOException {
 +    Message responseMsg = servConn.getResponseMessage();
 +    responseMsg.setMessageType(MessageType.RESPONSE);
 +    responseMsg.setTransactionId(origMsg.getTransactionId());
 +    responseMsg.setNumberOfParts(1);
 +    responseMsg.addBytesPart(data);
 +    servConn.getCache().getCancelCriterion().checkCancelInProgress(null);
 +    responseMsg.send(servConn);
 +    origMsg.clearParts();
 +  }
 +  
 +  static protected void checkForInterrupt(ServerConnection servConn, Exception e) 
 +      throws InterruptedException, InterruptedIOException {
 +    servConn.getCachedRegionHelper().checkCancelInProgress(e);
 +    if (e instanceof InterruptedException) {
 +      throw (InterruptedException)e;
 +    }
 +    if (e instanceof InterruptedIOException) {
 +      throw (InterruptedIOException)e;
 +    }
 +  }
 +
 +  protected static void writeQueryResponseChunk(Object queryResponseChunk,
 +      CollectionType collectionType, boolean lastChunk,
 +      ServerConnection servConn) throws IOException {
 +    ChunkedMessage queryResponseMsg = servConn.getQueryResponseMessage();
 +    queryResponseMsg.setNumberOfParts(2);
 +    queryResponseMsg.setLastChunk(lastChunk);
 +    queryResponseMsg.addObjPart(collectionType, zipValues);
 +    queryResponseMsg.addObjPart(queryResponseChunk, zipValues);
 +    queryResponseMsg.sendChunk(servConn);
 +  }
 +
 +  protected static void writeQueryResponseException(Message origMsg,
 +      Throwable e, boolean isSevere, ServerConnection servConn)
 +      throws IOException {
 +    ChunkedMessage queryResponseMsg = servConn.getQueryResponseMessage();
 +    ChunkedMessage chunkedResponseMsg = servConn.getChunkedResponseMessage();
 +    if (queryResponseMsg.headerHasBeenSent()) {
 +      // fix for bug 35442
 +      // This client is expecting 2 parts in this message so send 2 parts
 +      queryResponseMsg.setServerConnection(servConn);
 +      queryResponseMsg.setNumberOfParts(2);
 +      queryResponseMsg.setLastChunkAndNumParts(true, 2);
 +      queryResponseMsg.addObjPart(e);
 +      queryResponseMsg.addStringPart(getExceptionTrace(e));
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}: Sending exception chunk while reply in progress: {}", servConn.getName(), e.getMessage(), e);
 +      }
 +      queryResponseMsg.sendChunk(servConn);
 +    }
 +    else {
 +      chunkedResponseMsg.setServerConnection(servConn);
 +      chunkedResponseMsg.setMessageType(MessageType.EXCEPTION);
 +      chunkedResponseMsg.setNumberOfParts(2);
 +      chunkedResponseMsg.setLastChunkAndNumParts(true, 2);
 +      chunkedResponseMsg.setTransactionId(origMsg.getTransactionId());
 +      chunkedResponseMsg.sendHeader();
 +      chunkedResponseMsg.addObjPart(e);
 +      chunkedResponseMsg.addStringPart(getExceptionTrace(e));
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}: Sending exception chunk: {}", servConn.getName(), e.getMessage(), e);
 +      }
 +      chunkedResponseMsg.sendChunk(servConn);
 +    }
 +  }
 +
 +  protected static void writeChunkedErrorResponse(Message origMsg,
 +      int messageType, String message, ServerConnection servConn)
 +      throws IOException {
 +    // Send chunked response header identifying error message
 +    ChunkedMessage chunkedResponseMsg = servConn.getChunkedResponseMessage();
 +    if (logger.isDebugEnabled()) {
 +      logger.debug(servConn.getName() + ": Sending error message header type: "
 +          + messageType + " transaction: " + origMsg.getTransactionId());
 +    }
 +    chunkedResponseMsg.setMessageType(messageType);
 +    chunkedResponseMsg.setTransactionId(origMsg.getTransactionId());
 +    chunkedResponseMsg.sendHeader();
 +
 +    // Send actual error
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("{}: Sending error message chunk: {}", servConn.getName(), message);
 +    }
 +    chunkedResponseMsg.setNumberOfParts(1);
 +    chunkedResponseMsg.setLastChunk(true);
 +    chunkedResponseMsg.addStringPart(message);
 +    chunkedResponseMsg.sendChunk(servConn);
 +  }
 +  
 +  protected static void writeFunctionResponseException(Message origMsg,
 +      int messageType, String message, ServerConnection servConn, Throwable e)
 +      throws IOException {
 +    ChunkedMessage functionResponseMsg = servConn.getFunctionResponseMessage();
 +    ChunkedMessage chunkedResponseMsg = servConn.getChunkedResponseMessage();
 +    if (functionResponseMsg.headerHasBeenSent()) {
 +      functionResponseMsg.setServerConnection(servConn);
 +      functionResponseMsg.setNumberOfParts(2);
 +      functionResponseMsg.setLastChunkAndNumParts(true,2);
 +      functionResponseMsg.addObjPart(e);
 +      functionResponseMsg.addStringPart(getExceptionTrace(e));
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}: Sending exception chunk while reply in progress: {}", servConn.getName(), e.getMessage(), e);
 +      }
 +      functionResponseMsg.sendChunk(servConn);
 +    }
 +    else {
 +      chunkedResponseMsg.setServerConnection(servConn);
 +      chunkedResponseMsg.setMessageType(messageType);
 +      chunkedResponseMsg.setNumberOfParts(2);
 +      chunkedResponseMsg.setLastChunkAndNumParts(true,2);
 +      chunkedResponseMsg.setTransactionId(origMsg.getTransactionId());
 +      chunkedResponseMsg.sendHeader();
 +      chunkedResponseMsg.addObjPart(e);
 +      chunkedResponseMsg.addStringPart(getExceptionTrace(e));
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}: Sending exception chunk: {}", servConn.getName(), e.getMessage(), e);
 +      }
 +      chunkedResponseMsg.sendChunk(servConn);
 +    }
 +  }
 +  
 +  protected static void writeFunctionResponseError(Message origMsg,
 +      int messageType, String message, ServerConnection servConn)
 +      throws IOException {
 +    ChunkedMessage functionResponseMsg = servConn.getFunctionResponseMessage();
 +    ChunkedMessage chunkedResponseMsg = servConn.getChunkedResponseMessage();
 +    if (functionResponseMsg.headerHasBeenSent()) {
 +      functionResponseMsg.setNumberOfParts(1);
 +      functionResponseMsg.setLastChunk(true);
 +      functionResponseMsg.addStringPart(message);
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}: Sending Error chunk while reply in progress: {}", servConn.getName(), message);
 +      }
 +      functionResponseMsg.sendChunk(servConn);
 +    }
 +    else {
 +      chunkedResponseMsg.setMessageType(messageType);
 +      chunkedResponseMsg.setNumberOfParts(1);
 +      chunkedResponseMsg.setLastChunk(true);
 +      chunkedResponseMsg.setTransactionId(origMsg.getTransactionId());
 +      chunkedResponseMsg.sendHeader();
 +      chunkedResponseMsg.addStringPart(message);
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}: Sending Error chunk: {}", servConn.getName(), message);
 +      }
 +      chunkedResponseMsg.sendChunk(servConn);
 +    }
 +  }
 +
 +  protected static void writeKeySetErrorResponse(Message origMsg,
 +      int messageType, String message, ServerConnection servConn)
 +      throws IOException {
 +    // Send chunked response header identifying error message
 +    ChunkedMessage chunkedResponseMsg = servConn.getKeySetResponseMessage();
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("{}: Sending error message header type: {} transaction: {}",
 +          servConn.getName(), messageType, origMsg.getTransactionId());
 +    }
 +    chunkedResponseMsg.setMessageType(messageType);
 +    chunkedResponseMsg.setTransactionId(origMsg.getTransactionId());
 +    chunkedResponseMsg.sendHeader();
 +    // Send actual error
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("{}: Sending error message chunk: {}", servConn.getName(), message);
 +    }
 +    chunkedResponseMsg.setNumberOfParts(1);
 +    chunkedResponseMsg.setLastChunk(true);
 +    chunkedResponseMsg.addStringPart(message);
 +    chunkedResponseMsg.sendChunk(servConn);
 +  }
 +  
 +  static Message readRequest(ServerConnection servConn) {
 +    Message requestMsg = null;
 +    try {
 +      requestMsg = servConn.getRequestMessage();
 +      requestMsg.recv(servConn, MAX_INCOMING_DATA, incomingDataLimiter,
-           MAX_INCOMING_MSGS, incomingMsgLimiter);
++          incomingMsgLimiter);
 +      return requestMsg;
 +    }
 +    catch (EOFException eof) {
 +      handleEOFException(null, servConn, eof);
 +      // TODO:Asif: Check if there is any need for explicitly returning
 +
 +    }
 +    catch (InterruptedIOException e) { // Solaris only
 +      handleInterruptedIOException(null, servConn, e);
 +
 +    }
 +    catch (IOException e) {
 +      handleIOException(null, servConn, e);
 +
 +    }
 +    catch (DistributedSystemDisconnectedException e) {
 +      handleShutdownException(null, servConn, e);
 +
 +    }
 +    catch (VirtualMachineError err) {
 +      SystemFailure.initiateFailure(err);
 +      // If this ever returns, rethrow the error.  We're poisoned
 +      // now, so don't let this thread continue.
 +      throw err;
 +    }
 +    catch (Throwable e) {
 +      SystemFailure.checkFailure();
 +      handleThrowable(null, servConn, e);
 +    }
 +    return requestMsg;
 +  }
 +
 +  protected static void fillAndSendRegisterInterestResponseChunks(
 +      LocalRegion region, Object riKey, int interestType,
 +      InterestResultPolicy policy, ServerConnection servConn)
 +      throws IOException {
 +    fillAndSendRegisterInterestResponseChunks(region, riKey, interestType,
 +        false, policy, servConn);
 +  }
 +
 +  /*
 +   * serializeValues is unused for clients < GFE_80
 +   */
 +  protected static void fillAndSendRegisterInterestResponseChunks(
 +      LocalRegion region, Object riKey, int interestType, boolean serializeValues,
 +      InterestResultPolicy policy, ServerConnection servConn)
 +      throws IOException {
 +    // Client is not interested.
 +    if (policy.isNone()) {
 +      sendRegisterInterestResponseChunk(region, riKey, new ArrayList(), true,
 +          servConn);
 +      return;
 +    }
 +    if (policy.isKeysValues()
 +        && servConn.getClientVersion().compareTo(Version.GFE_80) >= 0) {
 +        handleKeysValuesPolicy(region, riKey, interestType, serializeValues, servConn);
 +        return;
 +    }
 +    if (riKey instanceof List) {
 +      handleList(region, (List)riKey, policy, servConn);
 +      return;
 +    }
 +    if (!(riKey instanceof String)) {
 +      handleSingleton(region, riKey, policy, servConn);
 +      return;
 +    }
 +
 +    switch (interestType) {
 +    case InterestType.OQL_QUERY:
 +      // Not supported yet
 +      throw new InternalGemFireError(LocalizedStrings.BaseCommand_NOT_YET_SUPPORTED.toLocalizedString());
 +    case InterestType.FILTER_CLASS:
 +      throw new InternalGemFireError(LocalizedStrings.BaseCommand_NOT_YET_SUPPORTED.toLocalizedString());
 +      // handleFilter(region, (String)riKey, policy);
 +      // break;
 +    case InterestType.REGULAR_EXPRESSION: {
 +      String regEx = (String)riKey;
 +      if (regEx.equals(".*")) {
 +        handleAllKeys(region, policy, servConn);
 +      }
 +      else {
 +        handleRegEx(region, regEx, policy, servConn);
 +      }
 +    }
 +      break;
 +    case InterestType.KEY:
 +      if (riKey.equals("ALL_KEYS")) {
 +        handleAllKeys(region, policy, servConn);
 +      }
 +      else {
 +        handleSingleton(region, riKey, policy, servConn);
 +      }
 +      break;
 +    default:
 +      throw new InternalGemFireError(LocalizedStrings.BaseCommand_UNKNOWN_INTEREST_TYPE.toLocalizedString());
 +    }
 +  }
 +
 +  @SuppressWarnings("rawtypes")
 +  private static void handleKeysValuesPolicy(LocalRegion region, Object riKey,
 +      int interestType, boolean serializeValues, ServerConnection servConn)
 +      throws IOException {
 +    if (riKey instanceof List) {
 +      handleKVList(region, (List)riKey, serializeValues, servConn);
 +      return;
 +    }
 +    if (!(riKey instanceof String)) {
 +      handleKVSingleton(region, riKey, serializeValues, servConn);
 +      return;
 +    }
 +
 +    switch (interestType) {
 +    case InterestType.OQL_QUERY:
 +      throw new InternalGemFireError(LocalizedStrings.BaseCommand_NOT_YET_SUPPORTED.toLocalizedString());
 +    case InterestType.FILTER_CLASS:
 +      throw new InternalGemFireError(LocalizedStrings.BaseCommand_NOT_YET_SUPPORTED.toLocalizedString());
 +    case InterestType.REGULAR_EXPRESSION:
 +      String regEx = (String)riKey;
 +      if (regEx.equals(".*")) {
 +        handleKVAllKeys(region, null, serializeValues, servConn);
 +      } else {
 +        handleKVAllKeys(region, regEx, serializeValues, servConn);
 +      }
 +      break;
 +    case InterestType.KEY:
 +      if (riKey.equals("ALL_KEYS")) {
 +        handleKVAllKeys(region, null, serializeValues, servConn);
 +      } else {
 +        handleKVSingleton(region, riKey, serializeValues, servConn);
 +      }
 +      break;
 +    default:
 +      throw new InternalGemFireError(LocalizedStrings.BaseCommand_UNKNOWN_INTEREST_TYPE.toLocalizedString());
 +    }
 +  }
 +
 +  /**
 +   * @param list
 +   *                is a List of entry keys
 +   */
 +  protected static void sendRegisterInterestResponseChunk(Region region,
 +      Object riKey, ArrayList list, boolean lastChunk, ServerConnection servConn)
 +      throws IOException {
 +    ChunkedMessage chunkedResponseMsg = servConn.getRegisterInterestResponseMessage();
 +    chunkedResponseMsg.setNumberOfParts(1);
 +    chunkedResponseMsg.setLastChunk(lastChunk);
 +    chunkedResponseMsg.addObjPart(list, zipValues);
 +    String regionName = (region == null) ? " null " : region.getFullPath();
 +    if (logger.isDebugEnabled()) {
 +      String str = servConn.getName() + ": Sending"
 +          + (lastChunk ? " last " : " ")
 +          + "register interest response chunk for region: " + regionName
 +          + " for keys: " + riKey + " chunk=<" + chunkedResponseMsg + ">";
 +      logger.debug(str);
 +    }
 +
 +    chunkedResponseMsg.sendChunk(servConn);
 +  }
 +  
 +  /**
 +   * Determines whether keys for destroyed entries (tombstones) should be sent
 +   * to clients in register-interest results.
 +   * 
 +   * @param servConn
 +   * @param policy
 +   * @return true if tombstones should be sent to the client
 +   */
 +  private static boolean sendTombstonesInRIResults(ServerConnection servConn, InterestResultPolicy policy) {
 +    return (policy == InterestResultPolicy.KEYS_VALUES)
 +         && (servConn.getClientVersion().compareTo(Version.GFE_80) >= 0);
 +  }
 +
 +  /**
 +   * Process an interest request involving a list of keys
 +   *
 +   * @param region
 +   *                the region
 +   * @param keyList
 +   *                the list of keys
 +   * @param policy
 +   *                the policy
 +   * @throws IOException
 +   */
 +  private static void handleList(LocalRegion region, List keyList,
 +      InterestResultPolicy policy, ServerConnection servConn)
 +      throws IOException {
 +    if (region instanceof PartitionedRegion) {
 +      // too bad java doesn't provide another way to do this...
 +      handleListPR((PartitionedRegion)region, keyList, policy, servConn);
 +      return;
 +    }
 +    ArrayList newKeyList = new ArrayList(maximumChunkSize);
 +    // Handle list of keys
 +    if (region != null) {
 +      for (Iterator it = keyList.iterator(); it.hasNext();) {
 +        Object entryKey = it.next();
 +        if (region.containsKey(entryKey)
 +            || (sendTombstonesInRIResults(servConn, policy) && region.containsTombstone(entryKey))) {
 +          
 +          appendInterestResponseKey(region, keyList, entryKey, newKeyList,
 +              "list", servConn);
 +        }
 +      }
 +    }
 +    // Send the last chunk (the only chunk for individual and list keys)
 +    // always send it back, even if the list is of zero size.
 +    sendRegisterInterestResponseChunk(region, keyList, newKeyList, true,
 +        servConn);
 +  }
 +
 +  /**
 +   * Handles both RR and PR cases
 +   */
 +  @SuppressWarnings("rawtypes")
 +  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="NP_NULL_PARAM_DEREF", justification="Null value handled in sendNewRegisterInterestResponseChunk()")
 +  private static void handleKVSingleton(LocalRegion region, Object entryKey,
 +      boolean serializeValues, ServerConnection servConn)
 +      throws IOException {
 +    VersionedObjectList values = new VersionedObjectList(maximumChunkSize,
 +        true, region == null ? true : region.getAttributes()
 +            .getConcurrencyChecksEnabled(), serializeValues);
 +
 +    if (region != null) {
 +      if (region.containsKey(entryKey) || region.containsTombstone(entryKey)) {
 +        EntryEventImpl versionHolder = EntryEventImpl.createVersionTagHolder();
 +        ClientProxyMembershipID id = servConn == null ? null : servConn.getProxyID();
 +        // From Get70.getValueAndIsObject()
 +        Object data = region.get(entryKey, null, true, true, true, id, versionHolder, true, false);
 +        VersionTag vt = versionHolder.getVersionTag();
 +
 +        updateValues(values, entryKey, data, vt);
 +      }
 +    }
 +    // Send the last chunk (the only chunk for individual and list keys)
 +    // always send it back, even if the list is of zero size.
 +    sendNewRegisterInterestResponseChunk(region, entryKey, values, true, servConn);
 +  }
 +
 +  /**
 +   * Process an interest request consisting of a single key
 +   *
 +   * @param region
 +   *                the region
 +   * @param entryKey
 +   *                the key
 +   * @param policy
 +   *                the policy
 +   * @throws IOException
 +   */
 +  private static void handleSingleton(LocalRegion region, Object entryKey,
 +      InterestResultPolicy policy, ServerConnection servConn)
 +      throws IOException {
 +    ArrayList keyList = new ArrayList(1);
 +    if (region != null) {
 +      if (region.containsKey(entryKey) ||
 +          (sendTombstonesInRIResults(servConn, policy) && region.containsTombstone(entryKey))) {
 +        appendInterestResponseKey(region, entryKey, entryKey, keyList,
 +            "individual", servConn);
 +      }
 +    }
 +    // Send the last chunk (the only chunk for individual and list keys)
 +    // always send it back, even if the list is of zero size.
 +    sendRegisterInterestResponseChunk(region, entryKey, keyList, true, servConn);
 +  }
 +
 +  /**
 +   * Process an interest request of type ALL_KEYS
 +   *
 +   * @param region
 +   *                the region
 +   * @param policy
 +   *                the policy
 +   * @throws IOException
 +   */
 +  private static void handleAllKeys(LocalRegion region,
 +      InterestResultPolicy policy, ServerConnection servConn)
 +      throws IOException {
 +    ArrayList keyList = new ArrayList(maximumChunkSize);
 +    if (region != null) {
 +      for (Iterator it = region.keySet(sendTombstonesInRIResults(servConn, policy)).iterator(); it.hasNext();) {
 +        appendInterestResponseKey(region, "ALL_KEYS", it.next(), keyList,
 +            "ALL_KEYS", servConn);
 +      }
 +    }
 +    // Send the last chunk (the only chunk for individual and list keys)
 +    // always send it back, even if the list is of zero size.
 +    sendRegisterInterestResponseChunk(region, "ALL_KEYS", keyList, true,
 +        servConn);
 +  }
 +
 +  /**
 +   * @param region
 +   * @param regex
 +   * @param serializeValues
 +   * @param servConn
 +   * @throws IOException
 +   */
 +  private static void handleKVAllKeys(LocalRegion region, String regex,
 +      boolean serializeValues, ServerConnection servConn) throws IOException {
 +
 +    if (region != null && region instanceof PartitionedRegion) {
 +      handleKVKeysPR((PartitionedRegion) region, regex, serializeValues, servConn);
 +      return;
 +    }
 +
 +    VersionedObjectList values = new VersionedObjectList(maximumChunkSize,
 +        true, region == null ? true : region.getAttributes()
 +            .getConcurrencyChecksEnabled(), serializeValues);
 +
 +    if (region != null) {
 +
 +      VersionTag versionTag = null;
 +      Object data = null;
 +
 +      Pattern keyPattern = null;
 +      if (regex != null) {
 +        keyPattern = Pattern.compile(regex);
 +      }
 +
 +      for (Object key : region.keySet(true)) {
 +        EntryEventImpl versionHolder = EntryEventImpl.createVersionTagHolder();
 +        if (keyPattern != null) {
 +          if (!(key instanceof String)) {
 +            // key is not a String, cannot apply regex to this entry
 +            continue;
 +          }
 +          if (!keyPattern.matcher((String) key).matches()) {
 +            // key does not match the regex, this entry should not be
 +            // returned.
 +            continue;
 +          }
 +        }
 +
 +        ClientProxyMembershipID id = servConn == null ? null : servConn.getProxyID();
 +        data = region.get(key, null, true, true, true, id, versionHolder, true, false);
 +        versionTag = versionHolder.getVersionTag();
 +        updateValues(values, key, data, versionTag);
 +
 +        if (values.size() == maximumChunkSize) {
 +          sendNewRegisterInterestResponseChunk(region, regex != null ? regex : "ALL_KEYS", values, false, servConn);
 +          values.clear();
 +        }
 +      } // for
 +    } // if
 +
 +    // Send the last chunk (the only chunk for individual and list keys)
 +    // always send it back, even if the list is of zero size.
 +    sendNewRegisterInterestResponseChunk(region, regex != null ? regex : "ALL_KEYS", values, true, servConn);
 +  }
 +
 +  private static void handleKVKeysPR(PartitionedRegion region, Object keyInfo,
 +      boolean serializeValues, ServerConnection servConn) throws IOException {
 +    int id = 0;
 +    HashMap<Integer, HashSet> bucketKeys = null;
 +
 +    VersionedObjectList values = new VersionedObjectList(maximumChunkSize,
 +        true, region.getConcurrencyChecksEnabled(), serializeValues);
 +
 +    if (keyInfo != null && keyInfo instanceof List) {
 +      bucketKeys = new HashMap<Integer, HashSet>();
 +      for (Object key : (List) keyInfo) {
 +        id = PartitionedRegionHelper.getHashKey(region, null, key, null, null);
 +        if (bucketKeys.containsKey(id)) {
 +          bucketKeys.get(id).add(key);
 +        } else {
 +          HashSet<Object> keys = new HashSet<Object>();
 +          keys.add(key);
 +          bucketKeys.put(id, keys);
 +        }
 +      }
 +      region.fetchEntries(bucketKeys, values, servConn);
 +    } else { // keyInfo is a String
 +      region.fetchEntries((String)keyInfo, values, servConn);
 +    }
 +
 +    // Send the last chunk (the only chunk for individual and list keys)
 +    // always send it back, even if the list is of zero size.
 +    sendNewRegisterInterestResponseChunk(region, keyInfo != null ? keyInfo : "ALL_KEYS", values, true, servConn);
 +  }
 +
 +  /**
 +   * Copied from Get70.getValueAndIsObject(), except a minor change. (Make the
 +   * method static instead of copying it here?)
 +   * 
 +   * @param value
 +   */
 +  private static void updateValues(VersionedObjectList values, Object key, Object value, VersionTag versionTag) {
 +    boolean isObject = true;
 +
 +    // If the value in the VM is a CachedDeserializable,
 +    // get its value. If it is Token.REMOVED, Token.DESTROYED,
 +    // Token.INVALID, or Token.LOCAL_INVALID
 +    // set it to null. If it is NOT_AVAILABLE, get the value from
 +    // disk. If it is already a byte[], set isObject to false.
 +    boolean wasInvalid = false;
 +    if (value instanceof CachedDeserializable) {
 +      value = ((CachedDeserializable)value).getValue();
 +    }
 +    else if (value == Token.REMOVED_PHASE1 || value == Token.REMOVED_PHASE2 || value == Token.DESTROYED || value == Token.TOMBSTONE) {
 +      value = null;
 +    }
 +    else if (value == Token.INVALID || value == Token.LOCAL_INVALID) {
 +      value = null; // fix for bug 35884
 +      wasInvalid = true;
 +    }
 +    else if (value instanceof byte[]) {
 +      isObject = false;
 +    }
 +    boolean keyNotPresent = !wasInvalid && (value == null || value == Token.TOMBSTONE);
 +
 +    if (keyNotPresent) {
 +      values.addObjectPartForAbsentKey(key, value, versionTag);
 +    } else {
 +      values.addObjectPart(key, value, isObject, versionTag);
 +    }
 +  }
 +
 +  public static void appendNewRegisterInterestResponseChunkFromLocal(LocalRegion region,
 +      VersionedObjectList values, Object riKeys, Set keySet, ServerConnection servConn)
 +      throws IOException {
 +    Object key = null;
 +    EntryEventImpl versionHolder = null;
 +    ClientProxyMembershipID requestingClient = servConn == null ? null : servConn.getProxyID();
 +    for (Iterator it = keySet.iterator(); it.hasNext();) {
 +      key = it.next();
 +      versionHolder = EntryEventImpl.createVersionTagHolder();
 +
 +      Object value = region.get(key, null, true, true, true, requestingClient, versionHolder, true, false);
 +      
 +      updateValues(values, key, value, versionHolder.getVersionTag());
 +
 +      if (values.size() == maximumChunkSize) {
 +        // Send the chunk and clear the list
 +        // values.setKeys(null); // Now we need to send keys too.
 +        sendNewRegisterInterestResponseChunk(region, riKeys != null ? riKeys : "ALL_KEYS", values, false, servConn);
 +        values.clear();
 +      }
 +    } // for
 +  }
 +
 +  /**
 +   * 
 +   * @param region
 +   * @param values {@link VersionedObjectList}
 +   * @param riKeys
 +   * @param set set of entries
 +   * @param servConn
 +   * @throws IOException
 +   */
 +  public static void appendNewRegisterInterestResponseChunk(LocalRegion region,
 +      VersionedObjectList values, Object riKeys, Set set, ServerConnection servConn)
 +      throws IOException {
 +    for (Iterator<Map.Entry> it = set.iterator(); it.hasNext();) {
 +      Map.Entry entry = it.next(); // Region.Entry or Map.Entry
 +      if (entry instanceof Region.Entry) { // local entries
 +        VersionTag vt = null;
 +        Object key = null;
 +        Object value = null;
 +        if (entry instanceof EntrySnapshot) {
 +          vt = ((EntrySnapshot) entry).getVersionTag();
 +          key = ((EntrySnapshot) entry).getRegionEntry().getKey();
 +          value = ((EntrySnapshot) entry).getRegionEntry().getValue(null);
 +          updateValues(values, key, value, vt);
 +        } else {
 +          VersionStamp vs = ((NonTXEntry)entry).getRegionEntry().getVersionStamp();
 +          vt = vs == null ? null : vs.asVersionTag();
 +          key = entry.getKey();
 +          value = ((NonTXEntry)entry).getRegionEntry()._getValueRetain(region, true);
 +          try {
 +            updateValues(values, key, value, vt);
 +          } finally {
 +            // TODO OFFHEAP: in the future we might want to delay this release
 +            // until the "values" VersionedObjectList is released.
 +            // But for now "updateValues" copies the off-heap value to the heap.
 +            OffHeapHelper.release(value);
 +          }
 +        }
 +      } else { // Map.Entry (remote entries)
 +        ArrayList list = (ArrayList)entry.getValue();
 +        Object value = list.get(0);
 +        VersionTag tag = (VersionTag)list.get(1);
 +        updateValues(values, entry.getKey(), value, tag);
 +      }
 +      if (values.size() == maximumChunkSize) {
 +        // Send the chunk and clear the list
 +        // values.setKeys(null); // Now we need to send keys too.
 +        sendNewRegisterInterestResponseChunk(region, riKeys != null ? riKeys : "ALL_KEYS", values, false, servConn);
 +        values.clear();
 +      }
 +    } // for
 +  }
 +
 +  public static void sendNewRegisterInterestResponseChunk(LocalRegion region,
 +      Object riKey, VersionedObjectList list, boolean lastChunk, ServerConnection servConn)
 +      throws IOException {
 +    ChunkedMessage chunkedResponseMsg = servConn.getRegisterInterestResponseMessage();
 +    chunkedResponseMsg.setNumberOfParts(1);
 +    chunkedResponseMsg.setLastChunk(lastChunk);
 +    chunkedResponseMsg.addObjPart(list, zipValues);
 +    String regionName = (region == null) ? " null " : region.getFullPath();
 +    if (logger.isDebugEnabled()) {
 +      String str = servConn.getName() + ": Sending"
 +          + (lastChunk ? " last " : " ")
 +          + "register interest response chunk for region: " + regionName
 +          + " for keys: " + riKey + " chunk=<" + chunkedResponseMsg + ">";
 +      logger.debug(str);
 +    }
 +
 +    chunkedResponseMsg.sendChunk(servConn);
 +  }
 +
 +  /**
 +   * Process an interest request of type {@link InterestType#REGULAR_EXPRESSION}
 +   *
 +   * @param region
 +   *                the region
 +   * @param regex
 +   *                the regex
 +   * @param policy
 +   *                the policy
 +   * @throws IOException
 +   */
 +  private static void handleRegEx(LocalRegion region, String regex,
 +      InterestResultPolicy policy, ServerConnection servConn)
 +      throws IOException {
 +    if (region instanceof PartitionedRegion) {
 +      // too bad java doesn't provide another way to do this...
 +      handleRegExPR((PartitionedRegion)region, regex, policy, servConn);
 +      return;
 +    }
 +    ArrayList keyList = new ArrayList(maximumChunkSize);
 +    // Handle the regex pattern
 +    Pattern keyPattern = Pattern.compile(regex);
 +    if (region != null) {
 +      for (Iterator it = region.keySet(sendTombstonesInRIResults(servConn, policy)).iterator(); it.hasNext();) {
 +        Object entryKey = it.next();
 +        if (!(entryKey instanceof String)) {
 +          // key is not a String, cannot apply regex to this entry
 +          continue;
 +        }
 +        if (!keyPattern.matcher((String)entryKey).matches()) {
 +          // key does not match the regex, this entry should not be returned.
 +          continue;
 +        }
 +
 +        appendInterestResponseKey(region, regex, entryKey, keyList, "regex",
 +            servConn);
 +      }
 +    }
 +    // Send the last chunk (the only chunk for individual and list keys)
 +    // always send it back, even if the list is of zero size.
 +    sendRegisterInterestResponseChunk(region, regex, keyList, true, servConn);
 +  }
 +
 +  /**
 +   * Process an interest request of type {@link InterestType#REGULAR_EXPRESSION}
 +   *
 +   * @param region
 +   *                the region
 +   * @param regex
 +   *                the regex
 +   * @param policy
 +   *                the policy
 +   * @throws IOException
 +   */
 +  private static void handleRegExPR(final PartitionedRegion region,
 +      final String regex, final InterestResultPolicy policy,
 +      final ServerConnection servConn) throws IOException {
 +    final ArrayList keyList = new ArrayList(maximumChunkSize);
 +    region.getKeysWithRegEx(regex, sendTombstonesInRIResults(servConn, policy), new PartitionedRegion.SetCollector() {
 +      public void receiveSet(Set theSet) throws IOException {
 +        appendInterestResponseKeys(region, regex, theSet, keyList, "regex",
 +            servConn);
 +      }
 +    });
 +    // Send the last chunk (the only chunk for individual and list keys)
 +    // always send it back, even if the list is of zero size.
 +    sendRegisterInterestResponseChunk(region, regex, keyList, true, servConn);
 +  }
 +
 +  /**
 +   * Process an interest request involving a list of keys
 +   *
 +   * @param region
 +   *                the region
 +   * @param keyList
 +   *                the list of keys
 +   * @param policy
 +   *                the policy
 +   * @throws IOException
 +   */
 +  private static void handleListPR(final PartitionedRegion region,
 +      final List keyList, final InterestResultPolicy policy,
 +      final ServerConnection servConn) throws IOException {
 +    final ArrayList newKeyList = new ArrayList(maximumChunkSize);
 +    region.getKeysWithList(keyList, sendTombstonesInRIResults(servConn, policy), new PartitionedRegion.SetCollector() {
 +      public void receiveSet(Set theSet) throws IOException {
 +        appendInterestResponseKeys(region, keyList, theSet, newKeyList, "list",
 +            servConn);
 +      }
 +    });
 +    // Send the last chunk (the only chunk for individual and list keys)
 +    // always send it back, even if the list is of zero size.
 +    sendRegisterInterestResponseChunk(region, keyList, newKeyList, true,
 +        servConn);
 +  }
 +
 +  @SuppressWarnings("rawtypes")
 +  private static void handleKVList(final LocalRegion region,
 +      final List keyList, boolean serializeValues,
 +      final ServerConnection servConn) throws IOException {
 +
 +    if (region != null && region instanceof PartitionedRegion) {
 +      handleKVKeysPR((PartitionedRegion)region, keyList, serializeValues, servConn);
 +      return;
 +    }
 +    VersionedObjectList values = new VersionedObjectList(maximumChunkSize,
 +        true, region == null ? true : region.getAttributes()
 +            .getConcurrencyChecksEnabled(), serializeValues);
 +
 +    // Handle list of keys
 +    if (region != null) {
 +      VersionTag versionTag = null;
 +      Object data = null;
 +
 +      for (Iterator it = keyList.iterator(); it.hasNext();) {
 +        Object key = it.next();
 +        if (region.containsKey(key) || region.containsTombstone(key)) {
 +          EntryEventImpl versionHolder = EntryEventImpl
 +              .createVersionTagHolder();
 +
 +          ClientProxyMembershipID id = servConn == null ? null : servConn
 +              .getProxyID();
 +          data = region.get(key, null, true, true, true, id, versionHolder,
 +              true, false);
 +          versionTag = versionHolder.getVersionTag();
 +          updateValues(values, key, data, versionTag);
 +
 +          if (values.size() == maximumChunkSize) {
 +            // Send the chunk and clear the list
 +            // values.setKeys(null); // Now we need to send keys too.
 +            sendNewRegisterInterestResponseChunk(region, keyList, values, false, servConn);
 +            values.clear();
 +          }
 +        }
 +      }
 +    }
 +    // Send the last chunk (the only chunk for individual and list keys)
 +    // always send it back, even if the list is of zero size.
 +    sendNewRegisterInterestResponseChunk(region, keyList, values, true, servConn);
 +  }
 +
 +  /**
 +   * Append an interest response
 +   *
 +   * @param region
 +   *                the region (for debugging)
 +   * @param riKey
 +   *                the registerInterest "key" (what the client is interested
 +   *                in)
 +   * @param entryKey
 +   *                key we're responding to
 +   * @param list
 +   *                list to append to
 +   * @param kind
 +   *                for debugging
 +   */
 +  private static void appendInterestResponseKey(LocalRegion region,
 +      Object riKey, Object entryKey, ArrayList list, String kind,
 +      ServerConnection servConn) throws IOException {
 +    list.add(entryKey);
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("{}: appendInterestResponseKey <{}>; list size was {}; region: {}",
 +          servConn.getName(), entryKey, list.size(), region.getFullPath());
 +    }
 +    if (list.size() == maximumChunkSize) {
 +      // Send the chunk and clear the list
 +      sendRegisterInterestResponseChunk(region, riKey, list, false, servConn);
 +      list.clear();
 +    }
 +  }
 +
 +  protected static void appendInterestResponseKeys(LocalRegion region,
 +      Object riKey, Collection entryKeys, ArrayList collector, String riDescr,
 +      ServerConnection servConn) throws IOException {
 +    for (Iterator it = entryKeys.iterator(); it.hasNext();) {
 +      appendInterestResponseKey(region, riKey, it.next(), collector, riDescr,
 +          servConn);
 +    }
 +  }
 +}


[37/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/gms/mgr/GMSMembershipManager.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/gms/mgr/GMSMembershipManager.java
index d75b28d,0000000..edfee10
mode 100755,000000..100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/gms/mgr/GMSMembershipManager.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/gms/mgr/GMSMembershipManager.java
@@@ -1,2652 -1,0 +1,2655 @@@
 +/*
 + * 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.distributed.internal.membership.gms.mgr;
 +
 +import java.io.IOException;
 +import java.io.NotSerializableException;
 +import java.util.ArrayList;
 +import java.util.Arrays;
 +import java.util.Collection;
 +import java.util.Collections;
 +import java.util.HashMap;
 +import java.util.HashSet;
 +import java.util.Iterator;
 +import java.util.LinkedHashMap;
 +import java.util.LinkedList;
 +import java.util.List;
 +import java.util.Map;
 +import java.util.Set;
 +import java.util.concurrent.ConcurrentHashMap;
 +import java.util.concurrent.CountDownLatch;
 +import java.util.concurrent.TimeUnit;
 +import java.util.concurrent.TimeoutException;
 +import java.util.concurrent.locks.ReadWriteLock;
 +import java.util.concurrent.locks.ReentrantReadWriteLock;
 +
 +import org.apache.logging.log4j.Logger;
 +
 +import com.gemstone.gemfire.CancelException;
 +import com.gemstone.gemfire.ForcedDisconnectException;
 +import com.gemstone.gemfire.GemFireConfigException;
 +import com.gemstone.gemfire.InternalGemFireError;
 +import com.gemstone.gemfire.SystemConnectException;
 +import com.gemstone.gemfire.SystemFailure;
 +import com.gemstone.gemfire.ToDataException;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.DistributedSystem;
 +import com.gemstone.gemfire.distributed.DistributedSystemDisconnectedException;
 +import com.gemstone.gemfire.distributed.Locator;
 +import com.gemstone.gemfire.distributed.internal.AdminMessageType;
 +import com.gemstone.gemfire.distributed.internal.DMStats;
 +import com.gemstone.gemfire.distributed.internal.DSClock;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 +import com.gemstone.gemfire.distributed.internal.DistributionException;
 +import com.gemstone.gemfire.distributed.internal.DistributionManager;
 +import com.gemstone.gemfire.distributed.internal.DistributionMessage;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.distributed.internal.InternalLocator;
 +import com.gemstone.gemfire.distributed.internal.OverflowQueueWithDMStats;
 +import com.gemstone.gemfire.distributed.internal.SizeableRunnable;
 +import com.gemstone.gemfire.distributed.internal.StartupMessage;
 +import com.gemstone.gemfire.distributed.internal.direct.DirectChannel;
 +import com.gemstone.gemfire.distributed.internal.direct.DirectChannelListener;
 +import com.gemstone.gemfire.distributed.internal.direct.ShunnedMemberException;
 +import com.gemstone.gemfire.distributed.internal.membership.DistributedMembershipListener;
 +import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
 +import com.gemstone.gemfire.distributed.internal.membership.MembershipManager;
 +import com.gemstone.gemfire.distributed.internal.membership.MembershipTestHook;
 +import com.gemstone.gemfire.distributed.internal.membership.NetView;
 +import com.gemstone.gemfire.distributed.internal.membership.QuorumChecker;
 +import com.gemstone.gemfire.distributed.internal.membership.gms.GMSMember;
 +import com.gemstone.gemfire.distributed.internal.membership.gms.Services;
 +import com.gemstone.gemfire.distributed.internal.membership.gms.SuspectMember;
 +import com.gemstone.gemfire.distributed.internal.membership.gms.fd.GMSHealthMonitor;
 +import com.gemstone.gemfire.distributed.internal.membership.gms.interfaces.Manager;
 +import com.gemstone.gemfire.distributed.internal.membership.gms.membership.GMSJoinLeave;
 +import com.gemstone.gemfire.distributed.internal.membership.gms.messenger.GMSQuorumChecker;
 +import com.gemstone.gemfire.internal.Assert;
 +import com.gemstone.gemfire.internal.SystemTimer;
 +import com.gemstone.gemfire.internal.Version;
 +import com.gemstone.gemfire.internal.admin.remote.RemoteTransportConfig;
++import com.gemstone.gemfire.internal.cache.CacheServerImpl;
 +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
 +import com.gemstone.gemfire.internal.cache.xmlcache.CacheServerCreation;
 +import com.gemstone.gemfire.internal.cache.xmlcache.CacheXmlGenerator;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.logging.log4j.AlertAppender;
 +import com.gemstone.gemfire.internal.logging.log4j.LocalizedMessage;
 +import com.gemstone.gemfire.internal.logging.log4j.LogMarker;
 +import com.gemstone.gemfire.internal.shared.StringPrintWriter;
 +import com.gemstone.gemfire.internal.tcp.ConnectExceptions;
 +import com.gemstone.gemfire.internal.tcp.MemberShunnedException;
 +import com.gemstone.gemfire.internal.util.Breadcrumbs;
 +
 +public class GMSMembershipManager implements MembershipManager, Manager
 +{
 +  private static final Logger logger = Services.getLogger();
 +  
 +  /** product version to use for multicast serialization */
 +  volatile boolean disableMulticastForRollingUpgrade;
 +  
 +  /**
 +   * set to true if the distributed system that created this manager was
 +   * auto-reconnecting when it was created.
 +   */
 +  boolean wasReconnectingSystem;
 +  
 +  /**
 +   * A quorum checker is created during reconnect and is held
 +   * here so it is available to the UDP protocol for passing off
 +   * the ping-pong responses used in the quorum-checking algorithm. 
 +   */
 +  private volatile QuorumChecker quorumChecker;
 +  
 +  /**
 +   * thread-local used to force use of Messenger for communications, usually to
 +   * avoid deadlock when conserve-sockets=true.  Use of this should be removed
 +   * when connection pools are implemented in the direct-channel 
 +   */
 +  private ThreadLocal<Boolean> forceUseUDPMessaging = new ThreadLocal<Boolean>();
 +  
 +  /**
 +   * Trick class to make the startup synch more
 +   * visible in stack traces
 +   * 
 +   * @see GMSMembershipManager#startupLock
 +   */
 +  static class EventProcessingLock  {
 +    public EventProcessingLock() {
 +    }
 +  }
 +  
 +  static class StartupEvent  {
 +    static final int SURPRISE_CONNECT = 1;
 +    static final int VIEW = 2;
 +    static final int MESSAGE = 3;
 +    
 +    /**
 +     * indicates whether the event is a departure, a surprise connect
 +     * (i.e., before the view message arrived), a view, or a regular
 +     * message
 +     * 
 +     * @see #SURPRISE_CONNECT
 +     * @see #VIEW
 +     * @see #MESSAGE
 +     */
 +    private int kind;
 +    
 +    // Miscellaneous state depending on the kind of event
 +    InternalDistributedMember member;
 +    boolean crashed;
 +    String reason;
 +    DistributionMessage dmsg;
 +    NetView gmsView;
 +    
 +    @Override
 +    public String toString() {
 +      StringBuffer sb = new StringBuffer();
 +      sb.append("kind=");
 +      switch (kind) {
 +      case SURPRISE_CONNECT:
 +        sb.append("connect; member = <" + member + ">");
 +        break;
 +      case VIEW:
 +        String text = gmsView.toString();
 +        sb.append("view <" + text + ">");
 +        break;
 +      case MESSAGE:
 +        sb.append("message <" + dmsg + ">");
 +        break;
 +      default:
 +        sb.append("unknown=<" + kind + ">");
 +        break;
 +      }
 +      return sb.toString();
 +    }
 +
 +    /**
 +     * Create a surprise connect event
 +     * @param member the member connecting
 +     */
 +    StartupEvent(final InternalDistributedMember member) {
 +      this.kind = SURPRISE_CONNECT;
 +      this.member = member;
 +    }
 +    /**
 +     * Indicate if this is a surprise connect event
 +     * @return true if this is a connect event
 +     */
 +    boolean isSurpriseConnect() {
 +      return this.kind == SURPRISE_CONNECT;
 +    }
 +
 +    /**
 +     * Create a view event
 +     * @param v the new view
 +     */
 +    StartupEvent(NetView v) {
 +      this.kind = VIEW;
 +      this.gmsView = v;
 +    }
 +    
 +    /**
 +     * Indicate if this is a view event
 +     * @return true if this is a view event
 +     */
 +    boolean isGmsView() {
 +      return this.kind == VIEW;
 +    }
 +
 +    /**
 +     * Create a message event
 +     * @param d the message
 +     */
 +    StartupEvent(DistributionMessage d) {
 +      this.kind = MESSAGE;
 +      this.dmsg = d;
 +    }
 +    /**
 +     * Indicate if this is a message event
 +     * @return true if this is a message event
 +     */
 +    boolean isDistributionMessage() {
 +      return this.kind == MESSAGE;
 +    }
 +  }
 +  
 +  private int membershipCheckTimeout = DistributionConfig.DEFAULT_SECURITY_PEER_VERIFYMEMBER_TIMEOUT;
 +
 +  /**
 +   * This object synchronizes threads waiting for
 +   * startup to finish.  Updates to {@link #startupMessages}
 +   * are synchronized through this object.
 +   */
 +  protected final EventProcessingLock startupLock = new EventProcessingLock();
 +  
 +  /**
 +   * This is the latest view (ordered list of DistributedMembers) 
 +   * that has been installed
 +   * 
 +   * All accesses to this object are protected via {@link #latestViewLock}
 +   */
 +  protected NetView latestView = new NetView();
 +
 +  /**
 +   * This is the lock for protecting access to latestView
 +   * 
 +   * @see #latestView
 +   */
 +  protected ReadWriteLock latestViewLock = new ReentrantReadWriteLock();
 +  
 +  /**
 +   * This is the listener that accepts our membership events
 +   */
 +  protected com.gemstone.gemfire.distributed.internal.membership.DistributedMembershipListener listener;
 +
 +  /**
 +   * Membership failure listeners - for testing
 +   */
 +  List membershipTestHooks;
 +  
 +  /**
 +   * This is a representation of the local member (ourself)
 +   */
 +  protected InternalDistributedMember address = null; // new DistributedMember(-1);
 +
 +  protected DirectChannel directChannel;
 +
 +  protected MyDCReceiver dcReceiver;
 +  
 +  volatile boolean isJoining;
 +  
 +  /** have we joined successfully? */
 +  volatile boolean hasJoined;
 +  
 +  /**
 +   * Members of the distributed system that we believe have shut down.
 +   * Keys are instances of {@link InternalDistributedMember}, values are 
 +   * Longs indicating the time this member was shunned.
 +   * 
 +   * Members are removed after {@link #SHUNNED_SUNSET} seconds have
 +   * passed.
 +   * 
 +   * Accesses to this list needs to be under the read or write lock of {@link #latestViewLock}
 +   * 
 +   * @see System#currentTimeMillis()
 +   */
 +//  protected final Set shunnedMembers = Collections.synchronizedSet(new HashSet());
 +  protected final Map shunnedMembers = new ConcurrentHashMap();
 +
 +  /**
 +   * Members that have sent a shutdown message.  This is used to suppress
 +   * suspect processing that otherwise becomes pretty aggressive 
 +   * when a member is shutting down.
 +   */
 +  private final Map shutdownMembers = new BoundedLinkedHashMap(1000);
 +  
 +  /**
 +   * per bug 39552, keep a list of members that have been shunned and for
 +   * which a message is printed.  Contents of this list are cleared at the
 +   * same time they are removed from {@link #shunnedMembers}.
 +   * 
 +   * Accesses to this list needs to be under the read or write lock of {@link #latestViewLock}
 +   */
 +  protected final HashSet shunnedAndWarnedMembers = new HashSet();
 +  /**
 +   * The identities and birth-times of others that we have allowed into
 +   * membership at the distributed system level, but have not yet appeared
 +   * in a view.
 +   * <p>
 +   * Keys are instances of {@link InternalDistributedMember}, values are 
 +   * Longs indicating the time this member was shunned.
 +   * <p>
 +   * Members are removed when a view containing them is processed.  If,
 +   * after {@link #surpriseMemberTimeout} milliseconds have passed, a view
 +   * containing the member has not arrived, the member is removed from
 +   * membership and member-left notification is performed. 
 +   * <p>>
 +   * Accesses to this list needs to be under the read or write lock of {@link #latestViewLock}
 +   * 
 +   * @see System#currentTimeMillis()
 +   */
 +  protected final Map<InternalDistributedMember, Long> surpriseMembers = new ConcurrentHashMap();
 +
 +  /**
 +   * the timeout interval for surprise members.  This is calculated from 
 +   * the member-timeout setting
 +   */
 +  protected int surpriseMemberTimeout;
 +  
 +  /**
 +   * javagroups can skip views and omit telling us about a crashed member.
 +   * This map holds a history of suspected members that we use to detect
 +   * crashes.
 +   */
 +  private final Map<InternalDistributedMember, Long> suspectedMembers = new ConcurrentHashMap();
 +  
 +  /**
 +   * the timeout interval for suspected members
 +   */
 +  private final long suspectMemberTimeout = 180000;
 +  
 +  /**
 +   * Length of time, in seconds, that a member is retained in the zombie set
 +   * 
 +   * @see #shunnedMembers
 +   */
 +  static private final int SHUNNED_SUNSET = Integer.getInteger(
 +      "gemfire.shunned-member-timeout", 300).intValue();
 +  
 +  /**
 +   * Set to true when the service should stop.
 +   */
 +  protected volatile boolean shutdownInProgress = false;
 +  
 +  /**
 +   * Set to true when upcalls should be generated for
 +   * events.
 +   */
 +  protected volatile boolean processingEvents = false;
 +  
 +  /**
 +   * This is the latest viewId installed
 +   */
 +  long latestViewId = -1;
 +  
 +  /** distribution manager statistics */
 +  DMStats stats;
 +
 +  /**
 +   A list of messages received during channel startup that couldn't be processed yet.
 +   Additions or removals of this list must be synchronized
 +   via {@link #startupLock}.
 +   @since 5.0
 +   */
 +  protected LinkedList<StartupEvent> startupMessages = new LinkedList<StartupEvent>();
 +  
 +  /**
 +   * ARB: the map of latches is used to block peer handshakes till
 +   * authentication is confirmed.
 +   */
 +  final private HashMap memberLatch = new HashMap();
 +  
 +  /**
 +   * Insert our own MessageReceiver between us and the direct channel, in order
 +   * to correctly filter membership events.
 +   * 
 +   * @author jpenney
 +   * 
 +   */
 +  class MyDCReceiver implements DirectChannelListener
 +  {
 +
 +    DirectChannelListener upCall;
 +    
 +    /**
 +     * Don't provide events until the caller has told us we are ready.
 +     * 
 +     * Synchronization provided via GroupMembershipService.class.
 +     * 
 +     * Note that in practice we only need to delay accepting the first
 +     * client; we don't need to put this check before every call...
 +     *
 +     */
 +   MyDCReceiver(DirectChannelListener up) {
 +      upCall = up;
 +    }
 +
 +    public void messageReceived(DistributionMessage msg) {
 +      // bug 36851 - notify failure detection that we've had contact from a member
 +      services.getHealthMonitor().contactedBy(msg.getSender());
 +      handleOrDeferMessage(msg);
 +    }
 +
 +    public DistributionManager getDM() {
 +     return upCall.getDM();
 +    }
 +
 +  }
 +
 +
 +  /** if we connect to a locator that has NPD enabled then we enable it in this VM */
 +  public void enableNetworkPartitionDetection() {
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("Network partition detection is being enabled");
 +    }
 +    this.services.getConfig().getDistributionConfig().setEnableNetworkPartitionDetection(true);
 +    this.services.getConfig().setNetworkPartitionDetectionEnabled(true);
 +  }
 +  
 +  /**
 +   * Analyze a given view object, generate events as appropriate
 +   * 
 +   * @param newView
 +   */
 +  protected void processView(long newViewId, NetView newView)
 +  {
 +    // Sanity check...
 +     if (logger.isDebugEnabled()) {
 +      StringBuffer msg = new StringBuffer(200);
 +      msg.append("Membership: Processing view ");
 +      msg.append(newView);
 +      msg.append("} on " + address.toString());
 +      if (!newView.contains(address)) {
 +        logger.info(LocalizedMessage.create(
 +            LocalizedStrings.GroupMembershipService_THE_MEMBER_WITH_ID_0_IS_NO_LONGER_IN_MY_OWN_VIEW_1,
 +            new Object[] {address, newView}));
 +      }
 +    }
 +     
 +//     if (newView.getCrashedMembers().size() > 0) {
 +//       // dump stack for debugging #39827
 +//       OSProcess.printStacks(0);
 +//     }
 +    // We perform the update under a global lock so that other
 +    // incoming events will not be lost in terms of our global view.
 +    latestViewLock.writeLock().lock();
 +    try {
 +      // first determine the version for multicast message serialization
 +      Version version = Version.CURRENT;
 +      for (Iterator<Map.Entry<InternalDistributedMember, Long>> it=surpriseMembers.entrySet().iterator(); it.hasNext(); ) {
 +        InternalDistributedMember mbr = it.next().getKey();
 +        Version itsVersion = mbr.getVersionObject();
 +        if (itsVersion != null && version.compareTo(itsVersion) < 0) {
 +          version = itsVersion;
 +        }
 +      }
 +      for (InternalDistributedMember mbr: newView.getMembers()) {
 +        Version itsVersion = mbr.getVersionObject();
 +        if (itsVersion != null && itsVersion.compareTo(version) < 0) {
 +          version = mbr.getVersionObject();
 +        }
 +      }
 +      disableMulticastForRollingUpgrade = !version.equals(Version.CURRENT);
 +      
 +      if (newViewId < latestViewId) {
 +        // ignore this view since it is old news
 +        return;
 +      }
 +
 +      // Save previous view, for delta analysis
 +      NetView priorView = latestView;
 +      
 +      // update the view to reflect our changes, so that
 +      // callbacks will see the new (updated) view.
 +      latestViewId = newViewId;
 +      latestView = new NetView(newView, newView.getViewId());
 +      
 +      // look for additions
 +      for (int i = 0; i < newView.getMembers().size(); i++) { // additions
 +        InternalDistributedMember m = (InternalDistributedMember)newView.getMembers().get(i);
 +        
 +        // Once a member has been seen via a view, remove them from the
 +        // newborn set
 +        boolean wasSurprise = surpriseMembers.remove(m) != null;
 +        
 +        // bug #45155 - membership view processing was slow, causing a member to connect as "surprise"
 +        // and the surprise timeout removed the member and shunned it, keeping it from being
 +        // recognized as a valid member when it was finally seen in a view
 +//        if (isShunned(m)) {
 +//          warnShuns.add(m);
 +//          continue;
 +//        }
 +
 +        // if it's in a view, it's no longer suspect
 +        suspectedMembers.remove(m);
 +
 +        if (priorView.contains(m) || wasSurprise) {
 +          continue; // already seen
 +        }
 +      
 +        // ARB: unblock any waiters for this particular member.
 +        // i.e. signal any waiting threads in tcpconduit.
 +        String authInit = this.services.getConfig().getDistributionConfig().getSecurityPeerAuthInit();
 +        boolean isSecure = authInit != null && authInit.length() != 0;
 +
 +        if (isSecure) {
 +          CountDownLatch currentLatch;
 +          if ((currentLatch = (CountDownLatch)memberLatch.get(m)) != null) {
 +            currentLatch.countDown();
 +          }
 +        }
 +
 +        if (shutdownInProgress()) {
 +          addShunnedMember(m);
 +          continue; // no additions processed after shutdown begins
 +        } else {
 +          boolean wasShunned = endShun(m); // bug #45158 - no longer shun a process that is now in view
 +          if (wasShunned && logger.isDebugEnabled()) {
 +            logger.debug("No longer shunning {} as it is in the current membership view", m);
 +          }
 +        }
 +        
 +        logger.info(LocalizedMessage.create(LocalizedStrings.GroupMembershipService_MEMBERSHIP_PROCESSING_ADDITION__0_, m));
 +
 +        try {
 +          listener.newMemberConnected(m);
 +        }
 +        catch (VirtualMachineError err) {
 +          SystemFailure.initiateFailure(err);
 +          // If this ever returns, rethrow the error.  We're poisoned
 +          // now, so don't let this thread continue.
 +          throw err;
 +        }
 +        catch (DistributedSystemDisconnectedException e) {
 +          // don't log shutdown exceptions
 +        }
 +        catch (Throwable t) {
 +          // Whenever you catch Error or Throwable, you must also
 +          // catch VirtualMachineError (see above).  However, there is
 +          // _still_ a possibility that you are dealing with a cascading
 +          // error condition, so you also need to check to see if the JVM
 +          // is still usable:
 +          SystemFailure.checkFailure();
 +          logger.info(LocalizedMessage.create(
 +              LocalizedStrings.GroupMembershipService_MEMBERSHIP_FAULT_WHILE_PROCESSING_VIEW_ADDITION_OF__0, m), t);
 +        }
 +      } // additions
 +      
 +      // look for departures
 +      for (int i = 0; i < priorView.getMembers().size(); i++) { // departures
 +        InternalDistributedMember m = (InternalDistributedMember)priorView.getMembers().get(i);
 +        if (newView.contains(m)) {
 +          continue; // still alive
 +        }
 +        
 +        if (surpriseMembers.containsKey(m)) {
 +          continue; // member has not yet appeared in a view
 +        }
 +
 +        try {
 +          removeWithViewLock(m,
 +              newView.getCrashedMembers().contains(m) || suspectedMembers.containsKey(m)
 +              , "departed membership view");
 +        }
 +        catch (VirtualMachineError err) {
 +          SystemFailure.initiateFailure(err);
 +          // If this ever returns, rethrow the error.  We're poisoned
 +          // now, so don't let this thread continue.
 +          throw err;
 +        }
 +        catch (Throwable t) {
 +          // Whenever you catch Error or Throwable, you must also
 +          // catch VirtualMachineError (see above).  However, there is
 +          // _still_ a possibility that you are dealing with a cascading
 +          // error condition, so you also need to check to see if the JVM
 +          // is still usable:
 +          SystemFailure.checkFailure();
 +          logger.info(LocalizedMessage.create(
 +              LocalizedStrings.GroupMembershipService_MEMBERSHIP_FAULT_WHILE_PROCESSING_VIEW_REMOVAL_OF__0, m), t);
 +        }
 +      } // departures
 +      
 +      // expire surprise members, add others to view
 +      long oldestAllowed = System.currentTimeMillis() - this.surpriseMemberTimeout;
 +      for (Iterator<Map.Entry<InternalDistributedMember, Long>> it=surpriseMembers.entrySet().iterator(); it.hasNext(); ) {
 +        Map.Entry<InternalDistributedMember, Long> entry = it.next();
 +        Long birthtime = (Long)entry.getValue();
 +        if (birthtime.longValue() < oldestAllowed) {
 +          it.remove();
 +          InternalDistributedMember m = entry.getKey();
 +          logger.info(LocalizedMessage.create(
 +            LocalizedStrings.GroupMembershipService_MEMBERSHIP_EXPIRING_MEMBERSHIP_OF_SURPRISE_MEMBER_0, m));
 +          removeWithViewLock(m, true, "not seen in membership view in "
 +              + this.surpriseMemberTimeout + "ms");
 +        }
 +        else {
 +          if (!latestView.contains(entry.getKey())) {
 +            latestView.add(entry.getKey());
 +          }
 +        }
 +      }
 +      // expire suspected members
 +      oldestAllowed = System.currentTimeMillis() - this.suspectMemberTimeout;
 +      for (Iterator it=suspectedMembers.entrySet().iterator(); it.hasNext(); ) {
 +        Map.Entry entry = (Map.Entry)it.next();
 +        Long birthtime = (Long)entry.getValue();
 +        if (birthtime.longValue() < oldestAllowed) {
 +          InternalDistributedMember m = (InternalDistributedMember)entry.getKey();
 +          it.remove();
 +        }
 +      }
 +      try {
 +        listener.viewInstalled(latestView);
 +        startCleanupTimer();
 +      }
 +      catch (DistributedSystemDisconnectedException se) {
 +      }
 +    } finally {
 +      latestViewLock.writeLock().unlock();
 +    }
 +  }
 +
 +  /**
 +   * the timer used to perform periodic tasks
 +   * 
 +   * Concurrency: protected by {@link #latestViewLock} ReentrantReadWriteLock
 +   */
 +  private SystemTimer cleanupTimer;
 +
 +  private Services services;
 +
 +  private boolean mcastEnabled;
 +
 +  private boolean tcpDisabled;
 +
 +
 +  @Override
 +  public boolean isMulticastAllowed() {
 +    return !disableMulticastForRollingUpgrade;
 +  }
 +
 +  /**
 +   * Joins the distributed system
 +   * 
 +   * @throws GemFireConfigException - configuration error
 +   * @throws SystemConnectException - problem joining
 +   */
 +  private void join() {
 +    services.setShutdownCause(null);
 +    services.getCancelCriterion().cancel(null);
 +    
 +    latestViewLock.writeLock().lock();
 +    try {
 +      try {
 +        this.isJoining = true; // added for bug #44373
 +
 +        // connect
 +        long start = System.currentTimeMillis();
 +
 +        boolean ok = services.getJoinLeave().join();
 +        
 +        if (!ok) {
 +          throw new GemFireConfigException("Unable to join the distributed system.  " +
 +              "Operation either timed out, was stopped or Locator does not exist.");
 +        }
 +
 +        long delta = System.currentTimeMillis() - start;
 +
 +        logger.info(LogMarker.DISTRIBUTION, LocalizedMessage.create(
 +            LocalizedStrings.GroupMembershipService_JOINED_TOOK__0__MS, delta));
 +
 +        NetView initialView = services.getJoinLeave().getView();
 +        latestView = new NetView(initialView, initialView.getViewId());
 +        listener.viewInstalled(latestView);
 +        
 +      } catch (RuntimeException ex) {
 +        throw ex;
 +      }
 +      catch (Exception ex) {
 +        if (ex.getCause() != null && ex.getCause().getCause() instanceof SystemConnectException) {
 +          throw (SystemConnectException)(ex.getCause().getCause());
 +        }
 +        throw new DistributionException(LocalizedStrings.GroupMembershipService_AN_EXCEPTION_WAS_THROWN_WHILE_JOINING.toLocalizedString(), ex);
 +      }
 +      finally {
 +        this.isJoining = false;
 +      }
 +    }
 +    finally {
 +      latestViewLock.writeLock().unlock();
 +    }
 +  }
 +
 +
 +  public GMSMembershipManager(DistributedMembershipListener listener) {
 +    Assert.assertTrue(listener != null);
 +    this.listener = listener;
 +  }
 +  
 +  @Override
 +  public void init(Services services) {
 +    this.services = services;
 +
 +    Assert.assertTrue(services != null);
 +    
 +    this.stats = services.getStatistics();
 +    DistributionConfig config = services.getConfig().getDistributionConfig();
 +    RemoteTransportConfig transport = services.getConfig().getTransport();
 +
 +    this.membershipCheckTimeout = config.getSecurityPeerMembershipTimeout();
 +    this.wasReconnectingSystem = transport.getIsReconnectingDS();
 +    
 +    // cache these settings for use in send()
 +    this.mcastEnabled = transport.isMcastEnabled();
 +    this.tcpDisabled  = transport.isTcpDisabled();
 +
 +    if (!this.tcpDisabled) {
 +      dcReceiver = new MyDCReceiver(listener);
 +    }
 +    
 +    surpriseMemberTimeout = Math.max(20 * DistributionConfig.DEFAULT_MEMBER_TIMEOUT,
 +        20 * config.getMemberTimeout());
 +    surpriseMemberTimeout = Integer.getInteger("gemfire.surprise-member-timeout", surpriseMemberTimeout).intValue();
 +    
 +  }
 +  
 +  @Override
 +  public void start() {
 +    DistributionConfig config = services.getConfig().getDistributionConfig();
 +    RemoteTransportConfig transport = services.getConfig().getTransport();
 +
 +    int dcPort = 0;
 +    if (!tcpDisabled) {
 +      directChannel = new DirectChannel(this, dcReceiver, config);
 +      dcPort = directChannel.getPort();
 +    }
 +
 +    
 +    services.getMessenger().getMemberID().setDirectChannelPort(dcPort);
 +
 +  }
 +  
 +  
 +  @Override
 +  public void joinDistributedSystem() {
 +    long startTime = System.currentTimeMillis();
 +    
 +    try {
 +      join();
 +    }
 +    catch (RuntimeException e) {
 +      if (directChannel != null) {
 +        directChannel.disconnect(e);
 +      }
 +      throw e;
 +    }
 +    
 +    this.address = services.getMessenger().getMemberID();
 +
 +    int dcPort = 0;
 +    if (directChannel != null) {
 +      dcPort = directChannel.getPort();
 +    }
 +    
 +    if (directChannel != null) {
 +      directChannel.setLocalAddr(address);
 +    }
 +
 +    this.hasJoined = true;
 +
 +    // in order to debug startup issues we need to announce the membership
 +    // ID as soon as we know it
 +    logger.info(LocalizedMessage.create(LocalizedStrings.GroupMembershipService_entered_into_membership_in_group_0_with_id_1,
 +        new Object[]{""+(System.currentTimeMillis()-startTime)}));
 +
 +  }
 +  
 +  @Override
 +  public void started() {
 +  }
 +  
 +
 +  /** this is invoked by JoinLeave when there is a loss of quorum in the membership system */
 +  public void quorumLost(Collection<InternalDistributedMember> failures, NetView view) {
 +    // notify of quorum loss if split-brain detection is enabled (meaning we'll shut down) or
 +    // if the loss is more than one member
 +    
 +    boolean notify = failures.size() > 1;
 +    if (!notify) {
 +      notify = services.getConfig().isNetworkPartitionDetectionEnabled();
 +    }
 +    
 +    if (notify) {
 +      List<InternalDistributedMember> remaining = new ArrayList<InternalDistributedMember>(view.getMembers());
 +      remaining.removeAll(failures);
 +      
 +      if (inhibitForceDisconnectLogging) {
 +        if (logger.isDebugEnabled()) {
 +          logger.debug("<ExpectedException action=add>Possible loss of quorum</ExpectedException>");
 +        }
 +      }
 +      logger.fatal(LocalizedMessage.create(
 +          LocalizedStrings.GroupMembershipService_POSSIBLE_LOSS_OF_QUORUM_DETECTED, new Object[] {failures.size(), failures}));
 +      if (inhibitForceDisconnectLogging) {
 +        if (logger.isDebugEnabled()) {
 +          logger.debug("<ExpectedException action=remove>Possible loss of quorum</ExpectedException>");
 +        }
 +      }
 +      
 +  
 +      try {
 +        this.listener.quorumLost(new HashSet<InternalDistributedMember>(failures),
 +            remaining);
 +      } catch (CancelException e) {
 +        // safe to ignore - a forced disconnect probably occurred
 +      }
 +    }
 +  }
 +  
 +
 +  @Override
 +  public boolean testMulticast() {
 +    try {
 +      return services.getMessenger().testMulticast(services.getConfig().getMemberTimeout());
 +    } catch (InterruptedException e) {
 +      services.getCancelCriterion().checkCancelInProgress(e);
 +      Thread.currentThread().interrupt();
 +      return false;
 +    }
 +  }
 +  
 +  /**
 +   * Remove a member.  {@link #latestViewLock} must be held
 +   * before this method is called.  If member is not already shunned,
 +   * the uplevel event handler is invoked.
 +   * 
 +   * @param dm
 +   * @param crashed
 +   * @param reason
 +   */
 +  protected void removeWithViewLock(InternalDistributedMember dm,
 +      boolean crashed, String reason) {
 +    boolean wasShunned = isShunned(dm);
 +
 +    // Delete resources
 +    destroyMember(dm, crashed, reason);
 +
 +    if (wasShunned) {
 +      return; // Explicit deletion, no upcall.
 +    }
 +    
 +    try {
 +      listener.memberDeparted(dm, crashed, reason);
 +    }
 +    catch (DistributedSystemDisconnectedException se) {
 +      // let's not get huffy about it
 +    }
 +  }
 +  
 +  /**
 +   * Process a surprise connect event, or place it on the startup queue.
 +   * @param member the member
 +   */
 +  protected void handleOrDeferSurpriseConnect(InternalDistributedMember member) {
 +    synchronized (startupLock) {
 +      if (!processingEvents) {
 +        startupMessages.add(new StartupEvent(member));
 +        return;
 +      }
 +    }
 +    processSurpriseConnect(member);
 +  }
 +  
 +  public void startupMessageFailed(DistributedMember mbr, String failureMessage) {
 +    // fix for bug #40666
 +    addShunnedMember((InternalDistributedMember)mbr);
 +    // fix for bug #41329, hang waiting for replies
 +    try {
 +      listener.memberDeparted((InternalDistributedMember)mbr, true, "failed to pass startup checks");
 +    }
 +    catch (DistributedSystemDisconnectedException se) {
 +      // let's not get huffy about it
 +    }
 +  }
 +
 +  
 +  /**
 +   * Logic for handling a direct connection event (message received
 +   * from a member not in the view).  Does not employ the
 +   * startup queue.
 +   * <p>
 +   * Must be called with {@link #latestViewLock} held.  Waits
 +   * until there is a stable view.  If the member has already
 +   * been added, simply returns; else adds the member.
 +   * 
 +   * @param dm the member joining
 +   */
 +  public boolean addSurpriseMember(DistributedMember dm) {
 +    final InternalDistributedMember member = (InternalDistributedMember)dm;
 +    boolean warn = false;
 +    
 +    latestViewLock.writeLock().lock();
 +    try {
 +      // At this point, the join may have been discovered by
 +      // other means.
 +      if (latestView.contains(member)) {
 +        return true;
 +      }
 +      if (surpriseMembers.containsKey(member)) {
 +        return true;
 +      }
 +      if (member.getVmViewId() < 0) {
 +        logger.warn("adding a surprise member that has not yet joined the distributed system: " + member, new Exception("stack trace"));
 +      }
 +      if (latestView.getViewId() > member.getVmViewId()) {
 +        // tell the process that it should shut down distribution.
 +        // Run in a separate thread so we don't hold the view lock during the request.  Bug #44995
 +        new Thread(Thread.currentThread().getThreadGroup(),
 +            "Removing shunned GemFire node " + member) {
 +          @Override
 +          public void run() {
 +            // fix for bug #42548
 +            // this is an old member that shouldn't be added
 +            logger.warn(LocalizedMessage.create(
 +                LocalizedStrings.GroupMembershipService_Invalid_Surprise_Member, new Object[]{member, latestView}));
 +            requestMemberRemoval(member, "this member is no longer in the view but is initiating connections");
 +          }
 +        }.start();
 +        addShunnedMember(member);
 +        return false;
 +      }
 +
 +      // Adding him to this set ensures we won't remove him if a new
 +      // view comes in and he's still not visible.
 +      surpriseMembers.put(member, Long.valueOf(System.currentTimeMillis()));
 +
 +      if (shutdownInProgress()) {
 +        // Force disconnect, esp. the TCPConduit
 +        String msg = LocalizedStrings.GroupMembershipService_THIS_DISTRIBUTED_SYSTEM_IS_SHUTTING_DOWN.toLocalizedString();
 +        if (directChannel != null) {
 +          try {
 +            directChannel.closeEndpoint(member, msg);
 +          } catch (DistributedSystemDisconnectedException e) {
 +            // ignore - happens during shutdown
 +          }
 +        }
 +        destroyMember(member, false, msg); // for good luck
 +        return true; // allow during shutdown
 +      }
 +
 +      if (isShunned(member)) {
 +        warn = true;
 +        surpriseMembers.remove(member);
 +      } else {
 +
 +        // Now that we're sure the member is new, add them.
 +        // make sure the surprise-member cleanup task is running
 +        if (this.cleanupTimer == null) {
 +          startCleanupTimer();
 +        } // cleanupTimer == null
 +
 +        // Ensure that the member is accounted for in the view
 +        // Conjure up a new view including the new member. This is necessary
 +        // because we are about to tell the listener about a new member, so
 +        // the listener should rightfully expect that the member is in our
 +        // membership view.
 +
 +        // However, we put the new member at the end of the list.  This
 +        // should ensure he's not chosen as an elder.
 +        // This will get corrected when he finally shows up in the
 +        // view.
 +        NetView newMembers = new NetView(latestView, latestView.getViewId());
 +        newMembers.add(member);
 +        latestView = newMembers;
 +      }
 +    } finally {
 +      latestViewLock.writeLock().unlock();
 +    }
 +    if (warn) { // fix for bug #41538 - deadlock while alerting
 +      logger.warn(LocalizedMessage.create(
 +          LocalizedStrings.GroupMembershipService_MEMBERSHIP_IGNORING_SURPRISE_CONNECT_FROM_SHUNNED_MEMBER_0, member));
 +    } else {
 +      listener.newMemberConnected(member);
 +    }
 +    return !warn;
 +  }
 +  
 +
 +  /** starts periodic task to perform cleanup chores such as expire surprise members */
 +  private void startCleanupTimer() {
 +    latestViewLock.writeLock().lock();
 +    try {
 +      if (this.cleanupTimer != null) {
 +        return;
 +      }
 +      DistributedSystem ds = InternalDistributedSystem.getAnyInstance();
 +      if (ds != null && ds.isConnected()) {
 +        this.cleanupTimer = new SystemTimer(ds, true);
 +        SystemTimer.SystemTimerTask st = new SystemTimer.SystemTimerTask() {
 +          @Override
 +          public void run2() {
 +            latestViewLock.writeLock().lock();
 +            try {
 +              long oldestAllowed = System.currentTimeMillis() - surpriseMemberTimeout;
 +              for (Iterator it=surpriseMembers.entrySet().iterator(); it.hasNext(); ) {
 +                Map.Entry entry = (Map.Entry)it.next();
 +                Long birthtime = (Long)entry.getValue();
 +                if (birthtime.longValue() < oldestAllowed) {
 +                  it.remove();
 +                  InternalDistributedMember m = (InternalDistributedMember)entry.getKey();
 +                  logger.info(LocalizedMessage.create(
 +                      LocalizedStrings.GroupMembershipService_MEMBERSHIP_EXPIRING_MEMBERSHIP_OF_SURPRISE_MEMBER_0, m));
 +                  removeWithViewLock(m, true, "not seen in membership view in "
 +                      + surpriseMemberTimeout + "ms");
 +                }
 +              }
 +            } finally {
 +              latestViewLock.writeLock().unlock();
 +            }
 +          }
 +        };
 +        this.cleanupTimer.scheduleAtFixedRate(st, surpriseMemberTimeout, surpriseMemberTimeout/3);
 +      } // ds != null && ds.isConnected()
 +    } finally {
 +      latestViewLock.writeLock().unlock();
 +    }
 +  }
 +  /**
 +   * Dispatch the distribution message, or place it on the startup queue.
 +   * 
 +   * @param msg the message to process
 +   */
 +  protected void handleOrDeferMessage(DistributionMessage msg) {
 +    synchronized(startupLock) {
 +      if (!processingEvents) {
 +        startupMessages.add(new StartupEvent(msg));
 +        return;
 +      }
 +    }
 +    dispatchMessage(msg);
 +  }
 +  
 +  public void warnShun(DistributedMember m) {
 +    latestViewLock.writeLock().lock();
 +    try {
 +      if (!shunnedMembers.containsKey(m)) {
 +        return; // not shunned
 +      }
 +      if (shunnedAndWarnedMembers.contains(m)) {
 +        return; // already warned
 +      }
 +      shunnedAndWarnedMembers.add(m);
 +    } finally {
 +      latestViewLock.writeLock().unlock();
 +    }
 +    // issue warning outside of sync since it may cause messaging and we don't
 +    // want to hold the view lock while doing that
 +    logger.warn(LocalizedMessage.create(LocalizedStrings.GroupMembershipService_MEMBERSHIP_DISREGARDING_SHUNNED_MEMBER_0, m));
 +  }
 +  
 +  @Override
 +  public void processMessage(DistributionMessage msg) {
 +    handleOrDeferMessage(msg);
 +  }
 +  
 +  /**
 +   * Logic for processing a distribution message.  
 +   * <p>
 +   * It is possible to receive messages not consistent with our view.
 +   * We handle this here, and generate an uplevel event if necessary
 +   * @param msg the message
 +   */
 +  public void dispatchMessage(DistributionMessage msg) {
 +    boolean isNew = false;
 +    InternalDistributedMember m = msg.getSender();
 +    boolean shunned = false;
 +
 +    // First grab the lock: check the sender against our stabilized view.
 +    latestViewLock.writeLock().lock();
 +    try {
 +      if (isShunned(m)) {
 +        if (msg instanceof StartupMessage) {
 +          endShun(m);
 +        }
 +        else {
 +          // fix for bug 41538 - sick alert listener causes deadlock
 +          // due to view lock being held during messaging
 +          shunned = true;
 +        }
 +      } // isShunned
 +
 +      if (!shunned) {
 +        isNew = !latestView.contains(m) && !surpriseMembers.containsKey(m);
 +
 +        // If it's a new sender, wait our turn, generate the event
 +        if (isNew) {
 +          shunned = !addSurpriseMember(m);
 +        } // isNew
 +      }
 +
 +      // Latch the view before we unlock
 +    } finally {
 +      latestViewLock.writeLock().unlock();
 +    }
 +    
 +    if (shunned) { // bug #41538 - shun notification must be outside synchronization to avoid hanging
 +      warnShun(m);
 +      logger.info("Membership: Ignoring message from shunned member <{}>:{}", m, msg);
 +      throw new MemberShunnedException(m);
 +    }
 +    
 +    listener.messageReceived(msg);
 +  }
 +
 +  /**
 +   * Process a new view object, or place on the startup queue
 +   * @param viewArg the new view
 +   */
 +  protected void handleOrDeferViewEvent(NetView viewArg) {
 +    if (this.isJoining) {
 +      // bug #44373 - queue all view messages while joining.
 +      // This is done under the latestViewLock, but we can't block here because
 +      // we're sitting in the UDP reader thread.
 +      synchronized(startupLock) {
 +        startupMessages.add(new StartupEvent(viewArg));
 +        return;
 +      }
 +    }
 +    latestViewLock.writeLock().lock();
 +    try {
 +      synchronized(startupLock) {
 +        if (!processingEvents) {
 +          startupMessages.add(new StartupEvent(viewArg));
 +          return;
 +        }
 +      }
 +      // view processing can take a while, so we use a separate thread
 +      // to avoid blocking a reader thread
 +      NetView newView = viewArg;
 +      long newId = viewArg.getViewId();
 +      LocalViewMessage v = new LocalViewMessage(address, newId, newView,
 +          GMSMembershipManager.this);
 +
 +      listener.messageReceived(v);
 +    } finally {
 +      latestViewLock.writeLock().unlock();
 +    }
 +  }
 +  
 +  @Override
 +  public void memberSuspected(InternalDistributedMember initiator, InternalDistributedMember suspect, String reason) {
 +    SuspectMember s = new SuspectMember(initiator, suspect, reason);
 +    handleOrDeferSuspect(s);
 +  }
 +
 +  /**
 +   * Process a new view object, or place on the startup queue
 +   * @param suspectInfo the suspectee and suspector
 +   */
 +  protected void handleOrDeferSuspect(SuspectMember suspectInfo) {
 +    latestViewLock.writeLock().lock();
 +    try {
 +      synchronized(startupLock) {
 +        if (!processingEvents) {
 +          return;
 +        }
 +      }
 +      InternalDistributedMember suspect = suspectInfo.suspectedMember;
 +      InternalDistributedMember who = suspectInfo.whoSuspected;
 +      this.suspectedMembers.put(suspect, Long.valueOf(System.currentTimeMillis()));
 +      try {
 +        listener.memberSuspect(suspect, who, suspectInfo.reason);
 +      }
 +      catch (DistributedSystemDisconnectedException se) {
 +        // let's not get huffy about it
 +      }
 +    } finally {
 +      latestViewLock.writeLock().unlock();
 +    }
 +  }
 +
 +  /**
 +   * Process a potential direct connect.  Does not use
 +   * the startup queue.  It grabs the {@link #latestViewLock} 
 +   * and then processes the event.
 +   * <p>
 +   * It is a <em>potential</em> event, because we don't know until we've
 +   * grabbed a stable view if this is really a new member.
 +   * 
 +   * @param member
 +   */
 +  private void processSurpriseConnect(
 +      InternalDistributedMember member) 
 +  {
 +    addSurpriseMember(member);
 +  }
 +  
 +  /**
 +   * Dispatch routine for processing a single startup event
 +   * @param o the startup event to handle
 +   */
 +  private void processStartupEvent(StartupEvent o) {
 +    // Most common events first
 +    
 +    if (o.isDistributionMessage()) { // normal message
 +      try {
 +        dispatchMessage(o.dmsg);
 +      }
 +      catch (MemberShunnedException e) {
 +        // message from non-member - ignore
 +      }
 +    } 
 +    else if (o.isGmsView()) { // view event
 +      processView(o.gmsView.getViewId(), o.gmsView);
 +    }
 +    else if (o.isSurpriseConnect()) { // connect
 +      processSurpriseConnect(o.member);
 +    }
 +    
 +    else // sanity
 +      throw new InternalGemFireError(LocalizedStrings.GroupMembershipService_UNKNOWN_STARTUP_EVENT_0.toLocalizedString(o));
 +  }
 +  
 +  /**
 +   * Special mutex to create a critical section for
 +   * {@link #startEventProcessing()}
 +   */
 +  private final Object startupMutex = new Object();
 +
 +  
 +  public void startEventProcessing()
 +  {
 +    // Only allow one thread to perform the work
 +    synchronized (startupMutex) {
 +      if (logger.isDebugEnabled())
 +        logger.debug("Membership: draining startup events.");
 +      // Remove the backqueue of messages, but allow
 +      // additional messages to be added.
 +      for (;;) {
 +        StartupEvent ev;
 +        // Only grab the mutex while reading the queue.
 +        // Other events may arrive while we're attempting to
 +        // drain the queue.  This is OK, we'll just keep processing
 +        // events here until we've caught up.
 +        synchronized (startupLock) {
 +          int remaining = startupMessages.size();
 +          if (remaining == 0) {
 +            // While holding the lock, flip the bit so that
 +            // no more events get put into startupMessages, and
 +            // notify all waiters to proceed.
 +            processingEvents = true;
 +            startupLock.notifyAll();
 +            break;  // ...and we're done.
 +          }
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("Membership: {} remaining startup message(s)", remaining);
 +          }
 +          ev = (StartupEvent)startupMessages.removeFirst();
 +        } // startupLock
 +        try {
 +          processStartupEvent(ev);
 +        }
 +        catch (VirtualMachineError err) {
 +          SystemFailure.initiateFailure(err);
 +          // If this ever returns, rethrow the error.  We're poisoned
 +          // now, so don't let this thread continue.
 +          throw err;
 +        }
 +        catch (Throwable t) {
 +          // Whenever you catch Error or Throwable, you must also
 +          // catch VirtualMachineError (see above).  However, there is
 +          // _still_ a possibility that you are dealing with a cascading
 +          // error condition, so you also need to check to see if the JVM
 +          // is still usable:
 +          SystemFailure.checkFailure();
 +          logger.warn(LocalizedMessage.create(LocalizedStrings.GroupMembershipService_MEMBERSHIP_ERROR_HANDLING_STARTUP_EVENT), t);
 +        }
 +        
 +      } // for
 +      if (logger.isDebugEnabled())
 +        logger.debug("Membership: finished processing startup events.");
 +    } // startupMutex
 +  }
 +
 + 
 +  public void waitForEventProcessing() throws InterruptedException {
 +    // First check outside of a synchronized block.  Cheaper and sufficient.
 +    if (Thread.interrupted()) throw new InterruptedException();
 +    if (processingEvents)
 +      return;
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("Membership: waiting until the system is ready for events");
 +    }
 +    for (;;) {
 +      directChannel.getCancelCriterion().checkCancelInProgress(null);
 +      synchronized (startupLock) {
 +        // Now check using a memory fence and synchronization.
 +        if (processingEvents)
 +          break;
 +        boolean interrupted = Thread.interrupted();
 +        try {
 +          startupLock.wait();
 +        }
 +        catch (InterruptedException e) {
 +          interrupted = true;
 +          directChannel.getCancelCriterion().checkCancelInProgress(e);
 +        }
 +        finally {
 +          if (interrupted) {
 +            Thread.currentThread().interrupt();
 +          }
 +        }
 +      } // synchronized
 +    } // for
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("Membership: continuing");
 +    }
 +  }
 +
 +  /**
 +   * for testing we need to validate the startup event list
 +   */
 +  public List<StartupEvent> getStartupEvents() {
 +    return this.startupMessages;
 +  }
 +
 +  public ReadWriteLock getViewLock() {
 +    return this.latestViewLock;
 +  }
 +
 +  /**
 +   * Returns a copy (possibly not current) of the current
 +   * view (a list of {@link DistributedMember}s)
 +   */
 +  public NetView getView()
 +  {
 +    // Grab the latest view under a mutex...
 +    NetView v;
 +
 +    latestViewLock.readLock().lock();
 +    v = latestView;
 +    latestViewLock.readLock().unlock();
 +
 +    NetView result = new NetView(v, v.getViewId());
 +    
 +    for (InternalDistributedMember m: v.getMembers()) {
 +      if (isShunned(m)) {
 +        result.remove(m);
 +      }
 +    }
 +    
 +    return result;
 +  }
 +  
 +  /**
 +   * test hook<p>
 +   * The lead member is the eldest member with partition detection enabled.<p>
 +   * If no members have partition detection enabled, there will be no
 +   * lead member and this method will return null.
 +   * @return the lead member associated with the latest view
 +   */
 +  public DistributedMember getLeadMember() {
 +    latestViewLock.readLock().lock();
 +    try {
 +      return latestView == null? null : latestView.getLeadMember();
 +    } finally {
 +      latestViewLock.readLock().unlock();
 +    }
 +  }
 +  
 +  protected boolean isJoining() {
 +    return this.isJoining;
 +  }
 +  
 +  /**
 +   * test hook
 +   * @return the current membership view coordinator
 +   */
 +  public DistributedMember getCoordinator() {
 +    // note - we go straight to JoinLeave because the
 +    // DistributionManager queues view changes in a serial executor, where
 +    // they're asynchronously installed.  The DS may still see the old coordinator
 +    latestViewLock.readLock().lock();
 +    try {
 +      return latestView == null? null : latestView.getCoordinator();
 +    } finally {
 +      latestViewLock.readLock().unlock();
 +    }
 +  }
 +
 +  public boolean memberExists(DistributedMember m) {
 +    latestViewLock.readLock().lock();
 +    NetView v = latestView;
 +    latestViewLock.readLock().unlock();
 +    return v.getMembers().contains(m);
 +  }
 +  
 +  /**
 +   * Returns the identity associated with this member. WARNING: this value will
 +   * be returned after the channel is closed, but in that case it is good for
 +   * logging purposes only. :-)
 +   */
 +  public InternalDistributedMember getLocalMember()
 +  {
 +    return address;
 +  }
 +  
 +  public Services getServices() {
 +    return services;
 +  }
 +
 +  public void postConnect() {
 +  }
 +  
 +  /**
 +   * @see SystemFailure#loadEmergencyClasses()
 +   /**
 +   * break any potential circularity in {@link #loadEmergencyClasses()}
 +   */
 +  private static volatile boolean emergencyClassesLoaded = false;
 +
 +  /**
 +   * inhibits logging of ForcedDisconnectException to keep dunit logs clean
 +   * while testing this feature
 +   */
 +  protected static volatile boolean inhibitForceDisconnectLogging;
 +  
 +  /**
 +   * Ensure that the critical classes from components
 +   * get loaded.
 +   * 
 +   * @see SystemFailure#loadEmergencyClasses()
 +   */
 +  public static void loadEmergencyClasses() {
 +    if (emergencyClassesLoaded) return;
 +    emergencyClassesLoaded = true;
 +    DirectChannel.loadEmergencyClasses();
 +    GMSJoinLeave.loadEmergencyClasses();
 +    GMSHealthMonitor.loadEmergencyClasses();
 +  }
 +  /**
 +   * Close the receiver, avoiding all potential deadlocks and
 +   * eschewing any attempts at being graceful.
 +   * 
 +   * @see SystemFailure#emergencyClose()
 +   */
 +  public void emergencyClose() {
 +    final boolean DEBUG = SystemFailure.TRACE_CLOSE;
 +    
 +    setShutdown(); 
 +
 +    // We can't call close() because they will allocate objects.  Attempt
 +    // a surgical strike and touch the important protocols.
 +    
 +    // MOST important, kill the FD protocols...
 +    services.emergencyClose();
 +    
 +    // Close the TCPConduit sockets...
 +    if (directChannel != null) {
 +      if (DEBUG) {
 +        System.err.println("DEBUG: emergency close of DirectChannel");
 +      }
 +      directChannel.emergencyClose();
 +    }
 +    
 +    if (DEBUG) {
 +      System.err.println("DEBUG: done closing GroupMembershipService");
 +    }
 +  }
 +  
 +  
 +  /**
 +   * in order to avoid split-brain occurring when a member is shutting down due to
 +   * race conditions in view management we add it as a shutdown member when we receive
 +   * a shutdown message.  This is not the same as a SHUNNED member.
 +   */
 +  public void shutdownMessageReceived(InternalDistributedMember id, String reason) {
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("Membership: recording shutdown status of {}", id);
 +    }
 +    synchronized(this.shutdownMembers) { 
 +      this.shutdownMembers.put(id, id);
 +      services.getHealthMonitor().memberShutdown(id, reason);
 +      services.getJoinLeave().memberShutdown(id, reason);
 +    }
 +  }
 +  
 +  /**
 +   * returns true if a shutdown message has been received from the given address but
 +   * that member is still in the membership view or is a surprise member.
 +   */
 +  public boolean isShuttingDown(InternalDistributedMember mbr) {
 +    synchronized(shutdownMembers) {
 +      return shutdownMembers.containsKey(mbr);
 +    }
 +  }
 +
 +  
 +  public void shutdown() {
 +    setShutdown();
 +    services.stop();
 +  }
 +  
 +  @Override
 +  public void stop() {
 +    
 +    // [bruce] Do not null out the channel w/o adding appropriate synchronization
 +    
 +    logger.debug("MembershipManager closing");
 +    
 +    if (directChannel != null) {
 +      directChannel.disconnect(null);
 +
 +      if (address != null) {
 +        // Make sure that channel information is consistent
 +        // Probably not important in this particular case, but just
 +        // to be consistent...
 +        latestViewLock.writeLock().lock();
 +        try {
 +          destroyMember(address, false, "orderly shutdown");
 +        } finally {
 +          latestViewLock.writeLock().unlock();
 +        }
 +      }
 +    }
 +    
 +    if (cleanupTimer != null) {
 +      cleanupTimer.cancel();
 +    }
 +    
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("Membership: channel closed");
 +    }
 +  }
 +  
 +  public void uncleanShutdown(String reason, final Exception e) {
 +    inhibitForcedDisconnectLogging(false);
 +    
 +    if (services.getShutdownCause() == null) {
 +      services.setShutdownCause(e);
 +    }
 +    
 +    if (this.directChannel != null) {
 +      this.directChannel.disconnect(e);
 +    }
 +    
 +    // first shut down communication so we don't do any more harm to other
 +    // members
 +    services.emergencyClose();
 +    
 +    if (e != null) {
 +      try {
 +        if (membershipTestHooks != null) {
 +          List l = membershipTestHooks;
 +          for (Iterator it=l.iterator(); it.hasNext(); ) {
 +            MembershipTestHook dml = (MembershipTestHook)it.next();
 +            dml.beforeMembershipFailure(reason, e);
 +          }
 +        }
 +        listener.membershipFailure(reason, e);
 +        if (membershipTestHooks != null) {
 +          List l = membershipTestHooks;
 +          for (Iterator it=l.iterator(); it.hasNext(); ) {
 +            MembershipTestHook dml = (MembershipTestHook)it.next();
 +            dml.afterMembershipFailure(reason, e);
 +          }
 +        }
 +      }
 +      catch (RuntimeException re) {
 +        logger.warn(LocalizedMessage.create(LocalizedStrings.GroupMembershipService_EXCEPTION_CAUGHT_WHILE_SHUTTING_DOWN), re);
 +      }
 +    }
 +  }
 +  
 +  /** generate XML for the cache before shutting down due to forced disconnect */
 +  public void saveCacheXmlForReconnect() {
 +    // there are two versions of this method so it can be unit-tested
 +    boolean sharedConfigEnabled = services.getConfig().getDistributionConfig().getUseSharedConfiguration();
 +    saveCacheXmlForReconnect(sharedConfigEnabled);
 +  }
 +  
 +  /** generate XML from the cache before shutting down due to forced disconnect */
 +  public void saveCacheXmlForReconnect(boolean sharedConfigEnabled) {
 +    // first save the current cache description so reconnect can rebuild the cache
 +    GemFireCacheImpl cache = GemFireCacheImpl.getInstance();
 +    if (cache != null && (cache instanceof Cache)) {
 +      if (!Boolean.getBoolean("gemfire.autoReconnect-useCacheXMLFile")
 +          && !cache.isSqlfSystem() && !sharedConfigEnabled) {
 +        try {
 +          logger.info("generating XML to rebuild the cache after reconnect completes");
 +          StringPrintWriter pw = new StringPrintWriter(); 
 +          CacheXmlGenerator.generate((Cache)cache, pw, true, false);
 +          String cacheXML = pw.toString();
 +          cache.getCacheConfig().setCacheXMLDescription(cacheXML);
 +          logger.info("XML generation completed: {}", cacheXML);
 +        } catch (CancelException e) {
 +          logger.info(LocalizedMessage.create(LocalizedStrings.GroupMembershipService_PROBLEM_GENERATING_CACHE_XML), e);
 +        }
 +      } else if (sharedConfigEnabled && !cache.getCacheServers().isEmpty()) {
 +        // we need to retain a cache-server description if this JVM was started by gfsh
 +        List<CacheServerCreation> list = new ArrayList<CacheServerCreation>(cache.getCacheServers().size());
 +        for (Iterator it = cache.getCacheServers().iterator(); it.hasNext(); ) {
-           CacheServer cs = (CacheServer)it.next();
-           CacheServerCreation bsc = new CacheServerCreation(cache, cs);
-           list.add(bsc);
++          CacheServerImpl cs = (CacheServerImpl)it.next();
++          if (cs.isDefaultServer()) {
++            CacheServerCreation bsc = new CacheServerCreation(cache, cs);
++            list.add(bsc);
++          }
 +        }
 +        cache.getCacheConfig().setCacheServerCreation(list);
 +        logger.info("CacheServer configuration saved");
 +      }
 +    }
 +  }
 +
 +  public boolean requestMemberRemoval(DistributedMember mbr, String reason) {
 +    if (mbr.equals(this.address)) {
 +      return false;
 +    }
 +    logger.warn(LocalizedMessage.create(
 +        LocalizedStrings.GroupMembershipService_MEMBERSHIP_REQUESTING_REMOVAL_OF_0_REASON_1,
 +        new Object[] {mbr, reason}));
 +    try {
 +      services.getJoinLeave().remove((InternalDistributedMember)mbr, reason);
 +    }
 +    catch (RuntimeException e) {
 +      Throwable problem = e;
 +      if (services.getShutdownCause() != null) {
 +        Throwable cause = services.getShutdownCause();
 +        // If ForcedDisconnectException occurred then report it as actual
 +        // problem.
 +        if (cause instanceof ForcedDisconnectException) {
 +          problem = (Exception) cause;
 +        } else {
 +          Throwable ne = problem;
 +          while (ne.getCause() != null) {
 +            ne = ne.getCause();
 +          }
 +          try {
 +            ne.initCause(services.getShutdownCause());
 +          }
 +          catch (IllegalArgumentException selfCausation) {
 +            // fix for bug 38895 - the cause is already in place
 +          }
 +        }
 +      }
 +      if (!services.getConfig().getDistributionConfig().getDisableAutoReconnect()) {
 +        saveCacheXmlForReconnect();
 +      }
 +      listener.membershipFailure("Channel closed", problem);
 +      throw new DistributedSystemDisconnectedException("Channel closed", problem);
 +    }
 +    return true;
 +  }
 +  
 +  public void suspectMembers(Set members, String reason) {
 +    for (Iterator it=members.iterator(); it.hasNext(); ) {
 +      verifyMember((DistributedMember)it.next(), reason);
 +    }
 +  }
 +  
 +  public void suspectMember(DistributedMember mbr, String reason) {
 +    if (!this.shutdownInProgress && !this.shutdownMembers.containsKey(mbr)) {
 +      verifyMember(mbr, reason);
 +    }
 +  }
 +
 +  /* like memberExists() this checks to see if the given ID is in the current
 +   * membership view.  If it is in the view though we try to contact it
 +   * to see if it's still around.  If we can't contact it then
 +   * suspect messages are sent to initiate final checks
 +   * @param mbr the member to verify
 +   * @param reason why the check is being done (must not be blank/null)
 +   * @return true if the member checks out
 +   */
 +  public boolean verifyMember(DistributedMember mbr, String reason) {
 +    if (mbr != null && memberExists((InternalDistributedMember)mbr)) {
 +      return this.services.getHealthMonitor().checkIfAvailable(mbr, reason, true);
 +    }
 +    return false;
 +  }
 +
 +  /**
 +   * Perform the grossness associated with sending a message over
 +   * a DirectChannel
 +   * 
 +   * @param destinations the list of destinations
 +   * @param content the message
 +   * @param theStats the statistics object to update
 +   * @return all recipients who did not receive the message (null if
 +   * all received it)
 +   * @throws NotSerializableException if the message is not serializable
 +   */
 +  protected Set<InternalDistributedMember> directChannelSend(InternalDistributedMember[] destinations,
 +      DistributionMessage content,
 +      DMStats theStats)
 +      throws NotSerializableException
 +  {
 +    boolean allDestinations;
 +    InternalDistributedMember[] keys;
 +    if (content.forAll()) {
 +      allDestinations = true;
 +      latestViewLock.writeLock().lock();
 +      try {
 +        List<InternalDistributedMember> keySet = latestView.getMembers();
 +        keys = new InternalDistributedMember[keySet.size()];
 +        keys = (InternalDistributedMember[])keySet.toArray(keys);
 +      } finally {
 +        latestViewLock.writeLock().unlock();
 +      }
 +    }
 +    else {
 +      allDestinations = false;
 +      keys = destinations;
 +    }
 +
 +    int sentBytes = 0;
 +    try {
 +      sentBytes = directChannel.send(this, keys, content,
 +          this.services.getConfig().getDistributionConfig().getAckWaitThreshold(),
 +          this.services.getConfig().getDistributionConfig().getAckSevereAlertThreshold());
 +                                     
 +      if (theStats != null) {
 +        theStats.incSentBytes(sentBytes);
 +      }
 +      
 +      if (sentBytes == 0) {
 +        if (services.getCancelCriterion().cancelInProgress() != null) {
 +          throw new DistributedSystemDisconnectedException();
 +        }
 +      }
 +    }
 +    catch (DistributedSystemDisconnectedException ex) {
 +      if (services.getShutdownCause() != null) {
 +        throw new DistributedSystemDisconnectedException("DistributedSystem is shutting down", services.getShutdownCause());
 +      } else {
 +        throw ex; // see bug 41416
 +      }
 +    }
 +    catch (ConnectExceptions ex) {
 +      if (allDestinations)
 +        return null;
 +      
 +      List members = ex.getMembers(); // We need to return this list of failures
 +      
 +      // SANITY CHECK:  If we fail to send a message to an existing member 
 +      // of the view, we have a serious error (bug36202).
 +      NetView view = services.getJoinLeave().getView(); // grab a recent view, excluding shunned members
 +      
 +      // Iterate through members and causes in tandem :-(
 +      Iterator it_mem = members.iterator();
 +      Iterator it_causes = ex.getCauses().iterator();
 +      while (it_mem.hasNext()) {
 +        InternalDistributedMember member = (InternalDistributedMember)it_mem.next();
 +        Throwable th = (Throwable)it_causes.next();
 +        
 +        if (!view.contains(member) || (th instanceof ShunnedMemberException)) {
 +          continue;
 +        }
 +        logger.fatal(LocalizedMessage.create(
 +            LocalizedStrings.GroupMembershipService_FAILED_TO_SEND_MESSAGE_0_TO_MEMBER_1_VIEW_2,
 +            new Object[] {content, member, view}), th);
 +//        Assert.assertTrue(false, "messaging contract failure");
 +      }
 +      return new HashSet<InternalDistributedMember>(members);
 +    } // catch ConnectionExceptions
 +    catch (ToDataException | CancelException e) {
 +      throw e;
 +    }
 +    catch (IOException e) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("Membership: directChannelSend caught exception: {}", e.getMessage(), e);
 +      }
 +      if (e instanceof NotSerializableException) {
 +        throw (NotSerializableException)e;
 +      }
 +    }
 +    catch (RuntimeException e) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("Membership: directChannelSend caught exception: {}", e.getMessage(), e);
 +      }
 +      throw e;
 +    }
 +    catch (Error e) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("Membership: directChannelSend caught exception: {}", e.getMessage(), e);
 +      }
 +      throw e;
 +    }
 +    return null;
 +  }
 +
 +  /*
 +   * (non-Javadoc)
 +   * @see com.gemstone.gemfire.distributed.internal.membership.MembershipManager#isConnected()
 +   */
 +  public boolean isConnected() {
 +    return (this.hasJoined && !this.shutdownInProgress); 
 +  }
 +  
 +  /**
 +   * Returns true if the distributed system is in the process of auto-reconnecting.
 +   * Otherwise returns false.
 +   */
 +  public boolean isReconnectingDS() {
 +    if (this.hasJoined) {
 +      return false;
 +    } else {
 +      return this.wasReconnectingSystem;
 +    }
 +  }
 +  
 +  @Override
 +  public QuorumChecker getQuorumChecker() {
 +    if ( ! (services.isShutdownDueToForcedDisconnect()) ) {
 +      return null;
 +    }
 +    if (this.quorumChecker != null) {
 +      return this.quorumChecker;
 +    }
 +
 +    QuorumChecker impl = services.getMessenger().getQuorumChecker();
 +    this.quorumChecker = impl;
 +    return impl;
 +  }
 +  
 +  @Override
 +  public void releaseQuorumChecker(QuorumChecker checker) {
 +    ((GMSQuorumChecker)checker).suspend();
 +    InternalDistributedSystem system = InternalDistributedSystem.getAnyInstance();
 +    if (system == null || !system.isConnected()) {
 +      checker.close();
 +    }
 +  }
 +  
 +  public Set send(InternalDistributedMember dest, DistributionMessage msg)
 +    throws NotSerializableException {
 +    
 +    InternalDistributedMember dests[] = new InternalDistributedMember[] { dest };
 +    return send (dests, msg, null);
 +  }
 +  
 +  public Set send(InternalDistributedMember[] destinations,
 +      DistributionMessage msg,
 +      DMStats theStats)
 +      throws NotSerializableException
 +  {
 +    Set result = null;
 +    boolean allDestinations = msg.forAll();
 +    
 +    if (services.getCancelCriterion().cancelInProgress() != null) {
 +      throw new DistributedSystemDisconnectedException("Distributed System is shutting down",
 +          services.getCancelCriterion().generateCancelledException(null));
 +    }
 +    
 +    if (playingDead) { // wellness test hook
 +      while (playingDead && !shutdownInProgress) {
 +        try {
 +          Thread.sleep(1000);
 +        } catch (InterruptedException e) {
 +          Thread.currentThread().interrupt();
 +        }
 +      }
 +    }
 +    
 +    if (isJoining()) {
 +      // If we get here, we are starting up, so just report a failure.
 +      if (allDestinations)
 +        return null;
 +      else {
 +        result = new HashSet();
 +        for (int i = 0; i < destinations.length; i ++)
 +          result.add(destinations[i]);
 +        return result;
 +      }
 +    }
 +    
 +    if (msg instanceof AdminMessageType
 +        && this.shutdownInProgress) {
 +      // no admin messages while shutting down - this can cause threads to hang
 +      return new HashSet(Arrays.asList(msg.getRecipients()));
 +    }
 +
 +    // Handle trivial cases
 +    if (destinations == null) {
 +      if (logger.isTraceEnabled())
 +        logger.trace("Membership: Message send: returning early because null set passed in: '{}'", msg);
 +      return null; // trivially: all recipients received the message
 +    }
 +    if (destinations.length == 0) {
 +      if (logger.isTraceEnabled())
 +        logger.trace("Membership: Message send: returning early because empty destination list passed in: '{}'", msg);
 +      return null; // trivially: all recipients received the message
 +    }
 +
 +    msg.setSender(address);
 +    
 +    msg.setBreadcrumbsInSender();
 +    Breadcrumbs.setProblem(null);
 +
 +    boolean useMcast = false;
 +    if (mcastEnabled) {
 +      useMcast = (msg.getMulticast() || allDestinations);
 +    }
 +    
 +    boolean sendViaMessenger = isForceUDPCommunications(); // enable when bug #46438 is fixed: || msg.sendViaUDP();
 +
 +    if (useMcast || tcpDisabled || sendViaMessenger) {
 +      checkAddressesForUUIDs(destinations);
 +      result = services.getMessenger().send(msg);
 +    }
 +    else {
 +      result = directChannelSend(destinations, msg, theStats);
 +    }
 +
 +    // If the message was a broadcast, don't enumerate failures.
 +    if (allDestinations)
 +      return null;
 +    else {
 +      return result;
 +    }
 +  }
 +  
 +  // MembershipManager method
 +  @Override
 +  public void forceUDPMessagingForCurrentThread() {
 +    forceUseUDPMessaging.set(null);
 +  }
 +  
 +  void checkAddressesForUUIDs(InternalDistributedMember[] addresses) {
 +    for (int i=0; i<addresses.length; i++) {
 +      InternalDistributedMember m = addresses[i];
 +      if(m != null) {
 +        GMSMember id = (GMSMember)m.getNetMember();
 +        if (!id.hasUUID()) {
 +          latestViewLock.readLock().lock();
 +          try {
 +            addresses[i] = latestView.getCanonicalID(addresses[i]);
 +          } finally {
 +            latestViewLock.readLock().unlock();
 +          }
 +        }
 +      }
 +    }
 +  }
 +  
 +  private boolean isForceUDPCommunications() {
 +    Boolean forced = forceUseUDPMessaging.get();
 +    return forced == Boolean.TRUE;
 +  }
 +
 +  // MembershipManager method
 +  @Override
 +  public void releaseUDPMessagingForCurrentThread() {
 +    // not currently supported by this manager
 +  }
 +  
 +  public void setShutdown()
 +  {
 +    latestViewLock.writeLock().lock();
 +    shutdownInProgress = true;
 +    latestViewLock.writeLock().unlock();
 +  }
 +
 +  @Override
 +  public boolean shutdownInProgress() {
 +    // Impossible condition (bug36329): make sure that we check DM's
 +    // view of shutdown here
 +    DistributionManager dm = listener.getDM();
 +    return shutdownInProgress || (dm != null && dm.shutdownInProgress());
 +  }
 +  
 +
 +  /**
 +   * Clean up and create consistent new view with member removed.
 +   * No uplevel events are generated.
 +   * 
 +   * Must be called with the {@link #latestViewLock} held.
 +   */
 +  protected void destroyMember(final InternalDistributedMember member,
 +      boolean crashed, final String reason) {
 +    
 +    // Make sure it is removed from the view
 +    latestViewLock.writeLock().lock();
 +    try {
 +      if (latestView.contains(member)) {
 +        NetView newView = new NetView(latestView, latestView.getViewId());
 +        newView.remove(member);
 +        latestView = newView;
 +      }
 +    } finally {
 +      latestViewLock.writeLock().unlock();
 +    }
 +    
 +    surpriseMembers.remove(member);
 +    
 +    // Trickiness: there is a minor recursion
 +    // with addShunnedMembers, since it will
 +    // attempt to destroy really really old members.  Performing the check
 +    // here breaks the recursion.
 +    if (!isShunned(member)) {
 +      addShunnedMember(member);
 +    }
 +
 +    final DirectChannel dc = directChannel;
 +    if (dc != null) {
 +//      if (crashed) {
 +//        dc.closeEndpoint(member, reason);
 +//      }
 +//      else
 +      // Bug 37944: make sure this is always done in a separate thread,
 +      // so that shutdown conditions don't wedge the view lock
 +      { // fix for bug 34010
 +        Thread t = new Thread() {
 +          @Override
 +          public void run() {
 +            try {
 +              Thread.sleep(
 +                  Integer.getInteger("p2p.disconnectDelay", 3000).intValue());
 +            }
 +            catch (InterruptedException ie) {
 +              Thread.currentThread().interrupt();
 +              // Keep going, try to close the endpoint.
 +            }
 +            if (!dc.isOpen()) {
 +              return;
 +            }
 +            if (logger.isDebugEnabled())
 +              logger.debug("Membership: closing connections for departed member {}", member);
 +            // close connections, but don't do membership notification since it's already been done
 +            dc.closeEndpoint(member, reason, false); 
 +          }
 +        };
 +        t.setDaemon(true);
 +        t.setName("disconnect thread for " + member);
 +        t.start();
 +      } // fix for bug 34010
 +    }
 +  }
 +  
 +  /**
 +   * Indicate whether the given member is in the zombie list (dead or dying)
 +   * @param m the member in question
 +   * 
 +   * This also checks the time the given member was shunned, and
 +   * has the side effect of removing the member from the
 +   * list if it was shunned too far in the past.
 +   * 
 +   * Concurrency: protected by {@link #latestViewLock} ReentrantReadWriteLock
 +   * 
 +   * @return true if the given member is a zombie
 +   */
 +  public boolean isShunned(DistributedMember m) {
 +    latestViewLock.writeLock().lock();
 +    try {
 +      if (!shunnedMembers.containsKey(m))
 +        return false;
 +      
 +      // Make sure that the entry isn't stale...
 +      long shunTime = ((Long)shunnedMembers.get(m)).longValue();
 +      long now = System.currentTimeMillis();
 +      if (shunTime + SHUNNED_SUNSET * 1000 > now)
 +        return true;
 +      
 +      // Oh, it _is_ stale.  Remove it while we're here.
 +      endShun(m);
 +      return false;
 +    } finally {
 +      latestViewLock.writeLock().unlock();
 +    }
 +  }
 +
 +  /**
 +   * Indicate whether the given member is in the surprise member list
 +   * <P>
 +   * Unlike isShunned, this method will not cause expiry of a surprise member.
 +   * That must be done during view processing.
 +   * <p>
 +   * Like isShunned, this method holds the view lock while executing
 +   * 
 +   * Concurrency: protected by {@link #latestViewLock} ReentrantReadWriteLock
 +   * 
 +   * @param m the member in question
 +   * @return true if the given member is a surprise member
 +   */
 +  public boolean isSurpriseMember(DistributedMember m) {
 +    latestViewLock.readLock().lock();
 +    try  {
 +      if (surpriseMembers.containsKey(m)) {
 +        long birthTime = ((Long)surpriseMembers.get(m)).longValue();
 +        long now = System.currentTimeMillis();
 +        return (birthTime >= (now - this.surpriseMemberTimeout));
 +      }
 +      return false;
 +    } finally {
 +      latestViewLock.readLock().unlock();
 +    }
 +  }
 +  
 +  /**
 +   * for testing we need to be able to inject surprise members into
 +   * the view to ensure that sunsetting works properly
 +   * @param m the member ID to add
 +   * @param birthTime the millisecond clock time that the member was first seen
 +   */
 +  public void addSurpriseMemberForTesting(DistributedMember m, long birthTime) {
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("test hook is adding surprise member {} birthTime={}", m, birthTime);
 +    }
 +    latestViewLock.writeLock().lock();
 +    try {
 +      surpriseMembers.put((InternalDistributedMember)m, Long.valueOf(birthTime));
 +    } finally {
 +      latestViewLock.writeLock().unlock();
 +    }
 +  }
 +  
 +  /**
 +   * returns the surpriseMemberTimeout interval, in milliseconds
 +   */
 +  public int getSurpriseMemberTimeout() {
 +    return this.surpriseMemberTimeout;
 +  }
 +  
 +  private boolean endShun(DistributedMember m) {
 +    boolean wasShunned = (shunnedMembers.remove(m) != null);
 +    shunnedAndWarnedMembers.remove(m);
 +    return wasShunned;
 +  }
 +  
 + /**
 +   * Add the given member to the shunned list.  Also, purge any shunned
 +   * members that are really really old.
 +   * <p>
 +   * Must be called with {@link #latestViewLock} held and
 +   * the view stable.
 +   * 
 +   * @param m the member to add
 +   */
 +  protected void addShunnedMember(InternalDistributedMember m) {
 +    long deathTime = System.currentTimeMillis() - SHUNNED_SUNSET * 1000;
 +    
 +    surpriseMembers.remove(m); // for safety
 +
 +    // Update the shunned set.
 +    if (!isShunned(m)) {
 +      shunnedMembers.put(m, Long.valueOf(System.currentTimeMillis()));
 +    }
 +
 +    // Remove really really old shunned members.
 +    // First, make a copy of the old set.  New arrivals _a priori_ don't matter,
 +    // and we're going to be updating the list so we don't want to disturb
 +    // the iterator.
 +    Set oldMembers = new HashSet(shunnedMembers.entrySet());
 +    
 +    Set removedMembers = new HashSet();
 +    
 +    Iterator it = oldMembers.iterator();
 +    while (it.hasNext()) {
 +      Map.Entry e = (Map.Entry)it.next();
 +      
 +      // Key is the member.  Value is the time to remove it.
 +      long ll = ((Long)e.getValue()).longValue(); 
 +      if (ll >= deathTime) {
 +        continue; // too new.
 +      }
 +      
 +      InternalDistributedMember mm = (InternalDistributedMember)e.getKey();
 +
 +      if (latestView.contains(mm)) {
 +        // Fault tolerance: a shunned member can conceivably linger but never 
 +        // disconnect.
 +        //
 +        // We may not delete it at the time that we shun it because the view 
 +        // isn't necessarily stable.  (Note that a well-behaved cache member
 +        // will depart on its own accord, but we force the issue here.)
 +        destroyMember(mm, true, "shunned but never disconnected");
 +      }
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("Membership: finally removed shunned member entry <{}>", mm);
 +      }
 +      
 +      removedMembers.add(mm);
 +    }
 +    
 +    // removed timed-out entries from the shunned-members collections
 +    if (removedMembers.size() > 0) {
 +      it = removedMembers.iterator();
 +      while (it.hasNext()) {
 +        InternalDistributedMember idm = (InternalDistributedMember)it.next();
 +        endShun(idm);
 +      }
 +    }
 +  }
 +  
 +  
 +  /**
 +   * Retrieve thread-local data for transport to another thread in hydra
 +   */
 +  public Object getThreadLocalData() {
 +    Map result = new HashMap();
 +    return result;
 +  }
 +  
 +  /**
 +   * for testing verification purposes, this return the port for the
 +   * direct channel, or zero if there is no direct
 +   * channel
 +   */
 +  public int getDirectChannelPort() {
 +    return directChannel == null? 0 : directChannel.getPort();
 +  }
 +  
 +  /**
 +   * for mock testing this allows insertion of a DirectChannel mock
 +   */
 +  protected void setDirectChannel(DirectChannel dc) {
 +    this.directChannel = dc;
 +    this.tcpDisabled = false;
 +  }
 +  
 +  /* non-thread-owned serial channels and high priority channels are not
 +   * included
 +   */
 +  public Map getMessageState(DistributedMember member, boolean includeMulticast) {
 +    Map result = new HashMap();
 +    DirectChannel dc = directChannel;
 +    if (dc != null) {
 +      dc.getChannelStates(member, result);
 +    }
 +    services.getMessenger().getMessageState((InternalDistributedMember)member, result, includeMulticast);
 +    return result;
 +  }
 +
 +  public void waitForMessageState(DistributedMember otherMember, Map state)
 +    throws InterruptedException
 +  {
 +    if (Thread.interrupted()) throw new InterruptedException();
 +    DirectChannel dc = directChannel;
 +    if (dc != null) {
 +      dc.waitForChannelState(otherMember, state);
 +    }
 +    services.getMessenger().waitForMessageState((InternalDistributedMember)otherMember, state);
 +  }
 +  
 +  /* 
 +   * (non-Javadoc)
 +   * MembershipManager method: wait for the given member to be gone.  Throws TimeoutException if
 +   * the wait goes too long
 +   * @see com.gemstone.gemfire.distributed.internal.membership.MembershipManager#waitForDeparture(com.gemstone.gemfire.distributed.DistributedMember)
 +   */
 +  public boolean waitForDeparture(DistributedMember mbr) throws TimeoutException, InterruptedException {
 +    if (Thread.interrupted()) throw new InterruptedException();
 +    boolean result = false;
 +    DirectChannel dc = directChannel;
 +    InternalDistributedMember idm = (InternalDistributedMember)mbr;
 +    int memberTimeout = this.services.getConfig().getDistributionConfig().getMemberTimeout();
 +    long pauseTime = (memberTimeout < 1000) ? 100 : memberTimeout / 10;
 +    boolean wait;
 +    int numWaits = 0;
 +    do {
 +      wait = false;
 +      if (dc != null) {
 +        if (dc.hasReceiversFor(idm)) {
 +          wait = true;
 +        }
 +        if (wait && logger.isDebugEnabled()) {
 +          logger.info("waiting for receivers for {} to shut down", mbr);
 +        }
 +      }
 +      if (!wait) {
 +        latestViewLock.readLock().lock();
 +        try {
 +          wait = this.latestView.contains(idm);
 +        } finally {
 +          latestViewLock.readLock().unlock();
 +        }
 +        if (wait && logger.isDebugEnabled()) {
 +          logger.debug("waiting for {} to leave the membership view", mbr);
 +        }
 +      }
 +      if (!wait) {
 +        // run a message through the member's serial execution queue to ensure that all of its
 +        // current messages have been processed
 +        OverflowQueueWithDMStats serialQueue = listener.getDM().getSerialQueue(idm);
 +        if (serialQueue != null) {
 +          final boolean done[] = new boolean[1];
 +          final FlushingMessage msg = new FlushingMessage(done);
 +          serialQueue.add(new SizeableRunnable(100) {
 +            public void run() {
 +              msg.invoke();
 +            }
 +            public String toString() {
 +              return "Processing fake message";
 +            }
 +          });
 +          synchronized(done) {
 +            while (done[0] == false) {
 +              done.wait(10);
 +            }
 +            result = true;
 +          }
 +        }
 +      }
 +      if (wait) {
 +        numWaits++;
 +        if (numWaits > 40) {
 +          // waited over 4 * memberTimeout ms.  Give up at this point
 +          throw new TimeoutException("waited too long for " + idm + " to be removed");
 +        }
 +        Thread.sleep(pauseTime);
 +      }
 +    } while (wait && (dc != null && dc.isOpen())
 +        && services.getCancelCriterion().cancelInProgress()==null );
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("operations for {} should all be in the cache at this point", mbr);
 +    }
 +    return result;
 +  }
 +  
 +
 +  // TODO remove this overly complex method and replace its use with
 +  // waitForViewChange using the remote member's view ID
 +  public boolean waitForMembershipCheck(InternalDistributedMember remoteId) {
 +    boolean foundRemoteId = false;
 +    CountDownLatch currentLatch = null;
 +    // ARB: preconditions
 +    // remoteId != null
 +    latestViewLock.writeLock().lock();
 +    try {
 +      if (latestView == null) {
 +        // Not sure how this would happen, but see bug 38460.
 +        // No view?? Not found!
 +      }
 +      else if (latestView.contains(remoteId)) {
 +        // ARB: check if remoteId is already in membership view.
 +        // If not, then create a latch if needed and wait for the latch to open.
 +        foundRemoteId = true;
 +      }
 +      else if ((currentLatch = (CountDownLatch)this.memberLatch.get(remoteId)) == null) {
 +        currentLatch = new CountDownLatch(1);
 +        this.memberLatch.put(remoteId, currentLatch);
 +      }
 +    } finally {
 +      latestViewLock.writeLock().unlock();
 +    }
 +
 +    if (!foundRemoteId) {
 +      try {
 +        if (currentLatch.await(membershipCheckTimeout, TimeUnit.MILLISECONDS)) {
 +          foundRemoteId = true;
 +          // @todo 
 +          // ARB: remove latch from memberLatch map if this is the last thread waiting on latch.
 +        }
 +      }
 +      catch (InterruptedException ex) {
 +        // ARB: latch attempt was interrupted.
 +        Thread.currentThread().interrupt();
 +        logger.warn(LocalizedMessage.create(
 +            LocalizedStrings.GroupMembershipService_THE_MEMBERSHIP_CHECK_WAS_TERMINATED_WITH_AN_EXCEPTION));
 +      }
 +    }
 +
 +    // ARB: postconditions
 +    // (foundRemoteId == true) ==> (currentLatch is non-null ==> currentLatch is open)
 +    return foundRemoteId;
 +  }
 +  
 +  /* returns the cause of shutdown, if known */
 +  public Throwable getShutdownCause() {
 +    return services.getShutdownCause();
 +  }
 +
 +//  @Override
 +//  public void membershipFailure(String reason, Exception e) {
 +//

<TRUNCATED>

[13/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache/query/dunit/RemoteQueryDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache/query/dunit/RemoteQueryDUnitTest.java
index fc79893,0000000..09fd945
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache/query/dunit/RemoteQueryDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache/query/dunit/RemoteQueryDUnitTest.java
@@@ -1,1502 -1,0 +1,1502 @@@
 +/*
 + * 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.cache.query.dunit;
 +
 +import java.io.DataInput;
 +import java.io.DataOutput;
 +import java.io.IOException;
 +import java.util.Comparator;
 +import java.util.Properties;
 +
 +import com.gemstone.gemfire.DataSerializable;
 +import com.gemstone.gemfire.DataSerializer;
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.client.PoolManager;
 +import com.gemstone.gemfire.cache.query.QueryInvocationTargetException;
 +import com.gemstone.gemfire.cache.query.SelectResults;
 +import com.gemstone.gemfire.cache.query.Struct;
 +import com.gemstone.gemfire.cache.query.internal.QueryObserverAdapter;
 +import com.gemstone.gemfire.cache.query.internal.QueryObserverHolder;
 +import com.gemstone.gemfire.cache.query.internal.ResultsBag;
 +import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.cache30.CacheSerializableRunnable;
 +import com.gemstone.gemfire.cache30.CacheTestCase;
 +import com.gemstone.gemfire.cache30.ClientServerTestCase;
 +import com.gemstone.gemfire.distributed.DistributedSystem;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +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.LogWriterUtils;
 +import com.gemstone.gemfire.test.dunit.NetworkUtils;
 +import com.gemstone.gemfire.test.dunit.SerializableRunnable;
 +import com.gemstone.gemfire.test.dunit.VM;
 +import com.gemstone.gemfire.test.dunit.Wait;
 +
 +import cacheRunner.Portfolio;
 +import cacheRunner.Position;
 +
 +/**
 + * Tests remote (client/server) query execution.
 + *
 + * @author Barry Oglesby
 + * @author Asif
 + * @since 5.0.1
 + */
 +public class RemoteQueryDUnitTest extends CacheTestCase {
 +
 +  /** The port on which the bridge server was started in this VM */
 +  private static int bridgeServerPort;
 +
 +  public RemoteQueryDUnitTest(String name) {
 +    super(name);
 +  }
 +
 +  ////////  Test Methods
 +
 +  @Override
 +  public void setUp() throws Exception {
 +    super.setUp();
 +    disconnectAllFromDS();
 +  }
 +
 +  @Override
 +  protected final void postTearDownCacheTestCase() throws Exception {
 +    disconnectAllFromDS();
 +  }
 +
 +  /**
 +   * Tests remote predicate query execution.
 +   */
 +  public void testRemotePredicateQueries() throws CacheException {
 +
 +    final String name = this.getName();
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    final int numberOfEntries = 100;
 +
 +    // Start server
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +        public void run2() throws CacheException {
 +          Properties config = new Properties();
 +          config.setProperty("locators", "localhost["+DistributedTestUtils.getDUnitLocatorPort()+"]");
 +          system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.LOCAL);
 +          createRegion(name, factory.create());
 +          Wait.pause(1000);
 +          try {
 +            startBridgeServer(0, false);
 +          } catch (Exception ex) {
 +            Assert.fail("While starting CacheServer", ex);
 +          }
 +        }
 +      });
 +
 +    // Initialize server region
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +        public void run2() throws CacheException {
 +          Region region = getRootRegion().getSubregion(name);
 +          for (int i=0; i<numberOfEntries; i++) {
 +            region.put("key-"+i, new TestObject(i, "ibm"));
 +          }
 +        }
 +      });
 +
 +    // Create client region
-     final int port = vm0.invokeInt(RemoteQueryDUnitTest.class, "getCacheServerPort");
++    final int port = vm0.invoke(() -> RemoteQueryDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +    vm1.invoke(new CacheSerializableRunnable("Create region") {
 +        public void run2() throws CacheException {
 +          Properties config = new Properties();
 +          config.setProperty("mcast-port", "0");
 +          system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +          getCache();
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.LOCAL);
 +          ClientServerTestCase.configureConnectionPool(factory, host0, port,-1, true, -1, -1, null);
 +          createRegion(name, factory.create());
 +        }
 +      });
 +
 +    // Execute client queries
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +        public void run2() throws CacheException {
 +          Region region = getRootRegion().getSubregion(name);
 +          String queryString = null;
 +          SelectResults results = null;
 +
 +          queryString = "ticker = 'ibm'";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(numberOfEntries, results.size());
 +          assertTrue(results.getClass() == ResultsBag.class);
 +          assertTrue(results.getCollectionType().allowsDuplicates() && !results.getCollectionType().getElementType().isStructType());
 +
 +          queryString = "ticker = 'IBM'";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(0, results.size());
 +          assertTrue(results.getClass() == ResultsBag.class);
 +          assertTrue(results.getCollectionType().allowsDuplicates() && !results.getCollectionType().getElementType().isStructType());
 +
 +          queryString = "price > 49";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(numberOfEntries/2, results.size());
 +          assertTrue(results.getClass() == ResultsBag.class);
 +          assertTrue(results.getCollectionType().allowsDuplicates() && !results.getCollectionType().getElementType().isStructType());
 +
 +          queryString = "price = 50";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(1, results.size());
 +          assertTrue(results.getClass() == ResultsBag.class);
 +          assertTrue(results.getCollectionType().allowsDuplicates() && !results.getCollectionType().getElementType().isStructType());
 +
 +          queryString = "ticker = 'ibm' and price = 50";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(1, results.size());
 +          assertTrue(results.getClass() == ResultsBag.class);
 +          assertTrue(results.getCollectionType().allowsDuplicates() && !results.getCollectionType().getElementType().isStructType());
 +
 +          /* Non-distinct order by query not yet supported
 +          queryString = "id < 101 ORDER BY id";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            fail("Failed executing " + queryString, e);
 +        }
 +          assertEquals(100, results.size());
 +          assertTrue(results instanceof ResultsCollectionWrapper);
 +          IdComparator comparator = new IdComparator();
 +          Object[] resultsArray = results.toArray();
 +          for (int i=0; i<resultsArray.length; i++) {
 +            if (i+1 != resultsArray.length) {
 +              // The id of the current element in the result set must be less
 +              // than the id of the next one to pass.
 +              assertTrue("The id for " + resultsArray[i] + " should be less than the id for " + resultsArray[i+1], comparator.compare(resultsArray[i], resultsArray[i+1]) == -1);
 +            }
 +          }
 +          */
 +        }
 +      });
 +
 +
 +    // Stop server
 +    vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +      public void run() {
 +        stopBridgeServer(getCache());
 +      }
 +    });
 +  }
 +
 +  /**
 +   * Tests remote import query execution.
 +   */
 +  public void testRemoteImportQueries() throws CacheException {
 +
 +    final String name = this.getName();
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    final int numberOfEntries = 100;
 +
 +    // Start server
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +        public void run2() throws CacheException {
 +          Properties config = new Properties();
 +          config.setProperty("locators", "localhost["+DistributedTestUtils.getDUnitLocatorPort()+"]");
 +          system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.LOCAL);
 +          createRegion(name, factory.create());
 +          Wait.pause(1000);
 +          try {
 +            startBridgeServer(0, false);
 +          } catch (Exception ex) {
 +            Assert.fail("While starting CacheServer", ex);
 +          }
 +        }
 +      });
 +
 +    // Initialize server region
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +        public void run2() throws CacheException {
 +          Region region = getRootRegion().getSubregion(name);
 +          for (int i=0; i<numberOfEntries; i++) {
 +            region.put("key-"+i, new TestObject(i, "ibm"));
 +          }
 +        }
 +      });
 +
 +    // Create client region
-     final int port = vm0.invokeInt(RemoteQueryDUnitTest.class, "getCacheServerPort");
++    final int port = vm0.invoke(() -> RemoteQueryDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +    vm1.invoke(new CacheSerializableRunnable("Create region") {
 +        public void run2() throws CacheException {
 +          Properties config = new Properties();
 +          config.setProperty("mcast-port", "0");
 +          system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +          getCache();
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.LOCAL);
 +          
 +          ClientServerTestCase.configureConnectionPool(factory, host0, port,-1, true, -1, -1, null);
 +          createRegion(name, factory.create());
 +        }
 +      });
 +
 +    // Execute client queries
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +        public void run2() throws CacheException {
 +          Region region = getRootRegion().getSubregion(name);
 +          String queryString = null;
 +          SelectResults results = null;
 +
 +          queryString = "import com.gemstone.gemfire.admin.RemoteQueryDUnitTest.TestObject; select distinct * from " + region.getFullPath();
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(numberOfEntries, results.size());
 +          assertTrue(!results.getCollectionType().allowsDuplicates());
 +
 +          queryString = "import com.gemstone.gemfire.admin.RemoteQueryDUnitTest.TestObject; select distinct * from " + region.getFullPath() + " where ticker = 'ibm'";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(numberOfEntries, results.size());
 +          assertTrue(!results.getCollectionType().allowsDuplicates());
 +
 +          queryString = "import com.gemstone.gemfire.admin.RemoteQueryDUnitTest.TestObject; select distinct * from " + region.getFullPath() + " where ticker = 'IBM'";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(0, results.size());
 +          assertTrue(!results.getCollectionType().allowsDuplicates());
 +
 +          queryString = "import com.gemstone.gemfire.admin.RemoteQueryDUnitTest.TestObject; select distinct * from " + region.getFullPath() + " where price > 49";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(numberOfEntries/2, results.size());
 +          assertTrue(!results.getCollectionType().allowsDuplicates());
 +
 +          queryString = "import com.gemstone.gemfire.admin.RemoteQueryDUnitTest.TestObject; select distinct * from " + region.getFullPath() + " where price = 50";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(1, results.size());
 +          assertTrue(!results.getCollectionType().allowsDuplicates());
 +
 +          queryString = "import com.gemstone.gemfire.admin.RemoteQueryDUnitTest.TestObject; select distinct * from " + region.getFullPath() + " where ticker = 'ibm' and price = 50";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(1, results.size());
 +          assertTrue(!results.getCollectionType().allowsDuplicates());
 +        }
 +      });
 +
 +
 +    // Stop server
 +    vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +      public void run() {
 +        stopBridgeServer(getCache());
 +      }
 +    });
 +  }
 +
 +  /**
 +   * Tests remote struct query execution.
 +   */
 +  public void testRemoteStructQueries() throws CacheException {
 +
 +    final String name = this.getName();
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    final int numberOfEntries = 100;
 +
 +    // Start server
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +        public void run2() throws CacheException {
 +          Properties config = new Properties();
 +          config.setProperty("locators", "localhost["+DistributedTestUtils.getDUnitLocatorPort()+"]");
 +          system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.LOCAL);
 +          createRegion(name, factory.create());
 +          Wait.pause(1000);
 +          try {
 +            startBridgeServer(0, false);
 +          } catch (Exception ex) {
 +            Assert.fail("While starting CacheServer", ex);
 +          }
 +        }
 +      });
 +
 +    // Initialize server region
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +        public void run2() throws CacheException {
 +          Region region = getRootRegion().getSubregion(name);
 +          for (int i=0; i<numberOfEntries; i++) {
 +            region.put("key-"+i, new TestObject(i, "ibm"));
 +          }
 +        }
 +      });
 +
 +    // Create client region
-     final int port = vm0.invokeInt(RemoteQueryDUnitTest.class, "getCacheServerPort");
++    final int port = vm0.invoke(() -> RemoteQueryDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +    vm1.invoke(new CacheSerializableRunnable("Create region") {
 +        public void run2() throws CacheException {
 +          Properties config = new Properties();
 +          config.setProperty("mcast-port", "0");
 +          system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +          getCache();
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.LOCAL);
 +          ClientServerTestCase.configureConnectionPool(factory, host0, port,-1, true, -1, -1, null);
 +          createRegion(name, factory.create());
 +        }
 +      });
 +
 +    // Execute client queries
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +        public void run2() throws CacheException {
 +          Region region = getRootRegion().getSubregion(name);
 +          String queryString = null;
 +          SelectResults results = null;
 +
 +          queryString = "import com.gemstone.gemfire.admin.RemoteQueryDUnitTest.TestObject; select distinct ticker, price from " + region.getFullPath();
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(numberOfEntries, results.size());
 +          assertTrue(!results.getCollectionType().allowsDuplicates() && results.getCollectionType().getElementType().isStructType());
 +
 +          queryString = "import com.gemstone.gemfire.admin.RemoteQueryDUnitTest.TestObject; select distinct ticker, price from " + region.getFullPath() + " where ticker = 'ibm'";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(numberOfEntries, results.size());
 +          assertTrue(!results.getCollectionType().allowsDuplicates() && results.getCollectionType().getElementType().isStructType());
 +
 +          queryString = "import com.gemstone.gemfire.admin.RemoteQueryDUnitTest.TestObject; select distinct ticker, price from " + region.getFullPath() + " where ticker = 'IBM'";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(0, results.size());
 +          assertTrue(!results.getCollectionType().allowsDuplicates() && results.getCollectionType().getElementType().isStructType());
 +
 +          queryString = "import com.gemstone.gemfire.admin.RemoteQueryDUnitTest.TestObject; select distinct ticker, price from " + region.getFullPath() + " where price > 49";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(numberOfEntries/2, results.size());
 +          assertTrue(!results.getCollectionType().allowsDuplicates() && results.getCollectionType().getElementType().isStructType());
 +
 +          queryString = "import com.gemstone.gemfire.admin.RemoteQueryDUnitTest.TestObject; select distinct ticker, price from " + region.getFullPath() + " where price = 50";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(1, results.size());
 +          assertTrue(!results.getCollectionType().allowsDuplicates() && results.getCollectionType().getElementType().isStructType());
 +
 +          queryString = "import com.gemstone.gemfire.admin.RemoteQueryDUnitTest.TestObject; select distinct ticker, price from " + region.getFullPath() + " where ticker = 'ibm' and price = 50";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(1, results.size());
 +          assertTrue(!results.getCollectionType().allowsDuplicates() && results.getCollectionType().getElementType().isStructType());
 +        }
 +      });
 +
 +    // Stop server
 +    vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +      public void run() {
 +        stopBridgeServer(getCache());
 +      }
 +    });
 +  }
 +
 +  /**
 +   * Tests remote complex query execution.
 +   */
 +  public void __testRemoteComplexQueries() throws CacheException {
 +
 +    final String name = this.getName();
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +//    final int numberOfEntries = 100;
 +
 +    // Start server
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +        public void run2() throws CacheException {
 +          Properties config = new Properties();
 +          config.setProperty("locators", "localhost["+DistributedTestUtils.getDUnitLocatorPort()+"]");
 +          system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.LOCAL);
 +          createRegion(name, factory.create());
 +          Wait.pause(1000);
 +          try {
 +            startBridgeServer(0, false);
 +          } catch (Exception ex) {
 +            Assert.fail("While starting CacheServer", ex);
 +          }
 +        }
 +      });
 +
 +    // Initialize server region
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +        public void run2() throws CacheException {
 +          Region region = getRootRegion().getSubregion(name);
 +          Portfolio portfolio = null;
 +          Position position1 = null;
 +          Position position2 = null;
 +          Properties portfolioProperties= null;
 +          Properties position1Properties = null;
 +          Properties position2Properties = null;
 +
 +          // Create portfolio 1
 +          portfolio = new Portfolio();
 +          portfolioProperties = new Properties();
 +          portfolioProperties.put("id", new Integer(1));
 +          portfolioProperties.put("type", "type1");
 +          portfolioProperties.put("status", "active");
 +
 +          position1 = new Position();
 +          position1Properties = new Properties();
 +          position1Properties.put("secId", "SUN");
 +          position1Properties.put("qty", new Double(34000.0));
 +          position1Properties.put("mktValue", new Double(24.42));
 +          position1.init(position1Properties);
 +          portfolioProperties.put("position1", position1);
 +
 +          position2 = new Position();
 +          position2Properties = new Properties();
 +          position2Properties.put("secId", "IBM");
 +          position2Properties.put("qty", new Double(8765.0));
 +          position2Properties.put("mktValue", new Double(34.29));
 +          position2.init(position2Properties);
 +          portfolioProperties.put("position2", position2);
 +
 +          portfolio.init(portfolioProperties);
 +          region.put(new Integer(1), portfolio);
 +
 +          // Create portfolio 2
 +          portfolio = new Portfolio();
 +          portfolioProperties = new Properties();
 +          portfolioProperties.put("id", new Integer(2));
 +          portfolioProperties.put("type", "type2");
 +          portfolioProperties.put("status", "inactive");
 +
 +          position1 = new Position();
 +          position1Properties = new Properties();
 +          position1Properties.put("secId", "YHOO");
 +          position1Properties.put("qty", new Double(9834.0));
 +          position1Properties.put("mktValue", new Double(12.925));
 +          position1.init(position1Properties);
 +          portfolioProperties.put("position1", position1);
 +
 +          position2 = new Position();
 +          position2Properties = new Properties();
 +          position2Properties.put("secId", "GOOG");
 +          position2Properties.put("qty", new Double(12176.0));
 +          position2Properties.put("mktValue", new Double(21.972));
 +          position2.init(position2Properties);
 +          portfolioProperties.put("position2", position2);
 +
 +          portfolio.init(portfolioProperties);
 +          region.put(new Integer(2), portfolio);
 +
 +          // Create portfolio 3
 +          portfolio = new Portfolio();
 +          portfolioProperties = new Properties();
 +          portfolioProperties.put("id", new Integer(3));
 +          portfolioProperties.put("type", "type3");
 +          portfolioProperties.put("status", "active");
 +
 +          position1 = new Position();
 +          position1Properties = new Properties();
 +          position1Properties.put("secId", "MSFT");
 +          position1Properties.put("qty", new Double(98327.0));
 +          position1Properties.put("mktValue", new Double(23.32));
 +          position1.init(position1Properties);
 +          portfolioProperties.put("position1", position1);
 +
 +          position2 = new Position();
 +          position2Properties = new Properties();
 +          position2Properties.put("secId", "AOL");
 +          position2Properties.put("qty", new Double(978.0));
 +          position2Properties.put("mktValue", new Double(40.373));
 +          position2.init(position2Properties);
 +          portfolioProperties.put("position2", position2);
 +
 +          portfolio.init(portfolioProperties);
 +          region.put(new Integer(3), portfolio);
 +
 +          // Create portfolio 4
 +          portfolio = new Portfolio();
 +          portfolioProperties = new Properties();
 +          portfolioProperties.put("id", new Integer(4));
 +          portfolioProperties.put("type", "type1");
 +          portfolioProperties.put("status", "inactive");
 +
 +          position1 = new Position();
 +          position1Properties = new Properties();
 +          position1Properties.put("secId", "APPL");
 +          position1Properties.put("qty", new Double(90.0));
 +          position1Properties.put("mktValue", new Double(67.356572));
 +          position1.init(position1Properties);
 +          portfolioProperties.put("position1", position1);
 +
 +          position2 = new Position();
 +          position2Properties = new Properties();
 +          position2Properties.put("secId", "ORCL");
 +          position2Properties.put("qty", new Double(376.0));
 +          position2Properties.put("mktValue", new Double(101.34));
 +          position2.init(position2Properties);
 +          portfolioProperties.put("position2", position2);
 +
 +          portfolio.init(portfolioProperties);
 +          region.put(new Integer(4), portfolio);
 +        }
 +      });
 +
 +    // Create client region
-     final int port = vm0.invokeInt(RemoteQueryDUnitTest.class, "getCacheServerPort");
++    final int port = vm0.invoke(() -> RemoteQueryDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +    vm1.invoke(new CacheSerializableRunnable("Create region") {
 +        public void run2() throws CacheException {
 +          Properties config = new Properties();
 +          config.setProperty("mcast-port", "0");
 +          system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +          getCache();
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.LOCAL);
 +          ClientServerTestCase.configureConnectionPool(factory, host0, port,-1, true, -1, -1, null);
 +          createRegion(name, factory.create());
 +        }
 +      });
 +
 +    // Execute client queries
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +        public void run2() throws CacheException {
 +          Region region = getRootRegion().getSubregion(name);
 +          String queryString = null;
 +          SelectResults results = null;
 +
 +          queryString =
 +            "IMPORT cacheRunner.Position; " +
 +            "SELECT DISTINCT id, status FROM " + region.getFullPath() +
 +            "WHERE NOT (SELECT DISTINCT * FROM positions.values posnVal TYPE Position " +
 +            "WHERE posnVal.secId='AOL' OR posnVal.secId='SAP').isEmpty";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          LogWriterUtils.getLogWriter().fine("size: " + results.size());
 +          //assertEquals(numberOfEntries, results.size());
 +          assertTrue(!results.getCollectionType().allowsDuplicates() && results.getCollectionType().getElementType().isStructType());
 +        }
 +      });
 +
 +
 +    // Stop server
 +    vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +      public void run() {
 +        stopBridgeServer(getCache());
 +      }
 +    });
 +  }
 +
 +  /**
 +   * Tests remote full region query execution.
 +   */
 +  public void testRemoteFullRegionQueries() throws CacheException {
 +
 +    final String name = this.getName();
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    final int numberOfEntries = 100;
 +
 +    // Start server
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +        public void run2() throws CacheException {
 +          Properties config = new Properties();
 +          config.setProperty("locators", "localhost["+DistributedTestUtils.getDUnitLocatorPort()+"]");
 +          system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.LOCAL);
 +          createRegion(name, factory.create());
 +          Wait.pause(1000);
 +          try {
 +            startBridgeServer(0, false);
 +          } catch (Exception ex) {
 +            Assert.fail("While starting CacheServer", ex);
 +          }
 +        }
 +      });
 +
 +    // Initialize server region
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +        public void run2() throws CacheException {
 +          Region region = getRootRegion().getSubregion(name);
 +          for (int i=0; i<numberOfEntries; i++) {
 +            region.put("key-"+i, new TestObject(i, "ibm"));
 +          }
 +        }
 +      });
 +
 +    // Create client region
-     final int port = vm0.invokeInt(RemoteQueryDUnitTest.class, "getCacheServerPort");
++    final int port = vm0.invoke(() -> RemoteQueryDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +    vm1.invoke(new CacheSerializableRunnable("Create region") {
 +        public void run2() throws CacheException {
 +          Properties config = new Properties();
 +          config.setProperty("mcast-port", "0");
 +          system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +          getCache();
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.LOCAL);
 +          ClientServerTestCase.configureConnectionPool(factory, host0, port,-1, true, -1, -1, null);
 +          createRegion(name, factory.create());
 +        }
 +      });
 +
 +    // Execute client queries
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +        public void run2() throws CacheException {
 +          Region region = getRootRegion().getSubregion(name);
 +          String queryString = null;
 +          SelectResults results = null;
 +          Comparator comparator = null;
 +          Object[] resultsArray = null;
 +
 +          // value query
 +          queryString = "SELECT DISTINCT itr.value FROM " + region.getFullPath() + ".entries itr where itr.key = 'key-1'";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(1, results.size());
 +          assertTrue(!results.getCollectionType().allowsDuplicates());
 +          assertTrue(results.asList().get(0) instanceof TestObject);
 +
 +          // key query
 +          queryString = "SELECT DISTINCT itr.key FROM " + region.getFullPath() + ".entries itr where itr.key = 'key-1'";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(1, results.size());
 +          assertTrue(!results.getCollectionType().allowsDuplicates());
 +          assertEquals("key-1", results.asList().get(0));
 +
 +          // order by value query
 +          queryString = "SELECT DISTINCT * FROM " + region.getFullPath() + " WHERE id < 101 ORDER BY id";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +        }
 +          assertEquals(numberOfEntries, results.size());
 +          // All order-by query results are stored in a ResultsCollectionWrapper
 +          // wrapping a list, so the assertion below is not correct even though
 +          // it should be.
 +          //assertTrue(!results.getCollectionType().allowsDuplicates());
 +          assertTrue(results.getCollectionType().isOrdered());
 +          comparator = new IdComparator();
 +          resultsArray = results.toArray();
 +          for (int i=0; i<resultsArray.length; i++) {
 +            if (i+1 != resultsArray.length) {
 +              // The id of the current element in the result set must be less
 +              // than the id of the next one to pass.
 +              assertTrue("The id for " + resultsArray[i] + " should be less than the id for " + resultsArray[i+1], comparator.compare(resultsArray[i], resultsArray[i+1]) == -1);
 +            }
 +          }
 +
 +          // order by struct query
 +          queryString = "SELECT DISTINCT id, ticker, price FROM " + region.getFullPath() + " WHERE id < 101 ORDER BY id";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(numberOfEntries, results.size());
 +          // All order-by query results are stored in a ResultsCollectionWrapper
 +          // wrapping a list, so the assertion below is not correct even though
 +          // it should be.
 +          //assertTrue(!results.getCollectionType().allowsDuplicates());
 +          assertTrue(results.getCollectionType().isOrdered());
 +          comparator = new StructIdComparator();
 +          resultsArray = results.toArray();
 +          for (int i=0; i<resultsArray.length; i++) {
 +            if (i+1 != resultsArray.length) {
 +              // The id of the current element in the result set must be less
 +              // than the id of the next one to pass.
 +              assertTrue("The id for " + resultsArray[i] + " should be less than the id for " + resultsArray[i+1], comparator.compare(resultsArray[i], resultsArray[i+1]) == -1);
 +            }
 +          }
 +
 +          // size query
 +          queryString = "(SELECT DISTINCT * FROM " + region.getFullPath() + " WHERE id < 101).size";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(1, results.size());
 +          Object result = results.iterator().next();
 +          assertTrue(result instanceof Integer);
 +          int resultInt = ((Integer) result).intValue();
 +          assertEquals(resultInt, 100);
 +
 +          // query with leading/trailing spaces
 +          queryString = " SELECT DISTINCT itr.key FROM " + region.getFullPath() + ".entries itr where itr.key = 'key-1' ";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(1, results.size());
 +          assertEquals("key-1", results.asList().get(0));
 +        }
 +      });
 +
 +    // Stop server
 +    vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +      public void run() {
 +        stopBridgeServer(getCache());
 +      }
 +    });
 +  }
 +
 +  /**
 +   * Tests remote join query execution.
 +   */
 +  public void testRemoteJoinRegionQueries() throws CacheException {
 +
 +    final String name = this.getName();
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    final int numberOfEntries = 100;
 +
 +    // Start server
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +        public void run2() throws CacheException {
 +          Properties config = new Properties();
 +          config.setProperty("locators", "localhost["+DistributedTestUtils.getDUnitLocatorPort()+"]");
 +          system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.LOCAL);
 +          createRegion(name+"1", factory.create());
 +          createRegion(name+"2", factory.create());
 +          Wait.pause(1000);
 +          try {
 +            startBridgeServer(0, false);
 +          } catch (Exception ex) {
 +            Assert.fail("While starting CacheServer", ex);
 +          }
 +        }
 +      });
 +
 +    // Initialize server region
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +        public void run2() throws CacheException {
 +          Region region1 = getRootRegion().getSubregion(name+"1");
 +          for (int i=0; i<numberOfEntries; i++) {
 +            region1.put("key-"+i, new TestObject(i, "ibm"));
 +          }
 +          Region region2 = getRootRegion().getSubregion(name+"2");
 +          for (int i=0; i<numberOfEntries; i++) {
 +            region2.put("key-"+i, new TestObject(i, "ibm"));
 +          }
 +        }
 +      });
 +
 +    // Create client region
-     final int port = vm0.invokeInt(RemoteQueryDUnitTest.class, "getCacheServerPort");
++    final int port = vm0.invoke(() -> RemoteQueryDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +    vm1.invoke(new CacheSerializableRunnable("Create region") {
 +        public void run2() throws CacheException {
 +          Properties config = new Properties();
 +          config.setProperty("mcast-port", "0");
 +          system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +          getCache();
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.LOCAL);
 +          ClientServerTestCase.configureConnectionPool(factory, host0, port,-1, true, -1, -1, null);
 +          createRegion(name+"1", factory.create());
 +          createRegion(name+"2", factory.create());
 +        }
 +      });
 +
 +    // Execute client queries
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +        public void run2() throws CacheException {
 +          Region region1 = getRootRegion().getSubregion(name+"1");
 +          Region region2 = getRootRegion().getSubregion(name+"2");
 +          String queryString = null;
 +          SelectResults results = null;
 +
 +          queryString =
 +            "select distinct a, b.price from " + region1.getFullPath() + " a, " + region2.getFullPath() + " b where a.price = b.price";
 +          try {
 +            results = region1.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(numberOfEntries, results.size());
 +          assertTrue(!results.getCollectionType().allowsDuplicates() && results.getCollectionType().getElementType().isStructType());
 +
 +          queryString =
 +            "select distinct a, b.price from " + region1.getFullPath() + " a, " + region2.getFullPath() + " b where a.price = b.price and a.price = 50";
 +          try {
 +            results = region1.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(1, results.size());
 +          assertTrue(!results.getCollectionType().allowsDuplicates() && results.getCollectionType().getElementType().isStructType());
 +        }
 +      });
 +
 +          // Stop server
 +    vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +      public void run() {
 +        stopBridgeServer(getCache());
 +      }
 +    });
 +  }
 +
 +  /**
 +   * Tests remote query execution using a BridgeClient as the CacheWriter
 +   * and CacheLoader.
 +   */
 +  public void testRemoteBridgeClientQueries() throws CacheException {
 +
 +    final String name = this.getName();
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    final int numberOfEntries = 100;
 +
 +    // Start server
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +        public void run2() throws CacheException {
 +          Properties config = new Properties();
 +          config.setProperty("locators", "localhost["+DistributedTestUtils.getDUnitLocatorPort()+"]");
 +          system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.LOCAL);
 +          createRegion(name, factory.create());
 +          try {
 +            startBridgeServer(0, false);
 +          } catch (Exception ex) {
 +            Assert.fail("While starting CacheServer", ex);
 +          }
 +        }
 +      });
 +
 +    // Initialize server region
 +    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +        public void run2() throws CacheException {
 +          Region region = getRootRegion().getSubregion(name);
 +          for (int i=0; i<numberOfEntries; i++) {
 +            region.put("key-"+i, new TestObject(i, "ibm"));
 +          }
 +        }
 +      });
 +
-     final int port = vm0.invokeInt(RemoteQueryDUnitTest.class, "getCacheServerPort");
++    final int port = vm0.invoke(() -> RemoteQueryDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +
 +    // Create client region in VM1
 +    vm1.invoke(new CacheSerializableRunnable("Create region") {
 +        public void run2() throws CacheException {
 +          Properties config = new Properties();
 +          config.setProperty("mcast-port", "0");
 +          system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +          PoolManager.createFactory().addServer(host0, port).setSubscriptionEnabled(true).create("clientPool");
 +          getCache();
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.LOCAL);
 +          factory.setPoolName("clientPool");
 +          createRegion(name, factory.create());
 +        }
 +      });
 +
 +    // Create client region in VM2
 +    vm2.invoke(new CacheSerializableRunnable("Create region") {
 +        public void run2() throws CacheException {
 +          Properties config = new Properties();
 +          config.setProperty("mcast-port", "0");
 +          system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +          PoolManager.createFactory().addServer(host0, port).setSubscriptionEnabled(true).create("clientPool");
 +          getCache();
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.LOCAL);
 +          factory.setPoolName("clientPool");
 +          createRegion(name, factory.create());
 +        }
 +      });
 +
 +    // Execute client queries in VM1
 +    vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +        public void run2() throws CacheException {
 +          Region region = getRootRegion().getSubregion(name);
 +          String queryString = null;
 +          SelectResults results = null;
 +
 +          queryString = "SELECT DISTINCT itr.value FROM " + region.getFullPath() + ".entries itr where itr.key = 'key-1'";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(1, results.size());
 +          assertTrue(!results.getCollectionType().allowsDuplicates() && !results.getCollectionType().getElementType().isStructType());
 +          assertTrue(results.asList().get(0) instanceof TestObject);
 +
 +          queryString = "SELECT DISTINCT itr.key FROM " + region.getFullPath() + ".entries itr where itr.key = 'key-1'";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(1, results.size());
 +          assertTrue(!results.getCollectionType().allowsDuplicates() && !results.getCollectionType().getElementType().isStructType());
 +          assertEquals("key-1", results.asList().get(0));
 +        }
 +      });
 +
 +    // Execute client queries in VM2
 +    vm2.invoke(new CacheSerializableRunnable("Execute queries") {
 +        public void run2() throws CacheException {
 +          Region region = getRootRegion().getSubregion(name);
 +          String queryString = null;
 +          SelectResults results = null;
 +
 +          queryString = "SELECT DISTINCT itr.value FROM " + region.getFullPath() + ".entries itr where itr.key = 'key-1'";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(1, results.size());
 +          assertTrue(!results.getCollectionType().allowsDuplicates() && !results.getCollectionType().getElementType().isStructType());
 +          assertTrue(results.asList().get(0) instanceof TestObject);
 +
 +          queryString = "SELECT DISTINCT itr.key FROM " + region.getFullPath() + ".entries itr where itr.key = 'key-1'";
 +          try {
 +            results = region.query(queryString);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString, e);
 +          }
 +          assertEquals(1, results.size());
 +          assertTrue(!results.getCollectionType().allowsDuplicates() && !results.getCollectionType().getElementType().isStructType());
 +          assertEquals("key-1", results.asList().get(0));
 +        }
 +      });
 +
 +      // Close client VM1
 +      vm1.invoke(new CacheSerializableRunnable("Close client") {
 +        public void run2() throws CacheException {
 +          Region region = getRootRegion().getSubregion(name);
 +          region.close();
 +          PoolManager.find("clientPool").destroy();
 +        }
 +      });
 +
 +      // Close client VM2
 +      vm2.invoke(new CacheSerializableRunnable("Close client") {
 +        public void run2() throws CacheException {
 +          Region region = getRootRegion().getSubregion(name);
 +          region.close();
 +          PoolManager.find("clientPool").destroy();
 +        }
 +      });
 +
 +    // Stop server
 +    vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +      public void run() {
 +        stopBridgeServer(getCache());
 +      }
 +    });
 +  }
 +
 +
 +  /**
 +   * This the dunit test for the bug no : 36434
 +   * @throws Exception
 +   */
 +
 +   public void testBug36434() throws Exception
 +   {
 +     final String name = this.getName();
 +     final Host host = Host.getHost(0);
 +     VM vm0 = host.getVM(0);
 +     VM vm1 = host.getVM(1);
 +
 +     final int numberOfEntries = 100;
 +
 +     // Start server
 +     vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +         public void run2() throws CacheException {
 +           Properties config = new Properties();
 +           config.setProperty("locators", "localhost["+DistributedTestUtils.getDUnitLocatorPort()+"]");
 +           system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +           AttributesFactory factory = new AttributesFactory();
 +           factory.setScope(Scope.LOCAL);
 +           createRegion(name, factory.createRegionAttributes());
 +           try {
 +             startBridgeServer(0, false);
 +           } catch (Exception ex) {
 +             Assert.fail("While starting CacheServer", ex);
 +           }
 +         }
 +       });
 +
 +     // Initialize server region
 +     vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +         public void run2() throws CacheException {
 +           Region region = getRootRegion().getSubregion(name);
 +           for (int i=0; i<numberOfEntries; i++) {
 +             region.put("key-"+i, new TestObject(i, "ibm"));
 +           }
 +         }
 +       });
 +
-      final int port = vm0.invokeInt(RemoteQueryDUnitTest.class, "getCacheServerPort");
++     final int port = vm0.invoke(() -> RemoteQueryDUnitTest.getCacheServerPort());
 +     final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +
 +     // Create client region in VM1
 +     vm1.invoke(new CacheSerializableRunnable("Create region") {
 +         public void run2() throws CacheException {
 +           Properties config = new Properties();
 +           config.setProperty("mcast-port", "0");
 +           system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +           PoolManager.createFactory().addServer(host0, port).setSubscriptionEnabled(true).create("clientPool");
 +           getCache();
 +           AttributesFactory factory = new AttributesFactory();
 +           factory.setScope(Scope.LOCAL);
 +           factory.setPoolName("clientPool");
 +           createRegion(name, factory.createRegionAttributes());
 +         }
 +       });
 +
 +
 +     // Execute client queries in VM1
 +     vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +         public void run2() throws CacheException {
 +           Region region = getRootRegion().getSubregion(name);
 +           String queryStrings [] ={"id<9", "selection<9", "important<9","\"select\"<9" };
 +           for(int i =0 ; i <queryStrings.length;++i) {
 +             SelectResults results = null;
 +
 +             try {
 +               results = region.query(queryStrings[i]);
 +             } catch (Exception e) {
 +               Assert.fail("Failed executing " + queryStrings[i], e);
 +             }
 +             assertEquals(9, results.size());
 +             String msg = "results expected to be instance of ResultsBag,"
 +               + " but was found to be is instance of '";
 +             assertTrue(msg + results.getClass().getName() + "'",
 +                        results instanceof ResultsBag);
 +             assertTrue(results.asList().get(0) instanceof TestObject);
 +           }
 +         }
 +       });
 +
 +       // Close client VM1
 +       vm1.invoke(new CacheSerializableRunnable("Close client") {
 +         public void run2() throws CacheException {
 +           Region region = getRootRegion().getSubregion(name);
 +           region.close();
 +           PoolManager.find("clientPool").destroy();
 +         }
 +       });
 +
 +
 +     // Stop server
 +     vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +       public void run() {
 +         stopBridgeServer(getCache());
 +       }
 +     });
 +
 +
 +
 +   }
 +
 +  /**
 +    * This the dunit test for the bug no : 36969
 +    * @throws Exception
 +    */
 +
 +    public void testBug36969() throws Exception
 +    {
 +      final String name = this.getName();
 +      final Host host = Host.getHost(0);
 +      VM vm0 = host.getVM(0);
 +      VM vm1 = host.getVM(1);
 +
 +      final int numberOfEntries = 100;
 +
 +      // Start server
 +      vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +          public void run2() throws CacheException {
 +            Properties config = new Properties();
 +            config.setProperty("locators", "localhost["+DistributedTestUtils.getDUnitLocatorPort()+"]");
 +            system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +            AttributesFactory factory = new AttributesFactory();
 +            factory.setScope(Scope.LOCAL);
 +            final Region region = createRegion(name, factory.createRegionAttributes());
 +            QueryObserverHolder.setInstance(new QueryObserverAdapter() {
 +              public void afterQueryEvaluation(Object result) {
 +                //Destroy the region in the test
 +                region.close();
 +              }
 +
 +            });
 +            try {
 +              startBridgeServer(0, false);
 +            } catch (Exception ex) {
 +              Assert.fail("While starting CacheServer", ex);
 +            }
 +          }
 +        });
 +
 +      // Initialize server region
 +      vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +          public void run2() throws CacheException {
 +            Region region = getRootRegion().getSubregion(name);
 +            for (int i=0; i<numberOfEntries; i++) {
 +              region.put("key-"+i, new TestObject(i, "ibm"));
 +            }
 +          }
 +        });
 +
-       final int port = vm0.invokeInt(RemoteQueryDUnitTest.class, "getCacheServerPort");
++      final int port = vm0.invoke(() -> RemoteQueryDUnitTest.getCacheServerPort());
 +      final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
 +
 +      // Create client region in VM1
 +      vm1.invoke(new CacheSerializableRunnable("Create region") {
 +          public void run2() throws CacheException {
 +            Properties config = new Properties();
 +            config.setProperty("mcast-port", "0");
 +            system = (InternalDistributedSystem) DistributedSystem.connect(config);
 +            PoolManager.createFactory().addServer(host0, port).setSubscriptionEnabled(true).create("clientPool");
 +            getCache();
 +            AttributesFactory factory = new AttributesFactory();
 +            factory.setScope(Scope.LOCAL);
 +            factory.setPoolName("clientPool");
 +            createRegion(name, factory.createRegionAttributes());
 +          }
 +        });
 +
 +
 +      // Execute client queries in VM1
 +      vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +          public void run2() throws CacheException {
 +            Region region = getRootRegion().getSubregion(name);
 +            String queryStrings = "id<9";
 +//            SelectResults results = null;
 +              try {
 +                region.query(queryStrings);
 +                fail("The query should have experienced RegionDestroyedException");
 +              } catch (QueryInvocationTargetException qte) {
 +                //Ok test passed
 +              } catch(Exception e) {
 +                Assert.fail("Failed executing query " + queryStrings+ " due  to unexpected Excecption", e);
 +            }
 +          }
 +        });
 +
 +      // Start server
 +      vm0.invoke(new CacheSerializableRunnable("Create two regions") {
 +          public void run2() throws CacheException {
 +            AttributesFactory factory = new AttributesFactory();
 +            factory.setScope(Scope.LOCAL);
 +            final Region region1 = createRegion(name, factory.createRegionAttributes());
 +            final Region region2 = createRegion(name+"_2", factory.createRegionAttributes());
 +            QueryObserverHolder.setInstance(new QueryObserverAdapter() {
 +              public void afterQueryEvaluation(Object result) {
 +                //Destroy the region in the test
 +                region1.close();
 +              }
 +
 +            });
 +            for (int i=0; i<numberOfEntries; i++) {
 +              region1.put("key-"+i, new TestObject(i, "ibm"));
 +              region2.put("key-"+i, new TestObject(i, "ibm"));
 +            }
 +
 +          }
 +        });
 +
 +   // Execute client queries in VM1
 +      vm1.invoke(new CacheSerializableRunnable("Execute queries") {
 +          public void run2() throws CacheException {
 +            Region region = getRootRegion().getSubregion(name);
 +            String queryString = "select distinct * from /"+name+",/"+name + "_2";
 +//            SelectResults results = null;
 +              try {
 +                region.query(queryString);
 +                fail("The query should have experienced RegionDestroyedException");
 +              } catch (QueryInvocationTargetException qte) {
 +                //Ok test passed
 +              } catch(Exception e) {
 +                Assert.fail("Failed executing query " + queryString+ " due  to unexpected Excecption", e);
 +            }
 +          }
 +        });
 +
 +        // Close client VM1
 +        vm1.invoke(new CacheSerializableRunnable("Close client") {
 +          public void run2() throws CacheException {
 +            Region region = getRootRegion().getSubregion(name);
 +            region.close();
 +            PoolManager.find("clientPool").destroy();
 +          }
 +        });
 +
 +
 +      // Stop server
 +      vm0.invoke(new SerializableRunnable("Stop CacheServer") {
 +        public void run() {
 +          QueryObserverHolder.setInstance(new QueryObserverAdapter());
 +          stopBridgeServer(getCache());
 +        }
 +      });
 +
 +
 +
 +    }
 +
 +
 +
 +  /**
 +   * Starts a bridge server on the given port, using the given
 +   * deserializeValues and notifyBySubscription to serve up the
 +   * given region.
 +   */
 +  protected void startBridgeServer(int port, boolean notifyBySubscription)
 +    throws IOException {
 +
 +    Cache cache = getCache();
 +    CacheServer bridge = cache.addCacheServer();
 +    bridge.setPort(port);
 +    bridge.setNotifyBySubscription(notifyBySubscription);
 +    bridge.start();
 +    bridgeServerPort = bridge.getPort();
 +  }
 +
 +  /**
 +   * Stops the bridge server that serves up the given cache.
 +   */
 +  protected void stopBridgeServer(Cache cache) {
 +    CacheServer bridge =
 +      (CacheServer) cache.getCacheServers().iterator().next();
 +    bridge.stop();
 +    assertFalse(bridge.isRunning());
 +  }
 +
 +  private static int getCacheServerPort() {
 +    return bridgeServerPort;
 +  }
 +
 +  public static class TestObject implements DataSerializable {
 +    protected String _ticker;
 +    protected int _price;
 +    public int id;
 +    public int important;
 +    public int selection;
 +    public int select;
 +
 +    public TestObject() {}
 +
 +    public TestObject(int id, String ticker) {
 +      this.id = id;
 +      this._ticker = ticker;
 +      this._price = id;
 +      this.important = id;
 +      this.selection =id;
 +      this.select =id;
 +    }
 +
 +    public int getId() {
 +      return this.id;
 +    }
 +
 +    public String getTicker() {
 +      return this._ticker;
 +    }
 +
 +    public int getPrice() {
 +      return this._price;
 +    }
 +
 +    public void toData(DataOutput out) throws IOException
 +    {
 +      //System.out.println("Is serializing in WAN: " + GatewayEventImpl.isSerializingValue());
 +      out.writeInt(this.id);
 +      DataSerializer.writeString(this._ticker, out);
 +      out.writeInt(this._price);
 +    }
 +
 +    public void fromData(DataInput in) throws IOException, ClassNotFoundException
 +    {
 +      //System.out.println("Is deserializing in WAN: " + GatewayEventImpl.isDeserializingValue());
 +      this.id = in.readInt();
 +      this._ticker = DataSerializer.readString(in);
 +      this._price = in.readInt();
 +    }
 +
 +    public String toString() {
 +      StringBuffer buffer = new StringBuffer();
 +      buffer
 +        .append("TestObject [")
 +        .append("id=")
 +        .append(this.id)
 +        .append("; ticker=")
 +        .append(this._ticker)
 +        .append("; price=")
 +        .append(this._price)
 +        .append("]");
 +      return buffer.toString();
 +    }
 +  }
 +
 +  public static class IdComparator implements Comparator {
 +
 +    public int compare(Object obj1, Object obj2) {
 +      int obj1Id = ((TestObject) obj1).getId();
 +      int obj2Id = ((TestObject) obj2).getId();
 +      if (obj1Id > obj2Id) {
 +        return 1;
 +      } else if (obj1Id < obj2Id) {
 +        return -1;
 +      } else {
 +        return 0;
 +      }
 +    }
 +  }
 +
 +  public static class StructIdComparator implements Comparator {
 +
 +    public int compare(Object obj1, Object obj2) {
 +      int obj1Id = ((Integer) ((Struct) obj1).get("id")).intValue();
 +      int obj2Id = ((Integer) ((Struct) obj2).get("id")).intValue();
 +      if (obj1Id > obj2Id) {
 +        return 1;
 +      } else if (obj1Id < obj2Id) {
 +        return -1;
 +      } else {
 +        return 0;
 +      }
 +    }
 +  }
 +}
 +
 +/*
 +    String queryString = "ticker = 'ibm'";
 +    SelectResults results = region.query(queryString);
 +
 +    String queryString = "ticker = 'ibm' and price = 50";
 +
 +    String queryString = "select distinct * from /trade";
 +
 +    String queryString = "import TestObject; select distinct * from /trade";
 +
 +    String queryString =
 +      "IMPORT cacheRunner.Position; " +
 +      "SELECT DISTINCT id, status FROM /root/portfolios " +
 +      "WHERE NOT (SELECT DISTINCT * FROM positions.values posnVal TYPE Position " +
 +      "WHERE posnVal.secId='AOL' OR posnVal.secId='SAP').isEmpty";
 +
 +    queryString = "SELECT DISTINCT itr.value FROM /trade.entries itr where itr.key = 'key-1'";
 +
 +    queryString = "SELECT DISTINCT itr.key FROM /trade.entries itr where itr.key = 'key-1'";
 +
 +    String queryString =
 +      "select distinct a, b.price from /generic/app1/Trade a, /generic/app1/Trade b where a.price = b.price";
 +
 +    String queryString =
 +        "SELECT DISTINCT a, b.unitPrice from /newegg/arinv a, /newegg/itemPriceSetting b where a.item = b.itemNumber and a.item = '26-106-934'";
 +
 +    String queryString =
 +        "SELECT DISTINCT a, UNDEFINED from /newegg/arinv a, /newegg/itemPriceSetting b where a.item = b.itemNumber and a.item = '26-106-934'";
 +
 +*/

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache/query/internal/QueryFromClauseCanonicalizationJUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache/query/internal/QueryFromClauseCanonicalizationJUnitTest.java
index 0f13a31,0000000..f62fb8a
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache/query/internal/QueryFromClauseCanonicalizationJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache/query/internal/QueryFromClauseCanonicalizationJUnitTest.java
@@@ -1,264 -1,0 +1,257 @@@
 +/*
 + * 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.
 + */
 +/*
 + * IndexTest.java JUnit based test
 + * 
 + * Created on March 9, 2005, 3:30 PM
 + */
 +package com.gemstone.gemfire.cache.query.internal;
 +
 +import static org.junit.Assert.assertEquals;
 +import static org.junit.Assert.assertTrue;
 +
 +import java.util.ArrayList;
 +import java.util.Iterator;
 +import java.util.List;
 +
 +import org.junit.After;
 +import org.junit.Assert;
 +import org.junit.Before;
 +import org.junit.Test;
 +import org.junit.experimental.categories.Category;
 +
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.query.CacheUtils;
 +import com.gemstone.gemfire.cache.query.Query;
 +import com.gemstone.gemfire.cache.query.QueryService;
 +import com.gemstone.gemfire.cache.query.data.Portfolio;
 +import com.gemstone.gemfire.test.junit.categories.IntegrationTest;
 +
 +/**
 + * 
 + * @author Asif
 + */
 +@Category(IntegrationTest.class)
 +public class QueryFromClauseCanonicalizationJUnitTest
 +{
- 
-   /*
-    * ========================================================================
-    * Copyright (C) GemStone Systems, Inc. 2000-2004. All Rights Reserved.
-    * 
-    * ========================================================================
-    */
 +  Region region = null;
 +
 +  QueryService qs = null;
 +
 +  static String queries[] = {
 +      "SELECT DISTINCT ID, value.secId FROM /pos, getPositions where status = 'active' and ID = 0",
 +      "SELECT DISTINCT ID, value.secId FROM /pos, positions where status = 'active' and ID = 0",
 +      "SELECT DISTINCT ID, value.secId FROM /pos, getPositions() where status = 'active' and ID = 0",
 +      "SELECT DISTINCT ID, p.value.secId FROM /pos, getPositions('true') p where status = 'active' and ID = 0",
 +      "SELECT DISTINCT * FROM /pos as a, a.collectionHolderMap['0'].arr as b where a.status = 'active' and a.ID = 0",
 +     /* "SELECT DISTINCT * FROM /pos as a, a.positions[a.collectionHolderMap['0'][1]] as b where a.status = 'active' and a.ID = 0",*/
 +
 +  };
 +
 +  @Before
 +  public void setUp() throws java.lang.Exception
 +  {
 +    CacheUtils.startCache();
 +    region = CacheUtils.createRegion("pos", Portfolio.class);
 +    region.put("0", new Portfolio(0));
 +    region.put("1", new Portfolio(1));
 +    region.put("2", new Portfolio(2));
 +    region.put("3", new Portfolio(3));
 +
 +    qs = CacheUtils.getQueryService();
 +  }
 +
 +  @After
 +  public void tearDown() throws java.lang.Exception
 +  {
 +    CacheUtils.closeCache();
 +  }
 +
 +  @Test
 +  public void testCanonicalizedFromClause() throws Throwable
 +  {  
 +
 +    boolean overallTestFailed = false;
 +    Query q = null;
 +    QueryObserverImpl observer = null;
 +    for (int j = 0; j < queries.length; j++) {
 +
 +      try {
 +        q = qs.newQuery(queries[j]);
 +        observer = new QueryObserverImpl();
 +        QueryObserverHolder.setInstance(observer);
 +        q.execute();
 +      }
 +      catch (Exception e) {
 +        System.err
 +            .println("QueryFromClauseCanonicalizationJUnitTest::testCanonicalizedFromClause.Exception in running query number="
 +                + j + "  Exception=" + e);
 +        e.printStackTrace();
 +        overallTestFailed = true;
 +        continue;
 +      }
 +
 +      switch (j) {
 +      case 0:
 +      case 1:
 +      case 2:
 +        if (observer.clauses.get(0).toString().equals("/pos")
 +            && observer.clauses.get(1).toString().equals("iter1.positions")) {
 +          assertTrue(true);
 +        }
 +        else {
 +          overallTestFailed = true;
 +          System.err
 +              .println("QueryFromClauseCanonicalizationJUnitTest::testCanonicalizedFromClause.Failure in query number="
 +                  + j);
 +        }
 +        break;
 +      case 3:
 +        if (observer.clauses.get(0).toString().equals("/pos")
 +            && observer.clauses.get(1).toString().equals(
 +                "iter1.getPositions('true')")) {
 +          assertTrue(true);
 +        }
 +        else {
 +          overallTestFailed = true;
 +          System.err
 +              .println("QueryFromClauseCanonicalizationJUnitTest::testCanonicalizedFromClause.Failure in query number="
 +                  + j);
 +        }
 +        break;
 +      case 5:
 +        if (observer.clauses.get(0).toString().equals("/pos")
 +            && observer.clauses.get(1).toString().equals(
 +                "iter1.positions[iter1.collectionHolderMap[][]]")) {
 +          assertTrue(true);
 +        }
 +        else {
 +          overallTestFailed = true;
 +          System.err
 +              .println("QueryFromClauseCanonicalizationJUnitTest::testCanonicalizedFromClause.Failure in query number="
 +                  + j);
 +        }
 +        break;
 +      case 4:
 +        if (observer.clauses.get(0).toString().equals("/pos")
 +            && observer.clauses.get(1).toString().equals(
 +                "iter1.collectionHolderMap['0'].arr")) {
 +          assertTrue(true);
 +        }
 +        else {
 +          overallTestFailed = true;
 +          System.err
 +              .println("QueryFromClauseCanonicalizationJUnitTest::testCanonicalizedFromClause.Failure in query number="
 +                  + j);
 +        }
 +        break;
 +
 +      }
 +
 +    }
 +    if (overallTestFailed)
 +      Assert.fail();
 +
 +  }
 +
 +  @Test
 +  public void testCanonicalizationOfMethod() throws Exception
 +  {
 +    QCompiler compiler = new QCompiler();
 +    List list = compiler.compileFromClause("/pos pf");
 +    ExecutionContext context = new ExecutionContext(new Object[]{"bindkey"}, CacheUtils.getCache());
 +    context.newScope(context.assosciateScopeID());
 +
 +    Iterator iter = list.iterator();
 +    while (iter.hasNext()) {
 +      CompiledIteratorDef iterDef = (CompiledIteratorDef)iter.next();
 +      context.addDependencies(new CompiledID("dummy"), iterDef
 +          .computeDependencies(context));
 +      RuntimeIterator rIter = iterDef.getRuntimeIterator(context);
 +      context.bindIterator(rIter);
 +      context.addToIndependentRuntimeItrMap(iterDef);
 +    }
 +    CompiledPath cp = new CompiledPath(new CompiledID("pf"), "positions");
 +    CompiledLiteral cl = new CompiledLiteral("key1");
 +    List args = new ArrayList();
 +    args.add(cl);
 +    CompiledOperation cop = new CompiledOperation(cp, "get", args);
 +    StringBuffer sbuff = new StringBuffer();
 +    cop.generateCanonicalizedExpression(sbuff, context);
 +    assertEquals(sbuff.toString(),"iter1.positions.get('key1')");
 +    
 +//    cp = new CompiledPath(new CompiledID("pf"), "positions");
 +//    CompiledBindArgument cb = new CompiledBindArgument(1);
 +//    args = new ArrayList();
 +//    args.add(cb);
 +//    cop = new CompiledOperation(cp, "get", args);
 +////    context.setBindArguments(new Object[]{"bindkey"});
 +//    sbuff = new StringBuffer();
 +//    cop.generateCanonicalizedExpression(sbuff, context);
 +//    assertEquals(sbuff.toString(),"iter1.positions.get('bindkey')");
 +//    
 +    
 +//    cp = new CompiledPath(new CompiledID("pf"), "getPositions()");
 +//    cb = new CompiledBindArgument(1);
 +//    args = new ArrayList();
 +//    args.add(cb);
 +//    cop = new CompiledOperation(cp, "get", args);
 +//    sbuff = new StringBuffer();
 +//    cop.generateCanonicalizedExpression(sbuff, context);
 +//    assertEquals(sbuff.toString(),"iter1.positions().get('bindkey')");
 +//    
 +//    
 +//    cp = new CompiledPath(new CompiledID("pf"), "getPositions");
 +//    cb = new CompiledBindArgument(1);
 +//    args = new ArrayList();
 +//    args.add(cb);
 +//    cop = new CompiledOperation(cp, "get", args);
 +//    sbuff = new StringBuffer();
 +//    cop.generateCanonicalizedExpression(sbuff, context);
 +//    assertEquals(sbuff.toString(),"iter1.positions.get('bindkey')");
 +    
 +    cp = new CompiledPath(new CompiledID("pf"), "getPositions");
 +    CompiledPath cp1 = new CompiledPath(new CompiledID("pf"),"pkid");
 +    args = new ArrayList();
 +    args.add(cp1);
 +    cop = new CompiledOperation(cp, "get", args);
 +    sbuff = new StringBuffer();
 +    cop.generateCanonicalizedExpression(sbuff, context);
 +    assertEquals(sbuff.toString(),"iter1.positions.get(iter1.pkid)");
 +    
 +    
 +    cp = new CompiledPath(new CompiledID("pf"), "getPositions");
 +    cp1 = new CompiledPath(new CompiledID("pf"),"pkid");    
 +    CompiledIndexOperation ciop = new CompiledIndexOperation(cp,cp1);
 +    sbuff = new StringBuffer();
 +    ciop.generateCanonicalizedExpression(sbuff, context);
 +    assertEquals(sbuff.toString(),"iter1.positions[iter1.pkid]");
 +  } 
 +  
 +  
 +  class QueryObserverImpl extends QueryObserverAdapter
 +  {
 +    public List clauses = new ArrayList();
 +
 +    public void beforeIterationEvaluation(CompiledValue executer,
 +        Object currentObject)
 +    {
 +      clauses.add(((RuntimeIterator)executer).getDefinition());
 +
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache/query/internal/ResultsBagJUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache/query/internal/ResultsBagJUnitTest.java
index e910e73,0000000..9758e04
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache/query/internal/ResultsBagJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache/query/internal/ResultsBagJUnitTest.java
@@@ -1,314 -1,0 +1,307 @@@
 +/*
 + * 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.
 + */
- //
- //  ResultsBagJUnitTest.java
- //  gemfire
- //
- //  Created by Eric Zoerner on 2/13/08.
- //  Copyright 2008 __MyCompanyName__. All rights reserved.
- //
 +package com.gemstone.gemfire.cache.query.internal;
 +
 +import java.util.*;
 +import java.io.*;
 +
 +import org.junit.experimental.categories.Category;
 +
 +import junit.framework.*;
 +
 +import com.gemstone.gemfire.cache.query.SelectResults;
 +import com.gemstone.gemfire.cache.query.internal.types.ObjectTypeImpl;
 +import com.gemstone.gemfire.internal.HeapDataOutputStream;
 +import com.gemstone.gemfire.internal.Version;
 +import com.gemstone.gemfire.DataSerializer;
 +import com.gemstone.gemfire.test.junit.categories.UnitTest;
 +
 +/**
 + * Test ResultsBag, including null elements
 + * @author ezoerner
 + */
 +@Category(UnitTest.class)
 +public class ResultsBagJUnitTest extends TestCase {
 +
 +  public ResultsBagJUnitTest(String testName) {
 +    super(testName);
 +  }
 +  
 +  public void testDuplicates() {
 +    ResultsBag bag = new ResultsBag();
 +    bag.add("one");
 +    bag.add("two");
 +    assertEquals(2, bag.size());
 +    bag.add("two");
 +    assertEquals(3, bag.size());
 +    assertEquals(1, bag.occurrences("one"));
 +    assertEquals(2, bag.occurrences("two"));
 +    
 +    assertTrue(bag.remove("two"));
 +    assertEquals(1, bag.occurrences("two"));
 +    assertTrue(bag.remove("one"));
 +    assertEquals(0, bag.occurrences("one"));
 +    assertTrue(!bag.remove("one"));
 +    assertEquals(0, bag.occurrences("one"));
 +    
 +  }
 +  
 +  public void testIteration() {
 +    ResultsBag bag = new ResultsBag();
 +    bag.add(new Integer(1));
 +    bag.add(new Integer(2));
 +    bag.add(new Integer(2));
 +    
 +    int numOnes = 0;
 +    int numTwos = 0;
 +    Integer one = new Integer(1);
 +    Integer two = new Integer(2);
 +    for (Iterator itr = bag.iterator(); itr.hasNext(); ) {
 +      Object n = itr.next();
 +      if (one.equals(n)) {
 +        numOnes++;
 +      }
 +      else if (two.equals(n)) {
 +        numTwos++;
 +      }
 +      else {
 +        fail(n + " did not equal 1 or 2");
 +      }
 +    }
 +    assertEquals(1, numOnes);
 +    assertEquals(2, numTwos);
 +  }
 +  
 +  public void testSerializingSetViewWithNulls()
 +  throws ClassNotFoundException, IOException {
 +    ResultsBag bag = new ResultsBag();
 +    bag.add(new Integer(4));
 +    bag.add(new Integer(2));
 +    bag.add(new Integer(42));
 +    bag.add(null);
 +    bag.add(null);
 +    bag.add(null);
 +    
 +    assertEquals(6, bag.size());
 +    assertEquals(1, bag.occurrences(new Integer(4)));
 +    assertEquals(3, bag.occurrences(null));
 +    
 +    Set set = bag.asSet();
 +    assertEquals(4, set.size());
 +    assertTrue(set.contains(new Integer(4)));
 +    assertTrue(set.contains(null));    
 +    
 +    ResultsCollectionWrapper w
 +      = new ResultsCollectionWrapper(new ObjectTypeImpl(Integer.class),
 +                                     set);
 +    
 +    HeapDataOutputStream hdos = new HeapDataOutputStream(Version.CURRENT);
 +    DataSerializer.writeObject(w, hdos);
 +    DataInputStream in = new DataInputStream(hdos.getInputStream());
 +    SelectResults setCopy = (SelectResults)DataSerializer.readObject(in);
 +    
 +    assertEquals(4, setCopy.size());
 +    assertTrue(setCopy.contains(new Integer(4)));
 +    assertTrue(setCopy.contains(null));
 +  }
 +    
 +  public void testNulls() {
 +    ResultsBag bag = new ResultsBag();
 +    assertTrue(bag.isEmpty());
 +    bag.add(null);
 +    assertTrue(!bag.isEmpty());
 +    assertEquals(1, bag.size());
 +    assertEquals(1, bag.occurrences(null));
 +    
 +    bag.add(new Integer(1));
 +    assertEquals(2, bag.size());
 +    bag.add(new Integer(2));
 +    assertEquals(3, bag.size());
 +    bag.add(new Integer(2));
 +    assertEquals(4, bag.size());
 +    
 +    bag.add(null);
 +    assertEquals(5, bag.size());
 +    assertEquals(2, bag.occurrences(null));
 +    
 +    int numNulls = 0;
 +    int numOnes = 0;
 +    int numTwos = 0;
 +    Integer one = new Integer(1);
 +    Integer two = new Integer(2);
 +    for (Iterator itr = bag.iterator(); itr.hasNext(); ) {
 +      Object n = itr.next();
 +      if (one.equals(n)) {
 +        numOnes++;
 +      }
 +      else if (two.equals(n)) {
 +        numTwos++;
 +      }
 +      else if (n == null) {
 +        numNulls++;
 +      }
 +      else {
 +        fail(n + " was not null and did not equal 1 or 2");
 +      }
 +    }
 +    assertEquals(1, numOnes);
 +    assertEquals(2, numTwos);
 +    assertEquals(2, numNulls);
 +    
 +    // make sure toString doesn't blow up with nulls
 +    String s = bag.toString();
 +    assertTrue("toString didn't contain 'null': '" + s + "'", s.indexOf("null") > 0);
 +    
 +    assertTrue(bag.remove(null));
 +    assertEquals(1, bag.occurrences(null));
 +    assertTrue(bag.remove(null));
 +    assertEquals(0, bag.occurrences(null));
 +    assertTrue(!bag.remove(null));
 +    assertEquals(0, bag.occurrences(null));
 +  }
 +  
 +  public void testIterationNullRemoval() {
 +    ResultsBag bag = new ResultsBag();
 +    bag.add(null);
 +    bag.add(null);
 +
 +    bag.add(new Integer(1));
 +    bag.add(new Integer(2));
 +    bag.add(new Integer(2));
 +    assertEquals(5, bag.size());
 +    
 +    for (Iterator itr = bag.iterator(); itr.hasNext(); ) {
 +      Object n = itr.next();
 +      if (n == null) {
 +        itr.remove();
 +      }
 +    }
 +    assertEquals(3, bag.size());
 +    assertEquals(0, bag.occurrences(null));
 +  }
 +  
 +  public void testIterationRemoval() {
 +    ResultsBag bag = new ResultsBag();
 +    
 +    bag.add(new Integer(1));
 +    bag.add(new Integer(2));
 +    bag.add(new Integer(2));
 +    bag.add(new Integer(3));
 +    bag.add(new Integer(3));
 +    bag.add(new Integer(4));
 +    
 +    assertEquals(6, bag.size());
 +    
 +//    Integer one = new Integer(1);
 +//    Integer two = new Integer(2);
 +    Iterator itr = bag.iterator();
 +    for (int i = 0 ; i < 3; i++) {
 +      itr.next();
 +      itr.remove();
 +    }
 +    assertEquals(3, bag.size());
 +    
 +    for (int i = 0 ; i < 3; i++) {
 +      itr.next();
 +      itr.remove();
 +    }
 +    assertTrue(bag.isEmpty());
 +    assertEquals(0, bag.size());
 +  }
 +  
 +  public void testNoSuchElementException() {
 +    ResultsBag bag = new ResultsBag();
 +    
 +    bag.add(new Integer(1));
 +    bag.add(new Integer(2));
 +    bag.add(new Integer(2));
 +    bag.add(new Integer(3));
 +    bag.add(new Integer(3));
 +    bag.add(new Integer(4));
 +    
 +    assertEquals(6, bag.size());
 +    
 +    Iterator itr = bag.iterator();
 +    for (int i = 0 ; i < 6; i++) {
 +      itr.next();
 +    }
 +    try {
 +      itr.next();
 +      fail("should have thrown a NoSuchElementException");
 +    }
 +    catch (NoSuchElementException e) {
 +      // pass
 +    }
 +    
 +    // test with removes
 +    itr = bag.iterator();
 +    for (int i = 0 ; i < 6; i++) {
 +      itr.next();
 +      itr.remove();
 +    }
 +    assertEquals(0, bag.size());
 +    try {
 +      itr.next();
 +      fail("should have thrown a NoSuchElementException");
 +    }
 +    catch (NoSuchElementException e) {
 +      // pass
 +    }
 +    
 +    // test with nulls
 +    bag = new ResultsBag();
 +    
 +    bag.add(new Integer(1));
 +    bag.add(new Integer(2));
 +    bag.add(new Integer(2));
 +    bag.add(null);
 +    bag.add(null);
 +    bag.add(null);
 +    bag.add(new Integer(3));
 +    bag.add(new Integer(3));
 +    bag.add(new Integer(4));
 +    
 +    assertEquals(9, bag.size());
 +    
 +    itr = bag.iterator();
 +    for (int i = 0 ; i < 9; i++) {
 +      itr.next();
 +    }
 +    try {
 +      itr.next();
 +      fail("should have thrown a NoSuchElementException");
 +    }
 +    catch (NoSuchElementException e) {
 +      // pass
 +    }
 +    
 +    // test with removes
 +    itr = bag.iterator();
 +    for (int i = 0 ; i < 9; i++) {
 +      itr.next();
 +      itr.remove();
 +    }
 +    assertEquals(0, bag.size());
 +    try {
 +      itr.next();
 +      fail("should have thrown a NoSuchElementException");
 +    }
 +    catch (NoSuchElementException e) {
 +      // pass
 +    }
 +  }
 +}


[51/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

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


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

Branch: refs/heads/feature/GEODE-917
Commit: 5beaaedcebdd08f17a9a4cc169178b9c4af5f567
Parents: 8780b5c 110a5b4
Author: Dan Smith <up...@apache.org>
Authored: Fri Feb 19 11:31:08 2016 -0800
Committer: Dan Smith <up...@apache.org>
Committed: Fri Feb 19 11:39:33 2016 -0800

----------------------------------------------------------------------
 LICENSE                                         |    6 +
 NOTICE                                          |   10 +
 geode-assembly/build.gradle                     |    7 +-
 geode-assembly/src/main/dist/LICENSE            |    5 +
 geode-assembly/src/src/dist/gradlew             |  221 ++
 .../management/internal/AgentUtilJUnitTest.java |   52 +-
 .../cache/FailedSynchronizationException.java   |   10 +-
 .../cache/client/internal/AbstractOp.java       |    2 +-
 .../cache/client/internal/AddPDXEnumOp.java     |    2 +-
 .../cache/client/internal/AddPDXTypeOp.java     |    4 +-
 .../client/internal/AuthenticateUserOp.java     |    4 +-
 .../client/internal/CloseConnectionOp.java      |    2 +-
 .../gemfire/cache/client/internal/CommitOp.java |    2 +-
 .../client/internal/GetClientPRMetaDataOp.java  |    2 +-
 .../GetClientPartitionAttributesOp.java         |    2 +-
 .../cache/client/internal/GetEventValueOp.java  |    2 +-
 .../client/internal/GetFunctionAttributeOp.java |    2 +-
 .../cache/client/internal/GetPDXEnumByIdOp.java |    4 +-
 .../cache/client/internal/GetPDXEnumsOp.java    |    2 +-
 .../client/internal/GetPDXIdForEnumOp.java      |    4 +-
 .../client/internal/GetPDXIdForTypeOp.java      |    4 +-
 .../cache/client/internal/GetPDXTypeByIdOp.java |    4 +-
 .../cache/client/internal/GetPDXTypesOp.java    |    2 +-
 .../cache/client/internal/MakePrimaryOp.java    |    2 +-
 .../gemfire/cache/client/internal/PingOp.java   |    2 +-
 .../cache/client/internal/PrimaryAckOp.java     |    2 +-
 .../client/internal/ProxyCacheCloseOp.java      |    2 +-
 .../cache/client/internal/ReadyForEventsOp.java |    2 +-
 .../internal/RegisterDataSerializersOp.java     |    2 +-
 .../internal/RegisterInstantiatorsOp.java       |    2 +-
 .../cache/client/internal/RollbackOp.java       |    2 +-
 .../gemfire/cache/client/internal/SizeOp.java   |    2 +-
 .../cache/client/internal/TXFailoverOp.java     |    2 +-
 .../client/internal/TXSynchronizationOp.java    |    2 +-
 .../cache/hdfs/internal/cardinality/Bits.java   |   54 -
 .../cardinality/CardinalityMergeException.java  |   42 -
 .../hdfs/internal/cardinality/HyperLogLog.java  |  313 ---
 .../hdfs/internal/cardinality/IBuilder.java     |   41 -
 .../hdfs/internal/cardinality/ICardinality.java |   87 -
 .../hdfs/internal/cardinality/MurmurHash.java   |  261 ---
 .../hdfs/internal/cardinality/RegisterSet.java  |  136 --
 .../hdfs/internal/hoplog/AbstractHoplog.java    |    2 +-
 .../hdfs/internal/hoplog/HFileSortedOplog.java  |    4 +-
 .../hoplog/HdfsSortedOplogOrganizer.java        |    8 +-
 .../cache/hdfs/internal/hoplog/Hoplog.java      |    3 +-
 .../internal/hoplog/SequenceFileHoplog.java     |    2 +-
 .../query/internal/index/IndexElemArray.java    |   94 +-
 .../query/internal/index/IndexManager.java      |    5 +-
 .../index/PrimaryKeyIndexCreationHelper.java    |   25 +-
 .../gemfire/distributed/LocatorLauncher.java    |    7 +-
 .../gemfire/distributed/ServerLauncher.java     |   12 +-
 .../gemfire/distributed/internal/DMStats.java   |    3 +
 .../distributed/internal/DistributionStats.java |   14 +
 .../internal/InternalDistributedSystem.java     |   53 +-
 .../internal/LonerDistributionManager.java      |    4 +
 .../membership/InternalDistributedMember.java   |    2 +-
 .../gms/messenger/JGroupsMessenger.java         |  105 +-
 .../gms/mgr/GMSMembershipManager.java           |    9 +-
 .../gemfire/internal/cache/BucketAdvisor.java   |    2 +-
 .../gemfire/internal/cache/BucketRegion.java    |   26 +-
 .../gemfire/internal/cache/CacheServerImpl.java |   11 +
 .../internal/cache/CacheServerLauncher.java     |    4 +-
 .../internal/cache/DistributedRegion.java       |   22 +-
 .../gemfire/internal/cache/EntryEventImpl.java  |    7 +
 .../gemfire/internal/cache/EventTracker.java    |   11 +-
 .../internal/cache/GemFireCacheImpl.java        |    0
 .../gemfire/internal/cache/TXManagerImpl.java   |    2 +-
 .../cache/tier/sockets/BaseCommand.java         |    2 +-
 .../cache/tier/sockets/CacheClientProxy.java    |    2 +
 .../cache/tier/sockets/CacheServerHelper.java   |   46 +-
 .../internal/cache/tier/sockets/Message.java    |  236 +-
 .../internal/cache/tier/sockets/Part.java       |    9 +-
 .../command/ExecuteRegionFunction66.java        |    9 +-
 .../cache/xmlcache/AsyncEventQueueCreation.java |    1 +
 .../internal/cache/xmlcache/CacheCreation.java  |    9 +-
 .../cache/xmlcache/CacheXmlGenerator.java       |   37 +-
 .../com/gemstone/gemfire/internal/hll/Bits.java |   48 +
 .../internal/hll/CardinalityMergeException.java |   26 +
 .../gemfire/internal/hll/HyperLogLog.java       |  345 +++
 .../gemfire/internal/hll/HyperLogLogPlus.java   | 1053 +++++++++
 .../gemstone/gemfire/internal/hll/IBuilder.java |   24 +
 .../gemfire/internal/hll/ICardinality.java      |   72 +
 .../gemfire/internal/hll/MurmurHash.java        |  245 ++
 .../gemfire/internal/hll/RegisterSet.java       |  110 +
 .../gemfire/internal/redis/RegionProvider.java  |    2 +-
 .../internal/redis/executor/hll/Bits.java       |   65 -
 .../executor/hll/CardinalityMergeException.java |   42 -
 .../redis/executor/hll/HyperLogLog.java         |  360 ---
 .../redis/executor/hll/HyperLogLogPlus.java     | 1068 ---------
 .../internal/redis/executor/hll/IBuilder.java   |   41 -
 .../redis/executor/hll/ICardinality.java        |   89 -
 .../internal/redis/executor/hll/MurmurHash.java |  234 --
 .../redis/executor/hll/PFAddExecutor.java       |    1 +
 .../redis/executor/hll/PFCountExecutor.java     |    2 +
 .../redis/executor/hll/PFMergeExecutor.java     |    2 +
 .../redis/executor/hll/RegisterSet.java         |  126 -
 .../gemfire/internal/tcp/Connection.java        |    5 +-
 .../gemfire/redis/GemFireRedisServer.java       |    2 +-
 .../cache/CacheRegionClearStatsDUnitTest.java   |   38 +-
 .../cache/ClientServerTimeSyncDUnitTest.java    |    4 +-
 .../internal/AutoConnectionSourceDUnitTest.java |    2 +-
 .../CacheServerSSLConnectionDUnitTest.java      |   44 +-
 .../internal/LocatorLoadBalancingDUnitTest.java |   15 +-
 .../cache/client/internal/LocatorTestBase.java  |    6 +-
 .../internal/SSLNoClientAuthDUnitTest.java      |   14 +-
 .../management/MemoryThresholdsDUnitTest.java   |   10 +-
 .../MemoryThresholdsOffHeapDUnitTest.java       |   10 +-
 .../query/dunit/PdxStringQueryDUnitTest.java    |   42 +-
 .../dunit/QueryDataInconsistencyDUnitTest.java  |   12 +-
 .../QueryUsingFunctionContextDUnitTest.java     |   41 +-
 .../query/dunit/QueryUsingPoolDUnitTest.java    |   32 +-
 .../cache/query/dunit/RemoteQueryDUnitTest.java |   18 +-
 ...ueryFromClauseCanonicalizationJUnitTest.java |    7 -
 .../query/internal/ResultsBagJUnitTest.java     |    7 -
 ...rrentIndexInitOnOverflowRegionDUnitTest.java |    3 +-
 .../query/internal/index/IndexUseJUnitTest.java |  452 +---
 .../index/PutAllWithIndexPerfDUnitTest.java     |    2 +-
 .../gemfire/cache30/Bug34387DUnitTest.java      |    2 +-
 .../gemfire/cache30/CacheMapTxnDUnitTest.java   |   20 +-
 .../cache30/CachedAllEventsDUnitTest.java       |    2 +-
 .../gemfire/cache30/CallbackArgDUnitTest.java   |    2 +-
 .../cache30/ClearMultiVmCallBkDUnitTest.java    |   18 +-
 .../gemfire/cache30/ClearMultiVmDUnitTest.java  |   22 +-
 .../cache30/ClientMembershipDUnitTest.java      |   30 +-
 .../ClientRegisterInterestDUnitTest.java        |    9 +-
 .../cache30/DistAckMapMethodsDUnitTest.java     |   52 +-
 .../DistributedNoAckRegionCCEDUnitTest.java     |   28 +-
 .../gemfire/cache30/MultiVMRegionTestCase.java  |   68 +-
 .../cache30/PartitionedRegionDUnitTest.java     |    2 +-
 .../gemfire/cache30/ProxyDUnitTest.java         |    2 +-
 .../cache30/PutAllCallBkRemoteVMDUnitTest.java  |    8 +-
 .../cache30/PutAllCallBkSingleVMDUnitTest.java  |   10 +-
 .../gemfire/cache30/PutAllMultiVmDUnitTest.java |    8 +-
 .../gemfire/cache30/ReconnectDUnitTest.java     |    6 +-
 .../ReconnectedCacheServerDUnitTest.java        |   27 +
 .../RegionMembershipListenerDUnitTest.java      |    2 +-
 .../cache30/RegionReliabilityTestCase.java      |   21 +-
 .../cache30/RemoveAllMultiVmDUnitTest.java      |    8 +-
 .../cache30/RolePerformanceDUnitTest.java       |    3 +-
 .../gemfire/cache30/SearchAndLoadDUnitTest.java |    4 +-
 .../gemfire/cache30/TXDistributedDUnitTest.java |   12 +-
 .../gemfire/cache30/TXOrderDUnitTest.java       |    2 +-
 .../gemfire/distributed/LocatorDUnitTest.java   |   59 +-
 .../distributed/LocatorLauncherJUnitTest.java   |   27 +-
 .../LocatorLauncherLocalJUnitTest.java          |    4 +-
 .../LocatorLauncherRemoteJUnitTest.java         |    2 +-
 .../distributed/ServerLauncherJUnitTest.java    |   26 +-
 .../ServerLauncherLocalJUnitTest.java           |    4 +-
 .../ServerLauncherRemoteJUnitTest.java          |    2 +-
 .../ConsoleDistributionManagerDUnitTest.java    |    7 +-
 .../messenger/JGroupsMessengerJUnitTest.java    |   86 +-
 .../gemfire/disttx/CacheMapDistTXDUnitTest.java |    8 +-
 .../gemfire/disttx/DistTXDebugDUnitTest.java    |   20 +-
 .../disttx/DistributedTransactionDUnitTest.java |    3 +-
 .../internal/cache/BucketRegionJUnitTest.java   |  208 ++
 .../internal/cache/Bug33359DUnitTest.java       |    8 +-
 .../internal/cache/Bug33726DUnitTest.java       |   10 +-
 .../internal/cache/Bug37241DUnitTest.java       |   26 +-
 .../internal/cache/Bug39079DUnitTest.java       |   21 +-
 .../internal/cache/Bug41733DUnitTest.java       |    2 +-
 .../internal/cache/ClearDAckDUnitTest.java      |   26 +-
 .../internal/cache/ClearGlobalDUnitTest.java    |    6 +-
 .../cache/DeltaPropagationDUnitTest.java        |  324 ++-
 .../cache/DeltaPropagationStatsDUnitTest.java   |  120 +-
 .../cache/DiskRegByteArrayDUnitTest.java        |   14 +-
 .../cache/DistributedCacheTestCase.java         |    5 +-
 .../internal/cache/EventTrackerDUnitTest.java   |    6 +-
 .../internal/cache/EvictionStatsDUnitTest.java  |   12 +-
 .../internal/cache/EvictionTestBase.java        |    9 +-
 .../cache/FixedPRSinglehopDUnitTest.java        |  140 +-
 .../internal/cache/GIIDeltaDUnitTest.java       |   84 +-
 .../internal/cache/HABug36773DUnitTest.java     |  370 ---
 .../HAOverflowMemObjectSizerDUnitTest.java      |   54 +-
 .../internal/cache/MapClearGIIDUnitTest.java    |   17 +-
 .../cache/P2PDeltaPropagationDUnitTest.java     |  105 +-
 .../PartitionedRegionCreationDUnitTest.java     |    2 +-
 .../PartitionedRegionSingleHopDUnitTest.java    |  460 ++--
 ...RegionSingleHopWithServerGroupDUnitTest.java |  243 +-
 .../internal/cache/PutAllDAckDUnitTest.java     |   12 +-
 .../internal/cache/PutAllGlobalDUnitTest.java   |   12 +-
 .../internal/cache/RemoveAllDAckDUnitTest.java  |   12 +-
 .../internal/cache/RemoveDAckDUnitTest.java     |   10 +-
 .../internal/cache/RemoveGlobalDUnitTest.java   |   12 +-
 .../internal/cache/SingleHopStatsDUnitTest.java |   60 +-
 .../internal/cache/SystemFailureDUnitTest.java  |    4 +-
 .../control/RebalanceOperationDUnitTest.java    |    6 +-
 .../cache/execute/Bug51193DUnitTest.java        |   18 +-
 .../ClientServerFunctionExecutionDUnitTest.java |  148 +-
 .../execute/ColocationFailoverDUnitTest.java    |   43 +-
 ...ributedRegionFunctionExecutionDUnitTest.java |  875 +++----
 .../cache/execute/LocalDataSetDUnitTest.java    |   26 +-
 .../LocalFunctionExecutionDUnitTest.java        |   16 +-
 .../MemberFunctionExecutionDUnitTest.java       |   59 +-
 .../MultiRegionFunctionExecutionDUnitTest.java  |    8 +-
 ...ntServerFunctionExecutionNoAckDUnitTest.java |   20 +-
 ...tServerRegionFunctionExecutionDUnitTest.java |  102 +-
 ...egionFunctionExecutionFailoverDUnitTest.java |  236 +-
 ...onFunctionExecutionNoSingleHopDUnitTest.java |  176 +-
 ...onExecutionSelectorNoSingleHopDUnitTest.java |  164 +-
 ...gionFunctionExecutionSingleHopDUnitTest.java |  132 +-
 .../cache/execute/PRClientServerTestBase.java   |  155 +-
 .../cache/execute/PRColocationDUnitTest.java    |  219 +-
 .../execute/PRCustomPartitioningDUnitTest.java  |    8 +-
 .../execute/PRFunctionExecutionDUnitTest.java   |    8 +-
 .../execute/PRPerformanceTestDUnitTest.java     |    8 +-
 .../cache/execute/PRTransactionDUnitTest.java   |   50 +-
 .../execute/SingleHopGetAllPutAllDUnitTest.java |    6 +-
 .../cache/ha/Bug36853EventsExpiryDUnitTest.java |   17 +-
 .../internal/cache/ha/Bug48571DUnitTest.java    |   22 +-
 .../internal/cache/ha/Bug48879DUnitTest.java    |   20 +-
 .../cache/ha/EventIdOptimizationDUnitTest.java  |   44 +-
 .../internal/cache/ha/FailoverDUnitTest.java    |   18 +-
 .../internal/cache/ha/HABugInPutDUnitTest.java  |   28 +-
 .../internal/cache/ha/HAClearDUnitTest.java     |   41 +-
 .../cache/ha/HAConflationDUnitTest.java         |   18 +-
 .../internal/cache/ha/HADuplicateDUnitTest.java |   30 +-
 .../cache/ha/HAEventIdPropagationDUnitTest.java |  107 +-
 .../internal/cache/ha/HAExpiryDUnitTest.java    |   56 +-
 .../internal/cache/ha/HAGIIBugDUnitTest.java    |   12 +-
 .../internal/cache/ha/HAGIIDUnitTest.java       |   37 +-
 .../cache/ha/HARQueueNewImplDUnitTest.java      |  440 ++--
 .../internal/cache/ha/HARegionDUnitTest.java    |   68 +-
 .../cache/ha/HARegionQueueDUnitTest.java        |   83 +-
 .../cache/ha/HASlowReceiverDUnitTest.java       |   32 +-
 .../ha/OperationsPropagationDUnitTest.java      |   36 +-
 .../internal/cache/ha/PutAllDUnitTest.java      |   59 +-
 .../internal/cache/ha/StatsBugDUnitTest.java    |   28 +-
 .../cache/locks/TXLockServiceDUnitTest.java     |   28 +-
 .../cache/partitioned/Bug43684DUnitTest.java    |   38 +-
 .../cache/partitioned/Bug47388DUnitTest.java    |   29 +-
 .../cache/partitioned/Bug51400DUnitTest.java    |   23 +-
 .../PartitionedRegionLoaderWriterDUnitTest.java |   54 +-
 .../fixed/FixedPartitioningDUnitTest.java       | 1251 +++++-----
 .../fixed/FixedPartitioningTestBase.java        |   10 +-
 ...ngWithColocationAndPersistenceDUnitTest.java | 1368 +++++------
 .../internal/cache/tier/Bug40396DUnitTest.java  |   21 +-
 ...mpatibilityHigherVersionClientDUnitTest.java |   31 +-
 .../cache/tier/sockets/Bug36269DUnitTest.java   |   14 +-
 .../cache/tier/sockets/Bug36457DUnitTest.java   |   31 +-
 .../cache/tier/sockets/Bug36805DUnitTest.java   |   34 +-
 .../cache/tier/sockets/Bug36829DUnitTest.java   |   23 +-
 .../cache/tier/sockets/Bug36995DUnitTest.java   |   35 +-
 .../cache/tier/sockets/Bug37210DUnitTest.java   |   22 +-
 .../cache/tier/sockets/Bug37805DUnitTest.java   |   16 +-
 .../CacheServerTransactionsDUnitTest.java       |  222 +-
 .../tier/sockets/ClearPropagationDUnitTest.java |   48 +-
 .../tier/sockets/ClientConflationDUnitTest.java |   30 +-
 .../sockets/ClientInterestNotifyDUnitTest.java  |   88 +-
 .../tier/sockets/ClientServerMiscDUnitTest.java |   35 +-
 .../cache/tier/sockets/ConflationDUnitTest.java |  106 +-
 .../DataSerializerPropogationDUnitTest.java     |  337 ++-
 .../DestroyEntryPropagationDUnitTest.java       |   96 +-
 .../DurableClientQueueSizeDUnitTest.java        |  106 +-
 .../DurableClientReconnectDUnitTest.java        |   40 +-
 .../sockets/DurableClientStatsDUnitTest.java    |   72 +-
 .../sockets/DurableRegistrationDUnitTest.java   |  335 +--
 .../sockets/DurableResponseMatrixDUnitTest.java |   86 +-
 .../sockets/EventIDVerificationDUnitTest.java   |   71 +-
 .../EventIDVerificationInP2PDUnitTest.java      |   51 +-
 .../cache/tier/sockets/HABug36738DUnitTest.java |   18 +-
 .../tier/sockets/HAInterestPart1DUnitTest.java  |  100 +-
 .../tier/sockets/HAInterestPart2DUnitTest.java  |  114 +-
 .../cache/tier/sockets/HAInterestTestCase.java  |   32 +-
 .../sockets/HAStartupAndFailoverDUnitTest.java  |  108 +-
 .../InstantiatorPropagationDUnitTest.java       |  236 +-
 .../tier/sockets/InterestListDUnitTest.java     |  300 +--
 .../sockets/InterestListEndpointDUnitTest.java  |   66 +-
 .../sockets/InterestListFailoverDUnitTest.java  |   50 +-
 .../sockets/InterestListRecoveryDUnitTest.java  |   26 +-
 .../sockets/InterestRegrListenerDUnitTest.java  |   82 +-
 .../sockets/InterestResultPolicyDUnitTest.java  |   35 +-
 .../cache/tier/sockets/MessageJUnitTest.java    |   75 +
 .../sockets/NewRegionAttributesDUnitTest.java   |   28 +-
 .../sockets/RedundancyLevelPart1DUnitTest.java  |   36 +-
 .../sockets/RedundancyLevelPart2DUnitTest.java  |   67 +-
 .../sockets/RedundancyLevelPart3DUnitTest.java  |  100 +-
 .../tier/sockets/RedundancyLevelTestBase.java   |   20 +-
 .../tier/sockets/RegionCloseDUnitTest.java      |   20 +-
 ...erInterestBeforeRegionCreationDUnitTest.java |   20 +-
 .../sockets/RegisterInterestKeysDUnitTest.java  |   32 +-
 .../sockets/ReliableMessagingDUnitTest.java     |   22 +-
 .../sockets/UnregisterInterestDUnitTest.java    |   62 +-
 .../sockets/UpdatePropagationDUnitTest.java     |   91 +-
 .../sockets/UpdatePropagationPRDUnitTest.java   |    3 -
 .../VerifyEventIDGenerationInP2PDUnitTest.java  |   13 +-
 ...UpdatesFromNonInterestEndPointDUnitTest.java |   29 +-
 .../cache/wan/AsyncEventQueueTestBase.java      |  233 +-
 .../asyncqueue/AsyncEventListenerDUnitTest.java | 2185 +++++++-----------
 .../AsyncEventQueueStatsDUnitTest.java          |  321 ++-
 .../ConcurrentAsyncEventQueueDUnitTest.java     |  354 ++-
 .../CommonParallelAsyncEventQueueDUnitTest.java |   15 +-
 .../internal/jta/dunit/ExceptionsDUnitTest.java |   10 +-
 .../jta/dunit/IdleTimeOutDUnitTest.java         |    7 +-
 .../jta/dunit/LoginTimeOutDUnitTest.java        |    8 +-
 .../jta/dunit/MaxPoolSizeDUnitTest.java         |    5 +-
 .../jta/dunit/TransactionTimeOutDUnitTest.java  |   24 +-
 .../dunit/TxnManagerMultiThreadDUnitTest.java   |   14 +-
 .../internal/jta/dunit/TxnTimeOutDUnitTest.java |    9 +-
 .../internal/offheap/FragmentJUnitTest.java     |  280 +++
 .../statistics/StatisticsDUnitTest.java         |   11 +-
 .../management/CacheManagementDUnitTest.java    |   16 +-
 .../management/ClientHealthStatsDUnitTest.java  |   58 +-
 .../gemfire/management/QueryDataDUnitTest.java  |   20 +-
 ...ersalMembershipListenerAdapterDUnitTest.java |   54 +-
 .../cli/commands/FunctionCommandsDUnitTest.java |    4 +-
 .../commands/GemfireDataCommandsDUnitTest.java  |    4 +-
 ...WithCacheLoaderDuringCacheMissDUnitTest.java |    8 +-
 ...stAndDescribeDiskStoreCommandsDUnitTest.java |    4 +-
 .../cli/commands/ListIndexCommandDUnitTest.java |    8 +-
 .../MiscellaneousCommandsDUnitTest.java         |    6 +-
 ...laneousCommandsExportLogsPart3DUnitTest.java |    2 +-
 .../internal/pulse/TestFunctionsDUnitTest.java  |    3 +-
 .../internal/pulse/TestHeapDUnitTest.java       |    5 +-
 .../internal/pulse/TestLocatorsDUnitTest.java   |    3 +-
 .../web/controllers/RestAPITestBase.java        |    8 +-
 .../security/ClientAuthenticationDUnitTest.java |  502 ++--
 .../security/ClientAuthorizationDUnitTest.java  |  272 ++-
 .../security/ClientAuthorizationTestBase.java   |   23 +-
 .../security/ClientMultiUserAuthzDUnitTest.java |   52 +-
 .../DeltaClientAuthorizationDUnitTest.java      |   69 +-
 .../DeltaClientPostAuthorizationDUnitTest.java  |   40 +-
 .../security/P2PAuthenticationDUnitTest.java    |  127 +-
 .../gemfire/test/dunit/AsyncInvocation.java     |    4 +-
 .../gemfire/test/dunit/IgnoredException.java    |   12 +-
 .../gemfire/test/dunit/RMIException.java        |    2 +-
 .../test/dunit/SerializableRunnableIF.java      |    3 +-
 .../com/gemstone/gemfire/test/dunit/VM.java     |  927 +-------
 .../gemfire/test/dunit/rules/RemoteInvoker.java |    6 +-
 .../test/dunit/tests/BasicDUnitTest.java        |   12 +-
 .../gemfire/test/dunit/tests/VMDUnitTest.java   |   51 +-
 .../gemfire/codeAnalysis/excludedClasses.txt    |    3 +
 .../codeAnalysis/sanctionedSerializables.txt    |   23 +-
 .../cache/query/cq/dunit/CqDataDUnitTest.java   |   33 +-
 .../cq/dunit/CqDataUsingPoolDUnitTest.java      |   91 +-
 .../cache/query/cq/dunit/CqPerfDUnitTest.java   |   19 +-
 .../cq/dunit/CqPerfUsingPoolDUnitTest.java      |   17 +-
 .../cache/query/cq/dunit/CqQueryDUnitTest.java  |   94 +-
 .../dunit/CqQueryOptimizedExecuteDUnitTest.java |   12 +-
 .../cq/dunit/CqQueryUsingPoolDUnitTest.java     |   49 +-
 .../cq/dunit/CqResultSetUsingPoolDUnitTest.java |   34 +-
 ...ltSetUsingPoolOptimizedExecuteDUnitTest.java |    4 +-
 .../cache/query/cq/dunit/CqStatsDUnitTest.java  |    4 +-
 .../cq/dunit/CqStatsUsingPoolDUnitTest.java     |    4 +-
 .../PartitionedRegionCqQueryDUnitTest.java      |   34 +-
 ...dRegionCqQueryOptimizedExecuteDUnitTest.java |    6 +-
 .../query/cq/dunit/PrCqUsingPoolDUnitTest.java  |   28 +-
 .../cache/query/dunit/PdxQueryCQDUnitTest.java  |   14 +-
 .../cache/query/dunit/PdxQueryCQTestBase.java   |    2 +-
 .../dunit/QueryIndexUpdateRIDUnitTest.java      |   24 +-
 .../query/dunit/QueryMonitorDUnitTest.java      |    7 +-
 .../cache/PRDeltaPropagationDUnitTest.java      |  192 +-
 .../internal/cache/ha/CQListGIIDUnitTest.java   |  138 +-
 .../cache/ha/HADispatcherDUnitTest.java         |   50 +-
 .../sockets/ClientToServerDeltaDUnitTest.java   |  282 +--
 .../DeltaPropagationWithCQDUnitTest.java        |   41 +-
 ...ToRegionRelationCQRegistrationDUnitTest.java |  144 +-
 .../sockets/DurableClientCrashDUnitTest.java    |    4 +-
 .../sockets/DurableClientNetDownDUnitTest.java  |   10 +-
 .../sockets/DurableClientSimpleDUnitTest.java   |  442 ++--
 .../tier/sockets/DurableClientTestCase.java     |  206 +-
 .../CacheServerManagementDUnitTest.java         |    3 +-
 .../cli/commands/ClientCommandsDUnitTest.java   |    8 +-
 .../DurableClientCommandsDUnitTest.java         |    6 +-
 .../internal/pulse/TestCQDUnitTest.java         |    6 +-
 .../internal/pulse/TestClientsDUnitTest.java    |    6 +-
 .../internal/pulse/TestServerDUnitTest.java     |    3 +-
 .../ClientAuthorizationTwoDUnitTest.java        |   20 +-
 .../security/ClientAuthzObjectModDUnitTest.java |   23 +-
 .../ClientCQPostAuthorizationDUnitTest.java     |   62 +-
 .../ClientPostAuthorizationDUnitTest.java       |   49 +-
 .../gemfire/security/MultiuserAPIDUnitTest.java |   41 +-
 .../MultiuserDurableCQAuthzDUnitTest.java       |   52 +-
 geode-pulse/build.gradle                        |    8 +-
 .../src/main/resources/gemfire.properties       |    4 +-
 geode-pulse/src/main/webapp/DataBrowser.html    |   52 +-
 geode-pulse/src/main/webapp/Login.html          |   46 +-
 geode-pulse/src/main/webapp/MemberDetails.html  |   35 +-
 .../src/main/webapp/QueryStatistics.html        |   31 +-
 geode-pulse/src/main/webapp/clusterDetail.html  |   36 +-
 .../src/main/webapp/css/grips/horizontal.png    |  Bin 0 -> 2753 bytes
 .../src/main/webapp/css/grips/vertical.png      |  Bin 0 -> 91 bytes
 geode-pulse/src/main/webapp/css/style.css       |   44 +-
 .../src/main/webapp/images/about-geode.png      |  Bin 0 -> 7640 bytes
 .../main/webapp/images/apache_geode_logo.png    |  Bin 0 -> 23616 bytes
 .../src/main/webapp/images/pivotal-logo.png     |  Bin 4302 -> 3500 bytes
 .../main/webapp/properties/gemfire.properties   |    4 +-
 .../webapp/properties/gemfire_en.properties     |    4 +-
 .../main/webapp/properties/gemfirexd.properties |    2 +-
 .../webapp/properties/gemfirexd_en.properties   |    2 +-
 geode-pulse/src/main/webapp/regionDetail.html   |   33 +-
 .../src/main/webapp/scripts/lib/split.js        |  375 +++
 .../scripts/pulsescript/pages/DataBrowser.js    |   13 +-
 .../client/internal/GatewaySenderBatchOp.java   |    2 +-
 .../cache/CacheXml80GatewayDUnitTest.java       |   72 +-
 .../internal/cache/UpdateVersionDUnitTest.java  |   88 +-
 .../gemfire/internal/cache/wan/WANTestBase.java |   33 +-
 ...oncurrentParallelGatewaySenderDUnitTest.java | 1164 +++++-----
 ...allelGatewaySenderOperation_1_DUnitTest.java | 1063 +++++----
 ...allelGatewaySenderOperation_2_DUnitTest.java |   63 +-
 .../ConcurrentWANPropogation_1_DUnitTest.java   |  644 +++---
 .../ConcurrentWANPropogation_2_DUnitTest.java   |  699 +++---
 .../cache/wan/disttx/DistTXWANDUnitTest.java    |  258 +--
 .../CommonParallelGatewaySenderDUnitTest.java   |  459 ++--
 ...wWANConcurrencyCheckForDestroyDUnitTest.java |  168 +-
 .../cache/wan/misc/PDXNewWanDUnitTest.java      |  744 +++---
 ...dRegion_ParallelWANPersistenceDUnitTest.java |  767 +++---
 ...dRegion_ParallelWANPropogationDUnitTest.java | 1014 ++++----
 .../SenderWithTransportFilterDUnitTest.java     |   66 +-
 ...downAllPersistentGatewaySenderDUnitTest.java |   48 +-
 .../wan/misc/WANLocatorServerDUnitTest.java     |   40 +-
 .../cache/wan/misc/WANSSLDUnitTest.java         |   98 +-
 .../wan/misc/WanAutoDiscoveryDUnitTest.java     |  284 +--
 .../cache/wan/misc/WanValidationsDUnitTest.java | 1594 ++++++-------
 ...arallelGatewaySenderOperationsDUnitTest.java |  273 ++-
 ...llelGatewaySenderQueueOverflowDUnitTest.java |  476 ++--
 ...ersistenceEnabledGatewaySenderDUnitTest.java | 1851 +++++++--------
 ...llelWANPropagationClientServerDUnitTest.java |   94 +-
 ...lelWANPropagationConcurrentOpsDUnitTest.java |  264 ++-
 .../ParallelWANPropagationDUnitTest.java        | 2015 ++++++++--------
 ...ParallelWANPropagationLoopBackDUnitTest.java |  438 ++--
 .../wan/parallel/ParallelWANStatsDUnitTest.java |  366 ++-
 ...tewaySenderDistributedDeadlockDUnitTest.java |  171 +-
 ...rialGatewaySenderEventListenerDUnitTest.java |  312 ++-
 .../SerialGatewaySenderOperationsDUnitTest.java |   26 +-
 .../SerialGatewaySenderQueueDUnitTest.java      |  224 +-
 ...ersistenceEnabledGatewaySenderDUnitTest.java |  707 +++---
 .../SerialWANPropagationLoopBackDUnitTest.java  |  547 ++---
 .../serial/SerialWANPropogationDUnitTest.java   | 1674 +++++++-------
 ...NPropogation_PartitionedRegionDUnitTest.java |  693 +++---
 .../SerialWANPropogationsFeatureDUnitTest.java  |  562 +++--
 .../wan/serial/SerialWANStatsDUnitTest.java     |  733 +++---
 .../wan/wancommand/WANCommandTestBase.java      |   16 +-
 ...anCommandCreateGatewayReceiverDUnitTest.java |  274 +--
 .../WanCommandCreateGatewaySenderDUnitTest.java |  302 +--
 ...WanCommandGatewayReceiverStartDUnitTest.java |  171 +-
 .../WanCommandGatewayReceiverStopDUnitTest.java |  175 +-
 .../WanCommandGatewaySenderStartDUnitTest.java  |  289 ++-
 .../WanCommandGatewaySenderStopDUnitTest.java   |  273 +--
 .../wan/wancommand/WanCommandListDUnitTest.java |  172 +-
 .../WanCommandPauseResumeDUnitTest.java         |  630 +++--
 .../wancommand/WanCommandStatusDUnitTest.java   |  234 +-
 .../management/WANManagementDUnitTest.java      |  108 +-
 .../pulse/TestRemoteClusterDUnitTest.java       |   46 +-
 443 files changed, 23025 insertions(+), 29264 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-assembly/build.gradle
----------------------------------------------------------------------
diff --cc geode-assembly/build.gradle
index 2c7e365,0000000..2a46147
mode 100755,000000..100755
--- a/geode-assembly/build.gradle
+++ b/geode-assembly/build.gradle
@@@ -1,340 -1,0 +1,345 @@@
 +/*
 + * 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.
 + */
 +import org.gradle.api.internal.artifacts.publish.ArchivePublishArtifact;
 +
 +apply plugin: 'distribution'
 +
 +// disable artifact generation for this project
 +jar.enabled = false
 +extraArchive {
 +  sources = false
 +  javadoc = false
 +  tests = false
 +}
 +
 +disableMavenPublishing()
 +
 +// Gradle doesn't automatically remove the jar artifact even though we disabled it
 +// this causes publishing to fail.  So we nuke all the disabled artifacts from all configurations.
 +configurations.all {
 +  artifacts.removeAll artifacts.findAll { it instanceof ArchivePublishArtifact && !it.archiveTask.enabled }
 +}
 +
 +gradle.taskGraph.whenReady( { graph ->
 +  tasks.withType(Tar).each { tar ->
 +    tar.compression = Compression.GZIP
 +    tar.extension = 'tar.gz'
 +  }
 +})
 +
 +dependencies {
 +  provided project(':geode-core')
 +  
 +  archives project(':geode-common')  
 +  archives project(':geode-json')  
 +  archives project(':geode-joptsimple')  
 +  archives project(':geode-core')  
 +  archives project(':geode-lucene')
 +  archives project(':geode-web')
 +  archives project(':geode-web-api')
 +  archives project(':geode-pulse')
 +  archives project(':geode-wan')
 +  archives project(':geode-cq')
 +
 +  testCompile project(':geode-junit')
 +  testCompile project(path: ':geode-core', configuration: 'testOutput')
 +
 +  testRuntime files("${System.getProperty('java.home')}/../lib/tools.jar")
 +}
 +
 +sourceSets {
 +  // need to remove this since we use the dependencies jar out of the install dir
 +  //test.runtimeClasspath -= configurations.provided
 +}
 +
 +test {
 +  // test from the actual classpath not the gradle classpath
 +  dependsOn installDist
 +  // @TODO: this doesn't seem to be working need to get basename first.
 +  classpath += files "$buildDir/install/apache-geode/lib/geode-dependencies.jar"
 +}
 +
 +tasks.withType(Test){
 +  environment 'GEMFIRE', "$buildDir/install/${distributions.main.baseName}/lib"
 +}
 +
 +task defaultDistributionConfig(type: JavaExec, dependsOn: classes) {
 +  outputs.file file("$buildDir/gemfire.properties")
 +  main 'com.gemstone.gemfire.distributed.internal.DistributionConfigImpl'
 +  classpath project(':geode-core').sourceSets.main.runtimeClasspath
 +  workingDir buildDir
 +  
 +  doFirst {
 +    buildDir.mkdirs()
 +  }
 +}
 +
 +task defaultCacheConfig(type: JavaExec, dependsOn: classes) {
 +  outputs.file file("$buildDir/cache.xml")
 +  main 'com.gemstone.gemfire.internal.cache.xmlcache.CacheXmlGenerator'
 +  classpath project(':geode-core').sourceSets.main.runtimeClasspath
 +  workingDir buildDir
 +
 +  doFirst {
 +    buildDir.mkdirs()
 +  }
 +}
 +
 +// This closure sets the gemfire classpath.  If we add another jar to the classpath it must
 +// be included in the filter logic below.
 +def cp = {
 +  // first add all the dependent project jars
 +  def jars = configurations.archives.dependencies.collect { it.dependencyProject }
 +    .findAll { !it.name.contains('web') }
 +    .collect { it.jar.archiveName }
 +    .join(' ')
 +
 +  // then add all the dependencies of the dependent jars
 +  jars += ' ' + configurations.archives.dependencies.collect { 
 +    it.dependencyProject.configurations.runtime.collect { it.getName() }.findAll {
 +      // depedencies from geode-core
 +      it.contains('antlr') ||
 +      it.contains('commons-io') ||
 +      it.contains('commons-lang') ||
 +      it.contains('commons-logging') ||
 +      it.contains('fastutil') ||
 +      it.contains('jackson-annotations') ||
 +      it.contains('jackson-core') ||
 +      it.contains('jackson-databind') ||
 +      it.contains('jansi') ||
 +      it.contains('javax.resource-api') ||
 +      it.contains('javax.servlet-api') ||
 +      it.contains('javax.transaction-api') ||
 +      it.contains('jetty-http') ||
 +      it.contains('jetty-io') ||
 +      it.contains('jetty-security') ||
 +      it.contains('jetty-server') ||
 +      it.contains('jetty-servlet') ||
 +      it.contains('jetty-webapp') ||
 +      it.contains('jetty-util') ||
 +      it.contains('jetty-xml') ||
 +      it.contains('jline') ||
 +      it.contains('jna') ||
 +      it.contains('log4j-api') ||
 +      it.contains('log4j-core') ||
 +      it.contains('log4j-jcl') ||
 +      it.contains('log4j-jul') ||
 +      it.contains('log4j-slf4j-impl') ||
 +      it.contains('slf4j-api') ||
 +      it.contains('spring-core') ||
 +      it.contains('spring-shell') ||
 +      it.contains('snappy-java') ||
 +      it.contains('hbase') ||
 +      it.contains('jgroups') ||
 +      it.contains('netty') ||
 +      
 +      // dependencies from geode-lucene
 +      it.contains('lucene-analyzers-common') ||
 +      it.contains('lucene-core') ||
 +      it.contains('lucene-queries') ||
 +      it.contains('lucene-queryparser')
 +    }
 +  }.flatten().unique().join(' ')
 +
 +  return jars
 +}
 +
 +// Note: this dependency doesn't work if you change a library version from
 +// a dependent project.  Please fix me.
 +task depsJar (type: Jar, dependsOn: ':geode-core:classes') {
 +  description 'Assembles the jar archive that defines the gemfire classpath.'
 +  archiveName 'geode-dependencies.jar'
 +  doFirst {
 +    manifest { 
 +      attributes("Class-Path": cp())
 +    }
 +  }
 +}
 +
 +// Note: this dependency doesn't work if you change a library version from
 +// a dependent project.  Please fix me.
 +task gfshDepsJar (type: Jar, dependsOn: ':geode-core:classes') {
 +  description 'Assembles the jar archive that defines the gfsh classpath.'
 +  def springWeb = configurations.runtime.collect { it.getName() }.find { it.contains('spring-web') }
 +  archiveName 'gfsh-dependencies.jar'
 +  doFirst {
 +    manifest {
 +      attributes("Class-Path": cp() + 
 +        ' ' + project(':geode-core').webJar.archiveName +
 +        ' ' + springWeb
 +      )
 +    }
 +  }
 +}
 +
 +
 +def docsDir = file("$buildDir/javadocs")
 +task docs(type: Javadoc) {
 +    options.addStringOption('Xdoclint:none', '-quiet')
 +    options.encoding='UTF-8'
 +    source parent.subprojects*.javadoc*.source
 +    classpath = files(parent.subprojects*.javadoc*.classpath)
 +    title = "Apache Geode ${project.version}"
 +    include 'com/gemstone/gemfire/**/'
 +    exclude 'com/gemstone/gemfire/internal/**/'
 +    exclude 'com/gemstone/gemfire/**/internal/**/'
 +    exclude 'com/gemstone/gemfire/**/xml/**/'
 +    exclude 'com/gemstone/gemfire/distributed/**/util/**/'
 +    exclude 'com/gemstone/gemfire/test/**/'
 +    destinationDir = docsDir
 +}
 +
 +gradle.taskGraph.whenReady( { graph ->
 +  tasks.withType(AbstractArchiveTask).findAll {
 +    it.name.toLowerCase().contains("dist")
 +  }.each { archive ->
 +    archive.doLast {
 +      ant.checksum file:"${archive.archivePath}", algorithm:"md5"
 +      ant.checksum file:"${archive.archivePath}", algorithm:"sha-256"
 +    }
 +  }
 +})
 +
 +distributions {
 +  src {
 +    baseName = 'apache-geode-src'
 +    contents {
 +      from (rootDir) {
 +        exclude 'KEYS'
 +        exclude 'gradlew'
 +        exclude 'gradlew.bat'
 +        exclude 'gradle/wrapper/gradle-wrapper.jar'
-         exclude 'gradle/wrapper/gradle-wrapper.properties'
 +        exclude '.gradle'
 +        exclude '**/build/**'
 +        exclude '**/.project'
 +        exclude '**/.classpath'
 +        exclude '**/.settings/**'
 +        exclude '**/build-eclipse/**'
 +        exclude '.idea/**'
 +        exclude '**/*.iml'
 +        exclude '**/*.ipr'
 +        exclude '**/*.iws'
 +        exclude '**/tags'
++
++        //These directories are generated on the jenkins server by gradle
++        exclude 'caches'
++        exclude 'daemon'
++        exclude 'native'
++        exclude 'wrapper'
 +      }
 +    }
 +  }
 +
 +  main {
 +    baseName = 'apache-geode' //TODO rootProject.name
 +    contents {
 +      duplicatesStrategy 'exclude'
 +      exclude '*.asc'
 +      
 +      exclude '*.asc'
 +      exclude '*-sources.jar'
 +      exclude '*-javadoc.jar'
 +
 +      into ('config') {
 +        from defaultCacheConfig
 +        from defaultDistributionConfig
 +        from (project(':geode-core').sourceSets.main.resources.files.find {
 +          it.name == 'log4j2.xml' 
 +        })
 +      }
 +      
 +      into ('lib') {
 +        from project(":geode-common").configurations.runtime
 +        from project(":geode-common").configurations.archives.allArtifacts.files
 +
 +        from project(":geode-json").configurations.runtime
 +        from project(":geode-json").configurations.archives.allArtifacts.files
 +
 +        from project(":geode-joptsimple").configurations.runtime
 +        from project(":geode-joptsimple").configurations.archives.allArtifacts.files
 +
 +        from project(":geode-wan").configurations.runtime
 +        from project(":geode-wan").configurations.archives.allArtifacts.files
 +
 +        from project(":geode-cq").configurations.runtime
 +        from project(":geode-cq").configurations.archives.allArtifacts.files
 +
 +        from project(":geode-core").configurations.runtime
 +        // Copying from provided configuration is only for supporting Spring Data GemFire.
 +        // If there are more dependencies added to provided configuration, this will need
 +        // to change
 +        from (project(":geode-core").configurations.provided) {
 +          include 'spring-data-gemfire-*'
 +        }
 +        from project(":geode-core").configurations.archives.allArtifacts.files
 +        from project(":geode-core").webJar
 +        from project(":geode-core").raJar
 +        from project(":geode-core").jcaJar
 +        
 +        from project(":geode-lucene").configurations.runtime
 +        from project(":geode-lucene").configurations.archives.allArtifacts.files
 +
 +        // include this jar        
 +        from project(":geode-web-api").jar
 +        
 +        // dependency jars
 +        from depsJar
 +        from gfshDepsJar
 +      }
 +
 +      into ('tools/Extensions') {
 +        from (project(":geode-web").configurations.archives.allArtifacts.files) {
 +          exclude '*.jar'
 +        }
 +        from (project(":geode-web-api").configurations.archives.allArtifacts.files) {
 +          exclude '*.jar'
 +        }
 +      }
 +
 +      into ('javadoc') {
 +        from docs
 +      }
 +
 +      into ('tools/Pulse') {
 +        from (project(":geode-pulse").configurations.archives.allArtifacts.files) {
 +          exclude '*.jar'
 +        }
 +      }
 +
 +      into ('tools/Modules') {
 +        from (project(':extensions/geode-modules-assembly').configurations.moduleDistOutputs.files)
 +      }
 +    }
 +  }
 +}
 +
 +// Create a configuration closure to configure test targets with the install directory
 +def dependOnInstalledProduct = {
 +  dependsOn installDist
 +  def install = file("$buildDir/install/${distributions.main.baseName}")
 +  environment ('GEMFIRE', install)
 +}
 +
 +// Add the configuration closure to the test targets so they depend on the install directory
 +test dependOnInstalledProduct
 +distributedTest dependOnInstalledProduct
 +integrationTest dependOnInstalledProduct
 +
 +// Make build final task to generate all test and product resources
 +build.dependsOn installDist
 +
 +installDist.dependsOn ':extensions/geode-modules-assembly:dist'

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-assembly/src/main/dist/LICENSE
----------------------------------------------------------------------
diff --cc geode-assembly/src/main/dist/LICENSE
index 12ffb42,0000000..2f1374b
mode 100644,000000..100644
--- a/geode-assembly/src/main/dist/LICENSE
+++ b/geode-assembly/src/main/dist/LICENSE
@@@ -1,429 -1,0 +1,434 @@@
 +                                 Apache License
 +                           Version 2.0, January 2004
 +                        http://www.apache.org/licenses/
 +
 +   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
 +
 +   1. Definitions.
 +
 +      "License" shall mean the terms and conditions for use, reproduction,
 +      and distribution as defined by Sections 1 through 9 of this document.
 +
 +      "Licensor" shall mean the copyright owner or entity authorized by
 +      the copyright owner that is granting the License.
 +
 +      "Legal Entity" shall mean the union of the acting entity and all
 +      other entities that control, are controlled by, or are under common
 +      control with that entity. For the purposes of this definition,
 +      "control" means (i) the power, direct or indirect, to cause the
 +      direction or management of such entity, whether by contract or
 +      otherwise, or (ii) ownership of fifty percent (50%) or more of the
 +      outstanding shares, or (iii) beneficial ownership of such entity.
 +
 +      "You" (or "Your") shall mean an individual or Legal Entity
 +      exercising permissions granted by this License.
 +
 +      "Source" form shall mean the preferred form for making modifications,
 +      including but not limited to software source code, documentation
 +      source, and configuration files.
 +
 +      "Object" form shall mean any form resulting from mechanical
 +      transformation or translation of a Source form, including but
 +      not limited to compiled object code, generated documentation,
 +      and conversions to other media types.
 +
 +      "Work" shall mean the work of authorship, whether in Source or
 +      Object form, made available under the License, as indicated by a
 +      copyright notice that is included in or attached to the work
 +      (an example is provided in the Appendix below).
 +
 +      "Derivative Works" shall mean any work, whether in Source or Object
 +      form, that is based on (or derived from) the Work and for which the
 +      editorial revisions, annotations, elaborations, or other modifications
 +      represent, as a whole, an original work of authorship. For the purposes
 +      of this License, Derivative Works shall not include works that remain
 +      separable from, or merely link (or bind by name) to the interfaces of,
 +      the Work and Derivative Works thereof.
 +
 +      "Contribution" shall mean any work of authorship, including
 +      the original version of the Work and any modifications or additions
 +      to that Work or Derivative Works thereof, that is intentionally
 +      submitted to Licensor for inclusion in the Work by the copyright owner
 +      or by an individual or Legal Entity authorized to submit on behalf of
 +      the copyright owner. For the purposes of this definition, "submitted"
 +      means any form of electronic, verbal, or written communication sent
 +      to the Licensor or its representatives, including but not limited to
 +      communication on electronic mailing lists, source code control systems,
 +      and issue tracking systems that are managed by, or on behalf of, the
 +      Licensor for the purpose of discussing and improving the Work, but
 +      excluding communication that is conspicuously marked or otherwise
 +      designated in writing by the copyright owner as "Not a Contribution."
 +
 +      "Contributor" shall mean Licensor and any individual or Legal Entity
 +      on behalf of whom a Contribution has been received by Licensor and
 +      subsequently incorporated within the Work.
 +
 +   2. Grant of Copyright License. Subject to the terms and conditions of
 +      this License, each Contributor hereby grants to You a perpetual,
 +      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
 +      copyright license to reproduce, prepare Derivative Works of,
 +      publicly display, publicly perform, sublicense, and distribute the
 +      Work and such Derivative Works in Source or Object form.
 +
 +   3. Grant of Patent License. Subject to the terms and conditions of
 +      this License, each Contributor hereby grants to You a perpetual,
 +      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
 +      (except as stated in this section) patent license to make, have made,
 +      use, offer to sell, sell, import, and otherwise transfer the Work,
 +      where such license applies only to those patent claims licensable
 +      by such Contributor that are necessarily infringed by their
 +      Contribution(s) alone or by combination of their Contribution(s)
 +      with the Work to which such Contribution(s) was submitted. If You
 +      institute patent litigation against any entity (including a
 +      cross-claim or counterclaim in a lawsuit) alleging that the Work
 +      or a Contribution incorporated within the Work constitutes direct
 +      or contributory patent infringement, then any patent licenses
 +      granted to You under this License for that Work shall terminate
 +      as of the date such litigation is filed.
 +
 +   4. Redistribution. You may reproduce and distribute copies of the
 +      Work or Derivative Works thereof in any medium, with or without
 +      modifications, and in Source or Object form, provided that You
 +      meet the following conditions:
 +
 +      (a) You must give any other recipients of the Work or
 +          Derivative Works a copy of this License; and
 +
 +      (b) You must cause any modified files to carry prominent notices
 +          stating that You changed the files; and
 +
 +      (c) You must retain, in the Source form of any Derivative Works
 +          that You distribute, all copyright, patent, trademark, and
 +          attribution notices from the Source form of the Work,
 +          excluding those notices that do not pertain to any part of
 +          the Derivative Works; and
 +
 +      (d) If the Work includes a "NOTICE" text file as part of its
 +          distribution, then any Derivative Works that You distribute must
 +          include a readable copy of the attribution notices contained
 +          within such NOTICE file, excluding those notices that do not
 +          pertain to any part of the Derivative Works, in at least one
 +          of the following places: within a NOTICE text file distributed
 +          as part of the Derivative Works; within the Source form or
 +          documentation, if provided along with the Derivative Works; or,
 +          within a display generated by the Derivative Works, if and
 +          wherever such third-party notices normally appear. The contents
 +          of the NOTICE file are for informational purposes only and
 +          do not modify the License. You may add Your own attribution
 +          notices within Derivative Works that You distribute, alongside
 +          or as an addendum to the NOTICE text from the Work, provided
 +          that such additional attribution notices cannot be construed
 +          as modifying the License.
 +
 +      You may add Your own copyright statement to Your modifications and
 +      may provide additional or different license terms and conditions
 +      for use, reproduction, or distribution of Your modifications, or
 +      for any such Derivative Works as a whole, provided Your use,
 +      reproduction, and distribution of the Work otherwise complies with
 +      the conditions stated in this License.
 +
 +   5. Submission of Contributions. Unless You explicitly state otherwise,
 +      any Contribution intentionally submitted for inclusion in the Work
 +      by You to the Licensor shall be under the terms and conditions of
 +      this License, without any additional terms or conditions.
 +      Notwithstanding the above, nothing herein shall supersede or modify
 +      the terms of any separate license agreement you may have executed
 +      with Licensor regarding such Contributions.
 +
 +   6. Trademarks. This License does not grant permission to use the trade
 +      names, trademarks, service marks, or product names of the Licensor,
 +      except as required for reasonable and customary use in describing the
 +      origin of the Work and reproducing the content of the NOTICE file.
 +
 +   7. Disclaimer of Warranty. Unless required by applicable law or
 +      agreed to in writing, Licensor provides the Work (and each
 +      Contributor provides its Contributions) on an "AS IS" BASIS,
 +      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 +      implied, including, without limitation, any warranties or conditions
 +      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
 +      PARTICULAR PURPOSE. You are solely responsible for determining the
 +      appropriateness of using or redistributing the Work and assume any
 +      risks associated with Your exercise of permissions under this License.
 +
 +   8. Limitation of Liability. In no event and under no legal theory,
 +      whether in tort (including negligence), contract, or otherwise,
 +      unless required by applicable law (such as deliberate and grossly
 +      negligent acts) or agreed to in writing, shall any Contributor be
 +      liable to You for damages, including any direct, indirect, special,
 +      incidental, or consequential damages of any character arising as a
 +      result of this License or out of the use or inability to use the
 +      Work (including but not limited to damages for loss of goodwill,
 +      work stoppage, computer failure or malfunction, or any and all
 +      other commercial damages or losses), even if such Contributor
 +      has been advised of the possibility of such damages.
 +
 +   9. Accepting Warranty or Additional Liability. While redistributing
 +      the Work or Derivative Works thereof, You may choose to offer,
 +      and charge a fee for, acceptance of support, warranty, indemnity,
 +      or other liability obligations and/or rights consistent with this
 +      License. However, in accepting such obligations, You may act only
 +      on Your own behalf and on Your sole responsibility, not on behalf
 +      of any other Contributor, and only if You agree to indemnify,
 +      defend, and hold each Contributor harmless for any liability
 +      incurred by, or claims asserted against, such Contributor by reason
 +      of your accepting any such warranty or additional liability.
 +
 +   END OF TERMS AND CONDITIONS
 +
 +   APPENDIX: How to apply the Apache License to your work.
 +
 +      To apply the Apache License to your work, attach the following
 +      boilerplate notice, with the fields enclosed by brackets "[]"
 +      replaced with your own identifying information. (Don't include
 +      the brackets!)  The text should be enclosed in the appropriate
 +      comment syntax for the file format. We also recommend that a
 +      file or class name and description of purpose be included on the
 +      same "printed page" as the copyright notice for easier
 +      identification within third-party archives.
 +
 +   Copyright [yyyy] [name of copyright owner]
 +
 +   Licensed 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.
 +
 +===========================================================================
 +
 +Apache Geode includes a number of components and libraries with separate 
 +copyright notices and license terms. Your use of those components are 
 +subject to the terms and conditions of the following licenses. 
 +
 +
 +---------------------------------------------------------------------------
 +Antlr (http://www.antlr.org/) antlr:antlr:2.7.7
 +---------------------------------------------------------------------------
 +This product bundles Antlr which is available under a BSD license.  
 +For details see http://www.antlr.org/license.html.
 +
 +---------------------------------------------------------------------------
 +Backbone (http://backbonejs.org)
 +---------------------------------------------------------------------------
 +This product bundles Backbone which is available under an MIT license.  
 +For details see http://www.opensource.org/licenses/mit-license.php.
 +
 +---------------------------------------------------------------------------
 +CompactConcurrentHashSet2 (http://gee.cs.oswego.edu/dl/concurrency-interest)
 +---------------------------------------------------------------------------
 +This product bundles CompactConcurrentHashSet2 derived from JSR-166 
 +ConcurrentHashMap v1.43 which is available in the public domain.  
 +
 +---------------------------------------------------------------------------
 +Handlebars (http://handlebarsjs.com)
 +---------------------------------------------------------------------------
 +This product bundles Handlebars which is available under an MIT license.  
 +For details see http://www.opensource.org/licenses/mit-license.php.
 +
 +---------------------------------------------------------------------------
 +highlight.js (https://highlightjs.org)
 +---------------------------------------------------------------------------
 +This product bundles highlight.js which is available under a BSD 3 clauses license.  
 +For details see http://opensource.org/licenses/BSD-3-Clause.
 +
 +---------------------------------------------------------------------------
 +javax.activation (http://java.sun.com/javase/technologies/desktop/javabeans/jaf/index.jsp) javax.activation:activation:1.1.1
 +---------------------------------------------------------------------------
 +This product bundles javax.activation which is available under a CDDL license.  
 +For details see https://glassfish.dev.java.net/public/CDDLv1.0.html.
 +
 +---------------------------------------------------------------------------
 +javax.mail (http://www.oracle.com/) javax.mail:javax.mail-api:1.4.5
 +---------------------------------------------------------------------------
 +This product bundles javax.mail which is available under a CDDL or GPLv2+CE license.  
 +For details see http://www.sun.com/cddl or https://glassfish.dev.java.net/public/CDDL+GPL.html.
 +
 +---------------------------------------------------------------------------
 +javax.resource (https://glassfish.java.net/) javax.resource:javax.resource-api:1.7
 +---------------------------------------------------------------------------
 +This product bundles javax.resource which is available under a CDDL or GPLv2+CE license.  
 +For details see https://glassfish.dev.java.net/public/CDDL+GPL.html.
 +
 +---------------------------------------------------------------------------
 +javax.servlet (https://glassfish.java.net/) javax.servlet:javax.servlet-api:3.1.0
 +---------------------------------------------------------------------------
 +This product bundles javax.servlet which is available under a CDDL or GPLv2+CE license.  
 +For details see https://glassfish.dev.java.net/public/CDDL+GPL.html.
 +
 +---------------------------------------------------------------------------
 +javax.transaction (https://glassfish.java.net/) javax.transaction:javax.transaction-api:1.2
 +---------------------------------------------------------------------------
 +This product bundles javax.transaction which is available under a CDDL or GPLv2+CE license.  
 +For details see https://glassfish.dev.java.net/public/CDDL+GPL.html.
 +
 +---------------------------------------------------------------------------
 +JLine (http://jline.sourceforge.net) jline:jline:2.12
 +---------------------------------------------------------------------------
 +This product bundles JLine which is available under a BSD license.  
 +For details see http://www.opensource.org/licenses/bsd-license.php.
 +
 +---------------------------------------------------------------------------
 +JOpt Simple (http://pholser.github.io/jopt-simple/)
 +---------------------------------------------------------------------------
 +This product bundles JOpt Simple which is available under an MIT license.  
 +For details see http://www.opensource.org/licenses/mit-license.php.
 +
 +---------------------------------------------------------------------------
 +jQuery (https://jquery.com)
 +---------------------------------------------------------------------------
 +This product bundles jQuery which is available under an MIT license.  
 +For details see http://www.opensource.org/licenses/mit-license.php.
 +
 +---------------------------------------------------------------------------
 +jQuery BBQ (http://benalman.com/projects/jquery-bbq-plugin/)
 +---------------------------------------------------------------------------
 +This product bundles jQuery BBQ which is available under an MIT and GPL dual license.  
 +For details see http://benalman.com/about/license.
 +
 +---------------------------------------------------------------------------
 +jQuery-slideto (https://github.com/Sleavely/jQuery-slideto)
 +---------------------------------------------------------------------------
 +This product bundles jQuery-slideto which is available under an MIT license.  
 +For details see http://www.opensource.org/licenses/mit-license.php.
 +
 +---------------------------------------------------------------------------
 +jQuery Wiggle (https://github.com/wilhelm-murdoch/jQuery-Wiggle)
 +---------------------------------------------------------------------------
 +This product bundles jQuery Wiggle which is available under an MIT license.  
 +For details see http://www.opensource.org/licenses/mit-license.php.
 +
 +---------------------------------------------------------------------------
 +JSON (http://json.org)
 +---------------------------------------------------------------------------
 +This product bundles JSON which is available under a BSD-like license.  
 +For details see http://json.org/license.html.
 +
 +---------------------------------------------------------------------------
 +Paranamer (https://github.com/paul-hammant/paranamer) com.thoughtworks.paranamer:paranamer:2.3
 +---------------------------------------------------------------------------
 +This product bundles Paranamer which is available under a BSD license.  
 +For details see http://www.opensource.org/licenses/bsd-license.php.
 +
 +---------------------------------------------------------------------------
 +reset.css (http://meyerweb.com/eric/tools/css/reset/)
 +---------------------------------------------------------------------------
 +This product bundles reset.css which is available in the public domain.
 +
 +---------------------------------------------------------------------------
 +tooltip.js (http://flowplayer.org/tools/tooltip/)
 +---------------------------------------------------------------------------
 +This product bundles tooltip.css which is available in the public domain.
 +
 +---------------------------------------------------------------------------
 +scala-reflect (http://www.scala-lang.org/) org.scala-lang:scala-reflect:2.10.0
 +---------------------------------------------------------------------------
 +This product bundles scala-reflect which is available under a BSD-like license.  
 +For details see http://www.scala-lang.org/downloads/license.html.
 +
 +---------------------------------------------------------------------------
 +scala-library (http://www.scala-lang.org/) org.scala-lang:scala-library:2.10.0
 +---------------------------------------------------------------------------
 +This product bundles scala-library which is available under a BSD-like license.  
 +For details see http://www.scala-lang.org/downloads/license.html.
 +
 +---------------------------------------------------------------------------
 +shred (https://github.com/pandastrike/shred)
 +---------------------------------------------------------------------------
 +This product bundles shred which is available under an MIT or ISC license.  
 +For details see https://github.com/pandastrike/shred/blob/master/LICENSE.
 +
 +---------------------------------------------------------------------------
 +SLF4J API (http://www.slf4j.org) org.slf4j:slf4j-api:jar:1.7.7
 +---------------------------------------------------------------------------
 +This product bundles SLF4J API which is available under an MIT license.  
 +For details see http://www.opensource.org/licenses/mit-license.php.
 +
 +---------------------------------------------------------------------------
 +underscore (http://underscorejs.org)
 +---------------------------------------------------------------------------
 +This product bundles underscore which is available under an MIT license.  
 +For details see http://www.opensource.org/licenses/mit-license.php.
 +
 +---------------------------------------------------------------------------
 +jquery jqGrid (https://plugins.jquery.com/jqGrid/)
 +---------------------------------------------------------------------------
 +This product bundles jqGrid which is available under an MIT license.
 +For details see http://www.opensource.org/licenses/mit-license.php.
 +
 +---------------------------------------------------------------------------
 +html5.js (https://github.com/aFarkas/html5shiv)
 +---------------------------------------------------------------------------
 +This product bundles html5 which is available under an MIT license.
 +For details see http://www.opensource.org/licenses/mit-license.php.
 +
 +---------------------------------------------------------------------------
 +jquery.i18n.properties (https://github.com/jquery-i18n-properties/jquery-i18n-properties)
 +---------------------------------------------------------------------------
 +This product bundles jQuery i18n which is available under an MIT license.
 +For details see http://www.opensource.org/licenses/mit-license.php.
 +
 +---------------------------------------------------------------------------
 +jquery.jscrollpane (http://jscrollpane.kelvinluck.com/)
 +---------------------------------------------------------------------------
 +This product bundles jquery.jscrollpane which is available under an MIT license.
 +For details see http://www.opensource.org/licenses/mit-license.php.
 +
 +---------------------------------------------------------------------------
 +jquery.mousewheel (http://brandonaaron.net)
 +---------------------------------------------------------------------------
 +This product bundles jquery.mousewheel which is available under an MIT license.
 +For details see http://www.opensource.org/licenses/mit-license.php.
 +
 +---------------------------------------------------------------------------
 +jquery.placeholder (http://webcloud.se)
 +---------------------------------------------------------------------------
 +This product bundles jquery.placeholder which is available under an MIT license.
 +For details see http://www.opensource.org/licenses/mit-license.php.
 +
 +---------------------------------------------------------------------------
 +jquery.tablednd (https://github.com/isocra/TableDnD)
 +---------------------------------------------------------------------------
 +This product bundles jquery.tablednd which is available under an MIT license.
 +For details see http://www.opensource.org/licenses/mit-license.php.
 +
 +---------------------------------------------------------------------------
 +jquery.timeago (http://timeago.yarp.com/)
 +---------------------------------------------------------------------------
 +This product bundles jquery.timeago which is available under an MIT license.
 +For details see http://www.opensource.org/licenses/mit-license.php.
 +
 +---------------------------------------------------------------------------
 +jquery.ztree (http://zTree.me/)
 +---------------------------------------------------------------------------
 +This product bundles jquery.ztree which is available under an MIT license.
 +For details see http://www.opensource.org/licenses/mit-license.php.
 +
 +---------------------------------------------------------------------------
 +jquery.multiselect (http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/)
 +---------------------------------------------------------------------------
 +This product bundles jQuery.multiselect which is available under an MIT license.
 +For details see http://www.opensource.org/licenses/mit-license.php.
 +
 +---------------------------------------------------------------------------
 +jquery.ui (http://jqueryui.com/about)
 +---------------------------------------------------------------------------
 +This product bundles jquery UI which is available under an MIT license.
 +For details see http://www.opensource.org/licenses/mit-license.php.
 +
 +---------------------------------------------------------------------------
 +jquery.sparkline.js (http://omnipotent.net/jquery.sparkline/)
 +---------------------------------------------------------------------------
 +This product bundles jquery.sparkline which is available under a BSD-like license.
 +For details see http://opensource.org/licenses/BSD-3-Clause.
 +
++---------------------------------------------------------------------------
++split.js (https://github.com/nathancahill/Split.js)
++---------------------------------------------------------------------------
++This product bundles split.js which is available under an MIT license.
++For details see http://www.opensource.org/licenses/mit-license.php.

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-assembly/src/src/dist/gradlew
----------------------------------------------------------------------
diff --cc geode-assembly/src/src/dist/gradlew
index 0000000,0000000..de155c6
new file mode 100755
--- /dev/null
+++ b/geode-assembly/src/src/dist/gradlew
@@@ -1,0 -1,0 +1,221 @@@
++#!/usr/bin/env bash
++
++# 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.
++
++##
++## Tries to recreate Gradle's gradlew command in pure bash.
++## This way you don't have to worry about binaries in your build.
++##
++## Depdencies
++## unzip
++##
++
++set -e
++set -o pipefail
++
++APP_NAME="Gradle"
++APP_BASE_NAME=`basename "$0"`
++
++# Use the maximum available, or set MAX_FD != -1 to use that value.
++    MAX_FD="maximum"
++
++# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
++DEFAULT_JVM_OPTS="-Dorg.gradle.appname=$APP_BASE_NAME"
++
++bin=`dirname "$0"`
++bin=`cd "$bin">/dev/null; pwd`
++
++if [ -e "$bin/gradle/wrapper/gradle-wrapper.properties" ]; then
++  . "$bin/gradle/wrapper/gradle-wrapper.properties"
++else
++  # the location that the wrapper is at doesn't have a properties
++  # check PWD, gradlew may be shared
++  if [ -e "$PWD/gradle/wrapper/gradle-wrapper.properties" ]; then
++    . "$PWD/gradle/wrapper/gradle-wrapper.properties"
++  else
++    echo "Unable to locate gradle-wrapper.properties.  Not at $PWD/gradle/wrapper/gradle-wrapper.properties or $bin/gradle/wrapper/gradle-wrapper.properties" 1>&2
++    exit 1
++  fi
++fi
++
++warn ( ) {
++    echo "$*"
++}
++
++die ( ) {
++    echo
++    echo "$*"
++    echo
++    exit 1
++}
++
++# OS specific support (must be 'true' or 'false').
++darwin=false
++case "`uname`" in
++  Darwin* )
++    darwin=true
++    ;;
++esac
++
++# Attempt to set APP_HOME
++# Resolve links: $0 may be a link
++PRG="$0"
++# Need this for relative symlinks.
++while [ -h "$PRG" ] ; do
++    ls=`ls -ld "$PRG"`
++    link=`expr "$ls" : '.*-> \(.*\)$'`
++    if expr "$link" : '/.*' > /dev/null; then
++        PRG="$link"
++    else
++        PRG=`dirname "$PRG"`"/$link"
++    fi
++done
++SAVED="`pwd`"
++cd "`dirname \"$PRG\"`/" >&-
++APP_HOME="`pwd -P`"
++cd "$SAVED" >&-
++
++CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
++
++# Determine the Java command to use to start the JVM.
++if [ -n "$JAVA_HOME" ] ; then
++    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
++        # IBM's JDK on AIX uses strange locations for the executables
++        JAVACMD="$JAVA_HOME/jre/sh/java"
++    else
++        JAVACMD="$JAVA_HOME/bin/java"
++    fi
++    if [ ! -x "$JAVACMD" ] ; then
++        die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
++
++Please set the JAVA_HOME variable in your environment to match the
++location of your Java installation."
++    fi
++else
++    JAVACMD="java"
++    which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
++
++Please set the JAVA_HOME variable in your environment to match the
++location of your Java installation."
++fi
++
++# Increase the maximum file descriptors if we can.
++if [ "$darwin" = "false" ] ; then
++    MAX_FD_LIMIT=`ulimit -H -n`
++    if [ $? -eq 0 ] ; then
++        if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
++            MAX_FD="$MAX_FD_LIMIT"
++        fi
++        ulimit -n $MAX_FD
++        if [ $? -ne 0 ] ; then
++            warn "Could not set maximum file descriptor limit: $MAX_FD"
++        fi
++    else
++        warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
++    fi
++fi
++
++# For Darwin, add options to specify how the application appears in the dock
++if $darwin; then
++    GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
++fi
++
++# does not match gradle's hash
++# waiting for http://stackoverflow.com/questions/26642077/java-biginteger-in-bash-rewrite-gradlew
++hash() {
++  local input="$1"
++  if $darwin; then
++    md5 -q -s "$1"
++  else
++    echo -n "$1" | md5sum  | cut -d" " -f1
++  fi
++}
++
++dist_path() {
++  local dir=$(basename $distributionUrl | sed 's;.zip;;g')
++  local id=$(hash "$distributionUrl")
++
++  echo "$HOME/.gradle/${distributionPath:-wrapper/dists}/$dir/$id"
++}
++
++zip_path() {
++  local dir=$(basename $distributionUrl | sed 's;.zip;;g')
++  local id=$(hash "$distributionUrl")
++
++  echo "$HOME/.gradle/${zipStorePath:-wrapper/dists}/$dir/$id"
++}
++
++download() {
++  local base_path=$(dist_path)
++  local file_name=$(basename $distributionUrl)
++  local dir_name=$(echo "$file_name" | sed 's;-bin.zip;;g' | sed 's;-src.zip;;g' |sed 's;-all.zip;;g')
++
++  if [ ! -d "$base_path" ]; then
++    mkdir -p "$base_path"
++  else
++    # if data already exists, it means we failed to do this before
++    # so cleanup last run and try again
++    rm -rf $base_path/*
++  fi
++
++  # download dist. curl on mac doesn't like the cert provided...
++  local zip_path=$(zip_path)
++  curl --insecure -L -o "$zip_path/$file_name" "$distributionUrl"
++
++  pushd "$base_path"
++    touch "$file_name.lck"
++    unzip "$zip_path/$file_name" 1> /dev/null
++    touch "$file_name.ok"
++  popd
++}
++
++is_cached() {
++  local file_name=$(basename $distributionUrl)
++
++  [ -e "$(dist_path)/$file_name.ok" ]
++}
++
++lib_path() {
++  local base_path=$(dist_path)
++  local file_name=$(basename $distributionUrl | sed 's;-bin.zip;;g' | sed 's;-src.zip;;g' |sed 's;-all.zip;;g')
++
++  echo "$base_path/$file_name/lib"
++}
++
++classpath() {
++  local dir=$(lib_path)
++  local cp=$(ls -1 $dir/*.jar | tr '\n' ':')
++  echo "$dir:$cp"
++}
++
++# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
++function splitJvmOpts() {
++    JVM_OPTS=("$@")
++}
++
++main() {
++  if ! is_cached; then
++    download
++  fi
++
++  eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
++  JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
++
++  $JAVACMD "${JVM_OPTS[@]}" -cp $(classpath) org.gradle.launcher.GradleMain "$@"
++}
++
++main "$@"
++

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-assembly/src/test/java/com/gemstone/gemfire/management/internal/AgentUtilJUnitTest.java
----------------------------------------------------------------------
diff --cc geode-assembly/src/test/java/com/gemstone/gemfire/management/internal/AgentUtilJUnitTest.java
index 9a446cc,0000000..59da3cf
mode 100644,000000..100644
--- a/geode-assembly/src/test/java/com/gemstone/gemfire/management/internal/AgentUtilJUnitTest.java
+++ b/geode-assembly/src/test/java/com/gemstone/gemfire/management/internal/AgentUtilJUnitTest.java
@@@ -1,98 -1,0 +1,50 @@@
 +/*
 + * 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.management.internal;
 +
++import com.gemstone.gemfire.internal.GemFireVersion;
 +import com.gemstone.gemfire.test.junit.categories.IntegrationTest;
 +import org.junit.Before;
 +import org.junit.Test;
 +import org.junit.experimental.categories.Category;
 +
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- 
 +import static org.junit.Assert.assertNotNull;
 +
 +@Category(IntegrationTest.class)
 +public class AgentUtilJUnitTest {
 +
 +  private AgentUtil agentUtil;
 +  private String version;
 +
 +  @Before
 +  public void setUp() {
-     version = getGemfireVersion();
++    version = GemFireVersion.getGemFireVersion();
 +    agentUtil = new AgentUtil(version);
 +  }
 +
 +  @Test
 +  public void testRESTApiExists() {
 +    String gemFireWarLocation = agentUtil.findWarLocation("geode-web-api");
 +    assertNotNull("GemFire REST API WAR File was not found", gemFireWarLocation);
 +  }
 +
 +  @Test
 +  public void testPulseWarExists() {
 +    String gemFireWarLocation = agentUtil.findWarLocation("geode-pulse");
 +    assertNotNull("Pulse WAR File was not found", gemFireWarLocation);
 +  }
- 
-   private String getGemfireVersion() {
-     String version = null;
- 
-     Properties prop = new Properties();
-     InputStream inputStream = null;
-     String pathPrefix = null;
-     try {
-       pathPrefix = calculatePathPrefixToProjectRoot("geode-assembly/");
-       inputStream = new FileInputStream(pathPrefix + "gradle.properties");
-     } catch (FileNotFoundException e1) {
-       try {
-         pathPrefix = calculatePathPrefixToProjectRoot("geode-core/");
-         inputStream = new FileInputStream(pathPrefix + "gradle.properties");
-       } catch (FileNotFoundException e) {
-       }
-     }
- 
-     if (inputStream != null) {
-       try {
-         prop.load(inputStream);
-         version = prop.getProperty("versionNumber") + prop.getProperty("releaseType");
-       } catch (FileNotFoundException e) {
-       } catch (IOException e) {
-       }
-     }
-     return version;
-   }
- 
-   private String calculatePathPrefixToProjectRoot(String subDirectory) {
-     String pathPrefix = "";
- 
-     String currentDirectoryPath = new File(".").getAbsolutePath();
-     int gemfireCoreLocationIx = currentDirectoryPath.indexOf(subDirectory);
-     if (gemfireCoreLocationIx < 0) {
-       return pathPrefix;
-     }
- 
-     return currentDirectoryPath.substring(0, gemfireCoreLocationIx);
-   }
- 
- 
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/FailedSynchronizationException.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/FailedSynchronizationException.java
index d7fd2fc,0000000..4842324
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/FailedSynchronizationException.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/FailedSynchronizationException.java
@@@ -1,55 -1,0 +1,59 @@@
 +/*
 + * 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.cache;
 +
++import javax.transaction.Status;
++import javax.transaction.Transaction;
++import javax.transaction.UserTransaction;
++
 +/** Thrown when a cache transaction fails to register with the
 + * <code>UserTransaction</code> (aka JTA transaction), most likely the
 + * cause of the <code>UserTransaction</code>'s
 + * <code>javax.transaction.Status#STATUS_MARKED_ROLLBACK</code>
 + * status.
 + *
 + * @author Mitch Thomas
 + *
-  * @see javax.transaction.UserTransaction#setRollbackOnly
-  * @see javax.transaction.Transaction#registerSynchronization
-  * @see javax.transaction.Status
++ * @see UserTransaction#setRollbackOnly
++ * @see Transaction#registerSynchronization
++ * @see Status
 + * @since 4.0
 + */
 +public class FailedSynchronizationException extends CacheRuntimeException {
 +private static final long serialVersionUID = -6225053492344591496L;
 +  /**
 +   * Constructs an instance of
 +   * <code>FailedSynchronizationException</code> with the
 +   * specified detail message.
 +   * @param msg the detail message
 +   */
 +  public FailedSynchronizationException(String msg) {
 +    super(msg);
 +  }
 +  
 +  /**
 +   * Constructs an instance of
 +   * <code>FailedSynchronizationException</code> with the
 +   * specified detail message and cause.
 +   * @param msg the detail message
 +   * @param cause the causal Throwable
 +   */
 +  public FailedSynchronizationException(String msg, Throwable cause) {
 +    super(msg, cause);
 +  }
 +}


[09/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/ClientMembershipDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/ClientMembershipDUnitTest.java
index dbbaaa8,0000000..23386a7
mode 100755,000000..100755
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/ClientMembershipDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/ClientMembershipDUnitTest.java
@@@ -1,1669 -1,0 +1,1659 @@@
 +/*
 + * 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.cache30;
 +
 +import java.io.IOException;
 +import java.net.InetAddress;
 +import java.net.InetSocketAddress;
 +import java.net.Socket;
 +import java.util.Arrays;
 +import java.util.Collection;
 +import java.util.Collections;
 +import java.util.HashSet;
 +import java.util.Iterator;
 +import java.util.List;
 +import java.util.Map;
 +import java.util.Properties;
 +import java.util.Set;
 +
 +import com.gemstone.gemfire.InternalGemFireException;
 +import com.gemstone.gemfire.LogWriter;
 +import com.gemstone.gemfire.Statistics;
 +import com.gemstone.gemfire.StatisticsType;
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.Scope;
 +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.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.DurableClientAttributes;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.internal.SocketCreator;
 +import com.gemstone.gemfire.internal.cache.tier.InternalClientMembership;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.AcceptorImpl;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.ServerConnection;
 +import com.gemstone.gemfire.internal.logging.LocalLogWriter;
 +import com.gemstone.gemfire.internal.logging.InternalLogWriter;
 +import com.gemstone.gemfire.management.membership.ClientMembership;
 +import com.gemstone.gemfire.management.membership.ClientMembershipEvent;
 +import com.gemstone.gemfire.management.membership.ClientMembershipListener;
 +import com.gemstone.gemfire.test.dunit.Assert;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.IgnoredException;
 +import com.gemstone.gemfire.test.dunit.NetworkUtils;
 +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;
 +
 +/**
 + * Tests the ClientMembership API including ClientMembershipListener.
 + *
 + * @author Kirk Lund
 + * @since 4.2.1
 + */
 +public class ClientMembershipDUnitTest extends ClientServerTestCase {
 +
 +  protected static final boolean CLIENT = true;
 +  protected static final boolean SERVER = false;
 +  
 +  protected static final int JOINED = 0;
 +  protected static final int LEFT = 1;
 +  protected static final int CRASHED = 2;
 +    
 +  public ClientMembershipDUnitTest(String name) {
 +    super(name);
 +  }
 +
 +  public void setUp() throws Exception {
 +    super.setUp();
 +  }
 +  
 +  @Override
 +  protected final void postTearDownCacheTestCase() throws Exception {
 +    InternalClientMembership.unregisterAllListeners();
 +  }
 +
 +  private void waitForAcceptsInProgressToBe(final int target)
 +    throws Exception {
 +    WaitCriterion ev = new WaitCriterion() {
 +      String excuse;
 +      public boolean done() {
 +        int actual = getAcceptsInProgress();
 +        if (actual == getAcceptsInProgress()) {
 +          return true;
 +        }
 +        excuse = "accepts in progress (" + actual + ") never became " + target;
 +        return false;
 +      }
 +      public String description() {
 +        return excuse;
 +      }
 +    };
 +    Wait.waitForCriterion(ev, 60 * 1000, 200, true);
 +  }
 +  
 +  protected int getAcceptsInProgress() {
 +    StatisticsType st = InternalDistributedSystem.getAnyInstance().findType("CacheServerStats");
 +    Statistics[] s = InternalDistributedSystem.getAnyInstance().findStatisticsByType(st);
 +    return s[0].getInt("acceptsInProgress");
 +  }
 +
 +  protected static Socket meanSocket;
 +
 +  /** test that a server times out waiting for a handshake that
 +      never arrives. 
 +   */
 +  public void testConnectionTimeout() throws Exception {
 +    IgnoredException.addIgnoredException("failed accepting client connection");
 +    final Host host = Host.getHost(0);
 +    final String hostName = NetworkUtils.getServerHostName(host);
 +    final VM vm0 = host.getVM(0);
 +    System.setProperty(AcceptorImpl.ACCEPT_TIMEOUT_PROPERTY_NAME, "1000");
 +    try {
 +    final int port = startBridgeServer(0);
 +//    AsyncInvocation ai = null;
 +    try {
 +      assertTrue(port != 0);
 +      SerializableRunnable createMeanSocket = new CacheSerializableRunnable("Connect to server with socket") {
 +        public void run2() throws CacheException {
 +          getCache(); // create a cache so we have stats
 +          com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("connecting to cache server with socket");
 +          try {
 +            InetAddress addr = InetAddress.getByName(hostName);
 +            meanSocket = new Socket(addr, port);
 +          }
 +          catch (Exception e) {
 +            throw new RuntimeException("Test failed to connect or was interrupted", e);
 +          }
 +        }
 +      };
 +      SerializableRunnable closeMeanSocket = new CacheSerializableRunnable("close mean socket") {
 +        public void run2() throws CacheException {
 +          com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("closing mean socket");
 +          try {
 +            meanSocket.close();
 +          }
 +          catch (IOException ignore) {
 +          }
 +        }
 +      };
 +
 +      assertEquals(0, getAcceptsInProgress());
 +      
 +      com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("creating mean socket");
 +      vm0.invoke(createMeanSocket);
 +      try {
 +        com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("waiting to see it connect on server");
 +        waitForAcceptsInProgressToBe(1);
 +      } finally {
 +        com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("closing mean socket");
 +        vm0.invoke(closeMeanSocket);
 +      }
 +      com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("waiting to see accept to go away on server");
 +      waitForAcceptsInProgressToBe(0);
 +
 +      // now try it without a close. Server should timeout the mean connect
 +      com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("creating mean socket 2");
 +      vm0.invoke(createMeanSocket);
 +      try {
 +        com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("waiting to see it connect on server 2");
 +        waitForAcceptsInProgressToBe(1);
 +        com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("waiting to see accept to go away on server without us closing");
 +        waitForAcceptsInProgressToBe(0);
 +      } finally {
 +        com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("closing mean socket 2");
 +        vm0.invoke(closeMeanSocket);
 +      }
 +
 +//       SerializableRunnable denialOfService = new CacheSerializableRunnable("Do lots of connects") {
 +//         public void run2() throws CacheException {
 +//           int connectionCount = 0;
 +//           ArrayList al = new ArrayList(60000);
 +//           try {
 +//             InetAddress addr = InetAddress.getLocalHost();
 +//             for (;;) {
 +//               Socket s = new Socket(addr, port);
 +//               al.add(s);
 +//               connectionCount++;
 +//               getLogWriter().info("connected # " + connectionCount + " s=" + s);
 +// //               try {
 +// //                 s.close();
 +// //               } catch (IOException ignore) {}
 +//             }
 +//           }
 +//           catch (Exception e) {
 +//             getLogWriter().info("connected # " + connectionCount
 +//                                 + " stopped because of exception " + e);
 +//             Iterator it = al.iterator();
 +//             while (it.hasNext()) {
 +//               Socket s = (Socket)it.next();
 +//               try {
 +//                 s.close();
 +//               } catch (IOException ignore) {}
 +//             }
 +//           }
 +//         }
 +//       };
 +//       // now pretend to do a denial of service attack by doing a bunch of connects
 +//       // really fast and see what that does to the server's fds.
 +//       getLogWriter().info("doing denial of service attach");
 +//       vm0.invoke(denialOfService);
 +//       // @todo darrel: check fd limit?
 +    }
 +    finally {
 +      stopBridgeServers(getCache());
 +    }
 +    }
 +    finally {
 +      System.getProperties().remove(AcceptorImpl.ACCEPT_TIMEOUT_PROPERTY_NAME);
 +    }
 +  }
 +
 +  public void testSynchronousEvents() throws Exception {
 +    getSystem();
 +    InternalClientMembership.setForceSynchronous(true);
 +    try {
 +      doTestBasicEvents();
 +    }
 +    finally {
 +      InternalClientMembership.setForceSynchronous(false);
 +    }
 +  }
 +  
 +  /**
 +   * Tests event notification methods on ClientMembership.
 +   */
 +  public void testBasicEvents() throws Exception {
 +    getSystem();
 +    doTestBasicEvents();
 +  }
 +  
 +  public void doTestBasicEvents() throws Exception {
 +    final boolean[] fired = new boolean[3];
 +    final DistributedMember[] member = new DistributedMember[3];
 +    final String[] memberId = new String[3];
 +    final boolean[] isClient = new boolean[3];
 +    
 +    ClientMembershipListener listener = new ClientMembershipListener() {
 +      public synchronized void memberJoined(ClientMembershipEvent event) {
 +        fired[JOINED] = true;
 +        member[JOINED] = event.getMember();
 +        memberId[JOINED] = event.getMemberId();
 +        isClient[JOINED] = event.isClient();
 +        notify();
 +      }
 +      public synchronized void memberLeft(ClientMembershipEvent event) {
 +        fired[LEFT] = true;
 +        member[LEFT] = event.getMember();
 +        memberId[LEFT] = event.getMemberId();
 +        isClient[LEFT] = event.isClient();
 +        notify();
 +      }
 +      public synchronized void memberCrashed(ClientMembershipEvent event) {
 +        fired[CRASHED] = true;
 +        member[CRASHED] = event.getMember();
 +        memberId[CRASHED] = event.getMemberId();
 +        isClient[CRASHED] = event.isClient();
 +        notify();
 +      }
 +    };
 +    ClientMembership.registerClientMembershipListener(listener);
 +    
 +    // test JOIN for server
 +    DistributedMember serverJoined = new TestDistributedMember("serverJoined");
 +    InternalClientMembership.notifyJoined(serverJoined, SERVER);
 +    synchronized(listener) {
 +      if (!fired[JOINED]) {
 +        listener.wait(2000);
 +      }
 +    }
 +    assertTrue(fired[JOINED]);
 +    assertEquals(serverJoined, member[JOINED]);
 +    assertEquals(serverJoined.getId(), memberId[JOINED]);
 +    assertFalse(isClient[JOINED]);
 +    assertFalse(fired[LEFT]);
 +    assertNull(memberId[LEFT]);
 +    assertFalse(isClient[LEFT]);
 +    assertFalse(fired[CRASHED]);
 +    assertNull(memberId[CRASHED]);
 +    assertFalse(isClient[CRASHED]);
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +
 +    // test JOIN for client
 +    DistributedMember clientJoined = new TestDistributedMember("clientJoined");
 +    InternalClientMembership.notifyJoined(clientJoined, CLIENT);
 +    synchronized(listener) {
 +      if (!fired[JOINED]) {
 +        listener.wait(2000);
 +      }
 +    }
 +    assertTrue(fired[JOINED]);
 +    assertEquals(clientJoined, member[JOINED]);
 +    assertEquals(clientJoined.getId(), memberId[JOINED]);
 +    assertTrue(isClient[JOINED]);
 +    assertFalse(fired[LEFT]);
 +    assertNull(memberId[LEFT]);
 +    assertFalse(isClient[LEFT]);
 +    assertFalse(fired[CRASHED]);
 +    assertNull(memberId[CRASHED]);
 +    assertFalse(isClient[CRASHED]);
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +
 +    // test LEFT for server
 +    DistributedMember serverLeft = new TestDistributedMember("serverLeft");
 +    InternalClientMembership.notifyLeft(serverLeft, SERVER);
 +    synchronized(listener) {
 +      if (!fired[LEFT]) {
 +        listener.wait(2000);
 +      }
 +    }
 +    assertFalse(fired[JOINED]);
 +    assertNull(memberId[JOINED]);
 +    assertFalse(isClient[JOINED]);
 +    assertTrue(fired[LEFT]);
 +    assertEquals(serverLeft, member[LEFT]);
 +    assertEquals(serverLeft.getId(), memberId[LEFT]);
 +    assertFalse(isClient[LEFT]);
 +    assertFalse(fired[CRASHED]);
 +    assertNull(memberId[CRASHED]);
 +    assertFalse(isClient[CRASHED]);
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +
 +    // test LEFT for client
 +    DistributedMember clientLeft = new TestDistributedMember("clientLeft");
 +    InternalClientMembership.notifyLeft(clientLeft, CLIENT);
 +    synchronized(listener) {
 +      if (!fired[LEFT]) {
 +        listener.wait(2000);
 +      }
 +    }
 +    assertFalse(fired[JOINED]);
 +    assertNull(memberId[JOINED]);
 +    assertFalse(isClient[JOINED]);
 +    assertTrue(fired[LEFT]);
 +    assertEquals(clientLeft, member[LEFT]);
 +    assertEquals(clientLeft.getId(), memberId[LEFT]);
 +    assertTrue(isClient[LEFT]);
 +    assertFalse(fired[CRASHED]);
 +    assertNull(memberId[CRASHED]);
 +    assertFalse(isClient[CRASHED]);
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +
 +    // test CRASHED for server
 +    DistributedMember serverCrashed = new TestDistributedMember("serverCrashed");
 +    InternalClientMembership.notifyCrashed(serverCrashed, SERVER);
 +    synchronized(listener) {
 +      if (!fired[CRASHED]) {
 +        listener.wait(2000);
 +      }
 +    }
 +    assertFalse(fired[JOINED]);
 +    assertNull(memberId[JOINED]);
 +    assertFalse(isClient[JOINED]);
 +    assertFalse(fired[LEFT]);
 +    assertNull(memberId[LEFT]);
 +    assertFalse(isClient[LEFT]);
 +    assertTrue(fired[CRASHED]);
 +    assertEquals(serverCrashed, member[CRASHED]);
 +    assertEquals(serverCrashed.getId(), memberId[CRASHED]);
 +    assertFalse(isClient[CRASHED]);
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +
 +    // test CRASHED for client
 +    DistributedMember clientCrashed = new TestDistributedMember("clientCrashed");
 +    InternalClientMembership.notifyCrashed(clientCrashed, CLIENT);
 +    synchronized(listener) {
 +      if (!fired[CRASHED]) {
 +        listener.wait(2000);
 +      }
 +    }
 +    assertFalse(fired[JOINED]);
 +    assertNull(memberId[JOINED]);
 +    assertFalse(isClient[JOINED]);
 +    assertFalse(fired[LEFT]);
 +    assertNull(memberId[LEFT]);
 +    assertFalse(isClient[LEFT]);
 +    assertTrue(fired[CRASHED]);
 +    assertEquals(clientCrashed, member[CRASHED]);
 +    assertEquals(clientCrashed.getId(), memberId[CRASHED]);
 +    assertTrue(isClient[CRASHED]);
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +  }
 +  
 +  /**
 +   * Resets all elements of arrays used for listener testing. Boolean values
 +   * are reset to false. String values are reset to null.
 +   */
 +  private void resetArraysForTesting(boolean[] fired, 
 +                                     DistributedMember[] member,
 +                                     String[] memberId, 
 +                                     boolean[] isClient) {
 +    for (int i = 0; i < fired.length; i++) {
 +      fired[i] = false;
 +      member[i] = null;
 +      memberId[i] = null;
 +      isClient[i] = false;
 +    }
 +  }
 +  
 +  /**
 +   * Tests unregisterClientMembershipListener to ensure that no further events
 +   * are delivered to unregistered listeners.
 +   */
 +  public void testUnregisterClientMembershipListener() throws Exception {
 +    final boolean[] fired = new boolean[1];
 +    final DistributedMember[] member = new DistributedMember[1];
 +    final String[] memberId = new String[1];
 +    final boolean[] isClient = new boolean[1];
 +    
 +    getSystem();
 +
 +    ClientMembershipListener listener = new ClientMembershipListener() {
 +      public synchronized void memberJoined(ClientMembershipEvent event) {
 +        fired[0] = true;
 +        member[0] = event.getMember();
 +        memberId[0] = event.getMemberId();
 +        isClient[0] = event.isClient();
 +        notify();
 +      }
 +      public void memberLeft(ClientMembershipEvent event) {
 +      }
 +      public void memberCrashed(ClientMembershipEvent event) {
 +      }
 +    };
 +    ClientMembership.registerClientMembershipListener(listener);
 +    
 +    // fire event to make sure listener is registered
 +    DistributedMember clientJoined = new TestDistributedMember("clientJoined");
 +    InternalClientMembership.notifyJoined(clientJoined, true);
 +    synchronized(listener) {
 +      if (!fired[0]) {
 +        listener.wait(2000);
 +      }
 +    }
 +    assertTrue(fired[0]);
 +    assertEquals(clientJoined, member[0]);
 +    assertEquals(clientJoined.getId(), memberId[0]);
 +    assertTrue(isClient[0]);
 +
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +    assertFalse(fired[0]);
 +    assertNull(memberId[0]);
 +    assertFalse(isClient[0]);
 +
 +    // unregister and verify listener is not notified
 +    ClientMembership.unregisterClientMembershipListener(listener);
 +    InternalClientMembership.notifyJoined(clientJoined, true);
 +    synchronized(listener) {
 +      listener.wait(20);
 +    }
 +    assertFalse(fired[0]);
 +    assertNull(member[0]);
 +    assertNull(memberId[0]);
 +    assertFalse(isClient[0]);
 +  }
 +  
 +  public void testMultipleListeners() throws Exception {
 +    final int NUM_LISTENERS = 4;
 +    final boolean[] fired = new boolean[NUM_LISTENERS];
 +    final DistributedMember[] member = new DistributedMember[NUM_LISTENERS];
 +    final String[] memberId = new String[NUM_LISTENERS];
 +    final boolean[] isClient = new boolean[NUM_LISTENERS];
 +    
 +    getSystem();
 +
 +    final ClientMembershipListener[] listeners = new ClientMembershipListener[NUM_LISTENERS];
 +    for (int i = 0; i < NUM_LISTENERS; i++) {
 +      final int whichListener = i;
 +      listeners[i] = new ClientMembershipListener() {
 +        public synchronized void memberJoined(ClientMembershipEvent event) {
 +          assertFalse(fired[whichListener]);
 +          assertNull(member[whichListener]);
 +          assertNull(memberId[whichListener]);
 +          assertFalse(isClient[whichListener]);
 +          fired[whichListener] = true;
 +          member[whichListener] = event.getMember();
 +          memberId[whichListener] = event.getMemberId();
 +          isClient[whichListener] = event.isClient();
 +          notify();
 +        }
 +        public void memberLeft(ClientMembershipEvent event) {
 +        }
 +        public void memberCrashed(ClientMembershipEvent event) {
 +        }
 +      };
 +    }
 +    
 +    final DistributedMember clientJoined = new TestDistributedMember("clientJoined");
 +    InternalClientMembership.notifyJoined(clientJoined, true);
 +    for (int i = 0; i < NUM_LISTENERS; i++) {
 +      synchronized(listeners[i]) {
 +        listeners[i].wait(20);
 +      }
 +      assertFalse(fired[i]);
 +      assertNull(member[i]);
 +      assertNull(memberId[i]);
 +      assertFalse(isClient[i]);
 +    }
 +    
 +    // attempt to register same listener twice... 2nd reg should be ignored
 +    // failure would cause an assertion failure in memberJoined impl
 +    ClientMembership.registerClientMembershipListener(listeners[0]);
 +    ClientMembership.registerClientMembershipListener(listeners[0]);
 +    
 +    ClientMembershipListener[] registeredListeners = 
 +      ClientMembership.getClientMembershipListeners();
 +    assertEquals(1, registeredListeners.length);
 +    assertEquals(listeners[0], registeredListeners[0]);
 +    
 +    ClientMembership.registerClientMembershipListener(listeners[1]);
 +    registeredListeners = ClientMembership.getClientMembershipListeners();
 +    assertEquals(2, registeredListeners.length);
 +    assertEquals(listeners[0], registeredListeners[0]);
 +    assertEquals(listeners[1], registeredListeners[1]);
 +
 +    InternalClientMembership.notifyJoined(clientJoined, true);
 +    synchronized(listeners[1]) {
 +      if (!fired[1]) {
 +        listeners[1].wait(2000);
 +      }
 +    }
 +    for (int i = 0; i < NUM_LISTENERS; i++) {
 +      if (i < 2) {
 +        assertTrue(fired[i]);
 +        assertEquals(clientJoined, member[i]);
 +        assertEquals(clientJoined.getId(), memberId[i]);
 +        assertTrue(isClient[i]);
 +      } else {
 +        assertFalse(fired[i]);
 +        assertNull(member[i]);
 +        assertNull(memberId[i]);
 +        assertFalse(isClient[i]);
 +      }
 +    }
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +        
 +    ClientMembership.unregisterClientMembershipListener(listeners[0]);
 +    registeredListeners = ClientMembership.getClientMembershipListeners();
 +    assertEquals(1, registeredListeners.length);
 +    assertEquals(listeners[1], registeredListeners[0]);
 +    
 +    InternalClientMembership.notifyJoined(clientJoined, true);
 +    synchronized(listeners[1]) {
 +      if (!fired[1]) {
 +        listeners[1].wait(2000);
 +      }
 +    }
 +    for (int i = 0; i < NUM_LISTENERS; i++) {
 +      if (i == 1) {
 +        assertTrue(fired[i]);
 +        assertEquals(clientJoined, member[i]);
 +        assertEquals(clientJoined.getId(), memberId[i]);
 +        assertTrue(isClient[i]);
 +      } else {
 +        assertFalse(fired[i]);
 +        assertNull(member[i]);
 +        assertNull(memberId[i]);
 +        assertFalse(isClient[i]);
 +      }
 +    }
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +
 +    ClientMembership.registerClientMembershipListener(listeners[2]);
 +    ClientMembership.registerClientMembershipListener(listeners[3]);
 +    registeredListeners = ClientMembership.getClientMembershipListeners();
 +    assertEquals(3, registeredListeners.length);
 +    assertEquals(listeners[1], registeredListeners[0]);
 +    assertEquals(listeners[2], registeredListeners[1]);
 +    assertEquals(listeners[3], registeredListeners[2]);
 +
 +    InternalClientMembership.notifyJoined(clientJoined, true);
 +    synchronized(listeners[3]) {
 +      if (!fired[3]) {
 +        listeners[3].wait(2000);
 +      }
 +    }
 +    for (int i = 0; i < NUM_LISTENERS; i++) {
 +      if (i != 0) {
 +        assertTrue(fired[i]);
 +        assertEquals(clientJoined, member[i]);
 +        assertEquals(clientJoined.getId(), memberId[i]);
 +        assertTrue(isClient[i]);
 +      } else {
 +        assertFalse(fired[i]);
 +        assertNull(member[i]);
 +        assertNull(memberId[i]);
 +        assertFalse(isClient[i]);
 +      }
 +    }
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +    
 +    ClientMembership.registerClientMembershipListener(listeners[0]);
 +    registeredListeners = ClientMembership.getClientMembershipListeners();
 +    assertEquals(4, registeredListeners.length);
 +    assertEquals(listeners[1], registeredListeners[0]);
 +    assertEquals(listeners[2], registeredListeners[1]);
 +    assertEquals(listeners[3], registeredListeners[2]);
 +    assertEquals(listeners[0], registeredListeners[3]);
 +
 +    InternalClientMembership.notifyJoined(clientJoined, true);
 +    synchronized(listeners[0]) {
 +      if (!fired[0]) {
 +        listeners[0].wait(2000);
 +      }
 +    }
 +    for (int i = 0; i < NUM_LISTENERS; i++) {
 +      assertTrue(fired[i]);
 +      assertEquals(clientJoined, member[i]);
 +      assertEquals(clientJoined.getId(), memberId[i]);
 +      assertTrue(isClient[i]);
 +    }
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +    
 +    ClientMembership.unregisterClientMembershipListener(listeners[3]);
 +    registeredListeners = ClientMembership.getClientMembershipListeners();
 +    assertEquals(3, registeredListeners.length);
 +    assertEquals(listeners[1], registeredListeners[0]);
 +    assertEquals(listeners[2], registeredListeners[1]);
 +    assertEquals(listeners[0], registeredListeners[2]);
 +    
 +    InternalClientMembership.notifyJoined(clientJoined, true);
 +    synchronized(listeners[0]) {
 +      if (!fired[0]) {
 +        listeners[0].wait(2000);
 +      }
 +    }
 +    for (int i = 0; i < NUM_LISTENERS; i++) {
 +      if (i < 3) {
 +        assertTrue(fired[i]);
 +        assertEquals(clientJoined, member[i]);
 +        assertEquals(clientJoined.getId(), memberId[i]);
 +        assertTrue(isClient[i]);
 +      } else {
 +        assertFalse(fired[i]);
 +        assertNull(member[i]);
 +        assertNull(memberId[i]);
 +        assertFalse(isClient[i]);
 +      }
 +    }
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +
 +    ClientMembership.unregisterClientMembershipListener(listeners[2]);
 +    registeredListeners = ClientMembership.getClientMembershipListeners();
 +    assertEquals(2, registeredListeners.length);
 +    assertEquals(listeners[1], registeredListeners[0]);
 +    assertEquals(listeners[0], registeredListeners[1]);
 +    
 +    InternalClientMembership.notifyJoined(clientJoined, true);
 +    synchronized(listeners[0]) {
 +      if (!fired[0]) {
 +        listeners[0].wait(2000);
 +      }
 +    }
 +    for (int i = 0; i < NUM_LISTENERS; i++) {
 +      if (i < 2) {
 +        assertTrue(fired[i]);
 +        assertEquals(clientJoined, member[i]);
 +        assertEquals(clientJoined.getId(), memberId[i]);
 +        assertTrue(isClient[i]);
 +      } else {
 +        assertFalse(fired[i]);
 +        assertNull(member[i]);
 +        assertNull(memberId[i]);
 +        assertFalse(isClient[i]);
 +      }
 +    }
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +
 +    ClientMembership.unregisterClientMembershipListener(listeners[1]);
 +    ClientMembership.unregisterClientMembershipListener(listeners[0]);
 +    registeredListeners = ClientMembership.getClientMembershipListeners();
 +    assertEquals(0, registeredListeners.length);
 +    
 +    InternalClientMembership.notifyJoined(clientJoined, true);
 +    for (int i = 0; i < NUM_LISTENERS; i++) {
 +      synchronized(listeners[i]) {
 +        listeners[i].wait(20);
 +      }
 +      assertFalse(fired[i]);
 +      assertNull(member[i]);
 +      assertNull(memberId[i]);
 +      assertFalse(isClient[i]);
 +    }
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +    
 +    ClientMembership.registerClientMembershipListener(listeners[1]);
 +    registeredListeners = ClientMembership.getClientMembershipListeners();
 +    assertEquals(1, registeredListeners.length);
 +    assertEquals(listeners[1], registeredListeners[0]);
 +    
 +    InternalClientMembership.notifyJoined(clientJoined, true);
 +    synchronized(listeners[1]) {
 +      if (!fired[1]) {
 +        listeners[1].wait(2000);
 +      }
 +    }
 +    for (int i = 0; i < NUM_LISTENERS; i++) {
 +      if (i == 1) {
 +        assertTrue(fired[i]);
 +        assertEquals(clientJoined, member[i]);
 +        assertEquals(clientJoined.getId(), memberId[i]);
 +        assertTrue(isClient[i]);
 +      } else {
 +        assertFalse(fired[i]);
 +        assertNull(member[i]);
 +        assertNull(memberId[i]);
 +        assertFalse(isClient[i]);
 +      }
 +    }
 +  }
 + 
 +  protected static int testClientMembershipEventsInClient_port;
 +  private static int getTestClientMembershipEventsInClient_port() {
 +    return testClientMembershipEventsInClient_port;
 +  }
 +  /**
 +   * Tests notification of events in client process. Bridge clients detect
 +   * server joins when the client connects to the server. If the server
 +   * crashes or departs gracefully, the client will detect this as a crash.
 +   */
 +  public void testClientMembershipEventsInClient() throws Exception {
 +    getSystem();
 +    IgnoredException.addIgnoredException("IOException");
 +    final boolean[] fired = new boolean[3];
 +    final DistributedMember[] member = new DistributedMember[3];
 +    final String[] memberId = new String[3];
 +    final boolean[] isClient = new boolean[3];
 +    
 +    // create and register ClientMembershipListener in controller vm...
 +    ClientMembershipListener listener = new ClientMembershipListener() {
 +      public synchronized void memberJoined(ClientMembershipEvent event) {
 +        com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInClient] memberJoined: " + event);
 +        fired[JOINED] = true;
 +        member[JOINED] = event.getMember();
 +        memberId[JOINED] = event.getMemberId();
 +        isClient[JOINED] = event.isClient();
 +        notifyAll();
 +      }
 +      public synchronized void memberLeft(ClientMembershipEvent event) {
 +        com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInClient] memberLeft: " + event);
 +//        fail("Please update testClientMembershipEventsInClient to handle memberLeft for BridgeServer.");
 +      }
 +      public synchronized void memberCrashed(ClientMembershipEvent event) {
 +        com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInClient] memberCrashed: " + event);
 +        fired[CRASHED] = true;
 +        member[CRASHED] = event.getMember();
 +        memberId[CRASHED] = event.getMemberId();
 +        isClient[CRASHED] = event.isClient();
 +        notifyAll();
 +      }
 +    };
 +    ClientMembership.registerClientMembershipListener(listener);
 +
 +    final VM vm0 = Host.getHost(0).getVM(0);
 +    final String name = this.getUniqueName();
 +    final int[] ports = new int[1];
 +
 +    // create BridgeServer in vm0...
 +    vm0.invoke(new CacheSerializableRunnable("Create BridgeServer") {
 +      public void run2() throws CacheException {
 +        try {
 +          com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInClient] Create BridgeServer");
 +          getSystem();
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.LOCAL);
 +          Region region = createRegion(name, factory.create());
 +          assertNotNull(region);
 +          assertNotNull(getRootRegion().getSubregion(name));
 +          testClientMembershipEventsInClient_port = startBridgeServer(0);
 +        }
 +        catch(IOException e) {
 +          getSystem().getLogWriter().fine(new Exception(e));
 +          fail("Failed to start CacheServer on VM1: " + e.getMessage());
 +        }
 +      }
 +    });
 +    
 +    // gather details for later creation of ConnectionPool...
-     ports[0] = vm0.invokeInt(ClientMembershipDUnitTest.class, 
-                              "getTestClientMembershipEventsInClient_port");
++    ports[0] = vm0.invoke(() -> ClientMembershipDUnitTest.getTestClientMembershipEventsInClient_port());
 +    assertTrue(ports[0] != 0);
 +
-     DistributedMember serverMember = (DistributedMember) vm0.invoke(ClientMembershipDUnitTest.class,
-     "getDistributedMember");
++    DistributedMember serverMember = (DistributedMember) vm0.invoke(() -> ClientMembershipDUnitTest.getDistributedMember());
 +
-     String serverMemberId = (String) vm0.invoke(ClientMembershipDUnitTest.class,
-                                                 "getMemberId");
++    String serverMemberId = (String) vm0.invoke(() -> ClientMembershipDUnitTest.getMemberId());
 +
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInClient] ports[0]=" + ports[0]);
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInClient] serverMember=" + serverMember);
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInClient] serverMemberId=" + serverMemberId);
 +
 +    assertFalse(fired[JOINED]);
 +    assertNull(member[JOINED]);
 +    assertNull(memberId[JOINED]);
 +    assertFalse(isClient[JOINED]);
 +    assertFalse(fired[LEFT]);
 +    assertNull(member[LEFT]);
 +    assertNull(memberId[LEFT]);
 +    assertFalse(isClient[LEFT]);
 +    assertFalse(fired[CRASHED]);
 +    assertNull(member[CRASHED]);
 +    assertNull(memberId[CRASHED]);
 +    assertFalse(isClient[CRASHED]);
 +    
 +    // sanity check...
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInClient] sanity check");
 +    DistributedMember test = new TestDistributedMember("test");
 +    InternalClientMembership.notifyJoined(test, SERVER);
 +    synchronized(listener) {
 +      if (!fired[JOINED] && !fired[CRASHED]) {
 +        listener.wait(2000);
 +      }
 +    }
 +    
 +    assertTrue(fired[JOINED]);
 +    assertEquals(test, member[JOINED]);
 +    assertEquals(test.getId(), memberId[JOINED]);
 +    assertFalse(isClient[JOINED]);
 +    assertFalse(fired[LEFT]);
 +    assertNull(member[LEFT]);
 +    assertNull(memberId[LEFT]);
 +    assertFalse(isClient[LEFT]);
 +    assertFalse(fired[CRASHED]);
 +    assertNull(member[CRASHED]);
 +    assertNull(memberId[CRASHED]);
 +    assertFalse(isClient[CRASHED]);
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +    
 +    // create bridge client in controller vm...
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInClient] create bridge client");
 +    Properties config = new Properties();
 +    config.setProperty(DistributionConfig.MCAST_PORT_NAME, "0");
 +    config.setProperty(DistributionConfig.LOCATORS_NAME, "");
 +    getSystem(config);
 +    
 +    try {
 +      getCache();
 +      AttributesFactory factory = new AttributesFactory();
 +      factory.setScope(Scope.LOCAL);
 +      ClientServerTestCase.configureConnectionPool(factory, NetworkUtils.getServerHostName(Host.getHost(0)), ports, true, -1, -1, null);
 +      createRegion(name, factory.create());
 +      assertNotNull(getRootRegion().getSubregion(name));
 +    }
 +    catch (CacheException ex) {
 +      Assert.fail("While creating Region on Edge", ex);
 +    }
 +    synchronized(listener) {
 +      if (!fired[JOINED] && !fired[CRASHED]) {
 +        listener.wait(60 * 1000);
 +      }
 +    }
 +    
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInClient] assert client detected server join");
 +    
 +    // first check the getCurrentServers() result
 +    ClientCache clientCache = (ClientCache)getCache();
 +    Set<InetSocketAddress> servers = clientCache.getCurrentServers();
 +    assertTrue(!servers.isEmpty());
 +    InetSocketAddress serverAddr = servers.iterator().next();
 +    InetSocketAddress expectedAddr = new InetSocketAddress(serverMember.getHost(), ports[0]);
 +    assertEquals(expectedAddr, serverAddr);
 +    
 +    // now check listener results
 +    assertTrue(fired[JOINED]);
 +    assertNotNull(member[JOINED]);
 +    assertNotNull(memberId[JOINED]);
 +    assertEquals(serverMember, member[JOINED]);
 +    assertEquals(serverMemberId, memberId[JOINED]);
 +    assertFalse(isClient[JOINED]);
 +    assertFalse(fired[LEFT]);
 +    assertNull(member[LEFT]);
 +    assertNull(memberId[LEFT]);
 +    assertFalse(isClient[LEFT]);
 +    assertFalse(fired[CRASHED]);
 +    assertNull(member[CRASHED]);
 +    assertNull(memberId[CRASHED]);
 +    assertFalse(isClient[CRASHED]);
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +
 +    vm0.invoke(new SerializableRunnable("Stop BridgeServer") {
 +      public void run() {
 +        com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInClient] Stop BridgeServer");
 +        stopBridgeServers(getCache());
 +      }
 +    });
 +    synchronized(listener) {
 +      if (!fired[JOINED] && !fired[CRASHED]) {
 +        listener.wait(60 * 1000);
 +      }
 +    }
 +    
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInClient] assert client detected server departure");
 +    assertFalse(fired[JOINED]);
 +    assertNull(member[JOINED]);
 +    assertNull(memberId[JOINED]);
 +    assertFalse(isClient[JOINED]);
 +    assertFalse(fired[LEFT]);
 +    assertNull(member[LEFT]);
 +    assertNull(memberId[LEFT]);
 +    assertFalse(isClient[LEFT]);
 +    assertTrue(fired[CRASHED]);
 +    assertNotNull(member[CRASHED]);
 +    assertNotNull(memberId[CRASHED]);
 +    assertEquals(serverMember, member[CRASHED]);
 +    assertEquals(serverMemberId, memberId[CRASHED]);
 +    assertFalse(isClient[CRASHED]);
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +    
 +    //now test that we redisover the bridge server
 +    vm0.invoke(new CacheSerializableRunnable("Recreate BridgeServer") {
 +      public void run2() throws CacheException {
 +        try {
 +          com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInClient] restarting BridgeServer");
 +          startBridgeServer(ports[0]);
 +        }
 +        catch(IOException e) {
 +          getSystem().getLogWriter().fine(new Exception(e));
 +          fail("Failed to start CacheServer on VM1: " + e.getMessage());
 +        }
 +      }
 +    });
 +    synchronized(listener) {
 +      if (!fired[JOINED] && !fired[CRASHED]) {
 +        listener.wait(60 * 1000);
 +      }
 +    }
 +    
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInClient] assert client detected server recovery");
 +    assertTrue(fired[JOINED]);
 +    assertNotNull(member[JOINED]);
 +    assertNotNull(memberId[JOINED]);
 +    assertFalse(isClient[JOINED]);
 +    assertEquals(serverMember, member[JOINED]);
 +    assertEquals(serverMemberId, memberId[JOINED]);
 +    assertFalse(fired[LEFT]);
 +    assertNull(member[LEFT]);
 +    assertNull(memberId[LEFT]);
 +    assertFalse(isClient[LEFT]);
 +    assertFalse(fired[CRASHED]);
 +    assertNull(member[CRASHED]);
 +    assertNull(memberId[CRASHED]);
 +  }
 +  
 +  /**
 +   * Tests notification of events in server process. Bridge servers detect
 +   * client joins when the client connects to the server.
 +   */
 +  public void testClientMembershipEventsInServer() throws Exception {
 +    final boolean[] fired = new boolean[3];
 +    final DistributedMember[] member = new DistributedMember[3];
 +    final String[] memberId = new String[3];
 +    final boolean[] isClient = new boolean[3];
 +    
 +    // create and register ClientMembershipListener in controller vm...
 +    ClientMembershipListener listener = new ClientMembershipListener() {
 +      public synchronized void memberJoined(ClientMembershipEvent event) {
 +        com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInServer] memberJoined: " + event);
 +        fired[JOINED] = true;
 +        member[JOINED] = event.getMember();
 +        memberId[JOINED] = event.getMemberId();
 +        isClient[JOINED] = event.isClient();
 +        notifyAll();
 +        assertFalse(fired[LEFT] || fired[CRASHED]);
 +      }
 +      public synchronized void memberLeft(ClientMembershipEvent event) {
 +        com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInServer] memberLeft: " + event);
 +        fired[LEFT] = true;
 +        member[LEFT] = event.getMember();
 +        memberId[LEFT] = event.getMemberId();
 +        isClient[LEFT] = event.isClient();
 +        notifyAll();
 +        assertFalse(fired[JOINED] || fired[CRASHED]);
 +      }
 +      public synchronized void memberCrashed(ClientMembershipEvent event) {
 +        com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInServer] memberCrashed: " + event);
 +        fired[CRASHED] = true;
 +        member[CRASHED] = event.getMember();
 +        memberId[CRASHED] = event.getMemberId();
 +        isClient[CRASHED] = event.isClient();
 +        notifyAll();
 +        assertFalse(fired[JOINED] || fired[LEFT]);
 +      }
 +    };
 +    ClientMembership.registerClientMembershipListener(listener);
 +
 +    final VM vm0 = Host.getHost(0).getVM(0);
 +    final String name = this.getUniqueName();
 +    final int[] ports = new int[1];
 +
 +    // create BridgeServer in controller vm...
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInServer] Create BridgeServer");
 +    getSystem();
 +    AttributesFactory factory = new AttributesFactory();
 +    factory.setScope(Scope.LOCAL);
 +    Region region = createRegion(name, factory.create());
 +    assertNotNull(region);
 +    assertNotNull(getRootRegion().getSubregion(name));
 +    
 +    ports[0] = startBridgeServer(0);
 +    assertTrue(ports[0] != 0);
 +    String serverMemberId = getMemberId();
 +    DistributedMember serverMember = getDistributedMember();
 +
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInServer] ports[0]=" + ports[0]);
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInServer] serverMemberId=" + serverMemberId);
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInServer] serverMember=" + serverMember);
 +
 +    assertFalse(fired[JOINED]);
 +    assertNull(member[JOINED]);
 +    assertNull(memberId[JOINED]);
 +    assertFalse(isClient[JOINED]);
 +    assertFalse(fired[LEFT]);
 +    assertNull(member[LEFT]);
 +    assertNull(memberId[LEFT]);
 +    assertFalse(isClient[LEFT]);
 +    assertFalse(fired[CRASHED]);
 +    assertNull(member[CRASHED]);
 +    assertNull(memberId[CRASHED]);
 +    assertFalse(isClient[CRASHED]);
 +    
 +    // sanity check...
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInServer] sanity check");
 +    DistributedMember test = new TestDistributedMember("test");
 +    InternalClientMembership.notifyJoined(test, CLIENT);
 +    synchronized(listener) {
 +      if (!fired[JOINED] && !fired[LEFT] && !fired[CRASHED]) {
 +        listener.wait(2000);
 +      }
 +    }
 +    assertTrue(fired[JOINED]);
 +    assertEquals(test, member[JOINED]);
 +    assertEquals(test.getId(), memberId[JOINED]);
 +    assertTrue(isClient[JOINED]);
 +    assertFalse(fired[LEFT]);
 +    assertNull(member[LEFT]);
 +    assertNull(memberId[LEFT]);
 +    assertFalse(isClient[LEFT]);
 +    assertFalse(fired[CRASHED]);
 +    assertNull(member[CRASHED]);
 +    assertNull(memberId[CRASHED]);
 +    assertFalse(isClient[CRASHED]);
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +    
 +    final Host host = Host.getHost(0);
 +    SerializableRunnable createConnectionPool =
 +    new CacheSerializableRunnable("Create connectionPool") {
 +      public void run2() throws CacheException {
 +        com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInServer] create bridge client");
 +        Properties config = new Properties();
 +        config.setProperty(DistributionConfig.MCAST_PORT_NAME, "0");
 +        config.setProperty(DistributionConfig.LOCATORS_NAME, "");
 +        getSystem(config);
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        ClientServerTestCase.configureConnectionPool(factory, NetworkUtils.getServerHostName(host), ports, true, -1, 2, null);
 +        createRegion(name, factory.create());
 +        assertNotNull(getRootRegion().getSubregion(name));
 +      }
 +    };
 +
 +    // create bridge client in vm0...
 +    vm0.invoke(createConnectionPool);
-     String clientMemberId = (String) vm0.invoke(ClientMembershipDUnitTest.class,
-                                                 "getMemberId");
-     DistributedMember clientMember = (DistributedMember) vm0.invoke(ClientMembershipDUnitTest.class,
-                                                 "getDistributedMember");
++    String clientMemberId = (String) vm0.invoke(() -> ClientMembershipDUnitTest.getMemberId());
++    DistributedMember clientMember = (DistributedMember) vm0.invoke(() -> ClientMembershipDUnitTest.getDistributedMember());
 +                                                
 +    synchronized(listener) {
 +      if (!fired[JOINED] && !fired[LEFT] && !fired[CRASHED]) {
 +        listener.wait(60000);
 +      }
 +    }
 +    
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInServer] assert server detected client join");
 +    assertTrue(fired[JOINED]);
 +    assertEquals(member[JOINED] + " should equal " + clientMember,
 +      clientMember, member[JOINED]);
 +    assertEquals(memberId[JOINED] + " should equal " + clientMemberId,
 +      clientMemberId, memberId[JOINED]);
 +    assertTrue(isClient[JOINED]);
 +    assertFalse(fired[LEFT]);
 +    assertNull(member[LEFT]);
 +    assertNull(memberId[LEFT]);
 +    assertFalse(isClient[LEFT]);
 +    assertFalse(fired[CRASHED]);
 +    assertNull(member[CRASHED]);
 +    assertNull(memberId[CRASHED]);
 +    assertFalse(isClient[CRASHED]);
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +
 +    pauseForClientToJoin();
 +    
 +    vm0.invoke(new SerializableRunnable("Stop bridge client") {
 +      public void run() {
 +        com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInServer] Stop bridge client");
 +        getRootRegion().getSubregion(name).close();
 +        Map m = PoolManager.getAll();
 +        Iterator mit = m.values().iterator();
 +        while(mit.hasNext()) {
 +          Pool p = (Pool)mit.next();
 +          p.destroy();
 +        }
 +      }
 +    });
 +
 +    synchronized(listener) {
 +      if (!fired[JOINED] && !fired[LEFT] && !fired[CRASHED]) {
 +        listener.wait(60000);
 +      }
 +    }
 +    
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInServer] assert server detected client left");
 +    assertFalse(fired[JOINED]);
 +    assertNull(member[JOINED]);
 +    assertNull(memberId[JOINED]);
 +    assertFalse(isClient[JOINED]);
 +    assertTrue(fired[LEFT]);
 +    assertEquals(clientMember, member[LEFT]);
 +    assertEquals(clientMemberId, memberId[LEFT]);
 +    assertTrue(isClient[LEFT]);
 +    assertFalse(fired[CRASHED]);
 +    assertNull(member[CRASHED]);
 +    assertNull(memberId[CRASHED]);
 +    assertFalse(isClient[CRASHED]);
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +
 +    // reconnect bridge client to test for crashed event
 +    vm0.invoke(createConnectionPool);
-     clientMemberId = (String) vm0.invoke(ClientMembershipDUnitTest.class,
-                                          "getMemberId");
++    clientMemberId = (String) vm0.invoke(() -> ClientMembershipDUnitTest.getMemberId());
 +                                                
 +    synchronized(listener) {
 +      if (!fired[JOINED] && !fired[LEFT] && !fired[CRASHED]) {
 +        listener.wait(60000);
 +      }
 +    }
 +    
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInServer] assert server detected client re-join");
 +    assertTrue(fired[JOINED]);
 +    assertEquals(clientMember, member[JOINED]);
 +    assertEquals(clientMemberId, memberId[JOINED]);
 +    assertTrue(isClient[JOINED]);
 +    assertFalse(fired[LEFT]);
 +    assertNull(member[LEFT]);
 +    assertNull(memberId[LEFT]);
 +    assertFalse(isClient[LEFT]);
 +    assertFalse(fired[CRASHED]);
 +    assertNull(member[CRASHED]);
 +    assertNull(memberId[CRASHED]);
 +    assertFalse(isClient[CRASHED]);
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +    
 +    pauseForClientToJoin();
 +
 +    ServerConnection.setForceClientCrashEvent(true);
 +    try {
 +      vm0.invoke(new SerializableRunnable("Stop bridge client") {
 +        public void run() {
 +          com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInServer] Stop bridge client");
 +          getRootRegion().getSubregion(name).close();
 +          Map m = PoolManager.getAll();
 +          Iterator mit = m.values().iterator();
 +          while(mit.hasNext()) {
 +            Pool p = (Pool)mit.next();
 +            p.destroy();
 +          }
 +        }
 +      });
 +  
 +      synchronized(listener) {
 +        if (!fired[JOINED] && !fired[LEFT] && !fired[CRASHED]) {
 +          listener.wait(60000);
 +        }
 +      }
 +      
 +      com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testClientMembershipEventsInServer] assert server detected client crashed");
 +      assertFalse(fired[JOINED]);
 +      assertNull(member[JOINED]);
 +      assertNull(memberId[JOINED]);
 +      assertFalse(isClient[JOINED]);
 +      assertFalse(fired[LEFT]);
 +      assertNull(member[LEFT]);
 +      assertNull(memberId[LEFT]);
 +      assertFalse(isClient[LEFT]);
 +      assertTrue(fired[CRASHED]);
 +      assertEquals(clientMember, member[CRASHED]);
 +      assertEquals(clientMemberId, memberId[CRASHED]);
 +      assertTrue(isClient[CRASHED]);
 +    }
 +    finally {
 +      ServerConnection.setForceClientCrashEvent(false);
 +    }
 +  }
 +  
 +  /**
 +   * The joined event fires when the first client handshake is processed.
 +   * This pauses long enough to allow the rest of the client sockets to
 +   * complete handshaking before making the client leave. Without doing this
 +   * subsequent socket handshakes that are processed could fire join events
 +   * after departure events and then a departure event again. If you see
 +   * failures in testClientMembershipEventsInServer, try increasing this
 +   * timeout.
 +   */
 +  private void pauseForClientToJoin() {
 +    Wait.pause(2000);
 +  }
 +  
 +  /** 
 +   * Tests registration and event notification in conjunction with 
 +   * disconnecting and reconnecting to DistributedSystem. 
 +   */
 +  public void testLifecycle() throws Exception {
 +    final boolean[] fired = new boolean[3];
 +    final DistributedMember[] member = new DistributedMember[3];
 +    final String[] memberId = new String[3];
 +    final boolean[] isClient = new boolean[3];
 +    
 +    // create and register ClientMembershipListener in controller vm...
 +    ClientMembershipListener listener = new ClientMembershipListener() {
 +      public synchronized void memberJoined(ClientMembershipEvent event) {
 +        assertFalse(fired[JOINED]);
 +        assertNull(member[JOINED]);
 +        assertNull(memberId[JOINED]);
 +        assertFalse(isClient[JOINED]);
 +        fired[JOINED] = true;
 +        member[JOINED] = event.getMember();
 +        memberId[JOINED] = event.getMemberId();
 +        isClient[JOINED] = event.isClient();
 +        notifyAll();
 +      }
 +      public synchronized void memberLeft(ClientMembershipEvent event) {
 +      }
 +      public synchronized void memberCrashed(ClientMembershipEvent event) {
 +      }
 +    };
 +    ClientMembership.registerClientMembershipListener(listener);
 +    
 +    // create loner in controller vm...
 +    Properties config = new Properties();
 +    config.setProperty(DistributionConfig.MCAST_PORT_NAME, "0");
 +    config.setProperty(DistributionConfig.LOCATORS_NAME, "");
 +    getSystem(config);
 +    
 +    // assert that event is fired while connected
 +    DistributedMember serverJoined = new TestDistributedMember("serverJoined");
 +    InternalClientMembership.notifyJoined(serverJoined, SERVER);
 +    synchronized(listener) {
 +      if (!fired[JOINED]) {
 +        listener.wait(2000);
 +      }
 +    }
 +    assertTrue(fired[JOINED]);
 +    assertEquals(serverJoined, member[JOINED]);
 +    assertEquals(serverJoined.getId(), memberId[JOINED]);
 +    assertFalse(isClient[JOINED]);
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +    
 +    // assert that event is NOT fired while disconnected
 +    disconnectFromDS();
 +    
 +
 +    InternalClientMembership.notifyJoined(serverJoined, SERVER);
 +    synchronized(listener) {
 +      listener.wait(20);
 +    }
 +    assertFalse(fired[JOINED]);
 +    assertNull(member[JOINED]);
 +    assertNull(memberId[JOINED]);
 +    assertFalse(isClient[JOINED]);
 +    resetArraysForTesting(fired, member, memberId, isClient);
 +    
 +    // assert that event is fired again after reconnecting
 +    InternalDistributedSystem sys = getSystem(config);
 +    assertTrue(sys.isConnected());
 +
 +    InternalClientMembership.notifyJoined(serverJoined, SERVER);
 +    synchronized(listener) {
 +      if (!fired[JOINED]) {
 +        listener.wait(2000);
 +      }
 +    }
 +    assertTrue(fired[JOINED]);
 +    assertEquals(serverJoined, member[JOINED]);
 +    assertEquals(serverJoined.getId(), memberId[JOINED]);
 +    assertFalse(isClient[JOINED]);
 +  }
 +  
 +  /**
 +   * Starts up server in controller vm and 4 clients, then calls and tests
 +   * ClientMembership.getConnectedClients(). 
 +   */
 +  public void testGetConnectedClients() throws Exception {
 +    final String name = this.getUniqueName();
 +    final int[] ports = new int[1];
 +    
 +    IgnoredException.addIgnoredException("ConnectException");
 +
 +    // create BridgeServer in controller vm...
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testGetConnectedClients] Create BridgeServer");
 +    getSystem();
 +    AttributesFactory factory = new AttributesFactory();
 +    factory.setScope(Scope.LOCAL);
 +    Region region = createRegion(name, factory.create());
 +    assertNotNull(region);
 +    assertNotNull(getRootRegion().getSubregion(name));
 +    
 +    ports[0] = startBridgeServer(0);
 +    assertTrue(ports[0] != 0);
 +    String serverMemberId = getMemberId();
 +
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testGetConnectedClients] ports[0]=" + ports[0]);
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testGetConnectedClients] serverMemberId=" + serverMemberId);
 +
 +    final Host host = Host.getHost(0);
 +    SerializableRunnable createPool =
 +    new CacheSerializableRunnable("Create connection pool") {
 +      public void run2() throws CacheException {
 +        com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testGetConnectedClients] create bridge client");
 +        Properties config = new Properties();
 +        config.setProperty(DistributionConfig.MCAST_PORT_NAME, "0");
 +        config.setProperty(DistributionConfig.LOCATORS_NAME, "");
 +        // 11/30/2015 this test is periodically failing during distributedTest runs
 +        // so we are setting the log-level to fine to figure out what's going on
 +        config.setProperty(DistributionConfig.LOG_LEVEL_NAME, "fine");
 +        getSystem(config);
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        Pool p = ClientServerTestCase.configureConnectionPool(factory, NetworkUtils.getServerHostName(host), ports, true, -1, -1, null);
 +        createRegion(name, factory.create());
 +        assertNotNull(getRootRegion().getSubregion(name));
 +        assertTrue(p.getServers().size() > 0);
 +      }
 +    };
 +
 +    // create bridge client in vm0...
 +    final String[] clientMemberIdArray = new String[host.getVMCount()];
 +    
 +    for (int i = 0; i < host.getVMCount(); i++) { 
 +      final VM vm = Host.getHost(0).getVM(i);
 +      System.out.println("creating pool in vm_"+i);
 +      vm.invoke(createPool);
-       clientMemberIdArray[i] =  String.valueOf(vm.invoke(
-         ClientMembershipDUnitTest.class, "getMemberId"));
++      clientMemberIdArray[i] =  String.valueOf(vm.invoke(() -> ClientMembershipDUnitTest.getMemberId()));
 +    }
 +    Collection clientMemberIds = Arrays.asList(clientMemberIdArray);
 +                                                
 +    {
 +      final int expectedClientCount = clientMemberIds.size();
 +      WaitCriterion wc = new WaitCriterion() {
 +        public String description() {
 +          return "wait for clients";
 +        }
 +        public boolean done() {
 +          Map connectedClients = InternalClientMembership.getConnectedClients(false);
 +          if (connectedClients == null) {
 +            return false;
 +          }
 +          if (connectedClients.size() != expectedClientCount) {
 +            return false;
 +          }
 +          return true;
 +        }
 +      };
 +      Wait.waitForCriterion(wc, 30000, 100, false);
 +    }
 +    
 +    Map connectedClients = InternalClientMembership.getConnectedClients(false);
 +    assertNotNull(connectedClients);
 +    assertEquals(clientMemberIds.size(), connectedClients.size());
 +    for (Iterator iter = connectedClients.keySet().iterator(); iter.hasNext();) {
 +      String connectedClient = (String)iter.next();
 +      com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testGetConnectedClients] checking for client " + connectedClient);
 +      assertTrue(clientMemberIds.contains(connectedClient));
 +      Object[] result = (Object[])connectedClients.get(connectedClient);
 +      com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testGetConnectedClients] result: " + 
 +                          (result==null? "none"
 +                              : String.valueOf(result[0])+"; connections="+result[1]));
 +    }
 +  }
 +
 +  /**
 +   * Starts up 4 server and the controller vm as a client, then calls and tests
 +   * ClientMembership.getConnectedServers(). 
 +   */
 +  public void testGetConnectedServers() throws Exception {
 +    final Host host = Host.getHost(0);
 +    final String name = this.getUniqueName();
 +    final int[] ports = new int[host.getVMCount()];
 +    
 +    for (int i = 0; i < host.getVMCount(); i++) { 
 +      final int whichVM = i;
 +      final VM vm = Host.getHost(0).getVM(i);
 +      vm.invoke(new CacheSerializableRunnable("Create bridge server") {
 +        public void run2() throws CacheException {
 +          // create BridgeServer in controller vm...
 +          com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testGetConnectedServers] Create BridgeServer");
 +          getSystem();
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.LOCAL);
 +          Region region = createRegion(name+"_"+whichVM, factory.create());
 +          assertNotNull(region);
 +          assertNotNull(getRootRegion().getSubregion(name+"_"+whichVM));
 +          region.put("KEY-1", "VAL-1");
 +          
 +          try {
 +            testGetConnectedServers_port = startBridgeServer(0);
 +          }
 +          catch (IOException e) {
 +            com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().error("startBridgeServer threw IOException", e);
 +            fail("startBridgeServer threw IOException " + e.getMessage());
 +          }
 +          
 +          assertTrue(testGetConnectedServers_port != 0);
 +      
 +          com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testGetConnectedServers] port=" + 
 +            ports[whichVM]);
 +          com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testGetConnectedServers] serverMemberId=" + 
 +            getDistributedMember());
 +        }
 +      });
-       ports[whichVM] = vm.invokeInt(ClientMembershipDUnitTest.class, 
-                                     "getTestGetConnectedServers_port");
++      ports[whichVM] = vm.invoke(() -> ClientMembershipDUnitTest.getTestGetConnectedServers_port());
 +      assertTrue(ports[whichVM] != 0);
 +    }
 +    
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testGetConnectedServers] create bridge client");
 +    Properties config = new Properties();
 +    config.setProperty(DistributionConfig.MCAST_PORT_NAME, "0");
 +    config.setProperty(DistributionConfig.LOCATORS_NAME, "");
 +    getSystem(config);
 +    getCache();
 +    
 +    AttributesFactory factory = new AttributesFactory();
 +    factory.setScope(Scope.LOCAL);
 +
 +    for (int i = 0; i < ports.length; i++) {
 +      com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testGetConnectedServers] creating connectionpool for " + 
 +        NetworkUtils.getServerHostName(host) + " " + ports[i]);
 +      int[] thisServerPorts = new int[] { ports[i] };
 +      ClientServerTestCase.configureConnectionPoolWithName(factory, NetworkUtils.getServerHostName(host), thisServerPorts, false, -1, -1, null,"pooly"+i);
 +      Region region = createRegion(name+"_"+i, factory.create());
 +      assertNotNull(getRootRegion().getSubregion(name+"_"+i));
 +      region.get("KEY-1");
 +    }
 +
 +    {
 +      final int expectedVMCount = host.getVMCount();
 +      WaitCriterion wc = new WaitCriterion() {
 +        public String description() {
 +          return "wait for pools and servers";
 +        }
 +        public boolean done() {
 +          if (PoolManager.getAll().size() != expectedVMCount) {
 +            return false;
 +          }
 +          Map connectedServers = InternalClientMembership.getConnectedServers();
 +          if (connectedServers == null) {
 +            return false;
 +          }
 +          if (connectedServers.size() != expectedVMCount) {
 +            return false;
 +          }
 +          return true;
 +        }
 +      };
 +      Wait.waitForCriterion(wc, 60000, 100, false);
 +    }
 +
 +    {
 +      assertEquals(host.getVMCount(), PoolManager.getAll().size());
 +      
 +    }
 +    
 +    Map connectedServers = InternalClientMembership.getConnectedServers();
 +    assertNotNull(connectedServers);
 +    assertEquals(host.getVMCount(), connectedServers.size());
 +    for (Iterator iter = connectedServers.keySet().iterator(); iter.hasNext();) {
 +      String connectedServer = (String) iter.next();
 +      com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testGetConnectedServers]  value for connectedServer: " + 
 +                          connectedServers.get(connectedServer));
 +    }
 +  }
 +
 +  protected static int testGetConnectedServers_port;
 +  private static int getTestGetConnectedServers_port() {
 +    return testGetConnectedServers_port;
 +  }
 +
 +  /**
 +   * Tests getConnectedClients(boolean onlyClientsNotifiedByThisServer) where
 +   * onlyClientsNotifiedByThisServer is true.
 +   */
 +  public void testGetNotifiedClients() throws Exception {
 +    final Host host = Host.getHost(0);
 +    final String name = this.getUniqueName();
 +    final int[] ports = new int[host.getVMCount()];
 +    
 +    for (int i = 0; i < host.getVMCount(); i++) { 
 +      final int whichVM = i;
 +      final VM vm = Host.getHost(0).getVM(i);
 +      vm.invoke(new CacheSerializableRunnable("Create bridge server") {
 +        public void run2() throws CacheException {
 +          // create BridgeServer in controller vm...
 +          com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testGetNotifiedClients] Create BridgeServer");
 +          getSystem();
 +          AttributesFactory factory = new AttributesFactory();
 +          Region region = createRegion(name, factory.create());
 +          assertNotNull(region);
 +          assertNotNull(getRootRegion().getSubregion(name));
 +          region.put("KEY-1", "VAL-1");
 +          
 +          try {
 +            testGetNotifiedClients_port = startBridgeServer(0);
 +          }
 +          catch (IOException e) {
 +            com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().error("startBridgeServer threw IOException", e);
 +            fail("startBridgeServer threw IOException " + e.getMessage());
 +          }
 +          
 +          assertTrue(testGetNotifiedClients_port != 0);
 +      
 +          com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testGetNotifiedClients] port=" + 
 +            ports[whichVM]);
 +          com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testGetNotifiedClients] serverMemberId=" + 
 +            getMemberId());
 +        }
 +      });
-       ports[whichVM] = vm.invokeInt(ClientMembershipDUnitTest.class, 
-                                     "getTestGetNotifiedClients_port");
++      ports[whichVM] = vm.invoke(() -> ClientMembershipDUnitTest.getTestGetNotifiedClients_port());
 +      assertTrue(ports[whichVM] != 0);
 +    }
 +    
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testGetNotifiedClients] create bridge client");
 +    Properties config = new Properties();
 +    config.setProperty(DistributionConfig.MCAST_PORT_NAME, "0");
 +    config.setProperty(DistributionConfig.LOCATORS_NAME, "");
 +    getSystem(config);
 +    getCache();
 +    
 +    AttributesFactory factory = new AttributesFactory();
 +    factory.setScope(Scope.LOCAL);
 +
 +    com.gemstone.gemfire.test.dunit.LogWriterUtils.getLogWriter().info("[testGetNotifiedClients] creating connection pool");
 +    ClientServerTestCase.configureConnectionPool(factory, NetworkUtils.getServerHostName(host), ports, true, -1, -1, null);
 +    Region region = createRegion(name, factory.create());
 +    assertNotNull(getRootRegion().getSubregion(name));
 +    region.registerInterest("KEY-1");
 +    region.get("KEY-1");
 +
 +    final String clientMemberId = getMemberId();
 +    
 +    pauseForClientToJoin();
 +    
 +    // assertions go here
 +    int[] clientCounts = new int[host.getVMCount()];
 +    
 +    // only one server vm will have that client for updating
 +    for (int i = 0; i < host.getVMCount(); i++) { 
 +      final int whichVM = i;
 +      final VM vm = Host.getHost(0).getVM(i);
 +      vm.invoke(new CacheSerializableRunnable("Create bridge server") {
 +        public void run2() throws CacheException {
 +          Map clients = InternalClientMembership.getConnectedClients(true);
 +          assertNotNull(clients);
 +          testGetNotifiedClients_clientCount = clients.size();
 +          if (testGetNotifiedClients_clientCount > 0) {
 +            // assert that the clientMemberId matches
 +            assertEquals(clientMemberId, clients.keySet().iterator().next());
 +          }
 +        }
 +      });
-       clientCounts[whichVM] = vm.invokeInt(ClientMembershipDUnitTest.class, 
-                               "getTestGetNotifiedClients_clientCount");
++      clientCounts[whichVM] = vm.invoke(() -> ClientMembershipDUnitTest.getTestGetNotifiedClients_clientCount());
 +    }
 +    
 +    // only one server should have a notifier for this client...
 +    int totalClientCounts = 0;
 +    for (int i = 0; i < clientCounts.length; i++) {
 +      totalClientCounts += clientCounts[i];
 +    }
 +    // this assertion fails because the count is 4
 +    //assertEquals(1, totalClientCounts);
 +  }
 +  protected static int testGetNotifiedClients_port;
 +  private static int getTestGetNotifiedClients_port() {
 +    return testGetNotifiedClients_port;
 +  }
 +  protected static int testGetNotifiedClients_clientCount;
 +  private static int getTestGetNotifiedClients_clientCount() {
 +    return testGetNotifiedClients_clientCount;
 +  }
 +
 +  // Simple DistributedMember implementation
 +  static final class TestDistributedMember implements DistributedMember {
 +    
 +    private String host;
 +    
 +    public TestDistributedMember(String host) {
 +      this.host = host;
 +    }
 +
 +    public String getName() {
 +      return "";
 +    }
 +
 +    public String getHost() {
 +      return this.host;
 +    }
 +
 +    public Set getRoles() {
 +      return new HashSet();
 +    }
 +
 +    public int getProcessId() {
 +      return 0;
 +    }
 +
 +    public String getId() {
 +      return this.host;
 +    }
 +    
 +    public int compareTo(DistributedMember o) {
 +      if ((o == null) || !(o instanceof TestDistributedMember)) {
 +        throw new InternalGemFireException("Invalidly comparing TestDistributedMember to " + o);
 +      }
 +      
 +      TestDistributedMember tds = (TestDistributedMember) o;
 +      return getHost().compareTo(tds.getHost());
 +    }
 +    
 +    public boolean equals(Object obj) {
 +      if ((obj == null) || !(obj instanceof TestDistributedMember)) {
 +        return false;
 +      }
 +      return compareTo((TestDistributedMember)obj) == 0;
 +    }
 +    
 +    public int hashCode() {
 +      return getHost().hashCode();
 +    }
 +    
 +    public DurableClientAttributes getDurableClientAttributes() {
 +      
 +      return null;
 +    }
 +
 +    public List<String> getGroups() {
 +      return Collections.emptyList();
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/ClientRegisterInterestDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/ClientRegisterInterestDUnitTest.java
index a734a10,0000000..f7e2277
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/ClientRegisterInterestDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/ClientRegisterInterestDUnitTest.java
@@@ -1,429 -1,0 +1,426 @@@
 +/*
 + * 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.cache30;
 +
 +import java.io.IOException;
 +import java.util.Properties;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.client.internal.PoolImpl;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.LogWriterUtils;
 +import com.gemstone.gemfire.test.dunit.NetworkUtils;
 +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.cache.client.SubscriptionNotEnabledException;
 +
 +/**
 + * Tests the client register interest
 + *
 + * @author Kirk Lund
 + * @since 4.2.3
 + */
 +public class ClientRegisterInterestDUnitTest extends ClientServerTestCase {
 +
 +  public ClientRegisterInterestDUnitTest(String name) {
 +    super(name);
 +  }
 +  
 +  @Override
 +  protected final void postTearDownCacheTestCase() throws Exception {
 +    disconnectAllFromDS(); // cleans up bridge server and client and lonerDS
 +  }
 +  
 +  /**
 +   * Tests for Bug 35381 Calling register interest if 
 +   * establishCallbackConnection is not set causes bridge server NPE.
 +   */
 +  public void testBug35381() throws Exception {
 +    final Host host = Host.getHost(0);
 +    final String name = this.getUniqueName();
 +    final int[] ports = new int[1]; // 1 server in this test
 +    
 +    final int whichVM = 0;
 +    final VM vm = Host.getHost(0).getVM(whichVM);
 +    vm.invoke(new CacheSerializableRunnable("Create bridge server") {
 +      public void run2() throws CacheException {
 +        LogWriterUtils.getLogWriter().info("[testBug35381] Create BridgeServer");
 +        getSystem();
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        Region region = createRegion(name, factory.create());
 +        assertNotNull(region);
 +        assertNotNull(getRootRegion().getSubregion(name));
 +        region.put("KEY-1", "VAL-1");
 +        
 +        try {
 +          bridgeServerPort = startBridgeServer(0);
 +        }
 +        catch (IOException e) {
 +          LogWriterUtils.getLogWriter().error("startBridgeServer threw IOException", e);
 +          fail("startBridgeServer threw IOException " + e.getMessage());
 +        }
 +        
 +        assertTrue(bridgeServerPort != 0);
 +    
 +        LogWriterUtils.getLogWriter().info("[testBug35381] port=" + bridgeServerPort);
 +        LogWriterUtils.getLogWriter().info("[testBug35381] serverMemberId=" + getMemberId());
 +      }
 +    });
-     ports[whichVM] = vm.invokeInt(ClientRegisterInterestDUnitTest.class, 
-                                   "getBridgeServerPort");
++    ports[whichVM] = vm.invoke(() -> ClientRegisterInterestDUnitTest.getBridgeServerPort());
 +    assertTrue(ports[whichVM] != 0);
 +    
 +    LogWriterUtils.getLogWriter().info("[testBug35381] create bridge client");
 +    Properties config = new Properties();
 +    config.setProperty(DistributionConfig.MCAST_PORT_NAME, "0");
 +    config.setProperty(DistributionConfig.LOCATORS_NAME, "");
 +    getSystem(config);
 +    getCache();
 +    
 +    AttributesFactory factory = new AttributesFactory();
 +    factory.setScope(Scope.LOCAL);
 +
 +    LogWriterUtils.getLogWriter().info("[testBug35381] creating connection pool");
 +    boolean establishCallbackConnection = false; // SOURCE OF BUG 35381
 +    ClientServerTestCase.configureConnectionPool(factory, NetworkUtils.getServerHostName(host), ports, establishCallbackConnection, -1, -1, null);
 +    Region region = createRegion(name, factory.create());
 +    assertNotNull(getRootRegion().getSubregion(name));
 +    try {
 +      region.registerInterest("KEY-1");
 +      fail("registerInterest failed to throw SubscriptionNotEnabledException with establishCallbackConnection set to false"); 
 +    }
 +    catch (SubscriptionNotEnabledException expected) {
 +    }
 +  }
 +  protected static int bridgeServerPort;
 +  private static int getBridgeServerPort() {
 +    return bridgeServerPort;
 +  }
 +  
 +  /**
 +   * Tests failover of register interest from client point of view. Related
 +   * bugs include:
 +   *
 +   * <p>Bug 35654 "failed re-registration may never be detected and thus
 +   * may never re-re-register"
 +   *
 +   * <p>Bug 35639 "registerInterest re-registration happens everytime a healthy
 +   * server is detected"
 +   *
 +   * <p>Bug 35655 "a single failed re-registration causes all other pending
 +   * re-registrations to be cancelled"
 +   */
 +  public void _testRegisterInterestFailover() throws Exception {
 +    // controller is bridge client
 +    
 +    final Host host = Host.getHost(0);
 +    final String name = this.getUniqueName();
 +    final String regionName1 = name+"-1";
 +    final String regionName2 = name+"-2";
 +    final String regionName3 = name+"-3";
 +    final String key1 = "KEY-"+regionName1+"-1";
 +    final String key2 = "KEY-"+regionName1+"-2";
 +    final String key3 = "KEY-"+regionName1+"-3";
 +    final int[] ports = new int[3]; // 3 servers in this test
 +    
 +    // create first bridge server with region for client...
 +    final int firstServerIdx = 0;
 +    final VM firstServerVM = Host.getHost(0).getVM(firstServerIdx);
 +    firstServerVM.invoke(new CacheSerializableRunnable("Create first bridge server") {
 +      public void run2() throws CacheException {
 +        LogWriterUtils.getLogWriter().info("[testRegisterInterestFailover] Create first bridge server");
 +        getSystem();
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        Region region1 = createRootRegion(regionName1, factory.create());
 +        Region region2 = createRootRegion(regionName2, factory.create());
 +        Region region3 = createRootRegion(regionName3, factory.create());
 +        region1.put(key1, "VAL-1");
 +        region2.put(key2, "VAL-1");
 +        region3.put(key3, "VAL-1");
 +        
 +        try {
 +          bridgeServerPort = startBridgeServer(0);
 +        }
 +        catch (IOException e) {
 +          LogWriterUtils.getLogWriter().error("startBridgeServer threw IOException", e);
 +          fail("startBridgeServer threw IOException " + e.getMessage());
 +        }
 +        
 +        assertTrue(bridgeServerPort != 0);
 +    
 +        LogWriterUtils.getLogWriter().info("[testRegisterInterestFailover] " +
 +          "firstServer port=" + bridgeServerPort);
 +        LogWriterUtils.getLogWriter().info("[testRegisterInterestFailover] " +
 +          "firstServer memberId=" + getMemberId());
 +      }
 +    });
 +
 +    // create second bridge server missing region for client...
 +    final int secondServerIdx = 1;
 +    final VM secondServerVM = Host.getHost(0).getVM(secondServerIdx);
 +    secondServerVM.invoke(new CacheSerializableRunnable("Create second bridge server") {
 +      public void run2() throws CacheException {
 +        LogWriterUtils.getLogWriter().info("[testRegisterInterestFailover] Create second bridge server");
 +        getSystem();
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        Region region1 = createRootRegion(regionName1, factory.create());
 +        Region region3 = createRootRegion(regionName3, factory.create());
 +        region1.put(key1, "VAL-2");
 +        region3.put(key3, "VAL-2");
 +        
 +        try {
 +          bridgeServerPort = startBridgeServer(0);
 +        }
 +        catch (IOException e) {
 +          LogWriterUtils.getLogWriter().error("startBridgeServer threw IOException", e);
 +          fail("startBridgeServer threw IOException " + e.getMessage());
 +        }
 +        
 +        assertTrue(bridgeServerPort != 0);
 +    
 +        LogWriterUtils.getLogWriter().info("[testRegisterInterestFailover] " +
 +          "secondServer port=" + bridgeServerPort);
 +        LogWriterUtils.getLogWriter().info("[testRegisterInterestFailover] " +
 +          "secondServer memberId=" + getMemberId());
 +      }
 +    });
 +
 +    // get the bridge server ports...
-     ports[firstServerIdx] = firstServerVM.invokeInt(
-       ClientRegisterInterestDUnitTest.class, "getBridgeServerPort");
++    ports[firstServerIdx] = firstServerVM.invoke(() -> ClientRegisterInterestDUnitTest.getBridgeServerPort());
 +    assertTrue(ports[firstServerIdx] != 0);
-     ports[secondServerIdx] = secondServerVM.invokeInt(
-       ClientRegisterInterestDUnitTest.class, "getBridgeServerPort");
++    ports[secondServerIdx] = secondServerVM.invoke(() -> ClientRegisterInterestDUnitTest.getBridgeServerPort());
 +    assertTrue(ports[secondServerIdx] != 0);
 +    assertTrue(ports[firstServerIdx] != ports[secondServerIdx]);
 +    
 +    // stop second and third servers
 +    secondServerVM.invoke(new CacheSerializableRunnable("Stop second bridge server") {
 +      public void run2() throws CacheException {
 +        stopBridgeServers(getCache());
 +      }
 +    });
 +    
 +    // create the bridge client
 +    LogWriterUtils.getLogWriter().info("[testBug35654] create bridge client");
 +    Properties config = new Properties();
 +    config.setProperty(DistributionConfig.MCAST_PORT_NAME, "0");
 +    config.setProperty(DistributionConfig.LOCATORS_NAME, "");
 +    getSystem(config);
 +    getCache();
 +    
 +    AttributesFactory factory = new AttributesFactory();
 +    factory.setScope(Scope.LOCAL);
 +
 +    LogWriterUtils.getLogWriter().info("[testRegisterInterestFailover] creating connection pool");
 +    boolean establishCallbackConnection = true;
 +    final PoolImpl p = (PoolImpl)ClientServerTestCase.configureConnectionPool(factory, NetworkUtils.getServerHostName(host), ports, establishCallbackConnection, -1, -1, null);
 +
 +    final Region region1 = createRootRegion(regionName1, factory.create());
 +    final Region region2 = createRootRegion(regionName2, factory.create());
 +    final Region region3 = createRootRegion(regionName3, factory.create());
 +
 +    assertTrue(region1.getInterestList().isEmpty());
 +    assertTrue(region2.getInterestList().isEmpty());
 +    assertTrue(region3.getInterestList().isEmpty());
 +
 +    region1.registerInterest(key1);
 +    region2.registerInterest(key2);
 +    region3.registerInterest(key3);
 +
 +    assertTrue(region1.getInterestList().contains(key1));
 +    assertTrue(region2.getInterestList().contains(key2));
 +    assertTrue(region3.getInterestList().contains(key3));
 +    
 +    assertTrue(region1.getInterestListRegex().isEmpty());
 +    assertTrue(region2.getInterestListRegex().isEmpty());
 +    assertTrue(region3.getInterestListRegex().isEmpty());
 +    
 +    // get ConnectionProxy and wait until connected to first server
 +    WaitCriterion ev = new WaitCriterion() {
 +      public boolean done() {
 +        return p.getPrimaryPort() != -1;
 +      }
 +      public String description() {
 +        return "primary port remained invalid";
 +      }
 +    };
 +    Wait.waitForCriterion(ev, 10 * 1000, 200, true);
 +    assertEquals(ports[firstServerIdx], p.getPrimaryPort()); 
 +    
 +    // assert intial values
 +    assertEquals("VAL-1", region1.get(key1));
 +    assertEquals("VAL-1", region2.get(key2));
 +    assertEquals("VAL-1", region3.get(key3));
 +    
 +    // do puts on server1 and make sure values come thru for all 3 registrations
 +    firstServerVM.invoke(new CacheSerializableRunnable("Puts from first bridge server") {
 +      public void run2() throws CacheException {
 +        Region region1 = getCache().getRegion(regionName1);
 +        region1.put(key1, "VAL-1-1");
 +        Region region2 = getCache().getRegion(regionName2);
 +        region2.put(key2, "VAL-1-1");
 +        Region region3 = getCache().getRegion(regionName3);
 +        region3.put(key3, "VAL-1-1");
 +      }
 +    });
 +
 +    ev = new WaitCriterion() {
 +      public boolean done() {
 +        if (!"VAL-1-1".equals(region1.get(key1)) || 
 +            !"VAL-1-1".equals(region2.get(key2)) ||
 +            !"VAL-1-1".equals(region3.get(key3))
 +            ) return  false;
 +        return true;
 +      }
 +      public String description() {
 +        return null;
 +      }
 +    };
 +    Wait.waitForCriterion(ev, 10 * 1000, 200, true);
 +    assertEquals("VAL-1-1", region1.get(key1));
 +    assertEquals("VAL-1-1", region2.get(key2));
 +    assertEquals("VAL-1-1", region3.get(key3));
 +    
 +    // force failover to server 2
 +    secondServerVM.invoke(new CacheSerializableRunnable("Start second bridge server") {
 +      public void run2() throws CacheException {
 +        try {
 +          startBridgeServer(ports[secondServerIdx]);
 +        }
 +        catch (IOException e) {
 +          LogWriterUtils.getLogWriter().error("startBridgeServer threw IOException", e);
 +          fail("startBridgeServer threw IOException " + e.getMessage());
 +        }
 +      }
 +    });
 +   
 +    firstServerVM.invoke(new CacheSerializableRunnable("Stop first bridge server") {
 +      public void run2() throws CacheException {
 +        stopBridgeServers(getCache());
 +      }
 +    });
 +
 +    // wait for failover to second server
 +    ev = new WaitCriterion() {
 +      public boolean done() {
 +        return ports[secondServerIdx] == p.getPrimaryPort();
 +      }
 +      public String description() {
 +        return "primary port never became " + ports[secondServerIdx];
 +      }
 +    };
 +    Wait.waitForCriterion(ev, 100 * 1000, 200, true);
 +    
 +    try {
 +      assertEquals(null, region2.get(key2));
 +      fail("CacheLoaderException expected");
 +    }
 +    catch (com.gemstone.gemfire.cache.CacheLoaderException e) {
 +    }
 +  
 +    // region2 registration should be gone now
 +    // do puts on server2 and make sure values come thru for only 2 registrations
 +    secondServerVM.invoke(new CacheSerializableRunnable("Puts from second bridge server") {
 +      public void run2() throws CacheException {
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        createRootRegion(regionName2, factory.create());
 +      }
 +    });
 +    
 +    // assert that there is no actively registered interest on region2
 +    assertTrue(region2.getInterestList().isEmpty());
 +    assertTrue(region2.getInterestListRegex().isEmpty());
 +
 +    region2.put(key2, "VAL-0");
 +    
 +    secondServerVM.invoke(new CacheSerializableRunnable("Put from second bridge server") {
 +      public void run2() throws CacheException {
 +        Region region1 = getCache().getRegion(regionName1);
 +        region1.put(key1, "VAL-2-2");
 +        Region region2 = getCache().getRegion(regionName2);
 +        region2.put(key2, "VAL-2-1");
 +        Region region3 = getCache().getRegion(regionName3);
 +        region3.put(key3, "VAL-2-2");
 +      }
 +    });
 +    
 +    // wait for updates to come thru
 +    ev = new WaitCriterion() {
 +      public boolean done() {
 +        if (!"VAL-2-2".equals(region1.get(key1)) || 
 +            !"VAL-2-2".equals(region3.get(key3)))
 +          return false;
 +        return true;
 +      }
 +      public String description() {
 +        return null;
 +      }
 +    };
 +    Wait.waitForCriterion(ev, 100 * 1000, 200, true);
 +    assertEquals("VAL-2-2", region1.get(key1));
 +    assertEquals("VAL-0",   region2.get(key2));
 +    assertEquals("VAL-2-2", region3.get(key3));
 +
 +    // assert again that there is no actively registered interest on region2
 +    assertTrue(region2.getInterestList().isEmpty());
 +
 +    // register interest again on region2 and make
 +    region2.registerInterest(key2);
 +    assertEquals("VAL-2-1", region2.get(key2));
 +    
 +    secondServerVM.invoke(new CacheSerializableRunnable("Put from second bridge server") {
 +      public void run2() throws CacheException {
 +        Region region1 = getCache().getRegion(regionName1);
 +        region1.put(key1, "VAL-2-3");
 +        Region region2 = getCache().getRegion(regionName2);
 +        region2.put(key2, "VAL-2-2");
 +        Region region3 = getCache().getRegion(regionName3);
 +        region3.put(key3, "VAL-2-3");
 +      }
 +    });
 +    
 +    // wait for updates to come thru
 +    ev = new WaitCriterion() {
 +      public boolean done() {
 +        if (!"VAL-2-3".equals(region1.get(key1)) || 
 +            !"VAL-2-2".equals(region2.get(key2)) ||
 +            !"VAL-2-3".equals(region3.get(key3)))
 +          return false;
 +        return true;
 +      }
 +      public String description() {
 +        return null;
 +      }
 +    };
 +    Wait.waitForCriterion(ev, 100 * 1000, 200, true);
 +    assertEquals("VAL-2-3", region1.get(key1));
 +    assertEquals("VAL-2-2", region2.get(key2));
 +    assertEquals("VAL-2-3", region3.get(key3));
 +
 +    // assert public methods report actively registered interest on region2
 +    assertTrue(region2.getInterestList().contains(key2));
 +  }
 +  
 +}
 +



[16/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache/query/dunit/PdxStringQueryDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache/query/dunit/PdxStringQueryDUnitTest.java
index 42459c9,0000000..934798c
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache/query/dunit/PdxStringQueryDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache/query/dunit/PdxStringQueryDUnitTest.java
@@@ -1,1987 -1,0 +1,1987 @@@
 +/*
 + * 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.cache.query.dunit;
 +
 +import java.io.IOException;
 +import java.util.HashMap;
 +import java.util.Iterator;
 +import java.util.List;
 +import java.util.Properties;
 +import java.util.Set;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.PartitionAttributes;
 +import com.gemstone.gemfire.cache.PartitionAttributesFactory;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionShortcut;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.client.ClientCache;
 +import com.gemstone.gemfire.cache.client.ClientCacheFactory;
 +import com.gemstone.gemfire.cache.client.ClientRegionShortcut;
 +import com.gemstone.gemfire.cache.client.PoolFactory;
 +import com.gemstone.gemfire.cache.client.PoolManager;
 +import com.gemstone.gemfire.cache.query.Index;
 +import com.gemstone.gemfire.cache.query.Query;
 +import com.gemstone.gemfire.cache.query.QueryService;
 +import com.gemstone.gemfire.cache.query.SelectResults;
 +import com.gemstone.gemfire.cache.query.Struct;
 +import com.gemstone.gemfire.cache.query.data.Portfolio;
 +import com.gemstone.gemfire.cache.query.data.PortfolioPdx;
 +import com.gemstone.gemfire.cache.query.data.PositionPdx;
 +import com.gemstone.gemfire.cache.query.internal.DefaultQuery;
 +import com.gemstone.gemfire.cache.query.internal.index.CompactRangeIndex;
 +import com.gemstone.gemfire.cache.query.internal.index.IndexManager;
 +import com.gemstone.gemfire.cache.query.internal.index.IndexStore.IndexStoreEntry;
 +import com.gemstone.gemfire.cache.query.internal.index.PartitionedIndex;
 +import com.gemstone.gemfire.cache.query.internal.index.RangeIndex;
 +import com.gemstone.gemfire.cache.query.types.CollectionType;
 +import com.gemstone.gemfire.cache.query.types.ObjectType;
 +import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.cache30.ClientServerTestCase;
 +import com.gemstone.gemfire.cache30.CacheSerializableRunnable;
 +import com.gemstone.gemfire.cache30.CacheTestCase;
 +import com.gemstone.gemfire.internal.AvailablePortHelper;
 +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
 +import com.gemstone.gemfire.internal.cache.persistence.query.CloseableIterator;
 +import com.gemstone.gemfire.pdx.internal.PdxString;
 +import com.gemstone.gemfire.test.dunit.Assert;
 +import com.gemstone.gemfire.test.dunit.DistributedTestCase;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.IgnoredException;
 +import com.gemstone.gemfire.test.dunit.Invoke;
 +import com.gemstone.gemfire.test.dunit.LogWriterUtils;
 +import com.gemstone.gemfire.test.dunit.NetworkUtils;
 +import com.gemstone.gemfire.test.dunit.SerializableCallable;
 +import com.gemstone.gemfire.test.dunit.SerializableRunnable;
 +import com.gemstone.gemfire.test.dunit.VM;
 +
 +public class PdxStringQueryDUnitTest extends CacheTestCase{
 +  private static int bridgeServerPort;
 +
 +  public PdxStringQueryDUnitTest(String name) {
 +    super(name);
 +   }
 +
 +  private final String rootRegionName = "root";
 +  private final String regionName = "PdxTest";
 +  private final String regName = "/" + rootRegionName + "/" + regionName;
 +  private final static int orderByQueryIndex = 11;
 +  private final static int [] groupByQueryIndex = new int[]{7, 8, 9,10};
 +  
 +  private final String[] queryString = new String[] { 
 +      "SELECT pos.secId FROM " + regName + " p, p.positions.values pos WHERE pos.secId LIKE '%L'",//0
 +      "SELECT pos.secId FROM " + regName + " p, p.positions.values pos where pos.secId = 'IBM'",//1
 +      "SELECT pos.secId, p.status FROM " + regName + " p, p.positions.values pos where pos.secId > 'APPL'",//2
 +      "SELECT pos.secId FROM " + regName + " p, p.positions.values pos WHERE pos.secId > 'APPL' and pos.secId < 'SUN'",//3
 +      "select pos.secId from " + regName + " p, p.positions.values pos where pos.secId  IN SET ('YHOO', 'VMW')",//4
 +      "select pos.secId from " + regName + " p, p.positions.values pos where NOT (pos.secId = 'VMW')",//5
 +      "select pos.secId from " + regName + " p, p.positions.values pos where NOT (pos.secId IN SET('SUN', 'ORCL')) ",//6
 +      "select pos.secId , count(pos.id) from " + regName + " p, p.positions.values pos where  pos.secId > 'APPL' group by pos.secId ",//7
 +      "select pos.secId , sum(pos.id) from " + regName + " p, p.positions.values pos where  pos.secId > 'APPL' group by pos.secId ",//8,
 +      "select pos.secId , count(distinct pos.secId) from " + regName + " p, p.positions.values pos where  pos.secId > 'APPL' group by pos.secId ",//9
 +      "select  count(distinct pos.secId) from " + regName + " p, p.positions.values pos where  pos.secId > 'APPL' ",//10
 +      "SELECT distinct pos.secId FROM " + regName + " p, p.positions.values pos order by pos.secId",//11
 +      "SELECT distinct pos.secId FROM " + regName + " p, p.positions.values pos WHERE p.ID > 1 order by pos.secId limit 5",//12
 + };
 +
 + private final String[] queryString2 = new String[] { 
 +      "SELECT pos.secIdIndexed FROM " + regName + " p, p.positions.values pos WHERE pos.secIdIndexed LIKE '%L'",//0
 +      "SELECT pos.secIdIndexed FROM " + regName + " p, p.positions.values pos where pos.secIdIndexed = 'IBM'",//1
 +      "SELECT pos.secIdIndexed, p.status FROM " + regName + " p, p.positions.values pos where pos.secIdIndexed > 'APPL'",//2
 +      "SELECT pos.secIdIndexed FROM " + regName + " p, p.positions.values pos WHERE pos.secIdIndexed > 'APPL' and pos.secIdIndexed < 'SUN'",//3
 +      "select pos.secIdIndexed from " + regName + " p, p.positions.values pos where pos.secIdIndexed  IN SET ('YHOO', 'VMW')",//4
 +      "select pos.secIdIndexed from " + regName + " p, p.positions.values pos where NOT (pos.secIdIndexed = 'VMW')",//5
 +      "select pos.secIdIndexed from " + regName + " p, p.positions.values pos where NOT (pos.secIdIndexed IN SET('SUN', 'ORCL')) ",//6
 +      "select pos.secIdIndexed , count(pos.id) from " + regName + " p, p.positions.values pos where  pos.secIdIndexed > 'APPL' group by pos.secIdIndexed ",//7
 +      "select pos.secIdIndexed , sum(pos.id) from " + regName + " p, p.positions.values pos where  pos.secIdIndexed > 'APPL' group by pos.secIdIndexed ",//8
 +      "select pos.secIdIndexed , count(distinct pos.secIdIndexed) from " + regName + " p, p.positions.values pos where  pos.secIdIndexed > 'APPL' group by pos.secIdIndexed ",//9
 +      "select  count(distinct pos.secIdIndexed) from " + regName + " p, p.positions.values pos where  pos.secIdIndexed > 'APPL'  ",//10
 +      "SELECT distinct pos.secIdIndexed FROM " + regName + " p, p.positions.values pos order by pos.secIdIndexed",//11
 +      "SELECT distinct pos.secIdIndexed FROM " + regName + " p, p.positions.values pos WHERE p.ID > 1 order by pos.secIdIndexed limit 5",//12
 + };
 +
 +  public void testReplicatedRegionNoIndex() throws CacheException {
 +    final Host host = Host.getHost(0);
 +    VM server0 = host.getVM(0);
 +    VM server1 = host.getVM(1);
 +    VM server2 = host.getVM(2);
 +    VM client = host.getVM(3);
 +    final int numberOfEntries = 10;
 +    
 +    // Start server1 and create index
 +    server0.invoke(new CacheSerializableRunnable("Create Server1") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer(false,false,false);
 +        // create a local query service
 +        QueryService localQueryService = null;
 +        try {
 +          localQueryService = getCache().getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        } 
 +        Index index = null;
 +        // create an index on statusIndexed is created
 +         try {
 +           index = localQueryService.createIndex("secIdIndex2", "pos.secIdIndexed", regName  + " p, p.positions.values pos");
 +             if(!(index instanceof RangeIndex)){
 +               fail("Range Index should have been created instead of " + index.getClass());
 +             }
 +            } catch (Exception ex) {
 +           fail("Failed to create index." + ex.getMessage());
 +         }
 +      }
 +    });
 +
 +    // Start server2
 +    server1.invoke(new CacheSerializableRunnable("Create Server2") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer(false,false, false);
 +        Region region = getRootRegion().getSubregion(regionName);
 +      }
 +    });
 +    
 +    // Start server3
 +    server2.invoke(new CacheSerializableRunnable("Create Server3") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer(false,false, false);
 +        Region region = getRootRegion().getSubregion(regionName);
 +      }
 +    });
 +
 +    // Client pool.
-     final int port0 = server0.invokeInt(PdxStringQueryDUnitTest.class, "getCacheServerPort");
-     final int port1 = server1.invokeInt(PdxStringQueryDUnitTest.class, "getCacheServerPort");
-     final int port2 = server2.invokeInt(PdxStringQueryDUnitTest.class, "getCacheServerPort");
++    final int port0 = server0.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
++    final int port1 = server1.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
++    final int port2 = server2.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
 +
 +    final String host0 = NetworkUtils.getServerHostName(server0.getHost());
 +
 +    // Create client pool.
 +    final String poolName = "testClientServerQueryPool"; 
 +    createPool(client, poolName, new String[]{host0}, new int[]{port0, port1, port2}, true);
 +
 +    // Create client region and put PortfolioPdx objects (PdxInstances)
 +    client.invoke(new CacheSerializableRunnable("Create client") {
 +      public void run2() throws CacheException {
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        ClientServerTestCase.configureConnectionPool(factory, host0, port1,-1, true, -1, -1, null);
 +        Region region = createRegion(regionName, rootRegionName,  factory.create());
 +        LogWriterUtils.getLogWriter().info("Put PortfolioPdx");
 +        for (int i=0; i<numberOfEntries; i++) {
 +          region.put("key-"+i, new PortfolioPdx(i));
 +         }
 +      }
 +    });
 +    
 +     // Execute queries from client to server and locally on client
 +    SerializableRunnable executeQueries = new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        QueryService remoteQueryService = null;
 +        QueryService localQueryService = null;
 +        SelectResults[][] rs = new SelectResults[1][2];
 +        SelectResults[] resWithoutIndexRemote = new SelectResults[queryString.length];
 +        SelectResults[] resWithIndexRemote = new SelectResults[queryString2.length];
 +        SelectResults[] resWithoutIndexLocal = new SelectResults[queryString.length];
 +        SelectResults[] resWithIndexLocal = new SelectResults[queryString2.length];
 +
 +        try {
 +          remoteQueryService = (PoolManager.find(poolName)).getQueryService();
 +          localQueryService = getCache().getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        }
 +        
 +        for (int i=0; i < queryString.length; i++){
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query on remote server:" + queryString[i]);
 +            Query query = remoteQueryService.newQuery(queryString[i]);
 +            rs[0][0] = (SelectResults)query.execute();
 +            resWithoutIndexRemote[i] = rs[0][0];
 +            LogWriterUtils.getLogWriter().info("RR remote indexType: no index  size of resultset: "+ rs[0][0].size() + " for query: " + queryString[i]);;
 +            checkForPdxString(rs[0][0].asList(), queryString[i]);
 +            
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on client:" + queryString[i]);
 +            query = localQueryService.newQuery(queryString[i]);
 +            rs[0][1] = (SelectResults)query.execute();
 +            resWithoutIndexLocal[i] = rs[0][1];
 +            LogWriterUtils.getLogWriter().info("RR  client local indexType:no index size of resultset: "+ rs[0][1].size() + " for query: " + queryString[i]);;
 +            checkForPdxString(rs[0][1].asList(), queryString[i]);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +          try{
 +            // to compare remote query results with and without index
 +            LogWriterUtils.getLogWriter().info("### Executing Query on remote server for region2:" + queryString2[i]);
 +            Query query = remoteQueryService.newQuery(queryString2[i]);
 +            resWithIndexRemote[i] = (SelectResults)query.execute();
 +            LogWriterUtils.getLogWriter().info("RR  remote region2 size of resultset: "+ resWithIndexRemote[i].size() + " for query: " + queryString2[i]);;
 +            checkForPdxString(resWithIndexRemote[i].asList(), queryString2[i]);
 +
 +           // to compare local query results with and without index
 +            LogWriterUtils.getLogWriter().info("### Executing Query on local for region2:" + queryString2[i]);
 +            query = localQueryService.newQuery(queryString2[i]);
 +            resWithIndexLocal[i] = (SelectResults)query.execute();
 +            LogWriterUtils.getLogWriter().info("RR  local region2 size of resultset: "+ resWithIndexLocal[i].size() + " for query: " + queryString2[i]);;
 +            checkForPdxString(resWithIndexLocal[i].asList(), queryString2[i]);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString2[i], e);
 +          }
 +
 +            if(i < orderByQueryIndex){
 +              // Compare local and remote query results.
 +              if (!compareResultsOfWithAndWithoutIndex(rs)){
 +               fail("Local and Remote Query Results are not matching for query :" + queryString[i]);  
 +              }
 +            }
 +            else{
 +              //compare the order of results returned 
 +              compareResultsOrder(rs, false);
 +            }
 +          
 +        }
 +        // compare remote query results with and without index
 +           for (int i=0; i < queryString.length; i++){
 +            rs[0][0] = resWithoutIndexRemote[i]; 
 +            rs[0][1] = resWithIndexRemote[i];
 +            if(i < orderByQueryIndex){
 +              if (!compareResultsOfWithAndWithoutIndex(rs)){
 +               fail("Results with and without index are not matching for query :" + queryString2[i]);  
 +              }
 +            }
 +            else{
 +              //compare the order of results returned 
 +              compareResultsOrder(rs, false);
 +            }
 +          }
 +           // compare local query results with and without index
 +          for (int i=0; i < queryString.length; i++){
 +            rs[0][0] = resWithoutIndexLocal[i]; 
 +            rs[0][1] = resWithIndexLocal[i];
 +            if(i < orderByQueryIndex){
 +              if (!compareResultsOfWithAndWithoutIndex(rs)){
 +               fail("Results with and without index are not matching for query :" + queryString2[i]);  
 +              }
 +            }
 +            else{
 +              //compare the order of results returned 
 +              compareResultsOrder(rs, false);
 +            }
 +          }
 +        
 +       }
 +    };
 +
 +    client.invoke(executeQueries);
 +
 +    // Put Non Pdx objects on server execute queries locally 
 +    server0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion().getSubregion(regionName);
 +
 +        LogWriterUtils.getLogWriter().info("Put Objects locally on server");
 +        for (int i=numberOfEntries; i<numberOfEntries*2; i++) {
 +          region.put("key-"+i, new Portfolio(i));
 +         }
 +        QueryService localQueryService = getCache().getQueryService();
 +
 +        // Query server1 locally to check if PdxString is not being returned
 +        for (int i = 0; i < queryString.length; i++) {
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on server:" + queryString[i]);
 +            SelectResults rs = (SelectResults) localQueryService.newQuery(queryString[i]).execute();
 +            LogWriterUtils.getLogWriter().info("RR server local indexType: no  size of resultset: " + rs.size() + " for query: " + queryString[i]);
 +            // The results should not be PdxString
 +            checkForPdxString(rs.asList(), queryString[i]);
 +
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +          try{
 +            SelectResults rs = (SelectResults) localQueryService.newQuery(queryString2[i]).execute();
 +            LogWriterUtils.getLogWriter().info("RR server local indexType: no size of resultset: " + rs.size() + " for query: " + queryString2[i]);
 +            // The results should not be PdxString
 +            checkForPdxString(rs.asList(), queryString2[i]);
 +          }catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString2[i], e);
 +          }
 +        }
 +       }
 +    });
 +    
 +   // test for readSerialized flag
 +    server0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        GemFireCacheImpl cache = (GemFireCacheImpl)getCache();
 +        cache.setReadSerialized(true);
 +        QueryService localQueryService = getCache().getQueryService();
 +
 +        // Query server1 locally to check if PdxString is not being returned
 +        for (int i = 0; i < queryString.length; i++) {
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on server:" + queryString[i]);
 +            SelectResults rs = (SelectResults) localQueryService.newQuery(queryString[i]).execute();
 +            LogWriterUtils.getLogWriter().info("isPR: false server local readSerializedTrue: indexType: false size of resultset: " + rs.size() + " for query: " + queryString[i]);
 +            // The results should not be PdxString
 +            checkForPdxString(rs.asList(), queryString[i]);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +        }
 +      }
 +    });
 +    
 +    // test for readSerialized flag on client
 +    client.invoke(new CacheSerializableRunnable("Create client") {
 +      public void run2() throws CacheException {
 +        GemFireCacheImpl cache = (GemFireCacheImpl)getCache();
 +        cache.setReadSerialized(true);
 +        QueryService  remoteQueryService = (PoolManager.find(poolName)).getQueryService();
 +
 +        // Query server1 remotely to check if PdxString is not being returned
 +        for (int i = 0; i < queryString.length; i++) {
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on server:" + queryString[i]);
 +            SelectResults rs = (SelectResults) remoteQueryService.newQuery(queryString[i]).execute();
 +            LogWriterUtils.getLogWriter().info("RR server remote readSerializedTrue: indexType: false size of resultset: " + rs.size() + " for query: " + queryString[i]);
 +            // The results should not be PdxString
 +            checkForPdxString(rs.asList(), queryString[i]);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +        }
 +      }
 +    });
 +    
 +    this.closeClient(server2);
 +    this.closeClient(client);
 +    this.closeClient(server1);
 +    this.closeClient(server0);
 +  }
 +
 +  public void testRepliacatedRegionCompactRangeIndex() throws CacheException {
 +    final Host host = Host.getHost(0);
 +    VM server0 = host.getVM(0);
 +    VM server1 = host.getVM(1);
 +    VM server2 = host.getVM(2);
 +    VM client = host.getVM(3);
 +    final int numberOfEntries = 10;
 +    
 +    // Start server1 and create index
 +    server0.invoke(new CacheSerializableRunnable("Create Server1") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer(false,false,false);
 +        // create a local query service
 +        QueryService localQueryService = null;
 +        try {
 +          localQueryService = getCache().getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        } 
 +        // Verify the type of index created  
 +        Index index = null;
 +         try {
 +           index = localQueryService.createIndex("statusIndex", "status", regName);
 +             if(!(index instanceof CompactRangeIndex)){
 +               fail("CompactRange Index should have been created instead of " + index.getClass());
 +             }
 +            } catch (Exception ex) {
 +           fail("Failed to create index." + ex.getMessage());
 +         }
 +      }
 +    });
 +
 +    // Start server2
 +    server1.invoke(new CacheSerializableRunnable("Create Server2") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer(false,false, false);
 +        Region region = getRootRegion().getSubregion(regionName);
 +      }
 +    });
 +    
 +    // Start server3
 +    server2.invoke(new CacheSerializableRunnable("Create Server3") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer(false,false, false);
 +        Region region = getRootRegion().getSubregion(regionName);
 +      }
 +    });
 +
 +    // Client pool.
-     final int port0 = server0.invokeInt(PdxStringQueryDUnitTest.class, "getCacheServerPort");
-     final int port1 = server1.invokeInt(PdxStringQueryDUnitTest.class, "getCacheServerPort");
-     final int port2 = server2.invokeInt(PdxStringQueryDUnitTest.class, "getCacheServerPort");
++    final int port0 = server0.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
++    final int port1 = server1.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
++    final int port2 = server2.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
 +
 +    final String host0 = NetworkUtils.getServerHostName(server0.getHost());
 +
 +    // Create client pool.
 +    final String poolName = "testClientServerQueryPool"; 
 +    createPool(client, poolName, new String[]{host0}, new int[]{port0, port1, port2}, true);
 +
 +    // Create client region and put PortfolioPdx objects (PdxInstances)
 +    client.invoke(new CacheSerializableRunnable("Create client") {
 +      public void run2() throws CacheException {
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        ClientServerTestCase.configureConnectionPool(factory, host0, port1,-1, true, -1, -1, null);
 +        Region region = createRegion(regionName, rootRegionName,  factory.create());
 +        LogWriterUtils.getLogWriter().info("Put PortfolioPdx");
 +        for (int i=0; i<numberOfEntries; i++) {
 +          region.put("key-"+i, new PortfolioPdx(i));
 +         }
 +      }
 +    });
 +    
 +    //Verify if all the index keys are PdxStrings
 +    server0.invoke(new CacheSerializableRunnable("Create Server") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion().getSubregion(regionName);
 +        QueryService localQueryService = getCache().getQueryService();
 +
 +        Index index = localQueryService.getIndex(region, "statusIndex");
 +        CloseableIterator<IndexStoreEntry> iter = ((CompactRangeIndex) index)
 +            .getIndexStorage().iterator(null);
 +        while (iter.hasNext()) {
 +          Object key = iter.next().getDeserializedKey();
 +          if (!(key instanceof PdxString)) {
 +            fail("All keys of the CompactRangeIndex should be PdxStrings and not "
 +                + key.getClass());
 +          }
 +        }
 +      }
 +    });
 + 
 +    // Execute queries from client to server and locally on client
 +    SerializableRunnable executeQueries = new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        QueryService remoteQueryService = null;
 +        QueryService localQueryService = null;
 +        SelectResults[][] rs = new SelectResults[1][2];
 +        try {
 +          remoteQueryService = (PoolManager.find(poolName)).getQueryService();
 +          localQueryService = getCache().getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        }
 +        
 +        for (int i=0; i < queryString.length; i++){
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query on remote server:" + queryString[i]);
 +            Query query = remoteQueryService.newQuery(queryString[i]);
 +            rs[0][0] = (SelectResults)query.execute();
 +            LogWriterUtils.getLogWriter().info("RR remote indexType: CompactRange size of resultset: "+ rs[0][0].size() + " for query: " + queryString[i]);;
 +            checkForPdxString(rs[0][0].asList(), queryString[i]);
 +             
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on client:" + queryString[i]);
 +            query = localQueryService.newQuery(queryString[i]);
 +            rs[0][1] = (SelectResults)query.execute();
 +            LogWriterUtils.getLogWriter().info("RR  client local indexType: CompactRange size of resultset: "+ rs[0][1].size() + " for query: " + queryString[i]);;
 +            checkForPdxString(rs[0][1].asList(), queryString[i]);
 +             
 +            if(i < orderByQueryIndex){
 +              // Compare local and remote query results.
 +              if (!compareResultsOfWithAndWithoutIndex(rs)){
 +               fail("Local and Remote Query Results are not matching for query :" + queryString[i]);  
 +              }
 +            }
 +            else{
 +              //compare the order of results returned 
 +              compareResultsOrder(rs, false);
 +            }
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +        }
 +        
 +        }
 +    };
 +
 +    client.invoke(executeQueries);
 +    
 +    // Put Non Pdx objects on server execute queries locally 
 +    server0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion().getSubregion(regionName);
 +
 +        LogWriterUtils.getLogWriter().info("Put Objects locally on server");
 +        for (int i=numberOfEntries; i<numberOfEntries*2; i++) {
 +          region.put("key-"+i, new Portfolio(i));
 +         }
 +        QueryService localQueryService = getCache().getQueryService();
 +
 +        // Query server1 locally to check if PdxString is not being returned
 +        for (int i = 0; i < queryString.length; i++) {
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on server:" + queryString[i]);
 +            SelectResults rs = (SelectResults) localQueryService.newQuery(queryString[i]).execute();
 +            LogWriterUtils.getLogWriter().info("RR server local indexType:Range  size of resultset: " + rs.size() + " for query: " + queryString[i]);
 +            // The results should not be PdxString
 +            checkForPdxString(rs.asList(), queryString[i]);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +        }
 +       }
 +    });
 +   
 +    // test for readSerialized flag
 +    server0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        GemFireCacheImpl cache = (GemFireCacheImpl)getCache();
 +        cache.setReadSerialized(true);
 +        QueryService localQueryService = getCache().getQueryService();
 +        // Query server1 locally to check if PdxString is not being returned
 +        for (int i = 0; i < queryString.length; i++) {
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on server:" + queryString[i]);
 +            SelectResults rs = (SelectResults) localQueryService.newQuery(queryString[i]).execute();
 +            LogWriterUtils.getLogWriter().info("RR server local readSerializedTrue: indexType: CompactRange size of resultset: " + rs.size() + " for query: " + queryString[i]);
 +            // The results should not be PdxString
 +            checkForPdxString(rs.asList(), queryString[i]);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +        }
 +      }
 +    });
 +    
 +    // test for readSerialized flag on client
 +    client.invoke(new CacheSerializableRunnable("Create client") {
 +      public void run2() throws CacheException {
 +        GemFireCacheImpl cache = (GemFireCacheImpl)getCache();
 +        cache.setReadSerialized(true);
 +        QueryService  remoteQueryService = (PoolManager.find(poolName)).getQueryService();
 +
 +        // Query server1 remotely to check if PdxString is not being returned
 +        for (int i = 0; i < queryString.length; i++) {
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on server:" + queryString[i]);
 +            SelectResults rs = (SelectResults) remoteQueryService.newQuery(queryString[i]).execute();
 +            LogWriterUtils.getLogWriter().info("RR server remote readSerializedTrue: indexType:CompactRange size of resultset: " + rs.size() + " for query: " + queryString[i]);
 +            // The results should not be PdxString
 +            checkForPdxString(rs.asList(), queryString[i]);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +        }
 +      }
 +    });
 +    
 +    this.closeClient(server2);
 +    this.closeClient(client);
 +    this.closeClient(server1);
 +    this.closeClient(server0);
 +  }
 +  
 +  public void testReplicatedRegionRangeIndex() throws CacheException {
 +    final Host host = Host.getHost(0);
 +    VM server0 = host.getVM(0);
 +    VM server1 = host.getVM(1);
 +    VM server2 = host.getVM(2);
 +    VM client = host.getVM(3);
 +    final int numberOfEntries = 10;
 +    // Start server1 and create index
 +    server0.invoke(new CacheSerializableRunnable("Create Server1") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer(false,false,false);
 +        // create a local query service
 +        QueryService localQueryService = null;
 +        try {
 +          localQueryService = getCache().getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        } 
 +        // Verify the type of index created  
 +        Index index = null;
 +          try {
 +           index = localQueryService.createIndex("secIdIndex", "pos.secId", regName  + " p, p.positions.values pos");
 +            if(!(index instanceof RangeIndex) ){
 +               fail("Range Index should have been created instead of "+ index.getClass());
 +             }
 +          } catch (Exception ex) {
 +             fail("Failed to create index." + ex.getMessage());
 +          }
 +       }
 +    });
 +
 +    // Start server2
 +    server1.invoke(new CacheSerializableRunnable("Create Server2") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer(false,false, false);
 +        Region region = getRootRegion().getSubregion(regionName);
 +      }
 +    });
 +    
 +    // Start server3
 +    server2.invoke(new CacheSerializableRunnable("Create Server3") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer(false,false, false);
 +        Region region = getRootRegion().getSubregion(regionName);
 +      }
 +    });
 +
 +    // Client pool.
-     final int port0 = server0.invokeInt(PdxStringQueryDUnitTest.class, "getCacheServerPort");
-     final int port1 = server1.invokeInt(PdxStringQueryDUnitTest.class, "getCacheServerPort");
-     final int port2 = server2.invokeInt(PdxStringQueryDUnitTest.class, "getCacheServerPort");
++    final int port0 = server0.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
++    final int port1 = server1.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
++    final int port2 = server2.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
 +    final String host0 = NetworkUtils.getServerHostName(server0.getHost());
 +
 +    // Create client pool.
 +    final String poolName = "testClientServerQueryPool"; 
 +    createPool(client, poolName, new String[]{host0}, new int[]{port0, port1, port2}, true);
 +
 +    // Create client region and put PortfolioPdx objects (PdxInstances)
 +    client.invoke(new CacheSerializableRunnable("Create client") {
 +      public void run2() throws CacheException {
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        ClientServerTestCase.configureConnectionPool(factory, host0, port1,-1, true, -1, -1, null);
 +        Region region = createRegion(regionName, rootRegionName,  factory.create());
 +        LogWriterUtils.getLogWriter().info("Put PortfolioPdx");
 +        for (int i=0; i<numberOfEntries; i++) {
 +          region.put("key-"+i, new PortfolioPdx(i));
 +         }
 +      }
 +    });
 +    
 +    //Verify if all the index keys are PdxStrings
 +    server0.invoke(new CacheSerializableRunnable("Create Server") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion().getSubregion(regionName);
 +        QueryService localQueryService = getCache().getQueryService();
 +          Index index = localQueryService.getIndex(region, "secIdIndex");
 +            for(Object key: ((RangeIndex)index).getValueToEntriesMap().keySet()){
 +              if(!(key instanceof PdxString)){
 +                fail("All keys of the RangeIndex should be PdxStrings and not " + key.getClass());
 +              }
 +            }
 +      }
 +    });
 + 
 +    // Execute queries from client to server and locally on client
 +    SerializableRunnable executeQueries = new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        QueryService remoteQueryService = null;
 +        QueryService localQueryService = null;
 +        SelectResults[][] rs = new SelectResults[1][2];
 +
 +        try {
 +          remoteQueryService = (PoolManager.find(poolName)).getQueryService();
 +          localQueryService = getCache().getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        }
 +        
 +        for (int i=0; i < queryString.length; i++){
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query on remote server:" + queryString[i]);
 +            Query query = remoteQueryService.newQuery(queryString[i]);
 +            rs[0][0] = (SelectResults)query.execute();
 +            LogWriterUtils.getLogWriter().info("RR remote indexType: Range size of resultset: "+ rs[0][0].size() + " for query: " + queryString[i]);;
 +            checkForPdxString(rs[0][0].asList(), queryString[i]);
 +           
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on client:" + queryString[i]);
 +            query = localQueryService.newQuery(queryString[i]);
 +            rs[0][1] = (SelectResults)query.execute();
 +            LogWriterUtils.getLogWriter().info("RR  client local indexType: Range size of resultset: "+ rs[0][1].size() + " for query: " + queryString[i]);;
 +            checkForPdxString(rs[0][1].asList(), queryString[i]);
 +            
 +            if(i < orderByQueryIndex){
 +              // Compare local and remote query results.
 +              if (!compareResultsOfWithAndWithoutIndex(rs)){
 +               fail("Local and Remote Query Results are not matching for query :" + queryString[i]);  
 +              }
 +            }
 +            else{
 +              //compare the order of results returned 
 +              compareResultsOrder(rs, false);
 +            }
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +        }
 +        }
 +    };
 +
 +    client.invoke(executeQueries);
 +    
 +    // Put Non Pdx objects on server execute queries locally 
 +    server0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion().getSubregion(regionName);
 +
 +        LogWriterUtils.getLogWriter().info("Put Objects locally on server");
 +        for (int i=numberOfEntries; i<numberOfEntries*2; i++) {
 +          region.put("key-"+i, new Portfolio(i));
 +         }
 +        QueryService localQueryService = getCache().getQueryService();
 +
 +        // Query server1 locally to check if PdxString is not being returned
 +        for (int i = 0; i < queryString.length; i++) {
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on server:" + queryString[i]);
 +            SelectResults rs = (SelectResults) localQueryService.newQuery(queryString[i]).execute();
 +            LogWriterUtils.getLogWriter().info("RR server local indexType:Range  size of resultset: " + rs.size() + " for query: " + queryString[i]);
 +            // The results should not be PdxString
 +            checkForPdxString(rs.asList(), queryString[i]);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +        }
 +       }
 +    });
 +    // test for readSerialized flag
 +    server0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        GemFireCacheImpl cache = (GemFireCacheImpl)getCache();
 +        cache.setReadSerialized(true);
 +        QueryService localQueryService = getCache().getQueryService();
 +       // Query server1 locally to check if PdxString is not being returned
 +        for (int i = 0; i < queryString.length; i++) {
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on server:" + queryString[i]);
 +            SelectResults rs = (SelectResults) localQueryService.newQuery(queryString[i]).execute();
 +            LogWriterUtils.getLogWriter().info("RR  server local readSerializedTrue: indexType: Range size of resultset: " + rs.size() + " for query: " + queryString[i]);
 +            // The results should not be PdxString
 +            checkForPdxString(rs.asList(), queryString[i]);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +        }
 +      }
 +    });
 +    // test for readSerialized flag on client
 +    client.invoke(new CacheSerializableRunnable("Create client") {
 +      public void run2() throws CacheException {
 +        GemFireCacheImpl cache = (GemFireCacheImpl)getCache();
 +        cache.setReadSerialized(true);
 +        QueryService  remoteQueryService = (PoolManager.find(poolName)).getQueryService();
 +        // Query server1 remotely to check if PdxString is not being returned
 +        for (int i = 0; i < queryString.length; i++) {
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on server:" + queryString[i]);
 +            SelectResults rs = (SelectResults) remoteQueryService.newQuery(queryString[i]).execute();
 +            LogWriterUtils.getLogWriter().info("RR server remote readSerializedTrue: indexType: Range size of resultset: " + rs.size() + " for query: " + queryString[i]);
 +            // The results should not be PdxString
 +            checkForPdxString(rs.asList(), queryString[i]);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +        }
 +      }
 +    });
 +    
 +    this.closeClient(server2);
 +    this.closeClient(client);
 +    this.closeClient(server1);
 +    this.closeClient(server0);
 +  }
 +  
 +  public void testPartitionRegionNoIndex() throws CacheException {
 +    final Host host = Host.getHost(0);
 +    VM server0 = host.getVM(0);
 +    VM server1 = host.getVM(1);
 +    VM server2 = host.getVM(2);
 +    VM client = host.getVM(3);
 +    final int numberOfEntries = 10;
 +    final boolean isPr = true;
 +    // Start server1 and create index
 +    server0.invoke(new CacheSerializableRunnable("Create Server1") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer(isPr,false,false);
 +        // create a local query service
 +        QueryService localQueryService = null;
 +        try {
 +          localQueryService = getCache().getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        } 
 +       
 +        Index index = null;
 +        // In the NO_INDEX case where no indexes are used an index on another field statusIndexed is created
 +         try {
 +           index = localQueryService.createIndex("secIdIndex", "pos.secIdIndexed", regName  + " p, p.positions.values pos");
 +           if(index instanceof PartitionedIndex){
 +               for(Object o : ((PartitionedIndex)index).getBucketIndexes()){
 +                 if(!(o instanceof RangeIndex) ){
 +                   fail("RangeIndex Index should have been created instead of "+ index.getClass());
 +                 }
 +               }
 +             }
 +             else{
 +               fail("Partitioned index expected");
 +             }
 +            } catch (Exception ex) {
 +           fail("Failed to create index." + ex.getMessage());
 +         }
 +      }
 +    });
 +
 +    // Start server2
 +    server1.invoke(new CacheSerializableRunnable("Create Server2") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer(isPr,false, false);
 +        Region region = getRootRegion().getSubregion(regionName);
 +      }
 +    });
 +    
 +    // Start server3
 +    server2.invoke(new CacheSerializableRunnable("Create Server3") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer(isPr,false, false);
 +        Region region = getRootRegion().getSubregion(regionName);
 +      }
 +    });
 +
 +    // Client pool.
-     final int port0 = server0.invokeInt(PdxStringQueryDUnitTest.class, "getCacheServerPort");
-     final int port1 = server1.invokeInt(PdxStringQueryDUnitTest.class, "getCacheServerPort");
-     final int port2 = server2.invokeInt(PdxStringQueryDUnitTest.class, "getCacheServerPort");
++    final int port0 = server0.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
++    final int port1 = server1.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
++    final int port2 = server2.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
 +
 +    final String host0 = NetworkUtils.getServerHostName(server0.getHost());
 +
 +    // Create client pool.
 +    final String poolName = "testClientServerQueryPool"; 
 +    createPool(client, poolName, new String[]{host0}, new int[]{port0, port1, port2}, true);
 +
 +    // Create client region and put PortfolioPdx objects (PdxInstances)
 +    client.invoke(new CacheSerializableRunnable("Create client") {
 +      public void run2() throws CacheException {
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        ClientServerTestCase.configureConnectionPool(factory, host0, port1,-1, true, -1, -1, null);
 +        Region region = createRegion(regionName, rootRegionName,  factory.create());
 +        LogWriterUtils.getLogWriter().info("Put PortfolioPdx");
 +        for (int i=0; i<numberOfEntries; i++) {
 +          region.put("key-"+i, new PortfolioPdx(i));
 +         }
 +      }
 +    });
 +    
 +    // Execute queries from client to server and locally on client
 +    SerializableRunnable executeQueries = new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        QueryService remoteQueryService = null;
 +        QueryService localQueryService = null;
 +        SelectResults[][] rs = new SelectResults[1][2];
 +        SelectResults[] resWithoutIndexRemote = new SelectResults[queryString.length];
 +        SelectResults[] resWithIndexRemote = new SelectResults[queryString.length];
 +        SelectResults[] resWithoutIndexLocal = new SelectResults[queryString.length];
 +        SelectResults[] resWithIndexLocal = new SelectResults[queryString.length];
 +
 +        try {
 +          remoteQueryService = (PoolManager.find(poolName)).getQueryService();
 +          localQueryService = getCache().getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        }
 +        
 +        for (int i=0; i < queryString.length; i++){
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query on remote server:" + queryString[i]);
 +            Query query = remoteQueryService.newQuery(queryString[i]);
 +            rs[0][0] = (SelectResults)query.execute();
 +            resWithoutIndexRemote[i] = rs[0][0];
 +            LogWriterUtils.getLogWriter().info("RR remote no index size of resultset: "+ rs[0][0].size() + " for query: " + queryString[i]);;
 +            checkForPdxString(rs[0][0].asList(), queryString[i]);
 +
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on client:" + queryString[i]);
 +            query = localQueryService.newQuery(queryString[i]);
 +            rs[0][1] = (SelectResults)query.execute();
 +            resWithoutIndexLocal[i] = rs[0][1];
 +            LogWriterUtils.getLogWriter().info("isPR: " + isPr+ "  client local indexType:no index size of resultset: "+ rs[0][1].size() + " for query: " + queryString[i]);;
 +            checkForPdxString(rs[0][1].asList(), queryString[i]);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +          try{  
 +            // to compare remote query results with and without index
 +            LogWriterUtils.getLogWriter().info("### Executing Query on remote server for region2:" + queryString2[i]);
 +            Query query = remoteQueryService.newQuery(queryString2[i]);
 +            resWithIndexRemote[i] = (SelectResults)query.execute();
 +            LogWriterUtils.getLogWriter().info("isPR: " + isPr+ "  remote region2 size of resultset: "+ resWithIndexRemote[i].size() + " for query: " + queryString2[i]);;
 +            checkForPdxString(resWithIndexRemote[i].asList(), queryString2[i]);
 +
 +           // to compare local query results with and without index
 +            LogWriterUtils.getLogWriter().info("### Executing Query on local for region2:" + queryString2[i]);
 +            query = localQueryService.newQuery(queryString2[i]);
 +            resWithIndexLocal[i] = (SelectResults)query.execute();
 +            LogWriterUtils.getLogWriter().info("isPR: " + isPr+ "  local region2 size of resultset: "+ resWithIndexLocal[i].size() + " for query: " + queryString2[i]);;
 +            checkForPdxString(resWithIndexLocal[i].asList(), queryString2[i]);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString2[i], e);
 +          }
 +
 +            if(i < orderByQueryIndex){
 +              // Compare local and remote query results.
 +              if (!compareResultsOfWithAndWithoutIndex(rs)){
 +                LogWriterUtils.getLogWriter().info("result0="+rs[0][0].asList());
 +                LogWriterUtils.getLogWriter().info("result1="+rs[0][1].asList());
 +               fail("Local and Remote Query Results are not matching for query :" + queryString[i]);  
 +              }
 +            }
 +            else{
 +              //compare the order of results returned 
 +              compareResultsOrder(rs, isPr);
 +            }
 +        }
 +        
 +          for (int i=0; i < queryString.length; i++){
 +            rs[0][0] = resWithoutIndexRemote[i]; 
 +            rs[0][1] = resWithIndexRemote[i];
 +            if(i < orderByQueryIndex){
 +              // Compare local and remote query results.
 +              if (!compareResultsOfWithAndWithoutIndex(rs)){
 +               fail("Results with and without index are not matching for query :" + queryString2[i]);  
 +              }
 +            }
 +            else{
 +              //compare the order of results returned 
 +              compareResultsOrder(rs, isPr);
 +            }
 +          }
 +          
 +          for (int i=0; i < queryString.length; i++){
 +            rs[0][0] = resWithoutIndexLocal[i]; 
 +            rs[0][1] = resWithIndexLocal[i];
 +            if(i < orderByQueryIndex){
 +              // Compare local and remote query results.
 +              if (!compareResultsOfWithAndWithoutIndex(rs)){
 +               fail("Results with and without index are not matching for query :" + queryString2[i]);  
 +              }
 +            }
 +            else{
 +              //compare the order of results returned 
 +              compareResultsOrder(rs, isPr);
 +            }
 +          }
 +       }
 +    };
 +    client.invoke(executeQueries);
 +
 +    // Put Non Pdx objects on server execute queries locally 
 +    server0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion().getSubregion(regionName);
 +
 +        LogWriterUtils.getLogWriter().info("Put Objects locally on server");
 +        for (int i=numberOfEntries; i<numberOfEntries*2; i++) {
 +          region.put("key-"+i, new Portfolio(i));
 +         }
 +        QueryService localQueryService = getCache().getQueryService();
 +
 +        // Query server1 locally to check if PdxString is not being returned
 +        for (int i = 0; i < queryString.length; i++) {
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on server:" + queryString[i]);
 +            SelectResults rs = (SelectResults) localQueryService.newQuery(queryString[i]).execute();
 +            LogWriterUtils.getLogWriter().info("PR server local indexType:no  size of resultset: " + rs.size() + " for query: " + queryString[i]);
 +            // The results should not be PdxString
 +            checkForPdxString(rs.asList(), queryString[i]);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +          try{
 +            SelectResults rs = (SelectResults) localQueryService.newQuery(queryString2[i]).execute();
 +            LogWriterUtils.getLogWriter().info("PR server local indexType: no size of resultset: " + rs.size() + " for query: " + queryString2[i]);
 +            // The results should not be PdxString
 +            checkForPdxString(rs.asList(), queryString2[i]);
 +          }catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString2[i], e);
 +          }
 +
 +        }
 +       }
 +    });
 +   
 +    // test for readSerialized flag
 +    server0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        GemFireCacheImpl cache = (GemFireCacheImpl)getCache();
 +        cache.setReadSerialized(true);
 +        QueryService localQueryService = getCache().getQueryService();
 +       // Query server1 locally to check if PdxString is not being returned
 +        for (int i = 0; i < queryString.length; i++) {
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on server:" + queryString[i]);
 +            SelectResults rs = (SelectResults) localQueryService.newQuery(queryString[i]).execute();
 +            LogWriterUtils.getLogWriter().info("isPR: " + isPr+ " server local readSerializedTrue: indexType: no index size of resultset: " + rs.size() + " for query: " + queryString[i]);
 +            // The results should not be PdxString
 +            checkForPdxString(rs.asList(), queryString[i]);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +        }
 +      }
 +    });
 +    
 +    // test for readSerialized flag on client
 +    client.invoke(new CacheSerializableRunnable("Create client") {
 +      public void run2() throws CacheException {
 +        GemFireCacheImpl cache = (GemFireCacheImpl)getCache();
 +        cache.setReadSerialized(true);
 +        QueryService  remoteQueryService = (PoolManager.find(poolName)).getQueryService();
 +        // Query server1 remotely to check if PdxString is not being returned
 +        for (int i = 0; i < queryString.length; i++) {
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on server:" + queryString[i]);
 +            SelectResults rs = (SelectResults) remoteQueryService.newQuery(queryString[i]).execute();
 +            LogWriterUtils.getLogWriter().info("RR server remote readSerializedTrue: indexType:no index size of resultset: " + rs.size() + " for query: " + queryString[i]);
 +            // The results should not be PdxString
 +            checkForPdxString(rs.asList(), queryString[i]);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +        }
 +      }
 +    });
 +    
 +    this.closeClient(server2);
 +    this.closeClient(client);
 +    this.closeClient(server1);
 +    this.closeClient(server0);
 +  }
 +  
 +  public void testPartitionRegionCompactRangeIndex() throws CacheException {
 +    final Host host = Host.getHost(0);
 +    VM server0 = host.getVM(0);
 +    VM server1 = host.getVM(1);
 +    VM server2 = host.getVM(2);
 +    VM client = host.getVM(3);
 +    final int numberOfEntries = 10;
 +    final boolean isPr = true;
 +    // Start server1 and create index
 +    server0.invoke(new CacheSerializableRunnable("Create Server1") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer(isPr,false,false);
 +        // create a local query service
 +        QueryService localQueryService = null;
 +        try {
 +          localQueryService = getCache().getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        } 
 +        // Verify the type of index created  
 +        Index index = null;
 +          try {
 +           index = localQueryService.createIndex("statusIndex", "status", regName);
 +             if(index instanceof PartitionedIndex){
 +               for(Object o : ((PartitionedIndex)index).getBucketIndexes()){
 +                 if(!(o instanceof CompactRangeIndex) ){
 +                   fail("CompactRangeIndex Index should have been created instead of "+ index.getClass());
 +                 }
 +               }
 +             }
 +             else{
 +               fail("Partitioned index expected");
 +             }
 +         } catch (Exception ex) {
 +           fail("Failed to create index." + ex.getMessage());
 +         }
 +       }
 +    });
 +
 +    // Start server2
 +    server1.invoke(new CacheSerializableRunnable("Create Server2") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer(isPr,false, false);
 +        Region region = getRootRegion().getSubregion(regionName);
 +      }
 +    });
 +    
 +    // Start server3
 +    server2.invoke(new CacheSerializableRunnable("Create Server3") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer(isPr,false, false);
 +        Region region = getRootRegion().getSubregion(regionName);
 +      }
 +    });
 +
 +    // Client pool.
-     final int port0 = server0.invokeInt(PdxStringQueryDUnitTest.class, "getCacheServerPort");
-     final int port1 = server1.invokeInt(PdxStringQueryDUnitTest.class, "getCacheServerPort");
-     final int port2 = server2.invokeInt(PdxStringQueryDUnitTest.class, "getCacheServerPort");
++    final int port0 = server0.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
++    final int port1 = server1.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
++    final int port2 = server2.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
 +
 +    final String host0 = NetworkUtils.getServerHostName(server0.getHost());
 +
 +    // Create client pool.
 +    final String poolName = "testClientServerQueryPool"; 
 +    createPool(client, poolName, new String[]{host0}, new int[]{port0, port1, port2}, true);
 +
 +    // Create client region and put PortfolioPdx objects (PdxInstances)
 +    client.invoke(new CacheSerializableRunnable("Create client") {
 +      public void run2() throws CacheException {
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        ClientServerTestCase.configureConnectionPool(factory, host0, port1,-1, true, -1, -1, null);
 +        Region region = createRegion(regionName, rootRegionName,  factory.create());
 +        LogWriterUtils.getLogWriter().info("Put PortfolioPdx");
 +        for (int i=0; i<numberOfEntries; i++) {
 +          region.put("key-"+i, new PortfolioPdx(i));
 +         }
 +      }
 +    });
 +    
 +    //Verify if all the index keys are PdxStrings
 +    server0.invoke(new CacheSerializableRunnable("Create Server") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion().getSubregion(regionName);
 +        QueryService localQueryService = getCache().getQueryService();
 +         Index index = localQueryService.getIndex(region, "statusIndex");
 +            if(index instanceof PartitionedIndex){
 +              for(Object o : ((PartitionedIndex)index).getBucketIndexes()){
 +                CloseableIterator<IndexStoreEntry> iter = ((CompactRangeIndex) o)
 +                    .getIndexStorage().iterator(null);
 +                while (iter.hasNext()) {
 +                  Object key = iter.next().getDeserializedKey();
 +                  if (!(key instanceof PdxString)) {
 +                    fail("All keys of the CompactRangeIndex in the Partitioned index should be PdxStrings and not "
 +                        + key.getClass());
 +                  }
 +                }
 +              }
 +            }
 +            else{
 +              fail("Partitioned index expected");
 +            }
 +      }
 +    });
 + 
 +    // Execute queries from client to server and locally on client
 +    SerializableRunnable executeQueries = new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        QueryService remoteQueryService = null;
 +        QueryService localQueryService = null;
 +        SelectResults[][] rs = new SelectResults[1][2];
 +
 +        try {
 +          remoteQueryService = (PoolManager.find(poolName)).getQueryService();
 +          localQueryService = getCache().getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        }
 +        
 +        for (int i=0; i < queryString.length; i++){
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query on remote server:" + queryString[i]);
 +            Query query = remoteQueryService.newQuery(queryString[i]);
 +            rs[0][0] = (SelectResults)query.execute();
 +            LogWriterUtils.getLogWriter().info("RR remote indexType:CompactRange size of resultset: "+ rs[0][0].size() + " for query: " + queryString[i]);;
 +            checkForPdxString(rs[0][0].asList(), queryString[i]);
 +           
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on client:" + queryString[i]);
 +            query = localQueryService.newQuery(queryString[i]);
 +            rs[0][1] = (SelectResults)query.execute();
 +            LogWriterUtils.getLogWriter().info("isPR: " + isPr+ "  client local indexType:CompactRange size of resultset: "+ rs[0][1].size() + " for query: " + queryString[i]);;
 +            checkForPdxString(rs[0][1].asList(), queryString[i]);
 + 
 +            if(i < orderByQueryIndex){
 +              // Compare local and remote query results.
 +              if (!compareResultsOfWithAndWithoutIndex(rs)){
 +               fail("Local and Remote Query Results are not matching for query :" + queryString[i]);  
 +              }
 +            }
 +            else{
 +              //compare the order of results returned 
 +              compareResultsOrder(rs, isPr);
 +            }
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +        }
 +      }       
 +     };
 +
 +    client.invoke(executeQueries);
 +    // Put Non Pdx objects on server execute queries locally 
 +    server0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion().getSubregion(regionName);
 +
 +        LogWriterUtils.getLogWriter().info("Put Objects locally on server");
 +        for (int i=numberOfEntries; i<numberOfEntries*2; i++) {
 +          region.put("key-"+i, new Portfolio(i));
 +         }
 +        QueryService localQueryService = getCache().getQueryService();
 +
 +        // Query server1 locally to check if PdxString is not being returned
 +        for (int i = 0; i < queryString.length; i++) {
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on server:" + queryString[i]);
 +            SelectResults rs = (SelectResults) localQueryService.newQuery(queryString[i]).execute();
 +            LogWriterUtils.getLogWriter().info("RR server local indexType:Range  size of resultset: " + rs.size() + " for query: " + queryString[i]);
 +            // The results should not be PdxString
 +            checkForPdxString(rs.asList(), queryString[i]);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +        }
 +       }
 +    });
 +    
 +    // test for readSerialized flag
 +    server0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        GemFireCacheImpl cache = (GemFireCacheImpl)getCache();
 +        cache.setReadSerialized(true);
 +        QueryService localQueryService = getCache().getQueryService();
 +
 +        // Query server1 locally to check if PdxString is not being returned
 +        for (int i = 0; i < queryString.length; i++) {
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on server:" + queryString[i]);
 +            SelectResults rs = (SelectResults) localQueryService.newQuery(queryString[i]).execute();
 +            LogWriterUtils.getLogWriter().info("isPR: " + isPr+ " server local readSerializedTrue: indexType:CompactRange size of resultset: " + rs.size() + " for query: " + queryString[i]);
 +            // The results should not be PdxString
 +            checkForPdxString(rs.asList(), queryString[i]);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +        }
 +      }
 +    });
 +    
 +    // test for readSerialized flag on client
 +    client.invoke(new CacheSerializableRunnable("Create client") {
 +      public void run2() throws CacheException {
 +        GemFireCacheImpl cache = (GemFireCacheImpl)getCache();
 +        cache.setReadSerialized(true);
 +        QueryService  remoteQueryService = (PoolManager.find(poolName)).getQueryService();
 +
 +        // Query server1 remotely to check if PdxString is not being returned
 +        for (int i = 0; i < queryString.length; i++) {
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on server:" + queryString[i]);
 +            SelectResults rs = (SelectResults) remoteQueryService.newQuery(queryString[i]).execute();
 +            LogWriterUtils.getLogWriter().info("RR server remote readSerializedTrue: indexType: indexType:CompactRange size of resultset: " + rs.size() + " for query: " + queryString[i]);
 +            // The results should not be PdxString
 +            checkForPdxString(rs.asList(), queryString[i]);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +        }
 +      }
 +    });
 +    
 +    this.closeClient(server2);
 +    this.closeClient(client);
 +    this.closeClient(server1);
 +    this.closeClient(server0);
 +  }
 +  
 +  public void testPartitionRegionRangeIndex() throws CacheException {
 +    final Host host = Host.getHost(0);
 +    VM server0 = host.getVM(0);
 +    VM server1 = host.getVM(1);
 +    VM server2 = host.getVM(2);
 +    VM client = host.getVM(3);
 +    final int numberOfEntries = 10;
 +    final boolean isPr = true;
 +    // Start server1 and create index
 +    server0.invoke(new CacheSerializableRunnable("Create Server1") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer(isPr,false,false);
 +        // create a local query service
 +        QueryService localQueryService = null;
 +        try {
 +          localQueryService = getCache().getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        } 
 +        // Verify the type of index created  
 +        Index index = null;
 +          try {
 +           index = localQueryService.createIndex("secIdIndex", "pos.secId", regName  + " p, p.positions.values pos");
 +            if(index instanceof PartitionedIndex){
 +               for(Object o : ((PartitionedIndex)index).getBucketIndexes()){
 +                 if(!(o instanceof RangeIndex) ){
 +                   fail("Range Index should have been created instead of "+ index.getClass());
 +                 }
 +               }
 +             }
 +             else{
 +               fail("Partitioned index expected");
 +             }
 +          } catch (Exception ex) {
 +             fail("Failed to create index." + ex.getMessage());
 +          }
 +        }
 +  });
 +
 +    // Start server2
 +    server1.invoke(new CacheSerializableRunnable("Create Server2") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer(isPr,false, false);
 +        Region region = getRootRegion().getSubregion(regionName);
 +      }
 +    });
 +    
 +    // Start server3
 +    server2.invoke(new CacheSerializableRunnable("Create Server3") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer(isPr,false, false);
 +        Region region = getRootRegion().getSubregion(regionName);
 +      }
 +    });
 +
 +    // Client pool.
-     final int port0 = server0.invokeInt(PdxStringQueryDUnitTest.class, "getCacheServerPort");
-     final int port1 = server1.invokeInt(PdxStringQueryDUnitTest.class, "getCacheServerPort");
-     final int port2 = server2.invokeInt(PdxStringQueryDUnitTest.class, "getCacheServerPort");
++    final int port0 = server0.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
++    final int port1 = server1.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
++    final int port2 = server2.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
 +
 +    final String host0 = NetworkUtils.getServerHostName(server0.getHost());
 +
 +    // Create client pool.
 +    final String poolName = "testClientServerQueryPool"; 
 +    createPool(client, poolName, new String[]{host0}, new int[]{port0, port1, port2}, true);
 +
 +    // Create client region and put PortfolioPdx objects (PdxInstances)
 +    client.invoke(new CacheSerializableRunnable("Create client") {
 +      public void run2() throws CacheException {
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        ClientServerTestCase.configureConnectionPool(factory, host0, port1,-1, true, -1, -1, null);
 +        Region region = createRegion(regionName, rootRegionName,  factory.create());
 +        LogWriterUtils.getLogWriter().info("Put PortfolioPdx");
 +        for (int i=0; i<numberOfEntries; i++) {
 +          region.put("key-"+i, new PortfolioPdx(i));
 +         }
 +      }
 +    });
 +    
 +    //Verify if all the index keys are PdxStrings
 +    server0.invoke(new CacheSerializableRunnable("Create Server") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion().getSubregion(regionName);
 +        QueryService localQueryService = getCache().getQueryService();
 +        
 +          Index index = localQueryService.getIndex(region, "secIdIndex");
 +             if(index instanceof PartitionedIndex){
 +              for(Object o : ((PartitionedIndex)index).getBucketIndexes()){
 +                for(Object key: ((RangeIndex)o).getValueToEntriesMap().keySet()){
 +                  if(!(key instanceof PdxString)){
 +                    fail("All keys of the RangeIndex in the Partitioned index should be PdxStrings and not " + key.getClass());
 +                  }
 +                }
 +              }
 +            }
 +            else{
 +              fail("Partitioned index expected");
 +            }
 +      }
 +    });
 + 
 +    // Execute queries from client to server and locally on client
 +    SerializableRunnable executeQueries = new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        QueryService remoteQueryService = null;
 +        QueryService localQueryService = null;
 +        SelectResults[][] rs = new SelectResults[1][2];
 + 
 +        try {
 +          remoteQueryService = (PoolManager.find(poolName)).getQueryService();
 +          localQueryService = getCache().getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        }
 +        
 +        for (int i=0; i < queryString.length; i++){
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query on remote server:" + queryString[i]);
 +            Query query = remoteQueryService.newQuery(queryString[i]);
 +            rs[0][0] = (SelectResults)query.execute();
 +            LogWriterUtils.getLogWriter().info("RR remote indexType: Range size of resultset: "+ rs[0][0].size() + " for query: " + queryString[i]);;
 +            checkForPdxString(rs[0][0].asList(), queryString[i]);
 +                      
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on client:" + queryString[i]);
 +            query = localQueryService.newQuery(queryString[i]);
 +            rs[0][1] = (SelectResults)query.execute();
 +            LogWriterUtils.getLogWriter().info("isPR: " + isPr+ "  client local indexType: Range size of resultset: "+ rs[0][1].size() + " for query: " + queryString[i]);;
 +            checkForPdxString(rs[0][1].asList(), queryString[i]);
 +                   
 +            if(i < orderByQueryIndex){
 +              // Compare local and remote query results.
 +              if (!compareResultsOfWithAndWithoutIndex(rs)){
 +                LogWriterUtils.getLogWriter().info("result0="+rs[0][0].asList());
 +                LogWriterUtils.getLogWriter().info("result1="+rs[0][1].asList());
 +               fail("Local and Remote Query Results are not matching for query :" + queryString[i]);  
 +              }
 +            }
 +            else{
 +              //compare the order of results returned 
 +              compareResultsOrder(rs, isPr);
 +            }
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +        }
 +       }
 +    };
 +
 +    client.invoke(executeQueries);
 +
 +    // Put Non Pdx objects on server execute queries locally 
 +    server0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion().getSubregion(regionName);
 +
 +        LogWriterUtils.getLogWriter().info("Put Objects locally on server");
 +        for (int i=numberOfEntries; i<numberOfEntries*2; i++) {
 +          region.put("key-"+i, new Portfolio(i));
 +         }
 +        QueryService localQueryService = getCache().getQueryService();
 +
 +        // Query server1 locally to check if PdxString is not being returned
 +        for (int i = 0; i < queryString.length; i++) {
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on server:" + queryString[i]);
 +            SelectResults rs = (SelectResults) localQueryService.newQuery(queryString[i]).execute();
 +            LogWriterUtils.getLogWriter().info("RR server local indexType:Range  size of resultset: " + rs.size() + " for query: " + queryString[i]);
 +            // The results should not be PdxString
 +            checkForPdxString(rs.asList(), queryString[i]);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +        }
 +       }
 +    });
 +    
 +    // test for readSerialized flag
 +    server0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
 +      public void run2() throws CacheException {
 +        GemFireCacheImpl cache = (GemFireCacheImpl)getCache();
 +        cache.setReadSerialized(true);
 +        QueryService localQueryService = getCache().getQueryService();
 +
 +        // Query server1 locally to check if PdxString is not being returned
 +        for (int i = 0; i < queryString.length; i++) {
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on server:" + queryString[i]);
 +            SelectResults rs = (SelectResults) localQueryService.newQuery(queryString[i]).execute();
 +            LogWriterUtils.getLogWriter().info("isPR: " + isPr+ " server local readSerializedTrue: indexType: Range size of resultset: " + rs.size() + " for query: " + queryString[i]);
 +            // The results should not be PdxString
 +            checkForPdxString(rs.asList(), queryString[i]);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +        }
 +      }
 +    });
 +    
 +    // test for readSerialized flag on client
 +    client.invoke(new CacheSerializableRunnable("Create client") {
 +      public void run2() throws CacheException {
 +        GemFireCacheImpl cache = (GemFireCacheImpl)getCache();
 +        cache.setReadSerialized(true);
 +        QueryService  remoteQueryService = (PoolManager.find(poolName)).getQueryService();
 +
 +        // Query server1 remotely to check if PdxString is not being returned
 +        for (int i = 0; i < queryString.length; i++) {
 +          try {
 +            LogWriterUtils.getLogWriter().info("### Executing Query locally on server:" + queryString[i]);
 +            SelectResults rs = (SelectResults) remoteQueryService.newQuery(queryString[i]).execute();
 +            LogWriterUtils.getLogWriter().info("RR server remote readSerializedTrue: indexType: Range size of resultset: " + rs.size() + " for query: " + queryString[i]);
 +            // The results should not be PdxString
 +            checkForPdxString(rs.asList(), queryString[i]);
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + queryString[i], e);
 +          }
 +        }
 +      }
 +    });
 +    
 +    this.closeClient(server2);
 +    this.closeClient(client);
 +    this.closeClient(server1);
 +    this.closeClient(server0);
 +  }
 +
 +  public void testNullPdxString() throws CacheException {
 +    final Host host = Host.getHost(0);
 +    VM server0 = host.getVM(0);
 +    VM server1 = host.getVM(1);
 +    VM server2 = host.getVM(2);
 +    VM client = host.getVM(3);
 +    final int numberOfEntries = 10;
 +    final boolean isPr = true;
 +    // Start server1 and create index
 +    server0.invoke(new CacheSerializableRunnable("Create Server1") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer(isPr,false,false);
 +        // create a local query service
 +        QueryService localQueryService = null;
 +        try {
 +          localQueryService = getCache().getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        } 
 +        // Verify the type of index created  
 +        Index index = null;
 +          try {
 +           index = localQueryService.createIndex("statusIndex", "status", regName);
 +             if(index instanceof PartitionedIndex){
 +               for(Object o : ((PartitionedIndex)index).getBucketIndexes()){
 +                 if(!(o instanceof CompactRangeIndex) ){
 +                   fail("CompactRangeIndex Index should have been created instead of "+ index.getClass());
 +                 }
 +               }
 +             }
 +             else{
 +               fail("Partitioned index expected");
 +             }
 +         } catch (Exception ex) {
 +           fail("Failed to create index." + ex.getMessage());
 +         }
 +       }
 +    });
 +
 +    // Start server2
 +    server1.invoke(new CacheSerializableRunnable("Create Server2") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer(isPr,false, false);
 +        Region region = getRootRegion().getSubregion(regionName);
 +      }
 +    });
 +    
 +    // Start server3
 +    server2.invoke(new CacheSerializableRunnable("Create Server3") {
 +      public void run2() throws CacheException {
 +        configAndStartBridgeServer(isPr,false, false);
 +        Region region = getRootRegion().getSubregion(regionName);
 +      }
 +    });
 +
 +    // Client pool.
-     final int port0 = server0.invokeInt(PdxStringQueryDUnitTest.class, "getCacheServerPort");
-     final int port1 = server1.invokeInt(PdxStringQueryDUnitTest.class, "getCacheServerPort");
-     final int port2 = server2.invokeInt(PdxStringQueryDUnitTest.class, "getCacheServerPort");
++    final int port0 = server0.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
++    final int port1 = server1.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
++    final int port2 = server2.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
 +
 +    final String host0 = NetworkUtils.getServerHostName(server0.getHost());
 +
 +    // Create client pool.
 +    final String poolName = "testClientServerQueryPool"; 
 +    createPool(client, poolName, new String[]{host0}, new int[]{port0, port1, port2}, true);
 +
 +    // Create client region and put PortfolioPdx objects (PdxInstances)
 +    client.invoke(new CacheSerializableRunnable("Create client") {
 +      public void run2() throws CacheException {
 +        AttributesFactory factory = new AttributesFactory();
 +        factory.setScope(Scope.LOCAL);
 +        ClientServerTestCase.configureConnectionPool(factory, host0, port1,-1, true, -1, -1, null);
 +        Region region = createRegion(regionName, rootRegionName,  factory.create());
 +      
 +        LogWriterUtils.getLogWriter().info("Put PortfolioPdx");
 +        // Put some PortfolioPdx objects with null Status and secIds 
 +        for (int i=0; i<numberOfEntries*2; i++) {
 +          PortfolioPdx portfolioPdx = new PortfolioPdx(i);
 +          portfolioPdx.status = null; // this will create NULL PdxStrings
 +          portfolioPdx.positions = new HashMap();
 +          portfolioPdx.positions.put(null, new PositionPdx(null, PositionPdx.cnt * 1000));
 +          region.put("key-"+i, portfolioPdx);
 +         }
 +        // Put some PortfolioPdx with non null status to reproduce bug#45351
 +        for (int i=0; i<numberOfEntries; i++) {
 +          PortfolioPdx portfolioPdx = new PortfolioPdx(i);
 +          region.put("key-"+i, portfolioPdx);
 +         }
 +      }
 +    });
 +    
 +    //Verify if all the index keys are PdxStrings
 +    server0.invoke(new CacheSerializableRunnable("Create Server") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion().getSubregion(regionName);
 +        QueryService localQueryService = getCache().getQueryService();
 +         Index index = localQueryService.getIndex(region, "statusIndex");
 +             if(index instanceof PartitionedIndex){
 +              for(Object o : ((PartitionedIndex)index).getBucketIndexes()){
 +                CloseableIterator<IndexStoreEntry> iter = ((CompactRangeIndex) o).getIndexStorage().iterator(null);
 +                while (iter.hasNext()) {
 +                  Object key = iter.next().getDeserializedKey();
 +                  if (!(key instanceof PdxString) && !(key == IndexManager.NULL)) {
 +                    fail("All keys of the CompactRangeIndex in the Partitioned index should be PdxStrings and not "
 +                        + key.getClass());
 +                  }
 +                }
 +              }
 +            }
 +            else{
 +              fail("Partitioned index expected");
 +            }
 +       }
 +    });
 + 
 +    // Execute queries from client to server and locally on client
 +    client.invoke( new CacheSerializableRunnable("Execute queries") {
 +      public void run2() throws CacheException {
 +        QueryService remoteQueryService = null;
 +        QueryService localQueryService = null;
 +        SelectResults[][] rs = new SelectResults[1][2];
 +
 +        try {
 +          remoteQueryService = (PoolManager.find(poolName)).getQueryService();
 +          localQueryService = getCache().getQueryService();
 +        } catch (Exception e) {
 +          Assert.fail("Failed to get QueryService.", e);
 +        }
 +        
 +        // Querying the fields with null values
 +        String[] qs = {"SELECT pos.secId FROM " + regName + "  p, p.positions.values pos where p.status = null",
 +                      "SELECT p.pkid FROM " + regName + "  p, p.positions.values pos where pos.secId = null"};
 +        
 +        for(int i = 0; i <2; i++){
 +          try {
 +            Query query = remoteQueryService.newQuery(qs[i]);
 +            SelectResults res = (SelectResults)query.execute();
 +            LogWriterUtils.getLogWriter().info("PR NULL Pdxstring test size of resultset: "+ res.size() + " for query: " + qs[i]);;
 +            if(i == 0){
 +              for(Object o : res){
 +                if(o != null){
 +                  fail("Query : " + qs[i] + " should have returned null and not " + o);
 +                }
 +              }
 +            }else{
 +              checkForPdxString(res.asList(), qs[i]);
 +            }
 +          } catch (Exception e) {
 +            Assert.fail("Failed executing " + qs[i], e);
 +          }
 +        }
 +      }
 +     });
 + 
 +    this.closeClient(server2);
 +    this.closeClient(client);
 +    this.closeClient(server1);
 +    this.closeClient(server0);
 +  }
 +  
 + 
 +    private void compareResultsOrder(SelectResults[][] r, boolean isPr){
 +    for (int j = 0; j < r.length; j++) {
 +      Object []r1 = (r[j][0]).toArray();
 +      Object []r2 = (r[j][1]).toArray();
 +      if(r1.length != r2.length){
 +        fail("Size of results not equal: " + r1.length + " vs " + r2.length);
 +      }
 +      for (int i = 0, k=0; i < r1.length && k < r2.length; i++,k++) {
 +        System.out.println("r1: " + r1[i] +  " r2: " + r2[k]);
 +        if(!r1[i].equals(r2[k])){
 +          fail("Order not equal: " + r1[i] + " : " +r2[k] + " isPR: " + isPr );
 +        }
 +      }
 +    }
 +  }
 +  
 +  private void checkForPdxString(List results, String query) {
 +    boolean isGroupByQuery = false;
 +    for (int i : groupByQueryIndex) {
 +      if (query.equals(queryString[i]) || query.equals(queryString2[i])) {
 +        isGroupByQuery = true;
 +        break;
 +      }
 +    }
 +    for (Object o : results) {
 +      if (o instanceof Struct) {
 +        if (!isGroupByQuery) {
 +          Object o1 = ((Struct) o).getFieldValues()[0];
 +          Object o2 = ((Struct) o).getFieldValues()[1];
 +          if (!(o1 instanceof String)) {
 +            fail("Returned instance of " + o1.getClass()
 +                + " instead of String for query: " + query);
 +          }
 +
 +          if (!(o2 instanceof String)) {
 +            fail("Returned instance of " + o2.getClass()
 +                + " instead of String for query: " + query);
 +          }
 +        }
 +      } else {
 +        if (!isGroupByQuery) {
 +          if (!(o instanceof String)) {
 +            fail("Returned instance of " + o.getClass()
 +                + " instead of String for query: " + query);
 +          }
 +        }
 +      }
 +    }
 +  }
 +
 +  public boolean compareResultsOfWithAndWithoutIndex(SelectResults[][] r ) { 
 +    boolean ok = true; 
 +    Set set1 = null; 
 +    Set set2 = null; 
 +    Iterator itert1 = null; 
 +    Iterator itert2 = null; 
 +    ObjectType type1, type2; 
 +    outer:  for (int j = 0; j < r.length; j++) { 
 +      CollectionType collType1 = r[j][0].getCollectionType(); 
 +      CollectionType collType2 = r[j][1].getCollectionType(); 
 +      type1 = collType1.getElementType(); 
 +      type2 = collType2.getElementType(); 
 +    
 +        if (r[j][0].size() == r[j][1].size()) { 
 +        System.out.println("Both SelectResults are of Same

<TRUNCATED>

[44/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/distributed/LocatorLauncher.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/distributed/LocatorLauncher.java
index d13dae5,0000000..b0be89f
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/distributed/LocatorLauncher.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/distributed/LocatorLauncher.java
@@@ -1,2046 -1,0 +1,2051 @@@
 +/*
 + * 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.distributed;
 +
 +import java.io.File;
 +import java.io.FileNotFoundException;
 +import java.io.IOException;
 +import java.lang.management.ManagementFactory;
 +import java.net.ConnectException;
 +import java.net.InetAddress;
 +import java.net.UnknownHostException;
 +import java.util.Arrays;
 +import java.util.Collections;
 +import java.util.HashMap;
 +import java.util.List;
 +import java.util.Map;
 +import java.util.Properties;
 +import java.util.TreeMap;
 +import java.util.concurrent.TimeUnit;
 +import java.util.concurrent.TimeoutException;
 +import java.util.concurrent.atomic.AtomicBoolean;
 +import java.util.concurrent.atomic.AtomicReference;
 +import java.util.logging.Level;
 +
 +import javax.management.MalformedObjectNameException;
 +import javax.management.ObjectName;
 +
 +import com.gemstone.gemfire.cache.client.internal.locator.LocatorStatusResponse;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 +import com.gemstone.gemfire.distributed.internal.InternalLocator;
 +import com.gemstone.gemfire.internal.DistributionLocator;
 +import com.gemstone.gemfire.internal.GemFireVersion;
 +import com.gemstone.gemfire.internal.SocketCreator;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.lang.ObjectUtils;
 +import com.gemstone.gemfire.internal.lang.StringUtils;
 +import com.gemstone.gemfire.internal.lang.SystemUtils;
 +import com.gemstone.gemfire.internal.process.ConnectionFailedException;
 +import com.gemstone.gemfire.internal.process.ControlNotificationHandler;
 +import com.gemstone.gemfire.internal.process.ControllableProcess;
 +import com.gemstone.gemfire.internal.process.FileAlreadyExistsException;
 +import com.gemstone.gemfire.internal.process.MBeanInvocationFailedException;
 +import com.gemstone.gemfire.internal.process.PidUnavailableException;
 +import com.gemstone.gemfire.internal.process.ProcessController;
 +import com.gemstone.gemfire.internal.process.ProcessControllerFactory;
 +import com.gemstone.gemfire.internal.process.ProcessControllerParameters;
 +import com.gemstone.gemfire.internal.process.ProcessLauncherContext;
 +import com.gemstone.gemfire.internal.process.ProcessType;
 +import com.gemstone.gemfire.internal.process.ProcessUtils;
 +import com.gemstone.gemfire.internal.process.StartupStatusListener;
 +import com.gemstone.gemfire.internal.process.UnableToControlProcessException;
 +import com.gemstone.gemfire.internal.util.IOUtils;
 +import com.gemstone.gemfire.lang.AttachAPINotFoundException;
 +import com.gemstone.gemfire.management.internal.cli.json.GfJsonArray;
 +import com.gemstone.gemfire.management.internal.cli.json.GfJsonException;
 +import com.gemstone.gemfire.management.internal.cli.json.GfJsonObject;
 +import joptsimple.OptionException;
 +import joptsimple.OptionParser;
 +import joptsimple.OptionSet;
 +
 +/**
 + * The LocatorLauncher class is a launcher for a GemFire Locator.
 + * 
 + * @author John Blum
 + * @author Kirk Lund
 + * @see com.gemstone.gemfire.distributed.AbstractLauncher
 + * @see com.gemstone.gemfire.distributed.ServerLauncher
 + * @since 7.0
 + */
 +@SuppressWarnings({ "unused" })
 +public final class LocatorLauncher extends AbstractLauncher<String> {
 +
 +  /**
 +   * @deprecated This is specific to the internal implementation and may go away in a future release.
 +   */
 +  public static final Integer DEFAULT_LOCATOR_PORT = getDefaultLocatorPort();
 +
 +  private static final Boolean DEFAULT_LOAD_SHARED_CONFIG_FROM_DIR = Boolean.FALSE;
 +
 +  private static final Map<String, String> helpMap = new HashMap<>();
 +
 +  static {
 +    helpMap.put("launcher", LocalizedStrings.LocatorLauncher_LOCATOR_LAUNCHER_HELP.toLocalizedString());
 +    helpMap.put(Command.START.getName(), LocalizedStrings.LocatorLauncher_START_LOCATOR_HELP.toLocalizedString(String.valueOf(
 +      getDefaultLocatorPort())));
 +    helpMap.put(Command.STATUS.getName(), LocalizedStrings.LocatorLauncher_STATUS_LOCATOR_HELP.toLocalizedString());
 +    helpMap.put(Command.STOP.getName(),LocalizedStrings.LocatorLauncher_STOP_LOCATOR_HELP.toLocalizedString());
 +    helpMap.put(Command.VERSION.getName(), LocalizedStrings.LocatorLauncher_VERSION_LOCATOR_HELP.toLocalizedString());
 +    helpMap.put("bind-address", LocalizedStrings.LocatorLauncher_LOCATOR_BIND_ADDRESS_HELP.toLocalizedString());
 +    helpMap.put("debug",LocalizedStrings.LocatorLauncher_LOCATOR_DEBUG_HELP.toLocalizedString());
 +    helpMap.put("dir",LocalizedStrings.LocatorLauncher_LOCATOR_DIR_HELP.toLocalizedString());
 +    helpMap.put("force", LocalizedStrings.LocatorLauncher_LOCATOR_FORCE_HELP.toLocalizedString());
 +    helpMap.put("help", LocalizedStrings.SystemAdmin_CAUSES_GEMFIRE_TO_PRINT_OUT_INFORMATION_INSTEAD_OF_PERFORMING_THE_COMMAND_THIS_OPTION_IS_SUPPORTED_BY_ALL_COMMANDS.toLocalizedString());
 +    helpMap.put("hostname-for-clients", LocalizedStrings.LocatorLauncher_LOCATOR_HOSTNAME_FOR_CLIENTS_HELP.toLocalizedString());
 +    helpMap.put("member", LocalizedStrings.LocatorLauncher_LOCATOR_MEMBER_HELP.toLocalizedString());
 +    helpMap.put("pid", LocalizedStrings.LocatorLauncher_LOCATOR_PID_HELP.toLocalizedString());
 +    helpMap.put("port", LocalizedStrings.LocatorLauncher_LOCATOR_PORT_HELP.toLocalizedString(String.valueOf(
 +      getDefaultLocatorPort())));
 +    helpMap.put("redirect-output", LocalizedStrings.LocatorLauncher_LOCATOR_REDIRECT_OUTPUT_HELP.toLocalizedString());
 +  }
 +
 +  private static final Map<Command, String> usageMap = new TreeMap<>();
 +
 +  static {
 +    usageMap.put(Command.START, "start <member-name> [--bind-address=<IP-address>] [--hostname-for-clients=<IP-address>] [--port=<port>] [--dir=<Locator-working-directory>] [--force] [--debug] [--help]");
 +    usageMap.put(Command.STATUS, "status [--bind-address=<IP-address>] [--port=<port>] [--member=<member-ID/Name>] [--pid=<process-ID>] [--dir=<Locator-working-directory>] [--debug] [--help]");
 +    usageMap.put(Command.STOP, "stop [--member=<member-ID/Name>] [--pid=<process-ID>] [--dir=<Locator-working-directory>] [--debug] [--help]");
 +    usageMap.put(Command.VERSION, "version");
 +  }
 +
 +  /**
 +   * @deprecated This is specific to the internal implementation and may go away in a future release.
 +   */
 +  public static final boolean DEFAULT_ENABLE_PEER_LOCATION = true;
 +  
 +  /**
 +   * @deprecated This is specific to the internal implementation and may go away in a future release.
 +   */
 +  public static final boolean DEFAULT_ENABLE_SERVER_LOCATION = true;
 +  
 +  /**
 +   * @deprecated This is specific to the internal implementation and may go away in a future release.
 +   */
 +  public static final String DEFAULT_LOCATOR_PID_FILE = "vf.gf.locator.pid";
 +
 +  private static final String DEFAULT_LOCATOR_LOG_EXT = ".log";
 +  private static final String DEFAULT_LOCATOR_LOG_NAME = "locator";
 +  private static final String LOCATOR_SERVICE_NAME = "Locator";
 +
 +  private static final AtomicReference<LocatorLauncher> INSTANCE = new AtomicReference<>();
 +
 +  //private volatile transient boolean debug;
 +
 +  private final transient ControlNotificationHandler controlHandler;
 +  
 +  private final AtomicBoolean starting = new AtomicBoolean(false);
 +
 +  private final boolean force;
 +  private final boolean help;
 +  private final boolean redirectOutput;
 +  
 +  private final Command command;
 +
 +  private final boolean bindAddressSpecified;
 +  private final boolean portSpecified;
 +  private final boolean workingDirectorySpecified;
 +
 +  private final InetAddress bindAddress;
 +
 +  private final Integer pid;
 +  private final Integer port;
 +
 +  private volatile transient InternalLocator locator;
 +
 +  private final Properties distributedSystemProperties;
 +
 +  private final String hostnameForClients;
 +  private final String memberName;
 +  private final String workingDirectory;
 +
 +  // NOTE in addition to debug and locator, the other shared, mutable state
 +  private volatile transient String statusMessage;
 +  
 +  private volatile transient ControllableProcess process;
 +
 +  private final transient LocatorControllerParameters controllerParameters;
 +  
 +  /**
 +   * Launches a GemFire Locator from the command-line configured with the given arguments.
 +   * 
 +   * @param args the command-line arguments used to configure the GemFire Locator at runtime.
 +   */
 +  public static void main(final String... args) {
 +    try {
 +      new Builder(args).build().run();
 +    }
 +    catch (AttachAPINotFoundException e) {
 +      System.err.println(e.getMessage());
 +    }
 +  }
 +
 +  private static Integer getDefaultLocatorPort() {
 +    return Integer.getInteger(DistributionLocator.TEST_OVERRIDE_DEFAULT_PORT_PROPERTY,
 +      DistributionLocator.DEFAULT_LOCATOR_PORT);
 +  }
 +
 +  /**
 +   * Gets the instance of the LocatorLauncher used to launch the GemFire Locator, or null if this VM does not
 +   * have an instance of LocatorLauncher indicating no GemFire Locator is running.
 +   * 
 +   * @return the instance of LocatorLauncher used to launcher a GemFire Locator in this VM.
 +   */
 +  public static LocatorLauncher getInstance() {
 +    return INSTANCE.get();
 +  }
 +
 +  /**
 +   * Gets the LocatorState for this process or null if this process was not launched using this VM's
 +   * LocatorLauncher reference.
 +   * 
 +   * @return the LocatorState for this process or null.
 +   */
 +  public static LocatorState getLocatorState() {
 +    return (getInstance() != null ? getInstance().status() : null);
 +  }
 +
 +  /**
 +   * Private constructor used to properly construct an immutable instance of the LocatorLauncher using a Builder.
 +   * The Builder is used to configure a LocatorLauncher instance.  The Builder can process user input from the
 +   * command-line or be used to properly construct an instance of the LocatorLauncher programmatically using the API.
 +   * 
 +   * @param builder an instance of LocatorLauncher.Builder for configuring and constructing an instance of the
 +   * LocatorLauncher.
 +   * @see com.gemstone.gemfire.distributed.LocatorLauncher.Builder
 +   */
 +  private LocatorLauncher(final Builder builder) {
 +    this.command = builder.getCommand();
 +    setDebug(Boolean.TRUE.equals(builder.getDebug()));
 +    this.force = Boolean.TRUE.equals(builder.getForce());
 +    this.help = Boolean.TRUE.equals(builder.getHelp());
 +    this.bindAddressSpecified = builder.isBindAddressSpecified();
 +    this.bindAddress = builder.getBindAddress();
 +    this.distributedSystemProperties = builder.getDistributedSystemProperties();
 +    this.hostnameForClients = builder.getHostnameForClients();
 +    this.memberName = builder.getMemberName();
 +    // TODO:KIRK: set ThreadLocal for LogService with getLogFile or getLogFileName
 +    this.pid = builder.getPid();
 +    this.portSpecified = builder.isPortSpecified();
 +    this.port = builder.getPort();
 +    this.redirectOutput = Boolean.TRUE.equals(builder.getRedirectOutput());
 +    this.workingDirectorySpecified = builder.isWorkingDirectorySpecified();
 +    this.workingDirectory = builder.getWorkingDirectory();
 +    this.controllerParameters = new LocatorControllerParameters();
 +    this.controlHandler = new ControlNotificationHandler() {
 +      @Override
 +      public void handleStop() {
 +        if (isStoppable()) {
 +          stopInProcess();
 +        }
 +      }
 +      @Override
 +      public ServiceState<?> handleStatus() {
 +        return statusInProcess();
 +      }
 +    };
 +  }
 +
 +  /**
 +   * Gets the reference to the Locator object representing the running GemFire Locator.
 +   * 
 +   * @return a reference to the Locator.
 +   */
 +  final InternalLocator getLocator() {
 +    return this.locator;
 +  }
 +
 +  /**
 +   * Gets an identifier that uniquely identifies and represents the Locator associated with this launcher.
 +   * 
 +   * @return a String value identifier to uniquely identify the Locator and it's launcher.
 +   * @see #getBindAddressAsString()
 +   * @see #getPortAsString()
 +   */
 +  public final String getId() {
 +    return LocatorState.getBindAddressAsString(this).concat("[").concat(LocatorState.getPortAsString(this)).concat("]");
 +  }
 +
 +  /**
 +   * Get the Locator launcher command used to invoke the Locator.
 +   * 
 +   * @return the Locator launcher command used to invoke the Locator.
 +   * @see com.gemstone.gemfire.distributed.LocatorLauncher.Command
 +   */
 +  public Command getCommand() {
 +    return this.command;
 +  }
 +
 +  /**
 +   * Determines whether the PID file is allowed to be overwritten when the Locator is started and a PID file
 +   * already exists in the Locator's specified working directory.
 +   * 
 +   * @return boolean indicating if force has been enabled.
 +   */
 +  public boolean isForcing() {
 +    return this.force;
 +  }
 +
 +  /**
 +   * Determines whether this launcher will be used to display help information.  If so, then none of the standard
 +   * Locator launcher commands will be used to affect the state of the Locator.  A launcher is said to be 'helping'
 +   * if the user entered the "--help" option (switch) on the command-line.
 +   * 
 +   * @return a boolean value indicating if this launcher is used for displaying help information.
 +   * @see com.gemstone.gemfire.distributed.LocatorLauncher.Command
 +   */
 +  public boolean isHelping() {
 +    return this.help;
 +  }
 +  
 +  /**
 +   * Determines whether this launcher will redirect output to system logs when
 +   * starting a new Locator process.
 +   * 
 +   * @return a boolean value indicating if this launcher will redirect output 
 +   * to system logs when starting a new Locator process
 +   */
 +  public boolean isRedirectingOutput() {
 +    return this.redirectOutput;
 +  }
 +
 +  /**
 +   * Gets the IP address of the NIC to which the Locator has bound itself listening for client requests.
 +   * 
 +   * @return an InetAddress object representing the configured bind address for the Locator.
 +   * @see java.net.InetAddress
 +   */
 +  public InetAddress getBindAddress() {
 +    return this.bindAddress;
 +  }
 +
 +  /**
 +   * Gets the host, as either hostname or IP address, on which the Locator was bound and running.  An attempt is made
 +   * to get the canonical hostname for IP address to which the Locator was bound for accepting client requests.  If
 +   * the bind address is null or localhost is unknown, then a default String value of "localhost/127.0.0.1" is returned.
 +   * 
 +   * Note, this information is purely information and should not be used to re-construct state or for
 +   * other purposes.
 +   * 
 +   * @return the hostname or IP address of the host running the Locator, based on the bind-address, or
 +   * 'localhost/127.0.0.1' if the bind address is null and localhost is unknown.
 +   * @see java.net.InetAddress
 +   * @see #getBindAddress()
 +   */
 +  protected String getBindAddressAsString() {
 +    try {
 +      if (getBindAddress() != null) {
 +        return getBindAddress().getCanonicalHostName();
 +      }
 +
 +      InetAddress localhost = SocketCreator.getLocalHost();
 +
 +      return localhost.getCanonicalHostName();
 +    }
 +    catch (UnknownHostException ignore) {
 +      // TODO determine a better value for the host on which the Locator is running to return here...
 +      // NOTE returning localhost/127.0.0.1 implies the bindAddress was null and no IP address for localhost
 +      // could be found
 +      return "localhost/127.0.0.1";
 +    }
 +  }
 +
 +  /**
 +   * Gets the hostname that clients will use to lookup the running Locator.
 +   * 
 +   * @return a String indicating the hostname used by clients to lookup the Locator.
 +   */
 +  public String getHostnameForClients() {
 +    return this.hostnameForClients;
 +  }
 +
 +  /**
 +   * Gets the name of the log file used to log information about this Locator.
 +   * 
 +   * @return a String value indicating the name of this Locator's log file.
 +   */
 +  public String getLogFileName() {
 +    return StringUtils.defaultIfBlank(getMemberName(), DEFAULT_LOCATOR_LOG_NAME).concat(DEFAULT_LOCATOR_LOG_EXT);
 +  }
 +
 +  /**
 +   * Gets the name of this member (this Locator) in the GemFire distributed system and determined by the 'name' GemFire
 +   * property.
 +   * 
 +   * @return a String indicating the name of the member (this Locator) in the GemFire distributed system.
 +   * @see AbstractLauncher#getMemberName()
 +   */
 +  public String getMemberName() {
 +    return StringUtils.defaultIfBlank(this.memberName, super.getMemberName());
 +  }
 +
 +  /**
 +   * Gets the user-specified process ID (PID) of the running Locator that LocatorLauncher uses to issue status and
 +   * stop commands to the Locator.
 +   * 
 +   * @return an Integer value indicating the process ID (PID) of the running Locator.
 +   */
 +  @Override
 +  public Integer getPid() {
 +    return this.pid;
 +  }
 +
 +  /**
 +   * Gets the port number on which the Locator listens for client requests.
 +   * 
 +   * @return an Integer value indicating the port number on which the Locator is listening for client requests.
 +   */
 +  public Integer getPort() {
 +    return this.port;
 +  }
 +
 +  /**
 +   * Gets the port number represented as a String value.  If the port number is null, the the default Locator port
 +   * (10334) is returned;
 +   * 
 +   * @return the port number as a String value.
 +   * @see #getPort()
 +   */
 +  public String getPortAsString() {
 +    return ObjectUtils.defaultIfNull(getPort(), getDefaultLocatorPort()).toString();
 +  }
 +
 +  /**
 +   * Gets the GemFire Distributed System (cluster) Properties.
 +   *
 +   * @return a Properties object containing the configuration settings for the GemFire Distributed System (cluster).
 +   * @see java.util.Properties
 +   */
 +  public Properties getProperties() {
 +    return (Properties) this.distributedSystemProperties.clone();
 +  }
 +
 +  /**
 +   * Gets the name for a GemFire Locator.
 +   * 
 +   * @return a String indicating the name for a GemFire Locator.
 +   */
 +  public String getServiceName() {
 +    return LOCATOR_SERVICE_NAME;
 +  }
 +
 +  /**
 +   * Gets the working directory pathname in which the Locator will be run.
 +   * 
 +   * @return a String value indicating the pathname of the Locator's working directory.
 +   */
 +  @Override
 +  public String getWorkingDirectory() {
 +    return this.workingDirectory;
 +  }
 +
 +  /**
 +   * Displays help for the specified Locator launcher command to standard err.  If the Locator launcher command
 +   * is unspecified, then usage information is displayed instead.
 +   * 
 +   * @param command the Locator launcher command in which to display help information.
 +   * @see #usage()
 +   */
 +  public void help(final Command command) {
 +    if (Command.isUnspecified(command)) {
 +      usage();
 +    }
 +    else {
 +      info(StringUtils.wrap(helpMap.get(command.getName()), 80, ""));
 +      info("\n\nusage: \n\n");
 +      info(StringUtils.wrap("> java ... " + getClass().getName() + " " + usageMap.get(command), 80, "\t\t"));
 +      info("\n\noptions: \n\n");
 +
 +      for (String option : command.getOptions()) {
 +        info(StringUtils.wrap("--" + option + ": " + helpMap.get(option) + "\n", 80, "\t"));
 +      }
 +
 +      info("\n\n");
 +    }
 +  }
 +
 +  /**
 +   * Displays usage information on the proper invocation of the LocatorLauncher from the command-line to standard err.
 +   * 
 +   * @see #help(com.gemstone.gemfire.distributed.LocatorLauncher.Command)
 +   */
 +  public void usage() {
 +    info(StringUtils.wrap(helpMap.get("launcher"), 80, "\t"));
 +    info("\n\nSTART\n\n");
 +    help(Command.START);
 +    info("STATUS\n\n");
 +    help(Command.STATUS);
 +    info("STOP\n\n");
 +    help(Command.STOP);
 +  }
 +
 +  /**
 +   * The Runnable method used to launch the Locator with the specified command.  If 'start' has been issued, then run
 +   * will block as expected for the Locator to stop.  The 'start' command is implemented with a call to start()
 +   * followed by a call to waitOnLocator().
 +   * 
 +   * @see java.lang.Runnable
 +   * @see LocatorLauncher.Command
 +   * @see LocatorLauncher#start()
 +   * @see LocatorLauncher#waitOnLocator()
 +   * @see LocatorLauncher#status()
 +   * @see LocatorLauncher#stop()
 +   * @see LocatorLauncher#version()
 +   * @see LocatorLauncher#help(com.gemstone.gemfire.distributed.LocatorLauncher.Command)
 +   * @see LocatorLauncher#usage()
 +   */
 +  public void run() {
 +    if (!isHelping()) {
 +      switch (getCommand()) {
 +        case START:
 +          info(start());
 +          waitOnLocator();
 +          break;
 +        case STATUS:
 +          info(status());
 +          break;
 +        case STOP:
 +          info(stop());
 +          break;
 +        case VERSION:
 +          info(version());
 +          break;
 +        default:
 +          usage();
 +      }
 +    }
 +    else {
 +      help(getCommand());
 +    }
 +  }
 +
 +  /**
 +   * Gets a File reference with the path to the PID file for the Locator.
 +   * 
 +   * @return a File reference to the path of the Locator's PID file.
 +   */
 +  protected File getLocatorPidFile() {
 +    return new File(getWorkingDirectory(), ProcessType.LOCATOR.getPidFileName());
 +  }
 +  
 +  /**
 +   * Determines whether a GemFire Locator can be started with this instance of LocatorLauncher.
 +   * 
 +   * @return a boolean indicating whether a GemFire Locator can be started with this instance of LocatorLauncher,
 +   * which is true if the LocatorLauncher has not already started a Locator or a Locator is not already running.
 +   * @see #start()
 +   */
 +  private boolean isStartable() {
 +    return (!isRunning() && this.starting.compareAndSet(false, true));
 +  }
 +
 +  /**
 +   * Starts a Locator running on the specified port and bind address, as determined by getPort and getBindAddress
 +   * respectively, defaulting to 10334 and 'localhost' if not specified, with both peer and server location enabled.
 +   * 
 +   * 'start' is an asynchronous invocation of the Locator.  As such, this method makes no guarantees whether the
 +   * Locator's location services (peer and server) are actually running before it returns.  The Locator's
 +   * location-based services are initiated in separate, daemon Threads and depends on the relative timing
 +   * and scheduling of those Threads by the JVM.  If the application using this API wishes for the Locator to continue
 +   * running after normal application processing completes, then one must call <code>waitOnLocator</code>.
 +   * 
 +   * Given the nature of start, the Locator's status will be in either 1 of 2 possible states.  If the 'request' to
 +   * start the Locator proceeds without exception, the status will be 'STARTED'.  However, if any exception is
 +   * encountered during the normal startup sequence, then a RuntimeException is thrown and the status is set to
 +   * 'STOPPED'.
 +   * 
 +   * @return a LocatorState to reflect the state of the Locator after start.
 +   * @throws RuntimeException if the Locator failed to start for any reason.
 +   * @throws IllegalStateException if the Locator is already running.
 +   * @see #failOnStart(Throwable)
 +   * @see #getBindAddress()
 +   * @see #getDistributedSystemProperties()
 +   * @see #isForcing()
 +   * @see #getLogFile()
 +   * @see #getLocatorPidFile
 +   * @see #getPort()
 +   * @see #status()
 +   * @see #stop()
 +   * @see #waitOnLocator()
 +   * @see #waitOnStatusResponse(long, long, java.util.concurrent.TimeUnit)
 +   * @see com.gemstone.gemfire.distributed.LocatorLauncher.LocatorState
 +   * @see com.gemstone.gemfire.distributed.AbstractLauncher.Status#NOT_RESPONDING
 +   * @see com.gemstone.gemfire.distributed.AbstractLauncher.Status#ONLINE
 +   * @see com.gemstone.gemfire.distributed.AbstractLauncher.Status#STARTING
 +   */
 +  @SuppressWarnings("deprecation")
 +  public LocatorState start() {
 +    if (isStartable()) {
 +      INSTANCE.compareAndSet(null, this);
 +
 +      try {
 +        this.process = new ControllableProcess(this.controlHandler, new File(getWorkingDirectory()), ProcessType.LOCATOR, isForcing());
 +        
 +        assertPortAvailable(getBindAddress(), getPort());
 +
 +        ProcessLauncherContext.set(isRedirectingOutput(), getOverriddenDefaults(), new StartupStatusListener() {
 +          @Override
 +          public void setStatus(final String statusMessage) {
 +            LocatorLauncher.this.statusMessage = statusMessage;
 +          }
 +        });
 +        
 +        //TODO : remove the extra param for loadFromSharedConfigDir
 +        try {
 +          this.locator = InternalLocator.startLocator(getPort(), getLogFile(), null, null, null, getBindAddress(),
 +            getDistributedSystemProperties(), DEFAULT_ENABLE_PEER_LOCATION, DEFAULT_ENABLE_SERVER_LOCATION,
 +              getHostnameForClients(), false);
 +        }
 +        finally {
 +          ProcessLauncherContext.remove();
 +        }
 +
 +        debug("Running Locator on (%1$s) in (%2$s) as (%2$s)...", getId(), getWorkingDirectory(), getMember());
 +        running.set(true);
 +
 +        return new LocatorState(this, Status.ONLINE);
 +      }
 +      catch (IOException e) {
 +        failOnStart(e);
 +        throw new RuntimeException(LocalizedStrings.Launcher_Command_START_IO_ERROR_MESSAGE.toLocalizedString(
 +          getServiceName(), getWorkingDirectory(), getId(), e.getMessage()), e);
 +      }
 +      catch (FileAlreadyExistsException e) {
 +        failOnStart(e);
 +        throw new RuntimeException(LocalizedStrings.Launcher_Command_START_PID_FILE_ALREADY_EXISTS_ERROR_MESSAGE
 +          .toLocalizedString(getServiceName(), getWorkingDirectory(), getId()), e);
 +      }
 +      catch (PidUnavailableException e) {
 +        failOnStart(e);
 +        throw new RuntimeException(LocalizedStrings.Launcher_Command_START_PID_UNAVAILABLE_ERROR_MESSAGE
 +          .toLocalizedString(getServiceName(), getId(), getWorkingDirectory(), e.getMessage()), e);
 +      }
 +      catch (Error e) {
 +        failOnStart(e);
 +        throw e;
 +      }
 +      catch (RuntimeException e) {
 +        failOnStart(e);
 +        throw e;
 +      }
 +      catch (Exception e) {
 +        failOnStart(e);
 +        throw new RuntimeException(e);
 +      }
 +      finally {
 +        this.starting.set(false);
 +      }
 +    }
 +    else {
 +      throw new IllegalStateException(LocalizedStrings.Launcher_Command_START_SERVICE_ALREADY_RUNNING_ERROR_MESSAGE
 +        .toLocalizedString(getServiceName(), getWorkingDirectory(), getId()));
 +    }
 +  }
 +
 +  @Override
 +  protected Properties getDistributedSystemProperties() {
 +     Properties properties = super.getDistributedSystemProperties(getProperties());
 +     return properties;
 +  }
 +
 +  /**
 +   * A helper method to ensure the same sequence of actions are taken when the Locator fails to start
 +   * caused by some exception.
 +   * 
 +   * @param cause the Throwable thrown during the startup or wait operation on the Locator.
 +   */
 +  private void failOnStart(final Throwable cause) {
 +
 +    if (cause != null) {
 +      logger.log(Level.INFO, "locator is exiting due to an exception", cause);
 +    } else {
 +      logger.log(Level.INFO, "locator is exiting normally");
 +    }
 +    
 +    if (this.locator != null) {
 +      this.locator.stop();
 +      this.locator = null;
 +    }
 +    if (this.process != null) {
 +      this.process.stop();
 +      this.process = null;
 +    }
 +
 +    INSTANCE.compareAndSet(this, null);
 +
 +    this.running.set(false);
 +  }
 +
 +  /**
 +   * Waits on the Locator to stop causing the calling Thread to join with the Locator's location-based services Thread.
 +   * 
 +   * @return the Locator's status once it stops.
 +   * @throws AssertionError if the Locator has not been started and the reference is null (assertions must be enabled
 +   * for the error to be thrown).
 +   * @see #failOnStart(Throwable)
 +   * @see com.gemstone.gemfire.distributed.AbstractLauncher.Status
 +   * @see com.gemstone.gemfire.distributed.LocatorLauncher.LocatorState
 +   */
 +  public LocatorState waitOnLocator() {
 +    Throwable t = null;
 +
 +    try {
 +      // make sure the Locator was started and the reference was set
 +      assert getLocator() != null : "The Locator must first be started with a call to start!";
 +
 +      debug("Waiting on Locator (%1$s) to stop...", getId());
 +
 +      // prevent the JVM from exiting by joining the Locator Thread
 +      getLocator().waitToStop();
 +    }
 +    catch (InterruptedException e) {
 +      Thread.currentThread().interrupt();
 +      t = e;
 +      debug(e);
 +    }
 +    catch (RuntimeException e) {
 +      t = e;
 +      throw e;
 +    }
 +    catch (Throwable e) {
 +      t = e;
 +      throw e;
 +    }
 +    finally {
 +      failOnStart(t);
 +    }
 +
 +    return new LocatorState(this, Status.STOPPED);
 +  }
 +
 +  /**
 +   * Waits for a Locator status request response to be returned up to the specified timeout in the given unit of time.
 +   * This call will send status requests at fixed intervals in the given unit of time until the timeout expires.  If
 +   * the request to determine the Locator's status is successful, then the Locator is considered to be 'ONLINE'.
 +   * Otherwise, the Locator is considered to be unresponsive to the status request.
 +   * 
 +   * However, this does not necessarily imply the Locator start was unsuccessful, only that a response was not received
 +   * in the given time period.
 +   * 
 +   * Note, this method does not block or cause the Locator's location-based services (daemon Threads) to continue
 +   * running in anyway if the main application Thread terminates when running the Locator in-process.  If the caller
 +   * wishes to start a Locator in an asynchronous manner within the application process, then a call should be made to
 +   * <code>waitOnLocator</code>.
 +   * 
 +   * @param timeout a long value in time unit indicating when the period of time should expire in attempting
 +   * to determine the Locator's status.
 +   * @param interval a long value in time unit for how frequent the requests should be sent to the Locator.
 +   * @param timeUnit the unit of time in which the timeout and interval are measured.
 +   * @return the state of the Locator, which will either be 'ONLINE' or "NOT RESPONDING'.  If the status returned is
 +   * 'NOT RESPONDING', it just means the Locator did not respond to the status request within the given time period.
 +   * It should not be taken as the Locator failed to start.
 +   * @see #waitOnLocator()
 +   */
 +  public LocatorState waitOnStatusResponse(final long timeout, final long interval, final TimeUnit timeUnit) {
 +    final long endTimeInMilliseconds = (System.currentTimeMillis() + timeUnit.toMillis(timeout));
 +
 +    while (System.currentTimeMillis() < endTimeInMilliseconds) {
 +      try {
 +        LocatorStatusResponse response = InternalLocator.statusLocator(getPort(), getBindAddress());
 +        return new LocatorState(this, Status.ONLINE, response);
 +      }
 +      catch (Exception ignore) {
 +        try {
 +          synchronized (this) {
 +            timeUnit.timedWait(this, interval);
 +          }
 +        }
 +        catch (InterruptedException ignoreInterrupt) {
 +          // NOTE just go and send another status request to the Locator...
 +        }
 +      }
 +    }
 +
 +    // NOTE just because we were not able to communicate with the Locator in the given amount of time does not mean
 +    // the Locator is having problems.  The Locator could be slow in starting up and the timeout may not be
 +    // long enough.
 +    return new LocatorState(this, Status.NOT_RESPONDING);
 +  }
 +
 +  /**
 +   * Attempts to determine the state of the Locator.  The Locator's status will be in only 1 of 2 possible states,
 +   * either ONLINE or OFFLINE.  This method behaves differently depending on which parameters were specified when
 +   * the LocatorLauncher was constructed with an instance of Builder.  If either the 'dir' or the 'pid' command-line
 +   * option were specified, then an attempt is made to determine the Locator's status by using the dir or pid to
 +   * correctly identify the Locator's MemberMXBean registered in the MBeanServer of the Locator's JVM, and invoking
 +   * the 'status' operation.  The same behavior occurs if the caller specified the Locator's GemFire member name or ID.
 +   * 
 +   * However, if 'dir' or 'pid' were not specified, then determining the Locator's status defaults to using the
 +   * configured bind address and port.  If the bind address or port was not specified when using the Builder to
 +   * construct a LocatorLauncher instance, then the defaults for both bind address and port are used.  In either case,
 +   * an actual TCP/IP request is made to the Locator's ServerSocket to ensure it is listening for client requests.
 +   * This is true even when the LocatorLauncher is used in-process by calling the API.
 +   * 
 +   * If the conditions above hold, then the Locator is deemed to be 'ONLINE', otherwise, the Locator is considered
 +   * 'OFFLINE'.
 +   * 
 +   * @return the Locator's state.
 +   * @see #start()
 +   * @see #stop()
 +   * @see com.gemstone.gemfire.distributed.AbstractLauncher.Status
 +   * @see com.gemstone.gemfire.distributed.LocatorLauncher.LocatorState
 +   */
 +  public LocatorState status() {
 +    final LocatorLauncher launcher = getInstance();
 +    // if this instance is starting then return local status
 +    if (this.starting.get()) {
 +      debug("Getting status from the LocatorLauncher instance that actually launched the GemFire Locator.%n");
 +      return new LocatorState(this, Status.STARTING);
 +    }
 +    // if this instance is running then return local status
 +    else if (isRunning()) {
 +      debug("Getting Locator status using host (%1$s) and port (%2$s)%n", getBindAddressAsString(), getPortAsString());
 +      return statusWithPort();
 +    }
 +    // if in-process do not use ProcessController
 +    else if (isPidInProcess() && launcher != null) {
 +      return launcher.statusInProcess();
 +    }
 +    // attempt to get status using pid if provided
 +    else if (getPid() != null) {
 +      debug("Getting Locator status using process ID (%1$s)%n", getPid());
 +      return statusWithPid();
 +    }
 +    // attempt to get status using workingDirectory unless port was specified
 +    else if (!(this.bindAddressSpecified || this.portSpecified)) {
 +      debug("Getting Locator status using working directory (%1$s)%n", getWorkingDirectory());
 +      return statusWithWorkingDirectory();
 +    }
 +    // attempt to get status using host and port (Note, bind address doubles as host when the launcher
 +    // is used to get the Locator's status).
 +    else {
 +      debug("Getting Locator status using host (%1$s) and port (%2$s)%n", getBindAddressAsString(), getPortAsString());
 +      return statusWithPort();
 +    }
 +  }
 +
 +  private LocatorState statusInProcess() {
 +    if (this.starting.get()) {
 +      debug("Getting status from the LocatorLauncher instance that actually launched the GemFire Locator.%n");
 +      return new LocatorState(this, Status.STARTING);
 +    }
 +    else {
 +      debug("Getting Locator status using host (%1$s) and port (%2$s)%n", getBindAddressAsString(), getPortAsString());
 +      return statusWithPort();
 +    }
 +
 +  }
 +
 +  private LocatorState statusWithPid() {
 +    try {
 +      final ProcessController controller = new ProcessControllerFactory().createProcessController(this.controllerParameters, getPid());
 +      controller.checkPidSupport();
 +      final String statusJson = controller.status();
 +      return LocatorState.fromJson(statusJson);
 +    }
 +    catch (ConnectionFailedException e) {
 +      // failed to attach to locator JVM
 +      return createNoResponseState(e, "Failed to connect to locator with process id " + getPid());
 +    } 
 +    catch (IOException e) {
 +      // failed to open or read file or dir
 +      return createNoResponseState(e, "Failed to communicate with locator with process id " + getPid());
 +    } 
 +    catch (MBeanInvocationFailedException e) {
 +      // MBean either doesn't exist or method or attribute don't exist
 +      return createNoResponseState(e, "Failed to communicate with locator with process id " + getPid());
 +    } 
 +    catch (UnableToControlProcessException e) {
 +      return createNoResponseState(e, "Failed to communicate with locator with process id " + getPid());
 +    } 
 +    catch (InterruptedException e) {
 +      Thread.currentThread().interrupt();
 +      return createNoResponseState(e, "Interrupted while trying to communicate with locator with process id " + getPid());
 +    } 
 +    catch (TimeoutException e) {
 +      return createNoResponseState(e, "Failed to communicate with locator with process id " + getPid());
 +    }
 +  }
 +
 +  private LocatorState statusWithPort() {
 +    try {
 +      LocatorStatusResponse response = InternalLocator.statusLocator(getPort(), getBindAddress());
 +      return new LocatorState(this, Status.ONLINE, response);
 +    }
 +    catch (Exception e) {
 +      return createNoResponseState(e, "Failed to connect to locator " + getId());
 +    }
 +  }
 +
 +  private LocatorState statusWithWorkingDirectory() {
 +    int parsedPid = 0;
 +    try {
 +      final ProcessController controller = new ProcessControllerFactory().createProcessController(this.controllerParameters, new File(getWorkingDirectory()), ProcessType.LOCATOR.getPidFileName(), READ_PID_FILE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
 +      parsedPid = controller.getProcessId();
 +      
 +      // note: in-process request will go infinite loop unless we do the following
 +      if (parsedPid == ProcessUtils.identifyPid()) {
 +        LocatorLauncher runningLauncher = getInstance();
 +        if (runningLauncher != null) {
 +          return runningLauncher.status();
 +        }
 +      }
 +
 +      final String statusJson = controller.status();
 +      return LocatorState.fromJson(statusJson);
 +    }
 +    catch (ConnectionFailedException e) {
 +      // failed to attach to locator JVM
 +      return createNoResponseState(e, "Failed to connect to locator with process id " + parsedPid);
 +    } 
 +    catch (FileNotFoundException e) {
 +      // could not find pid file
 +      return createNoResponseState(e, "Failed to find process file " + ProcessType.LOCATOR.getPidFileName() + " in " + getWorkingDirectory());
 +    } 
 +    catch (IOException e) {
 +      // failed to open or read file or dir
 +      return createNoResponseState(e, "Failed to communicate with locator with process id " + parsedPid);
 +    } 
 +    catch (MBeanInvocationFailedException e) {
 +      // MBean either doesn't exist or method or attribute don't exist
 +      return createNoResponseState(e, "Failed to communicate with locator with process id " + parsedPid);
 +    } 
 +    catch (PidUnavailableException e) {
 +      // couldn't determine pid from within locator JVM
 +      return createNoResponseState(e, "Failed to find usable process id within file " + ProcessType.LOCATOR.getPidFileName() + " in " + getWorkingDirectory());
 +    } 
 +    catch (UnableToControlProcessException e) {
 +      return createNoResponseState(e, "Failed to communicate with locator with process id " + parsedPid);
 +    } 
 +    catch (InterruptedException e) {
 +      Thread.currentThread().interrupt();
 +      return createNoResponseState(e, "Interrupted while trying to communicate with locator with process id " + parsedPid);
 +    } 
 +    catch (TimeoutException e) {
 +      return createNoResponseState(e, "Failed to communicate with locator with process id " + parsedPid);
 +    }
 +  }
 +
 +  /**
 +   * Determines whether the Locator can be stopped in-process, such as when a Locator is embedded in an application
 +   * and the LocatorLauncher API is being used.
 +   * 
 +   * @return a boolean indicating whether the Locator can be stopped in-process (the application's process with
 +   * an embedded Locator).
 +   */
 +  protected boolean isStoppable() {
 +    return (isRunning() && getLocator() != null);
 +  }
 +
 +  /**
 +   * Stop shuts the running Locator down.  Using the API, the Locator is requested to stop by calling the Locator
 +   * object's 'stop' method.  Internally, this method is no different than using the LocatorLauncher class from the
 +   * command-line or from within GemFire shell (Gfsh).  In every single case, stop sends a TCP/IP 'shutdown' request
 +   * on the configured address/port to which the Locator is bound and listening.
 +   * 
 +   * If the "shutdown" request is successful, then the Locator will be 'STOPPED'.  Otherwise, the Locator is considered
 +   * 'OFFLINE' since the actual state cannot be fully assessed (as in the application process in which the Locator was
 +   * hosted may still be running and the Locator object may still exist even though it is no longer responding to
 +   * location-based requests).  The later is particularly important in cases where the system resources (such as
 +   * Sockets) may not have been cleaned up yet.  Therefore, by returning a status of 'OFFLINE', the value is meant to
 +   * reflect this in-deterministic state.
 +   * 
 +   * @return a LocatorState indicating the state of the Locator after stop has been requested.
 +   * @see #start()
 +   * @see #status()
 +   * @see com.gemstone.gemfire.distributed.LocatorLauncher.LocatorState
 +   * @see com.gemstone.gemfire.distributed.AbstractLauncher.Status#NOT_RESPONDING
 +   * @see com.gemstone.gemfire.distributed.AbstractLauncher.Status#STOPPED
 +   */
 +  public LocatorState stop() {
 +    final LocatorLauncher launcher = getInstance();
 +    // if this instance is running then stop it
 +    if (isStoppable()) {
 +      return stopInProcess();
 +    }
 +    // if in-process but difference instance of LocatorLauncher
 +    else if (isPidInProcess() && launcher != null) {
 +      return launcher.stopInProcess();
 +    }
 +    // attempt to stop Locator using pid...
 +    else if (getPid() != null) {
 +      return stopWithPid();
 +    }
 +    // attempt to stop Locator using the working directory...
 +    //else if (this.workingDirectorySpecified) {
 +    else if (getWorkingDirectory() != null) {
 +      return stopWithWorkingDirectory();
 +    }
 +    else {
 +      return new LocatorState(this, Status.NOT_RESPONDING);
 +    }
 +  }
 +  
 +  private LocatorState stopInProcess() {
 +    if (isStoppable()) {
 +      this.locator.stop();
 +      this.locator = null;
 +      this.process.stop();
 +      this.process = null;
 +      INSTANCE.compareAndSet(this, null); // note: other thread may return Status.NOT_RESPONDING now
 +      this.running.set(false);
 +      return new LocatorState(this, Status.STOPPED);
 +    } else {
 +      return new LocatorState(this, Status.NOT_RESPONDING);
 +    }
 +  }
 +
 +  private LocatorState stopWithPid() {
 +    try {
 +      final ProcessController controller = new ProcessControllerFactory().createProcessController(new LocatorControllerParameters(), getPid());
 +      controller.checkPidSupport();
 +      controller.stop();
 +      return new LocatorState(this, Status.STOPPED);
 +    }
 +    catch (ConnectionFailedException e) {
 +      // failed to attach to locator JVM
 +      return createNoResponseState(e, "Failed to connect to locator with process id " + getPid());
 +    } 
 +    catch (IOException e) {
 +      // failed to open or read file or dir
 +      return createNoResponseState(e, "Failed to communicate with locator with process id " + getPid());
 +    } 
 +    catch (MBeanInvocationFailedException e) {
 +      // MBean either doesn't exist or method or attribute don't exist
 +      return createNoResponseState(e, "Failed to communicate with locator with process id " + getPid());
 +    } 
 +    catch (UnableToControlProcessException e) {
 +      return createNoResponseState(e, "Failed to communicate with locator with process id " + getPid());
 +    }
 +  }
 +
 +  @Deprecated
 +  private LocatorState stopWithPort() {
 +    try {
 +      InternalLocator.stopLocator(getPort(), getBindAddress());
 +      return new LocatorState(this, Status.STOPPED);
 +    }
 +    catch (ConnectException e) {
 +      if (getBindAddress() == null) {
 +        return createNoResponseState(e, "Failed to connect to locator on port " + getPort());
 +      } 
 +      else {
 +        return createNoResponseState(e, "Failed to connect to locator on " + getId());
 +      }
 +    }
 +  }
 +
 +  private LocatorState stopWithWorkingDirectory() {
 +    int parsedPid = 0;
 +    try {
 +      final ProcessController controller = new ProcessControllerFactory().createProcessController(this.controllerParameters, new File(getWorkingDirectory()), ProcessType.LOCATOR.getPidFileName(), READ_PID_FILE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
 +      parsedPid = controller.getProcessId();
 +      
 +      // NOTE in-process request will go infinite loop unless we do the following
 +      if (parsedPid == ProcessUtils.identifyPid()) {
 +        final LocatorLauncher runningLauncher = getInstance();
 +        if (runningLauncher != null) {
 +          return runningLauncher.stopInProcess();
 +        }
 +      }
 +      
 +      controller.stop();
 +      return new LocatorState(this, Status.STOPPED);
 +    }
 +    catch (ConnectionFailedException e) {
 +      // failed to attach to locator JVM
 +      return createNoResponseState(e, "Failed to connect to locator with process id " + parsedPid);
 +    } 
 +    catch (FileNotFoundException e) {
 +      // could not find pid file
 +      return createNoResponseState(e, "Failed to find process file " + ProcessType.LOCATOR.getPidFileName() + " in " + getWorkingDirectory());
 +    } 
 +    catch (IOException e) {
 +      // failed to open or read file or dir
 +      return createNoResponseState(e, "Failed to communicate with locator with process id " + parsedPid);
 +    } 
 +    catch (InterruptedException e) {
 +      Thread.currentThread().interrupt();
 +      return createNoResponseState(e, "Interrupted while trying to communicate with locator with process id " + parsedPid);
 +    } 
 +    catch (MBeanInvocationFailedException e) {
 +      // MBean either doesn't exist or method or attribute don't exist
 +      return createNoResponseState(e, "Failed to communicate with locator with process id " + parsedPid);
 +    } 
 +    catch (PidUnavailableException e) {
 +      // couldn't determine pid from within locator JVM
 +      return createNoResponseState(e, "Failed to find usable process id within file " + ProcessType.LOCATOR.getPidFileName() + " in " + getWorkingDirectory());
 +    } 
 +    catch (TimeoutException e) {
 +      return createNoResponseState(e, "Timed out trying to find usable process id within file " + ProcessType.LOCATOR.getPidFileName() + " in " + getWorkingDirectory());
 +    } 
 +    catch (UnableToControlProcessException e) {
 +      return createNoResponseState(e, "Failed to communicate with locator with process id " + parsedPid);
 +    }
 +  }
 +
 +  private LocatorState createNoResponseState(final Exception cause, final String errorMessage) {
 +    debug(cause);
 +    //info(errorMessage);
 +    return new LocatorState(this, Status.NOT_RESPONDING, errorMessage);
 +  }
 +  
 +  private Properties getOverriddenDefaults() {
 +    Properties overriddenDefaults = new Properties();
 +
 +    overriddenDefaults.put(ProcessLauncherContext.OVERRIDDEN_DEFAULTS_PREFIX.concat(DistributionConfig.LOG_FILE_NAME),
 +      getLogFileName());
 +
 +    for (String key : System.getProperties().stringPropertyNames()) {
 +      if (key.startsWith(ProcessLauncherContext.OVERRIDDEN_DEFAULTS_PREFIX)) {
 +        overriddenDefaults.put(key, System.getProperty(key));
 +      }
 +    }
 +    
 +    return overriddenDefaults;
 +  }
 +  
 +  private class LocatorControllerParameters implements ProcessControllerParameters {
 +    @Override
 +    public File getPidFile() {
 +      return getLocatorPidFile();
 +    }
 +
 +    @Override
 +    public File getWorkingDirectory() {
 +      return new File(LocatorLauncher.this.getWorkingDirectory());
 +    }
 +  
 +    @Override
 +    public int getProcessId() {
 +      return getPid();
 +    }
 +    
 +    @Override
 +    public ProcessType getProcessType() {
 +      return ProcessType.LOCATOR;
 +    }
 +  
 +    @Override
 +    public ObjectName getNamePattern() {
 +      try {
 +        return ObjectName.getInstance("GemFire:type=Member,*");
 +      } catch (MalformedObjectNameException e) {
 +        return null;
 +      } catch (NullPointerException e) {
 +        return null;
 +      }
 +    }
 +  
 +    @Override
 +    public String getPidAttribute() {
 +      return "ProcessId";
 +    }
 +  
 +    @Override
 +    public String getStopMethod() {
 +      return "shutDownMember";
 +    }
 +    
 +    @Override
 +    public String getStatusMethod() {
 +      return "status";
 +    }
 +  
 +    @Override
 +    public String[] getAttributes() {
 +      return new String[] {"Locator", "Server"};
 +    }
 +  
 +    @Override
 +    public Object[] getValues() {
 +      return new Object[] {Boolean.TRUE, Boolean.FALSE};
 +    }
 +  }
 +
 +  /**
 +   * Following the Builder design pattern, the LocatorLauncher Builder is used to configure and create a properly
 +   * initialized instance of the LocatorLauncher class for running the Locator and performing other Locator
 +   * operations.
 +   */
 +  public static class Builder {
 +
 +    protected static final Command DEFAULT_COMMAND = Command.UNSPECIFIED;
 +
 +    private Boolean debug;
 +    private Boolean force;
 +    private Boolean help;
 +    private Boolean redirectOutput;
 +    private Boolean loadSharedConfigFromDir;
 +    private Command command;
 +
 +    private InetAddress bindAddress;
 +
 +    private Integer pid;
 +    private Integer port;
 +
 +    private final Properties distributedSystemProperties = new Properties();
 +
 +    private String hostnameForClients;
 +    private String memberName;
 +    private String workingDirectory;
 +
 +    /**
 +     * Default constructor used to create an instance of the Builder class for programmatical access.
 +     */
 +    public Builder() {
 +    }
 +
 +    /**
 +     * Constructor used to create and configure an instance of the Builder class with the specified arguments, often
 +     * passed from the command-line when launching an instance of this class from the command-line using the Java
 +     * launcher.
 +     * 
 +     * @param args the array of arguments used to configure the Builder.
 +     */
 +    public Builder(final String... args) {
 +      parseArguments(args != null ? args : new String[0]);
 +    }
 +
 +    /**
 +     * Gets an instance of the JOpt Simple OptionParser to parse the command-line arguments.
 +     * 
 +     * @return an instance of the JOpt Simple OptionParser configured with the command-line options used by the Locator.
 +     */
 +    private OptionParser getParser() {
 +      final OptionParser parser = new OptionParser(true);
 +
 +      parser.accepts("bind-address").withRequiredArg().ofType(String.class);
 +      parser.accepts("debug");
 +      parser.accepts("dir").withRequiredArg().ofType(String.class);
 +      parser.accepts("force");
 +      parser.accepts("help");
 +      parser.accepts("hostname-for-clients").withRequiredArg().ofType(String.class);
 +      parser.accepts("pid").withRequiredArg().ofType(Integer.class);
 +      parser.accepts("port").withRequiredArg().ofType(Integer.class);
 +      parser.accepts("redirect-output");
 +      parser.accepts("version");
 +
 +      return parser;
 +    }
 +
 +    /**
 +     * Parses an array of arguments to configure this Builder with the intent of constructing a Locator launcher to
 +     * invoke a Locator.  This method is called to parse the arguments specified by the user on the command-line.
 +     * 
 +     * @param args the array of arguments used to configure this Builder and create an instance of LocatorLauncher.
 +     */
 +    protected void parseArguments(final String... args) {
 +      try {
 +        parseCommand(args);
 +        parseMemberName(args);
 +
 +        final OptionSet options = getParser().parse(args);
 +
 +        setDebug(options.has("debug"));
 +        setForce(options.has("force"));
 +        setHelp(options.has("help"));
 +        setRedirectOutput(options.has("redirect-output"));
 +
 +        if (!isHelping()) {
 +          if (options.has("bind-address")) {
 +            setBindAddress(ObjectUtils.toString(options.valueOf("bind-address")));
 +          }
 +
 +          if (options.has("dir")) {
 +            setWorkingDirectory(ObjectUtils.toString(options.valueOf("dir")));
 +          }
 +
 +          if (options.has("hostname-for-clients")) {
 +            setHostnameForClients(ObjectUtils.toString(options.valueOf("hostname-for-clients")));
 +          }
 +
 +          if (options.has("pid")) {
 +            setPid((Integer) options.valueOf("pid"));
 +          }
 +
 +          if (options.has("port")) {
 +            setPort((Integer) options.valueOf("port"));
 +          }
 +
 +          if (options.has("version")) {
 +            setCommand(Command.VERSION);
 +          }
 +        }
 +      }
 +      catch (OptionException e) {
 +        throw new IllegalArgumentException(LocalizedStrings.Launcher_Builder_PARSE_COMMAND_LINE_ARGUMENT_ERROR_MESSAGE
 +          .toLocalizedString("Locator", e.getMessage()), e);
 +      }
 +      catch (Exception e) {
 +        throw new RuntimeException(e.getMessage(), e);
 +      }
 +    }
 +
 +    /**
 +     * Iterates the list of arguments in search of the target Locator launcher command.
 +     * 
 +     * @param args an array of arguments from which to search for the Locator launcher command.
 +     * @see com.gemstone.gemfire.distributed.LocatorLauncher.Command#valueOfName(String)
 +     * @see #parseArguments(String...)
 +     */
 +    protected void parseCommand(final String... args) {
 +      // search the list of arguments for the command; technically, the command should be the first argument in the
 +      // list, but does it really matter?  stop after we find one valid command.
 +      for (String arg : args) {
 +        final Command command = Command.valueOfName(arg);
 +        if (command != null) {
 +          setCommand(command);
 +          break;
 +        }
 +      }
 +    }
 +
 +    /**
 +     * Iterates the list of arguments in search of the Locator's GemFire member name.  If the argument does not
 +     * start with '-' or is not the name of a Locator launcher command, then the value is presumed to be the member name
 +     * for the Locator in GemFire.
 +     * 
 +     * @param args the array of arguments from which to search for the Locator's member name in GemFire.
 +     * @see com.gemstone.gemfire.distributed.LocatorLauncher.Command#isCommand(String)
 +     * @see #parseArguments(String...)
 +     */
 +    protected void parseMemberName(final String[] args) {
 +      for (String arg : args) {
 +        if (!(arg.startsWith(OPTION_PREFIX) || Command.isCommand(arg))) {
 +          setMemberName(arg);
 +          break;
 +        }
 +      }
 +    }
 +
 +    /**
 +     * Gets the Locator launcher command used during the invocation of the LocatorLauncher.
 +     * 
 +     * @return the Locator launcher command used to invoke (run) the LocatorLauncher class.
 +     * @see #setCommand(com.gemstone.gemfire.distributed.LocatorLauncher.Command)
 +     * @see LocatorLauncher.Command
 +     */
 +    public Command getCommand() {
 +      return ObjectUtils.defaultIfNull(this.command, DEFAULT_COMMAND);
 +    }
 +
 +    /**
 +     * Sets the Locator launcher command used during the invocation of the LocatorLauncher
 +     * 
 +     * @param command the targeted Locator launcher command used during the invocation (run) of LocatorLauncher.
 +     * @return this Builder instance.
 +     * @see #getCommand()
 +     * @see LocatorLauncher.Command
 +     */
 +    public Builder setCommand(final Command command) {
 +      this.command = command;
 +      return this;
 +    }
 +
 +    /**
 +     * Determines whether the new instance of the LocatorLauncher will be set to debug mode.
 +     * 
 +     * @return a boolean value indicating whether debug mode is enabled or disabled.
 +     * @see #setDebug(Boolean)
 +     */
 +    public Boolean getDebug() {
 +      return this.debug;
 +    }
 +
 +    /**
 +     * Sets whether the new instance of the LocatorLauncher will be set to debug mode.
 +     * 
 +     * @param debug a boolean value indicating whether debug mode is to be enabled or disabled.
 +     * @return this Builder instance.
 +     * @see #getHelp()
 +     */
 +    public Builder setDebug(final Boolean debug) {
 +      this.debug = debug;
 +      return this;
 +    }
 +
 +    /**
 +     * Gets the GemFire Distributed System (cluster) Properties configuration.
 +     *
 +     * @return a Properties object containing configuration settings for the GemFire Distributed System (cluster).
 +     * @see java.util.Properties
 +     */
 +    public Properties getDistributedSystemProperties() {
 +      return this.distributedSystemProperties;
 +    }
 +
 +    /**
 +     * Gets the boolean value used by the Locator to determine if it should overwrite the PID file if it already exists.
 +     * 
 +     * @return the boolean value specifying whether or not to overwrite the PID file if it already exists.
 +     * @see com.gemstone.gemfire.internal.process.LocalProcessLauncher
 +     * @see #setForce(Boolean)
 +     */
 +    public Boolean getForce() {
 +      return ObjectUtils.defaultIfNull(this.force, DEFAULT_FORCE);
 +    }
 +
 +    /**
 +     * Sets the boolean value used by the Locator to determine if it should overwrite the PID file if it already exists.
 +     * 
 +     * @param force a boolean value indicating whether to overwrite the PID file when it already exists.
 +     * @return this Builder instance.
 +     * @see com.gemstone.gemfire.internal.process.LocalProcessLauncher
 +     * @see #getForce()
 +     */
 +    public Builder setForce(final Boolean force) {
 +      this.force = force;
 +      return this;
 +    }
 +
 + 
 +    /**
 +     * Determines whether the new instance of LocatorLauncher will be used to output help information for either
 +     * a specific command, or for using LocatorLauncher in general.
 +     * 
 +     * @return a boolean value indicating whether help will be output during the invocation of LocatorLauncher.
 +     * @see #setHelp(Boolean)
 +     */
 +    public Boolean getHelp() {
 +      return this.help;
 +    }
 +
 +    /**
 +     * Determines whether help has been enabled.
 +     * 
 +     * @return a boolean indicating if help was enabled.
 +     */
 +    private boolean isHelping() {
 +      return Boolean.TRUE.equals(getHelp());
 +    }
 +
 +    /**
 +     * Sets whether the new instance of LocatorLauncher will be used to output help information for either a specific
 +     * command, or for using LocatorLauncher in general.
 +     * 
 +     * @param help a boolean indicating whether help information is to be displayed during invocation of LocatorLauncher.
 +     * @return this Builder instance.
 +     * @see #getHelp()
 +     */
 +    public Builder setHelp(final Boolean help) {
 +      this.help = help;
 +      return this;
 +    }
 +
 +    final boolean isBindAddressSpecified() {
 +      return (getBindAddress() != null);
 +
 +    }
 +    /**
 +     * Gets the IP address to which the Locator has bound itself listening for client requests.
 +     * 
 +     * @return an InetAddress with the IP address or hostname on which the Locator is bound and listening.
 +     * @see #setBindAddress(String)
 +     * @see java.net.InetAddress
 +     */
 +    public InetAddress getBindAddress() {
 +      return this.bindAddress;
 +    }
 +
 +    /**
 +     * Sets the IP address as an java.net.InetAddress to which the Locator has bound itself listening for client
 +     * requests.
 +     * 
 +     * @param bindAddress the InetAddress with the IP address or hostname on which the Locator is bound and listening.
 +     * @return this Builder instance.
 +     * @throws IllegalArgumentException wrapping the UnknownHostException if the IP address or hostname for the
 +     * bind address is unknown.
 +     * @see #getBindAddress()
 +     * @see java.net.InetAddress
 +     */
 +    public Builder setBindAddress(final String bindAddress) {
 +      if (StringUtils.isBlank(bindAddress)) {
 +        this.bindAddress = null;
 +        return this;
 +      }
 +      else {
 +        try {
 +          this.bindAddress = InetAddress.getByName(bindAddress);
-           return this;
++          if (SocketCreator.isLocalHost(this.bindAddress)) {
++            return this;
++          }
++          else {
++            throw new IllegalArgumentException(bindAddress + " is not an address for this machine.");
++          }
 +        }
 +        catch (UnknownHostException e) {
 +          throw new IllegalArgumentException(LocalizedStrings.Launcher_Builder_UNKNOWN_HOST_ERROR_MESSAGE
 +            .toLocalizedString("Locator"), e);
 +        }
 +      }
 +    }
 +
 +    /**
 +     * Gets the hostname used by clients to lookup the Locator.
 +     * 
 +     * @return a String indicating the hostname Locator binding used in client lookups.
 +     * @see #setHostnameForClients(String)
 +     */
 +    public String getHostnameForClients() {
 +      return this.hostnameForClients;
 +    }
 +
 +    /**
 +     * Sets the hostname used by clients to lookup the Locator.
 +     * 
 +     * @param hostnameForClients a String indicating the hostname Locator binding used in client lookups.
 +     * @return this Builder instance.
 +     * @throws IllegalArgumentException if the hostname was not specified (is blank or empty), such as when the
 +     * --hostname-for-clients command-line option may have been specified without any argument.
 +     * @see #getHostnameForClients()
 +     */
 +    public Builder setHostnameForClients(final String hostnameForClients) {
 +      if (StringUtils.isEmpty(StringUtils.trim(hostnameForClients))) {
 +        throw new IllegalArgumentException(
 +          LocalizedStrings.LocatorLauncher_Builder_INVALID_HOSTNAME_FOR_CLIENTS_ERROR_MESSAGE.toLocalizedString());
 +      }
 +      this.hostnameForClients = hostnameForClients;
 +      return this;
 +    }
 +
 +    /**
 +     * Gets the member name of this Locator in GemFire.
 +     * 
 +     * @return a String indicating the member name of this Locator in GemFire.
 +     * @see #setMemberName(String)
 +     */
 +    public String getMemberName() {
 +      return this.memberName;
 +    }
 +
 +    /**
 +     * Sets the member name of the Locator in GemFire.
 +     * 
 +     * @param memberName a String indicating the member name of this Locator in GemFire.
 +     * @return this Builder instance.
 +     * @throws IllegalArgumentException if the member name is invalid.
 +     * @see #getMemberName()
 +     */
 +    public Builder setMemberName(final String memberName) {
 +      if (StringUtils.isEmpty(StringUtils.trim(memberName))) {
 +        throw new IllegalArgumentException(LocalizedStrings.Launcher_Builder_MEMBER_NAME_ERROR_MESSAGE
 +          .toLocalizedString("Locator"));
 +      }
 +      this.memberName = memberName;
 +      return this;
 +    }
 +
 +    /**
 +     * Gets the process ID (PID) of the running Locator indicated by the user as an argument to the LocatorLauncher.
 +     * This PID is used by the Locator launcher to determine the Locator's status, or invoke shutdown on the Locator.
 +     * 
 +     * @return a user specified Integer value indicating the process ID of the running Locator.
 +     * @see #setPid(Integer)
 +     */
 +    public Integer getPid() {
 +      return this.pid;
 +    }
 +
 +    /**
 +     * Sets the process ID (PID) of the running Locator indicated by the user as an argument to the LocatorLauncher.
 +     * This PID will be used by the Locator launcher to determine the Locator's status, or invoke shutdown on
 +     * the Locator.
 +     * 
 +     * @param pid a user specified Integer value indicating the process ID of the running Locator.
 +     * @return this Builder instance.
 +     * @throws IllegalArgumentException if the process ID (PID) is not valid (greater than zero if not null).
 +     * @see #getPid()
 +     */
 +    public Builder setPid(final Integer pid) {
 +      if (pid != null && pid < 0) {
 +        throw new IllegalArgumentException(LocalizedStrings.Launcher_Builder_PID_ERROR_MESSAGE.toLocalizedString());
 +      }
 +      this.pid = pid;
 +      return this;
 +    }
 +
 +    boolean isPortSpecified() {
 +      return (this.port != null);
 +    }
 +    
 +    /**
 +     * Gets the port number used by the Locator to listen for client requests.  If the port was not specified, then the
 +     * default Locator port (10334) is returned.
 +     * 
 +     * @return the specified Locator port or the default port if unspecified.
 +     * @see #setPort(Integer)
 +     */
 +    public Integer getPort() {
 +      return ObjectUtils.defaultIfNull(port, getDefaultLocatorPort());
 +    }
 +
 +    /**
 +     * Sets the port number used by the Locator to listen for client requests.  The port number must be between 1 and
 +     * 65535 inclusive.
 +     * 
 +     * @param port an Integer value indicating the port used by the Locator to listen for client requests.
 +     * @return this Builder instance.
 +     * @throws IllegalArgumentException if the port number is not valid.
 +     * @see #getPort()
 +     */
 +    public Builder setPort(final Integer port) {
 +      // NOTE if the user were to specify a port number of 0, then java.net.ServerSocket will pick an ephemeral port
 +      // to bind the socket, which we do not want.
 +      if (port != null && (port < 0 || port > 65535)) {
 +        throw new IllegalArgumentException(LocalizedStrings.Launcher_Builder_INVALID_PORT_ERROR_MESSAGE
 +          .toLocalizedString("Locator"));
 +      }
 +      this.port = port;
 +      return this;
 +    }
 +
 +    /**
 +     * Determines whether the new instance of LocatorLauncher will redirect
 +     * output to system logs when starting a Locator.
 +     * 
 +     * @return a boolean value indicating if output will be redirected to system 
 +     * logs when starting a Locator
 +     * 
 +     * @see #setRedirectOutput(Boolean)
 +     */
 +    public Boolean getRedirectOutput() {
 +      return this.redirectOutput;
 +    }
 +
 +    /**
 +     * Determines whether redirecting of output has been enabled.
 +     * 
 +     * @return a boolean indicating if redirecting of output was enabled.
 +     */
 +    private boolean isRedirectingOutput() {
 +      return Boolean.TRUE.equals(getRedirectOutput());
 +    }
 +
 +    /**
 +     * Sets whether the new instance of LocatorLauncher will redirect output to system logs when starting a Locator.
 +     * 
 +     * @param redirectOutput a boolean value indicating if output will be redirected to system logs when starting
 +     * a Locator.
 +     * @return this Builder instance.
 +     * @see #getRedirectOutput()
 +     */
 +    public Builder setRedirectOutput(final Boolean redirectOutput) {
 +      this.redirectOutput = redirectOutput;
 +      return this;
 +    }
 +
 +    boolean isWorkingDirectorySpecified() {
 +      return !StringUtils.isBlank(this.workingDirectory);
 +    }
 +
 +    /**
 +     * Gets the working directory pathname in which the Locator will be ran.  If the directory is unspecified,
 +     * then working directory defaults to the current directory.
 +     * 
 +     * @return a String indicating the working directory pathname.
 +     * @see #setWorkingDirectory(String)
 +     */
 +    public String getWorkingDirectory() {
 +      return IOUtils.tryGetCanonicalPathElseGetAbsolutePath(
 +        new File(StringUtils.defaultIfBlank(this.workingDirectory, DEFAULT_WORKING_DIRECTORY)));
 +    }
 +
 +    /**
 +     * Sets the working directory in which the Locator will be ran.  This also the directory in which all Locator
 +     * files (such as log and license files) will be written.  If the directory is unspecified, then the working
 +     * directory defaults to the current directory.
 +     * 
 +     * @param workingDirectory a String indicating the pathname of the directory in which the Locator will be ran.
 +     * @return this Builder instance.
 +     * @throws IllegalArgumentException wrapping a FileNotFoundException if the working directory pathname cannot be
 +     * found.
 +     * @see #getWorkingDirectory()
 +     * @see java.io.FileNotFoundException
 +     */
 +    public Builder setWorkingDirectory(final String workingDirectory) {
 +      if (!new File(StringUtils.defaultIfBlank(workingDirectory, DEFAULT_WORKING_DIRECTORY)).isDirectory()) {
 +        throw new IllegalArgumentException(
 +          LocalizedStrings.Launcher_Builder_WORKING_DIRECTORY_NOT_FOUND_ERROR_MESSAGE.toLocalizedString("Locator"),
 +            new FileNotFoundException(workingDirectory));
 +      }
 +      this.workingDirectory = workingDirectory;
 +      return this;
 +    }
 +
 +    /**
 +     * Sets a GemFire Distributed System Property.
 +     *
 +     * @param propertyName a String indicating the name of the GemFire Distributed System property.
 +     * @param propertyValue a String value for the GemFire Distributed System property.
 +     * @return this Builder instance.
 +     */
 +    public Builder set(final String propertyName, final String propertyValue) {
 +      this.distributedSystemProperties.setProperty(propertyName, propertyValue);
 +      return this;
 +    }
 +
 +    /**
 +     * Validates the configuration settings and properties of this Builder, ensuring that all invariants have been met.
 +     * Currently, the only invariant constraining the Builder is that the user must specify the member name for the
 +     * Locator in the GemFire distributed system as a command-line argument, or by setting the memberName property
 +     * programmatically using the corresponding setter method.  If the member name is not given, then the user must
 +     * have specified the pathname to the gemfire.properties file before validate is called.  It is then assumed,
 +     * but not further validated, that the user has specified the Locator's member name in the properties file.
 +     * 
 +     * @throws IllegalStateException if the Builder is not properly configured.
 +     */
 +    protected void validate() {
 +      if (!isHelping()) {
 +        validateOnStart();
 +        validateOnStatus();
 +        validateOnStop();
 +        // no validation for 'version' required
 +      }
 +    }
 +
 +    /**
 +     * Validates the arguments passed to the Builder when the 'start' command has been issued.
 +     * 
 +     * @see com.gemstone.gemfire.distributed.LocatorLauncher.Command#START
 +     */
 +    protected void validateOnStart() {
 +      if (Command.START.equals(getCommand())) {
 +        if (StringUtils.isBlank(getMemberName())
 +          && !isSet(System.getProperties(), DistributionConfig.GEMFIRE_PREFIX + DistributionConfig.NAME_NAME)
 +          && !isSet(getDistributedSystemProperties(), DistributionConfig.NAME_NAME)
 +          && !isSet(loadGemFireProperties(DistributedSystem.getPropertyFileURL()), DistributionConfig.NAME_NAME))
 +        {
 +          throw new IllegalStateException(LocalizedStrings.Launcher_Builder_MEMBER_NAME_VALIDATION_ERROR_MESSAGE
 +            .toLocalizedString("Locator"));
 +        }
 +
 +        if (!SystemUtils.CURRENT_DIRECTORY.equals(getWorkingDirectory())) {
 +          throw new IllegalStateException(LocalizedStrings.Launcher_Builder_WORKING_DIRECTORY_OPTION_NOT_VALID_ERROR_MESSAGE
 +            .toLocalizedString("Locator"));
 +        }
 +      }
 +    }
 +
 +    /**
 +     * Validates the arguments passed to the Builder when the 'status' command has been issued.
 +     * 
 +     * @see com.gemstone.gemfire.distributed.LocatorLauncher.Command#STATUS
 +     */
 +    protected void validateOnStatus() {
 +      if (Command.STATUS.equals(getCommand())) {
 +      }
 +    }
 +
 +    /**
 +     * Validates the arguments passed to the Builder when the 'stop' command has been issued.
 +     * 
 +     * @see com.gemstone.gemfire.distributed.LocatorLauncher.Command#STOP
 +     */
 +    protected void validateOnStop() {
 +      if (Command.STOP.equals(getCommand())) {
 +      }
 +    }
 +
 +    /**
 +     * Validates the Builder configuration settings and then constructs an instance of the LocatorLauncher class
 +     * to invoke operations on a GemFire Locator.
 +     * 
 +     * @return a newly constructed instance of LocatorLauncher configured with this Builder.
 +     * @see #validate()
 +     * @see com.gemstone.gemfire.distributed.LocatorLauncher
 +     */
 +    public LocatorLauncher build() {
 +      validate();
 +      return new LocatorLauncher(this);
 +    }
 +  }
 +
 +  /**
 +   * An enumerated type representing valid commands to the Locator launcher.
 +   */
 +  public static enum Command {
 +    START("start", "bind-address", "hostname-for-clients", "port", "force", "debug", "help"),
 +    STATUS("status", "bind-address", "port", "member", "pid", "dir", "debug", "help"),
 +    STOP("stop", "member", "pid", "dir", "debug", "help"),
 +    VERSION("version"),
 +    UNSPECIFIED("unspecified");
 +
 +    private final List<String> options;
 +
 +    private final String name;
 +
 +    Command(final String name, final String... options) {
 +      assert !StringUtils.isBlank(name) : "The name of the locator launcher command must be specified!";
 +      this.name = name;
 +      this.options = (options != null ? Collections.unmodifiableList(Arrays.asList(options))
 +        : Collections.<String>emptyList());
 +    }
 +
 +    /**
 +     * Determines whether the specified name refers to a valid Locator launcher command, as defined by this
 +     * enumerated type.
 +     * 
 +     * @param name a String value indicating the potential name of a Locator launcher command.
 +     * @return a boolean indicating whether the specified name for a Locator launcher command is valid.
 +     */
 +    public static boolean isCommand(final String name) {
 +      return (valueOfName(name) != null);
 +    }
 +
 +    /**
 +     * Determines whether the given Locator launcher command has been properly specified.  The command is deemed
 +     * unspecified if the reference is null or the Command is UNSPECIFIED.
 +     * 
 +     * @param command the Locator launcher command.
 +     * @return a boolean value indicating whether the Locator launcher command is unspecified.
 +     * @see Command#UNSPECIFIED
 +     */
 +    public static boolean isUnspecified(final Command command) {
 +      return (command == null || command.isUnspecified());
 +    }
 +
 +    /**
 +     * Looks up a Locator launcher command by name.  The equality comparison on name is case-insensitive.
 +     * 
 +     * @param name a String value indicating the name of the Locator launcher command.
 +     * @return an enumerated type representing the command name or null if the no such command with the specified name
 +     * exists.
 +     */
 +    public static Command valueOfName(final String name) {
 +      for (Command command : values()) {
 +        if (command.getName().equalsIgnoreCase(name)) {
 +          return command;
 +        }
 +      }
 +
 +      return null;
 +    }
 +
 +    /**
 +     * Gets the name of the Locator launcher command.
 +     * 
 +     * @return a String value indicating the name of the Locator launcher command.
 +     */
 +    public String getName() {
 +      return this.name;
 +    }
 +
 +    /**
 +     * Gets a set of valid options that can be used with the Locator launcher command when used from the command-line.
 +     * 
 +     * @return a Set of Strings indicating the names of the options available to the Locator launcher command.
 +     */
 +    public List<String> getOptions() {
 +      return this.options;
 +    }
 +
 +    /**
 +     * Determines whether this Locator launcher command has the specified command-line option.
 +     * 
 +     * @param option a String indicating the name of the command-line option to this command.
 +     * @return a boolean value indicating whether this command has the specified named command-line option.
 +     */
 +    public boolean hasOption(final String option) {
 +      return getOptions().contains(StringUtils.toLowerCase(option));
 +    }
 +
 +    /**
 +     * Convenience method for determining whether this is the UNSPECIFIED Locator launcher command.
 +     * 
 +     * @return a boolean indicating if this command is UNSPECIFIED.
 +     * @see #UNSPECIFIED
 +     */
 +    public boolean isUnspecified() {
 +      return UNSPECIFIED.equals(this);
 +    }
 +
 +    /**
 +     * Gets the String representation of this Locator launcher command.
 +     * 
 +     * @return a String value representing this Locator launcher command.
 +     */
 +    @Override
 +    public String toString() {
 +      return getName();
 +    }
 +  }
 +
 +  /**
 +   * The LocatorState is an immutable type representing the state of the specified Locator at any given moment in time.
 +   * The state of the Locator is assessed at the exact moment an instance of this class is constructed.
 +   * 
 +   * @see com.gemstone.gemfire.distributed.AbstractLauncher.ServiceState
 +   */
 +  public static final class LocatorState extends ServiceState<String> {
 +
 +    /**
 +     * Unmarshals a LocatorState instance from the JSON String.
 +     * 
 +     * @return a LocatorState value unmarshalled from the JSON String.
 +     */
 +    public static LocatorState fromJson(final String json) {
 +      try {
 +        final GfJsonObject gfJsonObject = new GfJsonObject(json);
 +        
 +        final Status status = Status.valueOfDescription(gfJsonObject.getString(JSON_STATUS));
 +
 +        final List<String> jvmArguments = Arrays.asList(GfJsonArray.toStringArray(
 +          gfJsonObject.getJSONArray(JSON_JVMARGUMENTS)));
 +
 +        return new LocatorState(status, 
 +            gfJsonObject.getString(JSON_STATUSMESSAGE),
 +            gfJsonObject.getLong(JSON_TIMESTAMP),
 +            gfJsonObject.getString(JSON_LOCATION),
 +            gfJsonObject.getInt(JSON_PID),
 +            gfJsonObject.getLong(JSON_UPTIME),
 +            gfJsonObject.getString(JSON_WORKINGDIRECTORY),
 +            jvmArguments,
 +            gfJsonObject.getString(JSON_CLASSPATH),
 +            gfJsonObject.getString(JSON_GEMFIREVERSION),
 +            gfJsonObject.getString(JSON_JAVAVERSION),
 +            gfJsonObject.getString(JSON_LOGFILE),
 +            gfJsonObject.getString(JSON_HOST),
 +            gfJsonObject.getString(JSON_PORT),
 +            gfJsonObject.getString(JSON_MEMBERNAME));
 +      }
 +      catch (GfJsonException e) {
 +        throw new IllegalArgumentException("Unable to create LocatorStatus from JSON: ".concat(json), e);
 +      }
 +    }
 +
 +    public LocatorState(final LocatorLauncher launcher, final Status status) {
 +      // if status is NOT_RESPONDING then this is executing inside the JVM asking for he status; pid etc will be set according to the caller's JVM instead
 +      this(status,
 +        launcher.statusMessage,
 +        System.currentTimeMillis(),
 +        launcher.getId(),
 +        identifyPid(),
 +        ManagementFactory.getRuntimeMXBean().getUptime(),
 +        launcher.getWorkingDirectory(),
 +        ManagementFactory.getRuntimeMXBean().getInputArguments(),
 +        System.getProperty("java.class.path"),
 +        GemFireVersion.getGemFireVersion(),
 +        System.getProperty("java.version"),
 +        getLogFileCanonicalPath(launcher),
 +        launcher.getBindAddressAsString(),
 +        launcher.getPortAsString(),
 +        launcher.getMemberName());
 +    }
 +    
 +    public LocatorState(final LocatorLauncher launcher, final Status status, final String errorMessage) {
 +      this(status, // status
 +          errorMessage, // statusMessage
 +          System.currentTimeMillis(), // timestamp
 +          null, // locatorLocation
 +          null, // pid
 +          0L, // uptime
 +          launcher.getWorkingDirectory(), // workingDirectory
 +          Collections.<String>emptyList(), // jvmArguments
 +          null, // classpath
 +          GemFireVersion.getGemFireVersion(), // gemfireVersion
 +          null, // javaVersion
 +          null, // logFile
 +          null, // host
 +          null, // port
 +          null);// memberName
 +    }
 +    
 +    private static String getBindAddressAsString(LocatorLauncher launcher) {
 +      if (InternalLocator.hasLocator()) {
 +        final InternalLocator locator = InternalLocator.getLocator();
 +        final InetAddress bindAddress = locator.getBindAddress();
 +        if (bindAddress != null) {
 +          if (StringUtils.isBlank(bindAddress.getHostAddress())) {
 +            return bindAddress.getHostAddress();
 +          }
 +        }
 +      }
 +      return launcher.getBindAddressAsString();
 +    }
 +
 +    private static String getLogFileCanonicalPath(LocatorLauncher launcher) {
 +      if (InternalLocator.hasLocator()) {
 +        final InternalLocator locator = InternalLocator.getLocator();
 +        final File logFile = locator.getLogFile();
 +
 +        if (logFile != null && logFile.isFile()) {
 +          final String logFileCanonicalPath = IOUtils.tryGetCanonicalPathElseGetAbsolutePath(logFile);
 +          if (!StringUtils.isBlank(logFileCanonicalPath)) { // this is probably not need but a safe check none-the-less.
 +            return logFileCanonicalPath;
 +          }
 +        }
 +      }
 +      return launcher.getLogFileCanonicalPath();
 +    }
 +
 +    private static String getPortAsString(LocatorLauncher launcher) {
 +      if (InternalLocator.hasLocator()) {
 +        final InternalLocator locator = InternalLocator.getLocator();
 +        final String portAsString =  String.valueOf(locator.getPort());
 +        if (!StringUtils.isBlank(portAsString)) {
 +          return portAsString;
 +        }
 +      }
 +      return launcher.getPortAsString();
 +    }
 +
 +    protected LocatorState(final Status status,
 +                           final String statusMessage,
 +                           final long timestamp,
 +                           final String locatorLocation,
 +                           final Integer pid,
 +                           final Long uptime,
 +                           final String workingDirectory,
 +                           final List<String> jvmArguments,
 +                           final String classpath,
 +                           final String gemfireVersion,
 +                           final String javaVersion,
 +                           final String logFile,
 +                           final String host,
 +                           final String port,
 +                           final String memberName)
 +    {
 +      super(status, statusMessage, timestamp, locatorLocation, pid, uptime, workingDirectory, jvmArguments, classpath,
 +        gemfireVersion, javaVersion, logFile, host, port, memberName);
 +    }
 +
 +    private LocatorState(final LocatorLauncher launcher, final Status status, final LocatorStatusResponse response) {
 +      this(status,
 +        launcher.statusMessage,
 +        System.currentTimeMillis(),
 +        launcher.getId(),
 +        response.getPid(),
 +        response.getUptime(),
 +        response.getWorkingDirectory(),
 +        response.getJvmArgs(),
 +        response.getClasspath(),
 +        response.getGemFireVersion(),
 +        response.getJavaVersion(),
 +        response.getLogFile(),
 +        response.getHost(),
 +        String.valueOf(response.getPort()),
 +        response.getName());
 +    }
 +
 +    @Override
 +    protected String getServiceName() {
 +      return LOCATOR_SERVICE_NAME;
 +    }
 +  }
 +
 +}


[27/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/ExecuteRegionFunction66.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/ExecuteRegionFunction66.java
index 6d9ca49,0000000..018d05b
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/ExecuteRegionFunction66.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/ExecuteRegionFunction66.java
@@@ -1,436 -1,0 +1,435 @@@
 +/*
 + * 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.tier.sockets.command;
 +
 +import java.io.IOException;
 +import java.util.HashSet;
 +import java.util.Set;
 +
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.client.internal.ExecuteFunctionHelper;
- import com.gemstone.gemfire.cache.client.internal.Op;
 +import com.gemstone.gemfire.cache.execute.Function;
 +import com.gemstone.gemfire.cache.execute.FunctionException;
 +import com.gemstone.gemfire.cache.execute.FunctionInvocationTargetException;
 +import com.gemstone.gemfire.cache.execute.FunctionService;
 +import com.gemstone.gemfire.cache.operations.ExecuteFunctionOperationContext;
- import com.gemstone.gemfire.i18n.LogWriterI18n;
++import com.gemstone.gemfire.cache.query.QueryInvocationTargetException;
 +import com.gemstone.gemfire.internal.Version;
 +import com.gemstone.gemfire.internal.cache.DistributedRegion;
 +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
 +import com.gemstone.gemfire.internal.cache.PartitionedRegion;
 +import com.gemstone.gemfire.internal.cache.execute.AbstractExecution;
 +import com.gemstone.gemfire.internal.cache.execute.DistributedRegionFunctionExecutor;
 +import com.gemstone.gemfire.internal.cache.execute.InternalFunctionInvocationTargetException;
 +import com.gemstone.gemfire.internal.cache.execute.MemberMappedArgument;
 +import com.gemstone.gemfire.internal.cache.execute.PartitionedRegionFunctionExecutor;
 +import com.gemstone.gemfire.internal.cache.execute.ServerToClientFunctionResultSender;
 +import com.gemstone.gemfire.internal.cache.execute.ServerToClientFunctionResultSender65;
 +import com.gemstone.gemfire.internal.cache.tier.CachedRegionHelper;
 +import com.gemstone.gemfire.internal.cache.tier.Command;
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.BaseCommand;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.ChunkedMessage;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.HandShake;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Part;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.ServerConnection;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.logging.log4j.LocalizedMessage;
 +import com.gemstone.gemfire.internal.security.AuthorizeRequest;
 +
 +/**
 + * 
 + * @author kbachhav
 + *  @since 6.6
 + */
 +public class ExecuteRegionFunction66 extends BaseCommand {
 +  
 +  private final static ExecuteRegionFunction66 singleton = new ExecuteRegionFunction66();
 +
 +  public static Command getCommand() {
 +    return singleton;
 +  }
 +
 +  private ExecuteRegionFunction66() {
 +  }
 +
 +  @Override
 +  public void cmdExecute(Message msg, ServerConnection servConn, long start)
 +      throws IOException {
 +    String regionName = null;
 +    Object function = null;
 +    Object args = null;
 +    MemberMappedArgument memberMappedArg = null;
 +    final boolean isBucketsAsFilter ;
 +    final byte isReExecute ;
 +    Set<Object> filter = null;
 +    byte hasResult = 0;
 +    int removedNodesSize = 0;
 +    Set<Object> removedNodesSet = null;
 +    int filterSize = 0, partNumber = 0;
 +    CachedRegionHelper crHelper = servConn.getCachedRegionHelper();
 +    byte functionState = 0;
 +    int functionTimeout = GemFireCacheImpl.DEFAULT_CLIENT_FUNCTION_TIMEOUT;
 +    try {
 +      byte[] bytes = msg.getPart(0).getSerializedForm();
 +      functionState = bytes[0];
 +      if (bytes.length >= 5 && servConn.getClientVersion().ordinal() >= Version.GFE_8009.ordinal()) {
 +        functionTimeout = Part.decodeInt(bytes, 1);
 +      }
 +      if (functionState != 1) {
 +        hasResult = (byte)((functionState & 2) - 1);
 +      }
 +      else {
 +        hasResult = functionState;
 +      }
 +      if (hasResult == 1) {
 +        servConn.setAsTrue(REQUIRES_RESPONSE);
 +        servConn.setAsTrue(REQUIRES_CHUNKED_RESPONSE);
 +      }
 +      regionName = msg.getPart(1).getString();
 +      function = msg.getPart(2).getStringOrObject();
 +      args = msg.getPart(3).getObject();
 +      Part part = msg.getPart(4);
 +      if (part != null) {
 +        Object obj = part.getObject();
 +        if (obj instanceof MemberMappedArgument) {
 +          memberMappedArg = (MemberMappedArgument)obj;
 +        }
 +      }
 +      byte[] flags =msg.getPart(5).getSerializedForm();
 +      if(servConn.getClientVersion().ordinal() > Version.GFE_81.ordinal()) {
 +        isBucketsAsFilter = (flags[0] & ExecuteFunctionHelper.BUCKETS_AS_FILTER_MASK) != 0 ; 
 +        isReExecute = (flags[0] & ExecuteFunctionHelper.IS_REXECUTE_MASK) != 0 ? (byte)1 : 0;
 +      }else {
 +        isReExecute = flags[0];
 +        isBucketsAsFilter = false;
 +      }
 +      filterSize = msg.getPart(6).getInt();
 +      if (filterSize != 0) {
 +        filter = new HashSet<Object>();
 +        partNumber = 7;
 +        for (int i = 0; i < filterSize; i++) {
 +          filter.add(msg.getPart(partNumber + i).getStringOrObject());
 +        }
 +      }
 +
 +      partNumber = 7 + filterSize;
 +      removedNodesSize = msg.getPart(partNumber).getInt();
 +
 +      if (removedNodesSize != 0) {
 +        removedNodesSet = new HashSet<Object>();
 +        partNumber = partNumber + 1;
 +
 +        for (int i = 0; i < removedNodesSize; i++) {
 +          removedNodesSet.add(msg.getPart(partNumber + i).getStringOrObject());
 +        }
 +      }
 +
 +    }
 +    catch (ClassNotFoundException exception) {
 +      logger.warn(LocalizedMessage.create(LocalizedStrings.ExecuteRegionFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, function), exception);
 +      if (hasResult == 1) {
 +        writeChunkedException(msg, exception, false, servConn);
 +      }
 +      else {
 +        writeException(msg, exception, false, servConn);
 +      }
 +      servConn.setAsTrue(RESPONDED);
 +      return;
 +    }
 +    if (function == null || regionName == null) {
 +      String message = null;
 +      if (function == null) {
 +        message = LocalizedStrings.ExecuteRegionFunction_THE_INPUT_0_FOR_THE_EXECUTE_FUNCTION_REQUEST_IS_NULL
 +            .toLocalizedString("function");
 +      }
 +      if (regionName == null) {
 +        message = LocalizedStrings.ExecuteRegionFunction_THE_INPUT_0_FOR_THE_EXECUTE_FUNCTION_REQUEST_IS_NULL
 +            .toLocalizedString("region");
 +      }
 +      logger.warn("{}: {}", servConn.getName(), message);
 +      sendError(hasResult, msg, message, servConn);
 +      return;
 +    }
 +    else {
 +      Region region = crHelper.getRegion(regionName);
 +      if (region == null) {
 +        String message = LocalizedStrings.ExecuteRegionFunction_THE_REGION_NAMED_0_WAS_NOT_FOUND_DURING_EXECUTE_FUNCTION_REQUEST
 +            .toLocalizedString(regionName);
 +        logger.warn("{}: {}", servConn.getName(), message);
 +        sendError(hasResult, msg, message, servConn);
 +        return;
 +      }
 +      HandShake handShake = (HandShake)servConn.getHandshake();
 +      int earlierClientReadTimeout = handShake.getClientReadTimeout();
 +      handShake.setClientReadTimeout(functionTimeout);
 +      ServerToClientFunctionResultSender resultSender = null;
 +      Function functionObject = null;
 +      try {
 +        if (function instanceof String) {
 +          functionObject = FunctionService.getFunction((String)function);
 +          if (functionObject == null) {
 +            String message = LocalizedStrings.ExecuteRegionFunction_THE_FUNCTION_0_HAS_NOT_BEEN_REGISTERED
 +                .toLocalizedString(function);
 +            logger.warn("{}: {}", servConn.getName(), message);
 +            sendError(hasResult, msg, message, servConn);
 +            return;
 +          }
 +          else {
 +            byte functionStateOnServerSide = AbstractExecution
 +                .getFunctionState(functionObject.isHA(), functionObject
 +                    .hasResult(), functionObject.optimizeForWrite());
 +            if (logger.isDebugEnabled()) {
 +              logger.debug("Function State on server side: {} on client: {}", functionStateOnServerSide, functionState);
 +            }
 +            if (functionStateOnServerSide != functionState) {
 +              String message = LocalizedStrings.FunctionService_FUNCTION_ATTRIBUTE_MISMATCH_CLIENT_SERVER
 +                  .toLocalizedString(function);
 +              logger.warn("{}: {}", servConn.getName(), message);
 +              sendError(hasResult, msg, message, servConn);
 +              return;
 +            }
 +          }
 +        }
 +        else {
 +          functionObject = (Function)function;
 +        }
 +        // check if the caller is authorized to do this operation on server
 +        AuthorizeRequest authzRequest = servConn.getAuthzRequest();
 +        final String functionName = functionObject.getId();
 +        final String regionPath = region.getFullPath();
 +        ExecuteFunctionOperationContext executeContext = null;
 +        if (authzRequest != null) {
 +          executeContext = authzRequest.executeFunctionAuthorize(functionName,
 +              regionPath, filter, args, functionObject.optimizeForWrite());
 +        }
 +
 +        // Construct execution
 +        AbstractExecution execution = (AbstractExecution)FunctionService
 +            .onRegion(region);
 +        ChunkedMessage m = servConn.getFunctionResponseMessage();
 +        m.setTransactionId(msg.getTransactionId());
 +        resultSender = new ServerToClientFunctionResultSender65(m,
 +            MessageType.EXECUTE_REGION_FUNCTION_RESULT, servConn,
 +            functionObject, executeContext);
 +
 +        if (execution instanceof PartitionedRegionFunctionExecutor) {
 +          if((hasResult == 1) && filter!= null &&filter.size() == 1) {
 +            ServerConnection.executeFunctionOnLocalNodeOnly((byte)1);
 +          }
 +          execution = new PartitionedRegionFunctionExecutor(
 +              (PartitionedRegion)region, filter, args, memberMappedArg,
 +              resultSender, removedNodesSet, isBucketsAsFilter);
 +        }
 +        else {
 +          execution = new DistributedRegionFunctionExecutor(
 +              (DistributedRegion)region, filter, args, memberMappedArg,
 +              resultSender);
 +        }
 +        if (isReExecute == 1) {
 +          execution = execution.setIsReExecute();
 +        }
 +        if (logger.isDebugEnabled()) {
 +          logger.debug("Executing Function: {} on Server: {} with Execution: {} functionState={} reExecute={} hasResult={}", functionObject.getId(), servConn, execution, functionState, isReExecute, hasResult);
 +        }
 +        if (hasResult == 1) {
 +          if (function instanceof String) {
 +            switch (functionState) {
 +              case AbstractExecution.NO_HA_HASRESULT_NO_OPTIMIZEFORWRITE:
 +                execution.execute((String)function, true, false, false)
 +                    .getResult();
 +                break;
 +              case AbstractExecution.HA_HASRESULT_NO_OPTIMIZEFORWRITE:
 +                execution.execute((String)function, true, true, false)
 +                    .getResult();
 +                break;
 +              case AbstractExecution.HA_HASRESULT_OPTIMIZEFORWRITE:
 +                execution.execute((String)function, true, true, true)
 +                    .getResult();
 +                break;
 +              case AbstractExecution.NO_HA_HASRESULT_OPTIMIZEFORWRITE:
 +                execution.execute((String)function, true, false, true)
 +                    .getResult();
 +                break;
 +            }
 +          }
 +          else {
 +            execution.execute(functionObject).getResult();
 +          }
 +        }
 +        else {
 +          if (function instanceof String) {
 +            switch (functionState) {
 +              case AbstractExecution.NO_HA_NO_HASRESULT_NO_OPTIMIZEFORWRITE:
 +                execution.execute((String)function, false, false, false);
 +                break;
 +              case AbstractExecution.NO_HA_NO_HASRESULT_OPTIMIZEFORWRITE:
 +                execution.execute((String)function, false, false, true);
 +                break;
 +            }
 +          }
 +          else {
 +            execution.execute(functionObject);
 +          }
 +          writeReply(msg, servConn);
 +        }
 +      }
 +      catch (IOException ioe) {
 +        logger.warn(LocalizedMessage.create(LocalizedStrings.ExecuteRegionFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, function), ioe);
 +        final String message = LocalizedStrings.ExecuteRegionFunction_SERVER_COULD_NOT_SEND_THE_REPLY
 +            .toLocalizedString();
 +        sendException(hasResult, msg, message, servConn, ioe);
 +      }
 +      catch (FunctionException fe) {
 +        String message = fe.getMessage();
- 
-         if (fe.getCause() instanceof FunctionInvocationTargetException) {
-           if (fe.getCause() instanceof InternalFunctionInvocationTargetException) {
++        Object cause = fe.getCause();
++        if (cause instanceof FunctionInvocationTargetException || cause instanceof QueryInvocationTargetException) {
++          if (cause instanceof InternalFunctionInvocationTargetException) {
 +            // Fix for #44709: User should not be aware of
 +            // InternalFunctionInvocationTargetException. No instance of
 +            // InternalFunctionInvocationTargetException is giving useful
 +            // information to user to take any corrective action hence logging
 +            // this at fine level logging
 +            // 1> When bucket is moved
 +            // 2> Incase of HA FucntionInvocationTargetException thrown. Since
 +            // it is HA, fucntion will be reexecuted on right node
 +            // 3> Multiple target nodes found for single hop operation
 +            // 4> in case of HA member departed
 +            if (logger.isDebugEnabled()) {
 +              logger.debug(LocalizedMessage.create(LocalizedStrings.ExecuteFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, new Object[] { function }), fe);
 +            }
 +          }
 +          else if (functionObject.isHA()) {
 +            logger.warn(LocalizedMessage.create(LocalizedStrings.ExecuteRegionFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, function + " :" + message));
 +          }
 +          else {
 +            logger.warn(LocalizedMessage.create(LocalizedStrings.ExecuteRegionFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, function), fe);
 +          }
 +
 +          resultSender.setException(fe);
 +        }
 +        else {
 +          logger.warn(LocalizedMessage.create(LocalizedStrings.ExecuteRegionFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, function), fe);
 +          sendException(hasResult, msg, message, servConn, fe);
 +        }
 +
 +      }
 +      catch (Exception e) {
 +        logger.warn(LocalizedMessage.create(LocalizedStrings.ExecuteRegionFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, function), e);
 +        String message = e.getMessage();
 +        sendException(hasResult, msg, message, servConn, e);
 +      }
 +
 +      finally {
 +        handShake.setClientReadTimeout(earlierClientReadTimeout);
 +        ServerConnection.executeFunctionOnLocalNodeOnly((byte)0);
 +      }
 +    }
 +  }
 +
 +  private void sendException(byte hasResult, Message msg, String message,
 +      ServerConnection servConn, Throwable e) throws IOException {
 +    synchronized (msg) {
 +      if (hasResult == 1) {
 +        writeFunctionResponseException(msg, MessageType.EXCEPTION, message,
 +            servConn, e);
 +      }
 +      else {
 +        writeException(msg, e, false, servConn);
 +      }
 +      servConn.setAsTrue(RESPONDED);
 +    }
 +  }
 +
 +  private void sendError(byte hasResult, Message msg, String message,
 +      ServerConnection servConn) throws IOException {
 +    synchronized (msg) {
 +      if (hasResult == 1) {
 +        writeFunctionResponseError(msg,
 +            MessageType.EXECUTE_REGION_FUNCTION_ERROR, message, servConn);
 +      }
 +      else {
 +        writeErrorResponse(msg, MessageType.EXECUTE_REGION_FUNCTION_ERROR,
 +            message, servConn);
 +      }
 +      servConn.setAsTrue(RESPONDED);
 +    }
 +  }
 +
 +  protected static void writeFunctionResponseException(Message origMsg,
 +      int messageType, String message, ServerConnection servConn, Throwable e)
 +      throws IOException {
 +    ChunkedMessage functionResponseMsg = servConn.getFunctionResponseMessage();
 +    ChunkedMessage chunkedResponseMsg = servConn.getChunkedResponseMessage();
 +    int numParts = 0;
 +    if (functionResponseMsg.headerHasBeenSent()) {
 +      if (e instanceof FunctionException
 +          && e.getCause() instanceof InternalFunctionInvocationTargetException) {
 +        functionResponseMsg.setNumberOfParts(3);
 +        functionResponseMsg.addObjPart(e);
 +        functionResponseMsg.addStringPart(BaseCommand.getExceptionTrace(e));
 +        InternalFunctionInvocationTargetException fe = (InternalFunctionInvocationTargetException)e
 +            .getCause();
 +        functionResponseMsg.addObjPart(fe.getFailedNodeSet());
 +        numParts = 3;
 +      }
 +      else {
 +        functionResponseMsg.setNumberOfParts(2);
 +        functionResponseMsg.addObjPart(e);
 +        functionResponseMsg.addStringPart(BaseCommand.getExceptionTrace(e));
 +        numParts = 2;
 +      }
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}: Sending exception chunk while reply in progress: ", servConn.getName(), e);
 +      }
 +      functionResponseMsg.setServerConnection(servConn);
 +      functionResponseMsg.setLastChunkAndNumParts(true, numParts);
 +      // functionResponseMsg.setLastChunk(true);
 +      functionResponseMsg.sendChunk(servConn);
 +    }
 +    else {
 +      chunkedResponseMsg.setMessageType(messageType);
 +      chunkedResponseMsg.setTransactionId(origMsg.getTransactionId());
 +      chunkedResponseMsg.sendHeader();
 +      if (e instanceof FunctionException
 +          && e.getCause() instanceof InternalFunctionInvocationTargetException) {
 +        chunkedResponseMsg.setNumberOfParts(3);
 +        chunkedResponseMsg.addObjPart(e);
 +        chunkedResponseMsg.addStringPart(BaseCommand.getExceptionTrace(e));
 +        InternalFunctionInvocationTargetException fe = (InternalFunctionInvocationTargetException)e
 +            .getCause();
 +        chunkedResponseMsg.addObjPart(fe.getFailedNodeSet());
 +        numParts = 3;
 +      }
 +      else {
 +        chunkedResponseMsg.setNumberOfParts(2);
 +        chunkedResponseMsg.addObjPart(e);
 +        chunkedResponseMsg.addStringPart(BaseCommand.getExceptionTrace(e));
 +        numParts = 2;
 +      }
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}: Sending exception chunk: ", servConn.getName(), e);
 +      }
 +      chunkedResponseMsg.setServerConnection(servConn);
 +      chunkedResponseMsg.setLastChunkAndNumParts(true, numParts);
 +      chunkedResponseMsg.sendChunk(servConn);
 +    }
 +  }
 +}
 +

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/xmlcache/AsyncEventQueueCreation.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/cache/xmlcache/AsyncEventQueueCreation.java
index 60afc14,0000000..77f9596
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/xmlcache/AsyncEventQueueCreation.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/xmlcache/AsyncEventQueueCreation.java
@@@ -1,222 -1,0 +1,223 @@@
 +/*
 + * 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 java.util.ArrayList;
 +import java.util.List;
 +
 +import com.gemstone.gemfire.cache.asyncqueue.AsyncEventListener;
 +import com.gemstone.gemfire.cache.asyncqueue.AsyncEventQueue;
 +import com.gemstone.gemfire.cache.wan.GatewayEventFilter;
 +import com.gemstone.gemfire.cache.wan.GatewayEventSubstitutionFilter;
 +import com.gemstone.gemfire.cache.wan.GatewaySender.OrderPolicy;
 +import com.gemstone.gemfire.internal.cache.wan.GatewaySenderAttributes;
 +
 +public class AsyncEventQueueCreation implements AsyncEventQueue {
 +
 +  private String id = null;
 +  private List<GatewayEventFilter> gatewayEventFilters = new ArrayList<GatewayEventFilter>();
 +  private GatewayEventSubstitutionFilter gatewayEventSubstitutionFilter = null;
 +  private AsyncEventListener asyncEventListener = null;
 +  private int batchSize = 0;
 +  private int batchTimeInterval = 0;
 +  private boolean isBatchConflationEnabled = false;
 +  private boolean isPersistent = false;
 +  private String diskStoreName = null;
 +  private boolean isDiskSynchronous = false;
 +  private int maxQueueMemory = 0;
 +  private boolean isParallel = false;
 +  private boolean isBucketSorted = false;
 +  private boolean isHDFSQueue = false;
 +  private int dispatcherThreads = 1;
 +  private OrderPolicy orderPolicy = OrderPolicy.KEY;
 +  
 +  public AsyncEventQueueCreation() {
 +  }
 +  
 +  public AsyncEventQueueCreation(String id, GatewaySenderAttributes senderAttrs, AsyncEventListener eventListener) {
 +    this.id = id;
 +    this.batchSize = senderAttrs.batchSize;
 +    this.batchTimeInterval = senderAttrs.batchTimeInterval;
 +    this.isBatchConflationEnabled = senderAttrs.isBatchConflationEnabled;
 +    this.isPersistent = senderAttrs.isPersistenceEnabled;
 +    this.diskStoreName = senderAttrs.diskStoreName;
 +    this.isDiskSynchronous = senderAttrs.isDiskSynchronous;
 +    this.maxQueueMemory = senderAttrs.maximumQueueMemory;
 +    this.isParallel = senderAttrs.isParallel;
 +    this.dispatcherThreads = senderAttrs.dispatcherThreads;
 +    this.orderPolicy = senderAttrs.policy;
 +    this.asyncEventListener = eventListener;
 +    this.isBucketSorted = senderAttrs.isBucketSorted; 
 +    this.isHDFSQueue = senderAttrs.isHDFSQueue;
++    this.gatewayEventSubstitutionFilter = senderAttrs.eventSubstitutionFilter;
 +  }
 +  
 +  @Override
 +  public AsyncEventListener getAsyncEventListener() {
 +    return this.asyncEventListener;
 +  }
 +  
 +  public void setAsyncEventListener(AsyncEventListener eventListener) {
 +    this.asyncEventListener = eventListener;
 +  }
 +  
 +  public void addGatewayEventFilter(
 +      GatewayEventFilter filter) {
 +    this.gatewayEventFilters.add(filter);
 +  }
 +
 +  public List<GatewayEventFilter> getGatewayEventFilters() {
 +    return this.gatewayEventFilters;
 +  }
 +
 +  public GatewayEventSubstitutionFilter getGatewayEventSubstitutionFilter() {
 +    return this.gatewayEventSubstitutionFilter;
 +  }
 +  
 +  public void setGatewayEventSubstitutionFilter(GatewayEventSubstitutionFilter filter) {
 +    this.gatewayEventSubstitutionFilter = filter;
 +  }
 +
 +  @Override
 +  public int getBatchSize() {
 +    return this.batchSize;
 +  }
 +  
 +  public void setBatchSize(int batchSize) {
 +    this.batchSize = batchSize;
 +  }
 +  
 +  @Override
 +  public int getBatchTimeInterval() {
 +    return this.batchTimeInterval;
 +  }
 +  
 +  public void setBatchTimeInterval(int batchTimeInterval) {
 +    this.batchTimeInterval = batchTimeInterval;
 +  }
 +  
 +  @Override
 +  public boolean isBatchConflationEnabled() {
 +    return this.isBatchConflationEnabled;
 +  }
 +  
 +  public void setBatchConflationEnabled(boolean batchConflationEnabled) {
 +    this.isBatchConflationEnabled = batchConflationEnabled;
 +  }
 +
 +  @Override
 +  public String getDiskStoreName() {
 +    return this.diskStoreName;
 +  }
 +  
 +  public void setDiskStoreName(String diskStore) {
 +    this.diskStoreName = diskStore;
 +  }
 +  
 +  @Override
 +  public boolean isDiskSynchronous() {
 +    return this.isDiskSynchronous;
 +  }
 +  
 +  public void setDiskSynchronous(boolean diskSynchronous) {
 +    this.isDiskSynchronous = diskSynchronous;
 +  }
 +
 +  @Override
 +  public String getId() {
 +    return this.id;
 +  }
 +  
 +  public void setId(String id) {
 +    this.id = id;
 +  }
 +  
 +  @Override
 +  public int getMaximumQueueMemory() {
 +    return this.maxQueueMemory;
 +  }
 +  
 +  public void setMaximumQueueMemory(int maxQueueMemory) {
 +    this.maxQueueMemory = maxQueueMemory;
 +  }
 +  
 +  @Override
 +  public boolean isPersistent() {
 +    return this.isPersistent;
 +  }
 +  
 +  public void setPersistent(boolean isPersistent) {
 +    this.isPersistent = isPersistent;
 +  }
 +
 +  public void setParallel(boolean isParallel) {
 +    this.isParallel = isParallel;
 +  }
 +  
 +  @Override
 +  public int getDispatcherThreads() {
 +    return this.dispatcherThreads;
 +  }
 +  
 +  public void setDispatcherThreads(int numThreads) {
 +    this.dispatcherThreads = numThreads;
 +  }
 +  
 +  @Override
 +  public OrderPolicy getOrderPolicy() {
 +    return this.orderPolicy;
 +  }
 +  
 +  public void setOrderPolicy(OrderPolicy policy) {
 +    this.orderPolicy = policy;
 +  }
 +  
 +  @Override
 +  public boolean isPrimary() {
 +    return true;
 +  }
 +  
 +  @Override
 +  public int size() {
 +    return 0;
 +  }
 + 
 +  public void start() {};
 +  public void stop() {};
 +  public void destroy() {};
 +  public void pause() {};
 +  public void resume() {}
 +
 +  public boolean isParallel() {
 +    return this.isParallel;
 +  }
 +
 +  public boolean isBucketSorted() {
 +    return this.isBucketSorted;
 +  }
 +  
 +  public void setBucketSorted(boolean isBucketSorted) {
 +    this.isBucketSorted = isBucketSorted;
 +  }
 +  public boolean isHDFSQueue() {
 +    return this.isHDFSQueue;
 +  }
 +  
 +  public void setIsHDFSQueue(boolean isHDFSQueue) {
 +    this.isHDFSQueue = isHDFSQueue;
 +  }
 +}


[17/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache/management/MemoryThresholdsOffHeapDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache/management/MemoryThresholdsOffHeapDUnitTest.java
index 532a239,0000000..3cf7b09
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache/management/MemoryThresholdsOffHeapDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache/management/MemoryThresholdsOffHeapDUnitTest.java
@@@ -1,1826 -1,0 +1,1826 @@@
 +/*
 + * 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.cache.management;
 +
 +import java.util.ArrayList;
 +import java.util.HashMap;
 +import java.util.HashSet;
 +import java.util.Iterator;
 +import java.util.List;
 +import java.util.Map;
 +import java.util.Properties;
 +import java.util.Set;
 +import java.util.concurrent.atomic.AtomicInteger;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.AttributesMutator;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheLoader;
 +import com.gemstone.gemfire.cache.CacheLoaderException;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.LoaderHelper;
 +import com.gemstone.gemfire.cache.LowMemoryException;
 +import com.gemstone.gemfire.cache.PartitionAttributesFactory;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionShortcut;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.client.PoolFactory;
 +import com.gemstone.gemfire.cache.client.PoolManager;
 +import com.gemstone.gemfire.cache.client.ServerOperationException;
 +import com.gemstone.gemfire.cache.control.ResourceManager;
 +import com.gemstone.gemfire.cache.management.MemoryThresholdsDUnitTest.Range;
 +import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.cache30.ClientServerTestCase;
 +import com.gemstone.gemfire.cache30.CacheSerializableRunnable;
 +import com.gemstone.gemfire.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
 +import com.gemstone.gemfire.internal.AvailablePortHelper;
 +import com.gemstone.gemfire.internal.cache.DistributedRegion;
 +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
 +import com.gemstone.gemfire.internal.cache.LocalRegion;
 +import com.gemstone.gemfire.internal.cache.PartitionedRegion;
 +import com.gemstone.gemfire.internal.cache.PartitionedRegionHelper;
 +import com.gemstone.gemfire.internal.cache.ProxyBucketRegion;
 +import com.gemstone.gemfire.internal.cache.control.InternalResourceManager;
 +import com.gemstone.gemfire.internal.cache.control.InternalResourceManager.ResourceType;
 +import com.gemstone.gemfire.internal.cache.control.MemoryEvent;
 +import com.gemstone.gemfire.internal.cache.control.MemoryThresholds.MemoryState;
 +import com.gemstone.gemfire.internal.cache.control.OffHeapMemoryMonitor;
 +import com.gemstone.gemfire.internal.cache.control.OffHeapMemoryMonitor.OffHeapMemoryMonitorObserver;
 +import com.gemstone.gemfire.internal.cache.control.ResourceAdvisor;
 +import com.gemstone.gemfire.internal.cache.control.ResourceListener;
 +import com.gemstone.gemfire.internal.cache.control.TestMemoryThresholdListener;
 +import com.gemstone.gemfire.internal.cache.partitioned.RegionAdvisor;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.test.dunit.Assert;
 +import com.gemstone.gemfire.test.dunit.AsyncInvocation;
 +import com.gemstone.gemfire.test.dunit.DistributedTestCase;
 +import com.gemstone.gemfire.test.dunit.DistributedTestUtils;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.IgnoredException;
 +import com.gemstone.gemfire.test.dunit.Invoke;
 +import com.gemstone.gemfire.test.dunit.LogWriterUtils;
 +import com.gemstone.gemfire.test.dunit.NetworkUtils;
 +import com.gemstone.gemfire.test.dunit.SerializableCallable;
 +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;
 +
 +/**
 + * Tests the Off-Heap Memory thresholds of {@link ResourceManager}
 + * 
 + * @author David Hoots
 + * @since 9.0
 + */
 +public class MemoryThresholdsOffHeapDUnitTest extends ClientServerTestCase {
 +  private static final long serialVersionUID = -684231183212051910L;
 +
 +  final String expectedEx = LocalizedStrings.MemoryMonitor_MEMBER_ABOVE_CRITICAL_THRESHOLD.getRawText().replaceAll("\\{[0-9]+\\}",
 +      ".*?");
 +  final String addExpectedExString = "<ExpectedException action=add>" + this.expectedEx + "</ExpectedException>";
 +  final String removeExpectedExString = "<ExpectedException action=remove>" + this.expectedEx + "</ExpectedException>";
 +  final String expectedBelow = LocalizedStrings.MemoryMonitor_MEMBER_BELOW_CRITICAL_THRESHOLD.getRawText().replaceAll(
 +      "\\{[0-9]+\\}", ".*?");
 +  final String addExpectedBelow = "<ExpectedException action=add>" + this.expectedBelow + "</ExpectedException>";
 +  final String removeExpectedBelow = "<ExpectedException action=remove>" + this.expectedBelow + "</ExpectedException>";
 +
 +  public MemoryThresholdsOffHeapDUnitTest(String name) {
 +    super(name);
 +  }
 +  
 +  
 +
 +  @Override
 +  public void setUp() throws Exception {
 +    IgnoredException.addIgnoredException(expectedEx);
 +    IgnoredException.addIgnoredException(expectedBelow);
 +  }
 +
 +
 +
 +  @Override
 +  protected void preTearDownClientServerTestCase() throws Exception {
 +    Invoke.invokeInEveryVM(this.resetResourceManager);
 +  }
 +
 +  private SerializableCallable resetResourceManager = new SerializableCallable() {
 +    public Object call() throws Exception {
 +      InternalResourceManager irm = ((GemFireCacheImpl)getCache()).getResourceManager();
 +      Set<ResourceListener> listeners = irm.getResourceListeners(ResourceType.OFFHEAP_MEMORY);
 +      Iterator<ResourceListener> it = listeners.iterator();
 +      while (it.hasNext()) {
 +        ResourceListener<MemoryEvent> l = it.next();
 +        if (l instanceof TestMemoryThresholdListener) {
 +          ((TestMemoryThresholdListener)l).resetThresholdCalls();
 +        }
 +      }
 +      return null;
 +    }
 +  };
 +
 +  /**
 +   * Make sure appropriate events are delivered when moving between states.
 +   * 
 +   * @throws Exception
 +   */
 +  public void testEventDelivery() throws Exception {
 +    final Host host = Host.getHost(0);
 +    final VM server1 = host.getVM(0);
 +    final VM server2 = host.getVM(1);
 +    
 +    final int[] ports = AvailablePortHelper.getRandomAvailableTCPPorts(2);
 +    final int port1 = ports[0];
 +    final int port2 = ports[1];
 +    final String regionName = "offHeapEventDelivery";
 +
 +    startCacheServer(server1, port1, 0f, 0f,
 +        regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +    startCacheServer(server2, port2, 70f, 90f,
 +        regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +
 +    registerTestMemoryThresholdListener(server1);
 +    registerTestMemoryThresholdListener(server2);
 +    
 +    // NORMAL -> EVICTION
 +    setUsageAboveEvictionThreshold(server2, regionName);
 +    verifyListenerValue(server1, MemoryState.EVICTION, 1, true);
 +    verifyListenerValue(server2, MemoryState.EVICTION, 1, true);
 +    
 +    // EVICTION -> CRITICAL
 +    setUsageAboveCriticalThreshold(server2, regionName);
 +    verifyListenerValue(server1, MemoryState.CRITICAL, 1, true);
 +    verifyListenerValue(server2, MemoryState.CRITICAL, 1, true);
 +    verifyListenerValue(server1, MemoryState.EVICTION, 2, true);
 +    verifyListenerValue(server2, MemoryState.EVICTION, 2, true);
 +    
 +    // CRITICAL -> CRITICAL
 +    server2.invoke(new SerializableCallable() {
 +      private static final long serialVersionUID = 1L;
 +
 +      @Override
 +      public Object call() throws Exception {
 +        getCache().getLogger().fine(MemoryThresholdsOffHeapDUnitTest.this.addExpectedExString);
 +        getRootRegion().getSubregion(regionName).destroy("oh3");
 +        getCache().getLogger().fine(MemoryThresholdsOffHeapDUnitTest.this.removeExpectedExString);
 +        return null;
 +      }
 +    });
 +    verifyListenerValue(server1, MemoryState.CRITICAL, 1, true);
 +    verifyListenerValue(server2, MemoryState.CRITICAL, 1, true);
 +    verifyListenerValue(server1, MemoryState.EVICTION, 2, true);
 +    verifyListenerValue(server2, MemoryState.EVICTION, 2, true);
 +    
 +    // CRITICAL -> EVICTION
 +    server2.invoke(new SerializableCallable() {
 +      private static final long serialVersionUID = 1L;
 +
 +      @Override
 +      public Object call() throws Exception {
 +        getCache().getLogger().fine(MemoryThresholdsOffHeapDUnitTest.this.addExpectedBelow);
 +        getRootRegion().getSubregion(regionName).destroy("oh2");
 +        getCache().getLogger().fine(MemoryThresholdsOffHeapDUnitTest.this.removeExpectedBelow);
 +        return null;
 +      }
 +    });
 +    verifyListenerValue(server1, MemoryState.EVICTION, 3, true);
 +    verifyListenerValue(server2, MemoryState.EVICTION, 3, true);
 +
 +    // EVICTION -> EVICTION
 +    server2.invoke(new SerializableCallable() {
 +      private static final long serialVersionUID = 1L;
 +
 +      @Override
 +      public Object call() throws Exception {
 +        getRootRegion().getSubregion(regionName).put("oh6", new byte[20480]);
 +        return null;
 +      }
 +    });
 +    verifyListenerValue(server1, MemoryState.EVICTION, 3, true);
 +    verifyListenerValue(server2, MemoryState.EVICTION, 3, true);
 +
 +    // EVICTION -> NORMAL
 +    server2.invoke(new SerializableCallable() {
 +      private static final long serialVersionUID = 1L;
 +
 +      @Override
 +      public Object call() throws Exception {
 +        getRootRegion().getSubregion(regionName).destroy("oh4");
 +        return null;
 +      }
 +    });
 +
 +    verifyListenerValue(server1, MemoryState.CRITICAL, 1, true);
 +    verifyListenerValue(server1, MemoryState.EVICTION, 3, true);
 +    verifyListenerValue(server1, MemoryState.NORMAL, 1, true);
 +    
 +    verifyListenerValue(server2, MemoryState.CRITICAL, 1, true);
 +    verifyListenerValue(server2, MemoryState.EVICTION, 3, true);
 +    verifyListenerValue(server2, MemoryState.NORMAL, 1, true);
 +  }
 +  
 +  /**
 +   * test that disabling threshold does not cause remote event and
 +   * remote DISABLED events are delivered
 +   * @throws Exception
 +   */
 +  public void testDisabledThresholds() throws Exception {
 +    final Host host = Host.getHost(0);
 +    final VM server1 = host.getVM(0);
 +    final VM server2 = host.getVM(1);
 +    
 +    final String regionName = "offHeapDisabledThresholds";
 +
 +    //set port to 0 in-order for system to pickup a random port.
 +    startCacheServer(server1, 0, 0f, 0f,
 +        regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +    startCacheServer(server2, 0, 0f, 0f,
 +        regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +
 +    registerTestMemoryThresholdListener(server1);
 +    registerTestMemoryThresholdListener(server2);
 +    
 +    setUsageAboveEvictionThreshold(server1, regionName);
 +    verifyListenerValue(server1, MemoryState.EVICTION, 0, true);
 +    verifyListenerValue(server2, MemoryState.EVICTION, 0, true);
 +
 +    setThresholds(server1, 70f, 0f);
 +    verifyListenerValue(server1, MemoryState.EVICTION, 1, true);
 +    verifyListenerValue(server2, MemoryState.EVICTION, 1, true);
 +
 +    setUsageAboveCriticalThreshold(server1, regionName);
 +    verifyListenerValue(server1, MemoryState.CRITICAL, 0, true);
 +    verifyListenerValue(server2, MemoryState.CRITICAL, 0, true);
 +
 +    setThresholds(server1, 0f, 0f);
 +    verifyListenerValue(server1, MemoryState.EVICTION_DISABLED, 1, true);
 +    verifyListenerValue(server2, MemoryState.EVICTION_DISABLED, 1, true);
 +
 +    setThresholds(server1, 0f, 90f);
 +    verifyListenerValue(server1, MemoryState.CRITICAL, 1, true);
 +    verifyListenerValue(server2, MemoryState.CRITICAL, 1, true);
 +
 +    //verify that stats on server2 are not changed by events on server1
 +    server2.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        InternalResourceManager irm = ((GemFireCacheImpl)getCache()).getResourceManager();
 +        assertEquals(0, irm.getStats().getOffHeapEvictionStartEvents());
 +        assertEquals(0, irm.getStats().getOffHeapCriticalEvents());
 +        assertEquals(0, irm.getStats().getOffHeapCriticalThreshold());
 +        assertEquals(0, irm.getStats().getOffHeapEvictionThreshold());
 +        return null;
 +      }
 +    });
 +  }
 +
 +  private void setUsageAboveCriticalThreshold(final VM vm, final String regionName) {
 +    vm.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        getCache().getLoggerI18n().fine(addExpectedExString);
 +        Region region = getRootRegion().getSubregion(regionName);
 +        if (!region.containsKey("oh1")) {
 +          region.put("oh5", new byte[954204]);
 +        } else {
 +          region.put("oh5", new byte[122880]);
 +        }
 +        getCache().getLoggerI18n().fine(removeExpectedExString);
 +        return null;
 +      }
 +    });
 +  }
 +
 +  private void setUsageAboveEvictionThreshold(final VM vm, final String regionName) {
 +    vm.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        getCache().getLoggerI18n().fine(addExpectedBelow);
 +        Region region = getRootRegion().getSubregion(regionName);
 +        region.put("oh1", new byte[245760]);
 +        region.put("oh2", new byte[184320]);
 +        region.put("oh3", new byte[33488]);
 +        region.put("oh4", new byte[378160]);
 +        getCache().getLoggerI18n().fine(removeExpectedBelow);
 +        return null;
 +      }
 +    });
 +  }
 +  
 +  private void setUsageBelowEviction(final VM vm, final String regionName) {
 +    vm.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        getCache().getLoggerI18n().fine(addExpectedBelow);
 +        Region region = getRootRegion().getSubregion(regionName);
 +        region.remove("oh1");
 +        region.remove("oh2");
 +        region.remove("oh3");
 +        region.remove("oh4");
 +        region.remove("oh5");
 +        getCache().getLoggerI18n().fine(removeExpectedBelow);
 +        return null;
 +      }
 +    });
 +  }
 +  
 +  private void setThresholds(VM server, final float evictionThreshold,
 +      final float criticalThreshold) {
 +    
 +    server.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        ResourceManager irm = getCache().getResourceManager();
 +        irm.setCriticalOffHeapPercentage(criticalThreshold);
 +        irm.setEvictionOffHeapPercentage(evictionThreshold);
 +        return null;
 +      }
 +    });
 +  }
 +  
 +  /**
 +   * test that puts in a client are rejected when a remote VM crosses
 +   * critical threshold
 +   * @throws Exception
 +   */
 +  public void testDistributedRegionRemoteClientPutRejection() throws Exception {
 +    final Host host = Host.getHost(0);
 +    final VM server1 = host.getVM(0);
 +    final VM server2 = host.getVM(1);
 +    final VM client = host.getVM(2);
 +
 +    final int[] ports = AvailablePortHelper.getRandomAvailableTCPPorts(2);
 +    final int port1 = ports[0];
 +    final int port2 = ports[1];
 +    final String regionName = "offHeapDRRemoteClientPutReject";
 +
 +    startCacheServer(server1, port1, 0f, 0f,
 +        regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +    startCacheServer(server2, port2, 0f, 90f,
 +        regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +
 +    startClient(client, server1, port1, regionName);
 +
 +    registerTestMemoryThresholdListener(server1);
 +    registerTestMemoryThresholdListener(server2);
 +
 +    doPuts(client, regionName, false/*catchRejectedException*/,
 +        false/*catchLowMemoryException*/);
 +    doPutAlls(client, regionName, false/*catchRejectedException*/,
 +        false/*catchLowMemoryException*/, Range.DEFAULT);
 +
 +    //make server2 critical
 +    setUsageAboveCriticalThreshold(server2, regionName);
 +
 +    verifyListenerValue(server1, MemoryState.CRITICAL, 1, true);
 +    verifyListenerValue(server2, MemoryState.CRITICAL, 1, true);
 +
 +    //make sure that client puts are rejected
 +    doPuts(client, regionName, true/*catchRejectedException*/,
 +        false/*catchLowMemoryException*/);
 +    doPutAlls(client, regionName, true/*catchRejectedException*/,
 +        false/*catchLowMemoryException*/, new Range(Range.DEFAULT, Range.DEFAULT.width()+1));
 +    
 +    setUsageBelowEviction(server2, regionName);
 +  }
 +
 +  public void testDistributedRegionRemotePutRejectionLocalDestroy() throws Exception {
 +    doDistributedRegionRemotePutRejection(true, false);
 +  }
 +  
 +  public void testDistributedRegionRemotePutRejectionCacheClose() throws Exception {
 +    doDistributedRegionRemotePutRejection(false, true);
 +  }
 +  
 +  public void testDistributedRegionRemotePutRejectionBelowThreshold() throws Exception {
 +    doDistributedRegionRemotePutRejection(false, false);
 +  }
 +  
 +  public void testGettersAndSetters() {
 +    getSystem(getOffHeapProperties());
 +    ResourceManager rm = getCache().getResourceManager();
 +    assertEquals(0.0f, rm.getCriticalOffHeapPercentage());
 +    assertEquals(0.0f, rm.getEvictionOffHeapPercentage());
 +    
 +    rm.setEvictionOffHeapPercentage(50);
 +    rm.setCriticalOffHeapPercentage(90);
 +    
 +    // verify
 +    assertEquals(50.0f, rm.getEvictionOffHeapPercentage());
 +    assertEquals(90.0f, rm.getCriticalOffHeapPercentage());
 +    
 +    getCache().createRegionFactory(RegionShortcut.REPLICATE_HEAP_LRU).create(getName());
 +    
 +    assertEquals(50.0f, rm.getEvictionOffHeapPercentage());
 +    assertEquals(90.0f, rm.getCriticalOffHeapPercentage());
 +  }
 +  
 +  /**
 +   * test that puts in a server are rejected when a remote VM crosses
 +   * critical threshold
 +   * @throws Exception
 +   */
 +  private void doDistributedRegionRemotePutRejection(boolean localDestroy, boolean cacheClose) throws Exception {
 +    final Host host = Host.getHost(0);
 +    final VM server1 = host.getVM(0);
 +    final VM server2 = host.getVM(1);
 +
 +    final String regionName = "offHeapDRRemotePutRejection";
 +
 +    //set port to 0 in-order for system to pickup a random port.
 +    startCacheServer(server1, 0, 0f, 0f,
 +        regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +    startCacheServer(server2, 0, 0f, 90f,
 +        regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +
 +    registerTestMemoryThresholdListener(server1);
 +    registerTestMemoryThresholdListener(server2);
 +
 +    doPuts(server1, regionName, false/*catchRejectedException*/,
 +        false/*catchLowMemoryException*/);
 +    doPutAlls(server1, regionName, false/*catchRejectedException*/,
 +        false/*catchLowMemoryException*/, Range.DEFAULT);
 +
 +    //make server2 critical
 +    setUsageAboveCriticalThreshold(server2, regionName);
 +
 +    verifyListenerValue(server1, MemoryState.CRITICAL, 1, true);
 +    verifyListenerValue(server2, MemoryState.CRITICAL, 1, true);
 +
 +    //make sure that local server1 puts are rejected
 +    doPuts(server1, regionName, false/*catchRejectedException*/,
 +        true/*catchLowMemoryException*/);
 +    Range r1 = new Range(Range.DEFAULT, Range.DEFAULT.width()+1);
 +    doPutAlls(server1, regionName, false/*catchRejectedException*/,
 +        true/*catchLowMemoryException*/, r1);
 +
 +    if (localDestroy) {
 +    //local destroy the region on sick member
 +    server2.invoke(new SerializableCallable("local destroy") {
 +      public Object call() throws Exception {
 +        Region r = getRootRegion().getSubregion(regionName);
 +        r.localDestroyRegion();
 +        return null;
 +      }
 +    });
 +    } else if (cacheClose) {
 +      server2.invoke(new SerializableCallable() {
 +        public Object call() throws Exception {
 +          getCache().close();
 +          return null;
 +        }
 +      });
 +    } else {
 +      setUsageBelowEviction(server2, regionName);
 +    }
 +    
 +    //wait for remote region destroyed message to be processed
 +    server1.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        WaitCriterion wc = new WaitCriterion() {
 +          public String description() {
 +            return "remote localRegionDestroyed message not received";
 +          }
 +          public boolean done() {
 +            DistributedRegion dr = (DistributedRegion)getRootRegion().
 +                                                getSubregion(regionName);
 +            return dr.getMemoryThresholdReachedMembers().size() == 0;
 +          }
 +        };
 +        Wait.waitForCriterion(wc, 10000, 10, true);
 +        return null;
 +      }
 +    });
 +
 +    //make sure puts succeed
 +    doPuts(server1, regionName, false/*catchRejectedException*/,
 +        false/*catchLowMemoryException*/);
 +    Range r2 = new Range(r1, r1.width()+1);
 +    doPutAlls(server1, regionName, false/*catchRejectedException*/,
 +        false/*catchLowMemoryException*/, r2);
 +  }
 +
 +  /**
 +   * Test that DistributedRegion cacheLoade and netLoad are passed through to the 
 +   * calling thread if the local VM is in a critical state.  Once the VM has moved
 +   * to a safe state then test that they are allowed.
 +   * @throws Exception
 +   */
 +  public void testDRLoadRejection() throws Exception {
 +    final Host host = Host.getHost(0);
 +    final VM replicate1 = host.getVM(1);
 +    final VM replicate2 = host.getVM(2);
 +    final String rName = getUniqueName();
 +    
 +    // Make sure the desired VMs will have a fresh DS.
-     AsyncInvocation d1 = replicate1.invokeAsync(DistributedTestCase.class, "disconnectFromDS");
-     AsyncInvocation d2 = replicate2.invokeAsync(DistributedTestCase.class, "disconnectFromDS");
++    AsyncInvocation d1 = replicate1.invokeAsync(() -> DistributedTestCase.disconnectFromDS());
++    AsyncInvocation d2 = replicate2.invokeAsync(() -> DistributedTestCase.disconnectFromDS());
 +    d1.join();
 +    assertFalse(d1.exceptionOccurred());
 +    d2.join();
 +    assertFalse(d2.exceptionOccurred());
 +    CacheSerializableRunnable establishConnectivity = new CacheSerializableRunnable("establishcConnectivity") {
 +      @SuppressWarnings("synthetic-access")
 +      @Override
 +      public void run2() throws CacheException {
 +        getSystem(getOffHeapProperties());
 +      }
 +    };
 +    replicate1.invoke(establishConnectivity);
 +    replicate2.invoke(establishConnectivity);
 +    
 +    CacheSerializableRunnable createRegion = new CacheSerializableRunnable("create DistributedRegion") {
 +      @Override
 +      public void run2() throws CacheException {
 +        // Assert some level of connectivity
 +        InternalDistributedSystem ds = getSystem(getOffHeapProperties());
 +        assertTrue(ds.getDistributionManager().getNormalDistributionManagerIds().size() >= 1);
 +
 +        InternalResourceManager irm = (InternalResourceManager)getCache().getResourceManager();
 +        irm.setCriticalOffHeapPercentage(90f);
 +        AttributesFactory af = new AttributesFactory();
 +        af.setScope(Scope.DISTRIBUTED_ACK);
 +        af.setDataPolicy(DataPolicy.REPLICATE);
 +        af.setOffHeap(true);
 +        Region region = getCache().createRegion(rName, af.create());
 +      }
 +    };
 +    replicate1.invoke(createRegion);
 +    replicate2.invoke(createRegion);
 +    
 +    replicate1.invoke(addExpectedException);
 +    replicate2.invoke(addExpectedException);
 +    
 +    final Integer expected = (Integer)replicate1.invoke(new SerializableCallable("test Local DistributedRegion Load") {
 +      public Object call() throws Exception {
 +        final DistributedRegion r = (DistributedRegion) getCache().getRegion(rName);
 +        AttributesMutator<Integer, String> am = r.getAttributesMutator();
 +        am.setCacheLoader(new CacheLoader<Integer, String>() {
 +          final AtomicInteger numLoaderInvocations = new AtomicInteger(0);
 +          public String load(LoaderHelper<Integer, String> helper) throws CacheLoaderException {
 +            Integer expectedInvocations = (Integer)helper.getArgument();
 +            final int actualInvocations = this.numLoaderInvocations.getAndIncrement();
 +            if (expectedInvocations.intValue() != actualInvocations) {
 +              throw new CacheLoaderException("Expected " + expectedInvocations 
 +                  + " invocations, actual is " + actualInvocations);
 +            }
 +            return helper.getKey().toString();
 +          }
 +          public void close() {}
 +        });
 +
 +        int expectedInvocations = 0;
 +        final OffHeapMemoryMonitor ohmm = ((InternalResourceManager)getCache().getResourceManager()).getOffHeapMonitor();
 +        assertFalse(ohmm.getState().isCritical());
 +        {
 +          Integer k = new Integer(1);
 +          assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
 +        }
 +
 +        r.put("oh1", new byte[838860]);
 +        r.put("oh3", new byte[157287]);
 +        
 +        WaitCriterion wc = new WaitCriterion() {
 +          public String description() {
 +            return "expected region " + r + " to set memoryThreshold";
 +          }
 +          public boolean done() {
 +            return r.memoryThresholdReached.get();
 +          }
 +        };
 +        Wait.waitForCriterion(wc, 30*1000, 10, true);
 +        {
 +          Integer k = new Integer(2); 
 +          assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
 +        }
 +        
 +        r.destroy("oh3");
 +        wc = new WaitCriterion() {
 +          public String description() {
 +            return "expected region " + r + " to unset memoryThreshold";
 +          }
 +          public boolean done() {
 +            return !r.memoryThresholdReached.get();
 +          }
 +        };
 +        Wait.waitForCriterion(wc, 30*1000, 10, true);
 +        {
 +          Integer k = new Integer(3);
 +          assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
 +        }
 +        return new Integer(expectedInvocations);
 +      }
 +    });
 +
 +    final CacheSerializableRunnable validateData1 = new CacheSerializableRunnable("Validate data 1") {
 +      @Override
 +      public void run2() throws CacheException {
 +        Region r = getCache().getRegion(rName);
 +        Integer i1 = new Integer(1);
 +        assertTrue(r.containsKey(i1));
 +        assertNotNull(r.getEntry(i1));
 +        Integer i2 = new Integer(2);
 +        assertFalse(r.containsKey(i2));
 +        assertNull(r.getEntry(i2));
 +        Integer i3 = new Integer(3);
 +        assertTrue(r.containsKey(i3));
 +        assertNotNull(r.getEntry(i3));
 +      }
 +    };
 +    replicate1.invoke(validateData1);
 +    replicate2.invoke(validateData1);
 +    
 +    replicate2.invoke(new SerializableCallable("test DistributedRegion netLoad") {
 +      public Object call() throws Exception {
 +        final DistributedRegion r = (DistributedRegion) getCache().getRegion(rName);
 +        final OffHeapMemoryMonitor ohmm = ((InternalResourceManager)getCache().getResourceManager()).getOffHeapMonitor();
 +        assertFalse(ohmm.getState().isCritical());
 +        
 +        int expectedInvocations = expected.intValue();
 +        {
 +          Integer k = new Integer(4);
 +          assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
 +        }
 +        
 +        // Place in a critical state for the next test
 +        r.put("oh3", new byte[157287]);
 +        WaitCriterion wc = new WaitCriterion() {
 +          public String description() {
 +            return "expected region " + r + " to set memoryThreshold";
 +          }
 +          public boolean done() {
 +            return r.memoryThresholdReached.get();
 +          }
 +        };
 +        Wait.waitForCriterion(wc, 30*1000, 10, true);
 +        {
 +          Integer k = new Integer(5);
 +          assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
 +        }
 +
 +        r.destroy("oh3");
 +        wc = new WaitCriterion() {
 +          public String description() {
 +            return "expected region " + r + " to unset memoryThreshold";
 +          }
 +          public boolean done() {
 +            return !r.memoryThresholdReached.get();
 +          }
 +        };
 +        Wait.waitForCriterion(wc, 30*1000, 10, true);
 +        {
 +          Integer k = new Integer(6);
 +          assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
 +        }
 +        return new Integer(expectedInvocations);
 +      }
 +    });
 +    
 +    replicate1.invoke(removeExpectedException);
 +    replicate2.invoke(removeExpectedException);
 +    
 +    final CacheSerializableRunnable validateData2 = new CacheSerializableRunnable("Validate data 2") {
 +      @Override
 +      public void run2() throws CacheException {
 +        Region<Integer, String> r = getCache().getRegion(rName);
 +        Integer i4 = new Integer(4);
 +        assertTrue(r.containsKey(i4));
 +        assertNotNull(r.getEntry(i4));
 +        Integer i5 = new Integer(5);
 +        assertFalse(r.containsKey(i5));
 +        assertNull(r.getEntry(i5));
 +        Integer i6 = new Integer(6);
 +        assertTrue(r.containsKey(i6));
 +        assertNotNull(r.getEntry(i6));
 +      }
 +    };
 +    replicate1.invoke(validateData2);
 +    replicate2.invoke(validateData2);
 +  }
 +  
 +
 +  private SerializableRunnable addExpectedException = new SerializableRunnable
 +  ("addExpectedEx") {
 +    public void run() {
 +      getCache().getLoggerI18n().fine(addExpectedExString);
 +      getCache().getLoggerI18n().fine(addExpectedBelow);
 +    };
 +  };
 +  
 +  private SerializableRunnable removeExpectedException = new SerializableRunnable
 +  ("removeExpectedException") {
 +    public void run() {
 +      getCache().getLoggerI18n().fine(removeExpectedExString);
 +      getCache().getLoggerI18n().fine(removeExpectedBelow);
 +    };
 +  };
 +  
 +  public void testPR_RemotePutRejectionLocalDestroy() throws Exception {
 +    prRemotePutRejection(false, true, false);
 +  }
 +
 +  public void testPR_RemotePutRejectionCacheClose() throws Exception {
 +    prRemotePutRejection(true, false, false);
 +  }
 +
 +  public void testPR_RemotePutRejection() throws Exception {
 +    prRemotePutRejection(false, false, false);
 +  }
 +
 +  public void testPR_RemotePutRejectionLocalDestroyWithTx() throws Exception {
 +    prRemotePutRejection(false, true, true);
 +  }
 +
 +  public void testPR_RemotePutRejectionCacheCloseWithTx() throws Exception {
 +    prRemotePutRejection(true, false, true);
 +  }
 +
 +  public void testPR_RemotePutRejectionWithTx() throws Exception {
 +    prRemotePutRejection(false, false, true);
 +  }
 +
 +  private void prRemotePutRejection(boolean cacheClose, boolean localDestroy, final boolean useTx) throws Exception {
 +    final Host host = Host.getHost(0);
 +    final VM accessor = host.getVM(0);
 +    final VM servers[] = new VM[3];
 +    servers[0] = host.getVM(1);
 +    servers[1] = host.getVM(2);
 +    servers[2] = host.getVM(3);
 +
 +    final int[] ports = AvailablePortHelper.getRandomAvailableTCPPorts(3);
 +    final String regionName = "offHeapPRRemotePutRejection";
 +    final int redundancy = 1;
 +
 +    startCacheServer(servers[0], ports[0], 0f, 90f,
 +        regionName, true/*createPR*/, false/*notifyBySubscription*/, redundancy);
 +    startCacheServer(servers[1], ports[1], 0f, 90f,
 +        regionName, true/*createPR*/, false/*notifyBySubscription*/, redundancy);
 +    startCacheServer(servers[2], ports[2], 0f, 90f,
 +        regionName, true/*createPR*/, false/*notifyBySubscription*/, redundancy);
 +    accessor.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        getSystem(getOffHeapProperties());
 +        getCache();
 +        AttributesFactory factory = new AttributesFactory();        
 +        PartitionAttributesFactory paf = new PartitionAttributesFactory();
 +        paf.setRedundantCopies(redundancy);
 +        paf.setLocalMaxMemory(0);
 +        paf.setTotalNumBuckets(11);
 +        factory.setPartitionAttributes(paf.create());
 +        factory.setOffHeap(true);
 +        createRegion(regionName, factory.create());
 +        return null;
 +      }
 +    });
 +    
 +    doPuts(accessor, regionName, false, false);
 +    final Range r1 = Range.DEFAULT;
 +    doPutAlls(accessor, regionName, false, false, r1);
 +
 +    servers[0].invoke(addExpectedException);
 +    servers[1].invoke(addExpectedException);
 +    servers[2].invoke(addExpectedException);
 +    setUsageAboveCriticalThreshold(servers[0], regionName);
 +    
 +    final Set<InternalDistributedMember> criticalMembers = (Set) servers[0].invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        final PartitionedRegion pr = (PartitionedRegion)getRootRegion().getSubregion(regionName);
 +        final int hashKey = PartitionedRegionHelper.getHashKey(pr, null, "oh5", null, null);
 +        return pr.getRegionAdvisor().getBucketOwners(hashKey);
 +      }
 +    });
 +    
 +    accessor.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {     
 +        final PartitionedRegion pr = (PartitionedRegion)getRootRegion().getSubregion(regionName);
 +        WaitCriterion wc = new WaitCriterion() {
 +          public String description() {
 +            return "remote bucket not marked sick";
 +          }
 +          public boolean done() {
 +            boolean keyFoundOnSickMember = false;
 +            boolean caughtException = false;
 +            for (int i=0; i<20; i++) {
 +              Integer key = Integer.valueOf(i);
 +              int hKey = PartitionedRegionHelper.getHashKey(pr, null, key, null, null);
 +              Set<InternalDistributedMember> owners = pr.getRegionAdvisor().getBucketOwners(hKey);
 +              final boolean hasCriticalOwners = owners.removeAll(criticalMembers);
 +              if (hasCriticalOwners) {
 +                keyFoundOnSickMember = true;
 +                try {
 +                  if (useTx) getCache().getCacheTransactionManager().begin();
 +                  pr.getCache().getLogger().fine("SWAP:putting in tx:"+useTx);
 +                  pr.put(key, "value");
 +                  if (useTx) getCache().getCacheTransactionManager().commit();
 +                } catch (LowMemoryException ex) {
 +                  caughtException = true;
 +                  if (useTx) getCache().getCacheTransactionManager().rollback();
 +                }
 +              } else {
 +                //puts on healthy member should continue
 +                pr.put(key, "value");
 +              }
 +            }
 +            return keyFoundOnSickMember && caughtException;
 +          }
 +        };
 +        Wait.waitForCriterion(wc, 10000, 10, true);
 +        return null;
 +      }
 +    });
 +
 +    {
 +      Range r2 = new Range(r1, r1.width()+1);
 +      doPutAlls(accessor, regionName, false, true, r2);
 +    }
 +    
 +    // Find all VMs that have a critical region
 +    SerializableCallable getMyId = new SerializableCallable() {
 +      public Object call() throws Exception {        
 +        return ((GemFireCacheImpl)getCache()).getMyId();
 +      }
 +    };
 +    final Set<VM> criticalServers = new HashSet<VM>();
 +    for (final VM server : servers) {
 +      DistributedMember member = (DistributedMember) server.invoke(getMyId);
 +      if (criticalMembers.contains(member)) {
 +        criticalServers.add(server);
 +      }
 +    }
 +    
 +    if (localDestroy) {
 +    //local destroy the region on sick members
 +      for (final VM vm : criticalServers) {
 +        vm.invoke(new SerializableCallable("local destroy sick member") {
 +          public Object call() throws Exception {
 +            Region r = getRootRegion().getSubregion(regionName);
 +            LogWriterUtils.getLogWriter().info("PRLocalDestroy");
 +            r.localDestroyRegion();
 +            return null;
 +          }
 +        });
 +      }
 +    } else if (cacheClose) {
 +      // close cache on sick members
 +      for (final VM vm : criticalServers) {
 +        vm.invoke(new SerializableCallable("close cache sick member") {
 +          public Object call() throws Exception {
 +            getCache().close();
 +            return null;
 +          }
 +        });
 +      }
 +    } else {
 +      setUsageBelowEviction(servers[0], regionName);
 +      servers[0].invoke(removeExpectedException);
 +      servers[1].invoke(removeExpectedException);
 +      servers[2].invoke(removeExpectedException);
 +    }
 +    
 +    //do put all in a loop to allow distribution of message
 +    accessor.invoke(new SerializableCallable("Put in a loop") {
 +      public Object call() throws Exception {
 +        final Region r = getRootRegion().getSubregion(regionName);
 +        WaitCriterion wc = new WaitCriterion() {
 +          public String description() {            
 +            return "pr should have gone un-critical";
 +          }
 +          public boolean done() {
 +            boolean done = true;
 +            for (int i=0; i<20; i++) {
 +              try {
 +                r.put(i,"value");
 +              } catch (LowMemoryException e) {
 +                //expected
 +                done = false;
 +              }
 +            }            
 +            return done;
 +          }
 +        };
 +        Wait.waitForCriterion(wc, 10000, 10, true);
 +        return null;
 +      }
 +    });
 +    doPutAlls(accessor, regionName, false, false, r1);
 +  }
 +
 +  /**
 +   * Test that a Partitioned Region loader invocation is rejected
 +   * if the VM with the bucket is in a critical state.
 +   * @throws Exception
 +   */
 +  public void testPRLoadRejection() throws Exception {
 +    final Host host = Host.getHost(0);
 +    final VM accessor = host.getVM(1);
 +    final VM ds1 = host.getVM(2);
 +    final String rName = getUniqueName();
 +
 +    // Make sure the desired VMs will have a fresh DS.
-     AsyncInvocation d0 = accessor.invokeAsync(DistributedTestCase.class, "disconnectFromDS");
-     AsyncInvocation d1 = ds1.invokeAsync(DistributedTestCase.class, "disconnectFromDS");
++    AsyncInvocation d0 = accessor.invokeAsync(() -> DistributedTestCase.disconnectFromDS());
++    AsyncInvocation d1 = ds1.invokeAsync(() -> DistributedTestCase.disconnectFromDS());
 +    d0.join();
 +    assertFalse(d0.exceptionOccurred());
 +    d1.join();
 +    assertFalse(d1.exceptionOccurred());
 +    CacheSerializableRunnable establishConnectivity = new CacheSerializableRunnable("establishcConnectivity") {
 +      @Override
 +      public void run2() throws CacheException { getSystem();  }
 +    };
 +    ds1.invoke(establishConnectivity);
 +    accessor.invoke(establishConnectivity);
 +
 +    ds1.invoke(createPR(rName, false));
 +    accessor.invoke(createPR(rName, true));
 +    
 +    final AtomicInteger expectedInvocations = new AtomicInteger(0);
 +
 +    Integer ex = (Integer) accessor.invoke(new SerializableCallable("Invoke loader from accessor, non-critical") {
 +      public Object call() throws Exception {
 +        Region<Integer, String> r = getCache().getRegion(rName);
 +        Integer k = new Integer(1);
 +        Integer expectedInvocations0 = new Integer(expectedInvocations.getAndIncrement());
 +        assertEquals(k.toString(), r.get(k, expectedInvocations0)); // should load for new key
 +        assertTrue(r.containsKey(k));
 +        Integer expectedInvocations1 = new Integer(expectedInvocations.get());
 +        assertEquals(k.toString(), r.get(k, expectedInvocations1)); // no load
 +        assertEquals(k.toString(), r.get(k, expectedInvocations1)); // no load
 +        return expectedInvocations1;
 +      }
 +    });
 +    expectedInvocations.set(ex.intValue());
 +
 +    ex = (Integer)ds1.invoke(new SerializableCallable("Invoke loader from datastore, non-critical") {
 +      public Object call() throws Exception {
 +        Region<Integer, String> r = getCache().getRegion(rName);
 +        Integer k = new Integer(2);
 +        Integer expectedInvocations1 = new Integer(expectedInvocations.getAndIncrement());
 +        assertEquals(k.toString(), r.get(k, expectedInvocations1)); // should load for new key
 +        assertTrue(r.containsKey(k));
 +        Integer expectedInvocations2 = new Integer(expectedInvocations.get());
 +        assertEquals(k.toString(), r.get(k, expectedInvocations2)); // no load
 +        assertEquals(k.toString(), r.get(k, expectedInvocations2)); // no load
 +        String oldVal = r.remove(k);
 +        assertFalse(r.containsKey(k));
 +        assertEquals(k.toString(), oldVal);
 +        return expectedInvocations2;
 +      }
 +    });
 +    expectedInvocations.set(ex.intValue());
 +
 +    accessor.invoke(addExpectedException);
 +    ds1.invoke(addExpectedException);
 +
 +    ex = (Integer)ds1.invoke(new SerializableCallable("Set critical state, assert local load behavior") {
 +      public Object call() throws Exception {
 +        final OffHeapMemoryMonitor ohmm = ((InternalResourceManager)getCache().getResourceManager()).getOffHeapMonitor();
 +        final PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
 +        final RegionAdvisor advisor = pr.getRegionAdvisor();
 +        
 +        pr.put("oh1", new byte[838860]);
 +        pr.put("oh3", new byte[157287]);
 +        
 +        WaitCriterion wc = new WaitCriterion() {
 +          public String description() {
 +            return "verify critical state";
 +          }
 +          public boolean done() {
 +            for (final ProxyBucketRegion bucket : advisor.getProxyBucketArray()) {
 +              if (bucket.isBucketSick()) {
 +                return true;
 +              }
 +            }
 +            return false;
 +          }
 +        };
 +        Wait.waitForCriterion(wc, 30*1000, 10, true);
 +        
 +        final Integer k = new Integer(2); // reload with same key again and again
 +        final Integer expectedInvocations3 = new Integer(expectedInvocations.getAndIncrement());
 +        assertEquals(k.toString(), pr.get(k, expectedInvocations3)); // load
 +        assertFalse(pr.containsKey(k));
 +        Integer expectedInvocations4 = new Integer(expectedInvocations.getAndIncrement());
 +        assertEquals(k.toString(), pr.get(k, expectedInvocations4)); // load
 +        assertFalse(pr.containsKey(k));
 +        Integer expectedInvocations5 = new Integer(expectedInvocations.get());
 +        assertEquals(k.toString(), pr.get(k, expectedInvocations5)); // load
 +        assertFalse(pr.containsKey(k));
 +        return expectedInvocations5;
 +      }
 +    });
 +    expectedInvocations.set(ex.intValue());
 +
 +    ex = (Integer)accessor.invoke(new SerializableCallable("During critical state on datastore, assert accesor load behavior") {
 +      public Object call() throws Exception {
 +        final Integer k = new Integer(2);  // reload with same key again and again
 +        Integer expectedInvocations6 = new Integer(expectedInvocations.incrementAndGet());
 +        Region<Integer, String> r = getCache().getRegion(rName);
 +        assertEquals(k.toString(), r.get(k, expectedInvocations6)); // load
 +        assertFalse(r.containsKey(k));
 +        Integer expectedInvocations7 = new Integer(expectedInvocations.incrementAndGet());
 +        assertEquals(k.toString(), r.get(k, expectedInvocations7)); // load
 +        assertFalse(r.containsKey(k));
 +        return expectedInvocations7;
 +      }
 +    });
 +    expectedInvocations.set(ex.intValue());
 +    
 +    ex = (Integer)ds1.invoke(new SerializableCallable("Set safe state on datastore, assert local load behavior") {
 +      public Object call() throws Exception {
 +        final PartitionedRegion r = (PartitionedRegion) getCache().getRegion(rName);
 +        
 +        r.destroy("oh3");
 +        WaitCriterion wc = new WaitCriterion() {
 +          public String description() {
 +            return "verify critical state";
 +          }
 +          public boolean done() {
 +            return !r.memoryThresholdReached.get();
 +          }
 +        };
 +        Wait.waitForCriterion(wc, 30*1000, 10, true);
 +        
 +        Integer k = new Integer(3); // same key as previously used, this time is should stick
 +        Integer expectedInvocations8 = new Integer(expectedInvocations.incrementAndGet());
 +        assertEquals(k.toString(), r.get(k, expectedInvocations8)); // last load for 3
 +        assertTrue(r.containsKey(k));
 +        return expectedInvocations8;
 +      }
 +    });
 +    expectedInvocations.set(ex.intValue());
 +
 +    accessor.invoke(new SerializableCallable("Data store in safe state, assert load behavior, accessor sets critical state, assert load behavior") {
 +      public Object call() throws Exception {
 +        final OffHeapMemoryMonitor ohmm = ((InternalResourceManager)getCache().getResourceManager()).getOffHeapMonitor();
 +        assertFalse(ohmm.getState().isCritical());
 +        Integer k = new Integer(4);
 +        Integer expectedInvocations9 = new Integer(expectedInvocations.incrementAndGet());
 +        final PartitionedRegion r = (PartitionedRegion) getCache().getRegion(rName);
 +        assertEquals(k.toString(), r.get(k, expectedInvocations9)); // load for 4
 +        assertTrue(r.containsKey(k));
 +        assertEquals(k.toString(), r.get(k, expectedInvocations9)); // no load
 +
 +        // Go critical in accessor
 +        r.put("oh3", new byte[157287]);
 +        
 +        WaitCriterion wc = new WaitCriterion() {
 +          public String description() {
 +            return "verify critical state";
 +          }
 +          public boolean done() {
 +            return r.memoryThresholdReached.get();
 +          }
 +        };
 +
 +        k = new Integer(5);
 +        Integer expectedInvocations10 = new Integer(expectedInvocations.incrementAndGet());
 +        assertEquals(k.toString(), r.get(k, expectedInvocations10)); // load for key 5
 +        assertTrue(r.containsKey(k));
 +        assertEquals(k.toString(), r.get(k, expectedInvocations10)); // no load
 +        
 +        // Clean up critical state
 +        r.destroy("oh3");
 +        wc = new WaitCriterion() {
 +          public String description() {
 +            return "verify critical state";
 +          }
 +          public boolean done() {
 +            return !ohmm.getState().isCritical();
 +          }
 +        };
 +        return expectedInvocations10;
 +      }
 +    });
 +
 +    accessor.invoke(removeExpectedException);
 +    ds1.invoke(removeExpectedException);
 +  }
 +  
 +  private CacheSerializableRunnable createPR(final String rName, final boolean accessor) {
 +    return new CacheSerializableRunnable("create PR accessor") {
 +    @Override
 +    public void run2() throws CacheException {
 +      // Assert some level of connectivity
 +      getSystem(getOffHeapProperties());      
 +      InternalResourceManager irm = (InternalResourceManager)getCache().getResourceManager();
 +      irm.setCriticalOffHeapPercentage(90f);
 +      AttributesFactory<Integer, String> af = new AttributesFactory<Integer, String>();
 +      if (!accessor) {
 +        af.setCacheLoader(new CacheLoader<Integer, String>() {
 +          final AtomicInteger numLoaderInvocations = new AtomicInteger(0);
 +          public String load(LoaderHelper<Integer, String> helper) throws CacheLoaderException {
 +            Integer expectedInvocations = (Integer)helper.getArgument();
 +            final int actualInvocations = this.numLoaderInvocations.getAndIncrement();
 +            if (expectedInvocations.intValue() != actualInvocations) {
 +              throw new CacheLoaderException("Expected " + expectedInvocations 
 +                  + " invocations, actual is " + actualInvocations);
 +            }
 +            return helper.getKey().toString();
 +          }
 +          public void close() {}
 +        });
 +
 +        af.setPartitionAttributes(new PartitionAttributesFactory().create());
 +      } else {
 +        af.setPartitionAttributes(new PartitionAttributesFactory().setLocalMaxMemory(0).create());
 +      }
 +      af.setOffHeap(true);
 +      getCache().createRegion(rName, af.create());
 +    }
 +  };
 +  }
 +  
 +  /**
 +   * Test that LocalRegion cache Loads are not stored in the Region
 +   * if the VM is in a critical state, then test that they are allowed
 +   * once the VM is no longer critical
 +   * @throws Exception
 +   */
 +  public void testLRLoadRejection() throws Exception {
 +    final Host host = Host.getHost(0);
 +    final VM vm = host.getVM(2);
 +    final String rName = getUniqueName();
 +
-     vm.invoke(DistributedTestCase.class, "disconnectFromDS");
++    vm.invoke(() -> DistributedTestCase.disconnectFromDS());
 +    
 +    vm.invoke(new CacheSerializableRunnable("test LocalRegion load passthrough when critical") {
 +      @Override
 +      public void run2() throws CacheException {
 +        getSystem(getOffHeapProperties());
 +        InternalResourceManager irm = (InternalResourceManager)getCache().getResourceManager();
 +        final OffHeapMemoryMonitor ohmm = irm.getOffHeapMonitor();
 +        irm.setCriticalOffHeapPercentage(90f);
 +        AttributesFactory<Integer, String> af = new AttributesFactory<Integer, String>();
 +        af.setScope(Scope.LOCAL);
 +        af.setOffHeap(true);
 +        final AtomicInteger numLoaderInvocations = new AtomicInteger(0);
 +        af.setCacheLoader(new CacheLoader<Integer, String>() {
 +          public String load(LoaderHelper<Integer, String> helper)
 +          throws CacheLoaderException {
 +            numLoaderInvocations.incrementAndGet();
 +            return helper.getKey().toString();
 +          }
 +          public void close() {}
 +        });
 +        final LocalRegion r = (LocalRegion) getCache().createRegion(rName, af.create());
 +        
 +        assertFalse(ohmm.getState().isCritical());
 +        int expectedInvocations = 0;
 +        assertEquals(expectedInvocations++, numLoaderInvocations.get());
 +        {
 +          Integer k = new Integer(1);
 +          assertEquals(k.toString(), r.get(k));
 +        }
 +        assertEquals(expectedInvocations++, numLoaderInvocations.get());
 +        expectedInvocations++; expectedInvocations++;
 +        r.getAll(createRanges(10, 12));
 +        assertEquals(expectedInvocations++, numLoaderInvocations.get());
 +        
 +        getCache().getLoggerI18n().fine(addExpectedExString);
 +        r.put("oh1", new byte[838860]);
 +        r.put("oh3", new byte[157287]);
 +        getCache().getLoggerI18n().fine(removeExpectedExString);
 +        WaitCriterion wc = new WaitCriterion() {
 +          public String description() {
 +            return "expected region " + r + " to set memoryThresholdReached";
 +          }
 +          public boolean done() {
 +            return r.memoryThresholdReached.get();
 +          }
 +        };
 +        Wait.waitForCriterion(wc, 30*1000, 10, true);
 +        { 
 +          Integer k = new Integer(2);
 +          assertEquals(k.toString(), r.get(k));
 +        }
 +        assertEquals(expectedInvocations++, numLoaderInvocations.get());
 +        expectedInvocations++; expectedInvocations++;
 +        r.getAll(createRanges(13, 15));
 +        assertEquals(expectedInvocations++, numLoaderInvocations.get());
 +        
 +        getCache().getLoggerI18n().fine(addExpectedBelow);
 +        r.destroy("oh3");
 +        getCache().getLoggerI18n().fine(removeExpectedBelow);
 +        wc = new WaitCriterion() {
 +          public String description() {
 +            return "expected region " + r + " to unset memoryThresholdReached";
 +          }
 +          public boolean done() {
 +            return !r.memoryThresholdReached.get();
 +          }
 +        };
 +        Wait.waitForCriterion(wc, 30*1000, 10, true);
 +        
 +        {
 +          Integer k = new Integer(3);
 +          assertEquals(k.toString(), r.get(k));
 +        }
 +        assertEquals(expectedInvocations++, numLoaderInvocations.get());
 +        expectedInvocations++; expectedInvocations++;
 +        r.getAll(createRanges(16, 18));
 +        assertEquals(expectedInvocations, numLoaderInvocations.get());
 +
 +        // Do extra validation that the entry doesn't exist in the local region
 +        for (Integer i: createRanges(2, 2, 13, 15)) {
 +          if (r.containsKey(i)) {
 +            fail("Expected containsKey return false for key" + i);
 +          }
 +          if (r.getEntry(i) != null) {
 +            fail("Expected getEntry to return null for key" + i);
 +          }
 +        }
 +      }
 +    });
 +  }
 +  
 +  /** Create a list of integers consisting of the ranges defined by the provided
 +  * argument e.g..  createRanges(1, 4, 10, 12) means create ranges 1 through 4 and
 +  * 10 through 12 and should yield the list:
 +  * 1, 2, 3, 4, 10, 11, 12
 +  */ 
 +  public static List<Integer> createRanges(int... startEnds) {
 +    assert startEnds.length % 2 == 0;
 +    ArrayList<Integer> ret = new ArrayList<Integer>();
 +    for (int si=0; si<startEnds.length; si++) { 
 +      final int start = startEnds[si++];
 +      final int end = startEnds[si];
 +      assert end >= start;
 +      ret.ensureCapacity(ret.size() + ((end-start)+1));
 +      for (int i=start; i<=end; i++) {
 +        ret.add(new Integer(i));
 +      }
 +    }
 +    return ret;
 +  }
 +  
 +  public void testCleanAdvisorClose() throws Exception {
 +    final Host host = Host.getHost(0);
 +    final VM server1 = host.getVM(0);
 +    final VM server2 = host.getVM(1);
 +    final VM server3 = host.getVM(2);
 +
 +    final int[] ports = AvailablePortHelper.getRandomAvailableTCPPorts(3);
 +    final int port1 = ports[0];
 +    final int port2 = ports[1];
 +    final int port3 = ports[2];
 +    final String regionName = "testEventOrder";
 +
 +    startCacheServer(server1, port1, 0f, 0f,
 +        regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +    startCacheServer(server2, port2, 0f, 0f,
 +        regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +
 +    verifyProfiles(server1, 2);
 +    verifyProfiles(server2, 2);
 +
 +    server2.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        closeCache();
 +        return null;
 +      }
 +    });
 +
 +    verifyProfiles(server1, 1);
 +
 +    startCacheServer(server3, port3, 0f, 0f,
 +        regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +
 +    verifyProfiles(server1, 2);
 +    verifyProfiles(server3, 2);
 +  }
 +  
 +  public void testPRClientPutRejection() throws Exception {
 +    doClientServerTest("parRegReject", true/*createPR*/);
 +  }
 +
 +  public void testDistributedRegionClientPutRejection() throws Exception {
 +    doClientServerTest("distrReject", false/*createPR*/);
 +  }
 +  
 +  private void doPuts(VM vm, final String regionName,
 +      final boolean catchServerException, final boolean catchLowMemoryException) {
 +
 +    vm.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        Region r = getRootRegion().getSubregion(regionName);
 +        try {
 +          r.put(Integer.valueOf(0), "value-1");
 +          if (catchServerException || catchLowMemoryException) {
 +            fail("An expected ResourceException was not thrown");
 +          }
 +        } catch (ServerOperationException ex) {
 +          if (!catchServerException) {
 +            Assert.fail("Unexpected exception: ", ex);
 +          }
 +          if (!(ex.getCause() instanceof LowMemoryException)) {
 +            Assert.fail("Unexpected exception: ", ex);
 +          }
 +        } catch (LowMemoryException low) {
 +          if (!catchLowMemoryException) {
 +            Assert.fail("Unexpected exception: ", low);
 +          }
 +        }
 +        return null;
 +      }
 +    });
 +  }
 +
 +  private void doPutAlls(VM vm, final String regionName,
 +      final boolean catchServerException, final boolean catchLowMemoryException, 
 +      final Range rng) {
 +
 +    vm.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        Region r = getRootRegion().getSubregion(regionName);
 +        Map<Integer, String> temp = new HashMap<Integer, String>();
 +        for (int i=rng.start; i<rng.end; i++) {
 +          Integer k = Integer.valueOf(i);
 +          temp.put(k, "value-"+i);
 +        }
 +        try {
 +          r.putAll(temp);
 +          if (catchServerException || catchLowMemoryException) {
 +            fail("An expected ResourceException was not thrown");
 +          }
 +          for (Map.Entry<Integer, String> me: temp.entrySet()) {
 +            assertEquals(me.getValue(), r.get(me.getKey()));
 +          }
 +        } catch (ServerOperationException ex) {
 +          if (!catchServerException) {
 +            Assert.fail("Unexpected exception: ", ex);
 +          }
 +          if (!(ex.getCause() instanceof LowMemoryException)) {
 +            Assert.fail("Unexpected exception: ", ex);
 +          }
 +          for(Integer me: temp.keySet()) {
 +            assertFalse("Key " + me + " should not exist", r.containsKey(me));
 +          }
 +        } catch (LowMemoryException low) {
 +          LogWriterUtils.getLogWriter().info("Caught LowMemoryException", low);
 +          if (!catchLowMemoryException) {
 +            Assert.fail("Unexpected exception: ", low);
 +          }
 +          for(Integer me: temp.keySet()) {
 +            assertFalse("Key " + me + " should not exist", r.containsKey(me));
 +          }
 +        }
 +        return null;
 +      }
 +    });
 +  }
 +  
 +  private void doClientServerTest(final String regionName, boolean createPR)
 +      throws Exception {
 +    //create region on the server
 +    final Host host = Host.getHost(0);
 +    final VM server = host.getVM(0);
 +    final VM client = host.getVM(1);
 +    final Object bigKey = -1;
 +    final Object smallKey = -2;
 +
 +    final int port = AvailablePortHelper.getRandomAvailableTCPPort();
 +    startCacheServer(server, port, 0f, 90f,
 +        regionName, createPR, false, 0);
 +    startClient(client, server, port, regionName);
 +    doPuts(client, regionName, false/*catchServerException*/,
 +        false/*catchLowMemoryException*/);
 +    doPutAlls(client, regionName, false/*catchServerException*/,
 +        false/*catchLowMemoryException*/, Range.DEFAULT);
 +
 +    
 +    //make the region sick in the server
 +    final long bytesUsedAfterSmallKey = (long)server.invoke(new SerializableCallable() {
 +      @Override
 +      public Object call() throws Exception {
 +        InternalResourceManager irm = ((GemFireCacheImpl)getCache()).getResourceManager();
 +        final OffHeapMemoryMonitor ohm = irm.getOffHeapMonitor();
 +        assertTrue(ohm.getState().isNormal());
 +        getCache().getLoggerI18n().fine(addExpectedExString);
 +        final LocalRegion r = (LocalRegion) getRootRegion().getSubregion(regionName);
 +        final long bytesUsedAfterSmallKey;
 +        {
 +          OffHeapMemoryMonitorObserverImpl _testHook = new OffHeapMemoryMonitorObserverImpl();
 +          ohm.testHook = _testHook;
 +          try {
 +            r.put(smallKey, "1234567890");
 +            bytesUsedAfterSmallKey = _testHook.verifyBeginUpdateMemoryUsed(false);
 +          } finally {
 +            ohm.testHook = null;
 +          }
 +        }
 +        {
 +          final OffHeapMemoryMonitorObserverImpl th = new OffHeapMemoryMonitorObserverImpl();
 +          ohm.testHook = th;
 +          try {
 +            r.put(bigKey, new byte[943720]);
 +            th.verifyBeginUpdateMemoryUsed(bytesUsedAfterSmallKey + 943720 + 8, true);
 +            WaitCriterion waitForCritical = new WaitCriterion() {
 +              public boolean done() {
 +                return th.checkUpdateStateAndSendEventBeforeProcess(bytesUsedAfterSmallKey + 943720 + 8, MemoryState.EVICTION_DISABLED_CRITICAL);
 +              }
 +              @Override
 +              public String description() {
 +                return null;
 +              }
 +            };
 +            Wait.waitForCriterion(waitForCritical, 30*1000, 9, false);
 +            th.validateUpdateStateAndSendEventBeforeProcess(bytesUsedAfterSmallKey + 943720 + 8, MemoryState.EVICTION_DISABLED_CRITICAL);
 +          } finally {
 +            ohm.testHook = null;
 +          }
 +        }
 +        WaitCriterion wc;
 +        if (r instanceof PartitionedRegion) {
 +          final PartitionedRegion pr = (PartitionedRegion) r;
 +          final int bucketId = PartitionedRegionHelper.getHashKey(pr, null, bigKey, null, null);
 +          wc = new WaitCriterion() {
 +            @Override
 +            public String description() {
 +              return "Expected to go critical: isCritical=" + ohm.getState().isCritical();
 +            }
 +
 +            @Override
 +            public boolean done() {
 +              if (!ohm.getState().isCritical()) return false;
 +              // Only done once the bucket has been marked sick
 +              try {
 +                pr.getRegionAdvisor().checkIfBucketSick(bucketId, bigKey);
 +                return false;
 +              } catch (LowMemoryException ignore) {
 +                return true;
 +              }
 +            }
 +          };
 +        } else {
 +          wc = new WaitCriterion() {
 +            @Override
 +            public String description() {
 +              return "Expected to go critical: isCritical=" + ohm.getState().isCritical() + " memoryThresholdReached=" + r.memoryThresholdReached.get();
 +            }
 +
 +            @Override
 +            public boolean done() {
 +              return ohm.getState().isCritical() && r.memoryThresholdReached.get();
 +            }
 +          };
 +        }
 +        Wait.waitForCriterion(wc, 30000, 9, true);
 +        getCache().getLoggerI18n().fine(removeExpectedExString);
 +        return bytesUsedAfterSmallKey;
 +      }
 +    });
 +
 +    //make sure client puts are rejected
 +    doPuts(client, regionName, true/*catchServerException*/,
 +        false/*catchLowMemoryException*/);
 +    doPutAlls(client, regionName, true/*catchServerException*/,
 +        false/*catchLowMemoryException*/, new Range(Range.DEFAULT, Range.DEFAULT.width()+1));
 +    
 +    //make the region healthy in the server
 +    server.invoke(new SerializableRunnable() {
 +      public void run() {
 +        InternalResourceManager irm = ((GemFireCacheImpl)getCache()).getResourceManager();
 +        final OffHeapMemoryMonitor ohm = irm.getOffHeapMonitor();
 +        assertTrue(ohm.getState().isCritical());
 +        getCache().getLogger().fine(MemoryThresholdsOffHeapDUnitTest.this.addExpectedBelow);
 +        OffHeapMemoryMonitorObserverImpl _testHook = new OffHeapMemoryMonitorObserverImpl();
 +        ohm.testHook = _testHook;
 +        try {
 +          getRootRegion().getSubregion(regionName).destroy(bigKey);
 +          _testHook.verifyBeginUpdateMemoryUsed(bytesUsedAfterSmallKey, true);
 +        } finally {
 +          ohm.testHook = null;
 +        }
 +        WaitCriterion wc = new WaitCriterion() {
 +          @Override
 +          public String description() {
 +            return "Expected to go normal";
 +          }
 +
 +          @Override
 +          public boolean done() {
 +            return ohm.getState().isNormal();
 +          }
 +        };
 +        Wait.waitForCriterion(wc, 30000, 9, true);
 +        getCache().getLogger().fine(MemoryThresholdsOffHeapDUnitTest.this.removeExpectedBelow);
 +        return;
 +      }
 +    });
 +  }
 +  
 +  private static class OffHeapMemoryMonitorObserverImpl implements OffHeapMemoryMonitorObserver {
 +    private boolean beginUpdateMemoryUsed;
 +    private long beginUpdateMemoryUsed_bytesUsed;
 +    private boolean beginUpdateMemoryUsed_willSendEvent;
 +    @Override
 +    public synchronized void beginUpdateMemoryUsed(long bytesUsed, boolean willSendEvent) {
 +      beginUpdateMemoryUsed = true;
 +      beginUpdateMemoryUsed_bytesUsed = bytesUsed;
 +      beginUpdateMemoryUsed_willSendEvent = willSendEvent;
 +    }
 +    @Override
 +    public synchronized void afterNotifyUpdateMemoryUsed(long bytesUsed) {
 +    }
 +    @Override
 +    public synchronized void beginUpdateStateAndSendEvent(long bytesUsed, boolean willSendEvent) {
 +    }
 +    private boolean updateStateAndSendEventBeforeProcess;
 +    private long updateStateAndSendEventBeforeProcess_bytesUsed;
 +    private MemoryEvent updateStateAndSendEventBeforeProcess_event;
 +    @Override
 +    public synchronized void updateStateAndSendEventBeforeProcess(long bytesUsed, MemoryEvent event) {
 +      updateStateAndSendEventBeforeProcess = true;
 +      updateStateAndSendEventBeforeProcess_bytesUsed = bytesUsed;
 +      updateStateAndSendEventBeforeProcess_event = event;
 +    }
 +    @Override
 +    public synchronized void updateStateAndSendEventBeforeAbnormalProcess(long bytesUsed, MemoryEvent event) {
 +    }
 +    @Override
 +    public synchronized void updateStateAndSendEventIgnore(long bytesUsed, MemoryState oldState, MemoryState newState, long mostRecentBytesUsed,
 +        boolean deliverNextAbnormalEvent) {
 +    }
 +
 +    public synchronized void verifyBeginUpdateMemoryUsed(long expected_bytesUsed, boolean expected_willSendEvent) {
 +      if (!beginUpdateMemoryUsed) {
 +        fail("beginUpdateMemoryUsed was not called");
 +      }
 +      assertEquals(expected_bytesUsed, beginUpdateMemoryUsed_bytesUsed);
 +      assertEquals(expected_willSendEvent, beginUpdateMemoryUsed_willSendEvent);
 +    }
 +    /**
 +     * Verify that beginUpdateMemoryUsed was called, event will be sent, and return the "bytesUsed" it recorded.
 +     */
 +    public synchronized long verifyBeginUpdateMemoryUsed(boolean expected_willSendEvent) {
 +      if (!beginUpdateMemoryUsed) {
 +        fail("beginUpdateMemoryUsed was not called");
 +      }
 +      assertEquals(expected_willSendEvent, beginUpdateMemoryUsed_willSendEvent);
 +      return beginUpdateMemoryUsed_bytesUsed;
 +    }
 +    public synchronized boolean checkUpdateStateAndSendEventBeforeProcess(long expected_bytesUsed, MemoryState expected_memoryState) {
 +      if (!updateStateAndSendEventBeforeProcess) {
 +        return false;
 +      }
 +      if (expected_bytesUsed != updateStateAndSendEventBeforeProcess_bytesUsed) {
 +        return false;
 +      }
 +      if (!expected_memoryState.equals(updateStateAndSendEventBeforeProcess_event.getState())) {
 +        return false;
 +      }
 +      return true;
 +    }
 +    public synchronized void validateUpdateStateAndSendEventBeforeProcess(long expected_bytesUsed, MemoryState expected_memoryState) {
 +      if (!updateStateAndSendEventBeforeProcess) {
 +        fail("updateStateAndSendEventBeforeProcess was not called");
 +      }
 +      assertEquals(expected_bytesUsed, updateStateAndSendEventBeforeProcess_bytesUsed);
 +      assertEquals(expected_memoryState, updateStateAndSendEventBeforeProcess_event.getState());
 +    }
 +   }
 +  private void registerTestMemoryThresholdListener(VM vm) {
 +    vm.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        TestMemoryThresholdListener listener = new TestMemoryThresholdListener();
 +        InternalResourceManager irm = ((GemFireCacheImpl)getCache()).getResourceManager();
 +        irm.addResourceListener(ResourceType.OFFHEAP_MEMORY, listener);
 +        assertTrue(irm.getResourceListeners(ResourceType.OFFHEAP_MEMORY).contains(listener));
 +        return null;
 +      }
 +    });
 +  }
 +
 +  private void startCacheServer(VM server, final int port,
 +      final float evictionThreshold, final float criticalThreshold, final String regionName,
 +      final boolean createPR, final boolean notifyBySubscription, final int prRedundancy) throws Exception {
 +
 +    server.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        getSystem(getOffHeapProperties());
 +        GemFireCacheImpl cache = (GemFireCacheImpl)getCache();
 +
 +        InternalResourceManager irm = cache.getResourceManager();
 +        irm.setEvictionOffHeapPercentage(evictionThreshold);
 +        irm.setCriticalOffHeapPercentage(criticalThreshold);
 +
 +        AttributesFactory factory = new AttributesFactory();
 +        if (createPR) {
 +          PartitionAttributesFactory paf = new PartitionAttributesFactory();
 +          paf.setRedundantCopies(prRedundancy);
 +          paf.setTotalNumBuckets(11);
 +          factory.setPartitionAttributes(paf.create());
 +          factory.setOffHeap(true);
 +        } else {
 +          factory.setScope(Scope.DISTRIBUTED_ACK);
 +          factory.setDataPolicy(DataPolicy.REPLICATE);
 +          factory.setOffHeap(true);
 +        }
 +        Region region = createRegion(regionName, factory.create());
 +        if (createPR) {
 +          assertTrue(region instanceof PartitionedRegion);
 +        } else {
 +          assertTrue(region instanceof DistributedRegion);
 +        }
 +        CacheServer cacheServer = getCache().addCacheServer();
 +        cacheServer.setPort(port);
 +        cacheServer.setNotifyBySubscription(notifyBySubscription);
 +        cacheServer.start();
 +        return null;
 +      }
 +    });
 +  }
 +  
 +  private void startClient(VM client, final VM server, final int serverPort,
 +      final String regionName) {
 +
 +    client.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        getSystem(getClientProps());
 +        getCache();
 +
 +        PoolFactory pf = PoolManager.createFactory();
 +        pf.addServer(NetworkUtils.getServerHostName(server.getHost()), serverPort);
 +        pf.create("pool1");
 +        
 +        AttributesFactory af = new AttributesFactory();
 +        af.setScope(Scope.LOCAL);
 +        af.setPoolName("pool1");        
 +        createRegion(regionName, af.create());
 +        return null;
 +      }
 +    });
 +  }
 +
 +  /**
 +   * Verifies that the test listener value on the given vm is what is expected
 +   * Note that for remote events useWaitCriterion must be true.
 +   * Note also that since off-heap local events are async local events must also
 +   * set useWaitCriterion to true.
 +   * 
 +   * @param vm
 +   *          the vm where verification should take place
 +   * @param value
 +   *          the expected value
 +   * @param useWaitCriterion
 +   *          must be true for both local and remote events (see GEODE-138)
 +   */
 +  private void verifyListenerValue(VM vm, final MemoryState state, final int value, final boolean useWaitCriterion) {
 +    vm.invoke(new SerializableCallable() {
 +      private static final long serialVersionUID = 1L;
 +
 +      @Override
 +      public Object call() throws Exception {
 +        WaitCriterion wc = null;
 +        Set<ResourceListener> listeners = getGemfireCache().getResourceManager().getResourceListeners(ResourceType.OFFHEAP_MEMORY);
 +        TestMemoryThresholdListener tmp_listener = null;
 +        Iterator<ResourceListener> it = listeners.iterator();
 +        while (it.hasNext()) {
 +          ResourceListener<MemoryEvent> l = it.next();
 +          if (l instanceof TestMemoryThresholdListener) {
 +            tmp_listener = (TestMemoryThresholdListener) l;
 +            break;
 +          }
 +        }
 +        final TestMemoryThresholdListener listener = tmp_listener == null ? null : tmp_listener;
 +        switch (state) {
 +        case CRITICAL:
 +          if (useWaitCriterion) {
 +            wc = new WaitCriterion() {
 +              @Override
 +              public String description() {
 +                return "Remote CRITICAL assert failed " + listener.toString();
 +              }
 +
 +              @Override
 +              public boolean done() {
 +                return value == listener.getCriticalThresholdCalls();
 +              }
 +            };
 +          } else {
 +            assertEquals(value, listener.getCriticalThresholdCalls());
 +          }
 +          break;
 +        case CRITICAL_DISABLED:
 +          if (useWaitCriterion) {
 +            wc = new WaitCriterion() {
 +              @Override
 +              public String description() {
 +                return "Remote CRITICAL_DISABLED assert failed " + listener.toString();
 +              }
 +
 +              @Override
 +              public boolean done() {
 +                return value == listener.getCriticalDisabledCalls();
 +              }
 +            };
 +          } else {
 +            assertEquals(value, listener.getCriticalDisabledCalls());
 +          }
 +          break;
 +        case EVICTION:
 +          if (useWaitCriterion) {
 +            wc = new WaitCriterion() {
 +              @Override
 +              public String description() {
 +                return "Remote EVICTION assert failed " + listener.toString();
 +              }
 +
 +              @Override
 +              public boolean done() {
 +                return value == listener.getEvictionThresholdCalls();
 +              }
 +            };
 +          } else {
 +            assertEquals(value, listener.getEvictionThresholdCalls());
 +          }
 +          break;
 +        case EVICTION_DISABLED:
 +          if (useWaitCriterion) {
 +            wc = new WaitCriterion() {
 +              @Override
 +              public String description() {
 +                return "Remote EVICTION_DISABLED assert failed " + listener.toString();
 +              }
 +
 +              @Override
 +              public boolean done() {
 +                return value == listener.getEvictionDisabledCalls();
 +              }
 +            };
 +          } else {
 +            assertEquals(value, listener.getEvictionDisabledCalls());
 +          }
 +          break;
 +        case NORMAL:
 +          if (useWaitCriterion) {
 +            wc = new WaitCriterion() {
 +              @Override
 +              public String description() {
 +                return "Remote NORMAL assert failed " + listener.toString();
 +              }
 +
 +              @Override
 +              public boolean done() {
 +                return value == listener.getNormalCalls();
 +              }
 +            };
 +          } else {
 +            assertEquals(value, listener.getNormalCalls());
 +          }
 +          break;
 +        default:
 +          throw new IllegalStateException("Unknown memory state");
 +        }
 +        if (useWaitCriterion) {
 +          Wait.waitForCriterion(wc, 5000, 10, true);
 +        }
 +        return null;
 +      }
 +    });
 +  }
 +  
 +  private void verifyProfiles(VM vm, final int numberOfProfiles) {
 +    vm.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        InternalResourceManager irm = ((GemFireCacheImpl)getCache()).getResourceManager();
 +        final ResourceAdvisor ra = irm.getResourceAdvisor();
 +        WaitCriterion wc = new WaitCriterion() {
 +          public String description() {
 +            return "verify profiles failed. Current profiles: " + ra.adviseGeneric();
 +          }
 +          public boolean done() {
 +            return numberOfProfiles == ra.adviseGeneric().size();
 +          }
 +        };
 +        Wait.waitForCriterion(wc, 10000, 10, true);
 +        return null;
 +      }
 +    });
 +  }
 +  
 +  private Properties getOffHeapProperties() {
 +    Properties p = new Properties();
 +    p.setProperty(DistributionConfig.LOCATORS_NAME, "localhost["+DistributedTestUtils.getDUnitLocatorPort()+"]");
 +    p.setProperty(DistributionConfig.OFF_HEAP_MEMORY_SIZE_NAME, "1m");
 +    return p;
 +  }
 +  
 +  protected Properties getClientProps() {
 +    Properties p = new Properties();
 +    p.setProperty(DistributionConfig.MCAST_PORT_NAME, "0");
 +    p.setProperty(DistributionConfig.LOCATORS_NAME, "");
 +    return p;
 +  }
 +}


[26/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/xmlcache/CacheCreation.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/cache/xmlcache/CacheCreation.java
index f7063bc,0000000..9456f9a
mode 100644,000000..100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/xmlcache/CacheCreation.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/xmlcache/CacheCreation.java
@@@ -1,1690 -1,0 +1,1695 @@@
 +/*
 + * 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 java.io.File;
 +import java.io.IOException;
 +import java.io.InputStream;
 +import java.util.ArrayList;
 +import java.util.Arrays;
 +import java.util.Collection;
 +import java.util.Collections;
 +import java.util.HashMap;
 +import java.util.HashSet;
 +import java.util.Iterator;
 +import java.util.LinkedHashMap;
 +import java.util.LinkedHashSet;
 +import java.util.List;
 +import java.util.Map;
 +import java.util.Map.Entry;
 +import java.util.Properties;
 +import java.util.Set;
 +import java.util.concurrent.TimeUnit;
 +
 +import javax.naming.Context;
 +
 +import com.gemstone.gemfire.CancelCriterion;
 +import com.gemstone.gemfire.GemFireIOException;
 +import com.gemstone.gemfire.LogWriter;
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheTransactionManager;
 +import com.gemstone.gemfire.cache.CacheWriterException;
 +import com.gemstone.gemfire.cache.Declarable;
 +import com.gemstone.gemfire.cache.DiskStore;
 +import com.gemstone.gemfire.cache.DiskStoreFactory;
 +import com.gemstone.gemfire.cache.DynamicRegionFactory;
 +import com.gemstone.gemfire.cache.GatewayException;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionAttributes;
 +import com.gemstone.gemfire.cache.RegionExistsException;
 +import com.gemstone.gemfire.cache.RegionFactory;
 +import com.gemstone.gemfire.cache.RegionShortcut;
 +import com.gemstone.gemfire.cache.TimeoutException;
 +import com.gemstone.gemfire.cache.asyncqueue.AsyncEventQueue;
 +import com.gemstone.gemfire.cache.asyncqueue.AsyncEventQueueFactory;
 +import com.gemstone.gemfire.cache.asyncqueue.internal.AsyncEventQueueFactoryImpl;
 +import com.gemstone.gemfire.cache.client.Pool;
 +import com.gemstone.gemfire.cache.client.PoolFactory;
 +import com.gemstone.gemfire.cache.client.PoolManager;
 +import com.gemstone.gemfire.cache.client.internal.PoolImpl;
 +import com.gemstone.gemfire.cache.execute.FunctionService;
 +import com.gemstone.gemfire.cache.query.CqAttributes;
 +import com.gemstone.gemfire.cache.query.CqException;
 +import com.gemstone.gemfire.cache.query.CqExistsException;
 +import com.gemstone.gemfire.cache.query.CqQuery;
 +import com.gemstone.gemfire.cache.query.CqServiceStatistics;
 +import com.gemstone.gemfire.cache.query.Index;
 +import com.gemstone.gemfire.cache.query.IndexExistsException;
 +import com.gemstone.gemfire.cache.query.IndexInvalidException;
 +import com.gemstone.gemfire.cache.query.IndexNameConflictException;
 +import com.gemstone.gemfire.cache.query.IndexType;
 +import com.gemstone.gemfire.cache.query.MultiIndexCreationException;
 +import com.gemstone.gemfire.cache.query.Query;
 +import com.gemstone.gemfire.cache.query.QueryInvalidException;
 +import com.gemstone.gemfire.cache.query.QueryService;
 +import com.gemstone.gemfire.cache.query.RegionNotFoundException;
 +import com.gemstone.gemfire.cache.query.internal.cq.CqService;
 +import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.cache.snapshot.CacheSnapshotService;
 +import com.gemstone.gemfire.cache.util.GatewayConflictResolver;
 +import com.gemstone.gemfire.cache.wan.GatewayReceiver;
 +import com.gemstone.gemfire.cache.wan.GatewayReceiverFactory;
 +import com.gemstone.gemfire.cache.wan.GatewaySender;
 +import com.gemstone.gemfire.cache.wan.GatewaySenderFactory;
 +import com.gemstone.gemfire.cache.wan.GatewayTransportFilter;
 +import com.gemstone.gemfire.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.DistributedSystem;
 +import com.gemstone.gemfire.i18n.LogWriterI18n;
 +import com.gemstone.gemfire.internal.Assert;
 +import com.gemstone.gemfire.cache.hdfs.HDFSStoreFactory;
 +import com.gemstone.gemfire.cache.hdfs.internal.HDFSIntegrationUtil;
 +import com.gemstone.gemfire.cache.hdfs.internal.HDFSStoreCreation;
 +import com.gemstone.gemfire.cache.hdfs.internal.HDFSStoreFactoryImpl;
 +import com.gemstone.gemfire.cache.hdfs.internal.HDFSStoreImpl;
 +import com.gemstone.gemfire.internal.cache.CacheServerImpl;
 +import com.gemstone.gemfire.internal.cache.CacheConfig;
 +import com.gemstone.gemfire.internal.cache.CacheServerLauncher;
 +import com.gemstone.gemfire.internal.cache.CacheService;
 +import com.gemstone.gemfire.internal.cache.DiskStoreFactoryImpl;
 +import com.gemstone.gemfire.internal.cache.DiskStoreImpl;
 +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
 +import com.gemstone.gemfire.internal.cache.InternalCache;
 +import com.gemstone.gemfire.internal.cache.PoolFactoryImpl;
 +import com.gemstone.gemfire.internal.cache.PoolManagerImpl;
 +import com.gemstone.gemfire.internal.cache.extension.Extensible;
 +import com.gemstone.gemfire.internal.cache.extension.ExtensionPoint;
 +import com.gemstone.gemfire.internal.cache.extension.SimpleExtensionPoint;
 +import com.gemstone.gemfire.internal.cache.ha.HARegionQueue;
 +import com.gemstone.gemfire.internal.cache.wan.AbstractGatewaySender;
 +import com.gemstone.gemfire.internal.cache.wan.WANServiceProvider;
 +import com.gemstone.gemfire.internal.cache.wan.InternalGatewaySenderFactory;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.jndi.JNDIInvoker;
 +import com.gemstone.gemfire.internal.logging.InternalLogWriter;
 +import com.gemstone.gemfire.internal.logging.LocalLogWriter;
 +import com.gemstone.gemfire.internal.logging.LogWriterFactory;
 +import com.gemstone.gemfire.pdx.PdxInstance;
 +import com.gemstone.gemfire.pdx.PdxInstanceFactory;
 +import com.gemstone.gemfire.pdx.PdxSerializer;
 +import com.gemstone.gemfire.pdx.internal.TypeRegistry;
 +
 +/**
 + * Represents a {@link Cache} that is created declaratively.  Notice
 + * that it implements the {@link Cache} interface so that this class
 + * must be updated when {@link Cache} is modified.  This class is
 + * public for testing purposes.
 + *
 + * @author David Whitlock
 + *
 + * @since 3.0
 + */
 +public class CacheCreation implements InternalCache {
 +
 +  /** The amount of time to wait for a distributed lock */
 +  private int lockTimeout = GemFireCacheImpl.DEFAULT_LOCK_TIMEOUT;
 +  private boolean hasLockTimeout = false;
 +
 +  /** The duration of a lease on a distributed lock */
 +  private int lockLease = GemFireCacheImpl.DEFAULT_LOCK_LEASE;
 +  private boolean hasLockLease = false;
 +
 +  /** The amount of time to wait for a <code>netSearch</code> */
 +  private int searchTimeout = GemFireCacheImpl.DEFAULT_SEARCH_TIMEOUT;
 +  private boolean hasSearchTimeout = false;
 +
 +  private boolean hasMessageSyncInterval = false;
 +
 +  /** This cache's roots keyed on name */
 +  protected final Map roots = new LinkedHashMap();
 +  
 +  /** Are dynamic regions enabled in this cache? */
 +  private DynamicRegionFactory.Config dynamicRegionFactoryConfig = null;
 +  private boolean hasDynamicRegionFactory = false;
 +
 +  /** Is this a cache server? */
 +  private boolean isServer = false;
 +  private boolean hasServer = false;
 +
 +  /** The bridge servers configured for this cache */
 +  private final List bridgeServers = new ArrayList();
 +  
 +  // Stores the properties used to initialize declarables.
 +  private final Map<Declarable, Properties> declarablePropertiesMap = new HashMap<Declarable, Properties>();
 +  
 +  private Set<GatewaySender> gatewaySenders = new HashSet<GatewaySender>(); 
 +  
 +  private Set<GatewayReceiver> gatewayReceivers = new HashSet<GatewayReceiver>();
 +  
 +  private Set<AsyncEventQueue> asyncEventQueues = new HashSet<AsyncEventQueue>();
 +  
 +  private GatewayConflictResolver gatewayConflictResolver;
 +  
 +  /** The copyOnRead attribute */
 +  private boolean copyOnRead = GemFireCacheImpl.DEFAULT_COPY_ON_READ;
 +  private boolean hasCopyOnRead = false;
 +
 +  /** The CacheTransactionManager representative for this Cache */
 +  protected CacheTransactionManagerCreation txMgrCreation = null;
 +
 +  /** JNDI Context associated with the Gemfire */
 +//  private static Context ctx;
 +
 +  /** The named region attributes associated with this cache */
 +  private final Map namedRegionAttributes = new HashMap();
 +
 +  /** The names of the region attributes in the order in which they
 +   * were added.  Keeping track of this ensures that named region
 +   * attributes are processed in the correct order.  That is, "parent"
 +   * named region attributes will be processed before "children" named
 +   * region attributes. */
 +  protected final List regionAttributesNames = new ArrayList();
 +
 +  /** The named disk store attributes associated with this cache.
 +   * Made this linked so its iteration would be in insert order.
 +   * This is important for unit testing 44914.
 +   */
 +  protected final Map diskStores = new LinkedHashMap();
 +  protected final Map hdfsStores = new LinkedHashMap();
 +  
 +  private final List<File> backups = new ArrayList<File>();
 +
 +  private CacheConfig cacheConfig = new CacheConfig();
 +
 +  /** A logger that is used in debugging */
 +  private InternalLogWriter logWriter =
 +    new LocalLogWriter(InternalLogWriter.ALL_LEVEL, System.out);
 +
 +  private InternalLogWriter securityLogWriter =
 +      LogWriterFactory.toSecurityLogWriter(logWriter);
 +  
 +  /**
 +   * {@link ExtensionPoint} support.
 +   * @since 8.1
 +   */
 +  private SimpleExtensionPoint<Cache> extensionPoint = new SimpleExtensionPoint<Cache>(this, this);
 +
 +  ////////////////////////  Constructors  ////////////////////////
 +
 +  /**
 +   * Creates a new <code>CacheCreation</code> with no root regions
 +   */
 +  public CacheCreation() {
 +    this(false);
 +  }
 +  
 +  /** clear thread locals that may have been set by previous uses of CacheCreation */
 +  public static void clearThreadLocals() {
 +    createInProgress = new ThreadLocal<>();
 +  }
 +
 +  /**
 +   * @param forParsing if true then this creation is used for parsing xml;
 +   *   if false then it is used for generating xml.
 +   * @since 5.7
 +   */
 +  public CacheCreation(boolean forParsing) {
 +    initializeRegionShortcuts();
 +    if (!forParsing) {
 +      createInProgress.set(this.pm);
 +    }
 +  }
 +  /**
 +   * @since 5.7
 +   */
 +  public void startingGenerate() {
 +    createInProgress.set(null);
 +  }
 +  
 +  //////////////////////  Instance Methods  //////////////////////
 +
 +  static final private RegionAttributes defaults = new AttributesFactory().create();
 +    
 +  RegionAttributes getDefaultAttributes() {
 +    return defaults;
 +  }
 +
 +  protected void initializeRegionShortcuts() {
 +    GemFireCacheImpl.initializeRegionShortcuts(this);
 +  }
 +
 +  /**
 +   * Sets the attributes of the root region
 +   *
 +   * @throws RegionExistsException
 +   *         If this cache already contains a region with the same
 +   *         name as <code>root</code>.
 +   */
 +  void addRootRegion(RegionCreation root)
 +    throws RegionExistsException {
 +
 +    String name = root.getName();
 +    RegionCreation existing = (RegionCreation) this.roots.get(name);
 +    if (existing != null) {
 +      throw new RegionExistsException(existing);
 +
 +    } else {
 +      this.roots.put(root.getName(), root);
 +    }
 +  }
 +
 +  public int getLockTimeout() {
 +    return this.lockTimeout;
 +  }
 +
 +  public void setLockTimeout(int seconds) {
 +    this.lockTimeout = seconds;
 +    this.hasLockTimeout = true;
 +  }
 +
 +  boolean hasLockTimeout() {
 +    return this.hasLockTimeout;
 +  }
 +
 +  public int getLockLease() {
 +    return this.lockLease;
 +  }
 +
 +  public void setLockLease(int seconds) {
 +    this.lockLease = seconds;
 +    this.hasLockLease = true;
 +  }
 +
 +  boolean hasLockLease() {
 +    return this.hasLockLease;
 +  }
 +
 +  public int getSearchTimeout() {
 +    return this.searchTimeout;
 +  }
 +
 +  public void setSearchTimeout(int seconds) {
 +    this.searchTimeout = seconds;
 +    this.hasSearchTimeout = true;
 +  }
 +
 +  boolean hasSearchTimeout() {
 +    return this.hasSearchTimeout;
 +  }
 +
 +  public int getMessageSyncInterval()
 +  {
 +    return HARegionQueue.getMessageSyncInterval();
 +  }
 +
 +  public void setMessageSyncInterval(int seconds)
 +  {
 +    if (seconds < 0) {
 +      throw new IllegalArgumentException(LocalizedStrings.CacheCreation_THE_MESSAGESYNCINTERVAL_PROPERTY_FOR_CACHE_CANNOT_BE_NEGATIVE.toLocalizedString());
 +    }
 +    HARegionQueue.setMessageSyncInterval(seconds);
 +    this.hasMessageSyncInterval = true;
 +  }
 +
 +  boolean hasMessageSyncInterval()
 +  {
 +    return this.hasMessageSyncInterval;
 +  }
 +
 +  public Set rootRegions() {
 +    Set regions = new LinkedHashSet();
 +    for (Iterator itr = this.roots.values().iterator(); itr.hasNext();) {
 +      regions.add(itr.next());
 +    }
 +    return Collections.unmodifiableSet(regions);
 +  }
 +
 +  /**
 +   * create diskstore factory
 +   * 
 +   * @since prPersistSprint2
 +   */
 +  public DiskStoreFactory createDiskStoreFactory() {
 +    return new DiskStoreFactoryImpl(this);
 +  }
 +  
 +  /**
 +   * Store the current CacheCreation that is doing a create.
 +   * Used from PoolManager to defer to CacheCreation as a manager of pools.
 +   * @since 5.7
 +   */
 +  private static ThreadLocal createInProgress = new ThreadLocal();
 +
 +  /**
 +   * Returns null if the current thread is not doing a CacheCreation create.
 +   * Otherwise returns the PoolManagerImpl of the CacheCreation of the
 +   * create being invoked.
 +   * @since 5.7
 +   */
 +  public static final PoolManagerImpl getCurrentPoolManager() {
 +    return (PoolManagerImpl)createInProgress.get();
 +  }
 +  
 +  /**
 +   * Fills in the contents of a {@link Cache} based on this creation
 +   * object's state.
 +   *
 +   * @throws TimeoutException
 +   * @throws CacheWriterException
 +   * @throws RegionExistsException
 +   * @throws GatewayException
 +   */
 +  void create(GemFireCacheImpl cache)
 +    throws TimeoutException, CacheWriterException,
 +           GatewayException,
 +           RegionExistsException {
 +    cache.setDeclarativeCacheConfig(cacheConfig);
 +    
 +    if (cache.isClient()) {
 +      throw new IllegalStateException("You must use client-cache in the cache.xml when ClientCacheFactory is used.");
 +    }
 +    if (this.hasLockLease()) {
 +      cache.setLockLease(this.lockLease);
 +    }
 +    if (this.hasLockTimeout()) {
 +      cache.setLockTimeout(this.lockTimeout);
 +    }
 +    if (this.hasSearchTimeout()) {
 +      cache.setSearchTimeout(this.searchTimeout);
 +    }
 +    if (this.hasMessageSyncInterval()) {
 +      cache.setMessageSyncInterval(this.getMessageSyncInterval());
 +    }
 +    if (this.gatewayConflictResolver != null) {
 +      cache.setGatewayConflictResolver(this.gatewayConflictResolver);
 +    }
 +//    if (this.hasCopyOnRead()) {
 +//      cache.setCopyOnRead(this.copyOnRead);
 +//    }
 +    { // create connection pools
 +      Map m = getPools();
 +      if (!m.isEmpty()) {
 +        Iterator it = m.values().iterator();
 +        while (it.hasNext()) {
 +          Pool cp = (Pool)it.next();
 +          PoolFactoryImpl f;
 +          f = (PoolFactoryImpl)PoolManager.createFactory();
 +          f.init(cp);
 +          PoolImpl p = (PoolImpl)f.create(cp.getName());
 +        }
 +      }
 +    }
 +
 +    if (hasResourceManager()) {
 +      // moved this up to fix bug 42128
 +      getResourceManager().configure(cache.getResourceManager());
 +    }
 +    
 +    DiskStoreAttributesCreation pdxRegDSC = initializePdxDiskStore(cache);
 +    
 +    cache.initializePdxRegistry();
 +
 +    for (Iterator iter = this.diskStores.values().iterator(); iter.hasNext();) {
 +      DiskStoreAttributesCreation creation = (DiskStoreAttributesCreation) iter.next();
 +      if (creation != pdxRegDSC) {
 +        createDiskStore(creation, cache);
 +      }
 +    }
 +    
 +    if (this.hasDynamicRegionFactory()) {
 +      DynamicRegionFactory.get().open(this.getDynamicRegionFactoryConfig());
 +    }
 +    if (this.hasServer()) {
 +      cache.setIsServer(this.isServer);
 +    }
 +    if (this.hasCopyOnRead()) {
 +      cache.setCopyOnRead(this.copyOnRead);
 +    }
 +
 +    if (this.txMgrCreation != null &&
 +        this.txMgrCreation.getListeners().length > 0 &&
 +        cache.getCacheTransactionManager()!=null) {
 +      cache.getCacheTransactionManager().initListeners(this.txMgrCreation.getListeners());
 +    }
 +
 +    if (this.txMgrCreation != null &&
 +        cache.getCacheTransactionManager()!=null) {
 +      cache.getCacheTransactionManager().setWriter(this.txMgrCreation.getWriter());
 +    }
 +    
 +    for (GatewaySender senderCreation : this.getGatewaySenders()) {
 +      GatewaySenderFactory factory = (GatewaySenderFactory)cache
 +          .createGatewaySenderFactory();
 +      ((InternalGatewaySenderFactory)factory).configureGatewaySender(senderCreation);
 +      GatewaySender gatewaySender = factory.create(senderCreation.getId(),
 +          senderCreation.getRemoteDSId());
 +      // Start the sender if it is not set to manually start
 +      if (gatewaySender.isManualStart()) {
 +        cache
 +            .getLoggerI18n()
 +            .info(
 +                LocalizedStrings.CacheCreation_0_IS_NOT_BEING_STARTED_SINCE_IT_IS_CONFIGURED_FOR_MANUAL_START,
 +                gatewaySender);
 +      }
 +    }
 +    
 +    for (AsyncEventQueue asyncEventQueueCreation : this.getAsyncEventQueues()) {
 +      AsyncEventQueueFactoryImpl asyncQueueFactory = 
 +        (AsyncEventQueueFactoryImpl) cache.createAsyncEventQueueFactory();
 +      asyncQueueFactory.configureAsyncEventQueue(asyncEventQueueCreation);
 +      
 +      AsyncEventQueue asyncEventQueue = cache.getAsyncEventQueue(asyncEventQueueCreation.getId());
 +//      AsyncEventQueue asyncEventQueue = 
 +//        asyncQueueFactory.create(asyncEventQueueCreation.getId(), asyncEventQueueCreation.getAsyncEventListener());
 +      if (asyncEventQueue == null) {
 +        asyncQueueFactory.create(asyncEventQueueCreation.getId(), asyncEventQueueCreation.getAsyncEventListener());
 +      }
 +    }
 +    
 +    for (GatewayReceiver receiverCreation : this.getGatewayReceivers()) {
 +      GatewayReceiverFactory factory = cache.createGatewayReceiverFactory();
 +      factory.setBindAddress(receiverCreation.getBindAddress());
 +      factory.setMaximumTimeBetweenPings(receiverCreation
 +          .getMaximumTimeBetweenPings());
 +      factory.setStartPort(receiverCreation.getStartPort());
 +      factory.setEndPort(receiverCreation.getEndPort());
 +      factory.setSocketBufferSize(receiverCreation.getSocketBufferSize());
 +      factory.setManualStart(receiverCreation.isManualStart());
 +      for (GatewayTransportFilter filter : receiverCreation
 +          .getGatewayTransportFilters()) {
 +        factory.addGatewayTransportFilter(filter);
 +      }
 +      factory.setHostnameForSenders(receiverCreation.getHost()); 
 +      GatewayReceiver receiver = factory.create();
 +       if (receiver.isManualStart()) {
 +        cache
 +            .getLoggerI18n()
 +            .info(
 +                LocalizedStrings.CacheCreation_0_IS_NOT_BEING_STARTED_SINCE_IT_IS_CONFIGURED_FOR_MANUAL_START,
 +                receiver);
 +      }
 +    }
 +
 +    for(Iterator iter = this.hdfsStores.entrySet().iterator(); iter.hasNext(); ) {
 +      Entry entry = (Entry) iter.next();
 +      HDFSStoreCreation hdfsStoreCreation = (HDFSStoreCreation) entry.getValue();
 +      HDFSStoreFactory storefactory = cache.createHDFSStoreFactory(hdfsStoreCreation);
 +      storefactory.create((String) entry.getKey());
 +    }
 +
 +    cache.initializePdxRegistry();
 +
 +    
 +    for (Iterator iter = this.regionAttributesNames.iterator();
 +         iter.hasNext(); ) {
 +      String id = (String) iter.next();
 +      RegionAttributesCreation creation =
 +        (RegionAttributesCreation) getRegionAttributes(id);
 +      creation.inheritAttributes(cache, false);
 +
 +      // TODO: HDFS: HDFS store/queue will be mapped against region path and not
 +      // the attribute id; don't really understand what this is trying to do
 +      if (creation.getHDFSStoreName() != null)
 +      {
 +        HDFSStoreImpl store = cache.findHDFSStore(creation.getHDFSStoreName());
 +        if(store == null) {
 +          HDFSIntegrationUtil.createDefaultAsyncQueueForHDFS((Cache)cache, creation.getHDFSWriteOnly(), id);
 +        }
 +      }
 +      if (creation.getHDFSStoreName() != null && creation.getPartitionAttributes().getColocatedWith() == null) {
 +        creation.addAsyncEventQueueId(HDFSStoreFactoryImpl.getEventQueueName(id));
 +      }
 +      
 +      RegionAttributes attrs;
 +      // Don't let the RegionAttributesCreation escape to the user
 +      AttributesFactory factory = new AttributesFactory(creation);
 +      attrs = factory.create();
 +
 +      cache.setRegionAttributes(id, attrs);
 +    }
 +
 +    initializeRegions(this.roots, cache);
 +
 +    cache.readyDynamicRegionFactory();
 +
 +    // Create and start the BridgeServers. This code was moved from
 +    // before region initialization to after it to fix bug 33587.
 +    // Create and start the CacheServers after the gateways have been intialized
 +    // to fix bug 39736.
 +
 +    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());
 +    
 +    // 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) {
- 
++    CacheServerCreation defaultServer = null;
++    
 +    if (declarativeCacheServers.size() > 1
 +        && (serverPort != null || serverBindAdd != null)) {
 +      throw new RuntimeException(
 +          LocalizedStrings.CacheServerLauncher_SERVER_PORT_MORE_THAN_ONE_CACHE_SERVER
 +              .toLocalizedString());
 +    }
 +
 +    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()) {
 +            existingCacheServer = true;
 +          }
 +        }
 +      }
 +      
 +      if (!existingCacheServer) {
-         declarativeCacheServers.add(new CacheServerCreation((GemFireCacheImpl)cache, false));
++        defaultServer = new CacheServerCreation((GemFireCacheImpl)cache, false);
++        declarativeCacheServers.add(defaultServer);
 +      }
 +    }
 +    
 +    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(declaredCacheServer);
++      if (declaredCacheServer == defaultServer) {
++        impl.setIsDefaultServer();
++      }
 +
 +      if (serverPort != null && serverPort != CacheServer.DEFAULT_PORT) {
 +        impl.setPort(serverPort);
 +      }
 +      if (serverBindAdd != null) {
 +        impl.setBindAddress(serverBindAdd.trim());
 +      }
 +      
 +      try {
 +        if (!impl.isRunning())
 +          impl.start();
 +
 +      }
 +      catch (IOException ex) {
 +        throw new GemFireIOException(
 +            LocalizedStrings.CacheCreation_WHILE_STARTING_CACHE_SERVER_0
 +                .toLocalizedString(impl), ex);
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Returns a description of the disk store used by the pdx registry.
 +   */
 +  protected DiskStoreAttributesCreation initializePdxDiskStore(GemFireCacheImpl cache) {
 +    // to fix bug 44271 create the disk store used by the pdx registry first.
 +    // If it is using the default disk store we need to create it now.
 +    // If the cache has a pool then no need to create disk store.
 +    DiskStoreAttributesCreation pdxRegDSC = null;
 +    if (TypeRegistry.mayNeedDiskStore(cache)) {
 +      String pdxRegDsName = cache.getPdxDiskStore();
 +      if (pdxRegDsName == null) {
 +        pdxRegDsName = DiskStoreFactory.DEFAULT_DISK_STORE_NAME;
 +      }
 +      // make sure pdxRegDSC gets set to fix for bug 44914
 +      pdxRegDSC = (DiskStoreAttributesCreation)this.diskStores.get(pdxRegDsName);
 +      if (pdxRegDSC == null) {
 +        if (pdxRegDsName.equals(DiskStoreFactory.DEFAULT_DISK_STORE_NAME)) {
 +          // need to create default disk store
 +          cache.getOrCreateDefaultDiskStore();
 +        }
 +      } else {
 +        createDiskStore(pdxRegDSC, cache);
 +      }
 +    }
 +    return pdxRegDSC;
 +  }
 +  
 +  protected void createDiskStore(DiskStoreAttributesCreation creation, GemFireCacheImpl cache) {
 +    // Don't let the DiskStoreAttributesCreation escape to the user
 +    DiskStoreFactory factory = cache.createDiskStoreFactory(creation);
 +    factory.create(creation.getName());
 +  }
 +
 +  /**
 +   * Returns whether or not this <code>CacheCreation</code> is
 +   * equivalent to another <code>Cache</code>.
 +   */
 +  public boolean sameAs(Cache other) {
 +    boolean sameConfig =
 +      other.getLockLease() == this.getLockLease() &&
 +      other.getLockTimeout() == this.getLockTimeout() &&
 +      other.getSearchTimeout() == this.getSearchTimeout() &&
 +      other.getMessageSyncInterval() == this.getMessageSyncInterval() &&
 +      other.getCopyOnRead() == this.getCopyOnRead() &&
 +      other.isServer() == this.isServer();
 +
 +    if (!sameConfig) {
 +      throw new RuntimeException(LocalizedStrings.CacheCreation_SAMECONFIG.toLocalizedString());
 +
 +    } else {
 +      DynamicRegionFactory.Config drc1 = this.getDynamicRegionFactoryConfig();
 +      if (drc1 != null) {
 +        // we have a dynamic region factory
 +        DynamicRegionFactory.Config drc2 = null;
 +        if (other instanceof CacheCreation) {
 +          drc2 = ((CacheCreation)other).getDynamicRegionFactoryConfig();
 +        } else {
 +          drc2 = DynamicRegionFactory.get().getConfig();
 +        }
 +        if (drc2 == null) {
 +          return false;
 +        }
 +        if (!drc1.equals(drc2)) {
 +          return false;
 +        }
 +      } else {
 +        // we have no dynamic region factory; how about other?
 +        if (other instanceof CacheCreation) {
 +          if (((CacheCreation)other).getDynamicRegionFactoryConfig() != null) {
 +            return false;
 +          }
 +        } else {
 +          // other must be real cache in which case we compare to DynamicRegionFactory
 +          if (DynamicRegionFactory.get().isOpen()) {
 +            return false;
 +          }
 +        }
 +      }
 +
 +      Collection myBridges = this.getCacheServers();
 +      Collection otherBridges = other.getCacheServers();
 +      if (myBridges.size() != otherBridges.size()) {
 +        throw new RuntimeException(LocalizedStrings.CacheCreation_CACHESERVERS_SIZE.toLocalizedString());
 +      }
 +
 +      for (Iterator myIter = myBridges.iterator(); myIter.hasNext(); ) {
 +        CacheServerCreation myBridge =
 +          (CacheServerCreation) myIter.next();
 +        boolean found = false;
 +        for (Iterator otherIter = otherBridges.iterator();
 +             otherIter.hasNext(); ) {
 +          CacheServer otherBridge = (CacheServer) otherIter.next();
 +          if (myBridge.sameAs(otherBridge)) {
 +            found = true;
 +            break;
 +          }
 +        }
 +
 +        if (!found) {
 +          throw new RuntimeException(LocalizedStrings.CacheCreation_CACHE_SERVER_0_NOT_FOUND.toLocalizedString(myBridge));
 +        }
 +      }
 +
 +      { // compare connection pools
 +        Map m1 = getPools();
 +        Map m2 = (other instanceof CacheCreation)
 +          ? ((CacheCreation)other).getPools()
 +          : PoolManager.getAll();
 +        int m1Size = m1.size();
 +        {
 +          // ignore any gateway instances
 +          Iterator it1 = m1.values().iterator();
 +          while (it1.hasNext()) {
 +            Pool cp = (Pool)it1.next();
 +            if (((PoolImpl)cp).isUsedByGateway()) {
 +              m1Size--;
 +            }
 +          }
 +        }
 +        int m2Size = m2.size();
 +        {
 +          // ignore any gateway instances
 +          Iterator it2 = m2.values().iterator();
 +          while (it2.hasNext()) {
 +            Pool cp = (Pool)it2.next();
 +            if (((PoolImpl)cp).isUsedByGateway()) {
 +              m2Size--;
 +            }
 +          }
 +        }
 +        if (m2Size == 1) {
 +          // if it is just the DEFAULT pool then ignore it
 +          Pool p = (Pool)m2.values().iterator().next();
 +          if (p.getName().equals("DEFAULT")) {
 +            m2Size = 0;
 +          }
 +        }
 +        
 +        if (m1Size != m2Size) {
 +          throw new RuntimeException("pool sizes differ m1Size=" + m1Size
 +                                     + " m2Size=" + m2Size
 +                                     + " m1=" + m1.values()
 +                                     + " m2=" + m2.values());
 +        }
 +        
 +        if (m1Size > 0) {
 +          Iterator it1 = m1.values().iterator();
 +          while (it1.hasNext()) {
 +            PoolImpl cp = (PoolImpl)it1.next();
 +            // ignore any gateway instances
 +            if (!(cp).isUsedByGateway()) {
 +              cp.sameAs(m2.get(cp.getName()));
 +            }
 +          }
 +        }
 +      }
 +
 +      // compare disk stores
 +      for (Iterator myIter = diskStores.values().iterator(); myIter.hasNext(); ) {
 +        DiskStoreAttributesCreation dsac = (DiskStoreAttributesCreation)myIter.next();
 +        String name = dsac.getName();
 +        DiskStore ds = other.findDiskStore(name);
 +        if (ds == null) {
 +          getLogger().fine("Disk store " + name+" not found.");
 +          throw new RuntimeException(LocalizedStrings.CacheCreation_DISKSTORE_NOTFOUND_0.toLocalizedString(name));
 +        } else {
 +          if (!dsac.sameAs(ds)) {
 +            getLogger().fine("Attributes for disk store " + name + " do not match");
 +            throw new RuntimeException(LocalizedStrings.CacheCreation_ATTRIBUTES_FOR_DISKSTORE_0_DO_NOT_MATCH.toLocalizedString(name));
 +          }
 +        }
 +      }
 +
 +      Map myNamedAttributes = this.listRegionAttributes();
 +      Map otherNamedAttributes = other.listRegionAttributes();
 +      if (myNamedAttributes.size() != otherNamedAttributes.size()) {
 +        throw new RuntimeException(LocalizedStrings.CacheCreation_NAMEDATTRIBUTES_SIZE.toLocalizedString());
 +      }
 +
 +      for (Iterator myIter = myNamedAttributes.entrySet().iterator();
 +           myIter.hasNext(); ) {
 +        Map.Entry myEntry = (Map.Entry) myIter.next();
 +        String myId = (String) myEntry.getKey();
 +        Assert.assertTrue(myEntry.getValue() instanceof RegionAttributesCreation,
 +                          "Entry value is a " + myEntry.getValue().getClass().getName());
 +        RegionAttributesCreation myAttrs =
 +          (RegionAttributesCreation) myEntry.getValue();
 +        RegionAttributes otherAttrs = other.getRegionAttributes(myId);
 +        if (otherAttrs == null) {
 +          getLogger().fine("No attributes for " + myId);
 +          throw new RuntimeException(LocalizedStrings.CacheCreation_NO_ATTRIBUTES_FOR_0.toLocalizedString(myId));
 +
 +        } else {
 +          if (!myAttrs.sameAs(otherAttrs)) {
 +            getLogger().fine("Attributes for " + myId +
 +                             " do not match");
 +            throw new RuntimeException(LocalizedStrings.CacheCreation_ATTRIBUTES_FOR_0_DO_NOT_MATCH.toLocalizedString(myId));
 +          }
 +        }
 +      }
 +
 +      Collection myRoots = this.roots.values();
 +      Collection otherRoots = other.rootRegions();
 +      if (myRoots.size() != otherRoots.size()) {
 +        throw new RuntimeException(LocalizedStrings.CacheCreation_ROOTS_SIZE.toLocalizedString());
 +      }
 +      Iterator it = myRoots.iterator();
 +      while (it.hasNext()) {
 +        RegionCreation r = (RegionCreation)it.next();
 +        Region r2 = other.getRegion(r.getName());
 +        if (r2 == null) {
 +          throw new RuntimeException(LocalizedStrings.CacheCreation_NO_ROOT_0.toLocalizedString(r.getName()));
 +        } else if (!r.sameAs(r2)) {
 +          throw new RuntimeException(LocalizedStrings.CacheCreation_REGIONS_DIFFER.toLocalizedString());
 +        }
 +      }
 +
 +      // If both have a listener, make sure they are equal.
 +      if (getCacheTransactionManager() != null) {
 +        // Currently the GemFireCache always has a CacheTransactionManager,
 +        // whereas that is not true for CacheTransactionManagerCreation.
 +
 +        List otherTxListeners =
 +          Arrays.asList(other.getCacheTransactionManager().getListeners());
 +        List thisTxListeners =
 +          Arrays.asList(getCacheTransactionManager().getListeners());
 +
 +        if (!thisTxListeners.equals(otherTxListeners)) {
 +          throw new RuntimeException(LocalizedStrings.CacheCreation_TXLISTENER.toLocalizedString());
 +        }
 +      }
 +    }
 +
 +    if (hasResourceManager()) {
 +      getResourceManager().sameAs(other.getResourceManager());
 +    }
 +
 +    return true;
 +  }
 +
 +  //////////  Inherited methods that don't do anything  //////////
 +
 +  public void close() {
 +    throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +  }
 +
 +  public void close(boolean keepalive) {
 +    throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +  }
 +
 +//  public Region createRootRegion(RegionAttributes aRegionAttributes)
 +//    throws RegionExistsException, TimeoutException  {
 +//
 +//    throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +//  }
 +
 +  // see Cache.isReconnecting()
 +  public boolean isReconnecting() {
 +    throw new UnsupportedOperationException();
 +  }
 +
 +  // see Cache.waitUntilReconnected(long, TimeUnit)
 +  public boolean waitUntilReconnected(long time, TimeUnit units) throws InterruptedException {
 +    throw new UnsupportedOperationException();
 +  }
 +  
 +  // see Cache.stopReconnecting()
 +  public void stopReconnecting() {
 +    throw new UnsupportedOperationException();
 +  }
 +  
 +  // see Cache.getReconnectedCache()
 +  public Cache getReconnectedCache() {
 +    throw new UnsupportedOperationException();
 +  }
 +  
 +  public LogWriter getLogger() {
 +    return this.logWriter;
 +  }
 +
 +  public LogWriter getSecurityLogger() {
 +    return this.securityLogWriter;
 +  }
 +
 +  public LogWriterI18n getLoggerI18n() {
 +    return this.logWriter.convertToLogWriterI18n();
 +  }
 +  
 +  public LogWriterI18n getSecurityLoggerI18n() {
 +    return this.securityLogWriter.convertToLogWriterI18n();
 +  }
 +  
 +  public DistributedSystem getDistributedSystem() {
 +    throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +  }
 +
 +  public boolean isClosed(){
 +    throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +  }
 +
 +  public String getName() {
 +    throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +  }
 +  
 +  public CancelCriterion getCancelCriterion() {
 +    throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +  }
 +
 +  public com.gemstone.gemfire.cache.query.QueryService getQueryService() {
 +    return queryService;
 +  }
 +    
 +  /**
 +   * @since 6.5
 +   */
 +  public <K,V> RegionFactory<K,V> createRegionFactory(RegionShortcut atts) {
 +    throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +  }
 +  /**
 +   * @since 6.5
 +   */
 +  public <K,V> RegionFactory<K,V> createRegionFactory() {
 +    throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +  }
 +  /**
 +   * @since 6.5
 +   */
 +  public <K,V> RegionFactory<K,V> createRegionFactory(String regionAttributesId) {
 +    throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +  }
 +  /**
 +   * @since 6.5
 +   */
 +  public <K,V> RegionFactory<K,V> createRegionFactory(RegionAttributes<K,V> regionAttributes) {
 +    throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +  }
 +
 +  public Region createVMRegion(String name, RegionAttributes attrs) throws RegionExistsException, TimeoutException {
 +    return createRegion(name, attrs);
 +  }
 +  public Region createRegion(String name, RegionAttributes attrs) throws RegionExistsException, TimeoutException {
 +    if (attrs instanceof RegionAttributesCreation) {
 +      ((RegionAttributesCreation) attrs).inheritAttributes(this);
 +      ((RegionAttributesCreation) attrs).prepareForValidation();
 +    }
 +    AttributesFactory.validateAttributes(attrs);
 +    RegionCreation region = new RegionCreation(this, name, null);
 +    region.setAttributes(attrs);
 +    this.addRootRegion(region);
 +    return region;
 +  }
 +  public Region createRegion(String name, String refid) throws RegionExistsException, TimeoutException {
 +    RegionCreation region = new RegionCreation(this, name, refid);
 +    this.addRootRegion(region);
 +    return region;
 +  }
 +
 +  public Region getRegion(String path) {
 +    if (path.indexOf('/') != -1) {
 +      throw new UnsupportedOperationException();
 +    }
 +    return (Region)this.roots.get(path);
 +  }
 +
 +  public CacheServer addCacheServer() {
 +    return addCacheServer(false);
 +  }
 +  
 +  public CacheServer addCacheServer(boolean isGatewayReceiver) {
 +    CacheServer bridge = new CacheServerCreation(this, false);
 +    this.bridgeServers.add(bridge);
 +    return bridge;
 +  }
 +
 +  public void addDeclarableProperties(final Declarable declarable, final Properties properties) {
 +    this.declarablePropertiesMap.put(declarable, properties);
 +  }
 +  
 +  public List getCacheServers() {
 +    return this.bridgeServers;
 +  }
 +  
 +  public GatewaySender addGatewaySender(GatewaySender sender){
 +    this.gatewaySenders.add(sender);
 +    return sender;
 +  }
 +  
 +  public GatewayReceiver addGatewayReceiver(GatewayReceiver receiver){
 +    this.gatewayReceivers.add(receiver);
 +    return receiver;
 +  }
 +  
 +  public AsyncEventQueue addAsyncEventQueue(AsyncEventQueue asyncEventQueue) {
 +    this.asyncEventQueues.add(asyncEventQueue);
 +    return asyncEventQueue;
 +  }
 +  
 +  public Set<GatewaySender> getGatewaySenders(){
 +    Set<GatewaySender> tempSet = new HashSet<GatewaySender>();
 +    for (GatewaySender sender : this.gatewaySenders) {
 +      if (!((AbstractGatewaySender)sender).isForInternalUse()) {
 +        tempSet.add(sender);
 +      }
 +    }
 +    return tempSet;
 +  }
 +  
 +  public GatewaySender getGatewaySender(String Id) {
 +    for (GatewaySender sender : this.gatewaySenders) {
 +      if (sender.getId().equals(Id)) {
 +        return sender;
 +      }
 +    }
 +    return null;
 +  }
 +  
 +//  public GatewayReceiver addGatewayReceiver(){
 +//    GatewayReceiverCreation receiver = new GatewayReceiverCreation();
 +//    this.gatewayReceivers.add(receiver);
 +//    return receiver;
 +//  }
 +//  
 +  public Set<GatewayReceiver>  getGatewayReceivers(){
 +    return this.gatewayReceivers;
 +  }
 +  
 +  public Set<AsyncEventQueue> getAsyncEventQueues() {
 +    return this.asyncEventQueues;
 +  }
 +  
 +  public AsyncEventQueue getAsyncEventQueue(String id) {
 +    for (AsyncEventQueue asyncEventQueue : this.asyncEventQueues) {
 +      if (asyncEventQueue.getId().equals(id)) {
 +        return asyncEventQueue;
 +      }
 +    }
 +    return null;
 +  }
 +
 +  public void setIsServer(boolean isServer) {
 +    this.isServer = isServer;
 +    this.hasServer = true;
 +  }
 +
 +  public boolean isServer() {
 +	if (!this.isServer) {  
 +	  return (this.bridgeServers.size() > 0);
 +	}
 +	else {
 +	  return true;	
 +	}
 +  }
 +
 +  boolean hasServer() {
 +    return this.hasServer;
 +  }
 +
 +  public void setDynamicRegionFactoryConfig(DynamicRegionFactory.Config v) {
 +    this.dynamicRegionFactoryConfig = v;
 +    this.hasDynamicRegionFactory = true;
 +  }
 +
 +  boolean hasDynamicRegionFactory() {
 +    return this.hasDynamicRegionFactory;
 +  }
 +
 +  public DynamicRegionFactory.Config getDynamicRegionFactoryConfig() {
 +    return this.dynamicRegionFactoryConfig;
 +  }
 +  
 +  public CacheTransactionManager getCacheTransactionManager() {
 +    return this.txMgrCreation;
 +  }
 +
 +  /**
 +   * Implementation of {@link Cache#setCopyOnRead}
 +   * @since 4.0
 +   */
 +  public void setCopyOnRead(boolean copyOnRead) {
 +    this.copyOnRead = copyOnRead;
 +    this.hasCopyOnRead = true;
 +  }
 +
 +  /**
 +   * Implementation of {@link Cache#getCopyOnRead}
 +   * @since 4.0
 +   */
 +  public boolean getCopyOnRead() {
 +    return this.copyOnRead;
 +  }
 +
 +  boolean hasCopyOnRead() {
 +    return this.hasCopyOnRead;
 +  }
 +
 +  /**
 +   * Adds a CacheTransactionManagerCreation for this Cache (really just a
 +   * placeholder since a CacheTransactionManager is really a Cache singleton)
 +   * @since 4.0
 +   * @see GemFireCacheImpl
 +   */
 +  public void
 +    addCacheTransactionManagerCreation(CacheTransactionManagerCreation txm) {
 +    this.txMgrCreation = txm;
 +  }
 +
 +/**
 + * @return Context jndi context associated with the Cache.
 + */
 +  public Context getJNDIContext() {
 +    return JNDIInvoker.getJNDIContext();
 +  }
 +  
 +  // It's not used
 +  public DiskStore findDiskStore(String storeName) {
 +    String s = storeName;
 +    if (s == null) {
 +      s = GemFireCacheImpl.getDefaultDiskStoreName();
 +    }
 +    return (DiskStore)this.diskStores.get(s);
 +  }
 +  public void addDiskStore(DiskStore ds) {
 +    this.diskStores.put(ds.getName(), ds);
 +  }
 +
 +  /**
 +   * Returns the DiskStore list
 +   *
 +   * @since prPersistSprint2
 +   */
 +  public Collection<DiskStoreImpl> listDiskStores() {
 +    return this.diskStores.values();
 +  }
 +  
 +  public void setDiskStore(String name, DiskStoreAttributesCreation dsac) {
 +//    Assert.assertTrue(ds instanceof DiskStoreAttributesCreation,
 +//                      "Attributes are a " + ds.getClass().getName());
 +    this.diskStores.put(name, dsac);
 +  }
 +
 +  public RegionAttributes getRegionAttributes(String id) {
 +    return (RegionAttributes) this.namedRegionAttributes.get(id);
 +  }
 +
 +  public void setRegionAttributes(String id, RegionAttributes attrs) {
 +    RegionAttributes a = attrs;
 +    if (!(a instanceof RegionAttributesCreation)) {
 +      a = new RegionAttributesCreation(this, a, false);
 +    }
 +    this.namedRegionAttributes.put(id, a);
 +    this.regionAttributesNames.add(id);
 +  }
 +
 +  public Map listRegionAttributes() {
 +    return Collections.unmodifiableMap(this.namedRegionAttributes);
 +  }
 +
 +  public void loadCacheXml(InputStream is)
 +    throws TimeoutException, CacheWriterException,
 +           RegionExistsException {
 +
 +    throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +  }
 +
 +  public void readyForEvents() {
 +    throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +  }
 +
 +  private final PoolManagerImpl pm = new PoolManagerImpl(false);
 +  private volatile FunctionServiceCreation functionServiceCreation;
 +
 +  public Map getPools() {
 +    return this.pm.getMap();
 +  }
 +
 +  public PoolFactory createPoolFactory() {
 +    return (new PoolFactoryImpl(this.pm)).setStartDisabled(true);
 +  }
 +
 +  public Pool findPool(String name) {
 +    return this.pm.find(name);
 +  }
 +  
 +  public void setFunctionServiceCreation(FunctionServiceCreation f) {
 +    this.functionServiceCreation = f;
 +  }
 +  public FunctionServiceCreation getFunctionServiceCreation() {
 +    return this.functionServiceCreation;
 +  }
 +
 +  private volatile boolean hasResourceManager = false;
 +  private volatile ResourceManagerCreation resourceManagerCreation;
 +  public void setResourceManagerCreation(ResourceManagerCreation rmc) {
 +    this.hasResourceManager = true;
 +    this.resourceManagerCreation = rmc;
 +  }
 +  public ResourceManagerCreation getResourceManager() {
 +    return this.resourceManagerCreation;
 +  }
 +  public boolean hasResourceManager() {
 +    return this.hasResourceManager;
 +  }
 +  
 +  private volatile boolean hasSerializerRegistration = false;
 +  private volatile SerializerCreation serializerCreation;
 +  public void setSerializerCreation(SerializerCreation sc) {
 +    this.hasSerializerRegistration = true;
 +    this.serializerCreation = sc;
 +  }
 +  public SerializerCreation getSerializerCreation() {
 +    return this.serializerCreation;
 +  }
 +  public boolean hasSerializerCreation() {
 +    return this.hasSerializerRegistration;
 +  }
 +  public FunctionService getFunctionService(){
 +    throw new UnsupportedOperationException();
 +  }
 +
 +  public void addBackup(File backup) {
 +    this.backups.add(backup);
 +  }
 +  
 +  public List<File> getBackupFiles() {
 +    return Collections.unmodifiableList(this.backups);
 +  }
 +  
 +  public GatewaySenderFactory createGatewaySenderFactory(){
 +    return WANServiceProvider.createGatewaySenderFactory(this);
 +  }
 +  
 +  public GatewayReceiverFactory createGatewayReceiverFactory() {
 +    return WANServiceProvider.createGatewayReceiverFactory(this);
 +  }
 +  
 +  public AsyncEventQueueFactory createAsyncEventQueueFactory() {
 +    return new AsyncEventQueueFactoryImpl(this);
 +  }
 +
 +  public void setPdxReadSerialized(boolean readSerialized) {
 +    cacheConfig.setPdxReadSerialized(readSerialized);
 +  }
 +  public void setPdxIgnoreUnreadFields(boolean ignore) {
 +    cacheConfig.setPdxIgnoreUnreadFields(ignore);
 +  }
 +
 + public void setPdxSerializer(PdxSerializer serializer) {
 +   cacheConfig.setPdxSerializer(serializer);
 + }
 +
 + public void setPdxDiskStore(String diskStore) {
 +   cacheConfig.setPdxDiskStore(diskStore);
 + }
 +
 + public void setPdxPersistent(boolean persistent) {
 +   cacheConfig.setPdxPersistent(persistent);
 + }
 +  
 +  /**
 +   * Returns whether PdxInstance is preferred for PDX types instead of Java object.
 +   * @see com.gemstone.gemfire.cache.CacheFactory#setPdxReadSerialized(boolean)
 +   *
 +   * @since 6.6
 +   */
 +  public boolean getPdxReadSerialized() {
 +     return cacheConfig.isPdxReadSerialized();
 +  }
 +
 +  public PdxSerializer getPdxSerializer() {
 +    return cacheConfig.getPdxSerializer();
 +  }
 +
 +  public String getPdxDiskStore() {
 +    return cacheConfig.getPdxDiskStore();
 +  }
 +
 +  public boolean getPdxPersistent() {
 +    return cacheConfig.isPdxPersistent();
 +  }
 +  public boolean getPdxIgnoreUnreadFields() {
 +    return cacheConfig.getPdxIgnoreUnreadFields();
 +  }
 +
 +
 +  public CacheConfig getCacheConfig() {
 +    return cacheConfig;
 +  }
 +
 +  /* (non-Javadoc)
 +   * @see com.gemstone.gemfire.cache.Cache#getMembers()
 +   */
 +  public Set<DistributedMember> getMembers() {
 +    return Collections.EMPTY_SET;
 +  }
 +
 +  /* (non-Javadoc)
 +   * @see com.gemstone.gemfire.cache.Cache#getAdminMembers()
 +   */
 +  public Set<DistributedMember> getAdminMembers() {
 +    return Collections.EMPTY_SET;
 +  }
 +
 +  /* (non-Javadoc)
 +   * @see com.gemstone.gemfire.cache.Cache#getMembers(com.gemstone.gemfire.cache.Region)
 +   */
 +  public Set<DistributedMember> getMembers(Region r) {
 +    return Collections.EMPTY_SET;
 +  }
 +
 +  private Declarable initializer = null;
 +  private Properties initializerProps = null;
 +  
 +  public Declarable getInitializer() {
 +    return this.initializer;
 +  }
 +  public Properties getInitializerProps() {
 +    return this.initializerProps;
 +  }
 +  
 +  public void setInitializer(Declarable d, Properties props) {
 +    this.initializer = d;
 +    this.initializerProps = props;
 +  }
 +  
 +  protected void runInitializer() {
 +    if (getInitializer() != null) {
 +      getInitializer().init(getInitializerProps());
 +    }
 +  }
 +
 +  public void setGatewayConflictResolver(GatewayConflictResolver g) {
 +    this.gatewayConflictResolver = g;
 +  }
 +
 +  public GatewayConflictResolver getGatewayConflictResolver() {
 +    return this.gatewayConflictResolver;
 +  }
 +    
 +  public PdxInstanceFactory createPdxInstanceFactory(String className) {
 +    throw new UnsupportedOperationException();
 +  }
 +  public PdxInstanceFactory createPdxInstanceFactory(String className, boolean b) {
 +    throw new UnsupportedOperationException();
 +  }
 +  public PdxInstance createPdxEnum(String className, String enumName, int enumOrdinal) {
 +    throw new UnsupportedOperationException();
 +  }
 +  
 +  public CacheSnapshotService getSnapshotService() {
 +    throw new UnsupportedOperationException();
 +  }
 +
 +  /**
 +   * @see Extensible#getExtensionPoint()
 +   * @since 8.1
 +   */
 +  @Override
 +  public ExtensionPoint<Cache> getExtensionPoint() {
 +    return extensionPoint;
 +  }
 +  
 +  @Override
 +  public Collection<HDFSStoreImpl> getHDFSStores() {
 +    return this.hdfsStores.values();
 +  }
 +
 +  public void addHDFSStore(String name, HDFSStoreCreation hs) {
 +    this.hdfsStores.put(name, hs);
 +  }
 +
 +  
 +
 +  @Override
 +  public DistributedMember getMyId() {
 +    return null;
 +  }
 +
 +  @Override
 +  public Collection<DiskStoreImpl> listDiskStoresIncludingDefault() {
 +    return null;
 +  }
 +
 +  @Override
 +  public Collection<DiskStoreImpl> listDiskStoresIncludingRegionOwned() {
 +    return null;
 +  }
 +
 +  @Override
 +  public CqService getCqService() {
 +    return null;
 +  }
 +
 +  public QueryService queryService = new com.gemstone.gemfire.cache.query.QueryService() {
 +
 +    private Map<String, List> indexes = new HashMap<String, List>();
 +    
 +    @Override
 +    public Query newQuery(String queryString) {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +    @Override
 +    public Index createHashIndex(String indexName, String indexedExpression,
 +        String regionPath) throws IndexInvalidException,
 +        IndexNameConflictException, IndexExistsException,
 +        RegionNotFoundException, UnsupportedOperationException {
 +      return createHashIndex(indexName, indexedExpression, regionPath, "");
 +    }
 +
 +    @Override
 +    public Index createHashIndex(String indexName, String indexedExpression,
 +        String regionPath, String imports) throws IndexInvalidException,
 +        IndexNameConflictException, IndexExistsException,
 +        RegionNotFoundException, UnsupportedOperationException {
 +      return createIndex(indexName, IndexType.HASH, indexedExpression, regionPath, imports);
 +    }
 +
 +    @Override
 +    public Index createIndex(String indexName, IndexType indexType,
 +        String indexedExpression, String fromClause)
 +        throws IndexInvalidException, IndexNameConflictException,
 +        IndexExistsException, RegionNotFoundException,
 +        UnsupportedOperationException {
 +      return createIndex(indexName, indexType, indexedExpression, fromClause, "");
 +    }
 +
 +    @Override
 +    /** 
 +     * Due to not having the full implementation to determine region names etc
 +     * this implementation will only match a single region with no alias at this time
 +     */
 +    public Index createIndex(String indexName, IndexType indexType,
 +        String indexedExpression, String fromClause, String imports)
 +        throws IndexInvalidException, IndexNameConflictException,
 +        IndexExistsException, RegionNotFoundException,
 +        UnsupportedOperationException {
 +      IndexCreationData indexData = new IndexCreationData(indexName);
 +      indexData.setFunctionalIndexData(fromClause, indexedExpression, imports);
 +      indexData.setIndexType(indexType.toString());
 +      List indexesForRegion = indexes.get(fromClause);
 +      if (indexesForRegion == null) {
 +        indexesForRegion = new ArrayList();
 +        indexes.put(fromClause, indexesForRegion);
 +      }
 +      indexesForRegion.add(indexData);
 +      return indexData;
 +    }
 +
 +    @Override
 +    public Index createIndex(String indexName, String indexedExpression,
 +        String regionPath) throws IndexInvalidException,
 +        IndexNameConflictException, IndexExistsException,
 +        RegionNotFoundException, UnsupportedOperationException {
 +      return createIndex(indexName, indexedExpression, regionPath, "");
 +    }
 +
 +    @Override
 +    public Index createIndex(String indexName, String indexedExpression,
 +        String regionPath, String imports) throws IndexInvalidException,
 +        IndexNameConflictException, IndexExistsException,
 +        RegionNotFoundException, UnsupportedOperationException {
 +       return createIndex(indexName, IndexType.FUNCTIONAL, indexedExpression, regionPath, imports);
 +
 +    }
 +
 +    @Override
 +    public Index createKeyIndex(String indexName, String indexedExpression,
 +        String regionPath) throws IndexInvalidException,
 +        IndexNameConflictException, IndexExistsException,
 +        RegionNotFoundException, UnsupportedOperationException {
 +      return createIndex(indexName, IndexType.PRIMARY_KEY, indexedExpression, regionPath, "");
 +
 +    }
 +
 +    @Override
 +    public Index getIndex(Region<?, ?> region, String indexName) {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +    @Override
 +    public Collection<Index> getIndexes() {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +    @Override
 +    public Collection<Index> getIndexes(Region<?, ?> region) {
 +      return indexes.get(region.getFullPath());
 +    }
 +
 +    @Override
 +    public Collection<Index> getIndexes(Region<?, ?> region,
 +        IndexType indexType) {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +    @Override
 +    public void removeIndex(Index index) {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());        
 +    }
 +
 +    @Override
 +    public void removeIndexes() {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());        
 +    }
 +
 +    @Override
 +    public void removeIndexes(Region<?, ?> region) {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());        
 +    }
 +
 +    @Override
 +    public CqQuery newCq(String queryString, CqAttributes cqAttr)
 +        throws QueryInvalidException, CqException {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +    @Override
 +    public CqQuery newCq(String queryString, CqAttributes cqAttr,
 +        boolean isDurable) throws QueryInvalidException, CqException {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +    @Override
 +    public CqQuery newCq(String name, String queryString, CqAttributes cqAttr)
 +        throws QueryInvalidException, CqExistsException, CqException {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +    @Override
 +    public CqQuery newCq(String name, String queryString,
 +        CqAttributes cqAttr, boolean isDurable) throws QueryInvalidException,
 +        CqExistsException, CqException {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +    @Override
 +    public void closeCqs() {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());        
 +    }
 +
 +    @Override
 +    public CqQuery[] getCqs() {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +    @Override
 +    public CqQuery[] getCqs(String regionName) throws CqException {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +    @Override
 +    public CqQuery getCq(String cqName) {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +    @Override
 +    public void executeCqs() throws CqException {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +    @Override
 +    public void stopCqs() throws CqException {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +    @Override
 +    public void executeCqs(String regionName) throws CqException {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +    @Override
 +    public void stopCqs(String regionName) throws CqException {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +    @Override
 +    public List<String> getAllDurableCqsFromServer() throws CqException {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +    @Override
 +    public CqServiceStatistics getCqStatistics() {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +
 +    @Override
 +    public void defineKeyIndex(String indexName, String indexedExpression,
 +        String fromClause) throws RegionNotFoundException {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +    @Override
 +    public void defineHashIndex(String indexName, String indexedExpression,
 +        String regionPath) throws RegionNotFoundException {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +    @Override
 +    public void defineHashIndex(String indexName, String indexedExpression,
 +        String regionPath, String imports) throws RegionNotFoundException {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +    @Override
 +    public void defineIndex(String indexName, String indexedExpression,
 +        String regionPath) throws RegionNotFoundException {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +    @Override
 +    public void defineIndex(String indexName, String indexedExpression,
 +        String regionPath, String imports) throws RegionNotFoundException {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +    @Override
 +    public List<Index> createDefinedIndexes() throws MultiIndexCreationException {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +
 +    @Override
 +    public boolean clearDefinedIndexes() {
 +      throw new UnsupportedOperationException(LocalizedStrings.SHOULDNT_INVOKE.toLocalizedString());
 +    }
 +    
 +  };
 +
 +  @Override
 +  public <T extends CacheService> T getService(Class<T> clazz) {
 +    throw new UnsupportedOperationException();
 +  }
 +}


[29/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/CacheClientProxy.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/CacheClientProxy.java
index adaf400,0000000..69ae6d8
mode 100755,000000..100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/CacheClientProxy.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/CacheClientProxy.java
@@@ -1,3110 -1,0 +1,3112 @@@
 +/*
 + * 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.tier.sockets;
 +
 +import java.io.ByteArrayInputStream;
 +import java.io.DataInputStream;
 +import java.io.IOException;
 +import java.net.Socket;
 +import java.net.SocketException;
 +import java.nio.ByteBuffer;
 +import java.util.Arrays;
 +import java.util.Collections;
 +import java.util.Date;
 +import java.util.HashMap;
 +import java.util.HashSet;
 +import java.util.Iterator;
 +import java.util.List;
 +import java.util.Map;
 +import java.util.Set;
 +import java.util.concurrent.ConcurrentLinkedQueue;
 +import java.util.concurrent.atomic.AtomicBoolean;
 +import java.util.concurrent.atomic.AtomicInteger;
 +import java.util.concurrent.atomic.AtomicReference;
 +import java.util.concurrent.locks.Lock;
 +import java.util.concurrent.locks.ReadWriteLock;
 +import java.util.concurrent.locks.ReentrantReadWriteLock;
 +import java.util.regex.Pattern;
 +
 +import org.apache.logging.log4j.Logger;
 +
 +import com.gemstone.gemfire.CancelException;
 +import com.gemstone.gemfire.DataSerializer;
 +import com.gemstone.gemfire.StatisticsFactory;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheClosedException;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.ClientSession;
 +import com.gemstone.gemfire.cache.DynamicRegionFactory;
 +import com.gemstone.gemfire.cache.InterestRegistrationEvent;
 +import com.gemstone.gemfire.cache.InterestResultPolicy;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionDestroyedException;
 +import com.gemstone.gemfire.cache.RegionExistsException;
 +import com.gemstone.gemfire.cache.client.internal.RegisterInterestTracker;
 +import com.gemstone.gemfire.cache.operations.DestroyOperationContext;
 +import com.gemstone.gemfire.cache.operations.InvalidateOperationContext;
 +import com.gemstone.gemfire.cache.operations.OperationContext;
 +import com.gemstone.gemfire.cache.operations.PutOperationContext;
 +import com.gemstone.gemfire.cache.operations.RegionClearOperationContext;
 +import com.gemstone.gemfire.cache.operations.RegionCreateOperationContext;
 +import com.gemstone.gemfire.cache.operations.RegionDestroyOperationContext;
 +import com.gemstone.gemfire.cache.query.CqException;
 +import com.gemstone.gemfire.cache.query.CqQuery;
 +import com.gemstone.gemfire.cache.query.internal.cq.CqService;
 +import com.gemstone.gemfire.cache.query.internal.cq.InternalCqQuery;
 +import com.gemstone.gemfire.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.internal.DistributionManager;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.internal.SystemTimer;
 +import com.gemstone.gemfire.internal.SystemTimer.SystemTimerTask;
 +import com.gemstone.gemfire.internal.Version;
 +import com.gemstone.gemfire.internal.cache.ClientServerObserver;
 +import com.gemstone.gemfire.internal.cache.ClientServerObserverHolder;
 +import com.gemstone.gemfire.internal.cache.CacheDistributionAdvisee;
 +import com.gemstone.gemfire.internal.cache.CacheDistributionAdvisor.InitialImageAdvice;
 +import com.gemstone.gemfire.internal.cache.Conflatable;
 +import com.gemstone.gemfire.internal.cache.DistributedRegion;
 +import com.gemstone.gemfire.internal.cache.EnumListenerEvent;
 +import com.gemstone.gemfire.internal.cache.EventID;
 +import com.gemstone.gemfire.internal.cache.FilterProfile;
 +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
 +import com.gemstone.gemfire.internal.cache.InterestRegistrationEventImpl;
 +import com.gemstone.gemfire.internal.cache.LocalRegion;
 +import com.gemstone.gemfire.internal.cache.PartitionedRegion;
 +import com.gemstone.gemfire.internal.cache.StateFlushOperation;
 +import com.gemstone.gemfire.internal.cache.ha.HAContainerWrapper;
 +import com.gemstone.gemfire.internal.cache.ha.HARegionQueue;
 +import com.gemstone.gemfire.internal.cache.ha.HARegionQueueAttributes;
 +import com.gemstone.gemfire.internal.cache.ha.HARegionQueueStats;
 +import com.gemstone.gemfire.internal.cache.tier.InterestType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.ClientUpdateMessageImpl.CqNameToOp;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.command.Get70;
 +import com.gemstone.gemfire.internal.cache.versions.VersionTag;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.logging.LogService;
 +import com.gemstone.gemfire.internal.logging.LoggingThreadGroup;
 +import com.gemstone.gemfire.internal.logging.log4j.LocalizedMessage;
 +import com.gemstone.gemfire.internal.logging.log4j.LogMarker;
 +import com.gemstone.gemfire.internal.security.AuthorizeRequestPP;
 +import com.gemstone.gemfire.security.AccessControl;
 +import com.gemstone.gemfire.i18n.StringId;
 +
 +/**
 + * Class <code>CacheClientProxy</code> represents the server side of the
 + * {@link CacheClientUpdater}. It queues messages to be sent from the server to
 + * the client. It then reads those messages from the queue and sends them to the
 + * client.
 + *
 + * @author Barry Oglesby
 + *
 + * @since 4.2
 + */
 +@SuppressWarnings("synthetic-access")
 +public class CacheClientProxy implements ClientSession {
 +  private static final Logger logger = LogService.getLogger();
 +  
 +  /**
 +   * The socket between the server and the client
 +   */
 +  protected Socket _socket;
 +  
 +  private final AtomicBoolean _socketClosed = new AtomicBoolean();
 +
 +  /**
 +   * A communication buffer used by each message we send to the client
 +   */
 +  protected ByteBuffer _commBuffer;
 +
 +  /**
 +   * The remote host's IP address string (cached for convenience)
 +   */
 +  protected String _remoteHostAddress;
 +
 +  /**
 +   * Concurrency: protected by synchronization of {@link #isMarkedForRemovalLock}
 +   */
 +  protected boolean isMarkedForRemoval = false;
 +  
 +  /**
 +   * @see #isMarkedForRemoval
 +   */
 +  protected final Object isMarkedForRemovalLock = new Object();
 +  
 +  /**
 +   * The proxy id of the client represented by this proxy
 +   */
 +  protected ClientProxyMembershipID proxyID;
 +
 +  /**
 +   * The GemFire cache
 +   */
 +  protected final GemFireCacheImpl _cache;
 +
 +  /**
 +   * The list of keys that the client represented by this proxy is interested in
 +   * (stored by region)
 +   */
 +  protected final ClientInterestList[] cils = new ClientInterestList[2];
 +  
 +  /**
 +   * A thread that dispatches messages to the client
 +   */
 +  protected volatile MessageDispatcher _messageDispatcher;
 +
 +  /**
 +   * The statistics for this proxy
 +   */
 +  protected final CacheClientProxyStats _statistics;
 +
 +  protected final AtomicReference _durableExpirationTask = new AtomicReference();
 +
 +  protected SystemTimer durableTimer;
 +
 +  /**
 +   * Whether this dispatcher is paused
 +   */
 +  protected volatile boolean _isPaused = true;
 +
 +  /**
 +   * True if we are connected to a client.
 +   */
 +  private volatile boolean connected = false;
 +//  /**
 +//   * A string representing interest in all keys
 +//   */
 +//  protected static final String ALL_KEYS = "ALL_KEYS";
 +//
 +  /**
 +   * True if a marker message is still in the ha queue.
 +   */
 +  private boolean markerEnqueued = false;
 +
 +  /**
 +   * The number of times to peek on shutdown before giving up and shutting down
 +   */
 +  protected static final int MAXIMUM_SHUTDOWN_PEEKS = Integer.getInteger("gemfire.MAXIMUM_SHUTDOWN_PEEKS",50).intValue();
 +
 +  /**
 +   * The number of milliseconds to wait for an offering to the message queue
 +   */
 +  protected static final int MESSAGE_OFFER_TIME = 0;
 +
 +  /**
 +   * The default maximum message queue size
 +   */
 +//   protected static final int MESSAGE_QUEUE_SIZE_DEFAULT = 230000;
 +
 +  /** The message queue size */
 +  protected final int _maximumMessageCount;
 +
 +  /**
 +   * The time (in seconds ) after which a message in the client queue will
 +   * expire.
 +   */
 +  protected final int _messageTimeToLive;
 +
 +  /**
 +   * The <code>CacheClientNotifier</code> registering this proxy.
 +   */
 +  protected final CacheClientNotifier _cacheClientNotifier;
 +
 +  /**
 +   * Defaults to true; meaning do some logging of dropped client notification
 +   * messages. Set the system property to true to cause dropped messages to NOT
 +   * be logged.
 +   */
 +  protected static final boolean LOG_DROPPED_MSGS = !Boolean
 +      .getBoolean("gemfire.disableNotificationWarnings");
 +
 +  /**
 +   * for testing purposes, delays the start of the dispatcher thread
 +   */
 +  public static boolean isSlowStartForTesting = false;
 +
 +  /**
 +   * Default value for slow starting time of dispatcher
 +   */
 +  private static final long DEFAULT_SLOW_STARTING_TIME = 5000;
 +
 +  /**
 +   * Key in the system property from which the slow starting time value will be
 +   * retrieved
 +   */
 +  private static final String KEY_SLOW_START_TIME_FOR_TESTING = "slowStartTimeForTesting";
 +
 +  private boolean isPrimary;
 +  
 +  /** @since 5.7 */
 +  protected byte clientConflation = HandShake.CONFLATION_DEFAULT;
 +  
 +  /**
 +   * Flag to indicate whether to keep a durable client's queue alive
 +   */
 +  boolean keepalive = false;
 +
 +  private AccessControl postAuthzCallback;
 +
 +  /**
 +   * For multiuser environment..
 +   */
 +  private ClientUserAuths clientUserAuths;
 +  
 +  private final Object clientUserAuthsLock = new Object();
 +  
 +  /**
 +   * The version of the client
 +   */
 +  private Version clientVersion;  
 +
 +  /**
 +   * A map of region name as key and integer as its value. Basically, it stores
 +   * the names of the regions with <code>DataPolicy</code> as EMPTY. If an 
 +   * event's region name is present in this map, it's full value (and not 
 +   * delta) is sent to the client represented by this proxy.
 +   *
 +   * @since 6.1
 +   */
 +  private volatile Map regionsWithEmptyDataPolicy = new HashMap();
 +
 +  /**
 +   * A debug flag used for testing Backward compatibility
 +   */
 +  public static boolean AFTER_MESSAGE_CREATION_FLAG = false;
 +  
 +  /**
 +   * Notify the region when a client interest registration occurs. This tells
 +   * the region to update access time when an update is to be pushed to a
 +   * client. It is enabled only for <code>PartitionedRegion</code>s
 +   * currently.
 +   */
 +  protected static final boolean NOTIFY_REGION_ON_INTEREST = Boolean
 +      .getBoolean("gemfire.updateAccessTimeOnClientInterest");   
 +  
 +  /**
 +   * The AcceptorImpl identifier to which the proxy is connected.
 +   */
 +  private final long _acceptorId;
 +  
 +  /** acceptor's setting for notifyBySubscription */
 +  private final boolean notifyBySubscription;
 +  
 +  /** To queue the events arriving during message dispatcher initialization */ 
 +  private volatile ConcurrentLinkedQueue<Conflatable> queuedEvents = new ConcurrentLinkedQueue<Conflatable>();
 +  
 +  private final Object queuedEventsSync = new Object();
 +  
 +  private volatile boolean messageDispatcherInit = false;
 +
 +  /**
 +   * A counter that keeps track of how many task iterations that have occurred
 +   * since the last ping or message. The
 +   * {@linkplain CacheClientNotifier#scheduleClientPingTask ping task}
 +   * increments it. Normal messages sent to the client reset it. If the counter
 +   * reaches 3, a ping is sent.
 +   */
 +  private final AtomicInteger pingCounter = new AtomicInteger();
 +  
 +  
 +  /** Date on which this instances was created */
 +  private Date creationDate;
 +
 +  /** true when the durable client associated with this proxy is being 
 +   * restarted and prevents cqs from being closed and drained**/
 +  private boolean drainLocked = false;
 +  private final Object drainLock = new Object();
 +
 +  /** number of cq drains that are currently in progress **/
 +  private int numDrainsInProgress = 0;
 +  private final Object drainsInProgressLock = new Object();
 +  
 +  /**
 +   * Constructor.
 +   *
 +   * @param ccn
 +   *          The <code>CacheClientNotifier</code> registering this proxy
 +   * @param socket
 +   *          The socket between the server and the client
 +   * @param proxyID
 +   *          representing the Connection Proxy of the clien
 +   * @param isPrimary
 +   *          The boolean stating whether this prozxy is primary
 +   * @throws CacheException {
 +   */
 +  protected CacheClientProxy(CacheClientNotifier ccn, Socket socket,
 +      ClientProxyMembershipID proxyID, boolean isPrimary, byte clientConflation, 
 +      Version clientVersion, long acceptorId, boolean notifyBySubscription)
 +      throws CacheException {
 +    initializeTransientFields(socket, proxyID, isPrimary, clientConflation, clientVersion);
 +    this._cacheClientNotifier = ccn;
 +    this._cache = (GemFireCacheImpl)ccn.getCache();
 +    this._maximumMessageCount = ccn.getMaximumMessageCount();    
 +    this._messageTimeToLive = ccn.getMessageTimeToLive();
 +    this._acceptorId = acceptorId;
 +    this.notifyBySubscription = notifyBySubscription;
 +    StatisticsFactory factory = this._cache.getDistributedSystem();
 +    this._statistics = new CacheClientProxyStats(factory, 
 +        "id_"+this.proxyID.getDistributedMember().getId()+ "_at_"+ this._remoteHostAddress + ":" + this._socket.getPort());
 +
 +    // Create the interest list
 +    this.cils[RegisterInterestTracker.interestListIndex] =
 +      new ClientInterestList(this, this.proxyID);
 +    // Create the durable interest list
 +    this.cils[RegisterInterestTracker.durableInterestListIndex] =
 +      new ClientInterestList(this, this.getDurableId());
 +    this.postAuthzCallback = null;
 +    this._cacheClientNotifier.getAcceptorStats().incCurrentQueueConnections();
 +    this.creationDate = new Date();
 +    initializeClientAuths();
 +  }
 +  
 +  private void initializeClientAuths()
 +  {
 +    if(AcceptorImpl.isPostAuthzCallbackPresent())
 +      this.clientUserAuths = ServerConnection.getClientUserAuths(this.proxyID);
 +  }
 +
 +  private void reinitializeClientAuths()
 +  {
 +    if (this.clientUserAuths != null && AcceptorImpl.isPostAuthzCallbackPresent()) {
 +      synchronized (this.clientUserAuthsLock) {
 +        ClientUserAuths newClientAuth = ServerConnection.getClientUserAuths(this.proxyID);
 +        newClientAuth.fillPreviousCQAuth(this.clientUserAuths);
 +        this.clientUserAuths = newClientAuth;
 +      }        
 +    }
 +  }
 +  
 +  public void setPostAuthzCallback(AccessControl authzCallback) {
 +    //TODO:hitesh synchronization
 +    synchronized (this.clientUserAuthsLock) {
 +      if (this.postAuthzCallback != null)
 +        this.postAuthzCallback.close();
 +      this.postAuthzCallback = authzCallback;
 +    }
 +  }
 +  
 +  public void setCQVsUserAuth(String cqName, long uniqueId, boolean isDurable)
 +  {
 +    if(postAuthzCallback == null) //only for multiuser
 +    {
 +      if(this.clientUserAuths != null)
 +        this.clientUserAuths.setUserAuthAttributesForCq(cqName, uniqueId, isDurable);
 +    }
 +  }
 +
 +  private void initializeTransientFields(Socket socket,
 +      ClientProxyMembershipID pid, boolean ip,  byte cc, Version vers) {
 +    this._socket = socket;
 +    this.proxyID = pid;
 +    this.connected = true;
 +    {
 +      int bufSize = 1024;
 +      try {
 +        bufSize = _socket.getSendBufferSize();
 +        if (bufSize < 1024) {
 +          bufSize = 1024;
 +        }
 +      } catch (SocketException ignore) {
 +      }
 +      this._commBuffer = ServerConnection.allocateCommBuffer(bufSize, socket);
 +    }
 +    this._remoteHostAddress = socket.getInetAddress().getHostAddress();
 +    this.isPrimary = ip;
 +    this.clientConflation = cc;
 +    this.clientVersion = vers;
 +  }
 +
 +  public boolean isMarkerEnqueued() {
 +    return markerEnqueued;
 +  }
 +
 +  public void setMarkerEnqueued(boolean bool) {
 +    markerEnqueued = bool;
 +  }
 +
 +  public long getAcceptorId(){
 +    return this._acceptorId;
 +  }
 +
 +  /**
 +   * @return the notifyBySubscription
 +   */
 +  public boolean isNotifyBySubscription() {
 +    return this.notifyBySubscription;
 +  }
 +
 +  
 +  /**
 +   * Returns the DistributedMember represented by this proxy
 +   */
 +  public ClientProxyMembershipID getProxyID()
 +  {
 +    return this.proxyID;
 +  }
 +  
 +  // the following code was commented out simply because it was not used
 +//   /**
 +//    * Determines if the proxy represents the client host (and only the host, not
 +//    * necessarily the exact VM running on the host)
 +//    *
 +//    * @return Whether the proxy represents the client host
 +//    */
 +//   protected boolean representsClientHost(String clientHost)
 +//   {
 +//     // [bruce] TODO BUGBUGBUG: this should compare InetAddresses, not Strings
 +//     return this._remoteHostAddress.equals(clientHost);
 +//   }
 +
 +//   protected boolean representsClientVM(DistributedMember remoteMember)
 +//   {
 +//     // logger.warn("Is input port " + clientPort + " contained in " +
 +//     // logger.warn("Does input host " + clientHost + " equal " +
 +//     // this._remoteHostAddress+ ": " + representsClientHost(clientHost));
 +//     // logger.warn("representsClientVM: " +
 +//     // (representsClientHost(clientHost) && containsPort(clientPort)));
 +//     return (proxyID.getDistributedMember().equals(remoteMember));
 +//   }
 +
 +//   /**
 +//    * Determines if the CacheClientUpdater proxied by this instance is listening
 +//    * on the input clientHost and clientPort
 +//    *
 +//    * @param clientHost
 +//    *          The host name of the client to compare
 +//    * @param clientPort
 +//    *          The port number of the client to compare
 +//    *
 +//    * @return Whether the CacheClientUpdater proxied by this instance is
 +//    *         listening on the input clientHost and clientPort
 +//    */
 +//   protected boolean representsCacheClientUpdater(String clientHost,
 +//       int clientPort)
 +//   {
 +//     return (clientPort == this._socket.getPort() && representsClientHost(clientHost));
 +//   }
 +
 +  protected boolean isMember(ClientProxyMembershipID memberId)
 +  {
 +    return this.proxyID.equals(memberId);
 +  }
 +
 +  protected boolean isSameDSMember(ClientProxyMembershipID memberId)
 +  {
 +    return this.proxyID.isSameDSMember(memberId);
 +  }
 +
 +  /**
 +   * Set the queue keepalive option
 +   *
 +   * @param option whether to keep the durable client's queue alive
 +   */
 +  protected void setKeepAlive(boolean option) {
 +    this.keepalive = option;
 +  }
 +
 +  /**
 +   * Returns the socket between the server and the client
 +   *
 +   * @return the socket between the server and the client
 +   */
 +  protected Socket getSocket()
 +  {
 +    return this._socket;
 +  }
 +  
 +  public String getSocketHost()
 +  {
 +    return this._socket.getInetAddress().getHostAddress();
 +  }
 +  
 +  protected ByteBuffer getCommBuffer() {
 +    return this._commBuffer;
 +  }
 +
 +  /**
 +   * Returns the remote host's IP address string
 +   *
 +   * @return the remote host's IP address string
 +   */
 +  protected String getRemoteHostAddress()
 +  {
 +    return this._remoteHostAddress;
 +  }
 +
 +  /**
 +   * Returns the remote host's port
 +   *
 +   * @return the remote host's port
 +   */
 +  public int getRemotePort()
 +  {
 +    return this._socket.getPort();
 +  }
 +
 +  /**
 +   * Returns whether the proxy is connected to a remote client
 +   *
 +   * @return whether the proxy is connected to a remote client
 +   */
 +  public boolean isConnected() {
 +    return this.connected;
 +  }
 +
 +  /**
 +   * Mark the receiver as needing removal
 +   * @return true if it was already marked for removal
 +   */
 +  protected boolean startRemoval() {
 +    boolean result;
 +    synchronized (this.isMarkedForRemovalLock) {
 +      result = this.isMarkedForRemoval;
 +      this.isMarkedForRemoval = true;
 +    }
 +    return result;
 +  }
 +  
 +  /**
 +   * Wait until the receiver's removal has completed before
 +   * returning.
 +   * @return true if the proxy was initially marked for removal
 +   */
 +  protected boolean waitRemoval() {
 +    boolean result;
 +    synchronized (this.isMarkedForRemovalLock) {
 +      result = this.isMarkedForRemoval;
 +      boolean interrupted = false;
 +      try {
 +        while (this.isMarkedForRemoval) {
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("Waiting for CacheClientProxy removal: {}", this);
 +          }
 +          try {
 +            this.isMarkedForRemovalLock.wait();
 +          }
 +          catch (InterruptedException e) {
 +            interrupted = true;
 +            this._cache.getCancelCriterion().checkCancelInProgress(e);
 +          }
 +        } // while
 +      }
 +      finally {
 +        if (interrupted) {
 +          Thread.currentThread().interrupt();
 +        }
 +      }
 +    } // synchronized
 +    return result;
 +  }
 +  
 +  /**
 +   * Indicate that removal has completed on this instance
 +   */
 +  protected void notifyRemoval() {
 +    synchronized (this.isMarkedForRemovalLock) {
 +      this.isMarkedForRemoval = false;
 +      this.isMarkedForRemovalLock.notifyAll();
 +    }
 +  }
 +  
 +  /**
 +   * Returns the GemFire cache
 +   *
 +   * @return the GemFire cache
 +   */
 +  public GemFireCacheImpl getCache()
 +  {
 +    return this._cache;
 +  }
 +
 +  public Set<String> getInterestRegisteredRegions() {
 +    HashSet<String> regions = new HashSet<String>();
 +    for(int i=0; i < this.cils.length; i++){
 +      if (!this.cils[i].regions.isEmpty()) {
 +        regions.addAll(this.cils[i].regions);
 +      }
 +    }
 +    return regions;
 +  }
 +  
 +  /**
 +   * Returns the proxy's statistics
 +   *
 +   * @return the proxy's statistics
 +   */
 +  public CacheClientProxyStats getStatistics()
 +  {
 +    return this._statistics;
 +  }
 +
 +  /**
 +   * Returns this proxy's <code>CacheClientNotifier</code>.
 +   * @return this proxy's <code>CacheClientNotifier</code>
 +   */
 +  protected CacheClientNotifier getCacheClientNotifier() {
 +    return this._cacheClientNotifier;
 +  }
 +
 +  /**
 +   * Returns the size of the queue for heuristic purposes.  This
 +   * size may be changing concurrently if puts/gets are occurring
 +   * at the same time.
 +   */
 +  public int getQueueSize() {
 +    return this._messageDispatcher == null ? 0
 +        : this._messageDispatcher.getQueueSize();
 +  }
 +  
 +  /** 
 +   * returns the queue size calculated through stats
 +   */
 +  public int getQueueSizeStat() {
 +    return this._messageDispatcher == null ? 0
 +        : this._messageDispatcher.getQueueSizeStat();  
 +  }
 +  
 +      
 +  public boolean drainInProgress() {
 +    synchronized(drainsInProgressLock) {
 +      return numDrainsInProgress > 0;
 +    }
 +  }
 +  
 +  //Called from CacheClientNotifier when attempting to restart paused proxy
 +  //locking the drain lock requires that no drains are in progress 
 +  //when the lock was acquired.
 +  public boolean lockDrain() {
 +    synchronized(drainsInProgressLock) {
 +      if (!drainInProgress()) {
 +        synchronized(drainLock) {
 +          if (testHook != null) {
 +            testHook.doTestHook("PRE_ACQUIRE_DRAIN_LOCK_UNDER_SYNC");
 +          }
 +          //prevent multiple lockings of drain lock
 +          if (!drainLocked) {
 +            drainLocked = true;
 +            return true;
 +          }
 +        }
 +      }
 +    }
 +    return false;
 +  }
 +  
 +  //Called from CacheClientNotifier when completed restart of proxy
 +  public void unlockDrain() {
 +    if (testHook != null) {
 +      testHook.doTestHook("PRE_RELEASE_DRAIN_LOCK");
 +    }
 +    synchronized(drainLock) {
 +      drainLocked = false;
 +    }
 +  }
 +  
 +  //Only close the client cq if it is paused and no one is attempting to restart the proxy
 +  public boolean closeClientCq(String clientCQName) throws CqException {
 +    if (testHook != null) {
 +      testHook.doTestHook("PRE_DRAIN_IN_PROGRESS");
 +    }
 +    synchronized(drainsInProgressLock) {
 +      numDrainsInProgress ++;
 +    }
 +    if (testHook != null) {
 +      testHook.doTestHook("DRAIN_IN_PROGRESS_BEFORE_DRAIN_LOCK_CHECK");
 +    }
 +    try {
 +      //If the drain lock was acquired, the other thread did so before we could bump up
 +      //the numDrainsInProgress.  That means we need to stop.
 +      if (drainLocked) {
 +        // someone is trying to restart a paused proxy
 +        String msg = LocalizedStrings.CacheClientProxy_COULD_NOT_DRAIN_CQ_DUE_TO_RESTARTING_DURABLE_CLIENT.toLocalizedString(clientCQName, proxyID.getDurableId());
 +        logger.info(msg);
 +        throw new CqException(msg);
 +      }
 +      //isConnected is to protect against the case where a durable client has reconnected
 +      //but has not yet sent a ready for events message
 +      //we can probably remove the isPaused check
 +      if (isPaused() && !isConnected()) {
 +        CqService cqService = getCache().getCqService();
 +        if (cqService != null) {
 +          InternalCqQuery  cqToClose = cqService.getCq(cqService.constructServerCqName(
 +              clientCQName, this.proxyID));
 +          // close and drain
 +          if (cqToClose != null) {
 +            cqService.closeCq(clientCQName, this.proxyID);
 +            this._messageDispatcher.drainClientCqEvents(this.proxyID, cqToClose);
 +          }
 +          else {
 +            String msg = LocalizedStrings.CqService_CQ_NOT_FOUND_FAILED_TO_CLOSE_THE_SPECIFIED_CQ_0.toLocalizedString(clientCQName);
 +            logger.info(msg);
 +            throw new CqException(msg);
 +          }
 +        }
 +      } else {
 +        String msg = LocalizedStrings.CacheClientProxy_COULD_NOT_DRAIN_CQ_DUE_TO_ACTIVE_DURABLE_CLIENT.toLocalizedString(clientCQName, proxyID.getDurableId());
 +        logger.info(msg);
 +        throw new CqException(msg);
 +      }
 +    } finally {
 +      synchronized (drainsInProgressLock) {
 +        numDrainsInProgress--;
 +      }
 +      if (testHook != null) {
 +        testHook.doTestHook("DRAIN_COMPLETE");
 +      }
 +
 +    }
 +    return true;
 +  }
 +  
 +  
 +  /**
 +   * Returns whether the proxy is alive. It is alive if its message dispatcher
 +   * is processing messages.
 +   *
 +   * @return whether the proxy is alive
 +   */
 +  protected boolean isAlive()
 +  {
 +    if (this._messageDispatcher == null) {
 +      return false;
 +    }
 +    return !this._messageDispatcher.isStopped();
 +  }
 +
 +  /**
 +   * Returns whether the proxy is paused. It is paused if its message dispatcher
 +   * is paused. This only applies to durable clients.
 +   *
 +   * @return whether the proxy is paused
 +   *
 +   * @since 5.5
 +   */
 +  protected boolean isPaused() {
 +    return this._isPaused;
 +  }
 +
 +  protected void setPaused(boolean isPaused) {
 +    this._isPaused = isPaused;
 +  }
 +
 +  /**
 +   * Closes the proxy. This method checks the message queue for any unprocessed
 +   * messages and processes them for MAXIMUM_SHUTDOWN_PEEKS.
 +   *
 +   * @see CacheClientProxy#MAXIMUM_SHUTDOWN_PEEKS
 +   */
 +  protected void close()
 +  {
 +    close(true, false);
 +  }
 +
 +  /**
 +   * Set to true once this proxy starts being closed.
 +   * Remains true for the rest of its existence.
 +   */
 +  private final AtomicBoolean closing = new AtomicBoolean(false);
 +
 +  /**
 +   * Close the <code>CacheClientProxy</code>.
 +   *
 +   * @param checkQueue
 +   *          Whether to message check the queue and process any contained
 +   *          messages (up to MAXIMUM_SHUTDOWN_PEEKS).
 +   * @param stoppedNormally
 +   *          Whether client stopped normally
 +   *
 +   * @return whether to keep this <code>CacheClientProxy</code>
 +   * @see CacheClientProxy#MAXIMUM_SHUTDOWN_PEEKS
 +   */
 +  protected boolean close(boolean checkQueue, boolean stoppedNormally) {
 +    boolean pauseDurable = false;
 +    // If the client is durable and either (a) it hasn't stopped normally or (b) it
 +    // has stopped normally but it is configured to be kept alive, set pauseDurable
 +    // to true
 +    if (isDurable()
 +        && (!stoppedNormally || (getDurableKeepAlive() && stoppedNormally))) {
 +      pauseDurable = true;
 +    }
 +
 +    boolean keepProxy = false;
 +    if (pauseDurable) {
 +      pauseDispatching();
 +      keepProxy = true;
 +    } else {
 +      terminateDispatching(checkQueue);
 +      closeTransientFields();
 +    }
 +
 +    this.connected = false;
 +
 +    // Close the Authorization callback (if any)
 +    try {
 +      if (!pauseDurable) {
 +        if (this.postAuthzCallback != null) {//for single user
 +          this.postAuthzCallback.close();
 +          this.postAuthzCallback = null;
 +        }else if(this.clientUserAuths != null) {//for multiple users
 +          this.clientUserAuths.cleanup(true);
 +          this.clientUserAuths = null;
 +        }
 +      }
 +    }
 +    catch (Exception ex) {
 +      if (this._cache.getSecurityLoggerI18n().warningEnabled()) {
 +        this._cache.getSecurityLoggerI18n().warning(LocalizedStrings.TWO_ARG_COLON, new Object[] {this, ex});
 +      }
 +    }
 +    // Notify the caller whether to keep this proxy. If the proxy is durable
 +    // and should be paused, then return true; otherwise return false.
 +    return keepProxy;
 +  }
 +
 +  protected void pauseDispatching() {
 +    if (this._messageDispatcher == null){
 +      return;
 +    }
 +
 +    // If this is the primary, pause the dispatcher (which closes its transient
 +    // fields. Otherwise, just close the transient fields.
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("{}: Pausing processing", this);
 +    }
 +    //BUGFIX for BUG#38234
 +    if(!testAndSetPaused(true) && this.isPrimary) {
 +      if (this._messageDispatcher != Thread.currentThread()) {
 +        // don't interrupt ourself to fix bug 40611
 +        this._messageDispatcher.interrupt();
 +      }
 +    }
 +
 +    try {
 +      // Close transient fields
 +      closeTransientFields();
 +    } finally {
 +      // make sure this gets called if closeTransientFields throws; see bug 40611
 +      // Start timer
 +      scheduleDurableExpirationTask();
 +    }
 +  }
 +
 +  private boolean testAndSetPaused(boolean newValue) {
 +    
 +    synchronized(this._messageDispatcher._pausedLock) {
 +      if (this._isPaused != newValue) {
 +        this._isPaused = newValue;
 +        this._messageDispatcher._pausedLock.notifyAll();
 +        return !this._isPaused;
 +      }
 +      else {
 +        this._messageDispatcher._pausedLock.notifyAll();
 +        return this._isPaused;
 +      }
 +    }
 +  }
 +  protected void terminateDispatching(boolean checkQueue) {
 +    if (this._messageDispatcher == null){
 +      return;
 +    }
 +    
 +    try {
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("{}: Terminating processing", this);
 +    }
 +    if (this._messageDispatcher == Thread.currentThread()) {
 +      // I'm not even sure this is possible but if the dispatcher
 +      // calls us then at least call stopDispatching
 +      // the old code did this (I'm not even sure it is safe to do).
 +      // This needs to be done without testing OR setting "closing".
 +      this._messageDispatcher.stopDispatching(checkQueue);
 +      this.cils[RegisterInterestTracker.interestListIndex].clearClientInterestList();
 +      this.cils[RegisterInterestTracker.durableInterestListIndex].clearClientInterestList();
 +      // VJR: bug 37487 fix
 +      destroyRQ();
 +      return;
 +    }
 +
 +    if (!this.closing.compareAndSet(false, true)) {
 +      // must already be closing so just return
 +      // this is part of the fix for 37684
 +      return;
 +    }
 +    // Unregister interest in all interests (if necessary) 
 +    this.cils[RegisterInterestTracker.interestListIndex].clearClientInterestList();
 +    this.cils[RegisterInterestTracker.durableInterestListIndex].clearClientInterestList();
 +
 +    // If the message dispatcher is paused, unpause it. The next bit of
 +    // code will interrupt the waiter.
 +    if (this.testAndSetPaused(false)) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}: Paused but terminating processing", this);
 +      }
 +      // Cancel the expiration task
 +      cancelDurableExpirationTask(false);
 +    }
 +
 +    boolean alreadyDestroyed = false;
 +    boolean gotInterrupt = Thread.interrupted(); // clears the flag
 +    try {
 +      // Stop the message dispatcher
 +      this._messageDispatcher.stopDispatching(checkQueue);
 +
 +      gotInterrupt |= Thread.interrupted(); // clears the flag
 +
 +      // to fix bug 37684
 +      // 1. check to see if dispatcher is still alive
 +      if (this._messageDispatcher.isAlive()) {
 +        closeSocket();
 +        destroyRQ();
 +        alreadyDestroyed = true;
 +        this._messageDispatcher.interrupt();
 +        if (this._messageDispatcher.isAlive()) {
 +          try {
 +            this._messageDispatcher.join(1000);
 +          } catch (InterruptedException ex) {
 +            gotInterrupt = true;
 +          }
 +          // if it is still alive then warn and move on
 +          if (this._messageDispatcher.isAlive()) {
 +            //com.gemstone.gemfire.internal.OSProcess.printStacks(com.gemstone.gemfire.internal.OSProcess.getId());
 +            logger.warn(LocalizedMessage.create(LocalizedStrings.CacheClientProxy_0_COULD_NOT_STOP_MESSAGE_DISPATCHER_THREAD, this));
 +          }
 +        }
 +      }
 +    }
 +    finally {
 +      if (gotInterrupt) {
 +        Thread.currentThread().interrupt();
 +      }
 +      if (!alreadyDestroyed) {
 +        destroyRQ();
 +      }
 +    }
 +    } finally {
 +      //  Close the statistics
 +      this._statistics.close(); // fix for bug 40105
 +      closeTransientFields(); // make sure this happens
 +    }
 +  }
 +
 +  private void closeSocket() {
 +    if (this._socketClosed.compareAndSet(false, true)) {
 +      // Close the socket
 +      this._cacheClientNotifier.getSocketCloser().asyncClose(this._socket, this._remoteHostAddress, null);
 +      getCacheClientNotifier().getAcceptorStats().decCurrentQueueConnections();
 +    }
 +  }
 +  
 +  private void closeTransientFields() {
 +    closeSocket();
 +
 +    // Null out comm buffer, host address, ports and proxy id. All will be
 +    // replaced when the client reconnects.
 +    releaseCommBuffer();
 +    {
 +      String remoteHostAddress = this._remoteHostAddress;
 +      if (remoteHostAddress != null) {
 +        this._cacheClientNotifier.getSocketCloser().releaseResourcesForAddress(remoteHostAddress);
 +        this._remoteHostAddress = null;
 +      }
 +    }
 +    try {
 +      this.cils[RegisterInterestTracker.interestListIndex].clearClientInterestList();
 +    } catch (CacheClosedException e) {
 +      // ignore if cache is shutting down
 +    }
 +    // Commented to fix bug 40259
 +    //this.clientVersion = null;
 +    closeNonDurableCqs();    
 +  }
 +  
 +  private void releaseCommBuffer() {
 +    ByteBuffer bb = this._commBuffer;
 +    if (bb != null) {
 +      this._commBuffer = null;
 +      ServerConnection.releaseCommBuffer(bb);
 +    }
 +  }
 +
 +  private void closeNonDurableCqs(){
 +    CqService cqService = getCache().getCqService();
 +    if (cqService != null) {
 +      try {
 +        cqService.closeNonDurableClientCqs(getProxyID());
 +      }
 +      catch (CqException ex) {
 +        logger.warn(LocalizedMessage.create(LocalizedStrings.CacheClientProxy_CQEXCEPTION_WHILE_CLOSING_NON_DURABLE_CQS_0, ex.getLocalizedMessage()));
 +      }
 +    }
 +  }
 +  
 +  private void destroyRQ() {
 +    if (this._messageDispatcher == null) {
 +      return;
 +    }
 +    try {
 +      // Using Destroy Region bcoz this method is modified in HARegion so as
 +      // not to distribute.
 +      // For normal Regions , even the localDestroyRegion actually propagates
 +      HARegionQueue rq = this._messageDispatcher._messageQueue;
 +      rq.destroy();
 +
 +      // if (!rq.getRegion().isDestroyed()) {
 +      // rq.getRegion().destroyRegion();
 +      // }
 +    }
 +    catch (RegionDestroyedException rde) {
 +//      throw rde;
 +    }
 +    catch (CancelException e) {
 +//      throw e;
 +    }
 +    catch (Exception warning) {
 +      logger.warn(LocalizedMessage.create(LocalizedStrings.CacheClientProxy_0_EXCEPTION_IN_CLOSING_THE_UNDERLYING_HAREGION_OF_THE_HAREGIONQUEUE, this), warning);
 +    }
 +  }
 +
 +  public void registerInterestRegex(String regionName, String regex,
 +      boolean isDurable) {
 +    registerInterestRegex(regionName, regex, isDurable, true);
 +  }
 +  
 +  public void registerInterestRegex(String regionName, String regex,
 +      boolean isDurable, boolean receiveValues) {
 +    if (this.isPrimary) {
 +      // Notify all secondaries and client of change in interest
 +      notifySecondariesAndClient(regionName, regex, InterestResultPolicy.NONE,
 +          isDurable, receiveValues, InterestType.REGULAR_EXPRESSION);
 +    } else {
 +      throw new IllegalStateException(LocalizedStrings.CacheClientProxy_NOT_PRIMARY.toLocalizedString());
 +    }
 +  }
 +
 +  public void registerInterest(String regionName, Object keyOfInterest,
 +      InterestResultPolicy policy, boolean isDurable) {
 +    registerInterest(regionName, keyOfInterest, policy, isDurable, true);
 +  }
 +  
 +  public void registerInterest(String regionName, Object keyOfInterest,
 +      InterestResultPolicy policy, boolean isDurable,
 +      boolean receiveValues) {
 +    if (keyOfInterest instanceof String && keyOfInterest.equals("ALL_KEYS")) {
 +      registerInterestRegex(regionName, ".*", isDurable, receiveValues);
 +    } else if (keyOfInterest instanceof List) {
 +      if (this.isPrimary) {
 +        notifySecondariesAndClient(regionName, keyOfInterest, policy,
 +            isDurable, receiveValues, InterestType.KEY);
 +      } else {
 +        throw new IllegalStateException(LocalizedStrings.CacheClientProxy_NOT_PRIMARY.toLocalizedString());
 +      }
 +    } else {
 +      if (this.isPrimary) {
 +        // Notify all secondaries and client of change in interest
 +        notifySecondariesAndClient(regionName, keyOfInterest, policy,
 +            isDurable, receiveValues, InterestType.KEY);
 +        
 +        // Enqueue the initial value message for the client if necessary
 +        if (policy == InterestResultPolicy.KEYS_VALUES) {
 +          Get70 request = (Get70)Get70.getCommand();
 +          LocalRegion lr = (LocalRegion) this._cache.getRegion(regionName);
 +          Get70.Entry entry = request.getValueAndIsObject(lr, keyOfInterest, null,
 +              null);
 +          boolean isObject = entry.isObject;
 +          byte[] value = null;
 +          if (entry.value instanceof byte[]) {
 +            value = (byte[])entry.value;
 +          } else {
 +            try {
 +              value = CacheServerHelper.serialize(entry.value);
 +            } catch (IOException e) {
 +              logger.warn(LocalizedMessage.create(LocalizedStrings.CacheClientProxy_THE_FOLLOWING_EXCEPTION_OCCURRED_0, entry.value), e);
 +            }
 +          }
 +          VersionTag tag = entry.versionTag;
 +          ClientUpdateMessage updateMessage = new ClientUpdateMessageImpl(
 +              EnumListenerEvent.AFTER_CREATE, lr, keyOfInterest, value, null,
 +              (isObject ? (byte) 0x01 : (byte) 0x00), null, this.proxyID,
 +              new EventID(this._cache.getDistributedSystem()), tag);
 +          CacheClientNotifier.routeSingleClientMessage(updateMessage, this.proxyID);
 +        }
 +        // Add the client to the region's filters
 +        //addFilterRegisteredClients(regionName, keyOfInterest);
 +      } else {
 +        throw new IllegalStateException(LocalizedStrings.CacheClientProxy_NOT_PRIMARY.toLocalizedString());
 +      }
 +    }
 +  }
 +
 +  private void notifySecondariesAndClient(String regionName,
 +      Object keyOfInterest, InterestResultPolicy policy, boolean isDurable,
 +      boolean receiveValues, int interestType) {
 +    // Create a client interest message for the keyOfInterest
 +    ClientInterestMessageImpl message = new ClientInterestMessageImpl(
 +        new EventID(this._cache.getDistributedSystem()), regionName,
 +        keyOfInterest, interestType, policy.getOrdinal(), isDurable,
 +        !receiveValues, ClientInterestMessageImpl.REGISTER);
 +
 +    // Notify all secondary proxies of a change in interest
 +    notifySecondariesOfInterestChange(message);
 +
 +    // Modify interest registration
 +    if (keyOfInterest instanceof List) {
 +      registerClientInterestList(regionName, (List) keyOfInterest, isDurable,
 +          !receiveValues, true);
 +    } else {
 +      registerClientInterest(regionName, keyOfInterest, interestType,
 +          isDurable, !receiveValues, true);
 +    }
 +
 +    // Enqueue the interest registration message for the client.
 +    // If the client is not 7.0.1 or greater and the key of interest is a list,
 +    // then create an individual message for each entry in the list since the
 +    // client doesn't support a ClientInterestMessageImpl containing a list.
 +    if (Version.GFE_701.compareTo(this.clientVersion) > 0
 +        && keyOfInterest instanceof List) {
 +      for (Iterator i = ((List) keyOfInterest).iterator(); i.hasNext();) {
 +        this._messageDispatcher.enqueueMessage(new ClientInterestMessageImpl(
 +            new EventID(this._cache.getDistributedSystem()), regionName,
 +            i.next(), interestType, policy.getOrdinal(), isDurable, !receiveValues,
 +            ClientInterestMessageImpl.REGISTER));
 +     }
 +    } else {
 +      this._messageDispatcher.enqueueMessage(message);
 +    }
 +  }
 +
 +  public void unregisterInterestRegex(String regionName, String regex,
 +      boolean isDurable) {
 +    unregisterInterestRegex(regionName, regex, isDurable, true);
 +  }
 +  
 +  public void unregisterInterestRegex(String regionName, String regex,
 +      boolean isDurable, boolean receiveValues) {
 +    if (this.isPrimary) {
 +      notifySecondariesAndClient(regionName, regex, isDurable, receiveValues,
 +          InterestType.REGULAR_EXPRESSION);
 +    } else {
 +      throw new IllegalStateException(LocalizedStrings.CacheClientProxy_NOT_PRIMARY.toLocalizedString());
 +    }
 +  }
 +  
 +  public void unregisterInterest(String regionName, Object keyOfInterest,
 +      boolean isDurable) {
 +    unregisterInterest(regionName, keyOfInterest, isDurable, true);
 +  }
 +  
 +  public void unregisterInterest(String regionName, Object keyOfInterest,
 +      boolean isDurable, boolean receiveValues) {
 +    if (keyOfInterest instanceof String && keyOfInterest.equals("ALL_KEYS")) {
 +      unregisterInterestRegex(regionName, ".*", isDurable, receiveValues);
 +    } else {
 +      if (this.isPrimary) {
 +        notifySecondariesAndClient(regionName, keyOfInterest, isDurable,
 +            receiveValues, InterestType.KEY);
 +      } else {
 +        throw new IllegalStateException(LocalizedStrings.CacheClientProxy_NOT_PRIMARY.toLocalizedString());
 +      }
 +    }
 +  }
 +
 +  private void notifySecondariesAndClient(String regionName,
 +      Object keyOfInterest, boolean isDurable, boolean receiveValues,
 +      int interestType) {
 +    // Notify all secondary proxies of a change in interest
 +    ClientInterestMessageImpl message = new ClientInterestMessageImpl(
 +        new EventID(this._cache.getDistributedSystem()), regionName,
 +        keyOfInterest, interestType, (byte) 0, isDurable, !receiveValues,
 +        ClientInterestMessageImpl.UNREGISTER);
 +    notifySecondariesOfInterestChange(message);
 + 
 +    // Modify interest registration
 +    if (keyOfInterest instanceof List) {
 +      unregisterClientInterest(regionName, (List) keyOfInterest, false);
 +    } else {
 +      unregisterClientInterest(regionName, keyOfInterest, interestType,
 +          false);
 +    }
 + 
 +    // Enqueue the interest unregistration message for the client.
 +    // If the client is not 7.0.1 or greater and the key of interest is a list,
 +    // then create an individual message for each entry in the list since the
 +    // client doesn't support a ClientInterestMessageImpl containing a list.
 +    if (Version.GFE_701.compareTo(this.clientVersion) > 0
 +        && keyOfInterest instanceof List) {
 +      for (Iterator i = ((List) keyOfInterest).iterator(); i.hasNext();) {
 +        this._messageDispatcher.enqueueMessage(new ClientInterestMessageImpl(
 +            new EventID(this._cache.getDistributedSystem()), regionName,
 +            i.next(), interestType, (byte) 0, isDurable, !receiveValues,
 +            ClientInterestMessageImpl.UNREGISTER));
 +      }
 +    } else {
 +      this._messageDispatcher.enqueueMessage(message);
 +    }
 +  }
 +  
 +  protected void notifySecondariesOfInterestChange(ClientInterestMessageImpl message) {
 +    if (logger.isDebugEnabled()) {
 +      StringBuffer subBuffer = new StringBuffer();
 +      if (message.isRegister()) {
 +        subBuffer
 +          .append("register ")
 +          .append(message.getIsDurable() ? "" : "non-")
 +          .append("durable interest in ");
 +      } else {
 +        subBuffer.append("unregister interest in ");
 +      }
 +      StringBuffer buffer = new StringBuffer();
 +      buffer
 +        .append(this)
 +        .append(": Notifying secondary proxies to ")
 +        .append(subBuffer.toString())
 +        .append(message.getRegionName())
 +        .append("->")
 +        .append(message.getKeyOfInterest())
 +        .append("->")
 +        .append(InterestType.getString(message.getInterestType()));
 +      logger.debug(buffer.toString());
 +    }
 +    this._cacheClientNotifier.deliverInterestChange(this.proxyID, message);
 +  }
 +
 +  /*
 +  protected void addFilterRegisteredClients(String regionName,
 +      Object keyOfInterest) {
 +    try {
 +      this._cacheClientNotifier.addFilterRegisteredClients(regionName,
 +          this.proxyID);
 +    } catch (RegionDestroyedException e) {
 +      logger.warn(LocalizedStrings.CacheClientProxy_0_INTEREST_REG_FOR_0_FAILED, regionName + "->" + keyOfInterest, e);
 +    }
 +  }
 +  */
 +  
 +  /**
 +   * Registers interest in the input region name and key
 +   *
 +   * @param regionName
 +   *          The fully-qualified name of the region in which to register
 +   *          interest
 +   * @param keyOfInterest
 +   *          The key in which to register interest
 +   */
 +  protected void registerClientInterest(String regionName,
 +      Object keyOfInterest, int interestType, boolean isDurable,
 +      boolean sendUpdatesAsInvalidates, boolean flushState)
 +  {
 +    ClientInterestList cil =
 +      this.cils[RegisterInterestTracker.getInterestLookupIndex(
 +          isDurable, false)];
 +    cil.registerClientInterest(regionName, keyOfInterest, interestType, sendUpdatesAsInvalidates);
 +    if (flushState) {
 +      flushForInterestRegistration(regionName, this._cache.getDistributedSystem().getDistributedMember());
 +    }
 +    HARegionQueue queue = getHARegionQueue();
 +    if (queue != null) { // queue is null during initialization
 +      queue.setHasRegisteredInterest(true);
 +    }
 +  }
 +  
 +  /**
 +   * flush other regions to the given target.  This is usually the member
 +   * that is registering the interest.  During queue creation it is the
 +   * queue's image provider.
 +   */
 +  public void flushForInterestRegistration(String regionName, DistributedMember target) {
 +    Region r = this._cache.getRegion(regionName);
 +    if (r == null) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("Unable to find region '{}' to flush for interest registration", regionName);
 +      }
 +    } else if (r.getAttributes().getScope().isDistributed()) {
 +      if (logger.isDebugEnabled()){
 +        logger.debug("Flushing region '{}' for interest registration", regionName);
 +      }
 +      CacheDistributionAdvisee cd = (CacheDistributionAdvisee)r;
 +      final StateFlushOperation sfo;
 +      if (r instanceof PartitionedRegion) {
 +        // need to flush all buckets.  SFO should be changed to target buckets
 +        // belonging to a particular PR, but it doesn't have that option right now
 +        sfo = new StateFlushOperation(
 +            this._cache.getDistributedSystem().getDistributionManager());
 +      } else {
 +        sfo = new StateFlushOperation((DistributedRegion)r);
 +      }
 +      try {
 +        // bug 41681 - we need to flush any member that may have a cache operation
 +        // in progress so that the changes are received there before returning
 +        // from this method
 +        InitialImageAdvice advice = cd.getCacheDistributionAdvisor().adviseInitialImage(null);
 +        HashSet recips = new HashSet(advice.getReplicates());
 +        recips.addAll(advice.getUninitialized());
 +        recips.addAll(advice.getEmpties());
 +        recips.addAll(advice.getPreloaded());
 +        recips.addAll(advice.getOthers());
 +        sfo.flush(recips,
 +            target,
 +            DistributionManager.HIGH_PRIORITY_EXECUTOR, true);
 +      } catch (InterruptedException ie) {
 +        Thread.currentThread().interrupt();
 +        return;
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Unregisters interest in the input region name and key
 +   *
 +   * @param regionName
 +   *          The fully-qualified name of the region in which to unregister
 +   *          interest
 +   * @param keyOfInterest
 +   *          The key in which to unregister interest
 +   * @param isClosing
 +   *          Whether the caller is closing
 +   */
 +  protected void unregisterClientInterest(String regionName,
 +      Object keyOfInterest, int interestType, boolean isClosing)
 +  {
 +    // only unregister durable interest if isClosing and !keepalive
 +    if (!isClosing /* explicit unregister */
 +        || !getDurableKeepAlive() /* close and no keepAlive*/) {
 +      this.cils[RegisterInterestTracker.durableInterestListIndex].
 +        unregisterClientInterest(regionName, keyOfInterest, interestType);
 +    }
 +    // always unregister non durable interest
 +    this.cils[RegisterInterestTracker.interestListIndex].
 +      unregisterClientInterest(regionName, keyOfInterest, interestType);
 +  }
 +
 +  /**
 +   * Registers interest in the input region name and list of keys
 +   *
 +   * @param regionName
 +   *          The fully-qualified name of the region in which to register
 +   *          interest
 +   * @param keysOfInterest
 +   *          The list of keys in which to register interest
 +   */
 +  protected void registerClientInterestList(String regionName,
 +      List keysOfInterest, boolean isDurable, boolean sendUpdatesAsInvalidates,
 +      boolean flushState)
 +  {
 +    // we only use two interest lists to map the non-durable and durable
 +    // identifiers to their interest settings
 +    ClientInterestList cil =
 +      this.cils[RegisterInterestTracker.getInterestLookupIndex(
 +          isDurable, false/*sendUpdatesAsInvalidates*/)];
 +    cil.registerClientInterestList(regionName, keysOfInterest, sendUpdatesAsInvalidates);
 +    if (getHARegionQueue() != null) {
 +      if (flushState) {
 +        flushForInterestRegistration(regionName, this._cache.getDistributedSystem().getDistributedMember());
 +      }
 +      getHARegionQueue().setHasRegisteredInterest(true);
 +    }
 +  }
 +
 +  /**
 +   * Unregisters interest in the input region name and list of keys
 +   *
 +   * @param regionName
 +   *          The fully-qualified name of the region in which to unregister
 +   *          interest
 +   * @param keysOfInterest
 +   *          The list of keys in which to unregister interest
 +   * @param isClosing
 +   *          Whether the caller is closing
 +   */
 +  protected void unregisterClientInterest(String regionName,
 +      List keysOfInterest, boolean isClosing)
 +  {
 +    // only unregister durable interest if isClosing and !keepalive
 +    if (!isClosing /* explicit unregister */
 +        || !getDurableKeepAlive() /* close and no keepAlive*/) {
 +      this.cils[RegisterInterestTracker.durableInterestListIndex].
 +        unregisterClientInterestList(regionName, keysOfInterest);
 +    }
 +    // always unregister non durable interest
 +    this.cils[RegisterInterestTracker.interestListIndex].
 +      unregisterClientInterestList(regionName, keysOfInterest);
 +  }
 +
 +
 +  /** sent by the cache client notifier when there is an interest registration change */
 +  protected void processInterestMessage(ClientInterestMessageImpl message) { 
 +    int interestType = message.getInterestType(); 
 +    String regionName = message.getRegionName(); 
 +    Object key = message.getKeyOfInterest(); 
 +    if (message.isRegister()) {
 +      // Register interest in this region->key
 +      if (key instanceof List) {
 +        registerClientInterestList(regionName, (List) key,
 +            message.getIsDurable(), message.getForUpdatesAsInvalidates(), true);
 +      } else {
 +        registerClientInterest(regionName, key, interestType,
 +            message.getIsDurable(), message.getForUpdatesAsInvalidates(), true);
 +      }
 +       
 +      // Add the client to the region's filters 
 +      //addFilterRegisteredClients(regionName, key); 
 + 
 +      if (logger.isDebugEnabled()) { 
 +        StringBuffer buffer = new StringBuffer();
 +        buffer 
 +          .append(this) 
 +          .append(": Interest listener registered ") 
 +          .append(message.getIsDurable() ? "" : "non-") 
 +          .append("durable interest in ") 
 +          .append(message.getRegionName()) 
 +          .append("->") 
 +          .append(message.getKeyOfInterest())
 +          .append("->")
 +          .append(InterestType.getString(message.getInterestType()));
 +        logger.debug(buffer.toString()); 
 +      } 
 +    } else { 
 +      // Unregister interest in this region->key 
 +      if (key instanceof List) {
 +        unregisterClientInterest(regionName, (List) key, false);
 +      } else {
 +        unregisterClientInterest(regionName, key, interestType, false);
 +      }
 +       
 +      if (logger.isDebugEnabled()) { 
 +        StringBuffer buffer = new StringBuffer(); 
 +        buffer 
 +          .append(this) 
 +          .append(": Interest listener unregistered interest in ") 
 +          .append(message.getRegionName()) 
 +          .append("->") 
 +          .append(message.getKeyOfInterest())
 +          .append("->")
 +          .append(InterestType.getString(message.getInterestType()));
 +        logger.debug(buffer.toString()); 
 +      } 
 +    } 
 +  } 
 +
 +  private boolean postDeliverAuthCheckPassed(ClientUpdateMessage clientMessage) {
 +    // Before adding it in the queue for dispatching, check for post
 +    // process authorization
 +    if (AcceptorImpl.isAuthenticationRequired()
 +        && this.postAuthzCallback == null
 +        && AcceptorImpl.isPostAuthzCallbackPresent()) {
 +      // security is on and callback is null: it means multiuser mode.
 +      ClientUpdateMessageImpl cumi = (ClientUpdateMessageImpl)clientMessage;
 +
 +      CqNameToOp clientCq = cumi.getClientCq(this.proxyID);
 +
 +      if (clientCq != null && !clientCq.isEmpty()) {
 +        if (logger.isDebugEnabled()) {
 +          logger.debug("CCP clientCq size before processing auth {}", clientCq.size());
 +        }
 +        String[] regionNameHolder = new String[1];
 +        OperationContext opctxt = getOperationContext(clientMessage,
 +            regionNameHolder);
 +        if (opctxt == null) {
 +          logger.warn(LocalizedMessage.create(
 +              LocalizedStrings.CacheClientProxy__0_NOT_ADDING_MESSAGE_TO_QUEUE_1_BECAUSE_THE_OPERATION_CONTEXT_OBJECT_COULD_NOT_BE_OBTAINED_FOR_THIS_CLIENT_MESSAGE,
 +              new Object[] {this, clientMessage}));
 +          return false;
 +        }
 +
 +        String[] cqNames = clientCq.getNames();
 +        if (logger.isDebugEnabled()) {
 +          logger.debug("CCP clientCq names array size {}", cqNames.length);
 +        }
 +        for (int i = 0; i < cqNames.length; i++) {
 +          try {
 +            if (logger.isDebugEnabled()) {
 +              logger.debug("CCP clientCq name {}", cqNames[i]);
 +            }
 +            boolean isAuthorized = false;
 +
 +            if (this.proxyID.isDurable() && this.getDurableKeepAlive()
 +                && this._isPaused) {
 +              // need to take lock as we may be reinitializing proxy cache
 +              synchronized (this.clientUserAuthsLock) {
 +                AuthorizeRequestPP postAuthCallback = this.clientUserAuths
 +                    .getUserAuthAttributes(cqNames[i]).getPostAuthzRequest();
 +                if (logger.isDebugEnabled() && postAuthCallback == null) {
 +                  logger.debug("CCP clientCq post callback is null");
 +                }
 +                if (postAuthCallback != null && postAuthCallback
 +                    .getPostAuthzCallback().authorizeOperation(
 +                        regionNameHolder[0], opctxt)) {
 +                  isAuthorized = true;
 +                }
 +              }
 +            } else {
 +              UserAuthAttributes userAuthAttributes = this.clientUserAuths
 +                  .getUserAuthAttributes(cqNames[i]);
 +
 +              AuthorizeRequestPP postAuthCallback = userAuthAttributes
 +                  .getPostAuthzRequest();
 +              if (postAuthCallback == null && logger.isDebugEnabled()) {
 +                logger.debug("CCP clientCq post callback is null");
 +              }
 +              if (postAuthCallback != null && postAuthCallback
 +                  .getPostAuthzCallback().authorizeOperation(
 +                      regionNameHolder[0], opctxt)) {
 +                isAuthorized = true;
 +              }
 +            }
 +
 +            if (!isAuthorized) {
 +              logger.warn(LocalizedMessage.create(
 +                  LocalizedStrings.CacheClientProxy__0_NOT_ADDING_CQ_MESSAGE_TO_QUEUE_1_BECAUSE_AUTHORIZATION_FAILED,
 +                  new Object[] {this, clientMessage}));
 +              clientCq.delete(cqNames[i]);
 +            }
 +          } catch (Exception ex) {
 +            // ignore...
 +          }
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("CCP clientCq size after processing auth {}", clientCq.size());
 +          }
 +        }
 +        // again need to check as there may be no CQ available
 +        if (!clientMessage.hasCqs(this.proxyID)) {
 +          this._statistics.incMessagesNotQueuedNotInterested();
 +          if (logger.isTraceEnabled(LogMarker.BRIDGE_SERVER)) {
 +            logger.debug("{}: Not adding message to queue. It is not interested in this region and key: {}", clientMessage);
 +          }
 +          return false;
 +        }
 +      }
 +    }
 +    else if (this.postAuthzCallback != null) {
 +      String[] regionNameHolder = new String[1];
 +      boolean isAuthorize = false;
 +      OperationContext opctxt = getOperationContext(clientMessage,
 +          regionNameHolder);
 +      if (opctxt == null) {
 +        logger.warn(LocalizedMessage.create(LocalizedStrings.CacheClientProxy__0_NOT_ADDING_MESSAGE_TO_QUEUE_1_BECAUSE_THE_OPERATION_CONTEXT_OBJECT_COULD_NOT_BE_OBTAINED_FOR_THIS_CLIENT_MESSAGE, new Object[] {this, clientMessage}));
 +        return false;
 +      }
 +      if (logger.isTraceEnabled()){
 +        logger.trace("{}: Invoking authorizeOperation for message: {}", this, clientMessage);
 +      }
 +
 +      if (this.proxyID.isDurable() && this.getDurableKeepAlive()
 +          && this._isPaused) {
 +        synchronized (this.clientUserAuthsLock) {
 +          isAuthorize = this.postAuthzCallback.authorizeOperation(
 +              regionNameHolder[0], opctxt);
 +        }
 +      } else {
 +        isAuthorize = this.postAuthzCallback.authorizeOperation(
 +            regionNameHolder[0], opctxt);
 +      }
 +      if (!isAuthorize) {
 +        logger.warn(LocalizedMessage.create(LocalizedStrings.CacheClientProxy__0_NOT_ADDING_MESSAGE_TO_QUEUE_1_BECAUSE_AUTHORIZATION_FAILED, new Object[] {this, clientMessage}));
 +        return false;
 +      }
 +    }
 +
 +    return true;
 +  }
 +
 +  /**
 +   * Delivers the message to the client representing this client proxy.
 +   * @param conflatable 
 +   */
 +  protected void deliverMessage(Conflatable conflatable)
 +  {
 +    ClientUpdateMessage clientMessage = null;
 +    if(conflatable instanceof HAEventWrapper) {
 +      clientMessage = ((HAEventWrapper)conflatable).getClientUpdateMessage();
 +    } else {
 +      clientMessage = (ClientUpdateMessage)conflatable;
 +    } 
 +
 +    this._statistics.incMessagesReceived();
 +
 +    if (clientMessage.needsNoAuthorizationCheck() || postDeliverAuthCheckPassed(clientMessage)) {
 +      // If dispatcher is getting initialized, add the event to temporary queue.
 +      if (this.messageDispatcherInit) {
 +        synchronized (this.queuedEventsSync) {
 +          if (this.messageDispatcherInit) {  // Check to see value did not changed while getting the synchronize lock.
 +            if (logger.isDebugEnabled()) {
 +              logger.debug("Message dispatcher for proxy {} is getting initialized. Adding message to the queuedEvents.", this);
 +            }
 +            this.queuedEvents.add(conflatable);
 +            return;
 +          }
 +        }
 +      }
 +      
 +      if (this._messageDispatcher != null) {
 +        this._messageDispatcher.enqueueMessage(conflatable);
 +      } else {
 +        this._statistics.incMessagesFailedQueued();
 +        if (logger.isDebugEnabled()) {
 +          logger.debug("Message is not added to the queue. Message dispatcher for proxy: {} doesn't exist.", this);
 +        }
 +      }
 +    } else {
 +      this._statistics.incMessagesFailedQueued();
 +    }
 +  }
 +
 +  protected void sendMessageDirectly(ClientMessage message) {
 +    // Send the message directly if the connection exists
 +    // (do not go through the queue).
 +    if (logger.isDebugEnabled()){
 +      logger.debug("About to send message directly to {}", this);
 +    }
 +    if (this._messageDispatcher != null && this._socket != null && !this._socket.isClosed()) {
 +      // If the socket is open, send the message to it
 +      this._messageDispatcher.sendMessageDirectly(message);
 +      if (logger.isDebugEnabled()){
 +        logger.debug("Sent message directly to {}", this);
 +      }
 +    } else {
 +      // Otherwise just reset the ping counter
 +      resetPingCounter();
 +      if (logger.isDebugEnabled()){
 +        logger.debug("Skipped sending message directly to {}", this);
 +      }
 +    }
 +  }
 +  
 +  private OperationContext getOperationContext(ClientMessage cmsg,
 +      String[] regionNameHolder) {
 +    ClientUpdateMessageImpl cmsgimpl = (ClientUpdateMessageImpl)cmsg;
 +    OperationContext opctxt = null;
 +    // TODO SW: Special handling for DynamicRegions; this should be reworked
 +    // when DynamicRegion API is deprecated
 +    String regionName = cmsgimpl.getRegionName();
 +    regionNameHolder[0] = regionName;
 +    if (cmsgimpl.isCreate()) {
 +      if (DynamicRegionFactory.regionIsDynamicRegionList(regionName)) {
 +        regionNameHolder[0] = (String)cmsgimpl.getKeyOfInterest();
 +        opctxt = new RegionCreateOperationContext(true);
 +      }
 +      else {
 +        PutOperationContext tmp = new PutOperationContext(cmsgimpl.getKeyOfInterest(), cmsgimpl
 +            .getValue(), cmsgimpl.valueIsObject(), PutOperationContext.CREATE,
 +            true);
 +        tmp.setCallbackArg(cmsgimpl.getCallbackArgument());
 +        opctxt = tmp;
 +      }
 +    }
 +    else if (cmsgimpl.isUpdate()) {
 +      if (DynamicRegionFactory.regionIsDynamicRegionList(regionName)) {
 +        regionNameHolder[0] = (String)cmsgimpl.getKeyOfInterest();
 +        opctxt = new RegionCreateOperationContext(true);
 +      }
 +      else {
 +        PutOperationContext tmp = new PutOperationContext(cmsgimpl.getKeyOfInterest(), cmsgimpl
 +            .getValue(), cmsgimpl.valueIsObject(), PutOperationContext.UPDATE,
 +            true);
 +        tmp.setCallbackArg(cmsgimpl.getCallbackArgument());
 +        opctxt = tmp;
 +      }
 +    }
 +    else if (cmsgimpl.isDestroy()) {
 +      if (DynamicRegionFactory.regionIsDynamicRegionList(regionName)) {
 +        regionNameHolder[0] = (String)cmsgimpl.getKeyOfInterest();
 +        opctxt = new RegionDestroyOperationContext(true);
 +      }
 +      else {
 +        DestroyOperationContext tmp = new DestroyOperationContext(cmsgimpl.getKeyOfInterest(), true);
 +        tmp.setCallbackArg(cmsgimpl.getCallbackArgument());
 +        opctxt = tmp;
 +      }
 +    }
 +    else if (cmsgimpl.isDestroyRegion()) {
 +      opctxt = new RegionDestroyOperationContext(true);
 +    }
 +    else if (cmsgimpl.isInvalidate()) {
 +      InvalidateOperationContext tmp = new InvalidateOperationContext(cmsgimpl.getKeyOfInterest(), true);
 +      tmp.setCallbackArg(cmsgimpl.getCallbackArgument());
 +      opctxt = tmp;
 +    }
 +    else if (cmsgimpl.isClearRegion()) {
 +      RegionClearOperationContext tmp = new RegionClearOperationContext(true);
 +      tmp.setCallbackArg(cmsgimpl.getCallbackArgument());
 +      opctxt = tmp;
 +    }
 +    return opctxt;
 +  }
 +
 +  /**
 +   * Initializes the message dispatcher thread. The
 +   * <code>MessageDispatcher</code> processes the message queue.
 +   *
 +   * @throws CacheException
 +   */
 +  public void initializeMessageDispatcher() throws CacheException
 +  {
 +    this.messageDispatcherInit = true; // Initialization process.
 +    try {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}: Initializing message dispatcher with capacity of {} entries", this, _maximumMessageCount);
 +      }
 +      String name = "Client Message Dispatcher for "
 +        + getProxyID().getDistributedMember() + (isDurable()? " (" + getDurableId()+")" : "");
 +      this._messageDispatcher = new MessageDispatcher(this, name);
 +      
 +      //Fix for 41375 - drain as many of the queued events
 +      //as we can without synchronization.
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{} draining {} events from init queue into intialized queue", this, this.queuedEvents.size());
 +      }
 +      Conflatable nextEvent;
 +      while((nextEvent = queuedEvents.poll()) != null) {
 +        this._messageDispatcher.enqueueMessage(nextEvent);
 +      }
 +      
 +      //Now finish emptying the queue with synchronization to make
 +      //sure we don't miss any events.
 +      synchronized (this.queuedEventsSync){
 +          while((nextEvent = queuedEvents.poll()) != null) {
 +            this._messageDispatcher.enqueueMessage(nextEvent);
 +          }
 +          
 +          this.messageDispatcherInit = false; // Done initialization.
 +      }
 +    } finally {
 +      if (this.messageDispatcherInit) { // If its not successfully completed.
 +        this._statistics.close();
 +      }
 +    }
 +  }
 +
 +  protected void startOrResumeMessageDispatcher(boolean processedMarker) {
 +    // Only start or resume the dispatcher if it is Primary
 +    if (this.isPrimary) {
 +      // Add the marker to the queue
 +      if (!processedMarker) {
 +        EventID eventId = new EventID(this._cache.getDistributedSystem());
 +        this._messageDispatcher.enqueueMarker(new ClientMarkerMessageImpl(eventId));
 +      }
 +
 +      // Set the message queue to primary.
 +      this._messageDispatcher._messageQueue.setPrimary(true);
 +
 +      // Start or resume the dispatcher
 +      synchronized (this._messageDispatcher._pausedLock) {
 +        if (this.isPaused()) {
 +          // It is paused, resume it
 +          this.setPaused(false);
 +          if (this._messageDispatcher.isStopped()) {
 +            if (logger.isDebugEnabled()) {
 +              logger.debug("{}: Starting dispatcher", this);
 +            }
 +            this._messageDispatcher.start();
 +          }
 +          else {
 +            // ARB: Initialize transient fields.
 +            this._messageDispatcher.initializeTransients();
 +            if (logger.isDebugEnabled()) {
 +              logger.debug("{}: Resuming dispatcher", this);
 +            }
 +            this._messageDispatcher.resumeDispatching();
 +          }
 +        } else if (!this._messageDispatcher.isAlive()) {
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("{}: Starting dispatcher", this);
 +          }
 +          this._messageDispatcher.start();
 +        }
 +      }
 +    }
 +  }
 +
 +  /*
 +   * Returns whether the client represented by this <code> CacheClientProxy
 +   * </code> has registered interest in anything. @return whether the client
 +   * represented by this <code> CacheClientProxy </code> has registered interest
 +   * in anything
 +   */
 +  protected boolean hasRegisteredInterested()
 +  {
 +    return
 +    this.cils[RegisterInterestTracker.interestListIndex].hasInterest() ||
 +    this.cils[RegisterInterestTracker.durableInterestListIndex].hasInterest();
 +  }
 +
 +  /**
 +   * Returns a string representation of the proxy
 +   */
 +  @Override
 +  public String toString()
 +  {
 +    StringBuffer buffer = new StringBuffer();
 +    buffer.append("CacheClientProxy[")
 +      // .append("client proxy id=")
 +      .append(this.proxyID)
 +      // .append("; client host name=")
 +      // .append(this._socket.getInetAddress().getCanonicalHostName())
 +      // .append("; client host address=")
 +      // .append(this._remoteHostAddress)
 +      .append("; port=").append(this._socket.getPort())
 +      .append("; primary=").append(isPrimary)
 +      .append("; version=").append(clientVersion)
 +      .append("]");
 +    return buffer.toString();
 +  }
 +  
 +  public String getState(){
 +    StringBuffer buffer = new StringBuffer();
 +    buffer.append("CacheClientProxy[")
 +      // .append("client proxy id=")
 +      .append(this.proxyID)
 +      // .append("; client host name=")
 +      // .append(this._socket.getInetAddress().getCanonicalHostName())
 +      // .append("; client host address=")
 +      // .append(this._remoteHostAddress)
 +      .append("; port=").append(this._socket.getPort())
 +      .append("; primary=").append(isPrimary)
 +      .append("; version=").append(clientVersion)
 +      .append("; paused=").append(isPaused())
 +      .append("; alive=").append(isAlive())
 +      .append("; connected=").append(isConnected())
 +      .append("; isMarkedForRemoval=").append(isMarkedForRemoval)
 +      .append("]");
 +    return buffer.toString();
 +  }
 +
 +  public boolean isPrimary()
 +  {
 +    //boolean primary = this._messageDispatcher.isAlive()
 +    //    || this._messageDispatcher._messageQueue.isPrimary();
 +    boolean primary = this.isPrimary;
 +    //System.out.println(this + ": DISPATCHER IS ALIVE: " + this._messageDispatcher.isAlive());
 +    //System.out.println(this + ": DISPATCHER QUEUE IS PRIMARY: " + this._messageDispatcher._messageQueue.isPrimary());
 +    //System.out.println(this + ": IS PRIMARY: " + primary);
 +    return primary;
 +    // return this.isPrimary ;
 +  }
 +
 +  protected boolean basicIsPrimary() {
 +    return this.isPrimary;
 +  }
 +
 +  protected void setPrimary(boolean isPrimary) {
 +    this.isPrimary = isPrimary;
 +  }
 +
 +  // private static int nextId = 0;
 +  // static protected int getNextId() {
 +  // synchronized (CacheClientProxy.class) {
 +  // return ++nextId;
 +  // }
 +  // }
 +  /*
 +   * Return this client's HA region queue
 +   * @returns - HARegionQueue of the client
 +   */
 +   public HARegionQueue getHARegionQueue() {
 +     if (this._messageDispatcher != null){
 +       return _messageDispatcher._messageQueue;
 +     }
 +     return null;
 +   }
 +
 +
 +  /**
 +   * Reinitialize a durable <code>CacheClientProxy</code> with a new client.
 +   * @param socket
 +   *          The socket between the server and the client
 +   * @param ip
 +   *          whether this proxy represents the primary
 +   */
 +  protected void reinitialize(Socket socket, ClientProxyMembershipID proxyId,
 +      Cache cache, boolean ip, byte cc, Version ver) {
 +    // Re-initialize transient fields
 +    initializeTransientFields(socket, proxyId, ip, cc, ver);
 +    getCacheClientNotifier().getAcceptorStats().incCurrentQueueConnections();
 +
 +
 +    // Cancel expiration task
 +    cancelDurableExpirationTask(true);
 +
 +    // Set the message dispatcher's primary flag. This could go from primary
 +    // to secondary
 +    this._messageDispatcher._messageQueue.setPrimary(ip);
 +    this._messageDispatcher._messageQueue.setClientConflation(cc);
 +
 +    reinitializeClientAuths();
 +    this.creationDate = new Date();
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("{}: Has been reinitialized", this);
 +    }
 +  }
 +
 +  protected boolean isDurable() {
 +    return getProxyID().isDurable();
 +  }
 +
 +  protected String getDurableId() {
 +    return getProxyID().getDurableId();
 +  }
 +
 +  protected int getDurableTimeout() {
 +    return getProxyID().getDurableTimeout();
 +  }
 +
 +  private boolean getDurableKeepAlive() {
 +    return this.keepalive;
 +  }
 +
 +  protected String getHARegionName() {
 +    return getProxyID().getHARegionName();
 +  }
 +
 +  public Region getHARegion() {
 +    return this._messageDispatcher._messageQueue.getRegion();
 +  }
 +  
 +  public Version getVersion() {
 +    return this.clientVersion;
 +  }
 +  
 +  protected void scheduleDurableExpirationTask() {
 +    SystemTimer.SystemTimerTask task = new SystemTimer.SystemTimerTask() {
 +      @Override
 +      public void run2() {
 +        _durableExpirationTask.compareAndSet(this, null);
 +        logger.warn(LocalizedMessage.create(LocalizedStrings.CacheClientProxy_0__THE_EXPIRATION_TASK_HAS_FIRED_SO_THIS_PROXY_IS_BEING_TERMINATED, CacheClientProxy.this));
 +        // Remove the proxy from the CacheClientNofier's registry
 +        getCacheClientNotifier().removeClientProxy(CacheClientProxy.this);
 +        getCacheClientNotifier().durableClientTimedOut(CacheClientProxy.this.proxyID);
 +
 +        // Close the proxy
 +        terminateDispatching(false);
 +        _cacheClientNotifier._statistics.incQueueDroppedCount();
 +
 +        /**
 +         * Setting the expiration task to null again and cancelling existing
 +         * one, if any. See #50894.
 +         * <p/>
 +         * The message dispatcher may again set the expiry task in below path:
 +         * <code>
 +         *  com.gemstone.gemfire.internal.cache.tier.sockets.CacheClientProxy.scheduleDurableExpirationTask(CacheClientProxy.java:2020)
 +         *  com.gemstone.gemfire.internal.cache.tier.sockets.CacheClientProxy.pauseDispatching(CacheClientProxy.java:924)
 +         *  com.gemstone.gemfire.internal.cache.tier.sockets.CacheClientProxy$MessageDispatcher.pauseOrUnregisterProxy(CacheClientProxy.java:2813)
 +         *  com.gemstone.gemfire.internal.cache.tier.sockets.CacheClientProxy$MessageDispatcher.run(CacheClientProxy.java:2692)
 +         * </code>
 +         * <p/>
 +         * This is because message dispatcher may get an IOException with
 +         * "Proxy closing due to socket being closed locally" during/after
 +         * terminateDispatching(false) above.
 +         */
 +        Object task = _durableExpirationTask.getAndSet(null);
 +        if (task != null) {
 +          ((SystemTimerTask)task).cancel();
 +        }
 +      }
 +
 +    };
 +    if(this._durableExpirationTask.compareAndSet(null, task)) {
 +      _cache.getCCPTimer().schedule(task,
 +          getDurableTimeout()*1000L);
 +    }
 +  }
 +
 +  protected void cancelDurableExpirationTask(boolean logMessage) {
 +    SystemTimer.SystemTimerTask task = (SystemTimerTask) _durableExpirationTask.getAndSet(null);
 +    if (task != null) {
 +      if (logMessage) {
 +        logger.info(LocalizedMessage.create(LocalizedStrings.CacheClientProxy_0_CANCELLING_EXPIRATION_TASK_SINCE_THE_CLIENT_HAS_RECONNECTED, this));
 +      }
 +      task.cancel();
 +    }
 +  }
 +
 +  /**
 +   * Class <code>ClientInterestList</code> provides a convenient interface
 +   * for manipulating client interest information.
 +   */
 +  static protected class ClientInterestList
 +   {
 +    
 +    final CacheClientProxy ccp;
 +    
 +    final Object id;
 +
 +     /**
 +     * An object used for synchronizing the interest lists
 +     */
 +    final private Object interestListLock = new Object();
 +    
 +    /**
 +     * Regions that this client is interested in
 +     */
 +    final protected Set<String> regions = new HashSet<String>();
 +
 +    /**
 +     * Constructor.
 +     */
 +    protected ClientInterestList(CacheClientProxy ccp, Object interestID) {
 +      this.ccp = ccp;
 +      this.id = interestID;
 +      // this.id = getNextId();
 +    }
 +
 +    /**
 +     * Registers interest in the input region name and key
 +     */
 +    protected void registerClientInterest(String regionName,
 +        Object keyOfInterest, int interestType, boolean sendUpdatesAsInvalidates)
 +    {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}: registerClientInterest region={} key={}", ccp, regionName, keyOfInterest);
 +      }
 +      Set keysRegistered = null;
 +      synchronized(this.interestListLock) {
 +        LocalRegion r = (LocalRegion)this.ccp._cache.getRegion(regionName, true);
 +        if (r == null) {
 +          throw new RegionDestroyedException("Region could not be found for interest registration", regionName);
 +        }
 +        if ( ! (r instanceof CacheDistributionAdvisee) ) {
 +          throw new IllegalArgumentException("region " + regionName + " is not distributed and does not support interest registration");
 +        }
 +        FilterProfile p = r.getFilterProfile();
 +        keysRegistered = p.registerClientInterest(id, keyOfInterest, interestType, sendUpdatesAsInvalidates);
 +        regions.add(regionName);
 +      }
 +      // Perform actions if any keys were registered  
 +      if ((keysRegistered != null) && containsInterestRegistrationListeners()
 +          && !keysRegistered.isEmpty()) {
 +        handleInterestEvent(regionName, keysRegistered, interestType, true);
 +      } 
 +    }
 +    
 +    
 +    protected FilterProfile getProfile(String regionName) {
 +      try {
 +        return this.ccp._cache.getFilterProfile(regionName);
 +      } catch (CacheClosedException e) {
 +        return null;
 +      }
 +    }
 +
 +    /**
 +     * Unregisters interest in the input region name and key
 +     *
 +     * @param regionName
 +     *          The fully-qualified name of the region in which to unregister
 +     *          interest
 +     * @param keyOfInterest
 +     *          The key in which to unregister interest
 +     */
 +    protected void unregisterClientInterest(String regionName,
 +        Object keyOfInterest, int interestType)
 +    {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}: unregisterClientInterest region={} key={}", ccp, regionName, keyOfInterest);
 +      }
 +      FilterProfile p = getProfile(regionName);
 +      Set keysUnregistered = null;
 +      synchronized(this.interestListLock) {
 +        if (p != null) {
 +          keysUnregistered = p.unregisterClientInterest(
 +            id, keyOfInterest, interestType);
 +          if (!p.hasInterestFor(id)) {
 +            this.regions.remove(regionName);
 +          }
 +        } else {
 +          this.regions.remove(regionName);
 +        }
 +      }
 +      if (keysUnregistered != null && !keysUnregistered.isEmpty()) { 
 +        handleInterestEvent(regionName, keysUnregistered, interestType, false); 
 +      } 
 +    }
 +
 +    /**
 +     * Registers interest in the input region name and list of keys
 +     *
 +     * @param regionName
 +     *          The fully-qualified name of the region in which to register
 +     *          interest
 +     * @param keysOfInterest
 +     *          The list of keys in which to register interest
 +     */
 +    protected void registerClientInterestList(String regionName,
 +        List keysOfInterest, boolean sendUpdatesAsInvalidates) {
 +      FilterProfile p = getProfile(regionName);
 +      if (p == null) {
 +        throw new RegionDestroyedException("Region not found during client interest registration", regionName);
 +      }
 +      Set keysRegistered = null;
 +      synchronized(this.interestListLock) {
 +        keysRegistered = p.registerClientInterestList(id, keysOfInterest, sendUpdatesAsInvalidates);
 +        regions.add(regionName);
 +      }
 +      // Perform actions if any keys were registered 
 +      if (containsInterestRegistrationListeners() && !keysRegistered.isEmpty()) {
 +        handleInterestEvent(regionName, keysRegistered, InterestType.KEY, true);
 +      } 
 +    }
 +
 +    /**
 +     * Unregisters interest in the input region name and list of keys
 +     *
 +     * @param regionName
 +     *          The fully-qualified name of the region in which to unregister
 +     *          interest
 +     * @param keysOfInterest
 +     *          The list of keys in which to unregister interest
 +     */
 +    protected void unregisterClientInterestList(String regionName,
 +        List keysOfInterest)
 +    {
 +      FilterProfile p = getProfile(regionName);
 +      Set keysUnregistered = null;
 +      synchronized(this.interestListLock) {
 +        if (p != null) {
 +          keysUnregistered = p.unregisterClientInterestList(
 +              id, keysOfInterest);
 +          if (!p.hasInterestFor(id)) {
 +            regions.remove(regionName);
 +          }
 +        } else {
 +          regions.remove(regionName);
 +        }
 +      }
 +     // Perform actions if any keys were unregistered
 +      if (!keysUnregistered.isEmpty()) { 
 +        handleInterestEvent(regionName, keysUnregistered, InterestType.KEY,false); 
 +      } 
 +    }
 +
 +    /*
 +     * Returns whether this interest list has any keys, patterns or filters of
 +     * interest. It answers the question: Are any clients being notified because
 +     * of this interest list? @return whether this interest list has any keys,
 +     * patterns or filters of interest
 +     */
 +    protected boolean hasInterest() {
 +      return regions.size() > 0;
 +    }
 +
 +    protected void clearClientInterestList() {
 +      boolean isClosed = ccp.getCache().isClosed();
 +      
 +      synchronized(this.interestListLock) {
 +        for (String regionName: regions) {
 +          FilterProfile p = getProfile(regionName);
 +          if (p == null) {
 +            continue;
 +          }
 +          if (!isClosed) {
 +            if (p.hasAllKeysInterestFor(id)) {
 +              Set allKeys = new HashSet();
 +              allKeys.add(".*");
 +              allKeys = Collections.unmodifiableSet(allKeys);
 +              handleInterestEvent(regionName, allKeys,
 +                  InterestType.REGULAR_EXPRESSION, false);
 +            }
 +            Set keysOfInterest = p.getKeysOfInterestFor(id);
 +            if (keysOfInterest != null && keysOfInterest.size() > 0) {
 +              handleInterestEvent(regionName, keysOfInterest,
 +                  InterestType.KEY, false);
 +            }
 +            Map<String,Pattern> patternsOfInterest =
 +              p.getPatternsOfInterestFor(id);
 +            if (patternsOfInterest != null && patternsOfInterest.size() > 0) {
 +              handleInterestEvent(regionName, patternsOfInterest.keySet(),
 +                  InterestType.REGULAR_EXPRESSION, false);
 +            }
 +          }
 +          p.clearInterestFor(id);
 +        }
 +        regions.clear();
 +      }
 +    }
 +
 +
 +    private void handleInterestEvent(String regionName, Set keysOfInterest,
 +        int interestType, boolean isRegister) {
 +      // Notify the region about this register interest event if:
 +      // - the application has requested it
 +      // - this is a primary CacheClientProxy (otherwise multiple notifications
 +      // may occur)
 +      // - it is a key interest type (regex is currently not supported)
 +      InterestRegistrationEvent event = null;
 +      if (NOTIFY_REGION_ON_INTEREST && this.ccp.isPrimary()
 +          && interestType == InterestType.KEY) {
 +        event = new InterestRegistrationEventImpl(this.ccp, regionName,
 +            keysOfInterest, interestType, isRegister);
 +        try {
 +          notifyRegionOfInterest(event);
 +        }
 +        catch (Exception e) {
 +          logger.warn(LocalizedStrings.CacheClientProxy_REGION_NOTIFICATION_OF_INTEREST_FAILED, e);
 +        }
 +      }
 +      // Invoke interest registration listeners
 +      if (containsInterestRegistrationListeners()) {
 +        if (event == null) {
 +          event = new InterestRegistrationEventImpl(this.ccp, regionName,
 +              keysOfInterest, interestType, isRegister);
 +        }
 +        notifyInterestRegistrationListeners(event);
 +      }      
 +    }
 +
 +    private void notifyRegionOfInterest(InterestRegistrationEvent event) {
 +      this.ccp.getCacheClientNotifier().handleInterestEvent(event);
 +    }
 +
 +    private void notifyInterestRegistrationListeners(
 +        InterestRegistrationEvent event) {
 +      this.ccp.getCacheClientNotifier().notifyInterestRegistrationListeners(
 +          event);
 +    }
 +
 +    private boolean containsInterestRegistrationListeners() {
 +      return this.ccp.getCacheClientNotifier()
 +          .containsInterestRegistrationListeners();
 +    }
 +  }
 +
 +
 +  /**
 +   * Class <code>MessageDispatcher</code> is a <code>Thread</code> that
 +   * processes messages bound for the client by taking messsages from the
 +   * message queue and sending them to the client over the socket.
 +   */
 +  static class MessageDispatcher extends Thread
 +   {
 +
 +    /**
 +     * The queue of messages to be sent to the client
 +     */
 +    protected final HARegionQueue _messageQueue;
 +
 +//    /**
 +//     * An int used to keep track of the number of messages dropped for logging
 +//     * purposes. If greater than zero then a warning has been logged about
 +//     * messages being dropped.
 +//     */
 +//    private int _numberOfMessagesDropped = 0;
 +
 +    /**
 +     * The proxy for which this dispatcher is processing messages
 +     */
 +    private final CacheClientProxy _proxy;
 +
 +//    /**
 +//     * The conflator faciliates message conflation
 +//     */
 +//     protected BridgeEventConflator _eventConflator;
 +
 +    /**
 +     * Whether the dispatcher is stopped
 +     */
 +    private volatile boolean _isStopped = true;
 +
 +    /**
 +     * @guarded.By _pausedLock
 +     */
 +    //boolean _isPausedDispatcher = false;
 +    
 +    /**
 +     * A lock object used to control pausing this dispatcher
 +     */
 +    protected final Object _pausedLock = new Object();
 +
 +    /**
 +     * An object used to protect when dispatching is being stopped.
 +     */
 +    private final Object _stopDispatchingLock = new Object();
 +
 +    private final ReadWriteLock socketLock = new ReentrantReadWriteLock();
 +
 +    private final Lock socketWriteLock = socketLock.writeLock();
 +//    /**
 +//     * A boolean verifying whether a warning has already been issued if the
 +//     * message queue has reached its capacity.
 +//     */
 +//    private boolean _messageQueueCapacityReachedWarning = false;
 +
 +    /**
 +     * Constructor.
 +     *
 +     * @param proxy
 +     *          The <code>CacheClientProxy</code> for which this dispatcher is
 +     *          processing messages
 +     * @param name thread name for this dispatcher
 +     

<TRUNCATED>


[49/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXTypeByIdOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXTypeByIdOp.java
index e0b2810,0000000..1d9bd2d
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXTypeByIdOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXTypeByIdOp.java
@@@ -1,94 -1,0 +1,92 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +import com.gemstone.gemfire.pdx.internal.PdxType;
 +
 +/**
 + * Retrieve the PDXType, given an integer PDX id, from a server.
 + * @author dsmith
 + * @since 6.6
 + */
 +public class GetPDXTypeByIdOp {
 +  /**
 +   * Get a PdxType from the given pool.
 +   * @param pool the pool to use to communicate with the server.
 +   */
 +  public static PdxType execute(ExecutablePool pool,
 +                             int pdxId)
 +  {
 +    AbstractOp op = new GetPDXTypeByIdOpImpl(pdxId);
 +    return (PdxType) pool.execute(op);
 +  }
 +                                                               
 +  private GetPDXTypeByIdOp() {
 +    // no instances allowed
 +  }
 +  
 +  private static class GetPDXTypeByIdOpImpl extends AbstractOp {
 +    /**
 +     * @throws com.gemstone.gemfire.SerializationException if serialization fails
 +     */
 +    public GetPDXTypeByIdOpImpl(int pdxId) {
 +      super(MessageType.GET_PDX_TYPE_BY_ID, 1);
 +      getMessage().addIntPart(pdxId);
 +    }
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      return processObjResponse(msg, "getPDXTypeById");
 +    }
 +    @Override
 +    protected boolean isErrorResponse(int msgType) {
 +      return false;
 +    }
 +    @Override
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startGetPDXTypeById();
 +    }
 +    @Override
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endGetPDXTypeByIdSend(start, hasFailed());
 +    }
 +    @Override
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endGetPDXTypeById(start, hasTimedOut(), hasFailed());
 +    }
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +    //Don't send the transaction id for this message type.
 +    @Override
 +    protected boolean participateInTransaction() {
 +      return false;
 +    }
 +    
-     //TODO - no idea what this mumbo jumbo means, but it's on
-     //most of the other messages like this.
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXTypesOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXTypesOp.java
index 2990192,0000000..262cb9a
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXTypesOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/GetPDXTypesOp.java
@@@ -1,112 -1,0 +1,112 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import java.util.Map;
 +
 +import com.gemstone.gemfire.InternalGemFireError;
 +import com.gemstone.gemfire.cache.client.ServerOperationException;
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Part;
 +import com.gemstone.gemfire.pdx.internal.PdxType;
 +
 +/**
 + * Retrieve all known PDX types.
 + * 
 + * @author bakera
 + * @since 7.0
 + */
 +public class GetPDXTypesOp {
 +
 +  public static Map<Integer, PdxType> execute(ExecutablePool pool) {
 +    AbstractOp op = new GetPDXTypesOpImpl();
 +    return (Map<Integer, PdxType>) pool.execute(op);
 +  }
 +                                                               
 +  private GetPDXTypesOp() {
 +    // no instances allowed
 +  }
 +  
 +  private static class GetPDXTypesOpImpl extends AbstractOp {
 +    public GetPDXTypesOpImpl() {
 +      super(MessageType.GET_PDX_TYPES, 1);
 +      getMessage().addIntPart(0); // must have at least one part
 +    }
 +
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      Part part = msg.getPart(0);
 +      int msgType = msg.getMessageType();
 +      if (msgType == MessageType.RESPONSE) {
 +        return (Map<Integer, PdxType>) part.getObject();
 +
 +      } else {
 +        if (msgType == MessageType.EXCEPTION) {
 +          String s = "While performing a remote " + "getPdxTypes";
 +          throw new ServerOperationException(s, (Throwable) part.getObject());
 +
 +        } else if (isErrorResponse(msgType)) {
 +          throw new ServerOperationException(part.getString());
 +
 +        } else {
 +          throw new InternalGemFireError("Unexpected message type "
 +              + MessageType.getString(msgType));
 +        }
 +      }
 +    }
 +
 +    @Override
 +    protected boolean isErrorResponse(int msgType) {
 +      return false;
 +    }
 +
 +    @Override
 +    protected long startAttempt(ConnectionStats stats) {
 +      return 0;
 +    }
 +
 +    @Override
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +    }
 +
 +    @Override
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +    }
 +    
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +
 +    @Override
 +    protected boolean participateInTransaction() {
 +      return false;
 +    }
 +
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/MakePrimaryOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/MakePrimaryOp.java
index 59b99f0,0000000..27d80b1
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/MakePrimaryOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/MakePrimaryOp.java
@@@ -1,91 -1,0 +1,91 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +
 +/**
 + * Tell a server to become the primary host of a server-to-client queue
 + * @author darrel
 + * @since 5.7
 + */
 +public class MakePrimaryOp {
 +  /**
 +   * Tell the given server to become the primary host of a server-to-client queue
 +   * @param pool the pool to use to communicate with the server.
 +   * @param conn the connection to do the execution on
 +   * @param sentClientReady true if the client ready message has already been sent
 +   */
 +  public static void execute(ExecutablePool pool, Connection conn, boolean sentClientReady)
 +  {
 +    AbstractOp op = new MakePrimaryOpImpl(sentClientReady);
 +    pool.executeOn(conn, op);
 +  }
 +                                                               
 +  private MakePrimaryOp() {
 +    // no instances allowed
 +  }
 +  
 +  private static class MakePrimaryOpImpl extends AbstractOp {
 +    /**
 +     * @throws com.gemstone.gemfire.SerializationException if serialization fails
 +     */
 +    public MakePrimaryOpImpl(boolean sentClientReady) {
 +      super(MessageType.MAKE_PRIMARY, 1);
 +      getMessage().addBytesPart(new byte[] {(byte)(sentClientReady?0x01:0x00)});
 +    }
 +
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }
 +
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      processAck(msg, "makePrimary");
 +      return null;
 +    }
 +    @Override
 +    protected boolean isErrorResponse(int msgType) {
 +      return false;
 +    }
 +    @Override
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startMakePrimary();
 +    }
 +    @Override
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endMakePrimarySend(start, hasFailed());
 +    }
 +    @Override
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endMakePrimary(start, hasTimedOut(), hasFailed());
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/PingOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/PingOp.java
index e0bc81b,0000000..e70d50a
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/PingOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/PingOp.java
@@@ -1,97 -1,0 +1,97 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.distributed.internal.ServerLocation;
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +
 +/**
 + * Ping a server to see if it is still alive.
 + * @author darrel
 + * @since 5.7
 + */
 +public class PingOp {
 +  /**
 +   * Ping the specified server to see if it is still alive
 +   * @param pool the pool to use to communicate with the server.
 +   * @param server the server to do the execution on
 +   */
 +  public static void execute(ExecutablePool pool, ServerLocation server)
 +  {
 +    AbstractOp op = new PingOpImpl();
 +    pool.executeOn(server, op, false,false);
 +  }
 +                                                               
 +  private PingOp() {
 +    // no instances allowed
 +  }
 +
 +  static class PingOpImpl extends AbstractOp {
 +    
 +    private long startTime;
 +    
 +    /**
 +     * @throws com.gemstone.gemfire.SerializationException if serialization fails
 +     */
 +    public PingOpImpl() {
 +      super(MessageType.PING, 0);
 +    }
 +
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +      Message.messageType.set(null);
 +    }
 +
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      startTime = System.currentTimeMillis();
 +      getMessage().send(false);
 +      Message.messageType.set(MessageType.PING);
 +    }
 +
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      processAck(msg, "ping");
 +      return null;
 +    }
 +    @Override
 +    protected boolean isErrorResponse(int msgType) {
 +      return false;
 +    }
 +    @Override
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startPing();
 +    }
 +    @Override
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endPingSend(start, hasFailed());
 +    }
 +    @Override
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endPing(start, hasTimedOut(), hasFailed());
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/PrimaryAckOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/PrimaryAckOp.java
index 0b65c56,0000000..4ee680a
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/PrimaryAckOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/PrimaryAckOp.java
@@@ -1,101 -1,0 +1,101 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +
 +import java.util.Iterator;
 +import java.util.List;
 +
 +/**
 + * Send the primary server acknowledgement on the events this client
 + * has received and processed from it.
 + * @author darrel
 + * @since 5.7
 + */
 +public class PrimaryAckOp {
 +  /**
 +   * Send the primary server acknowledgement on the events this client
 +   * has received and processed from it
 +   * using connections from the given pool
 +   * to communicate with the server.
 +   * @param connection 
 +   * @param pool the pool to use to communicate with the server.
 +   * @param events list of events to acknowledge
 +   */
 +  public static void execute(Connection connection, ExecutablePool pool,
 +                             List events)
 +  {
 +    AbstractOp op = new PrimaryAckOpImpl(events);
 +    pool.executeOn(connection, op);
 +  }
 +                                                               
 +  private PrimaryAckOp() {
 +    // no instances allowed
 +  }
 +  
 +  private static class PrimaryAckOpImpl extends AbstractOp {
 +    /**
 +     * @throws com.gemstone.gemfire.SerializationException if serialization fails
 +     */
 +    public PrimaryAckOpImpl(List events) {
 +      super(MessageType.PERIODIC_ACK, events.size());
 +      for (Iterator i = events.iterator(); i.hasNext();) {
 +        getMessage().addObjPart(i.next());
 +      }
 +    }
 +
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }
 +
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      processAck(msg, "primaryAck");
 +      return null;
 +    }
 +    @Override
 +    protected boolean isErrorResponse(int msgType) {
 +      return false;
 +    }
 +    @Override
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startPrimaryAck();
 +    }
 +    @Override
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endPrimaryAckSend(start, hasFailed());
 +    }
 +    @Override
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endPrimaryAck(start, hasTimedOut(), hasFailed());
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/ProxyCacheCloseOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/ProxyCacheCloseOp.java
index 53b8fa9,0000000..2747fa8
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/ProxyCacheCloseOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/ProxyCacheCloseOp.java
@@@ -1,124 -1,0 +1,124 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import java.util.Properties;
 +
 +import com.gemstone.gemfire.InternalGemFireError;
 +import com.gemstone.gemfire.cache.client.ServerConnectivityException;
 +import com.gemstone.gemfire.cache.client.ServerOperationException;
 +import com.gemstone.gemfire.distributed.internal.ServerLocation;
 +import com.gemstone.gemfire.internal.HeapDataOutputStream;
 +import com.gemstone.gemfire.internal.Version;
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Part;
 +
 +public class ProxyCacheCloseOp {
 +
 +  public static Object executeOn(ServerLocation location, ExecutablePool pool,
 +      Properties securityProps, boolean keepAlive) {
 +    AbstractOp op = new ProxyCacheCloseOpImpl(pool, securityProps, keepAlive);
 +    return pool.executeOn(location, op);
 +  }
 +
 +  private ProxyCacheCloseOp() {
 +    // no instances allowed
 +  }
 +
 +  static class ProxyCacheCloseOpImpl extends AbstractOp {
 +
 +    public ProxyCacheCloseOpImpl(ExecutablePool pool, Properties securityProps,
 +        boolean keepAlive) {
 +      super(MessageType.REMOVE_USER_AUTH, 1);
-       getMessage().setEarlyAck(Message.MESSAGE_HAS_SECURE_PART);
++      getMessage().setMessageHasSecurePartFlag();
 +      getMessage().addBytesPart(keepAlive ? new byte[] {1} : new byte[] {0});
 +    }
 +
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
 +      HeapDataOutputStream hdos = new HeapDataOutputStream(Version.CURRENT);
 +      byte[] secureBytes = null;
 +      hdos.writeLong(cnx.getConnectionID());
 +      Object userId = UserAttributes.userAttributes.get().getServerToId().get(cnx.getServer());
 +      if (userId == null) {
 +        // This will ensure that this op is retried on another server, unless
 +        // the retryCount is exhausted. Fix for Bug 41501
 +        throw new ServerConnectivityException(
 +            "Connection error while authenticating user");
 +      }
 +      hdos.writeLong((Long)userId);
 +      try {
 +        secureBytes = ((ConnectionImpl)cnx).getHandShake().encryptBytes(
 +            hdos.toByteArray());
 +      } finally {
 +        hdos.close();
 +      }
 +      getMessage().setSecurePart(secureBytes);
 +      getMessage().send(false);
 +    }
 +
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      Part part = msg.getPart(0);
 +      final int msgType = msg.getMessageType();
 +      if (msgType == MessageType.REPLY) {
 +        return part.getObject();
 +      }
 +      else if (msgType == MessageType.EXCEPTION) {
 +        String s = "While performing a remote proxy cache close";
 +        throw new ServerOperationException(s, (Throwable)part.getObject());
 +        // Get the exception toString part.
 +        // This was added for c++ thin client and not used in java
 +        // Part exceptionToStringPart = msg.getPart(1);
 +      }
 +      else if (isErrorResponse(msgType)) {
 +        throw new ServerOperationException(part.getString());
 +      }
 +      else {
 +        throw new InternalGemFireError("Unexpected message type "
 +            + MessageType.getString(msgType));
 +      }
 +    }
 +
 +    @Override
 +    protected boolean isErrorResponse(int msgType) {
 +      return msgType == MessageType.REQUESTDATAERROR;
 +    }
 +
 +    @Override
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startGet();
 +    }
 +
 +    @Override
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endGetSend(start, hasFailed());
 +    }
 +
 +    @Override
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endGet(start, hasTimedOut(), hasFailed());
 +    }
 +  }
 +
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/ReadyForEventsOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/ReadyForEventsOp.java
index a003538,0000000..d2631fc
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/ReadyForEventsOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/ReadyForEventsOp.java
@@@ -1,91 -1,0 +1,91 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +
 +/**
 + * Tells the server we are ready to receive server-to-client events
 + * from durable subscriptions.
 + * @author darrel
 + * @since 5.7
 + */
 +public class ReadyForEventsOp {
 +  /**
 +   * Tells the primary server we are ready to receive server-to-client events
 +   * from durable subscriptions.
 +   * @param pool the pool to use to communicate with the server.
 +   * @param primary 
 +   */
 +  public static void execute(ExecutablePool pool, QueueConnectionImpl primary)
 +  {
 +    AbstractOp op = new ReadyForEventsOpImpl();
 +    pool.executeOn(primary, op);
 +  }
 +                                                               
 +  private ReadyForEventsOp() {
 +    // no instances allowed
 +  }
 +  
 +  private static class ReadyForEventsOpImpl extends AbstractOp {
 +    /**
 +     * @throws com.gemstone.gemfire.SerializationException if serialization fails
 +     */
 +    public ReadyForEventsOpImpl() {
 +      super(MessageType.CLIENT_READY, 1);
 +    }
 +
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }
 +
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      processAck(msg, "readyForEvents");
 +      return null;
 +    }
 +    @Override
 +    protected boolean isErrorResponse(int msgType) {
 +      return false;
 +    }
 +    @Override
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startReadyForEvents();
 +    }
 +    @Override
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endReadyForEventsSend(start, hasFailed());
 +    }
 +    @Override
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endReadyForEvents(start, hasTimedOut(), hasFailed());
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/RegisterDataSerializersOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/RegisterDataSerializersOp.java
index b2b975f,0000000..869ad64
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/RegisterDataSerializersOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/RegisterDataSerializersOp.java
@@@ -1,138 -1,0 +1,138 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import java.io.IOException;
 +
 +import com.gemstone.gemfire.DataSerializer;
 +import com.gemstone.gemfire.SerializationException;
 +import com.gemstone.gemfire.internal.InternalDataSerializer.SerializerAttributesHolder;
 +import com.gemstone.gemfire.internal.cache.ClientServerObserver;
 +import com.gemstone.gemfire.internal.cache.ClientServerObserverHolder;
 +import com.gemstone.gemfire.internal.cache.EventID;
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +import com.gemstone.gemfire.internal.util.BlobHelper;
 +
 +public class RegisterDataSerializersOp {
 +
 +  public static void execute(ExecutablePool pool,
 +      DataSerializer[] dataSerializers, EventID eventId) {
 +    AbstractOp op = new RegisterDataSerializersOpImpl(dataSerializers,
 +        eventId);
 +    pool.execute(op);
 +  }
 +  
 +  public static void execute(ExecutablePool pool,
 +      SerializerAttributesHolder[] holders, EventID eventId) {
 +    AbstractOp op = new RegisterDataSerializersOpImpl(holders,
 +        eventId);
 +    pool.execute(op);
 +  }
 +  
 +  private RegisterDataSerializersOp() {
 +    // no instances allowed
 +  }
 +  
 +  private static class RegisterDataSerializersOpImpl extends AbstractOp {
 +
 +    /**
 +     * @throws com.gemstone.gemfire.SerializationException if serialization fails
 +     */
 +    public RegisterDataSerializersOpImpl(DataSerializer[] dataSerializers,
 +        EventID eventId) {
 +      super(MessageType.REGISTER_DATASERIALIZERS, dataSerializers.length * 2 + 1);
 +      for(int i = 0; i < dataSerializers.length; i++) {
 +        DataSerializer dataSerializer = dataSerializers[i];
 +         // strip '.class' off these class names
 +        String className = dataSerializer.getClass().toString().substring(6);
 +        try {
 +          getMessage().addBytesPart(BlobHelper.serializeToBlob(className));
 +        } catch (IOException ex) {
 +          throw new SerializationException("failed serializing object", ex);
 +        }
 +        getMessage().addIntPart(dataSerializer.getId());
 +      }
 +      getMessage().addBytesPart(eventId.calcBytes());
 +      // // CALLBACK FOR TESTING PURPOSE ONLY ////
 +      if (PoolImpl.IS_INSTANTIATOR_CALLBACK) {
 +        ClientServerObserver bo = ClientServerObserverHolder.getInstance();
 +        bo.beforeSendingToServer(eventId);
 +      }
 +   }
 +    
 +    /**
 +     * @throws SerializationException
 +     *           Thrown when serialization fails.
 +     */
 +    public RegisterDataSerializersOpImpl(SerializerAttributesHolder[] holders,
 +        EventID eventId) {
 +      super(MessageType.REGISTER_DATASERIALIZERS, holders.length * 2 + 1);
 +      for (int i = 0; i < holders.length; i++) {
 +        try {
 +          getMessage().addBytesPart(
 +              BlobHelper.serializeToBlob(holders[i].getClassName()));
 +        } catch (IOException ex) {
 +          throw new SerializationException("failed serializing object", ex);
 +        }
 +        getMessage().addIntPart(holders[i].getId());
 +      }
 +      getMessage().addBytesPart(eventId.calcBytes());
 +      // // CALLBACK FOR TESTING PURPOSE ONLY ////
 +      if (PoolImpl.IS_INSTANTIATOR_CALLBACK) {
 +        ClientServerObserver bo = ClientServerObserverHolder.getInstance();
 +        bo.beforeSendingToServer(eventId);
 +      }
 +    }
 +
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      processAck(msg, "registerDataSerializers");
 +      return null;
 +    }
 +    
 +    @Override
 +    protected boolean isErrorResponse(int msgType) {
 +      return false;
 +    }
 +    @Override
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startRegisterDataSerializers();
 +    }
 +    @Override
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endRegisterDataSerializersSend(start, hasFailed());
 +    }
 +    @Override
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endRegisterDataSerializers(start, hasTimedOut(), hasFailed());
 +    }
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/RegisterInstantiatorsOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/RegisterInstantiatorsOp.java
index 93d3756,0000000..0d5a137
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/RegisterInstantiatorsOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/RegisterInstantiatorsOp.java
@@@ -1,180 -1,0 +1,180 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import java.io.IOException;
 +
 +import com.gemstone.gemfire.Instantiator;
 +import com.gemstone.gemfire.SerializationException;
 +import com.gemstone.gemfire.internal.InternalInstantiator.InstantiatorAttributesHolder;
 +import com.gemstone.gemfire.internal.cache.ClientServerObserver;
 +import com.gemstone.gemfire.internal.cache.ClientServerObserverHolder;
 +import com.gemstone.gemfire.internal.cache.EventID;
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +import com.gemstone.gemfire.internal.util.BlobHelper;
 +
 +/**
 + * Register a bunch of instantiators on a server
 + * @author darrel
 + * @since 5.7
 + */
 +public class RegisterInstantiatorsOp {
 +  /**
 +   * Register a bunch of instantiators on a server
 +   * using connections from the given pool
 +   * to communicate with the server.
 +   * @param pool the pool to use to communicate with the server.
 +   * @param instantiators the instantiators to register
 +   * @param eventId the id of this event
 +   */
 +  public static void execute(ExecutablePool pool,
 +                             Instantiator[] instantiators,
 +                             EventID eventId)
 +  {
 +    AbstractOp op = new RegisterInstantiatorsOpImpl(instantiators, eventId);
 +    pool.execute(op, Integer.MAX_VALUE);
 +  }
 +
 +  /**
 +   * Register a bunch of instantiators on a server using connections from the
 +   * given pool to communicate with the server.
 +   * 
 +   * @param pool
 +   *          the pool to use to communicate with the server.
 +   * @param holders
 +   *          the {@link InstantiatorAttributesHolder}s containing info about
 +   *          the instantiators to register
 +   * @param eventId
 +   *          the id of this event
 +   */
 +  public static void execute(ExecutablePool pool,
 +      Object[] holders, EventID eventId) {
 +    AbstractOp op = new RegisterInstantiatorsOpImpl(holders,
 +        eventId);
 +    pool.execute(op, Integer.MAX_VALUE);
 +  }
 +
 +  private RegisterInstantiatorsOp() {
 +    // no instances allowed
 +  }
 +  
 +  private static class RegisterInstantiatorsOpImpl extends AbstractOp {
 +    /**
 +     * @throws com.gemstone.gemfire.SerializationException if serialization fails
 +     */
 +    public RegisterInstantiatorsOpImpl(Instantiator[] instantiators,
 +                                       EventID eventId) {
 +      super(MessageType.REGISTER_INSTANTIATORS, instantiators.length * 3 + 1);
 +      for(int i = 0; i < instantiators.length; i++) {
 +        Instantiator instantiator = instantiators[i];
 +         // strip '.class' off these class names
 +        String className = instantiator.getClass().toString().substring(6);
 +        String instantiatedClassName = instantiator.getInstantiatedClass().toString().substring(6);
 +        try {
 +          getMessage().addBytesPart(BlobHelper.serializeToBlob(className));
 +          getMessage().addBytesPart(BlobHelper.serializeToBlob(instantiatedClassName));
 +        } catch (IOException ex) {
 +          throw new SerializationException("failed serializing object", ex);
 +        }
 +        getMessage().addIntPart(instantiator.getId());
 +      }
 +      getMessage().addBytesPart(eventId.calcBytes());
 +//     // // CALLBACK FOR TESTING PURPOSE ONLY ////
 +      if (PoolImpl.IS_INSTANTIATOR_CALLBACK) {
 +        ClientServerObserver bo = ClientServerObserverHolder.getInstance();
 +        bo.beforeSendingToServer(eventId);
 +      }
 +    }
 +
 +    /**
 +     * @throws com.gemstone.gemfire.SerializationException
 +     *           if serialization fails
 +     */
 +    public RegisterInstantiatorsOpImpl(Object[] holders,
 +        EventID eventId) {
 +      super(MessageType.REGISTER_INSTANTIATORS, holders.length * 3 + 1);
 +      for (Object obj : holders) {
 +        String instantiatorClassName = null;
 +        String instantiatedClassName = null;
 +        int id = 0;
 +        if (obj instanceof Instantiator) {
 +          instantiatorClassName = ((Instantiator)obj).getClass().getName();
 +          instantiatedClassName = ((Instantiator)obj).getInstantiatedClass()
 +              .getName();
 +          id = ((Instantiator)obj).getId();
 +        } else {
 +          instantiatorClassName = ((InstantiatorAttributesHolder)obj)
 +              .getInstantiatorClassName();
 +          instantiatedClassName = ((InstantiatorAttributesHolder)obj)
 +              .getInstantiatedClassName();
 +          id = ((InstantiatorAttributesHolder)obj).getId();
 +        }
 +        try {
 +          getMessage().addBytesPart(
 +              BlobHelper.serializeToBlob(instantiatorClassName));
 +          getMessage().addBytesPart(
 +              BlobHelper.serializeToBlob(instantiatedClassName));
 +        } catch (IOException ex) {
 +          throw new SerializationException("failed serializing object", ex);
 +        }
 +        getMessage().addIntPart(id);
 +      }
 +      getMessage().addBytesPart(eventId.calcBytes());
 +      // // // CALLBACK FOR TESTING PURPOSE ONLY ////
 +      if (PoolImpl.IS_INSTANTIATOR_CALLBACK) {
 +        ClientServerObserver bo = ClientServerObserverHolder.getInstance();
 +        bo.beforeSendingToServer(eventId);
 +      }
 +    }
 +
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      processAck(msg, "registerInstantiators");
 +      return null;
 +    }
 +    @Override
 +    protected boolean isErrorResponse(int msgType) {
 +      return false;
 +    }
 +    @Override
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startRegisterInstantiators();
 +    }
 +    @Override
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endRegisterInstantiatorsSend(start, hasFailed());
 +    }
 +    @Override
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endRegisterInstantiators(start, hasTimedOut(), hasFailed());
 +    }
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/RollbackOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/RollbackOp.java
index 2793f32,0000000..6f01b96
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/RollbackOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/RollbackOp.java
@@@ -1,99 -1,0 +1,99 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +
 +/**
 + * Does a Rollback on the server
 + * @since 6.6
 + * @author sbawaska
 + */
 +public class RollbackOp {
 +
 +  /**
 +   * Does a rollback on the server for given transaction
 +   * @param pool the pool to use to communicate with the server.
 +   * @param txId the id of the transaction to rollback
 +   */
 +  public static void execute(ExecutablePool pool, int txId) {
 +    RollbackOpImpl op = new RollbackOpImpl(txId);
 +    pool.execute(op);
 +  }
 +  
 +  private RollbackOp() {
 +    // no instance allowed
 +  }
 +  
 +  private static class RollbackOpImpl extends AbstractOp {
 +    private int txId;
 +
 +    protected RollbackOpImpl(int txId) {
 +      super(MessageType.ROLLBACK, 1);
 +      getMessage().setTransactionId(txId);
 +      this.txId = txId;
 +    }
 +    
 +    @Override
 +    public String toString() {
 +      return "Rollback(txId="+this.txId+")";
 +    }
 +
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      processAck(msg, "rollback");
 +      return null;
 +    }
 +
 +    @Override
 +    protected boolean isErrorResponse(int msgType) {
 +      return msgType == MessageType.EXCEPTION;
 +    }
 +
 +    @Override
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startRollback();
 +    }
 +
 +    @Override
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endRollbackSend(start, hasFailed());
 +    }
 +
 +    @Override
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endRollback(start, hasTimedOut(), hasFailed());
 +    }
 +     
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/SizeOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/SizeOp.java
index 6d69083,0000000..42cc225
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/SizeOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/SizeOp.java
@@@ -1,92 -1,0 +1,92 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +
 +/**
 + * Does a region size on a server
 + * @author gregp
 + * @since 6.6
 + */
 +public class SizeOp {
 +  /**
 +   * Does a region size on a server using connections from the given pool
 +   * to communicate with the server.
 +   * @param pool the pool to use to communicate with the server.
 +   * @param region the name of the region to do the entry keySet on
 +   */
 +  public static Integer execute(InternalPool pool,
 +                            String region)
 +  {
 +    AbstractOp op = new SizeOpImpl(region);
 +    return (Integer)pool.execute(op);
 +  }
 +                                                               
 +  private SizeOp() {
 +    // no instances allowed
 +  }
 +  
 +  private static class SizeOpImpl extends AbstractOp {
 +    /**
 +     * @throws com.gemstone.gemfire.SerializationException if serialization fails
 +     */
 +    public SizeOpImpl(String region) {
 +      super(MessageType.SIZE, 1);
 +      getMessage().addStringPart(region);
 +    }
 +
 +    @Override  
 +    protected Object processResponse(Message msg) throws Exception {
 +      
 +      return processObjResponse(msg, "size");
 +    }
 +    @Override  
 +    protected boolean isErrorResponse(int msgType) {
 +      return msgType == MessageType.SIZE_ERROR;
 +    }
 +    @Override  
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startSize();
 +    }
 +    @Override  
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endSizeSend(start, hasFailed());
 +    }
 +    @Override  
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endSize(start, hasTimedOut(), hasFailed());
 +    }
 +
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/TXFailoverOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/TXFailoverOp.java
index 1fecc7d,0000000..64ee66e
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/TXFailoverOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/TXFailoverOp.java
@@@ -1,93 -1,0 +1,93 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +
 +/**
 + * Indicates to the server that a transaction is
 + * failing over to this server. The server then
 + * performs the necessary bootstrapping for the tx.
 + * @author sbawaska
 + * @since 6.6
 + */
 +public class TXFailoverOp {
 +
 +  public static void execute(ExecutablePool pool, int txId) {
 +    pool.execute(new TXFailoverOpImpl(txId));
 +  }
 +  
 +  private TXFailoverOp() {
 +    // no instance
 +  }
 +  
 +  private static class TXFailoverOpImpl extends AbstractOp {
 +    int txId;
 +
 +    protected TXFailoverOpImpl(int txId) {
 +      super(MessageType.TX_FAILOVER, 1);
 +      getMessage().setTransactionId(txId);
 +      this.txId = txId;
 +    }
 +    
 +    @Override
 +    public String toString() {
 +      return "TXFailoverOp(txId="+this.txId+")";
 +    }
 +
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      processAck(msg, "txFailover");
 +      return null;
 +    }
 +
 +    @Override
 +    protected boolean isErrorResponse(int msgType) {
 +      return msgType == MessageType.EXCEPTION;
 +    }
 +
 +    @Override  
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startTxFailover();
 +    }
 +    @Override  
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endTxFailoverSend(start, hasFailed());
 +    }
 +    @Override  
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endTxFailover(start, hasTimedOut(), hasFailed());
 +    }
 +
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/TXSynchronizationOp.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/TXSynchronizationOp.java
index 48d66f2,0000000..34ecf4d
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/TXSynchronizationOp.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/client/internal/TXSynchronizationOp.java
@@@ -1,163 -1,0 +1,163 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import com.gemstone.gemfire.GemFireException;
 +import com.gemstone.gemfire.cache.CommitConflictException;
 +import com.gemstone.gemfire.cache.SynchronizationCommitConflictException;
 +import com.gemstone.gemfire.cache.client.ServerOperationException;
 +import com.gemstone.gemfire.internal.cache.TXCommitMessage;
 +import com.gemstone.gemfire.internal.cache.TXManagerImpl;
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Part;
 +
 +/**
 + * TXSynchronizationOp sends JTA beforeCompletion and afterCompletion
 + * messages to the server pool.
 + * 
 + * @author bruce
 + *
 + */
 +public class TXSynchronizationOp {
 +
 +  public static enum CompletionType {
 +    BEFORE_COMPLETION, AFTER_COMPLETION
 +  }
 +
 +  /**
 +   * @param pool
 +   * @param status - the status of an afterCompletion notification
 +   * @param txId - the transaction identifier
 +   * @param type - BEFORE_COMPLETION or AFTER_COMPLETION
 +   * @return the server's commit message
 +   */
 +  public static TXCommitMessage execute(InternalPool pool, int status, int txId, CompletionType type) {
 +    Impl impl = new Impl(status, txId, type);
 +    pool.execute(impl);
 +    return impl.tXCommitMessageResponse;
 +  }
 +  
 +  static class Impl extends AbstractOp {
 +
 +    private int status;
 +    private CompletionType type;
 +    TXCommitMessage tXCommitMessageResponse;
 +
 +    /**
 +     * @param status
 +     * @param type
 +     */
 +    public Impl(int status, int txId, CompletionType type) {
 +      super(MessageType.TX_SYNCHRONIZATION, (type==CompletionType.AFTER_COMPLETION)? 3 : 2);
 +      this.status = status;
 +      this.type = type;
 +      getMessage().addIntPart(type.ordinal());
 +      getMessage().addIntPart(txId);
 +      if (type == CompletionType.AFTER_COMPLETION) {
 +        getMessage().addIntPart(status);
 +      }
 +    }
 +    
 +    @Override
 +    public String toString() {
 +      return "TXSynchronization(threadTxId=" + TXManagerImpl.getCurrentTXUniqueId()
 +      +"; "+this.type + "; status=" + this.status + ")";
 +    }
 +
 +    @Override
 +  protected void processAck(Message msg, String opName)
 +    throws Exception
 +  {
 +    final int msgType = msg.getMessageType();
 +    if (msgType == MessageType.REPLY) {
 +      return;
 +    } else {
 +      Part part = msg.getPart(0);
 +      if (msgType == MessageType.EXCEPTION) {
 +        Throwable t = (Throwable) part.getObject();
 +        if (t instanceof CommitConflictException ||
 +            t instanceof SynchronizationCommitConflictException) {
 +          throw (GemFireException)t;
 +        }
 +      }
 +      super.processAck(msg, opName);
 +    }
 +  }
 +
 +    
 +    /* (non-Javadoc)
 +     * @see com.gemstone.gemfire.cache.client.internal.AbstractOp#processResponse(com.gemstone.gemfire.internal.cache.tier.sockets.Message)
 +     */
 +    @Override
 +    protected Object processResponse(Message msg) throws Exception {
 +      if (this.type == CompletionType.BEFORE_COMPLETION) {
 +        try {
 +          processAck(msg, type.toString());
 +        } catch (ServerOperationException e) {
 +          if (e.getCause() instanceof SynchronizationCommitConflictException) {
 +            throw (SynchronizationCommitConflictException)e.getCause();
 +          }
 +        }
 +        return null;
 +      } else {
 +        TXCommitMessage rcs = (TXCommitMessage)processObjResponse(msg, this.type.toString());
 +        this.tXCommitMessageResponse = rcs;
 +        return rcs;
 +      }
 +    }
 +
 +    /* (non-Javadoc)
 +     * @see com.gemstone.gemfire.cache.client.internal.AbstractOp#isErrorResponse(int)
 +     */
 +    @Override
 +    protected boolean isErrorResponse(int msgType) {
 +      return msgType == MessageType.REQUESTDATAERROR;
 +    }
 +
 +    @Override  
 +    protected long startAttempt(ConnectionStats stats) {
 +      return stats.startTxSynchronization();
 +    }
 +    @Override  
 +    protected void endSendAttempt(ConnectionStats stats, long start) {
 +      stats.endTxSynchronizationSend(start, hasFailed());
 +    }
 +    @Override  
 +    protected void endAttempt(ConnectionStats stats, long start) {
 +      stats.endTxSynchronization(start, hasTimedOut(), hasFailed());
 +    }
 +
 +    @Override
 +    protected void processSecureBytes(Connection cnx, Message message)
 +        throws Exception {
 +    }
 +
 +    @Override
 +    protected boolean needsUserId() {
 +      return false;
 +    }
 +
 +    @Override
 +    protected void sendMessage(Connection cnx) throws Exception {
-       getMessage().setEarlyAck((byte)(getMessage().getEarlyAckByte() & Message.MESSAGE_HAS_SECURE_PART));
++      getMessage().clearMessageHasSecurePartFlag();
 +      getMessage().send(false);
 +    }
 +  }
 +
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/hdfs/internal/hoplog/AbstractHoplog.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/hdfs/internal/hoplog/AbstractHoplog.java
index 636dd91,0000000..d2fdbe7
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/hdfs/internal/hoplog/AbstractHoplog.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/hdfs/internal/hoplog/AbstractHoplog.java
@@@ -1,357 -1,0 +1,357 @@@
 +/*
 + * 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.cache.hdfs.internal.hoplog;
 +
 +import java.io.IOException;
 +import java.util.regex.Matcher;
 +
++import com.gemstone.gemfire.internal.hll.ICardinality;
 +import org.apache.hadoop.conf.Configuration;
 +import org.apache.hadoop.fs.FileStatus;
 +import org.apache.hadoop.fs.FileSystem;
 +import org.apache.hadoop.fs.Path;
 +import org.apache.hadoop.io.BytesWritable;
 +import org.apache.hadoop.io.Text;
 +import org.apache.hadoop.io.compress.CompressionCodec;
 +import org.apache.hadoop.io.compress.GzipCodec;
 +import org.apache.hadoop.io.compress.Lz4Codec;
 +import org.apache.hadoop.io.compress.SnappyCodec;
 +
 +import com.gemstone.gemfire.cache.hdfs.HDFSIOException;
 +import com.gemstone.gemfire.cache.hdfs.internal.HDFSStoreImpl;
- import com.gemstone.gemfire.cache.hdfs.internal.cardinality.ICardinality;
 +import com.gemstone.gemfire.cache.hdfs.internal.org.apache.hadoop.io.SequenceFile;
 +import com.gemstone.gemfire.cache.hdfs.internal.org.apache.hadoop.io.SequenceFile.CompressionType;
 +import com.gemstone.gemfire.cache.hdfs.internal.org.apache.hadoop.io.SequenceFile.Writer.Option;
 +import com.gemstone.gemfire.internal.cache.persistence.soplog.SortedOplogStatistics;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.Version;
 +import com.gemstone.gemfire.internal.logging.LogService;
 +import com.gemstone.gemfire.internal.logging.log4j.LocalizedMessage;
 +import org.apache.hadoop.hbase.util.FSUtils;
 +
 +import org.apache.logging.log4j.Logger;
 +
 +/**
 + * Abstract class for {@link Hoplog} with common functionality
 + */
 +public abstract class AbstractHoplog implements Hoplog {
 +  protected final FSProvider fsProvider;
 +  
 +  // path of the oplog file
 +  protected volatile Path path;
 +  private volatile HoplogDescriptor hfd;
 +  protected Configuration conf;
 +  protected SortedOplogStatistics stats;
 +  protected Long hoplogModificationTime;
 +  protected Long hoplogSize;
 +
 +  protected HoplogReaderActivityListener readerListener;
 +  
 +  // logger instance
 +  protected static final Logger logger = LogService.getLogger();
 +  
 +  protected static String logPrefix;
 +  // THIS CONSTRUCTOR SHOULD BE USED FOR LONER ONLY
 +  AbstractHoplog(FileSystem inputFS, Path filePath, SortedOplogStatistics stats)
 +      throws IOException {
 +    logPrefix = "<" + filePath.getName() + "> ";
 +    this.fsProvider = new FSProvider(inputFS);
 +    initialize(filePath, stats, inputFS);
 +  }
 +
 +  public AbstractHoplog(HDFSStoreImpl store, Path filePath,
 +      SortedOplogStatistics stats) throws IOException {
 +    logPrefix = "<" + filePath.getName() + "> ";
 +    this.fsProvider = new FSProvider(store);
 +    initialize(filePath, stats, store.getFileSystem());
 +  }
 +
 +  private void initialize(Path path, SortedOplogStatistics stats, FileSystem fs) {
 +    this.conf = fs.getConf();
 +    this.stats = stats;
 +    this.path = fs.makeQualified(path);
 +    this.hfd = new HoplogDescriptor(this.path.getName());
 +  }
 +  
 +  @Override
 +  public abstract void close() throws IOException; 
 +  @Override
 +  public abstract HoplogReader getReader() throws IOException;
 +
 +  @Override
 +  public abstract HoplogWriter createWriter(int keys) throws IOException;
 +
 +  @Override
 +  abstract public void close(boolean clearCache) throws IOException;
 +
 +  @Override
 +  public void setReaderActivityListener(HoplogReaderActivityListener listener) {
 +    this.readerListener = listener;
 +  }
 +  
 +  @Override
 +  public String getFileName() {
 +    return this.hfd.getFileName();
 +  }
 +  
 +  public final int compareTo(Hoplog o) {
 +    return hfd.compareTo( ((AbstractHoplog)o).hfd);
 +  }
 +
 +  @Override
 +  public ICardinality getEntryCountEstimate() throws IOException {
 +    return null;
 +  }
 +  
 +  @Override
 +  public synchronized void rename(String name) throws IOException {
 +    if (logger.isDebugEnabled())
 +      logger.debug("{}Renaming hoplog to " + name, logPrefix);
 +    Path parent = path.getParent();
 +    Path newPath = new Path(parent, name);
 +    fsProvider.getFS().rename(path, new Path(parent, newPath));
 +
 +    // close the old reader and let the new one get created lazily
 +    close();
 +    
 +    // update path to point to the new path
 +    path = newPath;
 +    this.hfd = new HoplogDescriptor(this.path.getName());
 +    logPrefix = "<" + path.getName() + "> ";
 +  }
 +  
 +  @Override
 +  public synchronized void delete() throws IOException {
 +    if (logger.isDebugEnabled())
 +      logger.debug("{}Deleting hoplog", logPrefix);
 +    close();
 +    this.hoplogModificationTime = null;
 +    this.hoplogSize = null;
 +    fsProvider.getFS().delete(path, false);
 +  }
 +
 +  @Override
 +  public long getModificationTimeStamp() {
 +    initHoplogSizeTimeInfo();
 +
 +    // modification time will not be null if this hoplog is existing. Otherwise
 +    // invocation of this method should is invalid
 +    if (hoplogModificationTime == null) {
 +      throw new IllegalStateException();
 +    }
 +    
 +    return hoplogModificationTime;
 +  }
 +
 +  @Override
 +  public long getSize() {
 +    initHoplogSizeTimeInfo();
 +    
 +    // size will not be null if this hoplog is existing. Otherwise
 +    // invocation of this method should is invalid
 +    if (hoplogSize == null) {
 +      throw new IllegalStateException();
 +    }
 +    
 +    return hoplogSize;
 +  }
 +  
 +  private synchronized void initHoplogSizeTimeInfo() {
 +    if (hoplogSize != null && hoplogModificationTime != null) {
 +      // time and size info is already initialized. no work needed here
 +      return;
 +    }
 +
 +    try {
 +      FileStatus[] filesInfo = FSUtils.listStatus(fsProvider.getFS(), path, null);
 +      if (filesInfo != null && filesInfo.length == 1) {
 +        this.hoplogModificationTime = filesInfo[0].getModificationTime();
 +        this.hoplogSize = filesInfo[0].getLen();
 +      }
 +      // TODO else condition may happen if user deletes hoplog from the file system.
 +    } catch (IOException e) {
 +      logger.error(LocalizedMessage.create(LocalizedStrings.HOPLOG_FAILED_TO_READ_HDFS_FILE, path), e);
 +      throw new HDFSIOException(
 +          LocalizedStrings.HOPLOG_FAILED_TO_READ_HDFS_FILE.toLocalizedString(path),e);
 +    }
 +  }
 +  public static SequenceFile.Writer getSequenceFileWriter(Path path, 
 +      Configuration conf, Logger logger) throws IOException {
 +    return getSequenceFileWriter(path,conf, logger, null); 
 +  }
 +  
 +  /**
 +   * 
 +   * @param path
 +   * @param conf
 +   * @param logger
 +   * @param version - is being used only for testing. Should be passed as null for other purposes. 
 +   * @return SequenceFile.Writer 
 +   * @throws IOException
 +   */
 +  public static SequenceFile.Writer getSequenceFileWriter(Path path, 
 +    Configuration conf, Logger logger, Version version) throws IOException {
 +    Option optPath = SequenceFile.Writer.file(path);
 +    Option optKey = SequenceFile.Writer.keyClass(BytesWritable.class);
 +    Option optVal = SequenceFile.Writer.valueClass(BytesWritable.class);
 +    Option optCom = withCompression(logger);
 +    if (logger.isDebugEnabled())
 +      logger.debug("{}Started creating hoplog " + path, logPrefix);
 +    
 +    if (version == null)
 +      version = Version.CURRENT;
 +    //Create a metadata option with the gemfire version, for future versioning
 +    //of the key and value format
 +    SequenceFile.Metadata metadata = new SequenceFile.Metadata();
 +    metadata.set(new Text(Meta.GEMFIRE_VERSION.name()), new Text(String.valueOf(version.ordinal())));
 +    Option optMeta = SequenceFile.Writer.metadata(metadata);
 +    
 +    SequenceFile.Writer writer = SequenceFile.createWriter(conf, optPath, optKey, optVal, optCom, optMeta);
 +    
 +    return writer;
 +  }
 +  
 +  private static Option withCompression(Logger logger) {
 +    String prop = System.getProperty(HoplogConfig.COMPRESSION);
 +    if (prop != null) {
 +      CompressionCodec codec;
 +      if (prop.equalsIgnoreCase("SNAPPY")) {
 +        codec = new SnappyCodec();
 +      } else if (prop.equalsIgnoreCase("LZ4")) {
 +        codec = new Lz4Codec();
 +      } else if (prop.equals("GZ")) {
 +        codec = new GzipCodec();
 +      } else {
 +        throw new IllegalStateException("Unsupported codec: " + prop);
 +      }
 +      if (logger.isDebugEnabled())
 +        logger.debug("{}Using compression codec " + codec, logPrefix);
 +      return SequenceFile.Writer.compression(CompressionType.BLOCK, codec);
 +    }
 +    return SequenceFile.Writer.compression(CompressionType.NONE, null);
 +  }
 +  
 +  public static final class HoplogDescriptor implements Comparable<HoplogDescriptor> {
 +     private final String fileName;
 +     private final String bucket;
 +     private final int sequence;
 +     private final long timestamp;
 +     private final String extension;
 +     
 +     HoplogDescriptor(final String fileName) {
 +       this.fileName = fileName;
 +       final Matcher matcher = AbstractHoplogOrganizer.HOPLOG_NAME_PATTERN.matcher(fileName);
 +       final boolean matched = matcher.find();
 +       assert matched;
 +       this.bucket = matcher.group(1);
 +       this.sequence = Integer.valueOf(matcher.group(3));
 +       this.timestamp = Long.valueOf(matcher.group(2)); 
 +       this.extension = matcher.group(4);
 +     }
 +     
 +     public final String getFileName() {
 +       return fileName;
 +     }
 +     
 +     @Override
 +     public boolean equals(Object o) {
 +       if (this == o) {
 +         return true;
 +       }
 +       
 +       if (!(o instanceof HoplogDescriptor)) {
 +         return false;
 +       }
 +       
 +       final HoplogDescriptor other = (HoplogDescriptor)o;
 +       // the two files should belong to same bucket
 +       assert this.bucket.equals(other.bucket);
 +       
 +       // compare sequence first
 +       if (this.sequence != other.sequence) {
 +         return false;
 +       }
 +       
 +       // sequence is same, compare timestamps
 +       if (this.timestamp != other.timestamp) {
 +         return false;
 +       }
 +       
 +       return extension.equals(other.extension);
 +     }
 +
 +    @Override
 +    public int compareTo(HoplogDescriptor o) {
 +      if (this == o) {
 +        return 0;
 +      }
 +      
 +      // the two files should belong to same bucket
 +      assert this.bucket.equals(o.bucket);
 +      
 +      // compare sequence first
 +      if (sequence > o.sequence) {
 +        return -1;
 +      } else if (sequence < o.sequence) {
 +        return 1;
 +      }
 +      
 +      // sequence is same, compare timestamps
 +      if(timestamp > o.timestamp) {
 +        return -1; 
 +      } else if (timestamp < o.timestamp) {
 +        return 1;
 +      }
 +      
 +      //timestamp is the same, compare the file extension. It's
 +      //possible a major compaction and minor compaction could finish
 +      //at the same time and create the same timestamp and sequence number
 +      //it doesn't matter which file we look at first in that case.
 +      return extension.compareTo(o.extension);
 +    }
 +     
 +     
 +  }
 +  
 +  protected static final class FSProvider {
 +    final FileSystem fs;
 +    final HDFSStoreImpl store;
 +    
 +    // THIS METHOD IS FOR TESTING ONLY
 +    FSProvider(FileSystem fs) {
 +      this.fs = fs;
 +      this.store = null;
 +    }
 +    
 +    FSProvider(HDFSStoreImpl store) {
 +      this.store = store;
 +      fs = null;
 +    }
 +    
 +    public FileSystem getFS() throws IOException {
 +      if (store != null) {
 +        return store.getFileSystem();
 +      }
 +      return fs;
 +    }
 +
 +    public FileSystem checkFileSystem() {
 +      store.checkAndClearFileSystem();
 +      return store.getCachedFileSystem();
 +    }
 +  }
 +}


[08/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/DistAckMapMethodsDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/DistAckMapMethodsDUnitTest.java
index 44f7f06,0000000..cf6226d
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/DistAckMapMethodsDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/DistAckMapMethodsDUnitTest.java
@@@ -1,707 -1,0 +1,705 @@@
 +/*
 + * 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.
 + */
 +
 +/*
 + * DistAckMapMethodsDUnitTest.java
 + *
 + * Created on August 4, 2005, 12:36 PM
 + */
 +package com.gemstone.gemfire.cache30;
 +
 +import java.util.HashSet;
 +import java.util.Properties;
 +import java.util.Set;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheFactory;
 +import com.gemstone.gemfire.cache.CacheListener;
 +import com.gemstone.gemfire.cache.CacheWriter;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.EntryEvent;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionAttributes;
 +import com.gemstone.gemfire.cache.RegionDestroyedException;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.util.CacheListenerAdapter;
 +import com.gemstone.gemfire.cache.util.CacheWriterAdapter;
 +import com.gemstone.gemfire.distributed.DistributedSystem;
 +import com.gemstone.gemfire.test.dunit.DistributedTestCase;
 +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;
 +
 +/**
 + *
 + * @author  prafulla
 + */
 +public class DistAckMapMethodsDUnitTest extends DistributedTestCase{
 +    static Cache cache;
 +    static Properties props = new Properties();
 +    static DistributedSystem ds = null;
 +    static Region region;
 +    static Region mirroredRegion;
 +    static Region remRegion;
 +    static boolean afterDestroy=false;
 +    
 +    
 +    //helper class referece objects
 +    static Object afterDestroyObj;
 +    
 +    /** Creates a new instance of DistAckMapMethodsDUnitTest */
 +    public DistAckMapMethodsDUnitTest(String name) {
 +        super(name);
 +    }
 +    
 +    @Override
 +    public void setUp() throws Exception {
 +      super.setUp();
 +      Host host = Host.getHost(0);
 +      VM vm0 = host.getVM(0);
 +      VM vm1 = host.getVM(1);
-       vm0.invoke(DistAckMapMethodsDUnitTest.class, "createCache");
-       vm1.invoke(DistAckMapMethodsDUnitTest.class, "createCache");
++      vm0.invoke(() -> DistAckMapMethodsDUnitTest.createCache());
++      vm1.invoke(() -> DistAckMapMethodsDUnitTest.createCache());
 +    }
 +    
 +    @Override
 +    protected final void preTearDown() throws Exception {
 +      Host host = Host.getHost(0);
 +      VM vm0 = host.getVM(0);
 +      VM vm1 = host.getVM(1);
-       vm0.invoke(DistAckMapMethodsDUnitTest.class, "closeCache");
-       vm1.invoke(DistAckMapMethodsDUnitTest.class, "closeCache");
++      vm0.invoke(() -> DistAckMapMethodsDUnitTest.closeCache());
++      vm1.invoke(() -> DistAckMapMethodsDUnitTest.closeCache());
 +      cache = null;
 +      Invoke.invokeInEveryVM(new SerializableRunnable() { public void run() { cache = null; } });
 +    }
 +    
 +    public static void createCache(){
 +        try{
 +            //props.setProperty("mcast-port", "1234");
 +            //ds = DistributedSystem.connect(props);
 +            ds = (new DistAckMapMethodsDUnitTest("temp")).getSystem(props);
 +            cache = CacheFactory.create(ds);
 +            AttributesFactory factory  = new AttributesFactory();
 +            factory.setScope(Scope.DISTRIBUTED_ACK);
 +            RegionAttributes attr = factory.create();
 +            region = cache.createRegion("map", attr);
 +        } catch (Exception ex){
 +            ex.printStackTrace();
 +        }
 +    }
 +    
 +    public static void closeCache(){
 +        try{
 +            cache.close();
 +            ds.disconnect();
 +        } catch (Exception ex){
 +            ex.printStackTrace();
 +        }
 +        
 +    }
 +    
 +    public static void createMirroredRegion(){
 +        try{
 +            AttributesFactory factory1  = new AttributesFactory();
 +            factory1.setScope(Scope.DISTRIBUTED_ACK);
 +            factory1.setDataPolicy(DataPolicy.REPLICATE);
 +            RegionAttributes attr1 = factory1.create();
 +            mirroredRegion = cache.createRegion("mirrored", attr1);
 +            
 +        } catch (Exception ex){
 +            ex.printStackTrace();
 +        }
 +    }
 +    
 +    public static void createRegionToTestRemove(){
 +        try{
 +            AttributesFactory factory2  = new AttributesFactory();
 +            factory2.setScope(Scope.DISTRIBUTED_ACK);
 +            CacheWriter cacheWriter = new RemoveCacheWriter();
 +            CacheListener cacheListener = new RemoveCacheListener();
 +            factory2.setCacheWriter(cacheWriter);
 +            factory2.setCacheListener(cacheListener);
 +            RegionAttributes attr2 = factory2.create();
 +            remRegion = cache.createRegion("remove", attr2);
 +            
 +        } catch (Exception ex){
 +            ex.printStackTrace();
 +        }
 +    }
 +    
 +    //testMethods
 +    
 +    public void testPutMethod(){
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +        VM vm1 = host.getVM(1);
 +        Object obj1;
 +        //put from one and get from other
 +        int i=1;
 +        Object[] objArr = new Object[1];
 +        objArr[0] = ""+i;
 +        //Integer in = new Integer(i);
 +        //objArr[0] = (Object) in;
 +        vm0.invoke(DistAckMapMethodsDUnitTest.class, "putMethod", objArr);
 +        obj1 = vm1.invoke(DistAckMapMethodsDUnitTest.class, "getMethod", objArr);
 +        if(obj1 == null ){
 +            fail("region.put(key, value) from one vm does not match with region.get(key) from other vm");
 +        }
 +        
 +        //put from both vms for same key
 +        i = 2;
 +        objArr[0] = ""+i;
 +        //in = new Integer(i);
 +        //objArr[0] = (Object) in;
 +        vm0.invoke(DistAckMapMethodsDUnitTest.class, "putMethod", objArr);
 +        obj1 = vm1.invoke(DistAckMapMethodsDUnitTest.class, "putMethod", objArr);
 +        if(obj1 != null){//here if some dummy object is returned on first time put then that should be checked
 +            fail("failed while region.put from both vms for same key");
 +        }
 +    }
 +    
 +    public void testRemoveMethod(){
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +        VM vm1 = host.getVM(1);
 +        Object obj1, obj2;
 +        boolean ret;
 +        //put from one and get from other
 +        int i=1;
 +        Object objArr[] = new Object[1];
 +        objArr[0] = ""+i;
 +        //Integer in = new Integer(i);
 +        //objArr[0] = (Object) in;
 +        vm0.invoke(DistAckMapMethodsDUnitTest.class, "putMethod", objArr);
 +        vm0.invoke(DistAckMapMethodsDUnitTest.class, "removeMethod", objArr);
 +        //validate if vm0 has that key value entry
-         ret = vm0.invokeBoolean(DistAckMapMethodsDUnitTest.class, "containsKeyMethod", objArr);
++        ret = vm0.invoke(() -> containsKeyMethod("" + i));
 +        if( ret ){//if returned true means that the key is still there
 +            fail("region.remove failed with distributed ack scope");
 +        }
 +        
 +        //test if the correct value is returned
 +        vm0.invoke(DistAckMapMethodsDUnitTest.class, "putMethod", objArr);
 +        obj1 = vm1.invoke(DistAckMapMethodsDUnitTest.class, "getMethod", objArr);//to make sure that vm1 region has the entry
 +        obj2 = vm1.invoke(DistAckMapMethodsDUnitTest.class, "removeMethod", objArr);
 +        LogWriterUtils.getLogWriter().fine("111111111"+obj1);
 +        LogWriterUtils.getLogWriter().fine("2222222222"+obj2);
 +        if (obj1 == null)
 +          fail("region1.getMethod returned null");
 +        if(!(obj1.equals(obj2))){
 +            fail("region.remove failed with distributed ack scope");
 +        }
 +    }
 +    
 +    public void testRemoveMethodDetails(){
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +        VM vm1 = host.getVM(1);
 +        
-         vm0.invoke(DistAckMapMethodsDUnitTest.class, "createRegionToTestRemove");
-         vm1.invoke(DistAckMapMethodsDUnitTest.class, "createRegionToTestRemove");
++        vm0.invoke(() -> DistAckMapMethodsDUnitTest.createRegionToTestRemove());
++        vm1.invoke(() -> DistAckMapMethodsDUnitTest.createRegionToTestRemove());
 +        
-         vm0.invoke(DistAckMapMethodsDUnitTest.class, "removeMethodDetails");
++        vm0.invoke(() -> DistAckMapMethodsDUnitTest.removeMethodDetails());
 +        vm1.invoke(new CacheSerializableRunnable("testRemoveMethodDetails"){
 +            public void run2() throws CacheException {
 +                Object ob1 = remRegion.get(new Integer(1));
 +                assertEquals("beforeDestroy", ob1.toString());
 +                //wait till listeber switches afterDestroy to true
 +                //                    while(!afterDestroy){
 +                //                        //wait
 +                //                    }
 +                assertEquals("afterDestroy", remRegion.get(new Integer(3)).toString());
 +            }
 +        }
 +        );
 +    }//end of testRemoveMethodDetails
 +    
 +    public void testIsEmptyMethod(){
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +        VM vm1 = host.getVM(1);
 +//        boolean ret;
 +        //put from one and get from other
 +        int i=1;
 +        Object objArr[] = new Object[1];
 +        objArr[0] = ""+i;
 +        //Integer in = new Integer(i);
 +        //objArr[0] = (Object) in;
 +        vm0.invoke(DistAckMapMethodsDUnitTest.class, "putMethod", objArr);
-         boolean val = vm1.invokeBoolean(DistAckMapMethodsDUnitTest.class, "isEmptyMethod");
++        boolean val = vm1.invoke(() -> DistAckMapMethodsDUnitTest.isEmptyMethod());
 +        if (!val){//val should be true
 +            fail("Failed in region.isEmpty");
 +        }
 +        
 +        vm1.invoke(DistAckMapMethodsDUnitTest.class, "getMethod", objArr);
-         boolean val1 = vm1.invokeBoolean(DistAckMapMethodsDUnitTest.class, "isEmptyMethod");
++        boolean val1 = vm1.invoke(() -> DistAckMapMethodsDUnitTest.isEmptyMethod());
 +        if (val1){
 +            fail("Failed in region.isEmpty");
 +        }
 +    }
 +    
 +    public void testContainsValueMethod(){
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +        VM vm1 = host.getVM(1);
 +//        boolean ret;
 +        //put from one and get from other
 +        int i=1;
 +        Object objArr[] = new Object[1];
 +        objArr[0] = ""+i;
 +        //Integer in = new Integer(i);
 +        //objArr[0] = (Object) in;
 +        vm0.invoke(DistAckMapMethodsDUnitTest.class, "putMethod", objArr);
-         Object ob[] = new Object[1];
-         ob[0] = "first";
-         boolean val = vm1.invokeBoolean(DistAckMapMethodsDUnitTest.class, "containsValueMethod", ob);
++        boolean val = vm1.invoke(() -> containsValueMethod("first"));
 +        if (val){//val should be false.
 +            fail("Failed in region.ContainsValue");
 +        }
 +        
 +        vm1.invoke(DistAckMapMethodsDUnitTest.class, "getMethod", objArr);
-         boolean val1 = vm1.invokeBoolean(DistAckMapMethodsDUnitTest.class, "containsValueMethod", ob);
++        boolean val1 = vm1.invoke(() -> containsValueMethod("first"));
 +        if (!val1){//val1 should be true.
 +            fail("Failed in region.ContainsValue");
 +        }
 +    }
 +    
 +    public void testKeySetMethod(){
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +        VM vm1 = host.getVM(1);
 +        
 +        int i=1;
 +        Object objArr[] = new Object[1];
 +        objArr[0] = ""+i;
 +        //Integer in = new Integer(i);
 +        //objArr[0] = (Object) in;
 +        vm0.invoke(DistAckMapMethodsDUnitTest.class, "putMethod", objArr);
-         int temp = vm1.invokeInt(DistAckMapMethodsDUnitTest.class, "keySetMethod");
++        int temp = vm1.invoke(() -> DistAckMapMethodsDUnitTest.keySetMethod());
 +        if (temp != 0){
 +            fail("failed in keySetMethodtest method");
 +        }
 +        
 +        vm1.invoke(DistAckMapMethodsDUnitTest.class, "getMethod", objArr);//to make sure that vm1 region has the entry
-         temp = vm1.invokeInt(DistAckMapMethodsDUnitTest.class, "keySetMethod");
++        temp = vm1.invoke(() -> DistAckMapMethodsDUnitTest.keySetMethod());
 +        if (temp == 0){
 +            fail("failed in keySetMethodtest method");
 +        }
 +        //in the above scenarion we can test this for mirrorred region scenarion as well
 +        temp=0;
-         vm0.invoke(DistAckMapMethodsDUnitTest.class, "createMirroredRegion");
-         vm1.invoke(DistAckMapMethodsDUnitTest.class, "createMirroredRegion");
++        vm0.invoke(() -> DistAckMapMethodsDUnitTest.createMirroredRegion());
++        vm1.invoke(() -> DistAckMapMethodsDUnitTest.createMirroredRegion());
 +        vm0.invoke(DistAckMapMethodsDUnitTest.class, "putMethod", objArr);
-         temp = vm1.invokeInt(DistAckMapMethodsDUnitTest.class, "keySetMethod");
++        temp = vm1.invoke(() -> DistAckMapMethodsDUnitTest.keySetMethod());
 +        if (temp == 0){
 +            fail("failed in keySetMethodtest method");
 +        }
 +    }
 +    
 +    
 +    public void testEntrySetMethod(){
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +        VM vm1 = host.getVM(1);
 +        
 +        int i=1;
 +        Object objArr[] = new Object[1];
 +        objArr[0] = ""+i;
 +        //Integer in = new Integer(i);
 +        //objArr[0] = (Object) in;
 +        vm0.invoke(DistAckMapMethodsDUnitTest.class, "putMethod", objArr);
-         int temp = vm1.invokeInt(DistAckMapMethodsDUnitTest.class, "entrySetMethod");
++        int temp = vm1.invoke(() -> DistAckMapMethodsDUnitTest.entrySetMethod());
 +        if (temp != 0){
 +            fail("failed in entrySetMethodtest method");
 +        }
 +        
 +        vm1.invoke(DistAckMapMethodsDUnitTest.class, "getMethod", objArr);//to make sure that vm1 region has the entry
-         temp = vm1.invokeInt(DistAckMapMethodsDUnitTest.class, "entrySetMethod");
++        temp = vm1.invoke(() -> DistAckMapMethodsDUnitTest.entrySetMethod());
 +        if (temp == 0){
 +            fail("failed in entrySetMethodtest method");
 +        }
 +        //in the above scenarion we can test this for mirrorred region scenarion as well
 +        temp=0;
-         vm0.invoke(DistAckMapMethodsDUnitTest.class, "createMirroredRegion");
-         vm1.invoke(DistAckMapMethodsDUnitTest.class, "createMirroredRegion");
++        vm0.invoke(() -> DistAckMapMethodsDUnitTest.createMirroredRegion());
++        vm1.invoke(() -> DistAckMapMethodsDUnitTest.createMirroredRegion());
 +        vm0.invoke(DistAckMapMethodsDUnitTest.class, "putOnMirroredRegion", objArr);
-         temp = vm1.invokeInt(DistAckMapMethodsDUnitTest.class, "entrySetMethod");
++        temp = vm1.invoke(() -> DistAckMapMethodsDUnitTest.entrySetMethod());
 +        if (temp == 0){
 +            fail("failed in entrySetMethodtest method");
 +        }
 +    }
 +    
 +    public void testSizeMethod(){
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +        VM vm1 = host.getVM(1);
 +        
 +        int i=1, j=0;
 +        Object objArr[] = new Object[1];
 +        objArr[0] = ""+i;
 +        //Integer in = new Integer(i);
 +        //objArr[0] = (Object) in;
 +        vm0.invoke(DistAckMapMethodsDUnitTest.class, "putMethod", objArr);
-         j = vm1.invokeInt(DistAckMapMethodsDUnitTest.class, "sizeMethod");
++        j = vm1.invoke(() -> DistAckMapMethodsDUnitTest.sizeMethod());
 +        if( j != 0){
 +            fail("failed in region.size method");
 +        }
 +        
 +        vm1.invoke(DistAckMapMethodsDUnitTest.class, "getMethod", objArr);//to make sure that vm1 region has the entry
-         j = vm1.invokeInt(DistAckMapMethodsDUnitTest.class, "sizeMethod");
++        j = vm1.invoke(() -> DistAckMapMethodsDUnitTest.sizeMethod());
 +        if( j == 0){
 +            fail("failed in region.size method");
 +        }
 +    }
 +    
 +    public void testallMethodsArgs(){
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
-         vm0.invoke(DistAckMapMethodsDUnitTest.class, "allMethodsArgs");
++        vm0.invoke(() -> DistAckMapMethodsDUnitTest.allMethodsArgs());
 +    }
 +    
 +    
 +    //following is the implementation of the methods of Map to use in dunit test cases.
 +    /*
 +     *
 +     *
 +     */
 +    
 +    public static Object putMethod(Object ob){
 +        Object obj=null;
 +        try{
 +            if(ob != null){
 +                String str = "first";
 +                obj = region.put(ob, str);
 +            }
 +        }catch(Exception ex){
 +            fail("Failed while region.put");
 +        }
 +        return obj;
 +    }
 +    
 +    public static Object getMethod(Object ob){
 +        Object obj=null;
 +        try{
 +            obj = region.get(ob);
 +            
 +        } catch(Exception ex){
 +            fail("Failed while region.get");
 +        }
 +        return obj;
 +    }
 +    
 +    public static Object removeMethod(Object ob){
 +        Object obj=null;
 +        try{
 +            obj = region.remove(ob);
 +        }catch(Exception ex){
 +            ex.printStackTrace();
 +            fail("Failed while region.remove");
 +        }
 +        return obj;
 +    }
 +    
 +    public static boolean containsKeyMethod(Object ob){
 +        boolean flag = false;
 +        try{
 +            flag = region.containsKey(ob);
 +        }catch(Exception ex){
 +            fail("Failed while region.containsKey");
 +        }
 +        return flag;
 +    }
 +    
 +    public static boolean isEmptyMethod(){
 +        boolean flag = false;
 +        try{
 +            flag = region.isEmpty();
 +        }catch(Exception ex){
 +            fail("Failed while region.isEmpty");
 +        }
 +        return flag;
 +    }
 +    
 +    public static boolean containsValueMethod(Object ob){
 +        boolean flag = false;
 +        try{
 +            flag = region.containsValue(ob);
 +        }catch(Exception ex){
 +            fail("Failed while region.containsValueMethod");
 +        }
 +        return flag;
 +    }
 +    
 +    public static int keySetMethod(){
 +        Set set = new HashSet();
 +        int i=0;
 +        try{
 +            set = region.keySet();
 +            i = set.size();
 +        }catch(Exception ex){
 +            ex.printStackTrace();
 +            fail("Failed while region.keySet");
 +        }
 +        return i;
 +    }
 +    
 +    public static int entrySetMethod(){
 +        Set set = new HashSet();
 +        int i=0;
 +        try{
 +            set = region.entrySet();
 +            i = set.size();
 +        }catch(Exception ex){
 +            ex.printStackTrace();
 +            fail("Failed while region.entrySet");
 +        }
 +        return i;
 +    }
 +    
 +    public static int sizeMethod(){
 +        int i=0;
 +        try{
 +            i = region.size();
 +        }catch(Exception ex){
 +            fail("Failed while region.size");
 +        }
 +        return i;
 +    }
 +    
 +    //following are methods for put on and get from mirrored regions
 +    
 +    public static Object putOnMirroredRegion(Object ob){
 +        Object obj=null;
 +        try{
 +            String str = "mirror";
 +            obj = mirroredRegion.put(ob, str);
 +        }catch(Exception ex){
 +            ex.printStackTrace();
 +            fail("Failed while mirroredRegion.put");
 +        }
 +        return obj;
 +    }
 +    
 +    public static Object getFromMirroredRegion(Object ob){
 +        Object obj=null;
 +        try{
 +            obj = mirroredRegion.get(ob);
 +            
 +        } catch(Exception ex){
 +            fail("Failed while mirroredRegion.get");
 +        }
 +        return obj;
 +    }
 +    
 +    public static void removeMethodDetails(){
 +        Object ob1;
 +//        Object ob2;
 +        Integer inOb1 = new Integer(1);
 +        try{
 +            region.put(inOb1, "first");
 +            ob1 = region.remove(inOb1);
 +            assertEquals("first", ob1.toString());
 +        }catch(Exception ex){
 +            ex.printStackTrace();
 +        }
 +        
 +        //to test EntryNotFoundException
 +        try{
 +            region.remove(new Integer(2));
 +            //fail("Should have thrown EntryNotFoundException");
 +        }//catch (EntryNotFoundException e){
 +        catch (Exception e){
 +            //pass
 +            //e.printStackTrace();
 +        }
 +        
 +        //to test NullPointerException
 +        try{
 +            Integer inOb2 = new Integer(2);
 +            region.put(inOb2, "second");
 +            inOb2 = null;
 +            region.remove(inOb2);
 +            fail("Should have thrown NullPointerException ");
 +        }catch (NullPointerException  e){
 +            //pass
 +        }
 +        
 +        //to test the cache writers and listeners
 +        try {
 +            //createRegionToTestRemove();
 +            Integer inOb2 = new Integer(2);
 +            remRegion.put(inOb2, "second");
 +            remRegion.remove(inOb2);
 +            
 +            //to test cacheWriter
 +            inOb2 = new Integer(1);
 +            assertEquals("beforeDestroy", remRegion.get(inOb2).toString());
 +            
 +            //wait till listeber switches afterDestroy to true
 +            while(!afterDestroy){
 +            }
 +            //to test cacheListener
 +            inOb2 = new Integer(3);
 +            assertEquals("afterDestroy", remRegion.get(inOb2).toString());
 +            
 +            //verify that entryEventvalue is correct for listener
 +            assertNotNull(afterDestroyObj);
 +            
 +        }catch (Exception ex){
 +            ex.printStackTrace();
 +        }
 +        
 +        
 +    }//end of removeMethodDetail
 +    
 +    public static void allMethodsArgs(){
 +        //testing args for put method
 +        try{
 +            region.put(new Integer(1), new String("first"));
 +            region.put(new Integer(2), new String("second"));
 +            region.put(new Integer(3), new String("third"));
 +            
 +            //test args for get method
 +            Object ob1 = region.get(new Integer(1));
 +            assertEquals("first", ob1.toString());
 +            
 +            //test args for containsKey method
 +            boolean val1 = region.containsKey(new Integer(2));
 +            assertEquals(true, val1);
 +            
 +            //test args for containsKey method
 +            boolean val2 = region.containsValue(new String("second"));
 +            //assertEquals(true, val2);
 +            
 +            //test args for remove method
 +            try{
 +                region.remove(new Integer(3));
 +            }//catch (EntryNotFoundException ex){
 +            catch (Exception ex){
 +                ex.printStackTrace();
 +                fail("failed while region.remove(new Object())");
 +            }
 +            
 +            //verifying the correct exceptions are thrown by the methods
 +            
 +            Object key=null, value=null;
 +            //testing put method
 +            try{
 +                region.put(key, value);
 +                fail("should have thrown NullPointerException");
 +            }catch(NullPointerException iex){
 +                //pass
 +            }
 +            
 +            //testing containsValue method
 +            try{
 +                region.containsValue(value);
 +                fail("should have thrown NullPointerException");
 +            }catch(NullPointerException iex){
 +                //pass
 +            }
 +            
 +            //RegionDestroyedException
 +            key = new Integer(5);
 +            value = new String("fifth");
 +            
 +            region.localDestroyRegion();
 +            //test put method
 +            try{
 +                region.put(key, value);
 +                fail("should have thrown RegionDestroyedException");
 +            }catch(RegionDestroyedException iex){
 +                //pass
 +            }
 +            
 +            //test remove method
 +            try{
 +                region.remove(key);
 +                fail("should have thrown RegionDestroyedException");
 +            }catch(RegionDestroyedException iex){
 +                //pass
 +            }
 +            
 +            //test containsValue method
 +            try{
 +                region.containsValue(value);
 +                fail("should have thrown RegionDestroyedException");
 +            }catch(RegionDestroyedException iex){
 +                //pass
 +            }
 +            
 +            //test size method
 +            try{
 +                region.size();
 +                fail("should have thrown RegionDestroyedException");
 +            }catch(RegionDestroyedException iex){
 +                //pass
 +            }
 +            
 +            //test keySet method
 +            try{
 +                region.keySet();
 +                fail("should have thrown RegionDestroyedException");
 +            }catch(RegionDestroyedException iex){
 +                //pass
 +            }
 +            
 +            //test entrySet method
 +            try{
 +                region.entrySet();
 +                fail("should have thrown RegionDestroyedException");
 +            }catch(RegionDestroyedException iex){
 +                //pass
 +            }
 +            
 +            
 +        } catch(Exception ex){
 +            ex.printStackTrace();
 +        }
 +        
 +    }//end of allMethodsArgs
 +    
 +    //helper classes
 +    
 +    static class RemoveCacheWriter extends CacheWriterAdapter{
 +        
 +        public void beforeDestroy(EntryEvent entryEvent) throws com.gemstone.gemfire.cache.CacheWriterException {
 +            Integer o1 = new Integer(1);
 +            remRegion.put(o1, "beforeDestroy");
 +        }
 +        
 +    }//end of RemoveCacheWriter
 +    
 +    
 +    static class RemoveCacheListener extends CacheListenerAdapter{
 +        
 +        public void afterDestroy(EntryEvent entryEvent) throws com.gemstone.gemfire.cache.CacheWriterException {
 +            Integer o1 = new Integer(3);
 +            remRegion.put(o1, "afterDestroy");
 +            
 +            afterDestroyObj = entryEvent.getKey();
 +            
 +            //to continue main thread where region.remove has actually occurred
 +            afterDestroy = true;
 +        }
 +        
 +    }//end of RemoveCacheListener
 +    
 +    
 +}//end of class

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/DistributedNoAckRegionCCEDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/DistributedNoAckRegionCCEDUnitTest.java
index a1cc2cd,0000000..f6836ae
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/DistributedNoAckRegionCCEDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/DistributedNoAckRegionCCEDUnitTest.java
@@@ -1,586 -1,0 +1,586 @@@
 +/*
 + * 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.cache30;
 +
 +import java.util.Map;
 +import java.util.Properties;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheListener;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.EntryEvent;
 +import com.gemstone.gemfire.cache.EntryNotFoundException;
 +import com.gemstone.gemfire.cache.RegionAttributes;
 +import com.gemstone.gemfire.cache.RegionFactory;
 +import com.gemstone.gemfire.cache.RegionShortcut;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.util.CacheListenerAdapter;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.internal.cache.LocalRegion;
 +import com.gemstone.gemfire.test.dunit.Assert;
 +import com.gemstone.gemfire.test.dunit.AsyncInvocation;
 +import com.gemstone.gemfire.test.dunit.Host;
 +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;
 +
 +public class DistributedNoAckRegionCCEDUnitTest extends
 +    DistributedNoAckRegionDUnitTest {
 +  
 +  static volatile boolean ListenerBlocking;
 +
 +  public DistributedNoAckRegionCCEDUnitTest(String name) {
 +    super(name);
 +  }
 +
 +  @Override
 +  public Properties getDistributedSystemProperties() {
 +    Properties p = super.getDistributedSystemProperties();
 +    p.put(DistributionConfig.CONSERVE_SOCKETS_NAME, "false");
 +    if (distributedSystemID > 0) {
 +      p.put(DistributionConfig.DISTRIBUTED_SYSTEM_ID_NAME, ""+distributedSystemID);
 +    }
 +    p.put(DistributionConfig.SOCKET_BUFFER_SIZE_NAME, ""+2000000);
 +    return p;
 +  }
 +
 +
 +  /**
 +   * Returns region attributes for a <code>GLOBAL</code> region
 +   */
 +  protected RegionAttributes getRegionAttributes() {
 +    AttributesFactory factory = new AttributesFactory();
 +    factory.setScope(Scope.DISTRIBUTED_NO_ACK);
 +    factory.setDataPolicy(DataPolicy.REPLICATE);
 +    factory.setConcurrencyChecksEnabled(true);
 +    return factory.create();
 +  }
 +  protected RegionAttributes getRegionAttributes(String type) {
 +    RegionAttributes ra = getCache().getRegionAttributes(type);
 +    if (ra == null) {
 +      throw new IllegalStateException("The region shortcut " + type
 +                                      + " has been removed.");
 +    }
 +    AttributesFactory factory = new AttributesFactory(ra);
 +    factory.setScope(Scope.DISTRIBUTED_NO_ACK);
 +    factory.setConcurrencyChecksEnabled(true);
 +    return factory.create();
 +  }
 +  
 +  @Override
 +  public void sendSerialMessageToAll() {
 +    try {
 +      com.gemstone.gemfire.distributed.internal.SerialAckedMessage msg = new com.gemstone.gemfire.distributed.internal.SerialAckedMessage();
 +      msg.send(InternalDistributedSystem.getConnectedInstance().getDM().getNormalDistributionManagerIds(), false);
 +    }
 +    catch (Exception e) {
 +      throw new RuntimeException("Unable to send serial message due to exception", e);
 +    }
 +  }
 +
 +
 +  @Override
 +  public void testLocalDestroy() throws InterruptedException {
 +    // replicates don't allow local destroy
 +  }
 +
 +  @Override
 +  public void testEntryTtlLocalDestroy() throws InterruptedException {
 +    // replicates don't allow local destroy
 +  }
 +
 +  public void testClearWithManyEventsInFlight() throws Exception {
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    VM vm3 = host.getVM(3);
 +    
 +    // create replicated regions in VM 0 and 1, then perform concurrent ops
 +    // on the same key while creating the region in VM2.  Afterward make
 +    // sure that all three regions are consistent
 +    
 +    final String name = this.getUniqueName() + "-CC";
 +    createRegionWithAttribute(vm0, name, false);
 +    createRegionWithAttribute(vm1, name, false);
 +    createRegionWithAttribute(vm2, name, false);
 +    createRegionWithAttribute(vm3, name, false);
-     vm0.invoke(DistributedNoAckRegionCCEDUnitTest.class, "addBlockingListener");
-     vm1.invoke(DistributedNoAckRegionCCEDUnitTest.class, "addBlockingListener");
-     vm2.invoke(DistributedNoAckRegionCCEDUnitTest.class, "addBlockingListener");
-     AsyncInvocation vm0Ops = vm0.invokeAsync(DistributedNoAckRegionCCEDUnitTest.class, "doManyOps");
-     AsyncInvocation vm1Ops = vm1.invokeAsync(DistributedNoAckRegionCCEDUnitTest.class, "doManyOps");
-     AsyncInvocation vm2Ops = vm2.invokeAsync(DistributedNoAckRegionCCEDUnitTest.class, "doManyOps");
++    vm0.invoke(() -> DistributedNoAckRegionCCEDUnitTest.addBlockingListener());
++    vm1.invoke(() -> DistributedNoAckRegionCCEDUnitTest.addBlockingListener());
++    vm2.invoke(() -> DistributedNoAckRegionCCEDUnitTest.addBlockingListener());
++    AsyncInvocation vm0Ops = vm0.invokeAsync(() -> DistributedNoAckRegionCCEDUnitTest.doManyOps());
++    AsyncInvocation vm1Ops = vm1.invokeAsync(() -> DistributedNoAckRegionCCEDUnitTest.doManyOps());
++    AsyncInvocation vm2Ops = vm2.invokeAsync(() -> DistributedNoAckRegionCCEDUnitTest.doManyOps());
 +    // pause to let a bunch of operations build up
 +    Wait.pause(5000);
-     AsyncInvocation a0 = vm3.invokeAsync(DistributedNoAckRegionCCEDUnitTest.class, "clearRegion");
-     vm0.invoke(DistributedNoAckRegionCCEDUnitTest.class, "unblockListener");
-     vm1.invoke(DistributedNoAckRegionCCEDUnitTest.class, "unblockListener");
-     vm2.invoke(DistributedNoAckRegionCCEDUnitTest.class, "unblockListener");
++    AsyncInvocation a0 = vm3.invokeAsync(() -> DistributedNoAckRegionCCEDUnitTest.clearRegion());
++    vm0.invoke(() -> DistributedNoAckRegionCCEDUnitTest.unblockListener());
++    vm1.invoke(() -> DistributedNoAckRegionCCEDUnitTest.unblockListener());
++    vm2.invoke(() -> DistributedNoAckRegionCCEDUnitTest.unblockListener());
 +    waitForAsyncProcessing(a0, "");
 +    waitForAsyncProcessing(vm0Ops, "");
 +    waitForAsyncProcessing(vm1Ops, "");
 +    waitForAsyncProcessing(vm2Ops, "");
 +
 +//    if (a0failed && a1failed) {
 +//      fail("neither member saw event conflation - check stats for " + name);
 +//    }
 +    Wait.pause(2000);//this test has with noack, thus we should wait before validating entries
 +    // check consistency of the regions
-     Map r0Contents = (Map)vm0.invoke(this.getClass(), "getCCRegionContents");
-     Map r1Contents = (Map)vm1.invoke(this.getClass(), "getCCRegionContents");
-     Map r2Contents = (Map)vm2.invoke(this.getClass(), "getCCRegionContents");
-     Map r3Contents = (Map)vm3.invoke(this.getClass(), "getCCRegionContents");
++    Map r0Contents = (Map)vm0.invoke(() -> this.getCCRegionContents());
++    Map r1Contents = (Map)vm1.invoke(() -> this.getCCRegionContents());
++    Map r2Contents = (Map)vm2.invoke(() -> this.getCCRegionContents());
++    Map r3Contents = (Map)vm3.invoke(() -> this.getCCRegionContents());
 +    
 +    for (int i=0; i<10; i++) {
 +      String key = "cckey" + i;
 +      assertEquals("region contents are not consistent", r0Contents.get(key), r1Contents.get(key));
 +      assertEquals("region contents are not consistent", r1Contents.get(key), r2Contents.get(key));
 +      assertEquals("region contents are not consistent", r2Contents.get(key), r3Contents.get(key));
 +      for (int subi=1; subi<3; subi++) {
 +        String subkey = key + "-" + subi;
 +        assertEquals("region contents are not consistent", r0Contents.get(subkey), r1Contents.get(subkey));
 +        assertEquals("region contents are not consistent", r1Contents.get(subkey), r2Contents.get(subkey));
 +        assertEquals("region contents are not consistent", r2Contents.get(subkey), r3Contents.get(subkey));
 +      }
 +    }
 +  }
 +  
 +  static void addBlockingListener() {
 +    ListenerBlocking = true;
 +    CCRegion.getAttributesMutator().addCacheListener(new CacheListenerAdapter(){
 +      public void afterCreate(EntryEvent event) {
 +        onEvent(event);
 +      }
 +      private void onEvent(EntryEvent event) {
 +        boolean blocked = false;
 +        if (event.isOriginRemote()) {
 +          synchronized(this) {
 +            while (ListenerBlocking) {
 +              LogWriterUtils.getLogWriter().info("blocking cache operations for " + event.getDistributedMember());
 +              blocked = true;
 +              try {
 +                wait();
 +              } catch (InterruptedException e) {
 +                Thread.currentThread().interrupt();
 +                LogWriterUtils.getLogWriter().info("blocking cache listener interrupted");
 +                return;
 +              }
 +            }
 +          }
 +          if (blocked) {
 +            LogWriterUtils.getLogWriter().info("allowing cache operations for " + event.getDistributedMember());
 +          }
 +        }
 +      }
 +      @Override
 +      public void close() {
 +        LogWriterUtils.getLogWriter().info("closing blocking listener");
 +        ListenerBlocking = false;
 +        synchronized(this) {
 +          notifyAll();
 +        }
 +      }
 +      @Override
 +      public void afterUpdate(EntryEvent event) {
 +        onEvent(event);
 +      }
 +      @Override
 +      public void afterInvalidate(EntryEvent event) {
 +        onEvent(event);
 +      }
 +      @Override
 +      public void afterDestroy(EntryEvent event) {
 +        onEvent(event);
 +      }
 +    });
 +  }
 +  
 +  static void doManyOps() {
 +    // do not include putAll, which requires an Ack to detect failures
 +    doOpsLoopNoFlush(5000, false, false);
 +  }
 +  
 +  static void unblockListener() {
 +    CacheListener listener = CCRegion.getCacheListener();
 +    ListenerBlocking = false;
 +    synchronized(listener) {
 +      listener.notifyAll();
 +    }
 +  }
 +  
 +  static void clearRegion() {
 +    CCRegion.clear();
 +  }
 +
 +  /**
 +   * This test creates a server cache in vm0 and a peer cache in vm1.
 +   * It then tests to see if GII transferred tombstones to vm1 like it's supposed to.
 +   * A client cache is created in vm2 and the same sort of check is performed
 +   * for register-interest.
 +   */
 +
 +  public void testGIISendsTombstones() throws Exception {
 +    versionTestGIISendsTombstones();
 +  }
 +
 +
 +  protected void do_version_recovery_if_necessary(final VM vm0, final VM vm1, final VM vm2, final Object[] params) {
 +    // do nothing here
 +  }
 +  
 +  /**
 +   * This tests the concurrency versioning system to ensure that event conflation
 +   * happens correctly and that the statistic is being updated properly
 +   */
 +  public void testConcurrentEvents() throws Exception {
 +    versionTestConcurrentEvents();
 +  }
 +  
 +  
 +  public void testClearWithConcurrentEvents() throws Exception {
 +    // need to figure out how to flush clear() ops for verification steps
 +  }
 +
 +  public void testClearWithConcurrentEventsAsync() throws Exception {
 +    // need to figure out how to flush clear() ops for verification steps
 +  }
 +
 +  public void testClearOnNonReplicateWithConcurrentEvents() throws Exception {
 +    // need to figure out how to flush clear() ops for verification steps
 +  }
 +  
 +  
 +  public void testTombstones() throws Exception {
 +//    for (int i=0; i<1000; i++) {
 +//      System.out.println("starting run #"+i);
 +      versionTestTombstones();
 +//      if (i < 999) {
 +//        tearDown();
 +//        setUp();
 +//      }
 +//    }
 +  }
 +  
 +
 +  
 +  
 +  
 +  
 +  
 +  public void testOneHopKnownIssues() {
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    VM vm3 = host.getVM(3); // this VM, but treat as a remote for uniformity
 +    
 +    // create an empty region in vm0 and replicated regions in VM 1 and 3,
 +    // then perform concurrent ops
 +    // on the same key while creating the region in VM2.  Afterward make
 +    // sure that all three regions are consistent
 +    
 +    final String name = this.getUniqueName() + "-CC";
 +    SerializableRunnable createRegion = new SerializableRunnable("Create Region") {
 +        public void run() {
 +          try {
 +            final RegionFactory f;
 +            int vmNumber = VM.getCurrentVMNum();
 +            switch (vmNumber) {
 +            case 0:
 +              f = getCache().createRegionFactory(getRegionAttributes(RegionShortcut.REPLICATE_PROXY.toString()));
 +              break;
 +            case 1:
 +              f = getCache().createRegionFactory(getRegionAttributes(RegionShortcut.REPLICATE.toString()));
 +              f.setDataPolicy(DataPolicy.NORMAL);
 +              break;
 +            default:
 +              f = getCache().createRegionFactory(getRegionAttributes());
 +              break;
 +            }
 +            CCRegion = (LocalRegion)f.create(name);
 +          } catch (CacheException ex) {
 +            Assert.fail("While creating region", ex);
 +          }
 +        }
 +      };
 +      
 +    vm0.invoke(createRegion); // empty
 +    vm1.invoke(createRegion); // normal
 +    vm2.invoke(createRegion); // replicate
 +    
 +    // case 1: entry already invalid on vm2 (replicate) is invalidated by vm0 (empty)
 +    final String invalidationKey = "invalidationKey";
 +    final String destroyKey = "destroyKey";
 +    SerializableRunnable test = new SerializableRunnable("case 1: second invalidation not applied or distributed") {
 +      public void run() {
 +        CCRegion.put(invalidationKey, "initialValue");
 +        
 +        int invalidationCount = CCRegion.getCachePerfStats().getInvalidates(); 
 +        CCRegion.invalidate(invalidationKey);
 +        CCRegion.invalidate(invalidationKey);
 +        assertEquals(invalidationCount+1, CCRegion.getCachePerfStats().getInvalidates());
 +
 +        // also test destroy() while we're at it.  It should throw an exception
 +        int destroyCount = CCRegion.getCachePerfStats().getDestroys();
 +        CCRegion.destroy(invalidationKey);
 +        try {
 +          CCRegion.destroy(invalidationKey);
 +          fail("expected an EntryNotFoundException");
 +        } catch (EntryNotFoundException e) {
 +          // expected
 +        }
 +        assertEquals(destroyCount+1, CCRegion.getCachePerfStats().getDestroys());
 +      }
 +    };
 +    vm0.invoke(test);
 +
 +    // now do the same with the datapolicy=normal region
 +    test.setName("case 2: second invalidation not applied or distributed");
 +    vm1.invoke(test);
 +  }
 +
 +  /**
 +   * This tests the concurrency versioning system to ensure that event conflation
 +   * happens correctly and that the statistic is being updated properly
 +   */
 +  public void testConcurrentEventsOnEmptyRegion() {
 +    versionTestConcurrentEventsOnEmptyRegion();
 +  }
 +  
 +  
 +  
 +  
 +  /**
 +   * This tests the concurrency versioning system to ensure that event conflation
 +   * happens correctly and that the statistic is being updated properly
 +   */
 +  public void testConcurrentEventsOnNonReplicatedRegion() {
 +    versionTestConcurrentEventsOnNonReplicatedRegion();
 +  }
 +  
 +  
 +  public void testGetAllWithVersions() {
 +    versionTestGetAllWithVersions();
 +  }
 +
 +  
 +
 +  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 +  // these methods can be uncommented to inhibit test execution
 +  // when new tests are added
 +  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 +
 +//  @Override
 +//  public void testNonblockingGetInitialImage() throws Throwable {
 +//  }
 +//  @Override
 +//  public void testConcurrentOperations() throws Exception {
 +//  }
 +//
 +//  @Override
 +//  public void testDistributedUpdate() {
 +//  }
 +//
 +//  @Override
 +//  public void testDistributedGet() {
 +//  }
 +//
 +//  @Override
 +//  public void testDistributedPutNoUpdate() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testDefinedEntryUpdated() {
 +//  }
 +//
 +//  @Override
 +//  public void testDistributedDestroy() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testDistributedRegionDestroy() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testLocalRegionDestroy() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testDistributedInvalidate() {
 +//  }
 +//
 +//  @Override
 +//  public void testDistributedInvalidate4() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testDistributedRegionInvalidate() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testRemoteCacheListener() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testRemoteCacheListenerInSubregion() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testRemoteCacheLoader() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testRemoteCacheLoaderArg() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testRemoteCacheLoaderException() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testCacheLoaderWithNetSearch() throws CacheException {
 +//  }
 +//
 +//  @Override
 +//  public void testCacheLoaderWithNetLoad() throws CacheException {
 +//  }
 +//
 +//  @Override
 +//  public void testNoRemoteCacheLoader() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testNoLoaderWithInvalidEntry() {
 +//  }
 +//
 +//  @Override
 +//  public void testRemoteCacheWriter() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testLocalAndRemoteCacheWriters() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testCacheLoaderModifyingArgument() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testRemoteLoaderNetSearch() throws CacheException {
 +//  }
 +//
 +//  @Override
 +//  public void testLocalCacheLoader() {
 +//  }
 +//
 +//  @Override
 +//  public void testDistributedPut() throws Exception {
 +//  }
 +//
 +//  @Override
 +//  public void testReplicate() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testDeltaWithReplicate() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testGetInitialImage() {
 +//  }
 +//
 +//  @Override
 +//  public void testLargeGetInitialImage() {
 +//  }
 +//
 +//  @Override
 +//  public void testMirroredDataFromNonMirrored() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testNoMirroredDataToNonMirrored() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testMirroredLocalLoad() {
 +//  }
 +//
 +//  @Override
 +//  public void testMirroredNetLoad() {
 +//  }
 +//
 +//  @Override
 +//  public void testNoRegionKeepAlive() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testNetSearchObservesTtl() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testNetSearchObservesIdleTime() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testEntryTtlDestroyEvent() throws InterruptedException {
 +//  }
 +//
 +//  @Override
 +//  public void testUpdateResetsIdleTime() throws InterruptedException {
 +//  }
 +//  @Override
 +//  public void testTXNonblockingGetInitialImage() throws Throwable {
 +//  }
 +//
 +//  @Override
 +//  public void testNBRegionInvalidationDuringGetInitialImage() throws Throwable {
 +//  }
 +//
 +//  @Override
 +//  public void testNBRegionDestructionDuringGetInitialImage() throws Throwable {
 +//  }
 +//
 +//  @Override
 +//  public void testNoDataSerializer() {
 +//  }
 +//
 +//  @Override
 +//  public void testNoInstantiator() {
 +//  }
 +//
 +//  @Override
 +//  public void testTXSimpleOps() throws Exception {
 +//  }
 +//
 +//  @Override
 +//  public void testTXUpdateLoadNoConflict() throws Exception {
 +//  }
 +//
 +//  @Override
 +//  public void testTXMultiRegion() throws Exception {
 +//  }
 +//
 +//  @Override
 +//  public void testTXRmtMirror() throws Exception {
 +//  }
 +
 +
 +}


[34/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/CacheServerImpl.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/cache/CacheServerImpl.java
index cd4f3e4,0000000..000120b
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/CacheServerImpl.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/CacheServerImpl.java
@@@ -1,821 -1,0 +1,832 @@@
 +/*
 + * 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;
 +
 +import java.io.File;
 +import java.io.IOException;
 +import java.util.ArrayList;
 +import java.util.Arrays;
 +import java.util.Collections;
 +import java.util.HashSet;
 +import java.util.LinkedList;
 +import java.util.List;
 +import java.util.Set;
 +import java.util.concurrent.atomic.AtomicInteger;
 +
 +import org.apache.logging.log4j.Logger;
 +
 +import com.gemstone.gemfire.CancelCriterion;
 +import com.gemstone.gemfire.GemFireIOException;
 +import com.gemstone.gemfire.InternalGemFireError;
 +import com.gemstone.gemfire.InvalidValueException;
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.ClientSession;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.DiskStore;
 +import com.gemstone.gemfire.cache.DiskStoreFactory;
 +import com.gemstone.gemfire.cache.DynamicRegionFactory;
 +import com.gemstone.gemfire.cache.EvictionAction;
 +import com.gemstone.gemfire.cache.ExpirationAction;
 +import com.gemstone.gemfire.cache.ExpirationAttributes;
 +import com.gemstone.gemfire.cache.InterestRegistrationListener;
 +import com.gemstone.gemfire.cache.RegionAttributes;
 +import com.gemstone.gemfire.cache.RegionExistsException;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.cache.server.ClientSubscriptionConfig;
 +import com.gemstone.gemfire.cache.server.ServerLoadProbe;
 +import com.gemstone.gemfire.cache.server.internal.LoadMonitor;
 +import com.gemstone.gemfire.cache.wan.GatewayTransportFilter;
 +import com.gemstone.gemfire.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.internal.DM;
 +import com.gemstone.gemfire.distributed.internal.DistributionAdvisee;
 +import com.gemstone.gemfire.distributed.internal.DistributionAdvisor;
 +import com.gemstone.gemfire.distributed.internal.DistributionAdvisor.Profile;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.distributed.internal.ResourceEvent;
 +import com.gemstone.gemfire.distributed.internal.ServerLocation;
 +import com.gemstone.gemfire.distributed.internal.membership.MemberAttributes;
 +import com.gemstone.gemfire.internal.Assert;
 +import com.gemstone.gemfire.internal.OSProcess;
 +import com.gemstone.gemfire.internal.admin.ClientHealthMonitoringRegion;
 +import com.gemstone.gemfire.internal.cache.CacheServerAdvisor.CacheServerProfile;
 +import com.gemstone.gemfire.internal.cache.ha.HARegionQueue;
 +import com.gemstone.gemfire.internal.cache.tier.Acceptor;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.AcceptorImpl;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.CacheClientNotifier;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.ClientProxyMembershipID;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.logging.LogService;
 +import com.gemstone.gemfire.internal.logging.log4j.LocalizedMessage;
 +import com.gemstone.gemfire.management.membership.ClientMembership;
 +import com.gemstone.gemfire.management.membership.ClientMembershipListener;
 +
 +/**
 + * An implementation of the <code>CacheServer</code> interface that delegates
 + * most of the heavy lifting to an {@link Acceptor}.
 + * 
 + * @author David Whitlock
 + * @since 4.0
 + */
 +@SuppressWarnings("deprecation")
 +public class CacheServerImpl
 +  extends AbstractCacheServer
 +  implements DistributionAdvisee {
 +
 +  private static final Logger logger = LogService.getLogger();
 +  
 +  private static final int FORCE_LOAD_UPDATE_FREQUENCY= Integer.getInteger("gemfire.BridgeServer.FORCE_LOAD_UPDATE_FREQUENCY", 10).intValue();
 +  
 +  /** The acceptor that does the actual serving */
 +  private volatile AcceptorImpl acceptor;
 +
 +  /**
 +   * The advisor used by this cache server.
 +   * @since 5.7
 +   */
 +  private volatile CacheServerAdvisor advisor;
 +
 +  /**
 +   * The monitor used to monitor load on this
 +   * bridge server and distribute load to the locators
 +   * @since 5.7
 +   */
 +  private volatile LoadMonitor loadMonitor;
 +
 +  /**
 +   * boolean that represents whether this server is a GatewayReceiver or a simple BridgeServer
 +   */
 +  private boolean isGatewayReceiver;
 +  
 +  private List<GatewayTransportFilter> gatewayTransportFilters = Collections.EMPTY_LIST;
 +  
++  /** is this a server created by a launcher as opposed to by an application or XML? */
++  private boolean isDefaultServer;
++  
 +  /**
 +   * Needed because this guy is an advisee
 +   * @since 5.7
 +   */
 +  private int serialNumber; // changed on each start
 +
 +  public static final boolean ENABLE_NOTIFY_BY_SUBSCRIPTION_FALSE = 
 +  Boolean.getBoolean("gemfire.cache-server.enable-notify-by-subscription-false");
 +  
 + 
 +  // ////////////////////// Constructors //////////////////////
 +
 +  /**
 +   * Creates a new <code>BridgeServerImpl</code> that serves the contents of
 +   * the give <code>Cache</code>. It has the default configuration.
 +   */
 +  public CacheServerImpl(GemFireCacheImpl cache, boolean isGatewayReceiver) {
 +    super(cache);
 +    this.isGatewayReceiver = isGatewayReceiver;
 +  }
 +
 +  // //////////////////// Instance Methods ///////////////////
 +  
 +  public CancelCriterion getCancelCriterion() {
 +    return cache.getCancelCriterion();    
 +  }
 +
 +  /**
 +   * Checks to see whether or not this bridge server is running. If so, an
 +   * {@link IllegalStateException} is thrown.
 +   */
 +  private void checkRunning() {
 +    if (this.isRunning()) {
 +      throw new IllegalStateException(LocalizedStrings.CacheServerImpl_A_CACHE_SERVERS_CONFIGURATION_CANNOT_BE_CHANGED_ONCE_IT_IS_RUNNING.toLocalizedString());
 +    }
 +  }
 +
 +  public boolean isGatewayReceiver() {
 +    return this.isGatewayReceiver;
 +  }
 +  
 +  @Override
 +  public int getPort() {
 +    if (this.acceptor != null) {
 +      return this.acceptor.getPort();
 +    }
 +    else {
 +      return super.getPort();
 +    }
 +  }
 +
 +  @Override
 +  public void setPort(int port) {
 +    checkRunning();
 +    super.setPort(port);
 +  }
 +
 +  @Override
 +  public void setBindAddress(String address) {
 +    checkRunning();
 +    super.setBindAddress(address);
 +  }
 +  @Override
 +  public void setHostnameForClients(String name) {
 +    checkRunning();
 +    super.setHostnameForClients(name);
 +  }
 +
 +  @Override
 +  public void setMaxConnections(int maxCon) {
 +    checkRunning();
 +    super.setMaxConnections(maxCon);
 +  }
 +
 +  @Override
 +  public void setMaxThreads(int maxThreads) {
 +    checkRunning();
 +    super.setMaxThreads(maxThreads);
 +  }
 +
 +  @Override
 +  public void setNotifyBySubscription(boolean b) {
 +    checkRunning();
 +    if (CacheServerImpl.ENABLE_NOTIFY_BY_SUBSCRIPTION_FALSE) {
 +      this.notifyBySubscription = b;
 +    }
 +  }
 +
 +  @Override
 +  public void setMaximumMessageCount(int maximumMessageCount) {
 +    checkRunning();
 +    super.setMaximumMessageCount(maximumMessageCount);
 +  }
 +
 +  @Override
 +  public void setSocketBufferSize(int socketBufferSize) {
 +    this.socketBufferSize = socketBufferSize;
 +  }
 +
 +  @Override
 +  public int getSocketBufferSize() {
 +    return this.socketBufferSize;
 +  }
 +  
 +  @Override
 +  public void setMaximumTimeBetweenPings(int maximumTimeBetweenPings) {
 +    this.maximumTimeBetweenPings = maximumTimeBetweenPings;
 +  }
 +
 +  @Override
 +  public int getMaximumTimeBetweenPings() {
 +    return this.maximumTimeBetweenPings;
 +  }
 +
 +
 +  @Override
 +  public void setLoadPollInterval(long loadPollInterval) {
 +    checkRunning();
 +    super.setLoadPollInterval(loadPollInterval);
 +  }
 +
 +  @Override
 +  public int getMaximumMessageCount() {
 +    return this.maximumMessageCount;
 +  }
 +
 +  @Override
 +  public void setLoadProbe(ServerLoadProbe loadProbe) {
 +    checkRunning();
 +    super.setLoadProbe(loadProbe);
 +  }
 +
 +  public void setGatewayTransportFilter(
 +      List<GatewayTransportFilter> transportFilters) {
 +    this.gatewayTransportFilters = transportFilters;
 +  }
 +  
 +  @Override
 +  public int getMessageTimeToLive() {
 +    return this.messageTimeToLive;
 +  }
 +  
 +
 +  public ClientSubscriptionConfig getClientSubscriptionConfig(){
 +    return this.clientSubscriptionConfig;
 +  }
 +
++  public boolean isDefaultServer() {
++    return isDefaultServer;
++  }
++
++  public void setIsDefaultServer() {
++    this.isDefaultServer = true;
++  }
++
 +  /**
 +   * Sets the configuration of <b>this</b> <code>CacheServer</code> based on
 +   * the configuration of <b>another</b> <code>CacheServer</code>.
 +   */
 +  public void configureFrom(CacheServer other) {
 +    setPort(other.getPort());
 +    setBindAddress(other.getBindAddress());
 +    setHostnameForClients(other.getHostnameForClients());
 +    setMaxConnections(other.getMaxConnections());
 +    setMaxThreads(other.getMaxThreads());
 +    setNotifyBySubscription(other.getNotifyBySubscription());
 +    setSocketBufferSize(other.getSocketBufferSize());
 +    setTcpNoDelay(other.getTcpNoDelay());
 +    setMaximumTimeBetweenPings(other.getMaximumTimeBetweenPings());
 +    setMaximumMessageCount(other.getMaximumMessageCount());
 +    setMessageTimeToLive(other.getMessageTimeToLive());
 +//    setTransactionTimeToLive(other.getTransactionTimeToLive());  not implemented in CacheServer for v6.6
 +    setGroups(other.getGroups());
 +    setLoadProbe(other.getLoadProbe());
 +    setLoadPollInterval(other.getLoadPollInterval());
 +    ClientSubscriptionConfig cscOther = other.getClientSubscriptionConfig();
 +    ClientSubscriptionConfig cscThis = this.getClientSubscriptionConfig();
 +    // added for configuration of ha overflow
 +    cscThis.setEvictionPolicy(cscOther.getEvictionPolicy());
 +    cscThis.setCapacity(cscOther.getCapacity());
 +    String diskStoreName = cscOther.getDiskStoreName();
 +    if (diskStoreName != null) {
 +      cscThis.setDiskStoreName(diskStoreName);
 +    } else {
 +      cscThis.setOverflowDirectory(cscOther.getOverflowDirectory());
 +    }
 +  }
 +
 +  @Override
 +  public synchronized void start() throws IOException {
 +    Assert.assertTrue(this.cache != null);
 +    boolean isSqlFabricSystem = ((GemFireCacheImpl)this.cache).isSqlfSystem();
 +    
 +    this.serialNumber = createSerialNumber();
 +    if (DynamicRegionFactory.get().isOpen()) {
 +      // force notifyBySubscription to be true so that meta info is pushed
 +      // from servers to clients instead of invalidates.
 +      if (!this.notifyBySubscription) {
 +        logger.info(LocalizedMessage.create(LocalizedStrings.CacheServerImpl_FORCING_NOTIFYBYSUBSCRIPTION_TO_SUPPORT_DYNAMIC_REGIONS));
 +        this.notifyBySubscription = true;
 +      }
 +    }
 +    this.advisor = CacheServerAdvisor.createCacheServerAdvisor(this);
 +    this.loadMonitor = new LoadMonitor(loadProbe, maxConnections,
 +        loadPollInterval, FORCE_LOAD_UPDATE_FREQUENCY, 
 +        advisor);
 +    List overflowAttributesList = new LinkedList();
 +    ClientSubscriptionConfig csc = this.getClientSubscriptionConfig();
 +    overflowAttributesList.add(0, csc.getEvictionPolicy());
 +    overflowAttributesList.add(1, Integer.valueOf(csc.getCapacity()));
 +    overflowAttributesList.add(2, Integer.valueOf(this.port));
 +    String diskStoreName = csc.getDiskStoreName();
 +    if (diskStoreName != null) {
 +      overflowAttributesList.add(3, diskStoreName);
 +      overflowAttributesList.add(4, true); // indicator to use diskstore
 +    } else {
 +      overflowAttributesList.add(3, csc.getOverflowDirectory());
 +      overflowAttributesList.add(4, false);
 +    }
 +
 +    this.acceptor = new AcceptorImpl(getPort(), 
 +                                     getBindAddress(),
 +                                     getNotifyBySubscription(),
 +                                     getSocketBufferSize(), 
 +                                     getMaximumTimeBetweenPings(), 
 +                                     this.cache,
 +                                     getMaxConnections(), 
 +                                     getMaxThreads(), 
 +                                     getMaximumMessageCount(),
 +                                     getMessageTimeToLive(),
 +                                     getTransactionTimeToLive(),
 +                                     this.loadMonitor,
 +                                     overflowAttributesList, 
 +                                     isSqlFabricSystem,
 +                                     this.isGatewayReceiver,
 +                                     this.gatewayTransportFilters, this.tcpNoDelay);
 +
 +    this.acceptor.start();
 +    this.advisor.handshake();
 +    this.loadMonitor.start(new ServerLocation(getExternalAddress(),
 +        getPort()), acceptor.getStats());
 +    
 +    // TODO : Need to provide facility to enable/disable client health monitoring.
 +    //Creating ClientHealthMonitoring region.
 +    // Force initialization on current cache
 +    if(cache instanceof GemFireCacheImpl) {
 +      ClientHealthMonitoringRegion.getInstance((GemFireCacheImpl)cache);
 +    }
 +    this.cache.getLoggerI18n().config(LocalizedStrings.CacheServerImpl_CACHESERVER_CONFIGURATION___0, getConfig());
 +    
 +    /* 
 +     * If the stopped bridge server is restarted, we'll need to re-register the 
 +     * client membership listener. If the listener is already registered it 
 +     * won't be registered as would the case when start() is invoked for the 
 +     * first time.  
 +     */
 +    ClientMembershipListener[] membershipListeners = 
 +                                ClientMembership.getClientMembershipListeners();
 +    
 +    boolean membershipListenerRegistered = false;
 +    for (ClientMembershipListener membershipListener : membershipListeners) {
 +      //just checking by reference as the listener instance is final
 +      if (listener == membershipListener) {
 +        membershipListenerRegistered = true;
 +        break;
 +      }
 +    }
 +    
 +    if (!membershipListenerRegistered) {
 +      ClientMembership.registerClientMembershipListener(listener);
 +    }
 +    
 +    if (!isGatewayReceiver) {
 +      InternalDistributedSystem system = ((GemFireCacheImpl) this.cache)
 +          .getDistributedSystem();
 +      system.handleResourceEvent(ResourceEvent.CACHE_SERVER_START, this);
 +    }
 +    
 +  }
 +
 +  
 +  /**
 +   * Gets the address that this bridge server can be contacted on from external
 +   * processes.
 +   * @since 5.7
 +   */
 +  public String getExternalAddress() {
 +    return getExternalAddress(true);
 +  }
 +  
 +  public String getExternalAddress(boolean checkServerRunning) {
 +    if (checkServerRunning) {
 +      if (!this.isRunning()) {
 +        String s = "A bridge server's bind address is only available if it has been started";
 +        this.cache.getCancelCriterion().checkCancelInProgress(null);
 +        throw new IllegalStateException(s);
 +      }
 +    }
 +    if (this.hostnameForClients == null || this.hostnameForClients.equals("")) {
 +      if (this.acceptor != null) {
 +        return this.acceptor.getExternalAddress();
 +      }
 +      else {
 +        return null;
 +      }
 +    }
 +    else {
 +      return this.hostnameForClients;
 +    }
 +  }
 +
 +  public boolean isRunning() {
 +    return this.acceptor != null && this.acceptor.isRunning();
 +  }
 +
 +  public synchronized void stop() {
 +    if (!isRunning()) {
 +      return;
 +    }
 +    
 +    RuntimeException firstException = null;
 +    
 +    try {
 +      if(this.loadMonitor != null) {
 +        this.loadMonitor.stop();
 +      }
 +    } catch(RuntimeException e) {
 +      cache.getLoggerI18n().warning(LocalizedStrings.CacheServerImpl_CACHESERVER_ERROR_CLOSING_LOAD_MONITOR, e);
 +      firstException = e;
 +    }
 +    
 +    try {
 +      if (this.advisor != null) {
 +        this.advisor.close();
 +      }
 +    } catch(RuntimeException e) {
 +      cache.getLoggerI18n().warning(LocalizedStrings.CacheServerImpl_CACHESERVER_ERROR_CLOSING_ADVISOR, e);
 +      firstException = e;
 +    } 
 +    
 +    try {
 +      if (this.acceptor != null) {
 +        this.acceptor.close();
 +      }
 +    } catch(RuntimeException e) {
 +      logger.warn(LocalizedMessage.create(LocalizedStrings.CacheServerImpl_CACHESERVER_ERROR_CLOSING_ACCEPTOR_MONITOR), e);
 +      if (firstException != null) {
 +        firstException = e;
 +      }
 +    }
 +    
 +    if(firstException != null) {
 +      throw firstException;
 +    }
 +    
 +    //TODO : We need to clean up the admin region created for client
 +    //monitoring.
 +    
 +    // BridgeServer is still available, just not running, so we don't take
 +    // it out of the cache's list...
 +    // cache.removeBridgeServer(this);
 +
 +    /* Assuming start won't be called after stop */
 +    ClientMembership.unregisterClientMembershipListener(listener);
 +    
 +    TXManagerImpl txMgr = (TXManagerImpl) cache.getCacheTransactionManager();
 +    txMgr.removeHostedTXStatesForClients();
 +    
 +    if (!isGatewayReceiver) {
 +      InternalDistributedSystem system = ((GemFireCacheImpl) this.cache)
 +          .getDistributedSystem();
 +      system.handleResourceEvent(ResourceEvent.CACHE_SERVER_STOP, this);
 +    }
 +
 +  }
 +
 +  private String getConfig() {
 +    ClientSubscriptionConfig csc = this.getClientSubscriptionConfig();
 +    String str =
 +    "port=" + getPort() + " max-connections=" + getMaxConnections()
 +        + " max-threads=" + getMaxThreads() + " notify-by-subscription="
 +        + getNotifyBySubscription() + " socket-buffer-size="
 +        + getSocketBufferSize() + " maximum-time-between-pings="
 +        + getMaximumTimeBetweenPings() + " maximum-message-count="
 +        + getMaximumMessageCount() + " message-time-to-live="
 +        + getMessageTimeToLive() + " eviction-policy=" + csc.getEvictionPolicy()
 +        + " capacity=" + csc.getCapacity() + " overflow directory=";
 +    if (csc.getDiskStoreName() != null) {
 +      str += csc.getDiskStoreName();
 +    } else {
 +      str += csc.getOverflowDirectory(); 
 +    }
 +    str += 
 +        " groups=" + Arrays.asList(getGroups())
 +        + " loadProbe=" + loadProbe
 +        + " loadPollInterval=" + loadPollInterval
 +        + " tcpNoDelay=" + tcpNoDelay;
 +    return str;
 +  }
 +
 +  @Override
 +  public String toString() {
 +    ClientSubscriptionConfig csc = this.getClientSubscriptionConfig();
 +    String str = 
 +    "CacheServer on port=" + getPort() + " client subscription config policy="
 +        + csc.getEvictionPolicy() + " client subscription config capacity="
 +        + csc.getCapacity();
 +    if (csc.getDiskStoreName() != null) {
 +      str += " client subscription config overflow disk store="
 +        + csc.getDiskStoreName();
 +    } else {
 +      str += " client subscription config overflow directory="
 +        + csc.getOverflowDirectory();
 +    }
 +    return str;
 +  }
 +
 +  /**
 +   * Test method used to access the internal acceptor
 +   * 
 +   * @return the internal acceptor
 +   */
 +  public AcceptorImpl getAcceptor() {
 +    return this.acceptor;
 +  }
 +
 +  // DistributionAdvisee methods
 +
 +  public DM getDistributionManager() {
 +    return getSystem().getDistributionManager();
 +  }
 +  
 +  public ClientSession getClientSession(String durableClientId) {
 +    return getCacheClientNotifier().getClientProxy(durableClientId);
 +  }
 +
 +  public ClientSession getClientSession(DistributedMember member) {
 +    return getCacheClientNotifier().getClientProxy(
 +        ClientProxyMembershipID.getClientId(member));
 +  }
 +  
 +  public Set getAllClientSessions() {
 +    return new HashSet(getCacheClientNotifier().getClientProxies());
 +  }
 +
 +  /**
 +   * create client subscription
 +   * 
 +   * @param cache
 +   * @param ePolicy
 +   * @param capacity
 +   * @param port
 +   * @param overFlowDir
 +   * @param isDiskStore
 +   * @return client subscription name
 +   * @since 5.7
 +   */
 +  public static String clientMessagesRegion(GemFireCacheImpl cache, String ePolicy,
 +      int capacity, int port, String overFlowDir, boolean isDiskStore) {
 +    AttributesFactory factory = getAttribFactoryForClientMessagesRegion(cache, 
 +        ePolicy, capacity, overFlowDir, isDiskStore);
 +    RegionAttributes attr = factory.create();
 +
 +    return createClientMessagesRegion(attr, cache, capacity, port);
 +  }
 +
 +  public static AttributesFactory getAttribFactoryForClientMessagesRegion(
 +      GemFireCacheImpl cache,
 +      String ePolicy, int capacity, String overflowDir, boolean isDiskStore)
 +      throws InvalidValueException, GemFireIOException {
 +    AttributesFactory factory = new AttributesFactory();
 +    factory.setScope(Scope.LOCAL);
 +
 +    if (isDiskStore) {
 +      // overflowDir parameter is actually diskstore name
 +      factory.setDiskStoreName(overflowDir);
 +      // client subscription queue is always overflow to disk, so do async
 +      // see feature request #41479
 +      factory.setDiskSynchronous(true);
 +    } else if  (overflowDir == null || overflowDir.equals(ClientSubscriptionConfig.DEFAULT_OVERFLOW_DIRECTORY)) {
 +      factory.setDiskStoreName(null);
 +      // client subscription queue is always overflow to disk, so do async
 +      // see feature request #41479
 +      factory.setDiskSynchronous(true);
 +    } else {
 +      File dir = new File(overflowDir + File.separatorChar
 +          + generateNameForClientMsgsRegion(OSProcess.getId()));
 +      // This will delete the overflow directory when virtual machine terminates.
 +      dir.deleteOnExit();
 +      if (!dir.mkdirs() && !dir.isDirectory()) {
 +        throw new GemFireIOException("Could not create client subscription overflow directory: "
 +            + dir.getAbsolutePath());
 +      }
 +      File[] dirs = { dir };
 +      DiskStoreFactory dsf = cache.createDiskStoreFactory();
 +      DiskStore bsi = dsf.setAutoCompact(true)
 +      .setDiskDirsAndSizes(dirs, new int[] { Integer.MAX_VALUE })
 +      .create("bsi");
 +      factory.setDiskStoreName("bsi");
 +      // backward compatibility, it was sync
 +      factory.setDiskSynchronous(true);
 +    }
 +    factory.setDataPolicy(DataPolicy.NORMAL);
 +    // enable statistics
 +    factory.setStatisticsEnabled(true);
 +    /* setting LIFO related eviction attributes */
 +    if (HARegionQueue.HA_EVICTION_POLICY_ENTRY.equals(ePolicy)) {
 +      factory
 +          .setEvictionAttributes(EvictionAttributesImpl.createLIFOEntryAttributes(
 +              capacity, EvictionAction.OVERFLOW_TO_DISK));
 +    }
 +    else if (HARegionQueue.HA_EVICTION_POLICY_MEMORY.equals(ePolicy)) { // condition refinement
 +      factory
 +          .setEvictionAttributes(EvictionAttributesImpl.createLIFOMemoryAttributes(
 +              capacity, EvictionAction.OVERFLOW_TO_DISK));
 +    }
 +    else {
 +      // throw invalid eviction policy exception
 +      throw new InvalidValueException(
 +        LocalizedStrings.CacheServerImpl__0_INVALID_EVICTION_POLICY.toLocalizedString(ePolicy));
 +    }
 +    return factory;
 +  }
 +
 +  public static String createClientMessagesRegion(RegionAttributes attr,
 +      GemFireCacheImpl cache, int capacity, int port) {
 +    // generating unique name in VM for ClientMessagesRegion
 +    String regionName = generateNameForClientMsgsRegion(port);
 +    try {
 +      cache.createVMRegion(regionName, attr,
 +          new InternalRegionArguments().setDestroyLockFlag(true)
 +              .setRecreateFlag(false).setSnapshotInputStream(null)
 +              .setImageTarget(null).setIsUsedForMetaRegion(true));
 +    }
 +    catch (RegionExistsException ree) {
 +      InternalGemFireError assErr = new InternalGemFireError(
 +          "unexpected exception");
 +      assErr.initCause(ree);
 +      throw assErr;
 +    }
 +    catch (IOException e) {
 +      // only if loading snapshot, not here
 +      InternalGemFireError assErr = new InternalGemFireError(
 +          "unexpected exception");
 +      assErr.initCause(e);
 +      throw assErr;
 +    }
 +    catch (ClassNotFoundException e) {
 +      // only if loading snapshot, not here
 +      InternalGemFireError assErr = new InternalGemFireError(
 +          "unexpected exception");
 +      assErr.initCause(e);
 +      throw assErr;
 +    }
 +    return regionName;
 +  }
 +
 +  public static String createClientMessagesRegionForTesting(GemFireCacheImpl cache,
 +      String ePolicy, int capacity, int port, int expiryTime, String overFlowDir, boolean isDiskStore) {
 +    AttributesFactory factory = getAttribFactoryForClientMessagesRegion(cache, 
 +        ePolicy, capacity, overFlowDir, isDiskStore);
 +    ExpirationAttributes ea = new ExpirationAttributes(expiryTime,
 +        ExpirationAction.LOCAL_INVALIDATE);
 +    factory.setEntryTimeToLive(ea);
 +    RegionAttributes attr = factory.create();
 +
 +    return createClientMessagesRegion(attr, cache, capacity, port);
 +  }
 +
 +  /**
 +   * Generates the name for the client subscription using the given id.
 +   * 
 +   * @param id
 +   * @return String
 +   * @since 5.7 
 +   */
 +  public static String generateNameForClientMsgsRegion(int id) {
 +    return ClientSubscriptionConfigImpl.CLIENT_SUBSCRIPTION + "_" + id;
 +  }
 +
 +  /*
 +   * Marker class name to identify the lock more easily in thread dumps private
 +   * static class ClientMessagesRegionLock extends Object { }
 +   */
 +  public DistributionAdvisor getDistributionAdvisor() {
 +    return this.advisor;
 +  }
 +  
 +  /**
 +   * Returns the BridgeServerAdvisor for this server
 +   */
 +  public CacheServerAdvisor getCacheServerAdvisor() {
 +    return this.advisor;
 +  }
 +  
 +  public Profile getProfile() {
 +    return getDistributionAdvisor().createProfile();
 +  }
 +  
 +  public DistributionAdvisee getParentAdvisee() {
 +    return null;
 +  }
 +  
 +  /**
 +   * Returns the underlying <code>InternalDistributedSystem</code> connection.
 +   * @return the underlying <code>InternalDistributedSystem</code>
 +   */
 +  public InternalDistributedSystem getSystem() {
 +    return (InternalDistributedSystem)this.cache.getDistributedSystem();
 +  }
 +  
 +  public String getName() {
 +    return "CacheServer";
 +  }
 +  
 +  public String getFullPath() {
 +    return getName();
 +  }
 +
 +  private final static AtomicInteger profileSN = new AtomicInteger();
 +  
 +  private static int createSerialNumber() {
 +    return profileSN.incrementAndGet();
 +  }
 +
 +  /**
 +   * Returns an array of all the groups of this bridge server.
 +   * This includes those from the groups gemfire property
 +   * and those explicitly added to this server.
 +   */
 +  public String[] getCombinedGroups() {
 +    ArrayList<String> groupList = new ArrayList<String>();
 +    for (String g: MemberAttributes.parseGroups(null, getSystem().getConfig().getGroups())) {
 +      if (!groupList.contains(g)) {
 +        groupList.add(g);
 +      }
 +    }
 +    for (String g: getGroups()) {
 +      if (!groupList.contains(g)) {
 +        groupList.add(g);
 +      }
 +    }
 +    String[] groups = new String[groupList.size()];
 +    return groupList.toArray(groups);
 +  }
 +  
 +  public /*synchronized causes deadlock*/ void fillInProfile(Profile profile) {
 +    assert profile instanceof CacheServerProfile;
 +    CacheServerProfile bp = (CacheServerProfile)profile;
 +    bp.setHost(getExternalAddress(false));
 +    bp.setPort(getPort());
 +    bp.setGroups(getCombinedGroups());
 +    bp.setMaxConnections(maxConnections);
 +    bp.setInitialLoad(loadMonitor.getLastLoad());
 +    bp.setLoadPollInterval(getLoadPollInterval());
 +    bp.serialNumber = getSerialNumber();
 +    bp.finishInit();
 +  }
 +
 +  public int getSerialNumber() {
 +    return this.serialNumber;
 +  }
 +
 +  
 +   protected CacheClientNotifier getCacheClientNotifier() {
 +    return getAcceptor().getCacheClientNotifier();
 +  } 
 +   
 +  /**
 +   * Registers a new <code>InterestRegistrationListener</code> with the set of
 +   * <code>InterestRegistrationListener</code>s.
 +   * 
 +   * @param listener
 +   *                The <code>InterestRegistrationListener</code> to register
 +   * @throws IllegalStateException if the BridgeServer has not been started
 +   * @since 5.8Beta
 +   */
 +  public void registerInterestRegistrationListener(
 +      InterestRegistrationListener listener) {
 +    if (!this.isRunning()) {
 +      throw new IllegalStateException(LocalizedStrings.CacheServerImpl_MUST_BE_RUNNING.toLocalizedString());
 +    }
 +    getCacheClientNotifier().registerInterestRegistrationListener(listener); 
 +  }
 +
 +  /**
 +   * Unregisters an existing <code>InterestRegistrationListener</code> from
 +   * the set of <code>InterestRegistrationListener</code>s.
 +   * 
 +   * @param listener
 +   *                The <code>InterestRegistrationListener</code> to
 +   *                unregister
 +   * 
 +   * @since 5.8Beta
 +   */
 +  public void unregisterInterestRegistrationListener(
 +      InterestRegistrationListener listener) {
 +    getCacheClientNotifier().unregisterInterestRegistrationListener(listener);     
 +  }
 +
 +  /**
 +   * Returns a read-only set of <code>InterestRegistrationListener</code>s
 +   * registered with this notifier.
 +   * 
 +   * @return a read-only set of <code>InterestRegistrationListener</code>s
 +   *         registered with this notifier
 +   * 
 +   * @since 5.8Beta
 +   */
 +  public Set getInterestRegistrationListeners() {
 +    return getCacheClientNotifier().getInterestRegistrationListeners(); 
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/CacheServerLauncher.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/cache/CacheServerLauncher.java
index ac198e9,0000000..c5de84b
mode 100755,000000..100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/CacheServerLauncher.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/CacheServerLauncher.java
@@@ -1,1391 -1,0 +1,1393 @@@
 +/*
 + * 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;
 +
 +import java.io.File;
 +import java.io.FileInputStream;
 +import java.io.FileNotFoundException;
 +import java.io.FileOutputStream;
 +import java.io.IOException;
 +import java.io.ObjectInputStream;
 +import java.io.ObjectOutputStream;
 +import java.io.PrintStream;
 +import java.io.Serializable;
 +import java.net.URL;
 +import java.util.AbstractList;
 +import java.util.ArrayList;
 +import java.util.HashMap;
 +import java.util.List;
 +import java.util.Map;
 +import java.util.Properties;
 +import java.util.concurrent.TimeUnit;
 +
 +import com.gemstone.gemfire.SystemFailure;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheFactory;
 +import com.gemstone.gemfire.cache.partition.PartitionRegionHelper;
 +import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.distributed.DistributedSystem;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfigImpl;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.i18n.LogWriterI18n;
 +import com.gemstone.gemfire.internal.OSProcess;
 +import com.gemstone.gemfire.internal.PureJavaMode;
 +import com.gemstone.gemfire.internal.SocketCreator;
++import com.gemstone.gemfire.internal.cache.tier.sockets.CacheServerHelper;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
- import com.gemstone.gemfire.internal.logging.PureLogWriter;
 +import com.gemstone.gemfire.internal.process.StartupStatus;
 +import com.gemstone.gemfire.internal.process.StartupStatusListener;
 +import com.gemstone.gemfire.internal.util.IOUtils;
 +import com.gemstone.gemfire.internal.util.JavaCommandBuilder;
 +
 +/**
 + * Launcher program to start a cache server.
 + *
 + * @author Sudhir Menon
 + * @author Barry Oglesby
 + * @author David Whitlock
 + * @author John Blum
 + *
 + * @since 2.0.2
 + */
 +public class CacheServerLauncher  {
 +
 +  /** Is this VM a dedicated Cache Server?  This value is used mainly by the admin API. */
 +  public static boolean isDedicatedCacheServer = Boolean.getBoolean("gemfire.isDedicatedServer");
 +
 +  public static boolean ASSIGN_BUCKETS = Boolean.getBoolean("gemfire.CacheServerLauncher.assignBucketsToPartitions");
 +
 +  //default is to exit if property not defined
 +  public static boolean DONT_EXIT_AFTER_LAUNCH = Boolean.getBoolean("gemfire.CacheServerLauncher.dontExitAfterLaunch");
 +
 +  /** Should the launch command be printed? */
 +  public static final boolean PRINT_LAUNCH_COMMAND = Boolean.getBoolean(
 +    CacheServerLauncher.class.getSimpleName() + ".PRINT_LAUNCH_COMMAND");
 +  
 +  private static final long STATUS_WAIT_TIME 
 +    = Long.getLong("gemfire.CacheServerLauncher.STATUS_WAIT_TIME_MS", 15000);
 +  
 +  /** How long to wait for a cache server to stop */
 +  private static final long SHUTDOWN_WAIT_TIME 
 +    = Long.getLong("gemfire.CacheServerLauncher.SHUTDOWN_WAIT_TIME_MS", 20000);
 +
 +  protected final String baseName;
 +  protected final String defaultLogFileName;
 +  protected final String startLogFileName;
 +  protected final String statusName;
 +  protected Status status = null;
 +  protected File workingDir = null;
 +  protected PrintStream oldOut = System.out;
 +  protected PrintStream oldErr = System.err;
 +  protected LogWriterI18n logger = null;
 +  protected String maxHeapSize;
 +  protected String initialHeapSize;
 +  protected String offHeapSize;
 +
 +
 +  public static final int SHUTDOWN = 0;
 +  public static final int STARTING = 1;
 +  public static final int RUNNING = 2;
 +  public static final int SHUTDOWN_PENDING = 3;
 +
 +  private static final int FORCE_STATUS_FILE_READ_ITERATION_COUNT = 10;
 +
 +  public CacheServerLauncher(final String baseName) {
 +    assert baseName != null : "The base name used for the cache server launcher files cannot be null!";
 +    this.baseName = baseName;
 +    final String baseNameLowerCase = baseName.toLowerCase().replace(" ", "");
 +    this.startLogFileName = "start_" + baseNameLowerCase + ".log";
 +    this.defaultLogFileName = baseNameLowerCase + ".log";
 +    this.statusName = "." + baseNameLowerCase + ".ser";
 +  }
 +
 +  protected static Status createStatus(final String baseName, final int state, final int pid) {
 +    return createStatus(baseName, state, pid, null, null);
 +  }
 +
 +  protected static Status createStatus(final String baseName, final int state, final int pid, final String msg, final Throwable t) {
 +    final Status status = new Status(baseName);
 +    status.state = state;
 +    status.pid = pid;
 +    status.msg = msg;
 +    status.exception = t;
 +    return status;
 +  }
 +
 +  /**
 +   * Prints usage information about this program.
 +   */
 +  protected void usage() {
 +    PrintStream out = System.out;
 +    out.println("cacheserver start [-J<vmarg>]* [<attName>=<attValue>]* [-dir=<workingdir>] [-classpath=<classpath>] [-disable-default-server] [-rebalance] [-lock-memory] [-server-port=<server-port>] [-server-bind-address=<server-bind-address>] [-critical-heap-percentage=<critical-heap-percentage>] [-eviction-heap-percentage=<eviction-heap-percentage>] [-critical-off-heap-percentage=<critical-off-heap-percentage>] [-eviction-off-heap-percentage=<eviction-off-heap-percentage>]\n" );
 +    out.println("\t" + LocalizedStrings.CacheServerLauncher_STARTS_A_GEMFIRE_CACHESERVER_VM.toLocalizedString() );
 +    out.println("\t" + LocalizedStrings.CacheServerLauncher_VMARG.toLocalizedString());
 +    out.println("\t" + LocalizedStrings.CacheServerLauncher_DIR.toLocalizedString());
 +    out.println("\t" + LocalizedStrings.CacheServerLauncher_CLASSPATH.toLocalizedString());
 +    out.println("\t" + LocalizedStrings.CacheServerLauncher_ATTNAME.toLocalizedString());
 +    out.println("\t" + LocalizedStrings.CacheServerLauncher_REBALANCE.toLocalizedString());
 +    out.println("\t" + LocalizedStrings.CacheServerLauncher_DISABLE_DEFAULT_SERVER.toLocalizedString());
 +    out.println("\t" + LocalizedStrings.CacheServerLauncher_SERVER_PORT.toLocalizedString());
 +    out.println("\t" + LocalizedStrings.CacheServerLauncher_SERVER_BIND_ADDRESS.toLocalizedString());
 +    out.println("\t" + LocalizedStrings.CacheServerLauncher_CRITICAL_HEAP_PERCENTAGE.toLocalizedString());
 +    out.println("\t" + LocalizedStrings.CacheServerLauncher_EVICTION_HEAP_PERCENTAGE.toLocalizedString());
 +    out.println("\t" + LocalizedStrings.CacheServerLauncher_CRITICAL_OFF_HEAP_PERCENTAGE.toLocalizedString());
 +    out.println("\t" + LocalizedStrings.CacheServerLauncher_EVICTION_OFF_HEAP_PERCENTAGE.toLocalizedString());
 +    out.println("\t" + LocalizedStrings.CacheServerLauncher_LOCK_MEMORY.toLocalizedString());
 +
 +    out.println();
 +    out.println( "cacheserver stop [-dir=<workingdir>]" );
 +    out.println("\t" + LocalizedStrings.CacheServerLauncher_STOPS_A_GEMFIRE_CACHESERVER_VM.toLocalizedString());
 +    out.println("\t" + LocalizedStrings.CacheServerLauncher_DIR.toLocalizedString());
 +    out.println();
 +    out.println( "cacheserver status [-dir=<workingdir>]" );
 +    out.println( "\t" + LocalizedStrings.CacheServerLauncher_STATUS.toLocalizedString());
 +    out.println("\t" + LocalizedStrings.CacheServerLauncher_DIR.toLocalizedString());
 +  }
 +
 +  /**
 +   * Prints the status of the cache server running the configured
 +   * working directory.
 +   */
 +  protected void status(final String[] args) throws Exception {
 +    workingDir = (File) getStopOptions(args).get(DIR);
 +    System.out.println(getStatus());
 +    System.exit(0);
 +  }
 +
 +  /**
 +   * Returns the <code>Status</code> of the cache server in the
 +   * <code>workingDir</code>.
 +   */
 +  protected Status getStatus() throws Exception {
 +    Status status;
 +
 +    if (new File(workingDir, statusName).exists()) {
 +      status = spinReadStatus(); // See bug 32456
 +    }
 +    else {
 +      // no pid since the cache server is not running
 +      status = createStatus(this.baseName, SHUTDOWN, 0);
 +    }
 +
 +    return status;
 +  }
 +
 +  /**
 +   * Main method that parses the command line and performs an
 +   * will start, stop, or get the status of a cache server.  This main
 +   * method is also the main method of the launched cacher server VM
 +   * ("server" mode).
 +   */
 +  public static void main(final String[] args) {
 +    final CacheServerLauncher launcher = new CacheServerLauncher("CacheServer");
 +    boolean inServer = false;
 +
 +    try {
 +      if (args.length > 0) {
 +        if (args[0].equalsIgnoreCase("start")) {
 +          launcher.start(args);
 +        }
 +        else if (args[0].equalsIgnoreCase("server")) {
 +          inServer = true;
 +          launcher.server(args);
 +        }
 +        else if (args[0].equalsIgnoreCase("stop")) {
 +          launcher.stop(args);
 +        }
 +        else if (args[0].equalsIgnoreCase("status")) {
 +          launcher.status(args);
 +        }
 +        else {
 +          launcher.usage();
 +          System.exit(1);
 +        }
 +      }
 +      else {
 +        launcher.usage();
 +        System.exit(1);
 +      }
 +
 +      throw new Exception(LocalizedStrings.CacheServerLauncher_INTERNAL_ERROR_SHOULDNT_REACH_HERE.toLocalizedString());
 +    }
 +    catch (VirtualMachineError err) {
 +      SystemFailure.initiateFailure(err);
 +      // If this ever returns, rethrow the error.  We're poisoned
 +      // now, so don't let this thread continue.
 +      throw err;
 +    }
 +    catch (Throwable t) {
 +      // Whenever you catch Error or Throwable, you must also
 +      // catch VirtualMachineError (see above).  However, there is
 +      // _still_ a possibility that you are dealing with a cascading
 +      // error condition, so you also need to check to see if the JVM
 +      // is still usable:
 +      SystemFailure.checkFailure();
 +      t.printStackTrace();
 +      if (inServer) {
 +        launcher.setServerError(LocalizedStrings.CacheServerLauncher_ERROR_STARTING_SERVER_PROCESS
 +          .toLocalizedString(), t);
 +      }
 +      launcher.restoreStdOut();
 +      if (launcher.logger != null) {
 +        launcher.logger.severe(LocalizedStrings.CacheServerLauncher_CACHE_SERVER_ERROR, t);
 +
 +      }
 +      else {
 +        System.out.println(LocalizedStrings.CacheServerLauncher_ERROR_0.toLocalizedString(t.getMessage()));
 +      }
 +      System.exit(1);
 +    }
 +  }
 +
 +  protected void restoreStdOut( ) {
 +    System.setErr( oldErr );
 +    System.setOut( oldOut );
 +  }
 +
 +  protected static final String DIR     = "dir";
 +  protected static final String VMARGS  = "vmargs";
 +  protected static final String PROPERTIES = "properties";
 +  protected static final String CLASSPATH = "classpath";
 +  protected static final String REBALANCE = "rebalance";
 +  protected static final String SERVER_PORT = "server-port";
 +  protected static final String SERVER_BIND_ADDRESS = "server-bind-address";
 +  protected static final String DISABLE_DEFAULT_SERVER = "disable-default-server";
 +  public static final String CRITICAL_HEAP_PERCENTAGE =
 +    "critical-heap-percentage";
 +  public static final String EVICTION_HEAP_PERCENTAGE =
 +      "eviction-heap-percentage";
 +  public static final String CRITICAL_OFF_HEAP_PERCENTAGE =
 +      "critical-off-heap-percentage";
 +  public static final String EVICTION_OFF_HEAP_PERCENTAGE =
 +      "eviction-off-heap-percentage";
 +  protected static final String LOCK_MEMORY = "lock-memory";
 +
 +  protected final File processDirOption(final Map<String, Object> options, final String dirValue) throws FileNotFoundException {
 +    final File inputWorkingDirectory = new File(dirValue);
 +
 +    if (!inputWorkingDirectory.exists()) {
 +      throw new FileNotFoundException(LocalizedStrings.CacheServerLauncher_THE_INPUT_WORKING_DIRECTORY_DOES_NOT_EXIST_0
 +        .toLocalizedString(dirValue));
 +    }
 +
 +    options.put(DIR, inputWorkingDirectory);
 +
 +    return inputWorkingDirectory;
 +  }
 +
 +  /**
 +   * Populates a map that maps the name of the start options such as {@link #DIR} to its value on the command line.
 +   * If no value is specified on the command line, a default one is provided.
 +   */
 +  protected Map<String, Object> getStartOptions(String[] args) throws Exception {
 +    final Map<String, Object> options = new HashMap<String, Object>();
 +    options.put(DIR, new File(System.getProperty("user.dir")));
 +
 +    final List<String> vmArgs = new ArrayList<String>();
 +    options.put(VMARGS, vmArgs);
 +
 +    final Properties props = new Properties();
 +    options.put(PROPERTIES, props);
 +
 +    for (final String arg : args) {
 +      if (arg.equals("start")) {
 +        // expected
 +      }
 +      else if (arg.startsWith("-classpath=")) {
 +        options.put(CLASSPATH, arg.substring(arg.indexOf("=") + 1));
 +      }
 +      else if (arg.startsWith("-dir=")) {
 +        processDirOption(options, arg.substring(arg.indexOf("=") + 1));
 +      }
 +      else if (arg.startsWith("-disable-default-server")) {
 +        options.put(DISABLE_DEFAULT_SERVER, arg);
 +      }
 +      else if (arg.startsWith("-lock-memory")) {
 +        if (System.getProperty("os.name").indexOf("Windows") >= 0) {
 +          throw new IllegalArgumentException("Unable to lock memory on this operating system");
 +        }
 +        props.put(DistributionConfig.LOCK_MEMORY_NAME, "true");
 +      }
 +      else if (arg.startsWith("-rebalance")) {
 +        options.put(REBALANCE, Boolean.TRUE);
 +      }
 +      else if (arg.startsWith("-server-port")) {
 +        options.put(SERVER_PORT, arg);
 +      }
 +      else if (arg.startsWith("-" + CRITICAL_HEAP_PERCENTAGE) ) {
 +        options.put(CRITICAL_HEAP_PERCENTAGE, arg);
 +      }
 +      else if (arg.startsWith("-" + EVICTION_HEAP_PERCENTAGE) ) {
 +        options.put(EVICTION_HEAP_PERCENTAGE, arg);
 +      }
 +      else if (arg.startsWith("-" + CRITICAL_OFF_HEAP_PERCENTAGE) ) {
 +        options.put(CRITICAL_OFF_HEAP_PERCENTAGE, arg);
 +      }
 +      else if (arg.startsWith("-" + EVICTION_OFF_HEAP_PERCENTAGE) ) {
 +        options.put(EVICTION_OFF_HEAP_PERCENTAGE, arg);
 +      }
 +      else if (arg.startsWith("-server-bind-address")) {
 +        options.put(SERVER_BIND_ADDRESS, arg);
 +      }
 +      else if (arg.startsWith("-J")) {
 +        String vmArg = arg.substring(2);
 +        if (vmArg.startsWith("-Xmx")) {
 +          this.maxHeapSize = vmArg.substring(4);
 +        } else if (vmArg.startsWith("-Xms")) {
 +          this.initialHeapSize = vmArg.substring(4);
 +        }
 +        vmArgs.add(vmArg);
 +      }
 +      // moved this default block down so that "-J" like options can have '=' in them.
 +      // an 'indexOf' the assignment operator with greater than 0 ensures a non-empty String key value
 +      else if (arg.indexOf("=") > 0) {
 +        final int assignmentIndex = arg.indexOf("=");
 +        final String key = arg.substring(0, assignmentIndex);
 +        final String value = arg.substring(assignmentIndex + 1);
 +
 +        if (key.startsWith("-")) {
 +          processStartOption(key.substring(1), value, options, vmArgs, props);
 +        }
 +        else {
 +          processStartArg(key, value, options, vmArgs, props);
 +        }
 +      }
 +      else {
 +        throw new IllegalArgumentException(LocalizedStrings.CacheServerLauncher_UNKNOWN_ARGUMENT_0
 +          .toLocalizedString(arg));
 +      }
 +    }
 +
 +    // -J-Djava.awt.headless=true has been added for Mac platform where it
 +    // causes an icon to appear for sqlf launched procs
 +    // TODO: check which library/GemFire code causes awt to be touched
 +    vmArgs.add("-Djava.awt.headless=true");
 +
 +    // configure commons-logging to use JDK logging 
 +    vmArgs.add("-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger");
 +    
 +    options.put(VMARGS, vmArgs);
 +    return options;
 +  }
 +
 +  /**
 +   * Process a command-line options of the form "key=value".
 +   */
 +  protected void processStartArg(final String key,
 +                                 final String value,
 +                                 final Map<String, Object> options,
 +                                 final List<String> vmArgs,
 +                                 final Properties props)
 +    throws Exception
 +  {
 +    props.setProperty(key, value);
 +  }
 +
 +  /**
 +   * Process a command-line option of the form "-key=value".
 +   */
 +  protected void processStartOption(final String key,
 +                                    final String value,
 +                                    final Map<String, Object> options,
 +                                    final List<String> vmArgs,
 +                                    final Properties props)
 +    throws Exception
 +  {
 +    processUnknownStartOption(key, value, options, vmArgs, props);
 +  }
 +
 +  /**
 +   * Process a command-line option of the form "-key=value" unknown to the base class.
 +   */
 +  protected void processUnknownStartOption(final String key,
 +                                           final String value,
 +                                           final Map<String, Object> options,
 +                                           final List<String> vmArgs,
 +                                           final Properties props) {
 +    throw new IllegalArgumentException(LocalizedStrings.CacheServerLauncher_UNKNOWN_ARGUMENT_0.toLocalizedString(key));
 +  }
 +
 +  /**
 +   * Extracts configuration information used when launching the cache server VM.
 +   */
 +  protected Map<String, Object> getServerOptions(final String[] args) throws Exception {
 +    final Map<String, Object> options = new HashMap<String, Object>();
 +    options.put(DIR, new File("."));
 +    workingDir = (File) options.get(DIR);
 +
 +    final Properties props = new Properties();
 +    options.put(PROPERTIES, props);
 +
 +    for (final String arg : args) {
 +      if (arg.equals("server")) {
 +        // expected
 +      }
 +      else if (arg.startsWith("-dir=")) {
 +        this.workingDir = processDirOption(options, arg.substring(arg.indexOf("=") + 1));
 +      }
 +      else if (arg.startsWith("-rebalance")) {
 +        options.put(REBALANCE, Boolean.TRUE);
 +      }
 +      else if (arg.startsWith("-disable-default-server")) {
 +        options.put(DISABLE_DEFAULT_SERVER, Boolean.TRUE);
 +      }
 +      else if (arg.startsWith("-lock-memory")) {
 +        props.put(DistributionConfig.LOCK_MEMORY_NAME, "true");
 +      }
 +      else if (arg.startsWith("-server-port")) {
 +        options.put(SERVER_PORT, arg.substring(arg.indexOf("=") + 1));
 +      }
 +      else if (arg.startsWith("-server-bind-address")) {
 +        options.put(SERVER_BIND_ADDRESS, arg.substring(arg.indexOf("=") + 1));
 +      }
 +      else if (arg.startsWith("-" + CRITICAL_HEAP_PERCENTAGE)) {
 +        options.put(CRITICAL_HEAP_PERCENTAGE, arg.substring(arg.indexOf("=") + 1));
 +      }
 +      else if (arg.startsWith("-" + EVICTION_HEAP_PERCENTAGE)) {
 +        options.put(EVICTION_HEAP_PERCENTAGE, arg.substring(arg.indexOf("=") + 1));
 +      }
 +      else if (arg.startsWith("-" + CRITICAL_OFF_HEAP_PERCENTAGE)) {
 +        options.put(CRITICAL_OFF_HEAP_PERCENTAGE, arg.substring(arg.indexOf("=") + 1));
 +      }
 +      else if (arg.startsWith("-" + EVICTION_OFF_HEAP_PERCENTAGE)) {
 +        options.put(EVICTION_OFF_HEAP_PERCENTAGE, arg.substring(arg.indexOf("=") + 1));
 +      }
 +      else if (arg.indexOf("=") > 1) {
 +        final int assignmentIndex = arg.indexOf("=");
 +        final String key = arg.substring(0, assignmentIndex);
 +        final String value = arg.substring(assignmentIndex + 1);
 +
 +        if (key.startsWith("-")) {
 +          options.put(key.substring(1), value);
 +        }
 +        else {
 +          props.setProperty(key, value);
 +        }
 +      }
 +      else {
 +        throw new IllegalArgumentException(LocalizedStrings.CacheServerLauncher_UNKNOWN_ARGUMENT_0.toLocalizedString(arg));
 +      }
 +    }
 +
 +    return options;
 +  }
 +
 +  /**
 +   * Extracts configuration information for stopping a cache server based on the contents of the command-line.
 +   * This method can also be used with getting the status of a cache server.
 +   */
 +  protected Map<String, Object> getStopOptions(final String[] args) throws Exception {
 +    final Map<String, Object> options = new HashMap<String, Object>();
 +    options.put(DIR, new File("."));
 +
 +    for (final String arg : args) {
 +      if (arg.equals("stop") || arg.equals("status")) {
 +        // expected
 +      }
 +      else if (arg.startsWith("-dir=")) {
 +        processDirOption(options, arg.substring(arg.indexOf("=") + 1));
 +      }
 +      else {
 +        throw new IllegalArgumentException(LocalizedStrings.CacheServerLauncher_UNKNOWN_ARGUMENT_0
 +          .toLocalizedString(arg));
 +      }
 +    }
 +
 +    return options;
 +  }
 +
 +  /**
 +   * Configures and spawns a VM that hosts a cache server.  Any output
 +   * generated while starting the VM will be placed in a special
 +   * {@linkplain #startLogFileName log file}.
 +   *
 +   * See #getStartOptions
 +   * @see OSProcess#bgexec(String[], File, File, boolean, Map)
 +   */
 +  public void start(final String[] args) throws Exception {
 +    final Map<String, Object> options = getStartOptions(args);
 +
 +    workingDir = (File) options.get(DIR);
 +
 +    // Complain if a cache server is already running in the specified working directory.
 +    // See bug 32574.
 +    verifyAndClearStatus();
 +
 +    // start the GemFire Cache Server proces...
 +    runCommandLine(options, buildCommandLine(options));
 +
 +    // wait for status.state == RUNNING
 +    waitForRunning();
 +
 +    if (DONT_EXIT_AFTER_LAUNCH) {
 +      return;
 +    }
 +
 +    System.exit(0);
 +  }
 +
 +  private void verifyAndClearStatus() throws Exception {
 +    final Status status = getStatus();
 +
 +    if (status != null && status.state != SHUTDOWN) {
 +      throw new IllegalStateException(LocalizedStrings.CacheServerLauncher_A_0_IS_ALREADY_RUNNING_IN_DIRECTORY_1_2
 +        .toLocalizedString(this.baseName, workingDir, status));
 +    }
 +
 +    deleteStatus();
 +  }
 +
 +  private String[] buildCommandLine(final Map<String, Object> options) {
 +    final List<String> commandLine = JavaCommandBuilder.buildCommand(this.getClass().getName(),
 +      (String) options.get(CLASSPATH), null, (List<String>) options.get(VMARGS));
 +
 +    commandLine.add("server");
 +    addToServerCommand(commandLine, options);
 +
 +    return commandLine.toArray(new String[commandLine.size()]);
 +  }
 +
 +  private void printCommandLine(final String[] commandLine) {
 +    if (PRINT_LAUNCH_COMMAND) {
 +      System.out.println("Starting " + this.baseName + " with command:");
 +      for (final String command : commandLine) {
 +        System.out.print(command);
 +        System.out.print(" ");
 +      }
 +      System.out.println();
 +    }
 +  }
 +
 +  private int runCommandLine(final Map<String, Object> options, final String[] commandLine) throws Exception {
 +    final File startLogFile = new File(workingDir, startLogFileName).getAbsoluteFile(); // see bug 32548
 +
 +    if (startLogFile.exists() && !startLogFile.delete()) {
 +      throw new IOException("Unable to delete start log file (" + startLogFile.getAbsolutePath() + ")!");
 +    }
 +
 +    Map<String, String> env = new HashMap<String, String>();
 +    // read the passwords from command line
 +    SocketCreator.readSSLProperties(env);
 +    
 +    printCommandLine(commandLine);
 +
 +    final int pid = OSProcess.bgexec(commandLine, workingDir, startLogFile, false, env);
 +
 +    printStartMessage(options, pid);
 +
 +    return pid;
 +  }
 +
 +  protected void printStartMessage(final Map<String, Object> options, final int pid) throws Exception {
 +    System.out.println(LocalizedStrings.CacheServerLauncher_STARTING_0_WITH_PID_1.toLocalizedString(this.baseName, pid));
 +  }
 +
 +  /**
 +   * Sets the status of the cache server to be {@link #RUNNING}.
 +   */
 +  public void running() {
 +    try {
 +      writeStatus(createStatus(this.baseName, RUNNING, OSProcess.getId()));
 +    }
 +    catch (Exception e) {
 +      e.printStackTrace();
 +    }
 +  }
 +
 +  public static ThreadLocal<Integer> serverPort = new ThreadLocal<Integer>();
 +
 +  public static ThreadLocal<String> serverBindAddress = new ThreadLocal<String>();
 +
 +  public static Integer getServerPort() {
 +    return serverPort.get();
 +  }
 +
 +  public static String getServerBindAddress() {
 +    return serverBindAddress.get();
 +  }
 +
 +  public static ThreadLocal<Boolean> disableDefaultServer = new ThreadLocal<Boolean>();
 +
 +  public static Boolean getDisableDefaultServer() {
 +    return disableDefaultServer.get();
 +  }
 +
 +  /**
 +   * The method that does the work of being a cache server.  It is
 +   * invoked in the VM spawned by the {@link #start} method.
 +   * Basically, it creates a GemFire {@link Cache} based on
 +   * configuration passed in from the command line.  (It will also
 +   * take <code>gemfire.properties</code>, etc. into account, just
 +   * like an application cache.)
 +   *
 +   * <P>
 +   *
 +   * After creating the cache and setting the server's status to {@link
 +   * #RUNNING}, it periodically monitors the status, waiting for it to
 +   * change to {@link #SHUTDOWN_PENDING} (see {@link #stop}).  When
 +   * the status does change, it closes the <code>Cache</code> and sets
 +   * the status to be {@link #SHUTDOWN}.
 +   *
 +   * @param args Configuration options passed in from the command line
 +   */
 +  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
 +  public void server(final String[] args) throws Exception {
 +    isDedicatedCacheServer = true;
 +    SystemFailure.setExitOK(true);
 +
 +    final Map<String, Object> options = getServerOptions(args);
 +
 +    final String serverPortString = (String) options.get(SERVER_PORT);
 +
 +    if (serverPortString != null) {
 +      serverPort.set(Integer.parseInt(serverPortString));
 +    }
 +
 +    serverBindAddress.set((String) options.get(SERVER_BIND_ADDRESS));
 +    disableDefaultServer.set((Boolean) options.get(DISABLE_DEFAULT_SERVER));
 +    workingDir = new File(System.getProperty("user.dir"));
 +
 +    // Say that we're starting...
 +    Status originalStatus = createStatus(this.baseName, STARTING, OSProcess.getId());
 +    status = originalStatus;
 +    writeStatus(status);
 +
 +    // Connect to the distributed system.  The properties will
 +    // properly configure logging, the declarative caching file, etc.
 +    final Properties props = (Properties) options.get(PROPERTIES);
 +
 +    if (props.getProperty(DistributionConfig.LOG_FILE_NAME) == null && CacheServerLauncher.isLoggingToStdOut()) {
 +      // Check First if the gemfire.properties set the log-file. If they do, we shouldn't override that default
 +      final Properties gemfireProperties = new Properties();
 +
 +      DistributionConfigImpl.loadGemFireProperties(gemfireProperties);
 +
 +      if (gemfireProperties.get(DistributionConfig.LOG_FILE_NAME) == null) {
 +        // Do not allow the cache server to log to stdout, override the logger with #defaultLogFileName
 +        props.setProperty(DistributionConfig.LOG_FILE_NAME, defaultLogFileName);
 +      }
 +    }
 +
 +    InternalDistributedSystem system = this.connect(props);
 +
 +    installLogListener();
 +    
 +    logger = system.getLogWriter().convertToLogWriterI18n();
 +    // redirect output to the log file
 +    OSProcess.redirectOutput(system.getConfig().getLogFile());
 +
 +    Cache cache = this.createCache(system, options);
 +    cache.setIsServer(true);
 +    startAdditionalServices(cache, options);
 +
 +    this.running();
 +    
 +    clearLogListener();
 +
 +    if (ASSIGN_BUCKETS) {
 +      for (PartitionedRegion region : ((GemFireCacheImpl) cache).getPartitionedRegions()) {
 +        PartitionRegionHelper.assignBucketsToPartitions(region);
 +      }
 +    }
 +
 +    if (Boolean.TRUE.equals(options.get(REBALANCE))) {
 +      cache.getResourceManager().createRebalanceFactory().start();
 +    }
 +
 +    File statusFile = new File( workingDir, statusName );
 +    long lastModified=0, oldModified = statusFile.lastModified();
 +    // Every FORCE_STATUS_FILE_READ_ITERATION_COUNT iterations, read the status file despite the modification time
 +    // to catch situations where the file is modified quicker than the file timestamp's resolution.
 +    short count = 0;
 +    boolean loggedWarning = false;
 +    while(true) {
 +      lastModified = statusFile.lastModified();
 +      if (lastModified > oldModified || count++ == FORCE_STATUS_FILE_READ_ITERATION_COUNT) {
 +        count = 0;
 +        Thread.sleep( 500 ); // allow for it to be finished writing.
 +        //Sometimes the status file is partially written causing readObject to
 +        //fail, sleep and retry.
 +        try {
 +          status = readStatus( );
 +        } catch(IOException ioeSecondChance) {
 +          Thread.sleep(1000);
 +          try {
 +            status = readStatus( );
 +          } catch(IOException ioeThirdChance) {
 +            Thread.sleep(5000);
 +            try {
 +              status = readStatus( );
 +            } catch (FileNotFoundException fnfe) {
 +              // See bug 44627.
 +              // The cache server used to just shutdown at this point. Instead,
 +              // recreate the status file if possible and continue.
 +              status = createStatus(this.baseName, RUNNING, originalStatus.pid);
 +              try {
 +                writeStatus(status);
 +              } catch (FileNotFoundException e) {
 +                if (!loggedWarning) {
 +                  logger.warning(LocalizedStrings.CacheServerLauncher_CREATE_STATUS_EXCEPTION_0, e.toString());
 +                  loggedWarning = true;
 +                }
 +              }
 +            }
 +          }
 +        }
 +        oldModified = lastModified;
 +        if (status.state == SHUTDOWN_PENDING) {
 +            stopAdditionalServices();
 +            this.disconnect(cache);
 +            status.state = SHUTDOWN;
 +            writeStatus(status);
 +        } else {
 +          Thread.sleep( 250 );
 +        }
 +
 +      } else {
 +        Thread.sleep(1000);
 +      }
 +      if (!system.isConnected()) {
 +//        System.out.println("System is disconnected.  isReconnecting = " + system.isReconnecting());
 +        boolean reconnected = false;
 +        if (system.isReconnecting()) {
 +          reconnected = system.waitUntilReconnected(-1, TimeUnit.SECONDS);
 +          if (reconnected) {
 +            system = (InternalDistributedSystem)system.getReconnectedSystem();
 +            cache = GemFireCacheImpl.getInstance();
 +          }
 +        }
 +        if (!reconnected) {
 +          // shutdown-all disconnected the DS
 +          System.exit(0);
 +        }
 +      }
 +    }
 +  }
 +
 +  private void installLogListener() {
 +    MainLogReporter reporter = new MainLogReporter(this.status);
 +    StartupStatus.setListener(reporter);
 +    reporter.setDaemon(true);
 +    reporter.start();
 +  }
 +  
 +  private void clearLogListener() {
 +    MainLogReporter mainLogListener = (MainLogReporter) StartupStatus.getStartupListener();
 +    if(mainLogListener != null) {
 +      mainLogListener.shutdown();
 +      StartupStatus.clearListener();
 +    }
 +  }
 +  
 +  protected InternalDistributedSystem connect(Properties props) {
 +    return (InternalDistributedSystem)DistributedSystem.connect(props);
 +  }
 +
 +  protected static float getCriticalHeapPercent(Map<String, Object> options) {
 +    if (options != null) {
 +      String criticalHeapThreshold = (String)options
 +          .get(CRITICAL_HEAP_PERCENTAGE);
 +      if (criticalHeapThreshold != null) {
 +        return Float.parseFloat(criticalHeapThreshold
 +            .substring(criticalHeapThreshold.indexOf("=") + 1));
 +      }
 +    }
 +    return -1.0f;
 +  }
 +  
 +  protected static float getEvictionHeapPercent(Map<String, Object> options) {
 +    if (options != null) {
 +      String evictionHeapThreshold = (String)options
 +          .get(EVICTION_HEAP_PERCENTAGE);
 +      if (evictionHeapThreshold != null) {
 +        return Float.parseFloat(evictionHeapThreshold
 +            .substring(evictionHeapThreshold.indexOf("=") + 1));
 +      }
 +    }
 +    return -1.0f;
 +  }
 +  
 +  protected static float getCriticalOffHeapPercent(Map<String, Object> options) {
 +    if (options != null) {
 +      String criticalOffHeapThreshold = (String)options
 +          .get(CRITICAL_OFF_HEAP_PERCENTAGE);
 +      if (criticalOffHeapThreshold != null) {
 +        return Float.parseFloat(criticalOffHeapThreshold
 +            .substring(criticalOffHeapThreshold.indexOf("=") + 1));
 +      }
 +    }
 +    return -1.0f;
 +  }
 +  
 +  protected static float getEvictionOffHeapPercent(Map<String, Object> options) {
 +    if (options != null) {
 +      String evictionOffHeapThreshold = (String)options
 +          .get(EVICTION_OFF_HEAP_PERCENTAGE);
 +      if (evictionOffHeapThreshold != null) {
 +        return Float.parseFloat(evictionOffHeapThreshold
 +            .substring(evictionOffHeapThreshold.indexOf("=") + 1));
 +      }
 +    }
 +    return -1.0f;
 +  }
 +  
 +  protected Cache createCache(InternalDistributedSystem system, Map<String, Object> options) throws IOException {
 +    Cache cache = CacheFactory.create(system);
 +
 +    float threshold = getCriticalHeapPercent(options);
 +    if (threshold > 0.0f) {
 +      cache.getResourceManager().setCriticalHeapPercentage(threshold);
 +    }
 +    threshold = getEvictionHeapPercent(options);
 +    if (threshold > 0.0f) {
 +      cache.getResourceManager().setEvictionHeapPercentage(threshold);
 +    }
 +    
 +    threshold = getCriticalOffHeapPercent(options);
 +    getCriticalOffHeapPercent(options);
 +    if (threshold > 0.0f) {
 +      cache.getResourceManager().setCriticalOffHeapPercentage(threshold);
 +    }
 +    threshold = getEvictionOffHeapPercent(options);
 +    if (threshold > 0.0f) {
 +      cache.getResourceManager().setEvictionOffHeapPercentage(threshold);
 +    }
 +
 +
 +    // Create and start a default cache server
 +    // If (disableDefaultServer is not set or it is set but false) AND (the number of cacheservers is 0)
 +    Boolean disable = disableDefaultServer.get();
 +    if ((disable == null || !disable) && cache.getCacheServers().size() == 0) {
 +      // Create and add a cache server
 +      CacheServer server = cache.addCacheServer();
++      
++      CacheServerHelper.setIsDefaultServer(server);
 +
 +      // Set its port if necessary
 +      Integer serverPort = CacheServerLauncher.getServerPort();
 +      if (serverPort != null) {
 +        server.setPort(serverPort);
 +      }
 +
 +      // Set its bind address if necessary
 +      String serverBindAddress = getServerBindAddress();
 +      if (serverBindAddress != null) {
 +        server.setBindAddress(serverBindAddress.trim());
 +      }
 +
 +      // Start it
 +      server.start();
 +    }
 +
 +    return cache;
 +  }
 +
 +  protected void disconnect(Cache cache) {
 +    DistributedSystem dsys = cache.getDistributedSystem();
 +    cache.close();
 +    dsys.disconnect();
 +  }
 +
 +  /**
 +   * Stops a cache server (which is running in a different VM) by setting its status to {@link #SHUTDOWN_PENDING}.
 +   * Waits for the cache server to actually shut down.
 +   */
 +  public void stop(final String[] args) throws Exception {
 +    this.workingDir = (File) getStopOptions(args).get(DIR);
 +
 +    // determine the current state of the Cache Server process...
 +    final File statusFile = new File(this.workingDir, this.statusName);
 +    int exitStatus = 1;
 +
 +    if (statusFile.exists()) {
 +      this.status = spinReadStatus();
 +
 +      // upon reading the status file, request the Cache Server to shutdown if it has not already...
 +      if (this.status.state != SHUTDOWN) {
 +        // copy server PID and not use own PID; see bug #39707
 +        this.status = createStatus(this.baseName, SHUTDOWN_PENDING, this.status.pid);
 +        writeStatus(this.status);
 +      }
 +
 +      // poll the Cache Server for a response to our shutdown request (passes through if the Cache Server
 +      // has already shutdown)...
 +      pollCacheServerForShutdown();
 +
 +      // after polling, determine the status of the Cache Server one last time and determine how to exit...
 +      if (this.status.state == SHUTDOWN) {
 +        System.out.println(LocalizedStrings.CacheServerLauncher_0_STOPPED.toLocalizedString(this.baseName));
 +        deleteStatus();
 +        exitStatus = 0;
 +      }
 +      else {
 +        System.out.println(LocalizedStrings.CacheServerLauncher_TIMEOUT_WAITING_FOR_0_TO_SHUTDOWN_STATUS_IS_1
 +          .toLocalizedString(this.baseName, this.status));
 +      }
 +    }
 +    else {
 +      System.out.println(LocalizedStrings.CacheServerLauncher_THE_SPECIFIED_WORKING_DIRECTORY_0_CONTAINS_NO_STATUS_FILE
 +        .toLocalizedString(this.workingDir));
 +    }
 +
 +    if (DONT_EXIT_AFTER_LAUNCH) {
 +      return;
 +    }
 +
 +    System.exit(exitStatus);
 +  }
 +
 +  private void pollCacheServerForShutdown() throws InterruptedException {
 +    final int increment = 250; // unit is in milliseconds
 +    int clock = 0;
 +
 +    // wait for a total of 20000 milliseconds (or 20 seconds)
 +    while (clock < SHUTDOWN_WAIT_TIME && status.state != SHUTDOWN) {
 +      try {
 +        status = readStatus();
 +      }
 +      catch (IOException ignore) {
 +      }
 +
 +      try {
 +        Thread.sleep(increment);
 +      }
 +      catch (InterruptedException ie) {
 +        break;
 +      }
 +
 +      clock += increment;
 +    }
 +  }
 +
 +  /**
 +   * A class that represents the status of a cache server.  Instances
 +   * of this class are serialized to a {@linkplain #statusName file}
 +   * on disk.
 +   *
 +   * @see #SHUTDOWN
 +   * @see #STARTING
 +   * @see #RUNNING
 +   * @see #SHUTDOWN_PENDING
 +   */
 +  static class Status implements Serializable {
 +
 +    private static final long serialVersionUID = 190943081363646485L;
 +    public int state = 0;
 +    public int pid = 0;
 +
 +    private final String baseName;
 +    public Throwable exception;
 +    public String msg;
 +    public String dsMsg;
 +
 +    public Status(String baseName) {
 +      this.baseName = baseName;
 +    }
 +
 +    @Override
 +    public String toString() {
 +      final StringBuilder buffer = new StringBuilder();
 +      buffer.append(this.baseName).append(" pid: ").append(pid).append(" status: ");
 +      switch (state) {
 +        case SHUTDOWN:
 +          buffer.append("stopped");
 +          break;
 +        case STARTING:
 +          buffer.append("starting");
 +          break;
 +        case RUNNING:
 +          buffer.append("running");
 +          break;
 +        case SHUTDOWN_PENDING:
 +          buffer.append("stopping");
 +          break;
 +        default:
 +          buffer.append("unknown");
 +          break;
 +      }
 +      if (exception != null) {
 +        if (msg != null) {
 +          buffer.append("\n").append(msg).append(" - ");
 +        }
 +        else {
 +          buffer.append("\nException in ").append(this.baseName).append(" - ");
 +        }
 +        buffer.append(LocalizedStrings
 +            .CacheServerLauncher_SEE_LOG_FILE_FOR_DETAILS.toLocalizedString());
 +      }
 +      else if (this.dsMsg != null) {
 +        buffer.append('\n').append(this.dsMsg);
 +      }
 +      return buffer.toString();
 +    }
 +  }
 +
 +  /**
 +   * Notes that an error has occurred in the cache server and that it
 +   * has shut down because of it.
 +   */
 +  protected void setServerError(final String msg, final Throwable t) {
 +    try {
 +      writeStatus(createStatus(this.baseName, SHUTDOWN, OSProcess.getId(), msg, t));
 +    }
 +    catch (Exception e) {
 +      if (logger != null) {
 +        logger.severe(e);
 +      }
 +      else {
 +        e.printStackTrace();
 +      }
 +      System.exit(1);
 +    }
 +  }
 +
 +  /**
 +   * Sets the status of a cache server by serializing a <code>Status</code>
 +   * instance to a file in the server's working directory.
 +   */
 +  public void writeStatus(final Status s) throws IOException {
 +    FileOutputStream fileOutput = null;
 +    ObjectOutputStream objectOutput = null;
 +
 +    try {
 +      fileOutput = new FileOutputStream(new File(workingDir, statusName));
 +      objectOutput = new ObjectOutputStream(fileOutput);
 +      objectOutput.writeObject(s);
 +      objectOutput.flush();
 +    }
 +    finally {
 +      IOUtils.close(objectOutput);
 +      IOUtils.close(fileOutput);
 +    }
 +  }
 +
 +  /**
 +   * Reads a cache server's status.  If the status file cannot be read because of I/O problems, it will try again.
 +   */
 +  protected Status spinReadStatus() {
 +    final long timeout = (System.currentTimeMillis() + 60000);
 +    Status status = null;
 +
 +    while (status == null && System.currentTimeMillis() < timeout) {
 +      try {
 +        status = readStatus();
 +      }
 +      catch (Exception e) {
 +        // try again - the status might have been read in the middle of it being written by the server resulting in
 +        // an EOFException here
 +        try {
 +          Thread.sleep(500);
 +        }
 +        catch (InterruptedException ie) {
 +          Thread.currentThread().interrupt();
 +          status = null;
 +          break;
 +        }
 +      }
 +    }
 +
 +    return status;
 +  }
 +
 +  /**
 +   * Reads a cache server's status from a file in its working directory.
 +   */
 +  protected Status readStatus() throws InterruptedException, IOException {
 +    final File statusFile = new File(workingDir, statusName);
 +
 +    FileInputStream fileInput = null;
 +    ObjectInputStream objectInput = null;
 +
 +    try {
 +      fileInput = new FileInputStream(statusFile);
 +      objectInput = new ObjectInputStream(fileInput);
 +
 +      Status status = (Status) objectInput.readObject();
 +
 +      // See bug 32760
 +      // Note, only execute the conditional createStatus statement if we are in native mode; if we are in pure Java mode
 +      // the the process ID identified in the Status object is assumed to exist!
 +      if (!isExistingProcess(status.pid)) {
 +        status = createStatus(this.baseName, SHUTDOWN, status.pid);
 +      }
 +
 +      return status;
 +    }
 +    catch (ClassNotFoundException e) {
 +      throw new RuntimeException(e);
 +    }
 +    catch (FileNotFoundException e) {
 +      Thread.sleep(500);
 +
 +      if (statusFile.exists()) {
 +        return readStatus();
 +      }
 +      else {
 +        throw e;
 +      }
 +    }
 +    finally {
 +      IOUtils.close(objectInput);
 +      IOUtils.close(fileInput);
 +    }
 +  }
 +
 +  /**
 +   * Removes a cache server's status file
 +   */
 +  private void deleteStatus() throws IOException {
 +    final File statusFile = new File(workingDir, statusName);
 +
 +    if (statusFile.exists() && !statusFile.delete()) {
 +      throw new IOException("Could not delete status file (" + statusFile.getAbsolutePath() + ")!");
 +    }
 +  }
 +
 +  protected boolean isExistingProcess(final int pid) {
 +    return (PureJavaMode.isPure() || (pid != 0 && OSProcess.exists(pid)));
 +  }
 +
 +  protected void waitForRunning() throws Exception {
 +    Status status = spinReadStatus();
 +    String lastReadMessage = null;
 +    String lastReportedMessage = null;
 +    long lastReadTime = System.nanoTime();
 +    if ( status == null ) {
 +      throw new Exception(LocalizedStrings.CacheServerLauncher_NO_AVAILABLE_STATUS.toLocalizedString());
 +    } else {
 +      switch ( status.state  ) {
 +        case STARTING:
 +          // re-read status for a while...
 +          while( status.state == STARTING ) {
 +            Thread.sleep( 500 ); // fix for bug 36998
 +            status = spinReadStatus();
 +            
 +            //check to see if the status message has changed
 +            if(status.dsMsg != null && !status.dsMsg.equals(lastReadMessage)) {
 +              lastReadMessage = status.dsMsg;
 +              lastReadTime = System.nanoTime();
 +            }
 +            
 +            //if the status message has not changed for 15 seconds, print
 +            //out the message.
 +            long elapsed = System.nanoTime() - lastReadTime;
 +            if(TimeUnit.NANOSECONDS.toMillis(elapsed) > STATUS_WAIT_TIME
 +                && lastReadMessage != null &&
 +                !lastReadMessage.equals(lastReportedMessage)) {
 +              long elapsedSec = TimeUnit.NANOSECONDS.toSeconds(elapsed);
 +              System.out.println(LocalizedStrings.CacheServerLauncher_LAUNCH_IN_PROGRESS_0
 +                  .toLocalizedString(elapsedSec, status.dsMsg));
 +              lastReportedMessage = lastReadMessage;
 +            }
 +          }
 +          if (status.state == SHUTDOWN) {
 +            System.out.println(status);
 +            System.exit(1);
 +          }
 +          break;
 +        default:
 +          break;
 +      }
 +      System.out.println( status );
 +    }
 +  }
 +
 +  /**
 +   * Reads {@link DistributedSystem#PROPERTY_FILE} and determines if the
 +   * {@link DistributionConfig#LOG_FILE_NAME} property is set to stdout
 +   * @return true if the logging would go to stdout
 +   */
 +  private static boolean isLoggingToStdOut() {
 +    Properties gfprops = new Properties();
 +    URL url = DistributedSystem.getPropertyFileURL();
 +    if (url != null) {
 +      try {
 +        gfprops.load(url.openStream());
 +      } catch (IOException io) {
 +        //throw new GemFireIOException("Failed reading " + url, io);
 +        System.out.println("Failed reading " + url);
 +        System.exit( 1 );
 +      }
 +      final String logFile = gfprops.getProperty(DistributionConfig.LOG_FILE_NAME);
 +      if ( logFile == null || logFile.length() == 0 ) {
 +        return true;
 +      }
 +    } else {
 +      //Didnt find a property file, assuming the default is to log to stdout
 +      return true;
 +    }
 +    return false;
 +  }
 +
 +  /**
 +   * Process information contained in the options map and add to the command
 +   * line of the subprocess as needed.
 +   */
 +  protected void addToServerCommand(final List<String> commandLine, final Map<String, Object> options) {
 +    final ListWrapper<String> commandLineWrapper = new ListWrapper<String>(commandLine);
 +
 +    if (Boolean.TRUE.equals(options.get(REBALANCE))) {
 +      commandLineWrapper.add("-rebalance");
 +    }
 +
 +    commandLineWrapper.add((String) options.get(DISABLE_DEFAULT_SERVER));
 +    commandLineWrapper.add((String) options.get(SERVER_PORT));
 +    commandLineWrapper.add((String) options.get(SERVER_BIND_ADDRESS));
 +
 +    String criticalHeapThreshold = (String)options.get(CRITICAL_HEAP_PERCENTAGE);
 +    if (criticalHeapThreshold != null) {
 +      commandLineWrapper.add(criticalHeapThreshold);
 +    }
 +    String evictionHeapThreshold = (String)options
 +        .get(EVICTION_HEAP_PERCENTAGE);
 +    if (evictionHeapThreshold != null) {
 +      commandLineWrapper.add(evictionHeapThreshold);
 +    }
 +    
 +    String criticalOffHeapThreshold = (String)options.get(CRITICAL_OFF_HEAP_PERCENTAGE);
 +    if (criticalOffHeapThreshold != null) {
 +      commandLineWrapper.add(criticalOffHeapThreshold);
 +    }
 +    String evictionOffHeapThreshold = (String)options
 +        .get(EVICTION_OFF_HEAP_PERCENTAGE);
 +    if (evictionOffHeapThreshold != null) {
 +      commandLineWrapper.add(evictionOffHeapThreshold);
 +    }
 +
 +    final Properties props = (Properties) options.get(PROPERTIES);
 +
 +    for (final Object key : props.keySet()) {
 +      commandLineWrapper.add(key + "=" + props.getProperty(key.toString()));
 +    }
 +
 +    if (props.getProperty(DistributionConfig.LOG_FILE_NAME) == null && CacheServerLauncher.isLoggingToStdOut()) {
 +      // Do not allow the cache server to log to stdout; override the logger with #defaultLogFileName
 +      commandLineWrapper.add(DistributionConfig.LOG_FILE_NAME + "=" + defaultLogFileName);
 +    }
 +  }
 +
 +  /**
 +   * This method is called immediately following cache creation in the spawned
 +   * process, but prior to setting the RUNNING flag in the status file. So the
 +   * spawning process will block until this method completes.
 +   */
 +  protected void startAdditionalServices(final Cache cache, final Map<String, Object> options) throws Exception {
 +  }
 +
 +  /**
 +   * This method is called prior to DistributedSytstem.disconnect(). Care should
 +   * be taken not to take too long in this method or else
 +   * #CacheServerLauncher.stop may timeout.
 +   */
 +  protected void stopAdditionalServices() throws Exception {
 +  }
 +
 +  /**
 +   * A List implementation that disallows null values.
 +   * @param <E> the Class type for the List elements.
 +   */
 +  protected static class ListWrapper<E> extends AbstractList<E>  {
 +
 +    private static final ThreadLocal<Boolean> addResult = new ThreadLocal<Boolean>();
 +
 +    private final List<E> list;
 +
 +    public ListWrapper(final List<E> list) {
 +      assert list != null : "The List cannot be null!";
 +      this.list = list;
 +    }
 +
 +    @Override
 +    public boolean add(final E e) {
 +      final boolean localAddResult = super.add(e);
 +      return (localAddResult && addResult.get());
 +    }
 +
 +    @Override
 +    public void add(final int index, final E element) {
 +      if (element != null) {
 +        list.add(index, element);
 +      }
 +      addResult.set(element != null);
 +    }
 +
 +    @Override
 +    public E get(final int index) {
 +      return this.list.get(index);
 +    }
 +
 +    @Override
 +    public E remove(final int index) {
 +      return list.remove(index);
 +    }
 +
 +    @Override
 +    public E set(final int index, final E element) {
 +      return (element != null ? list.set(index, element) : list.get(index));
 +    }
 +
 +    @Override
 +    public int size() {
 +      return list.size();
 +    }
 +  }
 +  
 +  private class MainLogReporter extends Thread implements StartupStatusListener {
 +    private String lastLogMessage;
 +    private final Status status;
 +    boolean running = true;
 +
 +    public MainLogReporter(Status status) {
 +      this.status = status;
 +    }
 +
 +    public synchronized void shutdown() {
 +      this.running = false;
 +      this.status.dsMsg = null;
 +      this.notifyAll();
 +    }
 +
 +    
 +    @Override
 +    public void setStatus(String status) {
 +      lastLogMessage = status;      
 +    }
 +
 +    public synchronized void run() {
 +      while(running) {
 +        try {
 +          wait(1000);
 +        } catch (InterruptedException e) {
 +          //this should not happen.
 +          break;
 +        }
 +        if(running && lastLogMessage != status.dsMsg) {
 +          status.dsMsg = lastLogMessage;
 +          try {
 +            writeStatus(status);
 +          } catch (IOException e) {
 +            //this could happen if there was a concurrent write to the file
 +            //eg a stop.
 +            continue;
 +          }
 +        }
 +      }
 +    }
 +  }
 +}


[28/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/CacheServerHelper.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/CacheServerHelper.java
index b120b57,0000000..b0b0be1
mode 100755,000000..100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/CacheServerHelper.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/CacheServerHelper.java
@@@ -1,187 -1,0 +1,193 @@@
 +/*
 + * 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.tier.sockets;
 +
++import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.internal.HeapDataOutputStream;
 +import com.gemstone.gemfire.internal.Version;
++import com.gemstone.gemfire.internal.cache.CacheServerImpl;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.util.BlobHelper;
 +
 +import java.io.ByteArrayInputStream;
 +import java.io.ByteArrayOutputStream;
 +import java.io.IOException;
 +import java.io.ObjectInputStream;
 +import java.io.ObjectOutputStream;
 +import java.io.UTFDataFormatException;
 +import java.io.UnsupportedEncodingException;
 +
 +import java.util.zip.GZIPInputStream;
 +import java.util.zip.GZIPOutputStream;
 +
 +/**
 + * <code>CacheServerHelper</code> is a static class that provides helper methods
 + * for the CacheServer classes.
 + *
 + * @author Barry Oglesby
 + * @since 3.5
 + */
- public class CacheServerHelper
-   {
-   public static byte[] serialize(Object obj) throws IOException
-   {
++public class CacheServerHelper {
++  
++  public static void setIsDefaultServer(CacheServer server) {
++    if (server instanceof CacheServerImpl) {
++      ((CacheServerImpl)server).setIsDefaultServer();
++    }
++  }
++  
++  public static boolean isDefaultServer(CacheServer server) {
++    if ( !(server instanceof CacheServerImpl) ) {
++      return false;
++    }
++    return ((CacheServerImpl)server).isDefaultServer();
++  }
++  
++  public static byte[] serialize(Object obj) throws IOException {
 +    return serialize(obj, false);
 +  }
 +
-   public static byte[] serialize(Object obj, boolean zipObject) throws IOException
-   {
++  public static byte[] serialize(Object obj, boolean zipObject) throws IOException {
 +    return zipObject
 +      ? zip(obj)
 +      : BlobHelper.serializeToBlob(obj);
 +  }
 +
-   public static Object deserialize(byte[] blob) throws IOException, ClassNotFoundException
-   {
++  public static Object deserialize(byte[] blob) throws IOException, ClassNotFoundException {
 +    return deserialize(blob, false);
 +  }
 +
-   public static Object deserialize(byte[] blob, boolean unzipObject) throws IOException, ClassNotFoundException
-   {
++  public static Object deserialize(byte[] blob, boolean unzipObject) throws IOException, ClassNotFoundException {
 +    return unzipObject
 +      ? unzip(blob)
 +      : BlobHelper.deserializeBlob(blob);
 +  }
 +
-   public static Object deserialize(byte[] blob, Version version, boolean unzipObject) throws IOException, ClassNotFoundException
-   {
++  public static Object deserialize(byte[] blob, Version version, boolean unzipObject) throws IOException, ClassNotFoundException {
 +    return unzipObject
 +      ? unzip(blob)
 +      : BlobHelper.deserializeBlob(blob, version, null);
 +  }
 +  
-   public static byte[] zip(Object obj) throws IOException
-   {
++  public static byte[] zip(Object obj) throws IOException {
 +//logger.info("CacheServerHelper: Zipping object to blob: " + obj);
 +    ByteArrayOutputStream baos = new ByteArrayOutputStream();
 +    GZIPOutputStream gz = new GZIPOutputStream(baos);
 +    ObjectOutputStream oos = new ObjectOutputStream(gz);
 +    oos.writeObject(obj);
 +    oos.flush();
 +    oos.close();
 +    byte[] blob = baos.toByteArray();
 +//logger.info("CacheServerHelper: Zipped object to blob: " + blob);
 +    return blob;
 +  }
 +
-   public static Object unzip(byte[] blob) throws IOException, ClassNotFoundException
-   {
++  public static Object unzip(byte[] blob) throws IOException, ClassNotFoundException {
 +//logger.info("CacheServerHelper: Unzipping blob to object: " + blob);
 +    ByteArrayInputStream bais = new ByteArrayInputStream(blob);
 +    GZIPInputStream gs = new GZIPInputStream(bais);
 +    ObjectInputStream ois = new ObjectInputStream(gs);
 +    Object obj = ois.readObject();
 +//logger.info("CacheServerHelper: Unzipped blob to object: " + obj);
 +    ois.close();
 +    bais.close();
 +    return obj;
 +  }
 +
 +
 +  /**
 +   * The logic used here is based on java's DataInputStream.writeUTF() from the version 1.6.0_10.
 +   * 
 +   * @param s
 +   * @return byte[]
 +   */
-   public static byte[] toUTF(String s)
-   {
++  public static byte[] toUTF(String s) {
 +    HeapDataOutputStream hdos = new HeapDataOutputStream(s);
 +    return hdos.toByteArray();
 +  }
 +
 +  /**
 +   * The logic used here is based on java's DataInputStream.readUTF() from the version 1.6.0_10.
 +   * @param bytearr
 +   * @return String 
 +   */
-   public static String fromUTF(byte[] bytearr)
-   {
++  public static String fromUTF(byte[] bytearr) {
 +    int utflen = bytearr.length;
 +    int c, char2, char3;
 +    int count = 0;
 +    int chararr_count=0;
 +
 +    char[] chararr = new char[utflen];
 +
 +    while (count < utflen) {
 +      c = (int) bytearr[count] & 0xff;
 +      if (c > 127) break;
 +      count++;
 +      chararr[chararr_count++]=(char)c;
 +    }
 +
 +    while (count < utflen) {
 +      c = (int) bytearr[count] & 0xff;
 +      switch (c >> 4) {
 +        case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
 +          /* 0xxxxxxx*/
 +          count++;
 +          chararr[chararr_count++]=(char)c;
 +          break;
 +        case 12: case 13:
 +          /* 110x xxxx   10xx xxxx*/
 +          count += 2;
 +          if (count > utflen) {
 +            throw new RuntimeException(LocalizedStrings.CacheServerHelper_UTF8_EXCEPTION.toLocalizedString(), 
 +                new UTFDataFormatException("malformed input: partial character at end"));
 +          }
 +          char2 = (int) bytearr[count-1];
 +          if ((char2 & 0xC0) != 0x80)
 +            throw new RuntimeException(
 +                "malformed input around byte " + count);
 +          chararr[chararr_count++]=(char)(((c & 0x1F) << 6) |
 +              (char2 & 0x3F));
 +          break;
 +        case 14:
 +          /* 1110 xxxx  10xx xxxx  10xx xxxx */
 +          count += 3;
 +          if (count > utflen){
 +            throw new RuntimeException(LocalizedStrings.CacheServerHelper_UTF8_EXCEPTION.toLocalizedString(), 
 +                new UTFDataFormatException("malformed input: partial character at end")); 
 +          }
 +          char2 = (int) bytearr[count-2];
 +          char3 = (int) bytearr[count-1];
 +          if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)){
 +            throw new RuntimeException(LocalizedStrings.CacheServerHelper_UTF8_EXCEPTION.toLocalizedString(), 
 +                new UTFDataFormatException("malformed input around byte " + (count-1)));
 +          }
 +          chararr[chararr_count++]=(char)(((c     & 0x0F) << 12) |
 +              ((char2 & 0x3F) << 6)  |
 +              ((char3 & 0x3F) << 0));
 +          break;
 +        default:
 +          /* 10xx xxxx,  1111 xxxx */
 +          throw new RuntimeException(LocalizedStrings.CacheServerHelper_UTF8_EXCEPTION.toLocalizedString(), 
 +              new UTFDataFormatException("malformed input around byte " + count));
 +      }
 +    }
 +    // The number of chars produced may be less than utflen
 +    return new String(chararr, 0, chararr_count);
 +  }
 +
 +  private CacheServerHelper() {}
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/Message.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/Message.java
index f6866bf,0000000..4bfd44b
mode 100755,000000..100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/Message.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/Message.java
@@@ -1,1190 -1,0 +1,1100 @@@
 +/*
 + * 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.tier.sockets;
 +
 +import java.io.EOFException;
 +import java.io.IOException;
 +import java.io.InputStream;
 +import java.io.OutputStream;
 +import java.net.Socket;
 +import java.net.SocketTimeoutException;
 +import java.nio.ByteBuffer;
 +import java.nio.channels.SocketChannel;
 +import java.util.concurrent.Semaphore;
 +import java.util.concurrent.TimeUnit;
 +
 +import org.apache.logging.log4j.Logger;
 +
 +import com.gemstone.gemfire.SerializationException;
 +import com.gemstone.gemfire.internal.Assert;
 +import com.gemstone.gemfire.internal.HeapDataOutputStream;
 +import com.gemstone.gemfire.internal.SocketUtils;
 +import com.gemstone.gemfire.internal.Version;
- import com.gemstone.gemfire.internal.cache.CachedDeserializable;
 +import com.gemstone.gemfire.internal.cache.TXManagerImpl;
 +import com.gemstone.gemfire.internal.cache.tier.MessageType;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.logging.LogService;
 +import com.gemstone.gemfire.internal.logging.log4j.LocalizedMessage;
 +import com.gemstone.gemfire.internal.offheap.StoredObject;
- import com.gemstone.gemfire.internal.offheap.annotations.Retained;
 +import com.gemstone.gemfire.internal.offheap.annotations.Unretained;
 +import com.gemstone.gemfire.internal.util.BlobHelper;
 +
 +/**
 + * This class encapsulates the wire protocol. It provides accessors to
 + * encode and decode a message and  serialize it out to the wire.
 + *
 + * <PRE>
 + * msgType       - int   - 4 bytes type of message, types enumerated below
 + *
 + * msgLength     - int - 4 bytes   total length of variable length payload
 + *
 + * numberOfParts - int - 4 bytes   number of elements (LEN-BYTE* pairs)
 + *                     contained in the payload. Message can
 + *                       be a multi-part message
 + *
 + * transId       - int - 4 bytes  filled in by the requestor, copied back into
 + *                    the response
 + *
-  * earlyAck      - byte- 1 byte   filled in by the requestor
++ * flags         - byte- 1 byte   filled in by the requestor
 + * len1
 + * part1
 + * .
 + * .
 + * .
 + * lenn
 + * partn
 + * </PRE>
 + *
 + * We read the fixed length 16 bytes into a byte[] and populate a bytebuffer
 + * We read the fixed length header tokens from the header
 + * parse the header and use information contained in there to read the payload.
 + *
 + * <P>
 + *
 + * See also <a href="package-summary.html#messages">package description</a>.
 + *
 + * @see com.gemstone.gemfire.internal.cache.tier.MessageType
 + *
-  * @author Sudhir Menon
-  * @since 2.0.2
 + */
 +public class Message  {
 +
 +  private static final Logger logger = LogService.getLogger();
 +  
-   @Override
-   public String toString() {
-     StringBuffer sb = new StringBuffer();
-     sb.append("type=" + MessageType.getString(msgType));
-     sb.append("; payloadLength=" + payloadLength);
-     sb.append("; numberOfParts=" + numberOfParts);
-     sb.append("; transactionId=" + transactionId);
-     //sb.append("; bufferLength=" + bufferLength);
-     sb.append("; currentPart=" + currentPart);
-     sb.append("; messageModified=" + messageModified);
-     sb.append("; earlyAck=" + earlyAck);
-     for (int i = 0; i < numberOfParts; i ++) {
-       sb.append("; part[" + i + "]={");
-       sb.append(this.partsList[i].toString());
-       sb.append("}");
-     }
-     return sb.toString();
-   }
++  private static final int PART_HEADER_SIZE = 5; // 4 bytes for length, 1 byte for isObject
++  
++  private static final int FIXED_LENGTH = 17;
++
++  private static final ThreadLocal<ByteBuffer> tlCommBuffer = new ThreadLocal<>();
 +
-   protected final static int FIXED_LENGTH = 17;
 +  protected int msgType;
 +  protected int payloadLength=0;
 +  protected int numberOfParts =0;
 +  protected int transactionId = TXManagerImpl.NOTX;
 +  protected int currentPart = 0;
 +  protected Part[] partsList = null;
 +  protected ByteBuffer cachedCommBuffer;
 +  protected Socket socket = null;
 +  protected SocketChannel sockCh = null;
 +  protected OutputStream os = null;
 +  protected InputStream is = null;
 +  protected boolean messageModified = true;
 +  /** is this message a retry of a previously sent message? */
 +  protected boolean isRetry;
-   private byte earlyAck = 0x00;
++  private byte flags = 0x00;
 +  protected MessageStats msgStats = null;
 +  protected ServerConnection sc = null;
-   private int MAX_DATA = -1;
++  private int maxIncomingMessageLength = -1;
 +  private Semaphore dataLimiter = null;
 +//  private int MAX_MSGS = -1;
 +  private Semaphore msgLimiter = null;
 +  private boolean hdrRead = false;  
 +  private int chunkSize = 1024;//Default Chunk Size.
 +
 +  protected Part securePart = null;
++  private boolean isMetaRegion = false;
++
 +
-   // These two statics are fields shoved into the earlyAck byte for transmission.
++  // These two statics are fields shoved into the flags byte for transmission.
 +  // The MESSAGE_IS_RETRY bit is stripped out during deserialization but the other
 +  // is left in place
 +  public static final byte MESSAGE_HAS_SECURE_PART = (byte)0x02;
 +  public static final byte MESSAGE_IS_RETRY = (byte)0x04;
 +  
 +  public static final byte MESSAGE_IS_RETRY_MASK = (byte)0xFB;
 +
 +  // Tentative workaround to avoid OOM stated in #46754.
 +  public static final ThreadLocal<Integer> messageType = new ThreadLocal<Integer>();
 +  
 +  Version version;
 +  
 +  /**
 +   * Creates a new message with the given number of parts
 +   */
 +  public Message(int numberOfParts, Version destVersion) {
 +    this.version = destVersion;
 +    Assert.assertTrue(destVersion != null, "Attempt to create an unversioned message");
 +    partsList = new Part[numberOfParts];
 +    this.numberOfParts = numberOfParts;
 +    for (int i=0;i<partsList.length;i++) {
 +      partsList[i] = new Part();
 +    }
 +  }
 +
 +  public boolean isSecureMode() {    
 +    return securePart != null;
 +  }
 +  
 +  public byte[] getSecureBytes()
 +    throws IOException, ClassNotFoundException {
 +    return (byte[])this.securePart.getObject();
 +  }
 +  
 +  public void setMessageType(int msgType) {
 +    this.messageModified = true;
 +    if (!MessageType.validate(msgType)) {
 +      throw new IllegalArgumentException(LocalizedStrings.Message_INVALID_MESSAGETYPE.toLocalizedString());
 +    }
 +    this.msgType = msgType;
 +  }
 +  
 +  public void setVersion(Version clientVersion) {
 +    this.version = clientVersion;
 +  }
 +
-   /**
-    * Sets whether this message is early-ack
-    * @param earlyAck whether this message is early-ack
-    */
-   public void setEarlyAck(boolean earlyAck) {
-     if (earlyAck) {
-       this.earlyAck = 0x01;
-     } else {
-       this.earlyAck = 0x00;
-     }
++  public void setMessageHasSecurePartFlag() {
++    this.flags = (byte)(this.flags | MESSAGE_HAS_SECURE_PART);
 +  }
- 
-   // TODO (ashetkar) To be removed later.
-   public void setEarlyAck(byte earlyAck) {
-     // Check that the passed in value is within the acceptable range.
-     if (0x00 <= earlyAck && earlyAck <= 0x02) {
-       this.earlyAck |= earlyAck;
-     }
++  
++  public void clearMessageHasSecurePartFlag() {
++    this.flags = (byte)(this.flags & MESSAGE_HAS_SECURE_PART);
 +  }
 +
-   /*
-    * public void setPayloadLength(int payloadLength) {
-      this.payloadLength = payloadLength;
-   }*/
- 
 +  /**
 +   *  Sets and builds the {@link Part}s that are sent
 +   *  in the payload of the Message
 +   * @param numberOfParts
 +   */
 +  public void setNumberOfParts(int numberOfParts) {
 +    //TODO:hitesh need to add security header here from server
 +    //need to insure it is not chunked message
 +    //should we look message type to avoid internal message like ping
 +    this.messageModified = true;
 +    this.currentPart=0;
 +    this.numberOfParts = numberOfParts;
 +    if (numberOfParts > this.partsList.length) {
 +      Part[] newPartsList = new Part[numberOfParts];
 +      for (int i=0;i<numberOfParts;i++) {
 +        if (i < this.partsList.length) {
 +          newPartsList[i] = this.partsList[i];
 +        } else {
 +          newPartsList[i] = new Part();
 +        }
 +      }
 +      this.partsList = newPartsList;
 +    }
 +  }
 +
 +  public void setTransactionId(int transactionId) {
 +    this.messageModified = true;
 +    this.transactionId = transactionId;
 +  }
 +  
 +  public void setIsRetry() {
 +    this.isRetry = true;
 +  }
 +  
 +  /**
 +   * This returns true if the message has been marked as having been previously
 +   * transmitted to a different server.
 +   */
 +  public boolean isRetry() {
 +    return this.isRetry;
 +  }
 +
 +  /*Sets size for HDOS chunk.*/
 +  public void setChunkSize(int chunkSize) {
 +    this.chunkSize = chunkSize;
 +  }
++  
++  /**
++   * When building a Message this will return the number of the
++   * next Part to be added to the message
++   */
++  public int getNextPartNumber() {
++    return this.currentPart;
++  }
 +
 +  public void addStringPart(String str) {
 +    if (str==null) {
 +      addRawPart((byte[])null, false);
 +    }
 +    else {
 +      HeapDataOutputStream hdos = new HeapDataOutputStream(str);
 +      this.messageModified = true;
 +      Part part = partsList[this.currentPart];
 +      part.setPartState(hdos, false);
 +      this.currentPart++;
 +    }
 +  }
 +
-   /**
-    * Sets whether or not a
-    * <code>DataOutputStream</code>/<code>DataOutputStream</code>
-    * should be used to send/receive data.
-       public void setUseDataStream (boolean useDataStream) {
-         this.useDataStream = useDataStream;
-     }
-    */
- 
 +  /*
 +   * Adds a new part to this message that contains a <code>byte</code>
 +   * array (as opposed to a serialized object).
 +   *
 +   * @see #addPart(byte[], boolean)
 +   */
 +  public void addBytesPart(byte[] newPart) {
 +    addRawPart(newPart, false);
 +  }
 +
 +  public void addStringOrObjPart(Object o) {
 +    if (o instanceof String || o == null) {
 +      addStringPart((String)o);
 +    } else {
 +      // Note even if o is a byte[] we need to serialize it.
 +      // This could be cleaned up but it would require C client code to change.
 +      serializeAndAddPart(o, false);
 +    }
 +  }
 +
-   public void addDeltaPart(HeapDataOutputStream hdos) { // TODO: Amogh- Should it be just DataOutput?
++  public void addDeltaPart(HeapDataOutputStream hdos) {
 +    this.messageModified = true;
 +    Part part = partsList[this.currentPart];
 +    part.setPartState(hdos, false);
 +    this.currentPart++;
 +  }
 +
 +  public void addObjPart(Object o) {
 +    addObjPart(o, false);
 +  }
 +  /**
 +   * Like addObjPart(Object) but also prefers to reference
 +   * objects in the part instead of copying them into a byte buffer.
 +   */
 +  public void addObjPartNoCopying(Object o) {
 +    if (o == null || o instanceof byte[]) {
 +      addRawPart((byte[])o, false);
 +    } else {
 +      serializeAndAddPartNoCopying(o);
 +    }
 +  }
 +  public void addObjPart(Object o, boolean zipValues) {
 +    if (o == null || o instanceof byte[]) {
 +      addRawPart((byte[])o, false);
 +    } else {
 +      serializeAndAddPart(o, zipValues);
 +    }
 +  }
 +  public void addPartInAnyForm(@Unretained Object o, boolean isObject) {
 +    if (o == null) {
 +      addRawPart((byte[])o, false);
 +    } else if (o instanceof byte[]) {
 +      addRawPart((byte[])o, isObject);
 +    } else if (o instanceof StoredObject) {
 +      // It is possible it is an off-heap StoredObject that contains a simple non-object byte[].
 +      this.messageModified = true;
 +      Part part = partsList[this.currentPart];
 +      part.setPartState((StoredObject)o, isObject);
 +      this.currentPart++;
 +    } else {
 +      serializeAndAddPart(o, false);
 +    }
 +  }
 +  
 +  private void serializeAndAddPartNoCopying(Object o) {
 +    HeapDataOutputStream hdos;
 +    Version v = version;
 +    if (version.equals(Version.CURRENT)){
 +      v = null;
 +    }
 +    // create the HDOS with a flag telling it that it can keep any byte[] or ByteBuffers/ByteSources passed to it.
 +    hdos = new HeapDataOutputStream(chunkSize, v, true);
 +    // TODO OFFHEAP: Change Part to look for an HDOS and just pass a reference to its DirectByteBuffer.
 +    // Then change HDOS sendTo(SocketChannel...) to use the GatheringByteChannel to write a bunch of bbs.
 +    // TODO OFFHEAP This code optimizes one part which works pretty good for getAll since all the values are
 +    // returned in one part. But the following seems even better...
 +    // BETTER: change Message to consolidate all the part hdos bb lists into a single bb array and have it do the GatheringByteChannel write.
 +    // Message can use slice for the small parts (msg header and part header) that are not in the parts data (its a byte array, Chunk, or HDOS).
 +    // EVEN BETTER: the message can have a single HDOS which owns a direct comm buffer. It can reserve space if it does not yet know the value to write (for example the size of the message or part).
 +    // If we write something to the HDOS that is direct then it does not need to be copied.
 +    // But large heap byte arrays will need to be copied to the hdos (the socket write does this anyway).
 +    // If the direct buffer is full then we can allocate another one. If a part is already in a heap byte array
 +    // then we could defer copying it by slicing the current direct bb and then adding the heap byte array
 +    // as bb using ByteBuffer.wrap. Once we have all the data in the HDOS we can finally generate the header
 +    // and then start working on sending the ByteBuffers to the channel. If we have room in a direct bb then
 +    // we can copy a heap bb to it. Otherwise we can write the bb ahead of it which would free up room to copy
 +    // the heap bb to the existing direct bb without needing to allocate extra direct bbs.
 +    // Delaying the flush uses more direct memory but reduces the number of system calls.
 +    try {
- //      logger.fine("hitesh before serializatino: " );
- //      
- //      if (o != null ){
- //        logger.fine("hitesh before serializatino: " + o.toString());
- //        logger.fine("hitesh before serializatino: " + o.getClass().getName());
- //      }
 +      BlobHelper.serializeTo(o, hdos);
 +    } catch (IOException ex) {
 +      throw new SerializationException("failed serializing object", ex);
 +    }
 +    this.messageModified = true;
 +    Part part = partsList[this.currentPart];
 +    part.setPartState(hdos, true);
 +    this.currentPart++;
 +    
 +  }
 +
 +  private void serializeAndAddPart(Object o, boolean zipValues) {
 +    if (zipValues) {
 +      throw new UnsupportedOperationException("zipValues no longer supported");    
 +      
- //       byte[] b = CacheServerHelper.serialize(o, zipValues);
- //       addRawPart(b, true);
 +    } else {
 +      HeapDataOutputStream hdos;
 +      Version v = version;
 +      if (version.equals(Version.CURRENT)){
 +        v = null;
 +      }
 +      hdos = new HeapDataOutputStream(chunkSize, v);
 +      try {
- //        logger.fine("hitesh before serializatino: " );
- //        
- //        if (o != null ){
- //          logger.fine("hitesh before serializatino: " + o.toString());
- //          logger.fine("hitesh before serializatino: " + o.getClass().getName());
- //        }
 +        BlobHelper.serializeTo(o, hdos);
 +      } catch (IOException ex) {
 +        throw new SerializationException("failed serializing object", ex);
 +      }
 +      this.messageModified = true;
 +      Part part = partsList[this.currentPart];
 +      part.setPartState(hdos, true);
 +      this.currentPart++;
 +    }
 +  }
 +
 +  public void addIntPart(int v) {
 +    this.messageModified = true;
 +    Part part = partsList[this.currentPart];
 +    part.setInt(v);
 +    this.currentPart++;
 +  }
 +  
 +  public void addLongPart(long v) {
 +    this.messageModified = true;
 +    Part part = partsList[this.currentPart];
 +    part.setLong(v);
 +    this.currentPart++;
 +  }
 +  
 +  /**
 +   * Adds a new part to this message that may contain a serialized
 +   * object.
 +   */
 +  public void addRawPart(byte[] newPart,boolean isObject) {
 +    this.messageModified = true;
 +    Part part = partsList[this.currentPart];
 +    part.setPartState(newPart, isObject);
 +    this.currentPart++;
 +  }
 +
 +  public int getMessageType() {
 +    return this.msgType;
 +  }
 +
 +  public int getPayloadLength() {
 +    return this.payloadLength;
 +  }
 +
 +  public int getHeaderLength() {
 +    return FIXED_LENGTH;
 +  }
 +
 +  public int getNumberOfParts() {
 +    return this.numberOfParts;
 +  }
 +
 +  public int getTransactionId() {
 +    return this.transactionId;
 +  }
 +  
 +  public Part getPart(int index) {
 +    if (index < this.numberOfParts) {
 +      Part p = partsList[index];
 +      if (this.version != null) {
 +        p.setVersion(this.version);
 +      }
 +      return p;
 +    }
 +    return null;
 +  }
 +
-   public boolean getEarlyAck() {
-     return this.earlyAck == 0x01 ? true: false;
-   }
- 
-   // TODO (ashetkar) To be removed
-   public byte getEarlyAckByte() {
-     return this.earlyAck;
-   }
- 
-   private static ThreadLocal tlCommBuffer = new ThreadLocal();
- 
 +  public static ByteBuffer setTLCommBuffer(ByteBuffer bb) {
-     ByteBuffer result = (ByteBuffer)tlCommBuffer.get();
++    ByteBuffer result = tlCommBuffer.get();
 +    tlCommBuffer.set(bb);
 +    return result;
 +  }
 +
 +  public ByteBuffer getCommBuffer() {
 +    if (this.cachedCommBuffer != null) {
 +      return this.cachedCommBuffer;
 +    }
 +    else {
-       return (ByteBuffer)tlCommBuffer.get();
++      return tlCommBuffer.get();
 +    }
 +  }
 +
 +  public void clear() {
 +    this.isRetry = false;
 +    int len = this.payloadLength;
 +    if (len != 0) {
 +      this.payloadLength = 0;
 +    }
 +    if (this.hdrRead) {
 +      if (this.msgStats != null) {
 +        this.msgStats.decMessagesBeingReceived(len);
 +      }
 +    }
-     if (this.socket != null) {
-       getCommBuffer().clear();
++    ByteBuffer buffer = getCommBuffer();
++    if (buffer != null) {
++      buffer.clear();
 +    }
 +    clearParts();
 +    if (len != 0 && this.dataLimiter != null) {
 +      this.dataLimiter.release(len);
 +      this.dataLimiter = null;
-       this.MAX_DATA = 0;
++      this.maxIncomingMessageLength = 0;
 +    }
 +    if (this.hdrRead) {
 +      if (this.msgLimiter != null) {
 +        this.msgLimiter.release(1);
 +        this.msgLimiter = null;
 +      }
 +      this.hdrRead = false;
 +    }
++    this.flags = 0;
 +  }
 +
 +  protected void packHeaderInfoForSending(int msgLen, boolean isSecurityHeader) {
-     //TODO:hitesh setting second bit of early ack for client 
++    //TODO:hitesh setting second bit of flags byte for client 
 +    //this is not require but this makes all changes easily at client side right now
 +    //just see this bit and process security header
-     byte eAck = this.earlyAck;
++    byte flagsByte = this.flags;
 +    if (isSecurityHeader) {
-       eAck |= MESSAGE_HAS_SECURE_PART;
++      flagsByte |= MESSAGE_HAS_SECURE_PART;
 +    }
 +    if (this.isRetry) {
-       eAck |= MESSAGE_IS_RETRY;
++      flagsByte |= MESSAGE_IS_RETRY;
 +    }
 +    getCommBuffer()
 +      .putInt(this.msgType)
 +      .putInt(msgLen)
 +      .putInt(this.numberOfParts)
 +      .putInt(this.transactionId)
-       .put(eAck);
++      .put(flagsByte);
 +  }
 +
-   private static final int PART_HEADER_SIZE = 5; // 4 bytes for length, 1 byte for isObject
-   
 +  protected Part getSecurityPart() {
 +    if (this.sc != null ) {
 +      //look types right put get etc
 +     return this.sc.updateAndGetSecurityPart(); 
 +    }
 +    return null;
 +  }
 +
 +  public void setSecurePart(byte[] bytes) {
 +    this.securePart = new Part();
 +    this.securePart.setPartState(bytes, false);
 +  }
 +
-   private boolean m_isMetaRegion = false;
- 
 +  public void setMetaRegion(boolean isMetaRegion) {
-     this.m_isMetaRegion = isMetaRegion;
++    this.isMetaRegion = isMetaRegion;
 +  }
 +
 +  public boolean getAndResetIsMetaRegion() {
-     boolean isMetaRegion = this.m_isMetaRegion;
-     this.m_isMetaRegion = false;
++    boolean isMetaRegion = this.isMetaRegion;
++    this.isMetaRegion = false;
 +    return isMetaRegion;
 +  }
 +
 +  /**
 +   * Sends this message out on its socket.
 +   */
 +  protected void sendBytes(boolean clearMessage) throws IOException {
 +    if (this.sc != null) {
 +      // Keep track of the fact that we are making progress.
 +      this.sc.updateProcessingMessage();
 +    }
 +    if (this.socket != null) {
 +      final ByteBuffer cb = getCommBuffer();
 +      if (cb == null) {
 +        throw new IOException("No buffer");
 +      }
 +      synchronized(cb) {
 +        int numOfSecureParts = 0;
 +        Part securityPart = this.getSecurityPart();
 +        boolean isSecurityHeader = false;
 +        
 +        if (securityPart != null) {
 +          isSecurityHeader = true;
 +          numOfSecureParts = 1;
 +        }
 +        else if (this.securePart != null) {
 +          // This is a client sending this message.
 +          securityPart = this.securePart;
 +          isSecurityHeader = true;
 +          numOfSecureParts = 1;          
 +        }
 +
-         //this.logger.fine("hitesh sendbytes forServer_SecurityPart " + numOfSecureParts);
 +        int totalPartLen = 0;
 +        for (int i=0;i<this.numberOfParts;i++){
 +          Part part = this.partsList[i];
 +          totalPartLen += part.getLength();
 +        }
 +
 +        if(numOfSecureParts == 1) {
 +          totalPartLen += securityPart.getLength();
 +        }
 +        int msgLen = (PART_HEADER_SIZE * (this.numberOfParts + numOfSecureParts)) + totalPartLen;
 +        cb.clear();
 +        packHeaderInfoForSending(msgLen, isSecurityHeader);
 +        for (int i=0;i<this.numberOfParts + numOfSecureParts;i++) {
 +          Part part = null;
 +          if(i == this.numberOfParts) {
 +            part = securityPart;
 +          }
 +          else {
 +            part = partsList[i];
 +          }
 +          if (cb.remaining() < PART_HEADER_SIZE) {
 +            flushBuffer();
 +          }
 +          int partLen = part.getLength();
 +          cb.putInt(partLen);
 +          cb.put(part.getTypeCode());
 +          if (partLen <= cb.remaining()) {
 +            part.sendTo(cb);
 +          } else {
 +            flushBuffer();
 +            // send partBytes
 +            if (this.sockCh != null) {
 +              part.sendTo(this.sockCh, cb);
 +            } else {
 +              part.sendTo(this.os, cb);
 +            }
 +            if (this.msgStats != null) {
 +              this.msgStats.incSentBytes(partLen);
 +            }
 +          }
 +        }
 +        if (cb.position() != 0) {
 +          flushBuffer();
 +        }
 +        this.messageModified = false;
 +        if (this.sockCh == null) {
 +          this.os.flush();
 +        }
 +      }
 +      if(clearMessage) {
 +        clearParts();
 +      }
 +    }
 +    else {
 +      throw new IOException(LocalizedStrings.Message_DEAD_CONNECTION.toLocalizedString());
 +    }
 +  }
 +
 +  protected void flushBuffer() throws IOException {
 +    final ByteBuffer cb = getCommBuffer();
 +    if (this.sockCh != null) {
 +      cb.flip();
 +      do {
 +        this.sockCh.write(cb);
 +      } while (cb.remaining() > 0);
 +    } else {
 +      this.os.write(cb.array(), 0, cb.position());
 +    }
 +    if (this.msgStats != null) {
 +      this.msgStats.incSentBytes(cb.position());
 +    }
 +    cb.clear();
 +  }
 +
 +  private void read()
 +  throws IOException {
 +    clearParts();
 +    //TODO:Hitesh ??? for server changes make sure sc is not null as this class also used by client :(
 +    readHeaderAndPayload();
 +  }
 +
 +  /**
 +   * Read the actual bytes of the header off the socket
 +   */
 +  protected final void fetchHeader() throws IOException {
 +    final ByteBuffer cb = getCommBuffer();
 +    cb.clear();
 +    // msgType is invalidated here and can be used as an indicator
 +    // of problems reading the message
 +    this.msgType = MessageType.INVALID;
 +
 +    int hdr = 0;
 +
 +    final int headerLength = getHeaderLength();
 +    if (this.sockCh != null) {
 +      cb.limit(headerLength);
 +      do {
 +        int bytesRead = this.sockCh.read(cb);
 +        //System.out.println("DEBUG: fetchHeader read " + bytesRead + " bytes commBuffer=" + cb);
 +        if (bytesRead == -1) {
 +          throw new EOFException(LocalizedStrings.Message_THE_CONNECTION_HAS_BEEN_RESET_WHILE_READING_THE_HEADER.toLocalizedString());
 +        }
 +        if (this.msgStats != null) {
 +          this.msgStats.incReceivedBytes(bytesRead);
 +        }
 +      } while (cb.remaining() > 0);
 +      cb.flip();
 +    } else {
 +      do {
 +        int bytesRead = -1;
 +        try {
 +          bytesRead = this.is.read(cb.array(),hdr, headerLength-hdr);
 +        }
 +        catch (SocketTimeoutException e) {
 +//          bytesRead = 0;
 +          // TODO add a cancellation check
 +          throw e;
 +        }
 +        if (bytesRead == -1) {
 +          throw new EOFException(LocalizedStrings.Message_THE_CONNECTION_HAS_BEEN_RESET_WHILE_READING_THE_HEADER.toLocalizedString());
 +        }
 +        hdr += bytesRead;
 +        if (this.msgStats != null) {
 +          this.msgStats.incReceivedBytes(bytesRead);
 +        }
 +      } while (hdr < headerLength);
 +
 +      // now setup the commBuffer for the caller to parse it
 +      cb.rewind();
 +    }
 +  }
 +
 +  private void readHeaderAndPayload()
 +  throws IOException {
 +    //TODO:Hitesh ???
 +    fetchHeader();
 +    final ByteBuffer cb = getCommBuffer();
 +    final int type = cb.getInt();
 +    final int len = cb.getInt();
 +    final int numParts = cb.getInt();
 +    final int txid = cb.getInt();
-     byte early = cb.get();
++    byte bits = cb.get();
 +    cb.clear();
 +
 +    if (!MessageType.validate(type)) {
 +      throw new IOException(LocalizedStrings.Message_INVALID_MESSAGE_TYPE_0_WHILE_READING_HEADER.toLocalizedString(Integer.valueOf(type)));
 +    }
 +    int timeToWait = 0;
 +    if (this.sc != null) {
 +      // Keep track of the fact that a message is being processed.
 +      this.sc.setProcessingMessage();
 +      timeToWait = sc.getClientReadTimeout();
 +    }
 +    this.hdrRead = true;
 +    if (this.msgLimiter != null) {
 +        for (;;) {
 +          this.sc.getCachedRegionHelper().checkCancelInProgress(null);
 +          boolean interrupted = Thread.interrupted();
 +          try {
 +            if (timeToWait == 0) {
 +              this.msgLimiter.acquire(1);
 +            } 
 +            else {
 +              if (!this.msgLimiter.tryAcquire(1, timeToWait, TimeUnit.MILLISECONDS)) {
 +                if (this.msgStats != null
 +                    && this.msgStats instanceof CacheServerStats) {
 +                  ((CacheServerStats)this.msgStats).incConnectionsTimedOut();
 +                }
 +                throw new IOException(LocalizedStrings.Message_OPERATION_TIMED_OUT_ON_SERVER_WAITING_ON_CONCURRENT_MESSAGE_LIMITER_AFTER_WAITING_0_MILLISECONDS.toLocalizedString(Integer.valueOf(timeToWait)));
 +              }
 +            }
 +            break;
 +          }
 +          catch (InterruptedException e) {
 +            interrupted = true;
 +          }
 +          finally {
 +            if (interrupted) {
 +              Thread.currentThread().interrupt();
 +            }
 +          }
 +        } // for
 +    }
 +    if (len > 0) {
-       if (this.MAX_DATA > 0 && len > this.MAX_DATA) {
-         throw new IOException(LocalizedStrings.Message_MESSAGE_SIZE_0_EXCEEDED_MAX_LIMIT_OF_1.toLocalizedString(new Object[] {Integer.valueOf(len), Integer.valueOf(this.MAX_DATA)}));
++      if (this.maxIncomingMessageLength > 0 && len > this.maxIncomingMessageLength) {
++        throw new IOException(LocalizedStrings.Message_MESSAGE_SIZE_0_EXCEEDED_MAX_LIMIT_OF_1.toLocalizedString(new Object[] {Integer.valueOf(len), Integer.valueOf(this.maxIncomingMessageLength)}));
 +      }
 +      if (this.dataLimiter != null) {
 +        for (;;) {
 +          if (sc != null) {
 +            this.sc.getCachedRegionHelper().checkCancelInProgress(null);
 +          }
 +          boolean interrupted = Thread.interrupted();
 +          try {
 +            if (timeToWait == 0) {
 +              this.dataLimiter.acquire(len);
 +            } 
 +            else {
 +              int newTimeToWait = timeToWait;
 +              if (this.msgLimiter != null) {
 +                // may have waited for msg limit so recalc time to wait
 +                newTimeToWait -= (int)sc.getCurrentMessageProcessingTime();
 +              }
 +              if (newTimeToWait <= 0 || !this.msgLimiter.tryAcquire(1, newTimeToWait, TimeUnit.MILLISECONDS)) {
 +                throw new IOException(LocalizedStrings.Message_OPERATION_TIMED_OUT_ON_SERVER_WAITING_ON_CONCURRENT_DATA_LIMITER_AFTER_WAITING_0_MILLISECONDS.toLocalizedString(timeToWait));
 +              }
 +            }
 +            this.payloadLength = len; // makes sure payloadLength gets set now so we will release the semaphore
 +            break; // success
 +          }
 +          catch (InterruptedException e) {
 +            interrupted = true;
 +          }
 +          finally {
 +            if (interrupted) {
 +              Thread.currentThread().interrupt();
 +            }
 +          }
 +        }
 +      }
 +    }
 +    if (this.msgStats != null) {
 +      this.msgStats.incMessagesBeingReceived(len);
 +      this.payloadLength = len; // makes sure payloadLength gets set now so we will dec on clear
 +    }
 +    
-     this.isRetry = (early & MESSAGE_IS_RETRY) != 0;
-     early = (byte)(early & MESSAGE_IS_RETRY_MASK);
- 
-     //TODO:hitesh it was below ??
-     this.earlyAck = early;
++    this.isRetry = (bits & MESSAGE_IS_RETRY) != 0;
++    bits = (byte)(bits & MESSAGE_IS_RETRY_MASK);
++    this.flags = bits;
++    // TODO why is the msgType set twice, here and after reading the payload fields?
 +    this.msgType = type;
-     //this.logger.fine("Before reading message parts, earlyAck already read as " + this.earlyAck);
++
 +    readPayloadFields(numParts, len);
 +
 +    // Set the header and payload fields only after receiving all the
 +    // socket data, providing better message consistency in the face
 +    // of exceptional conditions (e.g. IO problems, timeouts etc.)
 +    this.msgType = type;
 +    this.payloadLength = len;
 +    // this.numberOfParts = numParts;  Already set in setPayloadFields via setNumberOfParts
 +    this.transactionId = txid;
-     this.earlyAck = early;
++    this.flags = bits;
 +    if (this.sc != null) {
 +      // Keep track of the fact that a message is being processed.
 +      this.sc.updateProcessingMessage();
 +    }
 +  }
 +
- //   static final int MAX_PART_BUFFERS = 2;
- //   static final int MIN_PART_BUFFER_SIZE = 999;
- //   static final int MAX_PART_BUFFER_SIZE = 1024*1024*11;
- //   static ArrayList partBuffers = new ArrayList(2);
- //   static int partBufferIdx = 0;
- //   static {
- //     for (int i=0; i < MAX_PART_BUFFERS; i++) {
- //       partBuffers.add(i, null);
- //     }
- //   }
- 
- //   private static synchronized byte[] getPartBuffer(int size) {
- //     byte[] result;
- //     synchronized (partBuffers) {
- //       result = (byte[])partBuffers.get(partBufferIdx);
- //       if (result == null) {
- //         result = new byte[size];
- //         partBuffers.add(partBufferIdx, result);
- //       } else if (result.length != size) {
- //         // can't use a cached one
- //         return null;
- //       }
- //       partBufferIdx++;
- //       if (partBufferIdx >= MAX_PART_BUFFERS) {
- //         partBufferIdx = 0;
- //       }
- //     }
- //     return result;
- //   }
- 
 +  protected void readPayloadFields(final int numParts, final int len)
 +  throws IOException {
 +    //TODO:Hitesh
 +    if (len > 0 && numParts <= 0 ||
 +        len <= 0 && numParts > 0) {
 +      throw new IOException(LocalizedStrings.Message_PART_LENGTH_0_AND_NUMBER_OF_PARTS_1_INCONSISTENT.toLocalizedString(
 +            new Object[] {Integer.valueOf(len), Integer.valueOf(numParts)}));
 +    }
 +
 +    Integer msgType = messageType.get();
 +    if (msgType != null && msgType == MessageType.PING) {
 +      messageType.set(null); // set it to null right away.
 +      int pingParts = 10; // Some number which will not throw OOM but still be acceptable for a ping operation.
 +      if (numParts > pingParts) {
 +        throw new IOException("Part length ( " + numParts
 +            + " ) is  inconsistent for " + MessageType.getString(msgType)
 +            + " operation.");
 +      }
 +    }
 +    setNumberOfParts(numParts);
 +    if (numParts <= 0)
 +      return;
 +  
 +    if (len < 0) {
 +      logger.info(LocalizedMessage.create(LocalizedStrings.Message_RPL_NEG_LEN__0, len));
 +      throw new IOException(LocalizedStrings.Message_DEAD_CONNECTION.toLocalizedString());
 +    }    
 +    
 +    final ByteBuffer cb = getCommBuffer();
 +    cb.clear();
 +    cb.flip();
 +
 +    int readSecurePart = 0;
 +    //TODO:Hitesh look if securePart can be cached here
-     //this.logger.fine("readPayloadFields() early ack = " + this.earlyAck);
 +    readSecurePart = checkAndSetSecurityPart();
 +    
 +    int bytesRemaining = len;
-     //this.logger.fine("readPayloadFields() : numParts=" + numParts + " len=" + len);
 +    for (int i = 0; ((i < numParts + readSecurePart) || ((readSecurePart == 1) && (cb
 +        .remaining() > 0))); i++) {
 +      int bytesReadThisTime = readPartChunk(bytesRemaining);
 +      bytesRemaining -= bytesReadThisTime;
 +
 +      Part part;
 +      
 +      if(i < numParts) {
 +        part = this.partsList[i];
 +      }
 +      else {
 +        part = this.securePart;
 +      }
 +      
 +      int partLen = cb.getInt();
 +      byte partType = cb.get();
 +      byte[] partBytes = null;
- //      this.logger.fine("readPayloadFields(): partLen=" + partLen + " partType=" + partType);
 +      if (partLen > 0) {
- //         if (partLen >= MIN_PART_BUFFER_SIZE && partLen <= MAX_PART_BUFFER_SIZE) {
- //           partBytes = getPartBuffer(partLen);
- //         }
- //         if (partBytes == null) {
-           partBytes = new byte[partLen];
- //         }
++        partBytes = new byte[partLen];
 +        int alreadyReadBytes = cb.remaining();
 +        if (alreadyReadBytes > 0) {
 +          if (partLen < alreadyReadBytes) {
 +            alreadyReadBytes = partLen;
 +          }
 +          cb.get(partBytes, 0, alreadyReadBytes);
 +        }
 +        // now we need to read partLen - alreadyReadBytes off the wire
 +        int off = alreadyReadBytes;
 +        int remaining = partLen - off;
 +        while (remaining > 0) {
 +          if (this.sockCh != null) {
 +            int bytesThisTime = remaining;
 +            cb.clear();
 +            if (bytesThisTime > cb.capacity()) {
 +              bytesThisTime = cb.capacity();
 +            }
 +            cb.limit(bytesThisTime);
 +            int res = this.sockCh.read(cb);
-             //System.out.println("DEBUG: part read " + res + " bytes commBuffer=" + cb);
 +            if (res != -1) {
 +              cb.flip();
 +              bytesRemaining -= res;
 +              remaining -= res;
 +              cb.get(partBytes, off, res);
 +              off += res;
 +              if (this.msgStats != null) {
 +                this.msgStats.incReceivedBytes(res);
 +              }
 +            } else {
 +              throw new EOFException(LocalizedStrings.Message_THE_CONNECTION_HAS_BEEN_RESET_WHILE_READING_A_PART.toLocalizedString());
 +            }
 +          } else {
 +            int res = 0;
 +            try {
 +              res = this.is.read(partBytes, off, remaining);
 +            }
 +            catch (SocketTimeoutException e) {
- //              res = 0;
 +              // TODO: add cancellation check
 +              throw e;
 +            }
 +            if (res != -1) {
 +              bytesRemaining -= res;
 +              remaining -= res;
 +              off += res;
 +              if (this.msgStats != null) {
 +                this.msgStats.incReceivedBytes(res);
 +              }
 +            } else {
 +              throw new EOFException(LocalizedStrings.Message_THE_CONNECTION_HAS_BEEN_RESET_WHILE_READING_A_PART.toLocalizedString());
 +            }
 +          }
 +        }
 +      }
 +      part.init(partBytes, partType);
 +    }
 +  }
 +
 +  protected int checkAndSetSecurityPart() {
-     if ((this.earlyAck | MESSAGE_HAS_SECURE_PART) == this.earlyAck) {
++    if ((this.flags | MESSAGE_HAS_SECURE_PART) == this.flags) {
 +      this.securePart = new Part();
 +      return 1;
 +    }
 +    else {
 +      this.securePart = null;
 +      return 0;
 +    }
 +  }
 +
 +  /**
 +   * @param bytesRemaining the most bytes we can read
 +   * @return the number of bytes read into commBuffer
 +   */
 +  private int readPartChunk(int bytesRemaining) throws IOException {
 +    final ByteBuffer cb = getCommBuffer();
-     //this.logger.info("DEBUG: commBuffer.remaining=" + cb.remaining());
 +    if (cb.remaining() >= PART_HEADER_SIZE) {
 +      // we already have the next part header in commBuffer so just return
 +      return 0;
 +    }
 +    if (cb.position() != 0) {
 +      cb.compact();
 +    } else {
 +      cb.position(cb.limit());
 +      cb.limit(cb.capacity());
 +    }
 +    int bytesRead = 0;
 +    if (this.sc != null) {
 +      // Keep track of the fact that we are making progress
 +      this.sc.updateProcessingMessage();
 +    }
 +    if (this.sockCh != null) {
 +      int remaining = cb.remaining();
 +      if (remaining > bytesRemaining) {
 +        remaining = bytesRemaining;
 +        cb.limit(cb.position()+bytesRemaining);
 +      }
 +      while (remaining > 0) {
 +        int res = this.sockCh.read(cb);
-         //System.out.println("DEBUG: partChunk read " + res + " bytes commBuffer=" + cb);
 +        if (res != -1) {
 +          remaining -= res;
 +          bytesRead += res;
 +          if (this.msgStats != null) {
 +            this.msgStats.incReceivedBytes(res);
 +          }
 +        } else {
 +          throw new EOFException(LocalizedStrings.Message_THE_CONNECTION_HAS_BEEN_RESET_WHILE_READING_THE_PAYLOAD.toLocalizedString());
 +        }
 +      }
 +
 +    } else {
 +      int bufSpace = cb.capacity() - cb.position();
 +      int bytesToRead = bufSpace;
 +      if (bytesRemaining < bytesToRead) {
 +        bytesToRead = bytesRemaining;
 +      }
 +      int pos = cb.position();
 +      while (bytesToRead > 0) {
 +        int res = 0;
 +        try {
 +          res = this.is.read(cb.array(), pos, bytesToRead);
 +        }
 +        catch (SocketTimeoutException e) {
- //          res = 0;
 +          // TODO add a cancellation check
 +          throw e;
 +        }
 +        if (res != -1) {
 +          bytesToRead -= res;
 +          pos += res;
 +          bytesRead += res;
 +          if (this.msgStats != null) {
 +            this.msgStats.incReceivedBytes(res);
 +          }
 +        } else {
 +          throw new EOFException(LocalizedStrings.Message_THE_CONNECTION_HAS_BEEN_RESET_WHILE_READING_THE_PAYLOAD.toLocalizedString());
 +        }
 +      }
 +      cb.position(pos);
 +    }
 +    cb.flip();
 +    return bytesRead;
 +  }
 +
 +  /**
 +   * Gets rid of all the parts that have been added to this message.
 +   */
 +  public void clearParts() {
 +    for (int i=0; i< partsList.length; i++){
 +      partsList[i].clear();
 +    }
 +    this.currentPart=0;
 +  }
++
++  @Override
++  public String toString() {
++    StringBuffer sb = new StringBuffer();
++    sb.append("type=").append(MessageType.getString(msgType));
++    sb.append("; payloadLength=").append(payloadLength);
++    sb.append("; numberOfParts=").append(numberOfParts);
++    sb.append("; transactionId=").append(transactionId);
++    sb.append("; currentPart=").append(currentPart);
++    sb.append("; messageModified=").append(messageModified);
++    sb.append("; flags=").append(Integer.toHexString(flags));
++    for (int i = 0; i < numberOfParts; i ++) {
++      sb.append("; part[").append(i).append("]={");
++      sb.append(this.partsList[i].toString());
++      sb.append("}");
++    }
++    return sb.toString();
++  }
++
++  
 +  public void setComms(ServerConnection sc, Socket socket, ByteBuffer bb, MessageStats msgStats) throws IOException {
 +    this.sc = sc;
 +    setComms(socket, bb, msgStats);
 +  }
 +
 +  public void setComms(Socket socket, ByteBuffer bb, MessageStats msgStats) throws IOException {
 +    this.sockCh = socket.getChannel();
 +    if (this.sockCh == null) {
 +      setComms(socket, SocketUtils.getInputStream(socket), SocketUtils.getOutputStream(socket), bb, msgStats);
 +    } else {
 +      setComms(socket, null, null,  bb, msgStats);
 +    }
 +  }
 +  
 +  public void setComms(Socket socket, InputStream is, OutputStream os, ByteBuffer bb, MessageStats msgStats)
 +    throws IOException
 +  {
 +    Assert.assertTrue(socket != null);
 +    this.socket = socket;
 +    this.sockCh = socket.getChannel();
 +    this.is = is;
 +    this.os = os;
 +    this.cachedCommBuffer = bb;
 +    this.msgStats = msgStats;
 +  }
 +  /**
 +   * Undo any state changes done by setComms.
 +   * @since 5.7
 +   */
 +  public void unsetComms() {
 +    this.socket = null;
 +    this.sockCh = null;
 +    this.is = null;
 +    this.os = null;
 +    this.cachedCommBuffer = null;
 +    this.msgStats = null;
 +  }
 +
 +  /**
 +   * Sends this message to its receiver over its
 +   * setOutputStream?? output stream.
 +   */
 +  public void send()
 +  throws IOException {
 +    send(true);
 +  }
 +  
 +  public void send(ServerConnection servConn)
 +  throws IOException {
 +    if (this.sc != servConn) throw new IllegalStateException("this.sc was not correctly set");
 +    send(true);
 +  }
 +  
 +  /**
 +   * Sends this message to its receiver over its
 +   * setOutputStream?? output stream.
 +   */
 +  public void send(boolean clearMessage)
 +  throws IOException {
 +    sendBytes(clearMessage);
 +  }
 +
 +  /**
 +   *  Populates the stats of this <code>Message</code> with information
 +   *  received via its socket
 +   */
 +  public void recv()
 +  throws IOException {
 +    if (this.socket != null) {
 +      synchronized(getCommBuffer()) {
 +        read();
 +      }
 +    }
 +    else {
 +      throw new IOException(LocalizedStrings.Message_DEAD_CONNECTION.toLocalizedString());
 +    }
 +  }
-   public void recv(ServerConnection sc, int MAX_DATA, Semaphore dataLimiter, int MAX_MSGS, Semaphore msgLimiter)
++  public void recv(ServerConnection sc, int maxMessageLength, Semaphore dataLimiter, Semaphore msgLimiter)
 +  throws IOException {
 +    this.sc = sc;
-     this.MAX_DATA = MAX_DATA;
++    this.maxIncomingMessageLength = maxMessageLength;
 +    this.dataLimiter = dataLimiter;
- //    this.MAX_MSGS = MAX_MSGS;
 +    this.msgLimiter = msgLimiter;
 +    recv();
 +  }
 +
-   public boolean canStartRemoteTransaction() {
-     return true;
-   }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/Part.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/Part.java
index 5418c68,0000000..f5f6326
mode 100755,000000..100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/Part.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/Part.java
@@@ -1,457 -1,0 +1,452 @@@
 +/*
 + * 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.tier.sockets;
 +
 +import com.gemstone.gemfire.internal.*;
 +import com.gemstone.gemfire.internal.cache.CachedDeserializable;
 +import com.gemstone.gemfire.internal.offheap.Chunk;
 +import com.gemstone.gemfire.internal.offheap.DataAsAddress;
 +import com.gemstone.gemfire.internal.offheap.StoredObject;
 +import com.gemstone.gemfire.internal.offheap.UnsafeMemoryChunk;
 +
 +import java.io.*;
 +import java.nio.*;
 +import java.nio.channels.*;
 +
 +/**
 + * Represents one unit of information (essentially a <code>byte</code>
 + * array) in the wire protocol.  Each server connection runs in its
 + * own thread to maximize concurrency and improve response times to
 + * edge requests
 + *
 + * @see Message
 + *
 + * @author Sudhir Menon
 + * @since 2.0.2
 + */
 +public class Part {
 +  private static final byte BYTE_CODE = 0;
 +  private static final byte OBJECT_CODE = 1;
 +  
 +  private Version version;
 +  /**
 +   * Used to represent and empty byte array for bug 36279
 +   * @since 5.1
 +   */
 +  private static final byte EMPTY_BYTEARRAY_CODE = 2;
 +  private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
 +
 +  /** The payload of this part.
 +   * Could be null, a byte[] or a HeapDataOutputStream on the send side.
 +   * Could be null, or a byte[] on the receiver side.
 +   */
 +  private Object part;
 +
 +  /** Is the payload (<code>part</code>) a serialized object? */
 +  private byte typeCode;
 +
 +  public void init(byte[] v, byte tc) {
 +    if (tc == EMPTY_BYTEARRAY_CODE) {
 +      this.part = EMPTY_BYTE_ARRAY;
 +    }
 +    else {
 +      this.part = v;
 +    }
 +    this.typeCode = tc;
 +  }
 +
- //   public void init(HeapDataOutputStream os, byte typeCode) {
- //     this.part = os;
- //     this.typeCode = typeCode;
- //   }
 +
 +  public void clear() {
 +    this.part = null;
 +    this.typeCode = BYTE_CODE;
 +  }
 +
 +  public boolean isNull() {
 +    if (this.part == null) {
 +      return true;
 +    }
 +    if (isObject() && this.part instanceof byte[]) {
 +      byte[] b = (byte[])this.part;
 +      if (b.length == 1 && b[0] == DSCODE.NULL) {
 +        return true;
 +      }
 +    }
 +    return false;
 +  }
 +  public boolean isObject() {
 +    return this.typeCode == OBJECT_CODE;
 +  }
 +  public boolean isBytes() {
 +    return this.typeCode == BYTE_CODE || this.typeCode == EMPTY_BYTEARRAY_CODE;
 +  }
- //   public boolean isString() {
- //     return this.typeCode == STRING_CODE;
- //   }
++
 +  public void setPartState(byte[] b, boolean isObject) {
 +    if (isObject) {
 +      this.typeCode = OBJECT_CODE;
 +    } else if (b != null && b.length == 0) {
 +      this.typeCode = EMPTY_BYTEARRAY_CODE;
 +      b = EMPTY_BYTE_ARRAY;
 +    } else {
 +      this.typeCode = BYTE_CODE;
 +    }
 +    this.part = b;
 +  }
++  
 +  public void setPartState(HeapDataOutputStream os, boolean isObject) {
 +    if (isObject) {
 +      this.typeCode = OBJECT_CODE;
 +      this.part = os;
 +    } else if (os != null && os.size() == 0) {
 +      this.typeCode = EMPTY_BYTEARRAY_CODE;
 +      this.part = EMPTY_BYTE_ARRAY;
 +    } else {
 +      this.typeCode = BYTE_CODE;
 +      this.part = os;
 +    }
 +  }
 +  public void setPartState(StoredObject so, boolean isObject) {
 +    if (isObject) {
 +      this.typeCode = OBJECT_CODE;
 +    } else if (so.getValueSizeInBytes() == 0) {
 +      this.typeCode = EMPTY_BYTEARRAY_CODE;
 +      this.part = EMPTY_BYTE_ARRAY;
 +      return;
 +    } else {
 +      this.typeCode = BYTE_CODE;
 +    }
 +    if (so instanceof DataAsAddress) {
 +      this.part = ((DataAsAddress)so).getRawBytes();
 +    } else {
 +      this.part = (Chunk)so;
 +    }
 +  }
 +  public byte getTypeCode() {
 +    return this.typeCode;
 +  }
 +  /**
 +   * Return the length of the part. The length is the number of bytes needed
 +   * for its serialized form.
 +   */
 +  public int getLength() {
 +    if (this.part == null) {
 +      return 0;
 +    } else if (this.part instanceof byte[]) {
 +      return ((byte[])this.part).length;
 +    } else if (this.part instanceof Chunk) {
 +      return ((Chunk) this.part).getValueSizeInBytes();
 +    } else {
 +      return ((HeapDataOutputStream)this.part).size();
 +    }
 +  }
 +  public String getString() {
 +    if (this.part == null) {
 +      return null;
 +    }
 +    if (!isBytes()) {
 +      Assert.assertTrue(false, "expected String part to be of type BYTE, part ="
 +          + this.toString());
 +    }
 +    return CacheServerHelper.fromUTF((byte[])this.part);
 +  }
 +  
 +  public int getInt() {
 +    if (!isBytes()) {
 +      Assert.assertTrue(false, "expected int part to be of type BYTE, part = "
 +          + this.toString()); 
 +    }
 +    if (getLength() != 4) {
 +      Assert.assertTrue(false, 
 +          "expected int length to be 4 but it was " + getLength()
 +          + "; part = " + this.toString());
 +    }
 +    byte[] bytes = getSerializedForm();
 +    return decodeInt(bytes, 0);
 +  }
 +
 +  public static int decodeInt(byte[] bytes, int offset) {
 +    return (((bytes[offset + 0]) << 24) & 0xFF000000)
 +        | (((bytes[offset + 1]) << 16) & 0x00FF0000)
 +        | (((bytes[offset + 2]) << 8) & 0x0000FF00)
 +        | ((bytes[offset + 3]) & 0x000000FF);
 +  }
 +
 +  public void setInt(int v) {
 +    byte[] bytes = new byte[4];
 +    encodeInt(v, bytes);
 +    this.typeCode = BYTE_CODE;
 +    this.part = bytes;
 +  }
 +
 +  /**
 +   * @since 5.7
 +   */
 +  public static void encodeInt(int v, byte[] bytes) {
 +    encodeInt(v, bytes, 0);
 +  }
 +
 +  public static void encodeInt(int v, byte[] bytes, int offset) {
 +    // encode an int into the given byte array
 +    bytes[offset + 0] = (byte) ((v & 0xFF000000) >> 24);
 +    bytes[offset + 1] = (byte) ((v & 0x00FF0000) >> 16);
 +    bytes[offset + 2] = (byte) ((v & 0x0000FF00) >> 8 );
 +    bytes[offset + 3] = (byte) (v & 0x000000FF);
 +  }
 +  
 +  public void setLong(long v) {
 +    byte[] bytes = new byte[8];
 +    bytes[0] = (byte) ((v & 0xFF00000000000000l) >> 56);
 +    bytes[1] = (byte) ((v & 0x00FF000000000000l) >> 48);
 +    bytes[2] = (byte) ((v & 0x0000FF0000000000l) >> 40);
 +    bytes[3] = (byte) ((v & 0x000000FF00000000l) >> 32);
 +    bytes[4] = (byte) ((v & 0x00000000FF000000l) >> 24);
 +    bytes[5] = (byte) ((v & 0x0000000000FF0000l) >> 16);
 +    bytes[6] = (byte) ((v & 0x000000000000FF00l) >>  8);
 +    bytes[7] = (byte) (v & 0xFF);
 +    this.typeCode = BYTE_CODE;
 +    this.part = bytes;
 +  }
 +
 +  public long getLong() {
 +    if (!isBytes()) {
 +      Assert.assertTrue(false, "expected long part to be of type BYTE, part = "
 +          + this.toString()); 
 +    }
 +    if (getLength() != 8) {
 +      Assert.assertTrue(false, 
 +          "expected long length to be 8 but it was " + getLength()
 +          + "; part = " + this.toString());
 +    }
 +    byte[] bytes = getSerializedForm();
 +    return ((((long)bytes[0]) << 56) & 0xFF00000000000000l) |
 +           ((((long)bytes[1]) << 48) & 0x00FF000000000000l) |
 +           ((((long)bytes[2]) << 40) & 0x0000FF0000000000l) |
 +           ((((long)bytes[3]) << 32) & 0x000000FF00000000l) |
 +           ((((long)bytes[4]) << 24) & 0x00000000FF000000l) |
 +           ((((long)bytes[5]) << 16) & 0x0000000000FF0000l) |
 +           ((((long)bytes[6]) <<  8) & 0x000000000000FF00l) |
 +           (        bytes[7]         & 0x00000000000000FFl);
 +  }
 +
 +
 +  public byte[] getSerializedForm() {
 +    if (this.part == null) {
 +      return null;
 +    } else if (this.part instanceof byte[]) {
 +      return (byte[])this.part;
 +    } else {
 +      return null; // should not be called on sender side?
 +    }
 +  }
 +  public Object getObject(boolean unzip) throws IOException, ClassNotFoundException {
 +    if (isBytes()) {
 +      return this.part;
 +    }
 +    else {
 +      if (this.version != null) {
 +        return CacheServerHelper.deserialize((byte[])this.part, this.version,
 +            unzip);
 +      }
 +      else {
 +        return CacheServerHelper.deserialize((byte[])this.part, unzip);
 +      }
 +    }
 +  }
 +  public Object getObject() throws IOException, ClassNotFoundException {
 +    return getObject(false);
 +  }
 +
 +  public Object getStringOrObject() throws IOException, ClassNotFoundException {
 +    if (isObject()) {
 +      return getObject();
 +    } else {
 +      return getString();
 +    }
 +  }
 +  
 +  /**
 +   * Write the contents of this part to the specified output stream.
 +   * This is only called for parts that will not fit into the commBuffer
 +   * so they need to be written directly to the stream.
 +   * A stream is used because the client is configured for old IO (instead of nio).
 +   * @param buf the buffer to use if any data needs to be copied to one
 +   */
 +  public final void sendTo(OutputStream out, ByteBuffer buf) throws IOException {
 +    if (getLength() > 0) {
 +      if (this.part instanceof byte[]) {
 +        byte[] bytes = (byte[])this.part;
 +        out.write(bytes, 0, bytes.length);
 +      } else if (this.part instanceof Chunk) {
 +        Chunk c = (Chunk) this.part;
 +        ByteBuffer cbb = c.createDirectByteBuffer();
 +        if (cbb != null) {
 +          HeapDataOutputStream.writeByteBufferToStream(out,  buf, cbb);
 +        } else {
 +          int bytesToSend = c.getDataSize();
 +          long addr = c.getAddressForReading(0, bytesToSend);
 +          while (bytesToSend > 0) {
 +            if (buf.remaining() == 0) {
 +              HeapDataOutputStream.flushStream(out,  buf);
 +            }
 +            buf.put(UnsafeMemoryChunk.readAbsoluteByte(addr));
 +            addr++;
 +            bytesToSend--;
 +          }
 +        }
 +      } else {
 +        HeapDataOutputStream hdos = (HeapDataOutputStream)this.part;
 +        hdos.sendTo(out, buf);
 +        hdos.rewind();
 +      }
 +    }
 +  }
 +  /**
 +   * Write the contents of this part to the specified byte buffer.
 +   * Precondition: caller has already checked the length of this part
 +   * and it will fit into "buf".
 +   */
 +  public final void sendTo(ByteBuffer buf) {
 +    if (getLength() > 0) {
 +      if (this.part instanceof byte[]) {
 +        buf.put((byte[])this.part);
 +      } else if (this.part instanceof Chunk) {
 +        Chunk c = (Chunk) this.part;
 +        ByteBuffer bb = c.createDirectByteBuffer();
 +        if (bb != null) {
 +          buf.put(bb);
 +        } else {
 +          int bytesToSend = c.getDataSize();
 +          long addr = c.getAddressForReading(0, bytesToSend);
 +          while (bytesToSend > 0) {
 +            buf.put(UnsafeMemoryChunk.readAbsoluteByte(addr));
 +            addr++;
 +            bytesToSend--;
 +          }
 +        }
 +      } else {
 +        HeapDataOutputStream hdos = (HeapDataOutputStream)this.part;
 +        hdos.sendTo(buf);
 +        hdos.rewind();
 +      }
 +    }
 +  }
 +  /**
 +   * Write the contents of this part to the specified socket channel
 +   * using the specified byte buffer.
 +   * This is only called for parts that will not fit into the commBuffer
 +   * so they need to be written directly to the socket.
 +   * Precondition: buf contains nothing that needs to be sent
 +   */
 +  public final void sendTo(SocketChannel sc, ByteBuffer buf) throws IOException {
 +    if (getLength() > 0) {
 +      final int BUF_MAX = buf.capacity();
 +      if (this.part instanceof byte[]) {
 +        final byte[] bytes = (byte[])this.part;
 +        int off = 0;
 +        int len = bytes.length;
 +        buf.clear();
 +        while (len > 0) {
 +          int bytesThisTime = len;
 +          if (bytesThisTime > BUF_MAX) {
 +            bytesThisTime = BUF_MAX;
 +          }
 +          buf.put(bytes, off, bytesThisTime);
 +          len -= bytesThisTime;
 +          off += bytesThisTime;
 +          buf.flip();
 +          while (buf.remaining() > 0) {
 +            sc.write(buf);
 +          }
 +          buf.clear();
 +        }
 +      } else if (this.part instanceof Chunk) {
 +        // instead of copying the Chunk to buf try to create a direct ByteBuffer and
 +        // just write it directly to the socket channel.
 +        Chunk c = (Chunk) this.part;
 +        ByteBuffer bb = c.createDirectByteBuffer();
 +        if (bb != null) {
 +          while (bb.remaining() > 0) {
 +            sc.write(bb);
 +          }
 +        } else {
 +          int len = c.getDataSize();
 +          long addr = c.getAddressForReading(0, len);
 +          buf.clear();
 +          while (len > 0) {
 +            int bytesThisTime = len;
 +            if (bytesThisTime > BUF_MAX) {
 +              bytesThisTime = BUF_MAX;
 +            }
 +            len -= bytesThisTime;
 +            while (bytesThisTime > 0) {
 +              buf.put(UnsafeMemoryChunk.readAbsoluteByte(addr));
 +              addr++;
 +              bytesThisTime--;
 +            }
 +            buf.flip();
 +            while (buf.remaining() > 0) {
 +              sc.write(buf);
 +            }
 +            buf.clear();
 +          }
 +        }
 +      } else {
 +        HeapDataOutputStream hdos = (HeapDataOutputStream)this.part;
 +        hdos.sendTo(sc, buf);
 +        hdos.rewind();
 +      }
 +    }
 +  }
 +  
 +  static private String typeCodeToString(byte c) {
 +    switch (c) {
 +    case BYTE_CODE:
 +      return "BYTE_CODE";
 +    case OBJECT_CODE:
 +      return "OBJECT_CODE";
 +    case EMPTY_BYTEARRAY_CODE:
 +      return "EMPTY_BYTEARRAY_CODE";
 +    default:
 +      return "unknown code " + c;
 +    }
 +  }
 +
 +  @Override
 +  public String toString() {
 +    StringBuffer sb = new StringBuffer();
 +    sb.append("partCode=");
 +    sb.append(typeCodeToString(this.typeCode));
 +    sb.append(" partLength=" + getLength());
 +//    sb.append(" partBytes=");
 +//    byte[] b = getSerializedForm();
 +//    if (b == null) {
 +//      sb.append("null");
 +//    }
 +//    else {
 +//      sb.append("(");
 +//      for (int i = 0; i < b.length; i ++) {
 +//        sb.append(Integer.toString(b[i]));
 +//        sb.append(" ");
 +//      }
 +//      sb.append(")");
 +//    }
 +    return sb.toString();
 +  }
 +
 +  public void setVersion(Version clientVersion) {
 +    this.version = clientVersion;
 +  }
 +}


[32/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/EntryEventImpl.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/cache/EntryEventImpl.java
index 9cf2f13,0000000..c731721
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/EntryEventImpl.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/EntryEventImpl.java
@@@ -1,3135 -1,0 +1,3142 @@@
 +/*
 + * 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;
 +
 +import java.io.ByteArrayInputStream;
 +import java.io.DataInput;
 +import java.io.DataInputStream;
 +import java.io.DataOutput;
 +import java.io.IOException;
 +
 +import org.apache.logging.log4j.Logger;
 +
 +import com.gemstone.gemfire.CopyHelper;
 +import com.gemstone.gemfire.DataSerializer;
 +import com.gemstone.gemfire.DeltaSerializationException;
 +import com.gemstone.gemfire.GemFireIOException;
 +import com.gemstone.gemfire.InvalidDeltaException;
 +import com.gemstone.gemfire.SerializationException;
 +import com.gemstone.gemfire.SystemFailure;
 +import com.gemstone.gemfire.cache.EntryEvent;
 +import com.gemstone.gemfire.cache.EntryNotFoundException;
 +import com.gemstone.gemfire.cache.EntryOperation;
 +import com.gemstone.gemfire.cache.Operation;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.SerializedCacheValue;
 +import com.gemstone.gemfire.cache.TransactionId;
 +import com.gemstone.gemfire.cache.query.IndexMaintenanceException;
 +import com.gemstone.gemfire.cache.query.QueryException;
 +import com.gemstone.gemfire.cache.query.internal.IndexUpdater;
 +import com.gemstone.gemfire.cache.query.internal.index.IndexManager;
 +import com.gemstone.gemfire.cache.query.internal.index.IndexProtocol;
 +import com.gemstone.gemfire.cache.query.internal.index.IndexUtils;
 +import com.gemstone.gemfire.cache.util.TimestampedEntryEvent;
 +import com.gemstone.gemfire.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.DistributedSystem;
 +import com.gemstone.gemfire.distributed.internal.DistributionMessage;
 +import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
 +import com.gemstone.gemfire.internal.Assert;
 +import com.gemstone.gemfire.internal.ByteArrayDataInput;
 +import com.gemstone.gemfire.internal.DSFIDFactory;
 +import com.gemstone.gemfire.internal.DataSerializableFixedID;
 +import com.gemstone.gemfire.internal.HeapDataOutputStream;
 +import com.gemstone.gemfire.internal.InternalDataSerializer;
 +import com.gemstone.gemfire.internal.Sendable;
 +import com.gemstone.gemfire.internal.Version;
 +import com.gemstone.gemfire.internal.cache.FilterRoutingInfo.FilterInfo;
 +import com.gemstone.gemfire.internal.cache.delta.Delta;
 +import com.gemstone.gemfire.internal.cache.lru.Sizeable;
 +import com.gemstone.gemfire.internal.cache.partitioned.PartitionMessage;
 +import com.gemstone.gemfire.internal.cache.partitioned.PutMessage;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.CacheServerHelper;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.ClientProxyMembershipID;
 +import com.gemstone.gemfire.internal.cache.tx.DistTxKeyInfo;
 +import com.gemstone.gemfire.internal.cache.versions.VersionTag;
 +import com.gemstone.gemfire.internal.cache.wan.GatewaySenderEventCallbackArgument;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.lang.StringUtils;
 +import com.gemstone.gemfire.internal.logging.LogService;
 +import com.gemstone.gemfire.internal.logging.log4j.LocalizedMessage;
 +import com.gemstone.gemfire.internal.logging.log4j.LogMarker;
 +import com.gemstone.gemfire.internal.offheap.Chunk;
 +import com.gemstone.gemfire.internal.offheap.OffHeapHelper;
 +import com.gemstone.gemfire.internal.offheap.OffHeapRegionEntryHelper;
 +import com.gemstone.gemfire.internal.offheap.ReferenceCountHelper;
 +import com.gemstone.gemfire.internal.offheap.Releasable;
 +import com.gemstone.gemfire.internal.offheap.StoredObject;
 +import com.gemstone.gemfire.internal.offheap.annotations.Released;
 +import com.gemstone.gemfire.internal.offheap.annotations.Retained;
 +import com.gemstone.gemfire.internal.offheap.annotations.Unretained;
 +
 +import static com.gemstone.gemfire.internal.offheap.annotations.OffHeapIdentifier.ENTRY_EVENT_NEW_VALUE;
 +import static com.gemstone.gemfire.internal.offheap.annotations.OffHeapIdentifier.ENTRY_EVENT_OLD_VALUE;
 +
 +import com.gemstone.gemfire.internal.util.ArrayUtils;
 +import com.gemstone.gemfire.internal.util.BlobHelper;
 +import com.gemstone.gemfire.pdx.internal.PeerTypeRegistration;
 +
 +/**
 + * Implementation of an entry event
 + */
 +// must be public for DataSerializableFixedID
 +public class EntryEventImpl
 +  implements EntryEvent, InternalCacheEvent, DataSerializableFixedID, EntryOperation
 +             , Releasable
 +{
 +  private static final Logger logger = LogService.getLogger();
 +  
 +  // PACKAGE FIELDS //
 +  public transient LocalRegion region;
 +  private transient RegionEntry re;
 +
 +  protected KeyInfo keyInfo;
 +
 +  //private long eventId;
 +  /** the event's id. Scoped by distributedMember. */
 +  protected EventID eventID;
 +
 +  private Object newValue = null;
 +  /**
 +   * If we ever serialize the new value then it should be
 +   * stored in this field in case we need the serialized form
 +   * again later. This was added to fix bug 43781.
 +   * Note that we also have the "newValueBytes" field.
 +   * But it is only non-null if setSerializedNewValue was called.
 +   */
 +  private byte[] cachedSerializedNewValue = null;
 +  @Retained(ENTRY_EVENT_OLD_VALUE)
 +  private Object oldValue = null;
 +  protected Delta delta = null;
 + 
 +  protected short eventFlags = 0x0000;
 +
 +  protected TXId txId = null;
 +
 +  protected Operation op;
 +
 +  /* To store the operation/modification type */
 +  private transient EnumListenerEvent eventType;
 +
 +  /**
 +   * This field will be null unless this event is used for a putAll operation.
 +   *
 +   * @since 5.0
 +   */
 +  protected transient DistributedPutAllOperation putAllOp;
 +
 +  /**
 +   * This field will be null unless this event is used for a removeAll operation.
 +   *
 +   * @since 8.1
 +   */
 +  protected transient DistributedRemoveAllOperation removeAllOp;
 +
 +  /**
 +   * The member that originated this event
 +   *
 +   * @since 5.0
 +   */
 +  protected DistributedMember distributedMember;
 +
 +  
 +  /**
 +   * transient storage for the message that caused the event
 +   */
 +  transient DistributionMessage causedByMessage;
 +  
 +  
 +  //private static long eventID = 0;
 +
 +  /**
 +   * The originating membershipId of this event.
 +   *
 +   * @since 5.1
 +   */
 +  protected ClientProxyMembershipID context = null;
 +  
 +  /**
 +   * A custom context object that can be used for any other contextual
 +   * information. Currently used by SQL Fabric to pass around evaluated rows
 +   * from raw byte arrays and routing object.
 +   */
 +  private transient Object contextObj = null;
 +
 +  /**
 +   * this holds the bytes representing the change in value effected by this
 +   * event.  It is used when the value implements the Delta interface.
 +   */
 +  private byte[] deltaBytes = null;
 +
 +  
 +  /** routing information for cache clients for this event */
 +  private FilterInfo filterInfo;
 +  
 +  /**new value stored in serialized form*/
 +  protected byte[] newValueBytes;
 +  /**old value stored in serialized form*/
 +  private byte[] oldValueBytes;
 +  
 +  /** version tag for concurrency checks */
 +  protected VersionTag versionTag;
 +
 +  /** boolean to indicate that this operation should be optimized by not fetching from HDFS*/
 +  private transient boolean fetchFromHDFS = true;
 +  
 +  private transient boolean isPutDML = false;
 +
 +  /** boolean to indicate that the RegionEntry for this event was loaded from HDFS*/
 +  private transient boolean loadedFromHDFS= false;
 +  
 +  private transient boolean isCustomEviction = false;
 +  
 +  /** boolean to indicate that the RegionEntry for this event has been evicted*/
 +  private transient boolean isEvicted = false;
 +  
 +  private transient boolean isPendingSecondaryExpireDestroy = false;
 +  
 +  public final static Object SUSPECT_TOKEN = new Object();
 +  
 +  public EntryEventImpl() {
 +  }
 +  
 +  /**
 +   * create a new entry event that will be used for conveying version information
 +   * and anything else of use while processing another event
 +   * @return the empty event object
 +   */
 +  @Retained
 +  public static EntryEventImpl createVersionTagHolder() {
 +    return createVersionTagHolder(null);
 +  }
 +  
 +  /**
 +   * create a new entry event that will be used for conveying version information
 +   * and anything else of use while processing another event
 +   * @return the empty event object
 +   */
 +  @Retained
 +  public static EntryEventImpl createVersionTagHolder(VersionTag tag) {
 +    EntryEventImpl result = new EntryEventImpl();
 +    result.setVersionTag(tag);
 +    result.disallowOffHeapValues();
 +    return result;
 +  }
 +
 +  /**
 +   * Reads the contents of this message from the given input.
 +   */
 +  public void fromData(DataInput in) throws IOException, ClassNotFoundException {
 +    this.eventID = (EventID)DataSerializer.readObject(in);
 +    Object key = DataSerializer.readObject(in);
 +    Object value = DataSerializer.readObject(in);
 +    this.keyInfo = new KeyInfo(key, value, null);
 +    this.op = Operation.fromOrdinal(in.readByte());
 +    this.eventFlags = in.readShort();
 +    this.keyInfo.setCallbackArg(DataSerializer.readObject(in));
 +    this.txId = (TXId)DataSerializer.readObject(in);
 +
 +    if (in.readBoolean()) {     // isDelta
 +      this.delta = (Delta)DataSerializer.readObject(in);
 +    }
 +    else {
 +      // OFFHEAP Currently values are never deserialized to off heap memory. If that changes then this code needs to change.
 +      if (in.readBoolean()) {     // newValueSerialized
 +        this.newValueBytes = DataSerializer.readByteArray(in);
 +        this.cachedSerializedNewValue = this.newValueBytes;
 +        this.newValue = CachedDeserializableFactory.create(this.newValueBytes);
 +      }
 +      else {
 +        this.newValue = DataSerializer.readObject(in);
 +      }
 +    }
 +
 +    // OFFHEAP Currently values are never deserialized to off heap memory. If that changes then this code needs to change.
 +    if (in.readBoolean()) {     // oldValueSerialized
 +      this.oldValueBytes = DataSerializer.readByteArray(in);
 +      this.oldValue = CachedDeserializableFactory.create(this.oldValueBytes);
 +    }
 +    else {
 +      this.oldValue = DataSerializer.readObject(in);
 +    }
 +    this.distributedMember = DSFIDFactory.readInternalDistributedMember(in);
 +    this.context = ClientProxyMembershipID.readCanonicalized(in);
 +    this.tailKey = DataSerializer.readLong(in);
 +  }
 +
 +  @Retained
 +  protected EntryEventImpl(LocalRegion region, Operation op, Object key,
 +      boolean originRemote, DistributedMember distributedMember,
 +      boolean generateCallbacks, boolean fromRILocalDestroy) {
 +    this.region = region;
 +    this.op = op;
 +    this.keyInfo = this.region.getKeyInfo(key);
 +    setOriginRemote(originRemote);
 +    setGenerateCallbacks(generateCallbacks);
 +    this.distributedMember = distributedMember;
 +    setFromRILocalDestroy(fromRILocalDestroy);
 +  }
 +
 +  /**
 +   * Doesn't specify oldValue as this will be filled in later as part of an
 +   * operation on the region, or lets it default to null.
 +   */
 +  @Retained
 +  protected EntryEventImpl(
 +      final LocalRegion region,
 +      Operation op, Object key, @Retained(ENTRY_EVENT_NEW_VALUE) Object newVal,
 +      Object callbackArgument,
 +      boolean originRemote, DistributedMember distributedMember,
 +      boolean generateCallbacks, boolean initializeId) {
 +
 +    this.region = region;
 +    this.op = op;
 +    this.keyInfo = this.region.getKeyInfo(key, newVal, callbackArgument);
 +
 +    if (newVal instanceof Delta) {
 +      this.delta = (Delta)newVal;
 +    }
 +    else if (!Token.isInvalid(newVal)) {
 +      basicSetNewValue(newVal);
 +    }
 +
 +    this.txId = this.region.getTXId();
 +    /**
 +     * this might set txId for events done from a thread that has a tx even
 +     * though the op is non-tx. For example region ops.
 +     */
 +    if (newVal == Token.LOCAL_INVALID) {
 +      setLocalInvalid(true);
 +    }
 +    setOriginRemote(originRemote);
 +    setGenerateCallbacks(generateCallbacks);
 +    this.distributedMember = distributedMember;
 +  }
 +
 +  /**
 +   * Called by BridgeEntryEventImpl to use existing EventID
 +   */
 +  @Retained
 +  protected EntryEventImpl(LocalRegion region, Operation op, Object key,
 +      @Retained(ENTRY_EVENT_NEW_VALUE) Object newValue, Object callbackArgument, boolean originRemote,
 +      DistributedMember distributedMember, boolean generateCallbacks,
 +      EventID eventID) {
 +    this(region, op, key, newValue,
 +        callbackArgument, originRemote, distributedMember, generateCallbacks,
 +        true /* initializeId */);
 +    Assert.assertTrue(eventID != null || !(region instanceof PartitionedRegion));
 +    this.setEventId(eventID);
 +  }
 +
 +  /**
 +   * create an entry event from another entry event
 +   */
 +  @Retained
 +  public EntryEventImpl(@Retained({ENTRY_EVENT_NEW_VALUE, ENTRY_EVENT_OLD_VALUE}) EntryEventImpl other) {
 +    this(other, true);
 +  }
 +  
 +  @Retained
 +  public EntryEventImpl(@Retained({ENTRY_EVENT_NEW_VALUE, ENTRY_EVENT_OLD_VALUE}) EntryEventImpl other, boolean setOldValue) {
 +    region = other.region;
 +
 +    this.eventID = other.eventID;
 +    basicSetNewValue(other.basicGetNewValue());
 +    this.newValueBytes = other.newValueBytes;
 +    this.cachedSerializedNewValue = other.cachedSerializedNewValue;
 +    this.re = other.re;
 +    this.delta = other.delta;
 +    if (setOldValue) {
 +      retainAndSetOldValue(other.basicGetOldValue());
 +      this.oldValueBytes = other.oldValueBytes;
 +    }
 +    this.eventFlags = other.eventFlags;
 +    setEventFlag(EventFlags.FLAG_CALLBACKS_INVOKED, false);
 +    txId = other.txId;
 +    op = other.op;
 +    distributedMember = other.distributedMember;
 +    this.filterInfo = other.filterInfo;
 +    this.keyInfo = other.keyInfo.isDistKeyInfo() ? new DistTxKeyInfo(
 +        (DistTxKeyInfo) other.keyInfo) : new KeyInfo(other.keyInfo);
 +    if (other.getRawCallbackArgument() instanceof GatewaySenderEventCallbackArgument) {
 +      this.keyInfo
 +          .setCallbackArg((new GatewaySenderEventCallbackArgument(
 +              (GatewaySenderEventCallbackArgument) other
 +                  .getRawCallbackArgument())));
 +    }
 +    this.context = other.context;
 +    this.deltaBytes = other.deltaBytes;
 +    this.tailKey = other.tailKey;
 +    this.versionTag = other.versionTag;
 +    //set possible duplicate 
 +    this.setPossibleDuplicate(other.isPossibleDuplicate()); 
 +  }
 +
 +  @Retained
 +  public EntryEventImpl(Object key2) {
 +    this.keyInfo = new KeyInfo(key2, null, null);
 +  }
 +  
 +  /**
 +   * This constructor is used to create a bridge event in server-side
 +   * command classes.  Events created with this are not intended to be
 +   * used in cache operations.
 +   * @param id the identity of the client's event
 +   */
 +  @Retained
 +  public EntryEventImpl(EventID id) {
 +    this.eventID = id;
 +    this.offHeapOk = false;
 +  }
 +
 +  /**
 +   * Creates and returns an EntryEventImpl.  Generates and assigns a bucket id to the
 +   * EntryEventImpl if the region parameter is a PartitionedRegion.
 +   */  
 +  @Retained
 +  public static EntryEventImpl create(LocalRegion region,
 +      Operation op,
 +      Object key, @Retained(ENTRY_EVENT_NEW_VALUE) Object newValue, Object callbackArgument,
 +      boolean originRemote, DistributedMember distributedMember) {
 +    return create(region,op,key,newValue,callbackArgument,originRemote,distributedMember,true,true);
 +  }
 +  
 +  /**
 +   * Creates and returns an EntryEventImpl.  Generates and assigns a bucket id to the
 +   * EntryEventImpl if the region parameter is a PartitionedRegion.
 +   */
 +  @Retained
 +  public static EntryEventImpl create(LocalRegion region,
 +      Operation op,
 +      Object key,
 +      @Retained(ENTRY_EVENT_NEW_VALUE) Object newValue,
 +      Object callbackArgument,
 +      boolean originRemote,
 +      DistributedMember distributedMember,
 +      boolean generateCallbacks) {
 +    return create(region, op, key, newValue, callbackArgument, originRemote,
 +        distributedMember, generateCallbacks,true);
 +  }
 +  
 +  /**
 +   * Creates and returns an EntryEventImpl.  Generates and assigns a bucket id to the
 +   * EntryEventImpl if the region parameter is a PartitionedRegion.
 +   *  
 +   * Called by BridgeEntryEventImpl to use existing EventID
 +   * 
 +   * {@link EntryEventImpl#EntryEventImpl(LocalRegion, Operation, Object, Object, Object, boolean, DistributedMember, boolean, EventID)}
 +   */ 
 +  @Retained
 +  public static EntryEventImpl create(LocalRegion region, Operation op, Object key,
 +      @Retained(ENTRY_EVENT_NEW_VALUE) Object newValue, Object callbackArgument, boolean originRemote,
 +      DistributedMember distributedMember, boolean generateCallbacks,
 +      EventID eventID) {
 +    EntryEventImpl entryEvent = new EntryEventImpl(region,op,key,newValue,callbackArgument,originRemote,distributedMember,generateCallbacks,eventID);
 +    return entryEvent;
 +  }
 +  
 +  /**
 +   * Creates and returns an EntryEventImpl.  Generates and assigns a bucket id to the
 +   * EntryEventImpl if the region parameter is a PartitionedRegion.
 +   * 
 +   * {@link EntryEventImpl#EntryEventImpl(LocalRegion, Operation, Object, boolean, DistributedMember, boolean, boolean)}
 +   */
 +  @Retained
 +  public static EntryEventImpl create(LocalRegion region, Operation op, Object key,
 +      boolean originRemote, DistributedMember distributedMember,
 +      boolean generateCallbacks, boolean fromRILocalDestroy) {
 +    EntryEventImpl entryEvent = new EntryEventImpl(region,op,key,originRemote,distributedMember,generateCallbacks,fromRILocalDestroy);
 +    return entryEvent;
 +  }  
 +  
 +  /**
 +   * Creates and returns an EntryEventImpl.  Generates and assigns a bucket id to the
 +   * EntryEventImpl if the region parameter is a PartitionedRegion.
 +   * 
 +   * This creator does not specify the oldValue as this will be filled in later as part of an
 +   * operation on the region, or lets it default to null.
 +   * 
 +   * {@link EntryEventImpl#EntryEventImpl(LocalRegion, Operation, Object, Object, Object, boolean, DistributedMember, boolean, boolean)}
 +   */
 +  @Retained
 +  public static EntryEventImpl create(final LocalRegion region,
 +      Operation op, Object key, @Retained(ENTRY_EVENT_NEW_VALUE) Object newVal,
 +      Object callbackArgument,
 +      boolean originRemote, DistributedMember distributedMember,
 +      boolean generateCallbacks, boolean initializeId)  {
 +    EntryEventImpl entryEvent = new EntryEventImpl(region,op,key,newVal,callbackArgument,
 +        originRemote,distributedMember,generateCallbacks,initializeId);
 +    return entryEvent;
 +  }
 +  
 +  /**
 +   * Creates a PutAllEvent given the distributed operation, the region, and the
 +   * entry data.
 +   *
 +   * @since 5.0
 +   */
 +  @Retained
 +  static EntryEventImpl createPutAllEvent(
 +      DistributedPutAllOperation putAllOp, LocalRegion region,
 +      Operation entryOp, Object entryKey, @Retained(ENTRY_EVENT_NEW_VALUE) Object entryNewValue)
 +  {
 +    EntryEventImpl e;
 +    if (putAllOp != null) {
 +      EntryEventImpl event = putAllOp.getBaseEvent();
 +      if (event.isBridgeEvent()) {
 +        e = EntryEventImpl.create(region, entryOp, entryKey, entryNewValue,
 +            event.getRawCallbackArgument(), false, event.distributedMember,
 +            event.isGenerateCallbacks());
 +        e.setContext(event.getContext());
 +      } else {
 +        e = EntryEventImpl.create(region, entryOp, entryKey, entryNewValue, event.getCallbackArgument(),
 +            false, region.getMyId(), event.isGenerateCallbacks());
 +      }
 +      
 +    } else {
 +      e = EntryEventImpl.create(region, entryOp, entryKey, entryNewValue, null,
 +          false, region.getMyId(), true);
 +    }
 +    
 +    e.putAllOp = putAllOp;
 +    return e;
 +  }
 +  
 +  protected static EntryEventImpl createRemoveAllEvent(
 +      DistributedRemoveAllOperation op, 
 +      LocalRegion region,
 +      Object entryKey) {
 +    EntryEventImpl e;
 +    final Operation entryOp = Operation.REMOVEALL_DESTROY;
 +    if (op != null) {
 +      EntryEventImpl event = op.getBaseEvent();
 +      if (event.isBridgeEvent()) {
 +        e = EntryEventImpl.create(region, entryOp, entryKey, null,
 +            event.getRawCallbackArgument(), false, event.distributedMember,
 +            event.isGenerateCallbacks());
 +        e.setContext(event.getContext());
 +      } else {
 +        e = EntryEventImpl.create(region, entryOp, entryKey, null, event.getCallbackArgument(),
 +            false, region.getMyId(), event.isGenerateCallbacks());
 +      }
 +      
 +    } else {
 +      e = EntryEventImpl.create(region, entryOp, entryKey, null, null,
 +          false, region.getMyId(), true);
 +    }
 +    
 +    e.removeAllOp = op;
 +    return e;
 +  }
 +  public boolean isBulkOpInProgress() {
 +    return getPutAllOperation() != null || getRemoveAllOperation() != null;
 +  }
 +  
 +  /** return the putAll operation for this event, if any */
 +  public DistributedPutAllOperation getPutAllOperation() {
 +    return this.putAllOp;
 +  }
 +  public DistributedPutAllOperation setPutAllOperation(DistributedPutAllOperation nv) {
 +    DistributedPutAllOperation result = this.putAllOp;
 +    if (nv != null && nv.getBaseEvent() != null) {
 +      setCallbackArgument(nv.getBaseEvent().getCallbackArgument());
 +    }
 +    this.putAllOp = nv;
 +    return result;
 +  }
 +  public DistributedRemoveAllOperation getRemoveAllOperation() {
 +    return this.removeAllOp;
 +  }
 +  public DistributedRemoveAllOperation setRemoveAllOperation(DistributedRemoveAllOperation nv) {
 +    DistributedRemoveAllOperation result = this.removeAllOp;
 +    if (nv != null && nv.getBaseEvent() != null) {
 +      setCallbackArgument(nv.getBaseEvent().getCallbackArgument());
 +    }
 +    this.removeAllOp = nv;
 +    return result;
 +  }
 +
 +  private final boolean testEventFlag(short mask)
 +  {
 +    return EventFlags.isSet(this.eventFlags, mask);
 +  }
 +
 +  private final void setEventFlag(short mask, boolean on)
 +  {
 +    this.eventFlags = EventFlags.set(this.eventFlags, mask, on);
 +  }
 +
 +  public DistributedMember getDistributedMember()
 +  {
 +    return this.distributedMember;
 +  }
 +
 +  /////////////////////// INTERNAL BOOLEAN SETTERS
 +  public void setOriginRemote(boolean b)
 +  {
 +    setEventFlag(EventFlags.FLAG_ORIGIN_REMOTE, b);
 +  }
 +
 +  public void setLocalInvalid(boolean b)
 +  {
 +    setEventFlag(EventFlags.FLAG_LOCAL_INVALID, b);
 +  }
 +
 +  void setGenerateCallbacks(boolean b)
 +  {
 +    setEventFlag(EventFlags.FLAG_GENERATE_CALLBACKS, b);
 +  }
 +
 +  /** set the the flag telling whether callbacks should be invoked for a partitioned region */
 +  public void setInvokePRCallbacks(boolean b) {
 +    setEventFlag(EventFlags.FLAG_INVOKE_PR_CALLBACKS, b);
 +  }
 +
 +  /** get the flag telling whether callbacks should be invoked for a partitioned region */
 +  public boolean getInvokePRCallbacks() {
 +    return testEventFlag(EventFlags.FLAG_INVOKE_PR_CALLBACKS);
 +  }
 +  
 +  public boolean getInhibitDistribution() {
 +    return testEventFlag(EventFlags.FLAG_INHIBIT_DISTRIBUTION);
 +  }
 +  
 +  public void setInhibitDistribution(boolean b) {
 +    setEventFlag(EventFlags.FLAG_INHIBIT_DISTRIBUTION, b);
 +  }
 +  
 +  /** was the entry destroyed or missing and allowed to be destroyed again? */
 +  public boolean getIsRedestroyedEntry() {
 +    return testEventFlag(EventFlags.FLAG_REDESTROYED_TOMBSTONE);
 +  }
 +  
 +  public void setIsRedestroyedEntry(boolean b) {
 +    setEventFlag(EventFlags.FLAG_REDESTROYED_TOMBSTONE, b);
 +  }
 +  
 +  public void isConcurrencyConflict(boolean b) {
 +    setEventFlag(EventFlags.FLAG_CONCURRENCY_CONFLICT, b);
 +  }
 +  
 +  public boolean isConcurrencyConflict() {
 +    return testEventFlag(EventFlags.FLAG_CONCURRENCY_CONFLICT);
 +  }
 +
 +  /** set the DistributionMessage that caused this event */
 +  public void setCausedByMessage(DistributionMessage msg) {
 +    this.causedByMessage = msg;
 +  }
 +
 +  /**
 +   * get the PartitionMessage that caused this event, or null if
 +   * the event was not caused by a PartitionMessage
 +   */
 +  public PartitionMessage getPartitionMessage() {
 +    if (this.causedByMessage != null && this.causedByMessage instanceof PartitionMessage) {
 +      return (PartitionMessage)this.causedByMessage;
 +  }
 +    return null;
 +  }
 +
 +  /**
 +   * get the RemoteOperationMessage that caused this event, or null if
 +   * the event was not caused by a RemoteOperationMessage
 +   */
 +  public RemoteOperationMessage getRemoteOperationMessage() {
 +    if (this.causedByMessage != null && this.causedByMessage instanceof RemoteOperationMessage) {
 +      return (RemoteOperationMessage)this.causedByMessage;
 +    }
 +    return null;
 +  }
 +
 +  /////////////// BOOLEAN GETTERS
 +  public boolean isLocalLoad()
 +  {
 +    return this.op.isLocalLoad();
 +  }
 +
 +  public boolean isNetSearch()
 +  {
 +    return this.op.isNetSearch();
 +  }
 +
 +  public boolean isNetLoad()
 +  {
 +    return this.op.isNetLoad();
 +  }
 +
 +  public boolean isDistributed()
 +  {
 +    return this.op.isDistributed();
 +  }
 +
 +  public boolean isExpiration()
 +  {
 +    return this.op.isExpiration();
 +  }
 +  
 +  public boolean isEviction() {
 +    return this.op.isEviction();
 +  }
 +
 +  public final boolean isCustomEviction() {
 +    return this.isCustomEviction;
 +  }
 +  
 +  public final void setCustomEviction(boolean customEvict) {
 +    this.isCustomEviction = customEvict;
 +  }
 +  
 +  public final void setEvicted() {
 +    this.isEvicted = true;
 +  }
 +  
 +  public final boolean isEvicted() {
 +    return this.isEvicted;
 +  }
 +  
 +  public final boolean isPendingSecondaryExpireDestroy() {
 +    return this.isPendingSecondaryExpireDestroy;
 +  }
 +  
 +  public final void setPendingSecondaryExpireDestroy (boolean value) {
 +    this.isPendingSecondaryExpireDestroy = value;
 +  }
 +  // Note that isOriginRemote is sometimes set to false even though the event
 +  // was received from a peer.  This is done to force distribution of the
 +  // message to peers and to cause concurrency version stamping to be performed.
 +  // This is done by all one-hop operations, like RemoteInvalidateMessage.
 +  public boolean isOriginRemote()
 +  {
 +    return testEventFlag(EventFlags.FLAG_ORIGIN_REMOTE);
 +  }
 +
 +  /* return whether this event originated from a WAN gateway and carries a WAN version tag */
 +  public boolean isFromWANAndVersioned() {
 +    return (this.versionTag != null && this.versionTag.isGatewayTag());
 +  }
 +  
 +  /* return whether this event originated in a client and carries a version tag */
 +  public boolean isFromBridgeAndVersioned() {
 +    return (this.context != null) && (this.versionTag != null);
 +  }
 +
 +  public boolean isGenerateCallbacks()
 +  {
 +    return testEventFlag(EventFlags.FLAG_GENERATE_CALLBACKS);
 +  }
 +
 +  public void setNewEventId(DistributedSystem sys) {
 +    Assert.assertTrue(this.eventID == null, "Double setting event id");
 +    EventID newID = new EventID(sys);
 +    if (this.eventID != null) {
 +      if (logger.isTraceEnabled(LogMarker.BRIDGE_SERVER)) {
 +        logger.trace(LogMarker.BRIDGE_SERVER, "Replacing event ID with {} in event {}", newID, this);
 +      }
 +    }
 +    this.eventID = newID;
 +  }
 +  
 +  public void reserveNewEventId(DistributedSystem sys, int count) {
 +    Assert.assertTrue(this.eventID == null, "Double setting event id");
 +    this.eventID = new EventID(sys);
 +    if (count > 1) {
 +      this.eventID.reserveSequenceId(count-1);
 +    }
 +  }
 +
 +  public void setEventId(EventID id)
 +  {
 +    this.eventID = id;
 +  }
 +
 +  /**
 +   * Return the event id, if any
 +   * @return null if no event id has been set
 +   */
 +  public final EventID getEventId() {
 +    return this.eventID;
 +  }
 +
 +  public boolean isBridgeEvent() {
 +    return hasClientOrigin();
 +  }
 +  public boolean hasClientOrigin() {
 +    return getContext() != null;
 +  }
 +
 +  /**
 +   * sets the ID of the client that initiated this event
 +   */
 +  public void setContext(ClientProxyMembershipID contx) {
 +    Assert.assertTrue(contx != null);
 +    this.context = contx;
 +  }
 +
 +  /**
 +   * gets the ID of the client that initiated this event.  Null if a server-initiated event
 +   */
 +  public ClientProxyMembershipID getContext()
 +  {
 +    return this.context;
 +  }
 +
 +  // INTERNAL
 +  boolean isLocalInvalid()
 +  {
 +    return testEventFlag(EventFlags.FLAG_LOCAL_INVALID);
 +  }
 +
 +  /////////////////////////////////////////////////
 +
 +  /**
 +   * Returns the key.
 +   *
 +   * @return the key.
 +   */
 +  public Object getKey()
 +  {
 +    return keyInfo.getKey();
 +  }
 +
 +  /**
 +   * Returns the value in the cache prior to this event. When passed to an event
 +   * handler after an event occurs, this value reflects the value that was in
 +   * the cache in this VM, not necessarily the value that was in the cache VM
 +   * that initiated the operation.
 +   *
 +   * @return the value in the cache prior to this event.
 +   */
 +  public final Object getOldValue() {
 +    try {
 +      if (isOriginRemote() && this.region.isProxy()) {
 +        return null;
 +      }
 +      @Unretained Object ov = basicGetOldValue();
 +      if (ov == null) {
 +        return null;
 +      } else if (ov == Token.NOT_AVAILABLE) {
 +        return AbstractRegion.handleNotAvailable(ov);
 +      }
 +      boolean doCopyOnRead = getRegion().isCopyOnRead();
 +      if (ov != null) {
 +        if (ov instanceof StoredObject) {
 +          // TODO OFFHEAP: returns off-heap PdxInstance
 +          return ((StoredObject) ov).getValueAsDeserializedHeapObject();
 +        } else
 +        if (ov instanceof CachedDeserializable) {
 +          CachedDeserializable cd = (CachedDeserializable)ov;
 +          if (doCopyOnRead) {
 +            return cd.getDeserializedWritableCopy(this.region, this.re);
 +          } else {
 +            return cd.getDeserializedValue(this.region, this.re);
 +          }
 +        }
 +        else {
 +          if (doCopyOnRead) {
 +            return CopyHelper.copy(ov);
 +          } else {
 +            return ov;
 +          }
 +        }
 +      }
 +      return null;
 +    } catch(IllegalArgumentException i) {
 +      IllegalArgumentException iae = new IllegalArgumentException(LocalizedStrings.DONT_RELEASE.toLocalizedString("Error while deserializing value for key="+getKey()));
 +      iae.initCause(i);
 +      throw iae;
 +    }
 +  }
 +
 +  /**
 +   * Like getRawNewValue except that if the result is an off-heap reference then copy it to the heap.
 +   * ALERT: If there is a Delta, returns that, not the (applied) new value.
 +   * TODO OFFHEAP: to prevent the heap copy use getRawNewValue instead
 +   */
 +  public final Object getRawNewValueAsHeapObject() {
 +    if (this.delta != null) {
 +      return this.delta;
 +    }
 +    return OffHeapHelper.getHeapForm(OffHeapHelper.copyIfNeeded(basicGetNewValue()));
 +  }
 +  
 +  /**
 +   * If new value is a Delta return it.
 +   * Else if new value is off-heap return the StoredObject form (unretained OFF_HEAP_REFERENCE). 
 +   * Its refcount is not inced by this call and the returned object can only be safely used for the lifetime of the EntryEventImpl instance that returned the value.
 +   * Else return the raw form.
 +   */
 +  @Unretained(ENTRY_EVENT_NEW_VALUE)
 +  public final Object getRawNewValue() {
 +    if (this.delta != null) return this.delta;
 +    return basicGetNewValue();
 +  }
 +
 +  @Unretained(ENTRY_EVENT_NEW_VALUE)
 +  public Object getValue() {
 +    return basicGetNewValue();
 +  }
 +  
 +  /**
 +   * Returns the delta that represents the new value; null if no delta.
 +   * @return the delta that represents the new value; null if no delta.
 +   */
 +  public final Delta getDeltaNewValue() {
 +    return this.delta;
 +  }
 +
 +  /**
 +   *  Applies the delta 
 +   */
 +  private Object applyDeltaWithCopyOnRead(boolean doCopyOnRead) {
 +    //try {
 +      if (applyDelta(true)) {
 +        Object applied = basicGetNewValue();
 +        // if applyDelta returns true then newValue should not be off-heap
 +        assert !(applied instanceof StoredObject);
 +        if (applied == this.oldValue && doCopyOnRead) {
 +          applied = CopyHelper.copy(applied);
 +        }
 +        return applied;
 +      }
 +    //} catch (EntryNotFoundException ex) {
 +      // only (broken) product code has the opportunity to call this before
 +      // this.oldValue is set. If oldValue is not set yet, then
 +      // we most likely haven't synchronized on the region entry yet.
 +      // (If we have, then make sure oldValue is set before
 +      // calling this method).
 +      //throw new AssertionError("too early to call getNewValue");
 +    //}
 +    return null;
 +  }
 +
 +  @Released(ENTRY_EVENT_NEW_VALUE)
 +  protected void basicSetNewValue(@Retained(ENTRY_EVENT_NEW_VALUE) Object v) {
 +    if (v == this.newValue) return;
 +    if (this.offHeapOk) {
 +      OffHeapHelper.releaseAndTrackOwner(this.newValue, this);
 +    }
 +    if (v instanceof Chunk) {
 +      ReferenceCountHelper.setReferenceCountOwner(this);
 +      if (!((Chunk) v).retain()) {
 +        ReferenceCountHelper.setReferenceCountOwner(null);
 +        this.newValue = null;
 +        return;
 +      }
 +      ReferenceCountHelper.setReferenceCountOwner(null);
 +    }
 +    this.newValue = v;
 +    this.cachedSerializedNewValue = null;
 +  }
 +  /**
 +   * Returns true if this event has a reference to an off-heap new or old value.
 +   */
 +  public boolean hasOffHeapValue() {
 +    return (this.newValue instanceof Chunk) || (this.oldValue instanceof Chunk);
 +  }
 +  
 +  @Unretained
 +  protected final Object basicGetNewValue() {
 +    Object result = this.newValue;
 +    if (!this.offHeapOk && result instanceof Chunk) {
 +      //this.region.getCache().getLogger().info("DEBUG new value already freed " + System.identityHashCode(result));
 +      throw new IllegalStateException("Attempt to access off heap value after the EntryEvent was released.");
 +    }
 +    return result;
 +  }
 +  
 +  private class OldValueOwner {
 +    private EntryEventImpl getEvent() {
 +      return EntryEventImpl.this;
 +    }
 +    @Override
 +    public int hashCode() {
 +      return getEvent().hashCode();
 +    }
 +
 +    @Override
 +    public boolean equals(Object obj) {
 +      if (obj instanceof OldValueOwner) {
 +        return getEvent().equals(((OldValueOwner) obj).getEvent());
 +      } else {
 +        return false;
 +      }
 +    }
 +    @Override
 +    public String toString() {
 +      return "OldValueOwner " + getEvent().toString();
 +    }
 +  }
 +
 +  /**
 +   * Note if v might be an off-heap reference that you did not retain for this EntryEventImpl
 +   * then call retainsAndSetOldValue instead of this method.
 +   * @param v the caller should have already retained this off-heap reference.
 +   */
 +  @Released(ENTRY_EVENT_OLD_VALUE)
 +  private void basicSetOldValue(@Unretained(ENTRY_EVENT_OLD_VALUE) Object v) {
 +    @Released final Object curOldValue = this.oldValue;
 +    if (v == curOldValue) return;
 +    if (this.offHeapOk) {
 +      if (curOldValue instanceof Chunk) {
 +        if (ReferenceCountHelper.trackReferenceCounts()) {
 +          OffHeapHelper.releaseAndTrackOwner(curOldValue, new OldValueOwner());
 +        } else {
 +          OffHeapHelper.release(curOldValue);
 +        }
 +      }
 +    }
 +    
 +    this.oldValue = v;
 +  }
 +
 +  @Released(ENTRY_EVENT_OLD_VALUE)
 +  private void retainAndSetOldValue(@Retained(ENTRY_EVENT_OLD_VALUE) Object v) {
 +    if (v == this.oldValue) return;
 +    
 +    if (v instanceof Chunk) {
 +      if (ReferenceCountHelper.trackReferenceCounts()) {
 +        ReferenceCountHelper.setReferenceCountOwner(new OldValueOwner());
 +        boolean couldNotRetain = (!((Chunk) v).retain());
 +        ReferenceCountHelper.setReferenceCountOwner(null);
 +        if (couldNotRetain) {
 +          this.oldValue = null;
 +          return;
 +        }
 +      } else {
 +        if (!((Chunk) v).retain()) {
 +          this.oldValue = null;
 +          return;
 +        }
 +      }
 +    }
 +    basicSetOldValue(v);
 +  }
 +
 +  @Unretained(ENTRY_EVENT_OLD_VALUE)
 +  private Object basicGetOldValue() {
 +    @Unretained(ENTRY_EVENT_OLD_VALUE)
 +    Object result = this.oldValue;
 +    if (!this.offHeapOk && result instanceof Chunk) {
 +      //this.region.getCache().getLogger().info("DEBUG old value already freed " + System.identityHashCode(result));
 +      throw new IllegalStateException("Attempt to access off heap value after the EntryEvent was released.");
 +    }
 +    return result;
 +  }
 +
 +  /**
 +   * Like getRawOldValue except that if the result is an off-heap reference then copy it to the heap.
 +   * To avoid the heap copy use getRawOldValue instead.
 +   */
 +  public final Object getRawOldValueAsHeapObject() {
 +    return OffHeapHelper.getHeapForm(OffHeapHelper.copyIfNeeded(basicGetOldValue()));
 +  }
 +  /*
 +   * If the old value is off-heap return the StoredObject form (unretained OFF_HEAP_REFERENCE). 
 +   * Its refcount is not inced by this call and the returned object can only be safely used for the lifetime of the EntryEventImpl instance that returned the value.
 +   * Else return the raw form.
 +   */
 +  @Unretained
 +  public final Object getRawOldValue() {
 +    return basicGetOldValue();
 +  }
 +  /**
 +   * Just like getRawOldValue except if the raw old value is off-heap deserialize it.
 +   * Note that in some cases sqlf ignores the request to deserialize.
 +   */
 +  @Unretained(ENTRY_EVENT_OLD_VALUE)
 +  public final Object getOldValueAsOffHeapDeserializedOrRaw() {
 +    Object result = basicGetOldValue();
 +    if (result instanceof StoredObject) {
 +      result = ((StoredObject) result).getDeserializedForReading();
 +    }
 +    return AbstractRegion.handleNotAvailable(result); // fixes 49499
 +  }
 +
 +  /**
 +   * Added this function to expose isCopyOnRead function to the
 +   * child classes of EntryEventImpl  
 +   * 
 +   */
 +  protected boolean isRegionCopyOnRead() {
 +    return getRegion().isCopyOnRead();
 +  }
 + 
 +  /**
 +   * Returns the value in the cache after this event.
 +   *
 +   * @return the value in the cache after this event.
 +   */
 +  public final Object getNewValue() {
 +    
 +    boolean doCopyOnRead = getRegion().isCopyOnRead();
 +    try {
 +      if (applyDelta(true)) {
 +        @Unretained(ENTRY_EVENT_NEW_VALUE)
 +        Object applied = basicGetNewValue();
 +        if (applied == this.oldValue && doCopyOnRead) {
 +          applied = CopyHelper.copy(applied);
 +        }
 +        return applied;
 +      }
 +    } catch (EntryNotFoundException ex) {
 +      // only (broken) product code has the opportunity to call this before
 +      // this.oldValue is set. If oldValue is not set yet, then
 +      // we most likely haven't synchronized on the region entry yet.
 +      // (If we have, then make sure oldValue is set before
 +      // calling this method).
 +      throw new AssertionError("too early to call getNewValue");
 +    }
 +    Object nv = basicGetNewValue();
 +    if (nv != null) {
 +      if (nv == Token.NOT_AVAILABLE) {
 +        // I'm not sure this can even happen
 +        return AbstractRegion.handleNotAvailable(nv);
 +      }
 +      if (nv instanceof StoredObject) {
 +        // TODO OFFHEAP currently we copy offheap new value to the heap here. Check callers of this method to see if they can be optimized to use offheap values.
 +        // TODO OFFHEAP: returns off-heap PdxInstance
 +        return ((StoredObject) nv).getValueAsDeserializedHeapObject();
 +      } else
 +      if (nv instanceof CachedDeserializable) {
 +        CachedDeserializable cd = (CachedDeserializable)nv;
 +        Object v = null;
 +        if (doCopyOnRead) {
 +          v = cd.getDeserializedWritableCopy(this.region, this.re);
 +        } else {
 +          v = cd.getDeserializedValue(this.region, this.re);
 +        }
 +        assert !(v instanceof CachedDeserializable) : "for key "+this.getKey()+" found nested CachedDeserializable";
 +        return v;
 +      }
 +      else {
 +        if (doCopyOnRead) {
 +          return CopyHelper.copy(nv);
 +        } else {
 +          return nv;
 +        }
 +      }
 +    }
 +    return null;
 +  }
 +
 +  public final String getNewValueStringForm() {
 +    return StringUtils.forceToString(basicGetNewValue());
 +  }
 +  public final String getOldValueStringForm() {
 +    return StringUtils.forceToString(basicGetOldValue());
 +  }
 +  
 +  protected boolean applyDelta(boolean throwOnNullOldValue)
 +      throws EntryNotFoundException {
 +    if (this.newValue != null || this.delta == null) {
 +      return false;
 +    }
 +    if (this.oldValue == null) {
 +      if (throwOnNullOldValue) {
 +        // !!!:ezoerner:20080611 It would be nice if the client got this
 +        // exception
 +        throw new EntryNotFoundException(
 +            "Cannot apply a delta without an existing value");
 +      }
 +      return false;
 +    }
 +    // swizzle BucketRegion in event for Delta.
 +    // !!!:ezoerner:20090602 this is way ugly; this whole class severely
 +    // needs refactoring
 +    LocalRegion originalRegion = this.region;
 +    try {
 +      if (originalRegion instanceof BucketRegion) {
 +        this.region = ((BucketRegion)this.region).getPartitionedRegion();
 +      }
 +      basicSetNewValue(this.delta.apply(this));
 +    } finally {
 +      this.region = originalRegion;
 +    }
 +    return true;
 +  }
 +
 +  /** Set a deserialized value */
 +  public final void setNewValue(@Retained(ENTRY_EVENT_NEW_VALUE) Object obj) {
 +    if (obj instanceof Delta) {
 +      this.delta = (Delta)obj;
 +      basicSetNewValue(null);
 +    }
 +    else {
 +      basicSetNewValue(obj);
 +    }
 +  }
 +
 +  public TransactionId getTransactionId()
 +  {
 +    return this.txId;
 +  }
 +
 +  public void setTransactionId(TransactionId txId)
 +  {
 +    this.txId = (TXId)txId;
 +  }
 +
 +  /**
 +   * Answer true if this event resulted from a loader.
 +   *
 +   * @return true if isLocalLoad or isNetLoad
 +   */
 +  public boolean isLoad()
 +  {
 +    return this.op.isLoad();
 +  }
 +
 +  public void setRegion(LocalRegion r)
 +  {
 +    this.region = r;
 +  }
 +
 +  /**
 +   * @see com.gemstone.gemfire.cache.CacheEvent#getRegion()
 +   */
 +  public final LocalRegion getRegion() {
 +    return region;
 +  }
 +
 +  public Operation getOperation()
 +  {
 +    return this.op;
 +  }
 +
 +  public void setOperation(Operation op)
 +  {
 +    this.op = op;
 +    PartitionMessage prm = getPartitionMessage();
 +    if (prm != null) {
 +      prm.setOperation(this.op);
 +    }
 +  }
 +
 +  /**
 +   * @see com.gemstone.gemfire.cache.CacheEvent#getCallbackArgument()
 +   */
 +  public Object getCallbackArgument()
 +  {
 +    Object result = this.keyInfo.getCallbackArg();
 +    while (result instanceof WrappedCallbackArgument) {
 +      WrappedCallbackArgument wca = (WrappedCallbackArgument)result;
 +      result = wca.getOriginalCallbackArg();
 +    }
 +    if (result == Token.NOT_AVAILABLE) {
 +      result = AbstractRegion.handleNotAvailable(result);
 +    }
 +    return result;
 +  }
 +  public boolean isCallbackArgumentAvailable() {
 +    return this.getRawCallbackArgument() != Token.NOT_AVAILABLE;
 +  }
 +
 +  /**
 +   * Returns the value of the EntryEventImpl field.
 +   * This is for internal use only. Customers should always call
 +   * {@link #getCallbackArgument}
 +   * @since 5.5 
 +   */
 +  public Object getRawCallbackArgument() {
 +    return this.keyInfo.getCallbackArg();
 +  }
 +  
 +  /**
 +   * Sets the value of raw callback argument field.
 +   */
 +  public void setRawCallbackArgument(Object newCallbackArgument) {
 +    this.keyInfo.setCallbackArg(newCallbackArgument);
 +  }
 +
 +  public void setCallbackArgument(Object newCallbackArgument) {
 +    if (this.keyInfo.getCallbackArg() instanceof WrappedCallbackArgument) {
 +      ((WrappedCallbackArgument)this.keyInfo.getCallbackArg())
 +          .setOriginalCallbackArgument(newCallbackArgument);
 +    }
 +    else {
 +      this.keyInfo.setCallbackArg(newCallbackArgument);
 +    }
 +  }
 +
 +  /**
 +   * @return null if new value is not serialized; otherwise returns a SerializedCacheValueImpl containing the new value.
 +   */
 +  public SerializedCacheValue<?> getSerializedNewValue() {
 +    // In the case where there is a delta that has not been applied yet,
 +    // do not apply it here since it would not produce a serialized new
 +    // value (return null instead to indicate the new value is not
 +    // in serialized form).
 +    @Unretained(ENTRY_EVENT_NEW_VALUE)
 +    final Object tmp = basicGetNewValue();
 +    if (tmp instanceof CachedDeserializable) {
 +      if (tmp instanceof StoredObject) {
 +        if (!((StoredObject) tmp).isSerialized()) {
 +          // TODO OFFHEAP can we handle offheap byte[] better?
 +          return null;
 +        }
 +      }
 +      byte[] bytes = this.newValueBytes;
 +      if (bytes == null) {
 +        bytes = this.cachedSerializedNewValue;
 +      }
 +      return new SerializedCacheValueImpl(this, getRegion(), this.re,
 +          (CachedDeserializable)tmp, bytes);
 +    } else {
 +      // Note we return null even if cachedSerializedNewValue is not null.
 +      // This is because some callers of this method use it to indicate
 +      // that a CacheDeserializable should be created during deserialization.
 +      return null;
 +    }
 +  }
 +  
 +  /**
 +   * Implement this interface if you want to call {@link #exportNewValue}.
 +   * 
 +   * @author darrel
 +   *
 +   */
 +  public interface NewValueImporter {
 +    /**
 +     * @return true if the importer prefers the value to be in serialized form.
 +     */
 +    boolean prefersNewSerialized();
 +
 +    /**
 +     * Only return true if the importer can use the value before the event that exported it is released.
 +     * If false is returned then off-heap values will be copied to the heap for the importer.
 +     * @return true if the importer can deal with the value being an unretained OFF_HEAP_REFERENCE.
 +     */
 +    boolean isUnretainedNewReferenceOk();
 +
 +    /**
 +     * Import a new value that is currently in object form.
 +     * @param nv the new value to import; unretained if isUnretainedNewReferenceOk returns true
 +     * @param isSerialized true if the imported new value represents data that needs to be serialized; false if the imported new value is a simple sequence of bytes.
 +     */
 +    void importNewObject(@Unretained(ENTRY_EVENT_NEW_VALUE) Object nv, boolean isSerialized);
 +
 +    /**
 +     * Import a new value that is currently in byte array form.
 +     * @param nv the new value to import
 +     * @param isSerialized true if the imported new value represents data that needs to be serialized; false if the imported new value is a simple sequence of bytes.
 +     */
 +    void importNewBytes(byte[] nv, boolean isSerialized);
 +  }
 +  
 +  /**
 +   * Export the event's new value to the given importer.
 +   */
 +  public final void exportNewValue(NewValueImporter importer) {
 +    final boolean prefersSerialized = importer.prefersNewSerialized();
 +    if (prefersSerialized) {
 +      if (getCachedSerializedNewValue() != null) {
 +        importer.importNewBytes(getCachedSerializedNewValue(), true);
 +        return;
 +      } else {
 +      if (this.newValueBytes != null && this.newValue instanceof CachedDeserializable) {
 +        importer.importNewBytes(this.newValueBytes, true);
 +        return;
 +      }
 +      }
 +    }
 +    @Unretained(ENTRY_EVENT_NEW_VALUE) 
 +    final Object nv = getRawNewValue();
 +    if (nv instanceof StoredObject) {
 +      @Unretained(ENTRY_EVENT_NEW_VALUE)
 +      final StoredObject so = (StoredObject) nv;
 +      final boolean isSerialized = so.isSerialized();
 +      if (nv instanceof Chunk) {
 +        if (importer.isUnretainedNewReferenceOk()) {
 +          importer.importNewObject(nv, isSerialized);
 +        } else {
 +          if (!isSerialized || prefersSerialized) {
 +            byte[] bytes = so.getValueAsHeapByteArray();
 +            importer.importNewBytes(bytes, isSerialized);
 +            if (isSerialized) {
 +              setCachedSerializedNewValue(bytes);
 +            }
 +          } else {
 +            // TODO OFFHEAP: returns off-heap PdxInstance which is not ok since isUnretainedNewReferenceOk returned false
 +            importer.importNewObject(so.getValueAsDeserializedHeapObject(), true);
 +          }
 +        }
 +      } else {
 +        importer.importNewObject(nv, isSerialized);
 +      }
 +    } else if (nv instanceof byte[]) {
 +      importer.importNewBytes((byte[])nv, false);
 +    } else if (nv instanceof CachedDeserializable) {
 +      CachedDeserializable cd = (CachedDeserializable) nv;
 +      Object cdV = cd.getValue();
 +      if (cdV instanceof byte[]) {
 +        importer.importNewBytes((byte[]) cdV, true);
 +        setCachedSerializedNewValue((byte[]) cdV);
 +      } else {
 +        importer.importNewObject(cdV, true);
 +      }
 +    } else {
 +      importer.importNewObject(nv, true);
 +    }
 +  }
 +  /**
 +   * Implement this interface if you want to call {@link #exportOldValue}.
 +   * 
 +   * @author darrel
 +   *
 +   */
 +  public interface OldValueImporter {
 +    /**
 +     * @return true if the importer prefers the value to be in serialized form.
 +     */
 +    boolean prefersOldSerialized();
 +
 +    /**
 +     * Only return true if the importer can use the value before the event that exported it is released.
 +     * @return true if the importer can deal with the value being an unretained OFF_HEAP_REFERENCE.
 +     */
 +    boolean isUnretainedOldReferenceOk();
 +    
 +    /**
 +     * @return return true if you want the old value to possibly be an instanceof CachedDeserializable; false if you want the value contained in a CachedDeserializable.
 +     */
 +    boolean isCachedDeserializableValueOk();
 +
 +    /**
 +     * Import an old value that is currently in object form.
 +     * @param ov the old value to import; unretained if isUnretainedOldReferenceOk returns true
 +     * @param isSerialized true if the imported old value represents data that needs to be serialized; false if the imported old value is a simple sequence of bytes.
 +     */
 +    void importOldObject(@Unretained(ENTRY_EVENT_OLD_VALUE) Object ov, boolean isSerialized);
 +
 +    /**
 +     * Import an old value that is currently in byte array form.
 +     * @param ov the old value to import
 +     * @param isSerialized true if the imported old value represents data that needs to be serialized; false if the imported old value is a simple sequence of bytes.
 +     */
 +    void importOldBytes(byte[] ov, boolean isSerialized);
 +  }
 +  
 +  /**
 +   * Export the event's old value to the given importer.
 +   */
 +  public final void exportOldValue(OldValueImporter importer) {
 +    final boolean prefersSerialized = importer.prefersOldSerialized();
 +    if (prefersSerialized) {
 +      if (this.oldValueBytes != null && this.oldValue instanceof CachedDeserializable) {
 +        importer.importOldBytes(this.oldValueBytes, true);
 +        return;
 +      }
 +    }
 +    @Unretained(ENTRY_EVENT_OLD_VALUE)
 +    final Object ov = getRawOldValue();
 +    if (ov instanceof StoredObject) {
 +      final StoredObject so = (StoredObject) ov;
 +      final boolean isSerialized = so.isSerialized();
 +      if (ov instanceof Chunk) {
 +        if (importer.isUnretainedOldReferenceOk()) {
 +          importer.importOldObject(ov, isSerialized);
 +        } else {
 +          if (!isSerialized || prefersSerialized) {
 +            importer.importOldBytes(so.getValueAsHeapByteArray(), isSerialized);
 +          } else {
 +            // TODO OFFHEAP: returns off-heap PdxInstance which is not ok since isUnretainedNewReferenceOk returned false
 +           importer.importOldObject(so.getValueAsDeserializedHeapObject(), true);
 +          }
 +        }
 +      } else {
 +        importer.importOldObject(ov, isSerialized);
 +      }
 +    } else if (ov instanceof byte[]) {
 +      importer.importOldBytes((byte[])ov, false);
 +    } else if (!importer.isCachedDeserializableValueOk() && ov instanceof CachedDeserializable) {
 +      CachedDeserializable cd = (CachedDeserializable) ov;
 +      Object cdV = cd.getValue();
 +      if (cdV instanceof byte[]) {
 +        importer.importOldBytes((byte[]) cdV, true);
 +      } else {
 +        importer.importOldObject(cdV, true);
 +      }
 +    } else {
 +      importer.importOldObject(ov, true);
 +    }
 +  }
 +
 +  /**
 +   * If applyDelta is true then first attempt to apply a delta (if we have one) and return the value.
 +   * Else if new value is a Delta return it.
 +   * Else if new value is off-heap return the StoredObject form (unretained OFF_HEAP_REFERENCE). 
 +   * Its refcount is not inced by this call and the returned object can only be safely used for the lifetime of the EntryEventImpl instance that returned the value.
 +   * Else return the raw form.
 +   */
 +  @Unretained(ENTRY_EVENT_NEW_VALUE)
 +  public final Object getRawNewValue(boolean applyDelta) {
 +    if (applyDelta) {
 +      boolean doCopyOnRead = getRegion().isCopyOnRead();
 +      Object newValueWithDelta = applyDeltaWithCopyOnRead(doCopyOnRead);
 +      if (newValueWithDelta != null) {
 +        return newValueWithDelta;
 +      }
 +      // if applyDelta is true and we have already applied the delta then
 +      // just return the applied value instead of the delta object.
 +      @Unretained(ENTRY_EVENT_NEW_VALUE)
 +      Object newValue = basicGetNewValue();
 +      if (newValue != null) return newValue;
 +    }
 +    return getRawNewValue();
 +  }
 +  /**
 +   * Just like getRawNewValue(true) except if the raw new value is off-heap deserialize it.
 +   * Note that in some cases sqlf ignores the request to deserialize.
 +   */
 +  @Unretained(ENTRY_EVENT_NEW_VALUE)
 +  public final Object getNewValueAsOffHeapDeserializedOrRaw() {
 +    Object result = getRawNewValue(true);
 +    if (result instanceof StoredObject) {
 +      result = ((StoredObject) result).getDeserializedForReading();
 +    }
 +    return AbstractRegion.handleNotAvailable(result); // fixes 49499
 +  }
 +
 +  /**
 +   * If the new value is stored off-heap return a retained OFF_HEAP_REFERENCE (caller must release).
 +   * @return a retained OFF_HEAP_REFERENCE if the new value is off-heap; otherwise returns null
 +   */
 +  @Retained(ENTRY_EVENT_NEW_VALUE)
 +  public StoredObject getOffHeapNewValue() {
 +    final Object tmp = basicGetNewValue();
 +    if (tmp instanceof StoredObject) {
 +      StoredObject result = (StoredObject) tmp;
 +      if (!result.retain()) {
 +        return null;
 +      }
 +      return result;
 +    } else {
 +      return null;
 +    }
 +  }
 +  
 +  /**
 +   * If the old value is stored off-heap return a retained OFF_HEAP_REFERENCE (caller must release).
 +   * @return a retained OFF_HEAP_REFERENCE if the old value is off-heap; otherwise returns null
 +   */
 +  @Retained(ENTRY_EVENT_OLD_VALUE)
 +  public StoredObject getOffHeapOldValue() {
 +    final Object tmp = basicGetOldValue();
 +    if (tmp instanceof StoredObject) {
 +      StoredObject result = (StoredObject) tmp;
 +      if (!result.retain()) {
 +        return null;
 +      }
 +      return result;
 +    } else {
 +      return null;
 +    }
 +  }
 +
 +  /**
 +   * Result may be unretained because sqlf getDeserializedForReading returns unretained.
 +   */
 +  public final Object getDeserializedValue() {
 +    if (this.delta == null) {
 +      final Object val = basicGetNewValue();
 +      if (val instanceof StoredObject) {
 +        // TODO OFFHEAP: returns off-heap PdxInstance
 +        return ((StoredObject) val).getValueAsDeserializedHeapObject();
 +      } else 
 +      if (val instanceof CachedDeserializable) {
 +        return ((CachedDeserializable)val).getDeserializedForReading();
 +      }
 +      else {
 +        return val;
 +      }
 +    }
 +    else {
 +      return this.delta;
 +    }
 +  }
 +
 +  public final byte[] getSerializedValue() {
 +    if (this.newValueBytes == null) {
 +      final Object val;
 +      if (this.delta == null) {
 +        val = basicGetNewValue();
 +        if (val instanceof byte[]) {
 +          return (byte[])val;
 +        }
 +        else if (val instanceof CachedDeserializable) {
 +          return ((CachedDeserializable)val).getSerializedValue();
 +        }
 +      }
 +      else {
 +        val = this.delta;
 +      }
 +      try {
 +        return CacheServerHelper.serialize(val);
 +      } catch (IOException ioe) {
 +        throw new GemFireIOException("unexpected exception", ioe);
 +      }
 +    }
 +    else {
 +      return this.newValueBytes;
 +    }
 +  }
 +
 +  /**
 +   * Forces this entry's new value to be in serialized form.
 +   * @since 5.0.2
 +   */
 +  public void makeSerializedNewValue() {
 +    makeSerializedNewValue(false);
 +  }
 +
 +  /**
 +   * @param isSynced true if RegionEntry currently under synchronization
 +   */
 +  private final void makeSerializedNewValue(boolean isSynced) {
 +    Object obj = basicGetNewValue();
 +
 +    // ezoerner:20080611 In the case where there is an unapplied
 +    // delta, do not apply the delta or serialize yet unless entry is
 +    // under synchronization (isSynced is true) 
 +    if (isSynced) {
 +      this.setSerializationDeferred(false);
 +    }
 +    else if (obj == null && this.delta != null) {
 +      // defer serialization until setNewValueInRegion
 +      this.setSerializationDeferred(true);
 +      return;
 +    }
 +    basicSetNewValue(getCachedDeserializable(obj, this));
 +  }
 +
 +  public static Object getCachedDeserializable(Object obj) {
 +    return getCachedDeserializable(obj, null);
 +  }
 +
 +  public static Object getCachedDeserializable(Object obj, EntryEventImpl ev) {
 +    if (obj instanceof byte[]
 +                            || obj == null
 +                            || obj instanceof CachedDeserializable
 +                            || obj == Token.NOT_AVAILABLE
 +                            || Token.isInvalidOrRemoved(obj)
 +                            // don't serialize delta object already serialized
 +                            || obj instanceof com.gemstone.gemfire.Delta
 +                            || obj instanceof Delta) { // internal delta
 +      return obj;
 +    }
 +    final CachedDeserializable cd;
 +    // avoid unneeded serialization of byte[][] used by SQLFabric that
 +    // will end up being deserialized in any case (serialization is cheap
 +    //   for byte[][] anyways)
 +    if (obj instanceof byte[][]) {
 +      int objSize = Sizeable.PER_OBJECT_OVERHEAD + 4;
 +      for (byte[] bytes : (byte[][])obj) {
 +        if (bytes != null) {
 +          objSize += CachedDeserializableFactory.getByteSize(bytes);
 +        }
 +        else {
 +          objSize += Sizeable.PER_OBJECT_OVERHEAD;
 +        }
 +      }
 +      cd = CachedDeserializableFactory.create(obj, objSize);
 +    }
 +    else {
 +      final byte[] b = serialize(obj);
 +      cd = CachedDeserializableFactory.create(b);
 +      if (ev != null) {
 +        ev.newValueBytes = b;
 +        ev.cachedSerializedNewValue = b;
 +      }
 +    }
 +    return cd;
 +  }
 +  public void setCachedSerializedNewValue(byte[] v) {
 +    this.cachedSerializedNewValue = v;
 +  }
 +  public byte[] getCachedSerializedNewValue() {
 +    return this.cachedSerializedNewValue;
 +  }
 +
 +  public final void setSerializedNewValue(byte[] serializedValue) {
 +    Object newVal = null;
 +    if (serializedValue != null) {
 +      if (CachedDeserializableFactory.preferObject()) {
 +        newVal = deserialize(serializedValue);
 +      } else {
 +        newVal = CachedDeserializableFactory.create(serializedValue);
 +      }
 +      if (newVal instanceof Delta) {
 +        this.delta = (Delta)newVal;
 +        newVal = null;
 +        // We need the newValueBytes field and the newValue field to be in sync.
 +        // In the case of non-null delta set both fields to null.
 +        serializedValue = null;
 +      }
 +    }
 +    this.newValueBytes = serializedValue;
 +    basicSetNewValue(newVal);
 +    this.cachedSerializedNewValue = serializedValue;
 +  }
 +
 +  public void setSerializedOldValue(byte[] serializedOldValue){
 +    this.oldValueBytes = serializedOldValue;
 +    final Object ov;
 +    if (CachedDeserializableFactory.preferObject()) {
 +      ov = deserialize(serializedOldValue);
 +    }
 +    else if (serializedOldValue != null) {
 +      ov = CachedDeserializableFactory.create(serializedOldValue);
 +    }
 +    else {
 +      ov = null;
 +    }
 +    retainAndSetOldValue(ov);
 +  }
 +
 +  /**
 +   * If true (the default) then preserve old values in events.
 +   * If false then mark non-null values as being NOT_AVAILABLE.
 +   */
 +  private static final boolean EVENT_OLD_VALUE = !Boolean.getBoolean("gemfire.disable-event-old-value");
 +
 +  
 +  void putExistingEntry(final LocalRegion owner, RegionEntry entry) throws RegionClearedException {
 +    putExistingEntry(owner, entry, false, null);
 +  }
 +  
 +  /**
 +   * Put a newValue into the given, write synced, existing, region entry.
 +   * Sets oldValue in event if hasn't been set yet.
 +   * @param oldValueForDelta Used by Delta Propagation feature
 +   * 
 +   * @throws RegionClearedException
 +   */
 +  void putExistingEntry(final LocalRegion owner, final RegionEntry reentry,
 +     boolean requireOldValue, Object oldValueForDelta) throws RegionClearedException {
 +    makeUpdate();
 +    // only set oldValue if it hasn't already been set to something
 +    if (this.oldValue == null) {
 +      if (!reentry.isInvalidOrRemoved()) {
 +        if (requireOldValue ||
 +            EVENT_OLD_VALUE
 +            || this.region instanceof HARegion // fix for bug 37909
 +            || GemFireCacheImpl.sqlfSystem()
 +            ) {
 +          @Retained Object ov;
 +          if (ReferenceCountHelper.trackReferenceCounts()) {
 +            ReferenceCountHelper.setReferenceCountOwner(new OldValueOwner());
 +            if (GemFireCacheImpl.sqlfSystem()) {
 +              ov = reentry.getValueOffHeapOrDiskWithoutFaultIn(this.region);
 +            } else {
 +              ov = reentry._getValueRetain(owner, true);
 +            }
 +            ReferenceCountHelper.setReferenceCountOwner(null);
 +          } else {
 +            if (GemFireCacheImpl.sqlfSystem()) {
 +              ov = reentry.getValueOffHeapOrDiskWithoutFaultIn(this.region);
 +            } else {
 +              ov = reentry._getValueRetain(owner, true);
 +            }
 +          }
 +          if (ov == null) ov = Token.NOT_AVAILABLE;
 +          // ov has already been retained so call basicSetOldValue instead of retainAndSetOldValue
 +          basicSetOldValue(ov);
 +        } else {
 +          basicSetOldValue(Token.NOT_AVAILABLE);
 +        }
 +      }
 +    }
 +    if (this.oldValue == Token.NOT_AVAILABLE) {
 +      FilterProfile fp = this.region.getFilterProfile();
 +      if (this.op.guaranteesOldValue() || 
 +          (fp != null /* #41532 */&& fp.entryRequiresOldValue(this.getKey()))) {
 +        setOldValueForQueryProcessing();
 +      }
 +    }
 +
 +    //setNewValueInRegion(null);
 +    setNewValueInRegion(owner, reentry, oldValueForDelta);
 +  }
 +
 +  /**
 +   * If we are currently a create op then turn us into an update
 +   *
 +   * @since 5.0
 +   */
 +  void makeUpdate()
 +  {
 +    setOperation(this.op.getCorrespondingUpdateOp());
 +  }
 +
 +  /**
 +   * If we are currently an update op then turn us into a create
 +   *
 +   * @since 5.0
 +   */
 +  void makeCreate()
 +  {
 +    setOperation(this.op.getCorrespondingCreateOp());
 +  }
 +
 +  /**
 +   * Put a newValue into the given, write synced, new, region entry.
 +   * @throws RegionClearedException
 +   */
 +  void putNewEntry(final LocalRegion owner, final RegionEntry reentry)
 +      throws RegionClearedException {
 +    if (!this.op.guaranteesOldValue()) {  // preserves oldValue for CM ops in clients
 +      basicSetOldValue(null);
 +    }
 +    makeCreate();
 +    setNewValueInRegion(owner, reentry, null);
 +  }
 +
 +  void setRegionEntry(RegionEntry re) {
 +    this.re = re;
 +  }
 +
 +  RegionEntry getRegionEntry() {
 +    return this.re;
 +  }
 +
 +  @Retained(ENTRY_EVENT_NEW_VALUE)
 +  private void setNewValueInRegion(final LocalRegion owner,
 +      final RegionEntry reentry, Object oldValueForDelta) throws RegionClearedException {
 +    
 +    boolean wasTombstone = reentry.isTombstone();
 +    
 +    // put in newValue
 +
 +    if (applyDelta(this.op.isCreate())) {
 +      if (this.isSerializationDeferred()) {
 +        makeSerializedNewValue(true);
 +      }
 +    }
 +
 +    // If event contains new value, then it may mean that the delta bytes should
 +    // not be applied. This is possible if the event originated locally.
 +    if (this.deltaBytes != null && this.newValue == null) {
 +      processDeltaBytes(oldValueForDelta);
 +    }
 +
 +    if (owner!=null) {
 +      owner.generateAndSetVersionTag(this, reentry);
 +    } else {
 +      this.region.generateAndSetVersionTag(this, reentry);
 +    }
 +    
 +    Object v = this.newValue;
 +    if (v == null) {
 +      v = isLocalInvalid() ? Token.LOCAL_INVALID : Token.INVALID;
 +    }
 +    else {
 +      this.region.regionInvalid = false;
 +    }
 +
 +    reentry.setValueResultOfSearch(this.op.isNetSearch());
 +
 +    //dsmith:20090524
 +    //This is a horrible hack, but we need to get the size of the object
 +    //When we store an entry. This code is only used when we do a put
 +    //in the primary.
 +    if(v instanceof com.gemstone.gemfire.Delta && region.isUsedForPartitionedRegionBucket()) {
 +      int vSize;
 +      Object ov = basicGetOldValue();
 +      if(ov instanceof CachedDeserializable && !GemFireCacheImpl.DELTAS_RECALCULATE_SIZE) {
 +        vSize = ((CachedDeserializable) ov).getValueSizeInBytes();
 +      } else {
 +        vSize = CachedDeserializableFactory.calcMemSize(v, region.getObjectSizer(), false);
 +      }
 +      v = CachedDeserializableFactory.create(v, vSize);
 +      basicSetNewValue(v);
 +    }
 +
 +    Object preparedV = reentry.prepareValueForCache(this.region, v, this, this.hasDelta());
 +    if (preparedV != v) {
 +      v = preparedV;
 +      if (v instanceof Chunk) {
 +        if (!((Chunk) v).isCompressed()) { // fix bug 52109
 +          // If we put it off heap and it is not compressed then remember that value.
 +          // Otherwise we want to remember the decompressed value in the event.
 +          basicSetNewValue(v);
 +        }
 +      }
 +    }
 +    boolean isTombstone = (v == Token.TOMBSTONE);
 +    boolean success = false;
 +    boolean calledSetValue = false;
 +    try {
 +    setNewValueBucketSize(owner, v);
 +    
 +    // ezoerner:20081030 
 +    // last possible moment to do index maintenance with old value in
 +    // RegionEntry before new value is set.
 +    // As part of an update, this is a remove operation as prelude to an add that
 +    // will come after the new value is set.
 +    // If this is an "update" from INVALID state, treat this as a create instead
 +    // for the purpose of index maintenance since invalid entries are not
 +    // indexed.
 +    
 +    if ((this.op.isUpdate() && !reentry.isInvalid()) || this.op.isInvalidate()) {
 +      IndexManager idxManager = IndexUtils.getIndexManager(this.region, false);
 +      if (idxManager != null) {
 +        try {
 +          idxManager.updateIndexes(reentry,
 +                                   IndexManager.REMOVE_ENTRY,
 +                                   this.op.isUpdate() ?
 +                                     IndexProtocol.BEFORE_UPDATE_OP :
 +                                     IndexProtocol.OTHER_OP);
 +        }
 +        catch (QueryException e) {
 +          throw new IndexMaintenanceException(e);
 +        }
 +      }
 +    }
 +    final IndexUpdater indexUpdater = this.region.getIndexUpdater();
 +    if (indexUpdater != null) {
 +      final LocalRegion indexRegion;
 +      if (owner != null) {
 +        indexRegion = owner;
 +      }
 +      else {
 +        indexRegion = this.region;
 +      }
 +      try {
 +        indexUpdater.onEvent(indexRegion, this, reentry);
 +        calledSetValue = true;
 +        reentry.setValueWithTombstoneCheck(v, this); // already called prepareValueForCache
 +        success = true;
 +      } finally {
 +        indexUpdater.postEvent(indexRegion, this, reentry, success);
 +      }
 +    }
 +    else {
 +      calledSetValue = true;
 +      reentry.setValueWithTombstoneCheck(v, this); // already called prepareValueForCache
 +      success = true;
 +    }
 +    } finally {
 +      if (!success && reentry instanceof OffHeapRegionEntry && v instanceof Chunk) {
 +        OffHeapRegionEntryHelper.releaseEntry((OffHeapRegionEntry)reentry, (Chunk)v);
 +      }      
 +    }
 +    if (logger.isTraceEnabled()) {
 +      if (v instanceof CachedDeserializable) {
 +        logger.trace("EntryEventImpl.setNewValueInRegion: put CachedDeserializable({},{})",
 +            this.getKey(), ((CachedDeserializable)v).getStringForm());
 +      }
 +      else {
 +        logger.trace("EntryEventImpl.setNewValueInRegion: put({},{})",
 +            this.getKey(), StringUtils.forceToString(v));
 +      }
 +    }
 +
 +    if (!isTombstone  &&  wasTombstone) {
 +      owner.unscheduleTombstone(reentry);
 +    }
 +  }
 +
 +  /**
 +   * The size the new value contributes to a pr bucket.
 +   * Note if this event is not on a pr then this value will be 0.
 +   */
 +  private transient int newValueBucketSize;
 +  public int getNewValueBucketSize() {
 +    return this.newValueBucketSize;
 +  }
 +  private void setNewValueBucketSize(LocalRegion lr, Object v) {
 +    if (lr == null) {
 +      lr = this.region;
 +    }
 +    this.newValueBucketSize = lr.calculateValueSize(v);
 +  }
 +
 +  private void processDeltaBytes(Object oldValueInVM) {
 +    if (!this.region.hasSeenEvent(this)) {
 +      if (oldValueInVM == null || Token.isInvalidOrRemoved(oldValueInVM)) {
 +        this.region.getCachePerfStats().incDeltaFailedUpdates();
 +        throw new InvalidDeltaException("Old value not found for key "
 +            + this.keyInfo.getKey());
 +      }
 +      FilterProfile fp = this.region.getFilterProfile();
 +      // If compression is enabled then we've already gotten a new copy due to the
 +      // serializaion and deserialization that occurs.
 +      boolean copy = this.region.getCompressor() == null &&
 +          (this.region.isCopyOnRead()
 +          || this.region.getCloningEnabled()
 +          || (fp != null && fp.getCqCount() > 0));
 +      Object value = oldValueInVM;
 +      boolean wasCD = false;
 +      if (value instanceof CachedDeserializable) {
 +        wasCD = true;
 +        if (copy) {
 +          value = ((CachedDeserializable)value).getDeserializedWritableCopy(this.region, re);
 +        } else {
 +          value = ((CachedDeserializable)value).getDeserializedValue(
 +              this.region, re);
 +        }
 +      } else {
 +        if (copy) {
 +          value = CopyHelper.copy(value);
 +        }
 +      }
 +      boolean deltaBytesApplied = false;
 +      try {
 +        long start = CachePerfStats.getStatTime();
 +        ((com.gemstone.gemfire.Delta)value).fromDelta(new DataInputStream(
 +            new ByteArrayInputStream(getDeltaBytes())));
 +        this.region.getCachePerfStats().endDeltaUpdate(start);
 +        deltaBytesApplied = true;
 +      } catch (RuntimeException rte) {
 +        throw rte;
 +      } catch (VirtualMachineError e) {
 +        SystemFailure.initiateFailure(e);
 +        throw e;
 +      } catch (Throwable t) {
 +        SystemFailure.checkFailure();
 +        throw new DeltaSerializationException(
 +            "Exception while deserializing delta bytes.", t);
 +      } finally {
 +        if (!deltaBytesApplied) {
 +          this.region.getCachePerfStats().incDeltaFailedUpdates();
 +        }
 +      }
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("Delta has been applied for key {}", getKey());
 +      }
 +      // assert event.getNewValue() == null;
 +      if (wasCD) {
 +        CachedDeserializable old = (CachedDeserializable)oldValueInVM;
 +        int valueSize;
 +        if (GemFireCacheImpl.DELTAS_RECALCULATE_SIZE) {
 +          valueSize = CachedDeserializableFactory.calcMemSize(value, region
 +              .getObjectSizer(), false);
 +        } else {
 +          valueSize = old.getValueSizeInBytes();
 +        }
 +        value = CachedDeserializableFactory.create(value, valueSize);
 +      }
 +      setNewValue(value);
 +      if (this.causedByMessage != null
 +          && this.causedByMessage instanceof PutMessage) {
 +        ((PutMessage)this.causedByMessage).setDeltaValObj(value);
 +      }
 +    } else {
 +      this.region.getCachePerfStats().incDeltaFailedUpdates();
 +      throw new InvalidDeltaException(
 +          "Cache encountered replay of event containing delta bytes for key "
 +              + this.keyInfo.getKey());
 +    }
 +  }
 +
 +  void setTXEntryOldValue(Object oldVal, boolean mustBeAvailable) {
 +    if (Token.isInvalidOrRemoved(oldVal)) {
 +      oldVal = null;
 +    }
 +    else {
 +      if (mustBeAvailable || oldVal == null || EVENT_OLD_VALUE) {
 +        // set oldValue to oldVal
 +      }
 +      else {
 +        oldVal = Token.NOT_AVAILABLE;
 +      }
 +    }
 +    retainAndSetOldValue(oldVal);
 +  }
 +
 +  void putValueTXEntry(final TXEntryState tx) {
 +    Object v = basicGetNewValue();
 +    if (v == null) {
 +      if (deltaBytes != null) {
 +        // since newValue is null, and we have deltaBytes
 +        // there must be a nearSidePendingValue
 +        processDeltaBytes(tx.getNearSidePendingValue());
 +        v = basicGetNewValue();
 +      } else if (this.delta != null) {
 +        v = this.delta;
 +      } else {
 +        v = isLocalInvalid() ? Token.LOCAL_INVALID : Token.INVALID;
 +      }
 +    }
 +
 +    if (this.op != Operation.LOCAL_INVALIDATE
 +        && this.op != Operation.LOCAL_DESTROY) {
 +      // fix for bug 34387
 +      tx.setPendingValue(OffHeapHelper.copyIfNeeded(v)); // TODO OFFHEAP optimize
 +    }
 +    tx.setCallbackArgument(getCallbackArgument());
 +  }
 +
 +  /** @return false if entry doesn't exist */
 +  public boolean setOldValueFromRegion()
 +  {
 +    try {
 +      RegionEntry re = this.region.getRegionEntry(getKey());
 +      if (re == null) return false;
 +      ReferenceCountHelper.skipRefCountTracking();
 +      Object v = re._getValueRetain(this.region, true);
 +      ReferenceCountHelper.unskipRefCountTracking();
 +      try {
 +        return setOldValue(v);
 +      } finally {
 +        OffHeapHelper.releaseWithNoTracking(v);
 +      }
 +    }
 +    catch (EntryNotFoundException ex) {
 +      return false;
 +    }
 +  }
 +
 +  /** Return true if old value is the DESTROYED token */
 +  boolean oldValueIsDestroyedToken()
 +  {
 +    return this.oldValue == Token.DESTROYED || this.oldValue == Token.TOMBSTONE;
 +  }
 +
 +  void setOldValueDestroyedToken()
 +  {
 +    basicSetOldValue(Token.DESTROYED);
 +  }
 +
 +  /**
 +   * @return false if value 'v' indicates that entry does not exist
 +   */
 +  public boolean setOldValue(Object v) {
 +    return setOldValue(v, false);
 +  }
 +  
 +  
 +  /**
 +   * @param force true if the old value should be forcibly set, used
 +   * for HARegions, methods like putIfAbsent, etc.,
 +   * where the old value must be available.
 +   * @return false if value 'v' indicates that entry does not exist
 +   */
 +  public boolean setOldValue(Object v, boolean force) {
 +    if (v == null || Token.isRemoved(v)) {
 +      return false;
 +    }
 +    else {
 +      if (Token.isInvalid(v)) {
 +        v = null;
 +      }
 +      else {
 +        if (force ||
 +            (this.region instanceof HARegion) // fix for bug 37909
 +            ) {
 +          // set oldValue to "v".
 +        } else if (EVENT_OLD_VALUE) {
 +          // TODO Rusty add compression support here
 +          // set oldValue to "v".
 +        } else {
 +          v = Token.NOT_AVAILABLE;
 +        }
 +      }
 +      retainAndSetOldValue(v);
 +      return true;
 +    }
 +  }
 +
 +  /**
 +   * sets the old value for concurrent map operation results received
 +   * from a server.
 +   */
 +  public void setConcurrentMapOldValue(Object v) {
 +    if (Token.isRemoved(v)) {
 +      return;
 +    } else {
 +      if (Token.isInvalid(v)) {
 +        v = null;
 +      }   
 +      retainAndSetOldValue(v);
 +    }
 +  }
 +
 +  /** Return true if new value available */
 +  public boolean hasNewValue() {
 +    Object tmp = this.newValue;
 +    if (tmp == null && hasDelta()) {
 +      // ???:ezoerner:20080611 what if applying the delta would produce
 +      // null or (strangely) NOT_AVAILABLE.. do we need to apply it here to
 +      // find out?
 +      return true;
 +    }
 +    return  tmp != null && tmp != Token.NOT_AVAILABLE;
 +  }
 +
 +  public final boolean hasOldValue() {
 +    return this.oldValue != null && this.oldValue != Token.NOT_AVAILABLE;
 +  }
 +  public final boolean isOldValueAToken() {
 +    return this.oldValue instanceof Token;
 +  }
 +
 +  /**
 +   * This should only be used in case of internal delta and <B>not for Delta of
 +   * Delta Propagation feature</B>.
 +   * 
 +   * @return boolean
 +   */
 +  public boolean hasDelta() {
 +    return (this.delta != null);
 +  }
 +
 +  public boolean isOldValueAvailable() {
 +    if (isOriginRemote() && this.region.isProxy()) {
 +      return false;
 +    } else {
 +      return basicGetOldValue() != Token.NOT_AVAILABLE;
 +    }
 +  }
 +  
 +  public void oldValueNotAvailable() {
 +    basicSetOldValue(Token.NOT_AVAILABLE);
 +  }
 +
 +  public static Object deserialize(byte[] bytes) {
 +    return deserialize(bytes, null, null);
 +  }
 +
 +  public static Object deserialize(byte[] bytes, Version version,
 +      ByteArrayDataInput in) {
 +    if (bytes == null)
 +      return null;
 +    try {
 +      return BlobHelper.deserializeBlob(bytes, version, in);
 +    }
 +    catch (IOException e) {
 +      throw new SerializationException(LocalizedStrings.EntryEventImpl_AN_IOEXCEPTION_WAS_THROWN_WHILE_DESERIALIZING.toLocalizedString(), e);
 +    }
 +    catch (ClassNotFoundException e) {
 +      // fix for bug 43602
 +      throw new SerializationException(LocalizedStrings.EntryEventImpl_A_CLASSNOTFOUNDEXCEPTION_WAS_THROWN_WHILE_TRYING_TO_DESERIALIZE_CACHED_VALUE.toLocalizedString(), e);
 +    }
 +  }
 +
 +  /**
 +   * If a PdxInstance is returned then it will have an unretained reference
 +   * to Chunk's off-heap address.
 +   */
 +  public static @Unretained Object deserializeChunk(Chunk bytes) {
 +    if (bytes == null)
 +      return null;
 +    try {
 +      return BlobHelper.deserializeOffHeapBlob(bytes);
 +    }
 +    catch (IOException e) {
 +      throw new SerializationException(LocalizedStrings.EntryEventImpl_AN_IOEXCEPTION_WAS_THROWN_WHILE_DESERIALIZING.toLocalizedString(), e);
 +    }
 +    catch (ClassNotFoundException e) {
 +      // fix for bug 43602
 +      throw new SerializationException(LocalizedStrings.EntryEventImpl_A_CLASSNOTFOUNDEXCEPTION_WAS_THROWN_WHILE_TRYING_TO_DESERIALIZE_CACHED_VALUE.toLocalizedString(), e);
 +    }
 +  }
 +
 +  /**
 +   * Serialize an object into a <code>byte[]</code>
 +   *
 +   * @throws IllegalArgumentException
 +   *           If <code>obj</code> should not be serialized
 +   */
 +  public static byte[] serialize(Object obj) {
 +    return serialize(obj, null);
 +  }
 +
 +   /**
 +     * Serialize an object into a <code>byte[]</code>
 +     *
 +     * @throws IllegalArgumentException
 +     *           If <code>obj</code> should not be serialized
 +     */
 +  public static byte[] serialize(Object obj, Version version)
 +  {
 +    if (obj == null || obj == Token.NOT_AVAILABLE
 +        || Token.isInvalidOrRemoved(obj))
 +      throw new IllegalArgumentException(LocalizedStrings.EntryEventImpl_MUST_NOT_SERIALIZE_0_IN_THIS_CONTEXT.toLocalizedString(obj));
 +    try {
 +      return BlobHelper.serializeToBlob(obj, version);
 +    }
 +    catch (IOException e) {
 +      throw new SerializationException(LocalizedStrings.EntryEventImpl_AN_IOEXCEPTION_WAS_THROWN_WHILE_SERIALIZING.toLocalizedString(), e);
 +    }
 +  }
 +  
 +  
 +  /**
 +   * Serialize an object into a <code>byte[]</code> . If the byte array
 +   * provided by the wrapper is sufficient to hold the data, it is used
 +   * otherwise a new byte array gets created & its reference is stored in the
 +   * wrapper. The User Bit is also appropriately set as Serialized
 +   * 
 +   * @param wrapper
 +   *                Object of type BytesAndBitsForCompactor which is used to fetch
 +   *                the serialized data. The byte array of the wrapper is used
 +   *                if possible else a the new byte array containing the data is
 +   *                set in the wrapper.
 +   * @throws IllegalArgumentException
 +   *                 If <code>obj</code> should not be serialized
 +   */
 +  public static void fillSerializedValue(BytesAndBitsForCompactor wrapper,
 +                                         Object obj, byte userBits) {
 +    if (obj == null || obj == Token.NOT_AVAILABLE
 +        || Token.isInvalidOrRemoved(obj))
 +      throw new IllegalArgumentException(
 +        LocalizedStrings.EntryEvents_MUST_NOT_SERIALIZE_0_IN_THIS_CONTEXT.toLocalizedString(obj));
 +    try {
 +      HeapDataOutputStream hdos = null;
 +      if (wrapper.getBytes().length < 32) {
 +        hdos = new HeapDataOutputStream(Version.CURRENT);
 +      }
 +      else {
 +        hdos = new HeapDataOutputStream(wrapper.getBytes());
 +      }
 +      DataSerializer.writeObject(obj, hdos);
 +      // return hdos.toByteArray();
 +      hdos.sendTo(wrapper, userBits);
 +    }
 +    catch (IOException e) {
 +      RuntimeException e2 = new IllegalArgumentException(
 +        LocalizedStrings.EntryEventImpl_AN_IOEXCEPTION_WAS_THROWN_WHILE_SERIALIZING.toLocalizedString());
 +      e2.initCause(e);
 +      throw e2;
 +    }
 +  }
 +
 +  protected String getShortClassName() {
 +    String cname = getClass().getName();
 +    return cname.substring(getClass().getPackage().getName().length()+1);
 +  }
 +
 +  @Override
 +  public String toString() {
 +    StringBuilder buf = new StringBuilder();
 +    buf.append(getShortClassName());
 +    buf.append("[");
 +
 +    buf.append("op=");
 +    buf.append(getOperation());
 +    buf.append(";key=");
 +    buf.append(this.getKey());
 +    buf.append(";oldValue=");
 +    try {
 +      ArrayUtils.objectStringNonRecursive(basicGetOldValue(), buf);
 +    } catch (IllegalStateException ex) {
 +      buf.append("OFFHEAP_VALUE_FREED");
 +    }
 +    buf.append(";newValue=");
 +    try {
 +      ArrayUtils.objectStringNonRecursive(basicGetNewValue(), buf);
 +    } catch (IllegalStateException ex) {
 +      buf.append("OFFHEAP_VALUE_FREED");
 +    }
 +    buf.append(";callbackArg=");
 +    buf.append(this.getRawCallbackArgument());
 +    buf.append(";originRemote=");
 +    buf.append(isOriginRemote());
 +    buf.append(";originMember=");
 +    buf.append(getDistributedMember());
 +//    if (this.partitionMessage != null) {
 +//      buf.append("; partitionMessage=");
 +//      buf.append(this.partitionMessage);
 +//    }
 +    if (this.isPossibleDuplicate()) {
 +      buf.append(";posDup");
 +    }
 +    if (callbacksInvoked()) { 
 +      buf.append(";callbacksInvoked");
 +    }
 +    if (this.versionTag != null) {
 +      buf.append(";version=").append(this.versionTag);
 +    }
 +    if (getContext() != null) {
 +      buf.append(";context=");
 +      buf.append(getContext());
 +    }
 +    if (this.eventID != null) {
 +      buf.append(";id=");
 +      buf.append(this.eventID);
 +    }
 +    if (this.deltaBytes != null) {
 +      buf.append(";[" + this.deltaBytes.length + " deltaBytes]");
 +    }
 +//    else {
 +//      buf.append(";[no deltaBytes]");
 +//    }
 +    if (this.filterInfo != null) {
 +      buf.append(";routing=");
 +      buf.append(this.filterInfo);
 +    }
 +    if (this.isFromServer()) {
 +      buf.append(";isFromServer");
 +    }
 +    if (this.isConcurrencyConflict()) {
 +      buf.append(";isInConflict");
 +    }
 +    if (this.getInhibitDistribution()) {
 +      buf.append(";inhibitDistribution");
 +    }
 +    buf.append("]");
 +    return buf.toString();
 +  }
 +
 +  public int getDSFID() {
 +    return ENTRY_EVENT;
 +  }
 +
 +  public void toData(DataOutput out) throws IOException
 +  {
 +    DataSerializer.writeObject(this.eventID, out);    
 +    DataSerializer.writeObject(this.getKey(), out);
 +    DataSerializer.writeObject(this.keyInfo.getValue(), out);
 +    out.writeByte(this.op.ordinal);
 +    out.writeShort(this.eventFlags & EventFlags.FLAG_TRANSIENT_MASK);
 +    DataSerializer.writeObject(this.getRawCallbackArgument(), out);
 +    DataSerializer.writeObject(this.txId, out);
 +
 +    {
 +      boolean isDelta = this.delta != null;
 +      out.writeBoolean(isDelta);
 +      if (isDelta) {
 +        DataSerializer.writeObject(this.delta, out);
 +      }
 +      else {
 +        Object nv = basicGetNewValue();
 +        boolean newValueSerialized = nv instanceof CachedDeserializable;
 +        if (newValueSerialized) {
 +          if (nv instanceof StoredObject) {
 +            newValueSerialized = ((StoredObject) nv).isSerialized();
 +          }
 +        }
 +        out.writeBoolean(newValueSerialized);
 +        if (newValueSerialized) {
 +          if (this.newValueBytes != null) {
 +            DataSerializer.writeByteArray(this.newValueBytes, out);
 +          } else if (this.cachedSerializedNewValue != null) {
 +            DataSerializer.writeByteArray(this.cachedSerializedNewValue, out);
 +          } else {
 +            CachedDeserializable cd = (CachedDeserializable)nv;
 +            DataSerializer.writeObjectAsByteArray(cd.getValue(), out);
 +          }
 +        }
 +        else {
 +          DataSerializer.writeObject(nv, out);
 +        }
 +      }  
 +    }
 +
 +    {
 +      Object ov = basicGetOldValue();
 +      boolean oldValueSerialized = ov instanceof CachedDeserializable;
 +      if (oldValueSerialized) {
 +        if (ov instanceof StoredObject) {
 +          oldValueSerialized = ((StoredObject) ov).isSerialized();
 +        }
 +      }
 +      out.writeBoolean(oldValueSerialized);
 +      if (oldValueSerialized) {
 +        if (this.oldValueBytes != null) {
 +          DataSerializer.writeByteArray(this.oldValueBytes, out);
 +        }
 +        else {
 +          CachedDeserializable cd = (CachedDeserializable)ov;
 +          DataSerializer.writeObjectAsByteArray(cd.getValue(), out);
 +        }
 +      }
 +      else {
 +        DataSerializer.writeObject(ov, out);
 +      }
 +    }
 +    InternalDataSerializer.invokeToData((InternalDistributedMember)this.distributedMember, out);
 +    DataSerializer.writeObject(getContext(), out);
 +    DataSerializer.writeLong(tailKey, out);
 +  }
 +
 +  private static abstract class EventFlags
 +   {
 +    private static final short FLAG_ORIGIN_REMOTE = 0x01;
 +    // localInvalid: true if a null new value should be treated as a local
 + 

<TRUNCATED>


[20/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/redis/GemFireRedisServer.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/redis/GemFireRedisServer.java
index e90b724,0000000..1f7e7a9
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/redis/GemFireRedisServer.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/redis/GemFireRedisServer.java
@@@ -1,717 -1,0 +1,717 @@@
 +/*
 + * 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.redis;
 +
 +import io.netty.bootstrap.ServerBootstrap;
 +import io.netty.buffer.PooledByteBufAllocator;
 +import io.netty.channel.Channel;
 +import io.netty.channel.ChannelFuture;
 +import io.netty.channel.ChannelInitializer;
 +import io.netty.channel.ChannelOption;
 +import io.netty.channel.ChannelPipeline;
 +import io.netty.channel.EventLoopGroup;
 +import io.netty.channel.ServerChannel;
 +import io.netty.channel.nio.NioEventLoopGroup;
 +import io.netty.channel.oio.OioEventLoopGroup;
 +import io.netty.channel.socket.SocketChannel;
 +import io.netty.channel.socket.nio.NioServerSocketChannel;
 +import io.netty.channel.socket.oio.OioServerSocketChannel;
 +import io.netty.util.concurrent.Future;
 +
 +import java.io.IOException;
 +import java.net.InetAddress;
 +import java.net.InetSocketAddress;
 +import java.net.UnknownHostException;
 +import java.util.Collection;
 +import java.util.Map.Entry;
 +import java.util.concurrent.ConcurrentHashMap;
 +import java.util.concurrent.ConcurrentMap;
 +import java.util.concurrent.Executors;
 +import java.util.concurrent.ScheduledExecutorService;
 +import java.util.concurrent.ScheduledFuture;
 +import java.util.concurrent.ThreadFactory;
 +import java.util.concurrent.atomic.AtomicInteger;
 +
 +import com.gemstone.gemfire.LogWriter;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheFactory;
 +import com.gemstone.gemfire.cache.EntryEvent;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionDestroyedException;
 +import com.gemstone.gemfire.cache.RegionFactory;
 +import com.gemstone.gemfire.cache.RegionShortcut;
 +import com.gemstone.gemfire.cache.util.CacheListenerAdapter;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.internal.SocketCreator;
 +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
 +import com.gemstone.gemfire.internal.redis.ByteArrayWrapper;
 +import com.gemstone.gemfire.internal.redis.ByteToCommandDecoder;
 +import com.gemstone.gemfire.internal.redis.Coder;
 +import com.gemstone.gemfire.internal.redis.ExecutionHandlerContext;
 +import com.gemstone.gemfire.internal.redis.RedisDataType;
 +import com.gemstone.gemfire.internal.redis.RegionProvider;
- import com.gemstone.gemfire.internal.redis.executor.hll.HyperLogLogPlus;
++import com.gemstone.gemfire.internal.hll.HyperLogLogPlus;
 +
 +/**
 + * The GemFireRedisServer is a server that understands the Redis protocol. As
 + * commands are sent to the server, each command is picked up by a thread,
 + * interpreted and then executed and a response is sent back to the client. The
 + * default connection port is 6379 but that can be altered when run through GFSH
 + * or started through the provided static main class.
 + * <p>
 + * Each Redis data type instance is stored in a separate {@link Region} except
 + * for the Strings and HyperLogLogs which are collectively stored in one Region
 + * respectively. That Region along with a meta data region used internally are 
 + * protected so the client may not store keys with the name {@link GemFireRedisServer#REDIS_META_DATA_REGION}
 + * or {@link GemFireRedisServer#STRING_REGION}. The default Region type is 
 + * {@link RegionShortcut#PARTITION} although this can be changed by specifying the
 + * SystemProperty {@value #DEFAULT_REGION_SYS_PROP_NAME} to a type defined by {@link RegionShortcut}.
 + * If the {@link GemFireRedisServer#NUM_THREADS_SYS_PROP_NAME} system property is set to 0,
 + * one thread per client will be created. Otherwise a worker thread pool of specified size is
 + * used or a default size of 4 * {@link Runtime#availableProcessors()} if the property is not set.
 + * <p>
 + * Setting the AUTH password requires setting the property "redis-password" just as "redis-port"
 + * would be in xml or through GFSH.
 + * <p>
 + * The supported commands are as follows:
 + * <p>
 + * Supported String commands - APPEND, BITCOUNT, BITOP, BITPOS, DECR, DECRBY, 
 + * GET, GETBIT, GETRANGE, GETSET, INCR, INCRBY, INCRBYFLOAT, MGET, MSET, MSETNX,
 + * PSETEX, SET, SETBIT, SETEX, SETNX, STRLEN
 + * <p>
 + * Supported List commands - LINDEX, LLEN, LPOP, LPUSH, LPUSHX, LRANGE,
 + * LREM, LSET, LTRIM, RPOP, RPUSH, RPUSHX
 + * <p>
 + * Supported Hash commands - HDEL, HEXISTS, HGET, HGETALL, HINCRBY, HINCRBYFLOAT,
 + * HKEYS, HMGET, HMSET, HSETNX, HLEN, HSCAN, HSET, HVALS
 + * <p>
 + * Supported Set commands - SADD, SCARD, SDIFF, SDIFFSTORE, SINTER,
 + * SINTERSTORE, SISMEMBER, SMEMBERS, SMOVE, SREM, SPOP, SRANDMEMBER, 
 + * SCAN, SUNION, SUNIONSTORE
 + * <p>
 + * Supported SortedSet commands - ZADD, ZCARD, ZCOUNT, ZINCRBY, ZLEXCOUNT,
 + * ZRANGE, ZRANGEBYLEX, ZRANGEBYSCORE, ZRANK, ZREM, ZREMRANGEBYLEX,
 + * ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZREVRANGE, ZREVRANGEBYSCORE, ZREVRANK,
 + * ZSCAN, ZSCORE
 + * <p>
 + * Supported HyperLogLog commands - PFADD, PFCOUNT, PFMERGE
 + * <p>
 + * Supported Keys commands - DEL, DBSIZE, EXISTS, EXPIRE, EXPIREAT, FLUSHDB, FLUSHALL, 
 + * KEYS, PERSIST, PEXPIRE, PEXPIREAT, PTTL, SCAN, TTL
 + * <p>
 + * Supported Transaction commands - DISCARD, EXEC, MULTI
 + * <P>
 + * Supported Server commands - AUTH, ECHO, PING, TIME, QUIT
 + * <p>
 + * <p>
 + * The command executors are not explicitly documented but the functionality
 + * can be found at <a href="http://redis.io/commands">Redis Commands</a>
 + * <p>
 + * Exceptions to the Redis Commands Documents:<p>
 + * <ul>
 + * <li>Any command that removes keys and returns a count of removed
 + * entries will not return a total remove count but rather a count of how
 + * many entries have been removed that existed on the local vm, though 
 + * all entries will be removed</li>
 + * <li>Any command that returns a count of newly set members has an
 + * unspecified return value. The command will work just as the Redis protocol
 + * states but the count will not necessary reflect the number set compared
 + * to overridden.</li>
 + * <li>Transactions work just as they would on a Redis instance, they are local
 + * transaction. Transactions cannot be executed on data that is not local to the 
 + * executing server, that is on a partitioned region in a different server 
 + * instance or on a persistent region that does not have transactions enabled.
 + * Also, you cannot watch or unwatch keys as all keys within a GemFire 
 + * transaction are watched by default.</li>
 + * </ul>
 + * @author Vitaliy Gavrilov
 + *
 + */
 +
 +public class GemFireRedisServer {
 +
 +  /**
 +   * Thread used to start main method
 +   */
 +  private static Thread mainThread = null;
 +
 +  /**
 +   * The default Redis port as specified by their protocol, {@value #DEFAULT_REDIS_SERVER_PORT}
 +   */
 +  public static final int DEFAULT_REDIS_SERVER_PORT = 6379;
 +
 +  /**
 +   * The number of threads that will work on handling requests
 +   */
 +  private final int numWorkerThreads;
 +
 +  /**
 +   * The number of threads that will work socket selectors
 +   */
 +  private final int numSelectorThreads;
 +
 +  /**
 +   * The actual port being used by the server
 +   */
 +  private final int serverPort;
 +
 +  /**
 +   * The address to bind to
 +   */
 +  private final String bindAddress;
 +
 +  /**
 +   * Connection timeout in milliseconds
 +   */
 +  private static final int connectTimeoutMillis = 1000;
 +
 +  /**
 +   * Temporary constant whether to use old single thread per connection
 +   * model for worker group
 +   */
 +  private boolean singleThreadPerConnection;
 +
 +  /**
 +   * Logging level
 +   */
 +  private final String logLevel;
 +
 +  /**
 +   * The cache instance pointer on this vm
 +   */
 +  private Cache cache;
 +
 +  /**
 +   * Channel to be closed when shutting down
 +   */
 +  private Channel serverChannel;
 +
 +  /**
 +   * Gem logwriter
 +   */
 +  private LogWriter logger;
 +
 +  private RegionProvider regionCache;
 +
 +  private final MetaCacheListener metaListener;
 +
 +  private EventLoopGroup bossGroup;
 +  private EventLoopGroup workerGroup;
 +  private final static int numExpirationThreads = 1;
 +  private final ScheduledExecutorService expirationExecutor;
 +
 +  /**
 +   * Map of futures to be executed for key expirations
 +   */
 +  private final ConcurrentMap<ByteArrayWrapper, ScheduledFuture<?>> expirationFutures;
 +
 +
 +  /**
 +   * The field that defines the name of the {@link Region} which holds all of
 +   * the strings. The current value of this field is {@value #STRING_REGION}.
 +   */
 +  public static final String STRING_REGION = "__StRiNgS";
 +
 +  /**
 +   * The field that defines the name of the {@link Region} which holds all of
 +   * the HyperLogLogs. The current value of this field is {@value #HLL_REGION}.
 +   */
 +  public static final String HLL_REGION = "__HlL";
 +
 +  /**
 +   * The field that defines the name of the {@link Region} which holds all of
 +   * the Redis meta data. The current value of this field is {@value #REDIS_META_DATA_REGION}.
 +   */
 +  public static final String REDIS_META_DATA_REGION = "__ReDiS_MeTa_DaTa";
 +
 +  /**
 +   * The system property name used to set the default {@link Region} creation
 +   * type. The property name is {@value #DEFAULT_REGION_SYS_PROP_NAME} and the
 +   * acceptable values are types defined by {@link RegionShortcut}, 
 +   * i.e. "PARTITION" would be used for {@link RegionShortcut#PARTITION}.
 +   */
 +  public static final String DEFAULT_REGION_SYS_PROP_NAME = "gemfireredis.regiontype";
 +
 +  /**
 +   * System property name that can be used to set the number of threads to be
 +   * used by the GemFireRedisServer
 +   */
 +  public static final String NUM_THREADS_SYS_PROP_NAME = "gemfireredis.numthreads";
 +
 +  /**
 +   * The actual {@link RegionShortcut} type specified by the system property
 +   * {@value #DEFAULT_REGION_SYS_PROP_NAME}.
 +   */
 +  public final RegionShortcut DEFAULT_REGION_TYPE;
 +
 +  private boolean shutdown;
 +  private boolean started;
 +
 +  /**
 +   * Determine the {@link RegionShortcut} type from a String value.
 +   * If the String value doesn't map to a RegionShortcut type then 
 +   * {@link RegionShortcut#PARTITION} will be used by default.
 +   * 
 +   * @return {@link RegionShortcut}
 +   */
 +  private static RegionShortcut setRegionType() {
 +    String regionType = System.getProperty(DEFAULT_REGION_SYS_PROP_NAME, "PARTITION");
 +    RegionShortcut type;
 +    try {
 +      type = RegionShortcut.valueOf(regionType);
 +    } catch (Exception e) {
 +      type = RegionShortcut.PARTITION;
 +    }
 +    return type;
 +  }
 +
 +  /**
 +   * Helper method to set the number of worker threads
 +   * 
 +   * @return If the System property {@value #NUM_THREADS_SYS_PROP_NAME} is set then that number
 +   * is used, otherwise 4 * # of cores
 +   */
 +  private int setNumWorkerThreads() {
 +    String prop = System.getProperty(NUM_THREADS_SYS_PROP_NAME);
 +    int numCores = Runtime.getRuntime().availableProcessors();
 +    int def = 4 * numCores;
 +    if (prop == null || prop.isEmpty())
 +      return def;
 +    int threads;
 +    try {
 +      threads = Integer.parseInt(prop);
 +    } catch (NumberFormatException e) {
 +      return def;
 +    }
 +    return threads;
 +  }
 +
 +  /**
 +   * Constructor for {@link GemFireRedisServer} that will start the
 +   * server on the given port and bind to the first non-loopback address
 +   * 
 +   * @param port The port the server will bind to, will use {@value #DEFAULT_REDIS_SERVER_PORT} by default
 +   */
 +  public GemFireRedisServer(int port) {
 +    this(null, port, null);
 +  }
 +
 +  /**
 +   * Constructor for {@link GemFireRedisServer} that will start the
 +   * server and bind to the given address and port
 +   * 
 +   * @param bindAddress The address to which the server will attempt to bind to
 +   * @param port The port the server will bind to, will use {@value #DEFAULT_REDIS_SERVER_PORT} by default if argument is less than or equal to 0
 +   */
 +  public GemFireRedisServer(String bindAddress, int port) {
 +    this(bindAddress, port, null);
 +  }
 +
 +
 +  /**
 +   * Constructor for {@link GemFireRedisServer} that will start the
 +   * server and bind to the given address and port. Keep in mind that the
 +   * log level configuration will only be set if a {@link Cache} does not already
 +   * exist, if one already exists then setting that property will have no effect.
 +   * 
 +   * @param bindAddress The address to which the server will attempt to bind to
 +   * @param port The port the server will bind to, will use {@value #DEFAULT_REDIS_SERVER_PORT} by default if argument is less than or equal to 0
 +   * @param logLevel The logging level to be used by GemFire
 +   */
 +  public GemFireRedisServer(String bindAddress, int port, String logLevel) {
 +    if (port <= 0)
 +      this.serverPort = DEFAULT_REDIS_SERVER_PORT;
 +    else
 +      this.serverPort = port;
 +    this.bindAddress = bindAddress;
 +    this.logLevel = logLevel;
 +    this.numWorkerThreads = setNumWorkerThreads();
 +    if (this.numWorkerThreads == 0)
 +      this.singleThreadPerConnection = true;
 +    this.numSelectorThreads = 1;
 +    this.metaListener = new MetaCacheListener();
 +    this.expirationFutures = new ConcurrentHashMap<ByteArrayWrapper, ScheduledFuture<?>>();
 +    this.expirationExecutor = Executors.newScheduledThreadPool(numExpirationThreads, new ThreadFactory() {
 +      private final AtomicInteger counter = new AtomicInteger();
 +      @Override
 +      public Thread newThread(Runnable r) {
 +        Thread t = new Thread(r);
 +        t.setName("GemFireRedis-ScheduledExecutor-" + counter.incrementAndGet());
 +        t.setDaemon(true);
 +        return t;
 +      }
 +
 +    });
 +    this.DEFAULT_REGION_TYPE = setRegionType();
 +    this.shutdown = false;
 +    this.started = false;
 +  }
 +
 +  /**
 +   * Helper method to get the host name to bind to
 +   * 
 +   * @return The InetAddress to bind to
 +   * @throws UnknownHostException
 +   */
 +  private InetAddress getBindAddress() throws UnknownHostException {
 +    return this.bindAddress == null || this.bindAddress.isEmpty()
 +        ? SocketCreator.getLocalHost()
 +            : InetAddress.getByName(this.bindAddress);
 +  }
 +
 +  /**
 +   * This is function to call on a {@link GemFireRedisServer} instance
 +   * to start it running
 +   */
 +  public synchronized void start() {
 +    if (!started) {
 +      try {
 +        startGemFire();
 +        initializeRedis();
 +        startRedisServer();
 +      } catch (IOException e) {
 +        throw new RuntimeException("Could not start Server", e);
 +      } catch (InterruptedException e) {
 +        throw new RuntimeException("Could not start Server", e);
 +      }
 +      started = true;
 +    }
 +  }
 +
 +  /**
 +   * Initializes the {@link Cache}, and creates Redis necessities
 +   * Region and protects declares that {@link Region} to be protected. 
 +   * Also, every {@link GemFireRedisServer} will check for entries already in the 
 +   * meta data Region.
 +   */
 +  private void startGemFire() {
 +    Cache c = GemFireCacheImpl.getInstance();
 +    if (c == null) {
 +      synchronized (GemFireRedisServer.class) {
 +        c = GemFireCacheImpl.getInstance();
 +        if (c == null) {
 +          CacheFactory cacheFactory = new CacheFactory();
 +          if (logLevel != null)
 +            cacheFactory.set("log-level", logLevel);
 +          c = cacheFactory.create();
 +        }
 +      }
 +    }
 +    this.cache = c;
 +    this.logger = c.getLogger();
 +  }
 +
 +  private void initializeRedis() {
 +    synchronized (this.cache) {
 +      RegionFactory<String, RedisDataType> rfMeta = cache.createRegionFactory(RegionShortcut.REPLICATE);
 +      rfMeta.addCacheListener(this.metaListener);
 +      RegionFactory<ByteArrayWrapper, ByteArrayWrapper> rfString = cache.createRegionFactory(DEFAULT_REGION_TYPE);
 +      RegionFactory<ByteArrayWrapper, HyperLogLogPlus> rfHLL = cache.createRegionFactory(DEFAULT_REGION_TYPE);
 +      Region<ByteArrayWrapper, ByteArrayWrapper> stringsRegion;
 +      if ((stringsRegion = this.cache.getRegion(STRING_REGION)) == null)
 +        stringsRegion = rfString.create(GemFireRedisServer.STRING_REGION);
 +      Region<ByteArrayWrapper, HyperLogLogPlus> hLLRegion;
 +      if ((hLLRegion = this.cache.getRegion(HLL_REGION)) == null)
 +        hLLRegion = rfHLL.create(HLL_REGION);
 +      Region<String, RedisDataType> redisMetaData;
 +      if ((redisMetaData = this.cache.getRegion(REDIS_META_DATA_REGION)) == null)
 +        redisMetaData = rfMeta.create(REDIS_META_DATA_REGION);
 +      this.regionCache = new RegionProvider(stringsRegion, hLLRegion, redisMetaData, expirationFutures, expirationExecutor, this.DEFAULT_REGION_TYPE);
 +      redisMetaData.put(REDIS_META_DATA_REGION, RedisDataType.REDIS_PROTECTED);
 +      redisMetaData.put(HLL_REGION, RedisDataType.REDIS_PROTECTED);
 +      redisMetaData.put(STRING_REGION, RedisDataType.REDIS_PROTECTED);
 +    }
 +    checkForRegions();
 +  }
 +
 +  private void checkForRegions() {
 +    Collection<Entry<String, RedisDataType>> entrySet = this.regionCache.metaEntrySet();
 +    for (Entry<String, RedisDataType> entry: entrySet) {
 +      String regionName = entry.getKey();
 +      RedisDataType type = entry.getValue();
 +      Region<?, ?> newRegion = cache.getRegion(regionName);
 +      if (newRegion == null && type != RedisDataType.REDIS_STRING && type != RedisDataType.REDIS_HLL && type != RedisDataType.REDIS_PROTECTED) {
 +        try {
 +          this.regionCache.createRemoteRegionReferenceLocally(Coder.stringToByteArrayWrapper(regionName), type);
 +        } catch (Exception e) {
 +          if (logger.errorEnabled())
 +            logger.error(e);
 +        }
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Helper method to start the server listening for connections. The
 +   * server is bound to the port specified by {@link GemFireRedisServer#serverPort}
 +   * 
 +   * @throws IOException
 +   * @throws InterruptedException
 +   */
 +  private void startRedisServer() throws IOException, InterruptedException {
 +    ThreadFactory selectorThreadFactory = new ThreadFactory() {
 +      private final AtomicInteger counter = new AtomicInteger();
 +      @Override
 +      public Thread newThread(Runnable r) {
 +        Thread t = new Thread(r);
 +        t.setName("GemFireRedisServer-SelectorThread-" + counter.incrementAndGet());
 +        t.setDaemon(true);
 +        return t;
 +      }
 +
 +    };
 +
 +    ThreadFactory workerThreadFactory = new ThreadFactory() {
 +      private final AtomicInteger counter = new AtomicInteger();
 +      @Override
 +      public Thread newThread(Runnable r) {
 +        Thread t = new Thread(r);
 +        t.setName("GemFireRedisServer-WorkerThread-" + counter.incrementAndGet());
 +        return t;
 +      }
 +
 +    };
 +
 +    bossGroup = null;
 +    workerGroup = null;
 +    Class<? extends ServerChannel> socketClass = null;
 +    if (singleThreadPerConnection) {
 +      bossGroup = new OioEventLoopGroup(Integer.MAX_VALUE, selectorThreadFactory);
 +      workerGroup = new OioEventLoopGroup(Integer.MAX_VALUE, workerThreadFactory);
 +      socketClass = OioServerSocketChannel.class;
 +    } else {
 +      bossGroup = new NioEventLoopGroup(this.numSelectorThreads, selectorThreadFactory);
 +      workerGroup = new NioEventLoopGroup(this.numWorkerThreads, workerThreadFactory);
 +      socketClass = NioServerSocketChannel.class;
 +    }
 +    InternalDistributedSystem system = (InternalDistributedSystem) cache.getDistributedSystem();
 +    String pwd = system.getConfig().getRedisPassword();
 +    final byte[] pwdB = Coder.stringToBytes(pwd);
 +    ServerBootstrap b = new ServerBootstrap();
 +    b.group(bossGroup, workerGroup)
 +    .channel(socketClass)
 +    .childHandler(new ChannelInitializer<SocketChannel>() {
 +      @Override
 +      public void initChannel(SocketChannel ch) throws Exception {
 +        if (logger.fineEnabled())
 +          logger.fine("GemFireRedisServer-Connection established with " + ch.remoteAddress());
 +        ChannelPipeline p = ch.pipeline();
 +        p.addLast(ByteToCommandDecoder.class.getSimpleName(), new ByteToCommandDecoder());
 +        p.addLast(ExecutionHandlerContext.class.getSimpleName(), new ExecutionHandlerContext(ch, cache, regionCache, GemFireRedisServer.this, pwdB));
 +      }
 +    })
 +    .option(ChannelOption.SO_REUSEADDR, true)
 +    .option(ChannelOption.SO_RCVBUF, getBufferSize())
 +    .childOption(ChannelOption.SO_KEEPALIVE, true)
 +    .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, GemFireRedisServer.connectTimeoutMillis)
 +    .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
 +
 +    // Bind and start to accept incoming connections.
 +    ChannelFuture f = b.bind(new InetSocketAddress(getBindAddress(), serverPort)).sync();
 +    if (this.logger.infoEnabled()) {
 +      String logMessage = "GemFireRedisServer started {" + getBindAddress() + ":" + serverPort + "}, Selector threads: " + this.numSelectorThreads;
 +      if (this.singleThreadPerConnection)
 +        logMessage += ", One worker thread per connection";
 +      else
 +        logMessage += ", Worker threads: " + this.numWorkerThreads;
 +      this.logger.info(logMessage);
 +    }
 +    this.serverChannel = f.channel();
 +  }
 +
 +  /**
 +   * Takes an entry event and processes it. If the entry denotes that a
 +   * {@link RedisDataType#REDIS_LIST} or {@link RedisDataType#REDIS_SORTEDSET}
 +   * was created then this function will call the necessary calls to create the 
 +   * parameterized queries for those keys.
 +   * 
 +   * @param event EntryEvent from meta data region
 +   */
 +  private void afterKeyCreate(EntryEvent<String, RedisDataType> event) {
 +    if (event.isOriginRemote()) {
 +      final String key = (String) event.getKey();
 +      final RedisDataType value = event.getNewValue();
 +      if (value != RedisDataType.REDIS_STRING && value != RedisDataType.REDIS_HLL && value != RedisDataType.REDIS_PROTECTED) {
 +        try {
 +          this.regionCache.createRemoteRegionReferenceLocally(Coder.stringToByteArrayWrapper(key), value);
 +        } catch (RegionDestroyedException ignore) { // Region already destroyed, ignore
 +        }
 +      }
 +    }
 +  }
 +
 +  /**
 +   * When a key is removed then this function will make sure the associated
 +   * queries with the key are also removed from each vm to avoid unnecessary
 +   * data retention
 +   */
 +  private void afterKeyDestroy(EntryEvent<String, RedisDataType> event) {
 +    if (event.isOriginRemote()) {
 +      final String key = (String) event.getKey();
 +      final RedisDataType value = event.getOldValue();
 +      if (value != null && value != RedisDataType.REDIS_STRING && value != RedisDataType.REDIS_HLL && value != RedisDataType.REDIS_PROTECTED) {
 +        ByteArrayWrapper kW = Coder.stringToByteArrayWrapper(key);
 +        Region<?, ?> r = this.regionCache.getRegion(kW);
 +        if (r != null) { 
 +          this.regionCache.removeRegionReferenceLocally(kW, value);
 +        }
 +      }
 +    }
 +  }
 +
 +  private final class MetaCacheListener extends CacheListenerAdapter<String, RedisDataType> {
 +
 +    @Override
 +    public void afterCreate(EntryEvent<String, RedisDataType> event) {
 +      afterKeyCreate(event);
 +    }
 +
 +    @Override
 +    public void afterDestroy(EntryEvent<String, RedisDataType> event) {
 +      afterKeyDestroy(event);
 +    }
 +  }
 +
 +  /**
 +   * Helper method to get GemFire set socket buffer size,
 +   * possibly a default of 32k
 +   * 
 +   * @return Buffer size to use for server
 +   */
 +  private int getBufferSize() {
 +    InternalDistributedSystem system = (InternalDistributedSystem) cache.getDistributedSystem();
 +    return system.getConfig().getSocketBufferSize();
 +  }
 +
 +  /**
 +   * Shutdown method for {@link GemFireRedisServer}. This closes the {@link Cache},
 +   * interrupts all execution and forcefully closes all connections.
 +   */
 +  public synchronized void shutdown() {
 +    if (!shutdown) {
 +      if (logger.infoEnabled())
 +        logger.info("GemFireRedisServer shutting down");
 +      ChannelFuture closeFuture = this.serverChannel.closeFuture();
 +      Future<?> c = workerGroup.shutdownGracefully();
 +      Future<?> c2 = bossGroup.shutdownGracefully();
 +      this.serverChannel.close();
 +      c.syncUninterruptibly();
 +      c2.syncUninterruptibly();
 +      this.regionCache.close();
 +      if (mainThread != null)
 +        mainThread.interrupt();
 +      for (ScheduledFuture<?> f : this.expirationFutures.values())
 +        f.cancel(true);
 +      this.expirationFutures.clear();
 +      this.expirationExecutor.shutdownNow();
 +      closeFuture.syncUninterruptibly();
 +      shutdown = true;
 +    }
 +  }
 +
 +  /**
 +   * Static main method that allows the {@link GemFireRedisServer} to be 
 +   * started from the command line. The supported command line arguments are
 +   * <p>-port=
 +   * <br>-bind-address=
 +   * <br>-log-level=
 +   * 
 +   * @param args Command line args
 +   */
 +  public static void main(String[] args) {
 +    int port = DEFAULT_REDIS_SERVER_PORT;
 +    String bindAddress = null;
 +    String logLevel = null;
 +    for (String arg: args) {
 +      if (arg.startsWith("-port"))
 +        port = getPort(arg);
 +      else if (arg.startsWith("-bind-address"))
 +        bindAddress = getBindAddress(arg);
 +      else if (arg.startsWith("-log-level"))
 +        logLevel = getLogLevel(arg);
 +    }
 +    mainThread = Thread.currentThread();
 +    GemFireRedisServer server = new GemFireRedisServer(bindAddress, port, logLevel);
 +    server.start();
 +    while(true) {
 +      try {
 +        Thread.sleep(Long.MAX_VALUE);
 +      } catch (InterruptedException e1) { 
 +        break;
 +      } catch (Exception e) {
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Helper method to parse the port to a number
 +   * 
 +   * @param arg String where the argument is
 +   * @return The port number when the correct syntax was used, 
 +   * otherwise will return {@link #DEFAULT_REDIS_SERVER_PORT}
 +   */
 +  private static int getPort(String arg) {
 +    int port = DEFAULT_REDIS_SERVER_PORT;
 +    if (arg != null && arg.length() > 6) {
 +      if (arg.startsWith("-port")) {
 +        String p = arg.substring(arg.indexOf('=') + 1);
 +        p = p.trim();
 +        try {
 +          port = Integer.parseInt(p);
 +        } catch (NumberFormatException e) {
 +          System.out.println("Unable to parse port, using default port");
 +        }
 +      }
 +    }
 +    return port;
 +  }
 +
 +  /**
 +   * Helper method to parse bind address
 +   * 
 +   * @param arg String holding bind address
 +   * @return Bind address
 +   */
 +  private static String getBindAddress(String arg) {
 +    String address = null;
 +    if (arg != null && arg.length() > 14) {
 +      if (arg.startsWith("-bind-address")) {
 +        String p = arg.substring(arg.indexOf('=') + 1);
 +        address = p.trim();
 +      }
 +    }
 +    return address;
 +  }
 +
 +  /**
 +   * Helper method to parse log level
 +   * 
 +   * @param arg String holding log level
 +   * @return Log level
 +   */
 +  private static String getLogLevel(String arg) {
 +    String logLevel = null;
 +    if (arg != null && arg.length() > 11) {
 +      if (arg.startsWith("-log-level")) {
 +        String p = arg.substring(arg.indexOf('=') + 1);
 +        logLevel = p.trim();
 +      }
 +    }
 +    return logLevel;
 +  }
 +
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache/CacheRegionClearStatsDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache/CacheRegionClearStatsDUnitTest.java
index 7ea3565,0000000..88a32a3
mode 100755,000000..100755
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache/CacheRegionClearStatsDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache/CacheRegionClearStatsDUnitTest.java
@@@ -1,248 -1,0 +1,240 @@@
 +/*
 + * 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.cache;
 +
 +import java.util.Properties;
 +
 +import com.gemstone.gemfire.cache.client.PoolManager;
 +import com.gemstone.gemfire.cache.client.internal.PoolImpl;
 +import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.distributed.DistributedSystem;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 +import com.gemstone.gemfire.internal.AvailablePort;
 +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
 +import com.gemstone.gemfire.test.dunit.Assert;
 +import com.gemstone.gemfire.test.dunit.DistributedTestCase;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.NetworkUtils;
 +import com.gemstone.gemfire.test.dunit.VM;
 +/**
 + * verifies the count of clear operation
 + *  
 + * @author aingle
 + */
 +public class CacheRegionClearStatsDUnitTest extends DistributedTestCase {
 +  /** the cache */
 +  private static GemFireCacheImpl cache = null;
 +
-   private static VM server1 = null;
++  private VM server1 = null;
 +
 +  private static VM client1 = null;
 +
 +  /** name of the test region */
 +  private static final String REGION_NAME = "CacheRegionClearStatsDUnitTest_Region";
 +
 +  private static final String k1 = "k1";
 +
 +  private static final String k2 = "k2";
 +
 +  private static final String client_k1 = "client-k1";
 +
 +  private static final String client_k2 = "client-k2";
 +  
 +  private static final int clearOp = 2;
 +  
 +  /** constructor */
 +  public CacheRegionClearStatsDUnitTest(String name) {
 +    super(name);
 +  }
 +
 +  public void setUp() throws Exception {
 +    super.setUp();
 +    final Host host = Host.getHost(0);
 +    server1 = host.getVM(0);
 +    client1 = host.getVM(1);
 +  }
 +
 +  private void createCache(Properties props) throws Exception {
 +    DistributedSystem ds = getSystem(props);
 +    ds.disconnect();
 +    ds = getSystem(props);
 +    assertNotNull(ds);
 +    cache = (GemFireCacheImpl)CacheFactory.create(ds);
 +    assertNotNull(cache);
 +  }
 +
 +  public static void createClientCache(String host, Integer port1)
 +      throws Exception {
 +    new CacheRegionClearStatsDUnitTest("temp");
 +    Properties props = new Properties();
 +    props.setProperty(DistributionConfig.MCAST_PORT_NAME, "0");
 +    props.setProperty(DistributionConfig.LOCATORS_NAME, "");
 +    new CacheRegionClearStatsDUnitTest("temp").createCache(props);
 +    PoolImpl p = (PoolImpl)PoolManager.createFactory().addServer(host,
 +        port1.intValue()).setSubscriptionEnabled(false)
 +        .setThreadLocalConnections(true).setMinConnections(1).setReadTimeout(
 +            20000).setPingInterval(10000).setRetryAttempts(1)
 +        .create("CacheRegionClearStatsDUnitTest");
 +
 +    AttributesFactory factory = new AttributesFactory();
 +    factory.setScope(Scope.DISTRIBUTED_ACK);
 +    factory.setPoolName(p.getName());
 +
 +    RegionAttributes attrs = factory.create();
 +    Region region = cache.createRegion(REGION_NAME, attrs);
 +    //region.registerInterest("ALL_KEYS");
 +  }
 +
 +  public static Integer createServerCacheDisk() throws Exception {
 +    return createCache(DataPolicy.PERSISTENT_REPLICATE);
 +  }
 +
 +  private static Integer createCache(DataPolicy dataPolicy) throws Exception {
 +    new CacheRegionClearStatsDUnitTest("temp").createCache(new Properties());
 +    AttributesFactory factory = new AttributesFactory();
 +    factory.setScope(Scope.DISTRIBUTED_ACK);
 +    factory.setDataPolicy(dataPolicy);
 +    RegionAttributes attrs = factory.create();
 +    cache.createRegion(REGION_NAME, attrs);
 +    int port = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    CacheServer server1 = cache.addCacheServer();
 +    server1.setPort(port);
 +    server1.setNotifyBySubscription(true);
 +    server1.start();
 +    return new Integer(server1.getPort());
 +  }
 +
 +  public static Integer createServerCache() throws Exception {
 +    return createCache(DataPolicy.REPLICATE);
 +  }
 +
 +  public static void createClientCacheDisk(String host, Integer port1)
 +      throws Exception {
 +    new CacheRegionClearStatsDUnitTest("temp");
 +    Properties props = new Properties();
 +    props.setProperty(DistributionConfig.MCAST_PORT_NAME, "0");
 +    props.setProperty(DistributionConfig.LOCATORS_NAME, "");
 +    new CacheRegionClearStatsDUnitTest("temp").createCache(props);
 +    PoolImpl p = (PoolImpl)PoolManager.createFactory().addServer(host,
 +        port1.intValue()).setSubscriptionEnabled(false)
 +        .setThreadLocalConnections(true).setMinConnections(1).setReadTimeout(
 +            20000).setPingInterval(10000).setRetryAttempts(1).create(
 +            "CacheRegionClearStatsDUnitTest");
 +
 +    AttributesFactory factory = new AttributesFactory();
 +    factory.setScope(Scope.DISTRIBUTED_ACK);
 +    factory.setPoolName(p.getName());
 +    factory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
 +    RegionAttributes attrs = factory.create();
 +    Region region = cache.createRegion(REGION_NAME, attrs);
 +    //region.registerInterest("ALL_KEYS");
 +  }
 +  /**
 +   * This test does the following (<b> clear stats counter </b>):<br>
 +   * 1)Verifies that clear operation count matches with stats count<br>
 +   */
 +  public void testClearStatsWithNormalRegion(){
-     Integer port1 = ((Integer)server1.invoke(
-         CacheRegionClearStatsDUnitTest.class, "createServerCache"));
++    Integer port1 = ((Integer)server1.invoke(() -> CacheRegionClearStatsDUnitTest.createServerCache()));
 +
-     client1.invoke(CacheRegionClearStatsDUnitTest.class,
-         "createClientCache", new Object[] {
-             NetworkUtils.getServerHostName(server1.getHost()), port1 });
-     client1.invoke(CacheRegionClearStatsDUnitTest.class, "put");
++    client1.invoke(() -> CacheRegionClearStatsDUnitTest.createClientCache(
++            NetworkUtils.getServerHostName(server1.getHost()), port1 ));
++    client1.invoke(() -> CacheRegionClearStatsDUnitTest.put());
 +    
 +    try{
 +      Thread.sleep(10000);
 +    }catch(Exception e){
 +      // sleep 
 +    }
 +    
-     client1.invoke(CacheRegionClearStatsDUnitTest.class,
-         "validationClearStat");
++    client1.invoke(() -> CacheRegionClearStatsDUnitTest.validationClearStat());
 +    
-     server1.invoke(CacheRegionClearStatsDUnitTest.class,
-     "validationClearStat");
++    server1.invoke(() -> CacheRegionClearStatsDUnitTest.validationClearStat());
 +  }
 +  /**
 +   * This test does the following (<b> clear stats counter when disk involved </b>):<br>
 +   * 1)Verifies that clear operation count matches with stats count <br>
 +   */
 +  public void testClearStatsWithDiskRegion(){
-     Integer port1 = ((Integer)server1.invoke(
-         CacheRegionClearStatsDUnitTest.class, "createServerCacheDisk"));
++    Integer port1 = ((Integer)server1.invoke(() -> CacheRegionClearStatsDUnitTest.createServerCacheDisk()));
 +
-     client1.invoke(CacheRegionClearStatsDUnitTest.class,
-         "createClientCacheDisk", new Object[] {
-             NetworkUtils.getServerHostName(server1.getHost()), port1 });
-     client1.invoke(CacheRegionClearStatsDUnitTest.class, "put");
++    client1.invoke(() -> CacheRegionClearStatsDUnitTest.createClientCacheDisk(
++            NetworkUtils.getServerHostName(server1.getHost()), port1 ));
++    client1.invoke(() -> CacheRegionClearStatsDUnitTest.put());
 +    
 +    try{
 +      Thread.sleep(10000);
 +    }catch(Exception e){
 +      // sleep 
 +    }
 +    
-     client1.invoke(CacheRegionClearStatsDUnitTest.class,
-         "validationClearStat");
++    client1.invoke(() -> CacheRegionClearStatsDUnitTest.validationClearStat());
 +    
-     server1.invoke(CacheRegionClearStatsDUnitTest.class,
-     "validationClearStat");
++    server1.invoke(() -> CacheRegionClearStatsDUnitTest.validationClearStat());
 +  }
 +  
 +  @Override
 +  protected final void preTearDown() throws Exception {
-     client1.invoke(CacheRegionClearStatsDUnitTest.class, "closeCache");
++    client1.invoke(() -> CacheRegionClearStatsDUnitTest.closeCache());
 +    // then close the servers
-     server1.invoke(CacheRegionClearStatsDUnitTest.class, "closeCache");
++    server1.invoke(() -> CacheRegionClearStatsDUnitTest.closeCache());
 +  }
 +
 +  public static void closeCache() {
 +    if (cache != null && !cache.isClosed()) {
 +      cache.close();
 +      cache.getDistributedSystem().disconnect();
 +    }
 +  }
 +  
 +  public static void put() {
 +    try {
 +      Region r1 = cache.getRegion(Region.SEPARATOR + REGION_NAME);
 +      assertNotNull(r1);
 +
 +      r1.put(k1, client_k1);
 +      assertEquals(r1.getEntry(k1).getValue(), client_k1);
 +      r1.put(k2, client_k2);
 +      assertEquals(r1.getEntry(k2).getValue(), client_k2);
 +      try{
 +        Thread.sleep(10000);
 +      }catch(Exception e){
 +        // sleep 
 +      }
 +      r1.clear();
 +      
 +      r1.put(k1, client_k1);
 +      assertEquals(r1.getEntry(k1).getValue(), client_k1);
 +      r1.put(k2, client_k2);
 +      assertEquals(r1.getEntry(k2).getValue(), client_k2);
 +      try{
 +        Thread.sleep(10000);
 +      }catch(Exception e){
 +        // sleep 
 +      }
 +      r1.clear();
 +    }
 +    catch (Exception ex) {
 +      Assert.fail("failed while put", ex);
 +    }
 +  }
 +  
 +  public static void validationClearStat(){
 +    assertEquals(cache.getCachePerfStats().getClearCount(), clearOp);
 +  }
 +  
 +  
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache/ClientServerTimeSyncDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache/ClientServerTimeSyncDUnitTest.java
index 8166318,0000000..8b04ab5
mode 100755,000000..100755
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache/ClientServerTimeSyncDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache/ClientServerTimeSyncDUnitTest.java
@@@ -1,203 -1,0 +1,203 @@@
 +/*
 + * 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.cache;
 +
 +import java.io.IOException;
 +import java.util.Properties;
 +
 +import org.junit.Ignore;
 +
 +import com.gemstone.gemfire.cache.client.ClientCache;
 +import com.gemstone.gemfire.cache.client.ClientCacheFactory;
 +import com.gemstone.gemfire.cache.client.ClientRegionShortcut;
 +import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.cache30.CacheTestCase;
 +import com.gemstone.gemfire.distributed.internal.DSClock;
 +import com.gemstone.gemfire.internal.AvailablePortHelper;
 +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
 +import com.gemstone.gemfire.test.dunit.Assert;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.LogWriterUtils;
 +import com.gemstone.gemfire.test.dunit.NetworkUtils;
 +import com.gemstone.gemfire.test.dunit.SerializableCallable;
 +import com.gemstone.gemfire.test.dunit.VM;
 +import com.gemstone.gemfire.test.dunit.Wait;
 +import com.gemstone.gemfire.test.dunit.WaitCriterion;
 +
 +public class ClientServerTimeSyncDUnitTest extends CacheTestCase {
 +
 +  public ClientServerTimeSyncDUnitTest(String name) {
 +    super(name);
 +  }
 +
 +  @Ignore("Bug 52327")
 +  public void DISABLED_testClientTimeAdvances() {
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0); // Server
 +    VM vm1 = host.getVM(1); // Client
 +
 +    final String regionName = "testRegion";
 +    final long TEST_OFFSET = 10000;
 +
 +    ClientCache cache = null;
 +    
 +    try {
 +
 +      final int serverPort = (Integer)vm0.invoke(new SerializableCallable("Start server with a region") {
 +        
 +        @Override
 +        public Object call() {
 +          Cache cache = getCache();
 +          cache.createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
 +          LogWriterUtils.getLogWriter().info("Done creating region, now creating CacheServer");
 +          CacheServer server = null;
 +          try {
 +            server = cache.addCacheServer();
 +            server.setPort(AvailablePortHelper.getRandomAvailableTCPPort());
 +            server.start();
 +          } catch (IOException e) {
 +            Assert.fail("Starting cache server failed.", e);
 +          }
 +  
 +          // now set an artificial time offset for the test
 +          system.getClock().setCacheTimeOffset(null, TEST_OFFSET, true);
 +          
 +          LogWriterUtils.getLogWriter().info("Done creating and starting CacheServer on port " + server.getPort());
 +          return server.getPort();
 +        }
 +      });
 +      
 +      final String hostName = NetworkUtils.getServerHostName(vm0.getHost());
 +  
 +      // Start client with proxy region and register interest
 +        
 +      disconnectFromDS();
 +      Properties props = new Properties();
 +      props.setProperty("locators", "");
 +      props = getSystem(props).getProperties();
 +      cache = new ClientCacheFactory(props).setPoolSubscriptionEnabled(true)
 +          .addPoolServer(hostName, serverPort)
 +          .setPoolPingInterval(5000)
 +          .create();
 +      Region proxyRegion = cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regionName);
 +  
 +      proxyRegion.registerInterestRegex(".*");
 +  
 +      proxyRegion.put("testkey", "testValue1");
 +          
 +      final DSClock clock = ((GemFireCacheImpl)cache).getSystem().getClock();
 +      WaitCriterion wc = new WaitCriterion() {
 +        public boolean done() {
 +          long clientTimeOffset = clock.getCacheTimeOffset();
 +          LogWriterUtils.getLogWriter().info("Client node's new time offset is: " + clientTimeOffset);
 +          return clientTimeOffset >= TEST_OFFSET;
 +        }
 +        public String description() {
 +          return "Waiting for cacheTimeOffset to be non-zero.  PingOp should have set it to something";
 +        }
 +      };
 +      Wait.waitForCriterion(wc, 60000, 1000, true);
 +    } finally {
 +      cache.close();
-       vm1.invoke(CacheTestCase.class, "disconnectFromDS");
++      vm1.invoke(() -> CacheTestCase.disconnectFromDS());
 +    }
 +  }
 +  
 +  public void testNothing() {
 +    // place-holder to keep dunit runner from barfing
 +  }
 +
 +  @Ignore("not yet implemented")
 +  public void DISABLED_testClientTimeSlowsDown() {
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0); // Server
 +    VM vm1 = host.getVM(1); // Client
 +
 +    final String regionName = "testRegion";
 +    final long TEST_OFFSET = 10000;
 +
 +    ClientCache cache = null;
 +    
 +    try {
 +
 +      final int serverPort = (Integer)vm0.invoke(new SerializableCallable("Start server with a region") {
 +        
 +        @Override
 +        public Object call() {
 +          Cache cache = getCache();
 +          cache.createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
 +          LogWriterUtils.getLogWriter().info("Done creating region, now creating CacheServer");
 +          CacheServer server = null;
 +          try {
 +            server = cache.addCacheServer();
 +            server.setPort(AvailablePortHelper.getRandomAvailableTCPPort());
 +            server.start();
 +          } catch (IOException e) {
 +            Assert.fail("Starting cache server failed.", e);
 +          }
 +  
 +          // now set an artificial time offset for the test
 +          system.getClock().setCacheTimeOffset(null, -TEST_OFFSET, true);
 +          
 +          LogWriterUtils.getLogWriter().info("Done creating and starting CacheServer on port " + server.getPort());
 +          return server.getPort();
 +        }
 +      });
 +      
 +      Wait.pause((int)TEST_OFFSET);  // let cacheTimeMillis consume the time offset
 +      
 +      final String hostName = NetworkUtils.getServerHostName(vm0.getHost());
 +  
 +      // Start client with proxy region and register interest
 +        
 +      disconnectFromDS();
 +      Properties props = new Properties();
 +      props.setProperty("locators", "");
 +      props = getSystem(props).getProperties();
 +      cache = new ClientCacheFactory(props).setPoolSubscriptionEnabled(true)
 +          .addPoolServer(hostName, serverPort)
 +          .setPoolPingInterval(5000)
 +          .create();
 +      Region proxyRegion = cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regionName);
 +  
 +      proxyRegion.registerInterestRegex(".*");
 +  
 +      proxyRegion.put("testkey", "testValue1");
 +          
 +      final DSClock clock = ((GemFireCacheImpl)cache).getSystem().getClock();
 +      WaitCriterion wc = new WaitCriterion() {
 +        public boolean done() {
 +          long clientTimeOffset = clock.getCacheTimeOffset();
 +          LogWriterUtils.getLogWriter().info("Client node's new time offset is: " + clientTimeOffset);
 +          if (clientTimeOffset >= 0) {
 +            return false;
 +          }
 +          long cacheTime = clock.cacheTimeMillis();
 +          return Math.abs(System.currentTimeMillis() - (cacheTime - clientTimeOffset)) < 5;
 +        }
 +        public String description() {
 +          return "Waiting for cacheTimeOffset to be negative and cacheTimeMillis to stabilize";
 +        }
 +      };
 +      Wait.waitForCriterion(wc, 60000, 1000, true);
 +    } finally {
 +      cache.close();
-       vm1.invoke(CacheTestCase.class, "disconnectFromDS");
++      vm1.invoke(() -> CacheTestCase.disconnectFromDS());
 +    }
 +  }
 +
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/AutoConnectionSourceDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/AutoConnectionSourceDUnitTest.java
index b0f3b59,0000000..03368de
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/AutoConnectionSourceDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/AutoConnectionSourceDUnitTest.java
@@@ -1,598 -1,0 +1,598 @@@
 +/*
 + * 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.cache.client.internal;
 +
 +import java.io.Serializable;
 +import java.net.InetSocketAddress;
 +import java.util.HashSet;
 +import java.util.Iterator;
 +import java.util.List;
 +
 +import junit.framework.Assert;
 +
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.client.NoAvailableLocatorsException;
 +import com.gemstone.gemfire.cache.client.NoAvailableServersException;
 +import com.gemstone.gemfire.cache.client.PoolManager;
 +import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.distributed.internal.ServerLocation;
 +import com.gemstone.gemfire.internal.AvailablePort;
 +import com.gemstone.gemfire.internal.AvailablePortHelper;
 +import com.gemstone.gemfire.management.membership.ClientMembership;
 +import com.gemstone.gemfire.management.membership.ClientMembershipEvent;
 +import com.gemstone.gemfire.management.membership.ClientMembershipListenerAdapter;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.IgnoredException;
 +import com.gemstone.gemfire.test.dunit.NetworkUtils;
 +import com.gemstone.gemfire.test.dunit.SerializableCallable;
 +import com.gemstone.gemfire.test.dunit.SerializableRunnable;
 +import com.gemstone.gemfire.test.dunit.VM;
 +import com.gemstone.gemfire.test.dunit.Wait;
 +
 +/**
 + * Tests cases that are particular for the auto connection source
 + * - dynamically discovering servers, locators, handling 
 + * locator disappearance, etc.
 + * @author dsmith
 + *
 + */
 +public class AutoConnectionSourceDUnitTest extends LocatorTestBase {
 +  
 +  protected static final Object BRIDGE_LISTENER = "BRIDGE_LISTENER";
 +  private static final long MAX_WAIT = 60000;
 +  
 +  public void setUp() throws Exception {
 +    super.setUp();
 +    IgnoredException.addIgnoredException("NoAvailableLocatorsException");
 +  }
 +
 +  public AutoConnectionSourceDUnitTest(String name) {
 +    super(name);
 +  }
 +  
 +  public void testDiscoverBridgeServers() throws Exception {
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    
 +    int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    startLocatorInVM(vm0, locatorPort, "");
 +    
 +    String locators = NetworkUtils.getServerHostName(vm0.getHost())+ "[" + locatorPort + "]";
 +    
 +    startBridgeServerInVM(vm1, null, locators);
 +
 +    startBridgeClientInVM(vm2, null, NetworkUtils.getServerHostName(vm0.getHost()), locatorPort);
 +
 +    putAndWaitForSuccess(vm2, REGION_NAME, "key", "value");
 +    
 +    Assert.assertEquals("value", getInVM(vm1, "key"));
 +  }
 +
 +  public void testNoLocators() {
 +    
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    
 +    try {
 +      startBridgeClientInVM(vm0, null, NetworkUtils.getServerHostName(vm0.getHost()), AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET));
 +      putInVM(vm0, "key", "value");
 +      fail("Client cache should not have been able to start");
 +    } catch(Exception e) {
 +      //expected an exception
 +    }
 +  }
 +  
 +  public void testNoBridgeServer() {
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    
 +    int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    startLocatorInVM(vm0, locatorPort, "");
 +    try { 
 +      startBridgeClientInVM(vm1, null, NetworkUtils.getServerHostName(vm0.getHost()), locatorPort);
 +      putInVM(vm0, "key", "value");
 +      fail("Client cache should not have been able to start");
 +    } catch(Exception e) {
 +      //expected an exception
 +    }
 +  }
 +  
 +  public void testDynamicallyFindBridgeServer() throws Exception  {
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    VM vm3 = host.getVM(3);
 +    
 +    int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    startLocatorInVM(vm0, locatorPort, "");
 +    
 +    String locators = NetworkUtils.getServerHostName(vm0.getHost()) + "[" + locatorPort + "]";
 +    
 +    startBridgeServerInVM(vm1, null, locators);
 +    
 +    startBridgeClientInVM(vm2, null, NetworkUtils.getServerHostName(vm0.getHost()), locatorPort);
 +    
 +    putAndWaitForSuccess(vm2, REGION_NAME, "key", "value");
 +    
 +    startBridgeServerInVM(vm3, null, locators);
 +    
 +    stopBridgeMemberVM(vm1);
 +    
 +    putAndWaitForSuccess(vm2, REGION_NAME, "key2", "value2");
 +    
 +    Assert.assertEquals("value2", getInVM(vm3, "key2"));
 +  }
 +  
 +  public void testDynamicallyFindLocators() throws Exception {
 +    final Host host = Host.getHost(0);
 +    final String hostName = NetworkUtils.getServerHostName(host);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    VM vm3 = host.getVM(3);
 +    
 +    int[] ports = AvailablePortHelper.getRandomAvailableTCPPorts(3);
 +    
 +    final int locatorPort0 = ports[0];
 +    final int locatorPort1 = ports[1];
 +    final int locatorPort3 = ports[2];
 +    String locators = getLocatorString(host, new int[] { locatorPort0, locatorPort1, locatorPort3});
 +    startLocatorInVM(vm0, locatorPort0, locators);
 +    
 +    startLocatorInVM(vm1, locatorPort1, locators);
 +    startBridgeClientInVM(vm2, null, NetworkUtils.getServerHostName(vm0.getHost()), locatorPort0);
 +    
 +    InetSocketAddress locatorToWaitFor= new InetSocketAddress(hostName, locatorPort1);
 +    waitForLocatorDiscovery(vm2, locatorToWaitFor);
 +    
 +    stopLocatorInVM(vm0);
 +    startBridgeServerInVM(vm0, null, locators);
 +    
 +    putAndWaitForSuccess(vm2, REGION_NAME, "key", "value");
 +    Assert.assertEquals("value", getInVM(vm0, "key"));
 +    
 +    startLocatorInVM(vm3, locatorPort3, locators);
 +    stopBridgeMemberVM(vm0);
 +    locatorToWaitFor= new InetSocketAddress(hostName, locatorPort3);
 +    waitForLocatorDiscovery(vm2, locatorToWaitFor);
 +    stopLocatorInVM(vm1);
 +    startBridgeServerInVM(vm1, null, locators);
 +    putAndWaitForSuccess(vm2, REGION_NAME, "key2", "value2");
 +    Assert.assertEquals("value2", getInVM(vm1, "key2"));
 +    
 +  }
 +  
 +  public void testEmbeddedLocator() throws Exception  {
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    VM vm3 = host.getVM(3);
 +    
 +    int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    
 +    String locators = NetworkUtils.getServerHostName(vm0.getHost()) + "[" + locatorPort + "]";
 +    
 +    startBridgeServerWithEmbeddedLocator(vm0, null, locators, new String[] {REGION_NAME}, CacheServer.DEFAULT_LOAD_PROBE);
 +    
 +    startBridgeClientInVM(vm2, null, NetworkUtils.getServerHostName(vm0.getHost()), locatorPort);
 +    
 +    putAndWaitForSuccess(vm2, REGION_NAME, "key", "value");
 +    
 +    Assert.assertEquals("value", getInVM(vm2, "key"));
 +  }
 +
 +  private void waitForLocatorDiscovery(VM vm,
 +      final InetSocketAddress locatorToWaitFor) {
 +    vm.invoke(new SerializableCallable() {
 +      public Object call() throws InterruptedException {
 +        MyLocatorCallback callback = (MyLocatorCallback) remoteObjects.get(CALLBACK_KEY);
 +        
 +        boolean discovered = callback.waitForDiscovery(locatorToWaitFor, MAX_WAIT);
 +        Assert.assertTrue("Waited " + MAX_WAIT + " for " + locatorToWaitFor
 +            + " to be discovered on client. List is now: "
 +            + callback.getDiscovered(), discovered);
 +        return null;
 +      }
 +    });
 +  }
 +  
 +  public void testServerGroups() throws Exception {
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    VM vm3 = host.getVM(3);
 +    
 +    int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    startLocatorInVM(vm0, locatorPort, "");
 +    
 +    String locators = NetworkUtils.getServerHostName(vm0.getHost()) + "[" + locatorPort + "]";
 +    
 +    startBridgeServerInVM(vm1, new String[] {"group1", "group2"} , locators, new String[] {"A", "B"});
 +    startBridgeServerInVM(vm2, new String[] {"group2", "group3"}, locators, new String[] {"B", "C"});
 +
 +    
 +    startBridgeClientInVM(vm3, "group1", NetworkUtils.getServerHostName(vm0.getHost()), locatorPort, new String [] {"A", "B", "C"});
 +    putAndWaitForSuccess(vm3, "A", "key", "value");
 +    Assert.assertEquals("value", getInVM(vm1, "A", "key"));
 +    try {
 +      putInVM(vm3, "C", "key2", "value2");
 +      fail("Should not have been able to find Region C on the server");
 +    } catch(Exception expected) {}
 +    
 +    stopBridgeMemberVM(vm3);
 +    
 +    startBridgeClientInVM(vm3, "group3", NetworkUtils.getServerHostName(vm0.getHost()), locatorPort, new String [] {"A", "B", "C"});
 +    try {
 +      putInVM(vm3, "A", "key3", "value");
 +      fail("Should not have been able to find Region A on the server");
 +    } catch(Exception expected) {}
 +    putInVM(vm3, "C", "key4", "value");
 +    Assert.assertEquals("value", getInVM(vm2, "C", "key4"));
 +    
 +    stopBridgeMemberVM(vm3);
 +    
 +    startBridgeClientInVM(vm3, "group2", NetworkUtils.getServerHostName(vm0.getHost()), locatorPort, new String [] {"A", "B", "C"});
 +    putInVM(vm3, "B", "key5", "value");
 +    Assert.assertEquals("value", getInVM(vm1, "B", "key5"));
 +    Assert.assertEquals("value", getInVM(vm2, "B", "key5"));
 +    
 +    stopBridgeMemberVM(vm1);
 +    putInVM(vm3, "B", "key6", "value");
 +    Assert.assertEquals("value", getInVM(vm2, "B", "key6"));
 +    startBridgeServerInVM(vm1, new String[] {"group1", "group2"} , locators, new String[] {"A", "B"});
 +    stopBridgeMemberVM(vm2);
 +    
 +    putInVM(vm3, "B", "key7", "value");
 +    Assert.assertEquals("value", getInVM(vm1, "B", "key7"));
 +  }
 +  
-   public void testTwoServersInSameVM() {
++  public void testTwoServersInSameVM() throws Exception {
 +    final Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +//    VM vm3 = host.getVM(3);
 +    
 +    int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    
 +    startLocatorInVM(vm0, locatorPort, "");
 +    
 +    final String locators = NetworkUtils.getServerHostName(vm0.getHost()) + "[" + locatorPort + "]";
 +    
 +    final int serverPort1 =startBridgeServerInVM(vm1, new String[] {"group1"}, locators);
 +    final int serverPort2 =addCacheServerInVM(vm1, new String[] {"group2"});
 +    
 +    startBridgeClientInVM(vm2, "group2", NetworkUtils.getServerHostName(vm0.getHost()), locatorPort);
 +    
 +    checkEndpoints(vm2, new int[] {serverPort2});
 +    
 +    stopBridgeMemberVM(vm2);
 +
 +    startBridgeClientInVM(vm2, "group1", NetworkUtils.getServerHostName(vm0.getHost()), locatorPort);
 +    
 +    checkEndpoints(vm2, new int[] {serverPort1});
 +  }
 +  
 +  public void testClientMembershipListener() throws Exception {
 +    final Host host = Host.getHost(0);
 +    VM locatorVM = host.getVM(0);
 +    VM bridge1VM = host.getVM(1);
 +    VM bridge2VM = host.getVM(2);
 +    VM clientVM = host.getVM(3);
 +    int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
 +    startLocatorInVM(locatorVM, locatorPort, "");
 +    String locators = NetworkUtils.getServerHostName(locatorVM.getHost()) + "[" + locatorPort + "]";
 +
 +    //start a bridge server with a listener
 +    addBridgeListener(bridge1VM);
 +    int serverPort1 = startBridgeServerInVM(bridge1VM, null, locators);
 +
 +    //start a bridge client with a listener
 +    addBridgeListener(clientVM);
 +    startBridgeClientInVM(clientVM, null, NetworkUtils.getServerHostName(locatorVM.getHost()), locatorPort);
 +    // wait for client to connect
 +    checkEndpoints(clientVM, new int[] {serverPort1});
 +    
 +    //make sure the client and bridge server both noticed each other
 +    waitForJoin(bridge1VM);
 +    MyListener serverListener = getBridgeListener(bridge1VM);
 +    Assert.assertEquals(0, serverListener.getCrashes());
 +    Assert.assertEquals(0, serverListener.getDepartures());
 +    Assert.assertEquals(1, serverListener.getJoins());
 +    resetBridgeListener(bridge1VM);
 +    
 +    waitForJoin(clientVM);
 +    MyListener clientListener= getBridgeListener(clientVM);
 +    Assert.assertEquals(0, clientListener.getCrashes());
 +    Assert.assertEquals(0, clientListener.getDepartures());
 +    Assert.assertEquals(1, clientListener.getJoins());
 +    resetBridgeListener(clientVM);
 +    
 +    checkEndpoints(clientVM, new int[] {serverPort1});
 +
 +    //start another bridge server and make sure it is detected by the client
 +    int serverPort2 = startBridgeServerInVM(bridge2VM, null, locators);
 +    
 +    checkEndpoints(clientVM, new int[] {serverPort1, serverPort2});
 +    serverListener = getBridgeListener(bridge1VM);
 +    Assert.assertEquals(0, serverListener.getCrashes());
 +    Assert.assertEquals(0, serverListener.getDepartures());
 +    Assert.assertEquals(0, serverListener.getJoins());
 +    resetBridgeListener(bridge1VM);
 +    waitForJoin(clientVM);
 +    clientListener= getBridgeListener(clientVM);
 +    Assert.assertEquals(0, clientListener.getCrashes());
 +    Assert.assertEquals(0, clientListener.getDepartures());
 +    Assert.assertEquals(1, clientListener.getJoins());
 +    resetBridgeListener(clientVM);
 +    
 +    //stop the second bridge server and make sure it is detected by the client
 +    stopBridgeMemberVM(bridge2VM);
 +    
 +    checkEndpoints(clientVM, new int[] {serverPort1});
 +    serverListener = getBridgeListener(bridge1VM);
 +    Assert.assertEquals(0, serverListener.getCrashes());
 +    Assert.assertEquals(0, serverListener.getDepartures());
 +    Assert.assertEquals(0, serverListener.getJoins());
 +    resetBridgeListener(bridge1VM);
 +    waitForCrash(clientVM);
 +    clientListener= getBridgeListener(clientVM);
 +    Assert.assertEquals(1, clientListener.getCrashes());
 +    Assert.assertEquals(0, clientListener.getDepartures());
 +    Assert.assertEquals(0, clientListener.getJoins());
 +    resetBridgeListener(clientVM);
 +    
 +    //stop the client and make sure the bridge server notices
 +    stopBridgeMemberVM(clientVM);
 +    waitForDeparture(bridge1VM);
 +    serverListener = getBridgeListener(bridge1VM);
 +    Assert.assertEquals(0, serverListener.getCrashes());
 +    Assert.assertEquals(1, serverListener.getDepartures());
 +    Assert.assertEquals(0, serverListener.getJoins());
 +  }
 +
 +  protected Object getInVM(VM vm, final Serializable key) {
 +    return getInVM(vm, REGION_NAME, key);
 +  }
 +  
 +  protected Object getInVM(VM vm, final String regionName, final Serializable key) {
 +    return vm.invoke(new SerializableCallable("Get in VM") {
 +      public Object call() throws Exception {
 +        Cache cache = (Cache) remoteObjects.get(CACHE_KEY);
 +        Region region = cache.getRegion(regionName);
 +        return region.get(key);
 +      }
 +    });
 +  }
 +  
 +  protected void putAndWaitForSuccess(VM vm,  final String regionName, final Serializable key, final Serializable value) throws InterruptedException
 +  {
 +    long endTime = System.currentTimeMillis() + MAX_WAIT;
 +    long remaining = MAX_WAIT;
 +    int i = 0;
 +    while(true) {
 +      try {
 +        System.err.println("Attempt: " + (i++));
 +        putInVM(vm, regionName, key, value);
 +        break;
 +      } catch (NoAvailableLocatorsException | com.gemstone.gemfire.test.dunit.RMIException e) {
 +        if( !(e instanceof NoAvailableLocatorsException)
 +            && !(e.getCause() instanceof NoAvailableServersException)) {
 +          throw e;
 +        }
 +        if(remaining <= 0) {
 +          throw e;
 +        }
 +        Wait.pause(100);
 +        remaining = endTime - System.currentTimeMillis();
 +      }
 +    }
 +  }
 +
 +  protected void putInVM(VM vm, final Serializable key, final Serializable value) {
 +    putInVM(vm, REGION_NAME, key, value);
 +  }
 +  
 +  
 +  
 +  protected void putInVM(VM vm,  final String regionName, final Serializable key, final Serializable value) {
 +    vm.invoke(new SerializableCallable("Put in VM") {
 +      public Object call() throws Exception {
 +        Cache cache = (Cache) remoteObjects.get(CACHE_KEY);
 +        Region region = cache.getRegion(regionName);
 +        return region.put(key, value);
 +      }
 +    });
 +  }
 +  
 +  /**
 +   * Assert that there is one endpoint with the given host in port
 +   * on the client vm.
 +   * @param vm - the vm the client is running in
 +   * @param expectedPorts - The server ports we expect the client to be connected to.
 +   */
 +  protected void checkEndpoints(VM vm, final int[] expectedPorts) {
 +    vm.invoke(new SerializableRunnable("Check endpoint") {
 +      public void run() {
 +        PoolImpl pool = (PoolImpl) PoolManager.find(POOL_NAME);
 +        int retryCount = 50;
 +        List/*<ServerLocation>*/ endpoints;
 +        HashSet actualEndpointPorts;
 +        HashSet expectedEndpointPorts = new HashSet();
 +        for(int i = 0; i < expectedPorts.length; i++) {
 +          expectedEndpointPorts.add(new Integer(expectedPorts[i]));
 +        }
 +        do {
 +          endpoints = pool.getCurrentServers();
 +          actualEndpointPorts = new HashSet();
 +          for(Iterator itr = endpoints.iterator(); itr.hasNext();) {
 +            ServerLocation sl = (ServerLocation)itr.next();
 +            actualEndpointPorts.add(new Integer(sl.getPort()));
 +          }
 +          if (expectedEndpointPorts.size() == actualEndpointPorts.size()) {
 +            break;
 +          } else {
 +            Wait.pause(100);
 +          }
 +        } while(retryCount-- > 0);
 +        Assert.assertEquals(expectedEndpointPorts, actualEndpointPorts);
 +      }
 +    });
 +  }
 +  
 +  protected void addBridgeListener(VM vm) {
 +    vm.invoke(new SerializableRunnable("Add membership listener") {
 +      public void run() {
 +        MyListener listener = new MyListener();
 +        ClientMembership.registerClientMembershipListener(listener);
 +        remoteObjects.put(BRIDGE_LISTENER, listener);
 +      }
 +    });
 +  }
 +  
 +  protected void resetBridgeListener(VM vm) {
 +    vm.invoke(new SerializableRunnable("Add membership listener") {
 +      public void run() {
 +        MyListener listener = (MyListener) remoteObjects.get(BRIDGE_LISTENER);
 +        listener.reset();
 +      }
 +    });
 +  }
 +  
 +  private MyListener getBridgeListener(VM vm) {
 +    return (MyListener) vm.invoke(new SerializableCallable("Add membership listener") {
 +      public Object call() {
 +        return remoteObjects.get(BRIDGE_LISTENER);
 +      }
 +    });
 +  }
 +  
 +  private void waitForJoin(VM vm) {
 +    vm.invoke(new SerializableRunnable() {
 +      public void run() {
 +        MyListener listener = (MyListener) remoteObjects.get(BRIDGE_LISTENER);
 +        synchronized(listener) {
 +          long end = System.currentTimeMillis() + 10000;
 +          while (listener.joins == 0) {
 +            try {
 +              long remaining = end - System.currentTimeMillis();
 +              if(remaining < 0) {
 +                break;
 +              }
 +              listener.wait(remaining);
 +            } catch (InterruptedException e) {
 +              fail("interrupted");
 +            }
 +          }
 +        }
 +      }
 +    });
 +  }
 +  
 +  private void waitForCrash(VM vm) {
 +    vm.invoke(new SerializableRunnable() {
 +      public void run() {
 +        MyListener listener = (MyListener) remoteObjects.get(BRIDGE_LISTENER);
 +        synchronized(listener) {
 +          long end = System.currentTimeMillis() + 10000;
 +          while (listener.crashes== 0) {
 +            try {
 +              long remaining = end - System.currentTimeMillis();
 +              if(remaining < 0) {
 +                break;
 +              }
 +              listener.wait(remaining);
 +            } catch (InterruptedException e) {
 +              fail("interrupted");
 +            }
 +          }
 +        }
 +      }
 +    });
 +  }
 +  
 +  private void waitForDeparture(VM vm) {
 +    vm.invoke(new SerializableRunnable() {
 +      public void run() {
 +        MyListener listener = (MyListener) remoteObjects.get(BRIDGE_LISTENER);
 +        synchronized(listener) {
 +          long end = System.currentTimeMillis() + 10000;
 +          while (listener.departures == 0) {
 +            try {
 +              long remaining = end - System.currentTimeMillis();
 +              if(remaining < 0) {
 +                break;
 +              }
 +              listener.wait(remaining);
 +            } catch (InterruptedException e) {
 +              fail("interrupted");
 +            }
 +          }
 +        }
 +      }
 +    });
 +  }
 +  
 +  public static class MyListener extends ClientMembershipListenerAdapter implements Serializable {
 +    protected int crashes = 0;
 +    protected int joins = 0;
 +    protected int departures= 0;
 +
 +    @Override
 +    public synchronized void memberCrashed(ClientMembershipEvent event) {
 +      crashes++;
 +      notifyAll();
 +    }
 +
 +    public synchronized void reset() {
 +      crashes = 0;
 +      joins = 0;
 +      departures = 0;
 +    }
 +
 +    @Override
 +    public synchronized void memberJoined(ClientMembershipEvent event) {
 +      joins++;
 +      notifyAll();
 +    }
 +
 +    @Override
 +    public synchronized void memberLeft(ClientMembershipEvent event) {
 +      departures++;
 +      notifyAll();
 +    }
 +
 +    public synchronized int getCrashes() {
 +      return crashes;
 +    }
 +
 +    public synchronized int getJoins() {
 +      return joins;
 +    }
 +
 +    public synchronized int  getDepartures() {
 +      return departures;
 +    }
 +  }
 +}


[42/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/DMStats.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/DMStats.java
index 7bf5b80,0000000..6091b83
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/DMStats.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/DMStats.java
@@@ -1,619 -1,0 +1,622 @@@
 +/*
 + * 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.distributed.internal;
 +
 +/**
 + * Defines the interface used to access and modify DM statistics.
 + *
 + * @author Darrel Schneider
 + *
 + */
 +public interface DMStats {
 +
 +  /**
 +   * Returns the total number of messages sent by the distribution
 +   * manager 
 +   */
 +  public long getSentMessages();
 +
 +  /**
 +   * Increments the total number of messages sent by the distribution
 +   * manager 
 +   */
 +  public void incSentMessages(long messages);
 +
 +  public void incTOSentMsg();
 +  /**
 +   * Returns the total number of transaction commit messages
 +   * sent by the distribution manager 
 +   */
 +  public long getSentCommitMessages();
 +
 +  /**
 +   * Increments the total number of transaction commit messages
 +   * sent by the distribution manager 
 +   */
 +  public void incSentCommitMessages(long messages);
 +
 +  /**
 +   * Returns the total number commits that had to wait for an ack
 +   * before completion.
 +   */
 +  public long getCommitWaits();
 +
 +  /**
 +   * Increments the number of commit waits by one.
 +   */
 +  public void incCommitWaits();
 +
 +  /**
 +   * Returns the total number of nanoseconds spent sending messages.
 +   */
 +  public long getSentMessagesTime();
 +
 +  /**
 +   * Increments the total number of nanoseconds spent sending messages.
 +   */
 +  public void incSentMessagesTime(long nanos);
 +
 +  /**
 +   * Returns the total number of messages broadcast by the distribution
 +   * manager 
 +   */
 +  public long getBroadcastMessages();
 +
 +  /**
 +   * Increments the total number of messages broadcast by the distribution
 +   * manager 
 +   */
 +  public void incBroadcastMessages(long messages);
 +
 +  /**
 +   * Returns the total number of nanoseconds spent sending messages.
 +   */
 +  public long getBroadcastMessagesTime();
 +
 +  /**
 +   * Increments the total number of nanoseconds spend sending messages.
 +   */
 +  public void incBroadcastMessagesTime(long nanos);
 +
 +  /**
 +   * Returns the total number of messages received by the distribution
 +   * manager
 +   */
 +  public long getReceivedMessages();
 +
 +  /**
 +   * Increments the total number of messages received by the distribution
 +   * manager 
 +   */
 +  public void incReceivedMessages(long messages);
 +
 +  /**
 +   * Returns the total number of message bytes received by the distribution
 +   * manager
 +   */
 +  public long getReceivedBytes();
 +
 +  /**
 +   * Increments the total number of message bytes received by the distribution
 +   * manager 
 +   */
 +  public void incReceivedBytes(long bytes);
 +
 +  /**
 +   * Increments the total number of message bytes sent by the distribution
 +   * manager 
 +   */
 +  public void incSentBytes(long bytes);
 +
 +  /**
 +   * Returns the total number of messages processed by the distribution
 +   * manager
 +   */
 +  public long getProcessedMessages();
 +
 +  /**
 +   * Increments the total number of messages processed by the distribution
 +   * manager 
 +   */
 +  public void incProcessedMessages(long messages);
 +
 +  /**
 +   * Returns the total number of nanoseconds spent processing messages.
 +   */
 +  public long getProcessedMessagesTime();
 +
 +  /**
 +   * Increments the total number of nanoseconds spent processing messages.
 +   */
 +  public void incProcessedMessagesTime(long nanos);
 +
 +  /**
 +   * Returns the total number of nanoseconds spent scheduling messages to be processed.
 +   */
 +  public long getMessageProcessingScheduleTime();
 +
 +  public void incBatchSendTime(long start);
 +  public void incBatchCopyTime(long start);
 +  public void incBatchWaitTime(long start);
 +  public void incBatchFlushTime(long start);
 +  /**
 +   * Increments the total number of nanoseconds spent scheduling messages to be processed.
 +   */
 +  public void incMessageProcessingScheduleTime(long nanos);
 +
 +  public int getOverflowQueueSize();
 +  
 +  public void incOverflowQueueSize(int messages);
 +  
 +  public int getNumProcessingThreads();
 +  
 +  public void incNumProcessingThreads(int threads);
 +
 +  public int getNumSerialThreads();
 +  
 +  public void incNumSerialThreads(int threads);
 +
 +  public void incMessageChannelTime(long val);
++
++  public void incUDPDispatchRequestTime(long val);
++  public long getUDPDispatchRequestTime();
 +  
 +  public long getReplyMessageTime();
 +  public void incReplyMessageTime(long val);
 +
 +  public long getDistributeMessageTime();
 +  public void incDistributeMessageTime(long val);
 +  
 +  public long startSocketWrite(boolean sync);
 +  public void endSocketWrite(boolean sync, long start, int bytesWritten, int retries);
 +  /**
 +   * increments
 +   * the number of unicast writes performed and the number of bytes written
 +   * @since 5.0
 +   */
 +  public void incUcastWriteBytes(int bytesWritten);
 +  /**
 +   * increment the number of unicast datagram payload bytes received and
 +   * the number of unicast reads performed
 +   */
 +  public void incUcastReadBytes(int amount);
 +  /**
 +   * increment the number of multicast datagrams sent and
 +   * the number of multicast bytes transmitted
 +   */
 +  public void incMcastWriteBytes(int bytesWritten);
 +  /**
 +   * increment the number of multicast datagram payload bytes received, and
 +   * the number of mcast messages read
 +   */
 +  public void incMcastReadBytes(int amount);
 +  /**
 +   * returns the current value of the mcastWrites statistic
 +   */
 +  public int getMcastWrites();
 +  public int getMcastReads();
 +
 +  public long startSerialization();
 +  public void endSerialization(long start, int bytes);
 +  public long startDeserialization();
 +  public void endDeserialization(long start, int bytes);
 +  public long startMsgSerialization();
 +  public void endMsgSerialization(long start);
 +  public long startMsgDeserialization();
 +  public void endMsgDeserialization(long start);
 +
 +  public int getNodes();
 +  public void setNodes(int val);
 +  public void incNodes(int val);
 +  public int getReplyWaitsInProgress();
 +  public int getReplyWaitsCompleted();
 +  public long getReplyWaitTime();
 +  /**
 +   * @return the timestamp that marks the start of the operation
 +   */
 +  public long startReplyWait();
 +  /**
 +   * @param startNanos the timestamp taken when the operation started
 +   * @param initTime the time the operation begain (before msg transmission) 
 +   */
 +  public void endReplyWait(long startNanos, long initTime);
 +
 +  /**
 +   * Increments the number of message replies that have timed out
 +   *
 +   * @since 3.5
 +   */
 +  public void incReplyTimeouts();
 +
 +  /**
 +   * Returns the number of message replies that have timed out
 +   *
 +   * @since 3.5
 +   */
 +  public long getReplyTimeouts();
 +
 +  /**
 +   * @since 4.1
 +   */
 +  public void incReceivers();
 +  /**
 +   * @since 4.1
 +   */
 +  public void decReceivers();
 +  /**
 +   * @since 4.1
 +   */
 +  public void incFailedAccept();
 +  /**
 +   * @since 4.1
 +   */
 +  public void incFailedConnect();
 +  /**
 +   * @since 4.1.1
 +   */
 +  public void incReconnectAttempts();
 +  /**
 +   * @since 4.1
 +   */
 +  public void incLostLease();
 +  /**
 +   * @since 4.1
 +   */
 +  public void incSenders(boolean shared, boolean preserveOrder);
 +  /**
 +   * @since 4.1
 +   */
 +  public void decSenders(boolean shared, boolean preserveOrder);
 +  /**
 +   * @since 4.1
 +   */
 +  public int getSendersSU();
 +  /**
 +   * increment the number of unicast UDP retransmission requests received from
 +   * other processes
 +   * @since 5.0
 +   */
 +  public void incUcastRetransmits();
 +  /**
 +   * increment the number of multicast UDP retransmissions sent to
 +   * other processes
 +   * @since 5.0
 +   */
 +  public void incMcastRetransmits();
 +  /**
 +   * returns the current number of multicast retransmission requests
 +   * processed
 +   */
 +  public int getMcastRetransmits();
 +  /**
 +   * increment the number of multicast UDP retransmission requests sent to
 +   * other processes
 +   * @since 5.0
 +   */
 +  public void incMcastRetransmitRequests();
 +
 +  /**
 +   * @since 4.2.2
 +   */
 +  public int getAsyncSocketWritesInProgress();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public int getAsyncSocketWrites();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public int getAsyncSocketWriteRetries();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public long getAsyncSocketWriteBytes();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public long getAsyncSocketWriteTime();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public int getAsyncQueues();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public void incAsyncQueues(int inc);
 +  /**
 +   * @since 4.2.2
 +   */
 +  public int getAsyncQueueFlushesInProgress();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public int getAsyncQueueFlushesCompleted();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public long getAsyncQueueFlushTime();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public long startAsyncQueueFlush();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public void endAsyncQueueFlush(long start);
 +  /**
 +   * @since 4.2.2
 +   */
 +  public int getAsyncQueueTimeouts();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public void incAsyncQueueTimeouts(int inc);
 +  /**
 +   * @since 4.2.2
 +   */
 +  public int getAsyncQueueSizeExceeded();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public void incAsyncQueueSizeExceeded(int inc);
 +  /**
 +   * @since 4.2.2
 +   */
 +  public int getAsyncDistributionTimeoutExceeded();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public void incAsyncDistributionTimeoutExceeded();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public long getAsyncQueueSize();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public void incAsyncQueueSize(long inc);
 +  /**
 +   * @since 4.2.2
 +   */
 +  public long getAsyncQueuedMsgs();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public void incAsyncQueuedMsgs();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public long getAsyncDequeuedMsgs();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public void incAsyncDequeuedMsgs();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public long getAsyncConflatedMsgs();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public void incAsyncConflatedMsgs();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public int getAsyncThreads();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public void incAsyncThreads(int inc);
 +  /**
 +   * @since 4.2.2
 +   */
 +  public int getAsyncThreadInProgress();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public int getAsyncThreadCompleted();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public long getAsyncThreadTime();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public long startAsyncThread();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public void endAsyncThread(long start);
 +  /**
 +   * @since 4.2.2
 +   */
 +  public long getAsyncQueueAddTime();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public void incAsyncQueueAddTime(long inc);
 +  /**
 +   * @since 4.2.2
 +   */
 +  public long getAsyncQueueRemoveTime();
 +  /**
 +   * @since 4.2.2
 +   */
 +  public void incAsyncQueueRemoveTime(long inc);
 +
 +  /**
 +   * @since 5.0.2.4 
 +   */
 +  public void incReceiverBufferSize(int inc, boolean direct);
 +  /**
 +   * @since 5.0.2.4 
 +   */
 +  public void incSenderBufferSize(int inc, boolean direct);
 +  /**
 +   * @since 5.0.2.4 
 +   */
 +  public long startSocketLock();
 +  /**
 +   * @since 5.0.2.4 
 +   */
 +  public void endSocketLock(long start);
 +
 +  /**
 +   * @since 5.0.2.4 
 +   */
 +  public long startBufferAcquire();
 +  /**
 +   * @since 5.0.2.4 
 +   */
 +  public void endBufferAcquire(long start);
 +
 +  /**
 +   * increment/decrement the number of thread-owned receivers with the given
 +   * domino count
 +   * @param value
 +   * @param dominoCount thread-owned connection chain count
 +   */
 +  public void incThreadOwnedReceivers(long value, int dominoCount);
 +  
 +  /**
 +   * Called when a new message is received.
 +   * @param newMsg true if a new message being received was detected; false if
 +   *   this is just additional data for a message already detected.
 +   * @param bytes the number of bytes read, so far, for the message being received. 
 +   * @since 5.0.2
 +   */
 +  public void incMessagesBeingReceived(boolean newMsg, int bytes);
 +  /**
 +   * Called when we finish processing a received message.
 +   * @param bytes the number of bytes read off the wire for the message we have finished with.
 +   * @since 5.0.2
 +   */
 +  public void decMessagesBeingReceived(int bytes);
 +
 +  public void incReplyHandOffTime(long start);
 +  
 +  /**
 +   * Returns 1 if the system elder is this member, else returns 0.
 +   * @return 1 if the system elder is this member, else returns 0
 +   */
 +  public int getElders();
 +  public void incElders(int val);
 +  
 +  /**
 +   * Returns the number of initial image reply messages sent from this member
 +   * which have not yet been acked.  
 +   */
 +  public int getInitialImageMessagesInFlight();
 +
 +  public void incInitialImageMessagesInFlight(int val);
 +  
 +  /**
 +   * Returns the number of initial images this member is currently requesting. 
 +   */
 +  public int getInitialImageRequestsInProgress();
 +  
 +  public void incInitialImageRequestsInProgress(int val);
 +
 +  public void incPdxSerialization(int bytesWritten);
 +
 +  public void incPdxDeserialization(int i);
 +
 +  public long startPdxInstanceDeserialization();
 +
 +  public void endPdxInstanceDeserialization(long start);
 +  public void incPdxInstanceCreations();
 +  
 +  //Stats for GMSHealthMonitor
 +  public long getHeartbeatRequestsSent();
 +  
 +  public void incHeartbeatRequestsSent();
 +  
 +  public long getHeartbeatRequestsReceived();
 +  
 +  public void incHeartbeatRequestsReceived();
 +  
 +  public long getHeartbeatsSent();
 +  
 +  public void incHeartbeatsSent();
 +
 +  public long getHeartbeatsReceived();
 +  
 +  public void incHeartbeatsReceived();
 +  
 +
 +  public long getSuspectsSent();
 +  
 +  public void incSuspectsSent();
 +
 +  public long getSuspectsReceived();
 +  
 +  public void incSuspectsReceived();
 +  
 +  
 +  public long getFinalCheckRequestsSent();
 +  
 +  public void incFinalCheckRequestsSent();
 +  
 +  public long getFinalCheckRequestsReceived();
 +  
 +  public void incFinalCheckRequestsReceived();
 +  
 +  public long getFinalCheckResponsesSent();
 +  
 +  public void incFinalCheckResponsesSent();
 +  
 +  public long getFinalCheckResponsesReceived();
 +  
 +  public void incFinalCheckResponsesReceived();
 +  
 +  
 +  public long getTcpFinalCheckRequestsSent();
 +  
 +  public void incTcpFinalCheckRequestsSent();
 +
 +  public long getTcpFinalCheckRequestsReceived();
 +  
 +  public void incTcpFinalCheckRequestsReceived();
 +  
 +  public long getTcpFinalCheckResponsesSent();
 +  
 +  public void incTcpFinalCheckResponsesSent();
 +
 +  public long getTcpFinalCheckResponsesReceived();
 +  
 +  public void incTcpFinalCheckResponsesReceived();
 +
 +  
 +  public long getUdpFinalCheckRequestsSent();
 +  
 +  public void incUdpFinalCheckRequestsSent();
 +  
 +//  UDP final check is implemented using HeartbeatRequestMessage and HeartbeatMessage
 +//  So the following code is commented out.
 +  
 +//  public long getUdpFinalCheckRequestsReceived();
 +//  
 +//  public void incUdpFinalCheckRequestsReceived();
 +//  
 +//  public long getUdpFinalCheckResponsesSent();
 +//  
 +//  public void incUdpFinalCheckResponsesSent();
 +
 +  public long getUdpFinalCheckResponsesReceived();
 +  
 +  public void incUdpFinalCheckResponsesReceived();
 +}


[35/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/BucketRegion.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/cache/BucketRegion.java
index fae381f,0000000..69f61c4
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/BucketRegion.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/BucketRegion.java
@@@ -1,2614 -1,0 +1,2622 @@@
 +/*
 + * 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;
 +
 +import java.io.DataOutput;
 +import java.io.IOException;
 +import java.io.InputStream;
 +import java.util.Collections;
 +import java.util.HashMap;
 +import java.util.HashSet;
 +import java.util.Iterator;
 +import java.util.Map;
 +import java.util.Set;
 +import java.util.concurrent.atomic.AtomicLong;
 +import java.util.concurrent.atomic.AtomicReference;
 +import java.util.concurrent.locks.Lock;
 +
 +import org.apache.logging.log4j.Logger;
 +
 +import com.gemstone.gemfire.CancelException;
 +import com.gemstone.gemfire.CopyHelper;
 +import com.gemstone.gemfire.DataSerializer;
 +import com.gemstone.gemfire.DeltaSerializationException;
 +import com.gemstone.gemfire.GemFireIOException;
 +import com.gemstone.gemfire.InternalGemFireError;
 +import com.gemstone.gemfire.InvalidDeltaException;
 +import com.gemstone.gemfire.SystemFailure;
 +import com.gemstone.gemfire.cache.CacheClosedException;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheWriter;
 +import com.gemstone.gemfire.cache.CacheWriterException;
 +import com.gemstone.gemfire.cache.CustomEvictionAttributes;
 +import com.gemstone.gemfire.cache.DiskAccessException;
 +import com.gemstone.gemfire.cache.EntryNotFoundException;
 +import com.gemstone.gemfire.cache.EvictionAction;
 +import com.gemstone.gemfire.cache.EvictionAlgorithm;
 +import com.gemstone.gemfire.cache.EvictionAttributes;
 +import com.gemstone.gemfire.cache.EvictionCriteria;
 +import com.gemstone.gemfire.cache.ExpirationAction;
 +import com.gemstone.gemfire.cache.Operation;
 +import com.gemstone.gemfire.cache.RegionAttributes;
 +import com.gemstone.gemfire.cache.RegionDestroyedException;
 +import com.gemstone.gemfire.cache.TimeoutException;
 +import com.gemstone.gemfire.cache.hdfs.HDFSIOException;
 +import com.gemstone.gemfire.cache.hdfs.internal.hoplog.HoplogOrganizer;
 +import com.gemstone.gemfire.cache.partition.PartitionListener;
 +import com.gemstone.gemfire.cache.query.internal.IndexUpdater;
 +import com.gemstone.gemfire.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.DistributedSystem;
 +import com.gemstone.gemfire.distributed.internal.AtomicLongWithTerminalState;
 +import com.gemstone.gemfire.distributed.internal.DirectReplyProcessor;
 +import com.gemstone.gemfire.distributed.internal.DistributionAdvisor.Profile;
 +import com.gemstone.gemfire.distributed.internal.DistributionStats;
 +import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
 +import com.gemstone.gemfire.internal.Assert;
 +import com.gemstone.gemfire.internal.HeapDataOutputStream;
 +import com.gemstone.gemfire.internal.Version;
 +import com.gemstone.gemfire.internal.cache.BucketAdvisor.BucketProfile;
 +import com.gemstone.gemfire.internal.cache.FilterRoutingInfo.FilterInfo;
 +import com.gemstone.gemfire.internal.cache.control.MemoryEvent;
 +import com.gemstone.gemfire.internal.cache.delta.Delta;
 +import com.gemstone.gemfire.internal.cache.partitioned.Bucket;
 +import com.gemstone.gemfire.internal.cache.partitioned.DestroyMessage;
 +import com.gemstone.gemfire.internal.cache.partitioned.InvalidateMessage;
 +import com.gemstone.gemfire.internal.cache.partitioned.LockObject;
 +import com.gemstone.gemfire.internal.cache.partitioned.PRTombstoneMessage;
 +import com.gemstone.gemfire.internal.cache.partitioned.PartitionMessage;
 +import com.gemstone.gemfire.internal.cache.partitioned.PutAllPRMessage;
 +import com.gemstone.gemfire.internal.cache.partitioned.PutMessage;
 +import com.gemstone.gemfire.internal.cache.partitioned.RemoveAllPRMessage;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.CacheClientNotifier;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.ClientProxyMembershipID;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.ClientTombstoneMessage;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.ClientUpdateMessage;
 +import com.gemstone.gemfire.internal.cache.versions.VersionSource;
 +import com.gemstone.gemfire.internal.cache.versions.VersionStamp;
 +import com.gemstone.gemfire.internal.cache.versions.VersionTag;
 +import com.gemstone.gemfire.internal.cache.wan.GatewaySenderEventImpl;
 +import com.gemstone.gemfire.internal.cache.wan.parallel.ConcurrentParallelGatewaySenderQueue;
 +import com.gemstone.gemfire.internal.concurrent.Atomics;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.logging.LogService;
 +import com.gemstone.gemfire.internal.logging.log4j.LocalizedMessage;
 +import com.gemstone.gemfire.internal.logging.log4j.LogMarker;
 +import com.gemstone.gemfire.internal.offheap.StoredObject;
 +import com.gemstone.gemfire.internal.offheap.annotations.Unretained;
 +import com.gemstone.gemfire.internal.concurrent.AtomicLong5;
 +
 +
 +/**
 + * The storage used for a Partitioned Region.
 + * This class asserts distributed scope as well as a replicate data policy
 + * It does not support transactions
 + * 
 + * Primary election for a BucketRegion can be found in the 
 + * {@link com.gemstone.gemfire.internal.cache.BucketAdvisor} class
 + * 
 + * @author Mitch Thomas
 + * @since 5.1
 + *
 + */
 +public class BucketRegion extends DistributedRegion
 +implements Bucket
 +{
 +  private static final Logger logger = LogService.getLogger();
 +  
 +  public static final RawValue NULLVALUE = new RawValue(null);
 +  public static final RawValue REQUIRES_ENTRY_LOCK = new RawValue(null);
 +  /**
 +   * A special value for the bucket size indicating that this bucket
 +   * has been destroyed.
 +   */
 +  private static final long BUCKET_DESTROYED = Long.MIN_VALUE;
 +  private AtomicLong counter = new AtomicLong();
 +  private AtomicLong limit;
 +  private final AtomicLong numOverflowOnDisk = new AtomicLong();
 +  private final AtomicLong numOverflowBytesOnDisk = new AtomicLong();
 +  private final AtomicLong numEntriesInVM = new AtomicLong();
 +  private final AtomicLong evictions = new AtomicLong();
 +  
 +  /**
 +   * Contains size in bytes of the values stored
 +   * in theRealMap. Sizes are tallied during put and remove operations.
 +   */
 +  private final AtomicLongWithTerminalState bytesInMemory = new AtomicLongWithTerminalState();
 +  
 +  public static final class RawValue {
 +    private final Object rawValue;
 +    public RawValue(Object rawVal) {
 +      this.rawValue = rawVal;
 +    }
 +
 +    public final boolean isValueByteArray() {
 +      return this.rawValue instanceof byte[];
 +    }
 +    
 +    public Object getRawValue() {
 +      return this.rawValue;
 +    }
 +
 +    public void writeAsByteArray(DataOutput out) throws IOException {
 +      if (isValueByteArray()) {
 +        DataSerializer.writeByteArray((byte[]) this.rawValue, out);
 +      } else if (this.rawValue instanceof CachedDeserializable) {
 +        ((CachedDeserializable)this.rawValue).writeValueAsByteArray(out);
 +      } else if (Token.isInvalid(this.rawValue)) {
 +        DataSerializer.writeByteArray(null, out);
 +      } else if (this.rawValue == Token.TOMBSTONE) { 
 +        DataSerializer.writeByteArray(null, out);
 +      } else {
 +        DataSerializer.writeObjectAsByteArray(this.rawValue, out);
 +      }
 +    }
 +    
 +    @Override
 +    public String toString() {
 +      return "RawValue("+this.rawValue+")";
 +    }
 +
 +    /**
 +     * Return the de-serialized value without changing the stored form
 +     * in the heap.  This causes local access to create a de-serialized copy (extra work)
 +     * in favor of keeping values in serialized form which is important because
 +     * it makes remote access more efficient.  This assumption is that remote
 +     * access is much more frequent.
 +     * TODO Unused, but keeping for potential performance boost when local Bucket 
 +     * access de-serializes the entry (which could hurt perf.)
 +     * 
 +     * @return the de-serialized value
 +     */
 +    public Object getDeserialized(boolean copyOnRead) {
 +      if (isValueByteArray()) {
 +        if (copyOnRead) {
 +          // TODO move this code to CopyHelper.copy?
 +          byte[] src = (byte[])this.rawValue;
 +          byte[] dest = new byte[src.length];
 +          System.arraycopy(this.rawValue, 0, dest, 0, dest.length);
 +          return dest;
 +        } else {
 +          return this.rawValue;
 +        }
 +      } else if (this.rawValue instanceof CachedDeserializable) {
 +        if (copyOnRead) {
 +          return ((CachedDeserializable)this.rawValue).getDeserializedWritableCopy(null, null);
 +        } else {
 +          return ((CachedDeserializable)this.rawValue).getDeserializedForReading();
 +        }
 +      } else if (Token.isInvalid(this.rawValue)) {
 +        return null;
 +      } else {
 +        if (copyOnRead) {
 +          return CopyHelper.copy(this.rawValue);
 +        } else {
 +          return this.rawValue;
 +        }
 +      }
 +    }
 +  }
 +
 +  private static final long serialVersionUID = 1L;
 +
 +  private final int redundancy;
 +  
 +  /** the partitioned region to which this bucket belongs */
 +  private final PartitionedRegion partitionedRegion;
 +  private final Map<Object, ExpiryTask> pendingSecondaryExpires = new HashMap<Object, ExpiryTask>(); 
 + 
 +  /* one map per bucket region */
 +  public HashMap allKeysMap = new HashMap();
 +
 +  static final boolean FORCE_LOCAL_LISTENERS_INVOCATION = 
 +    Boolean.getBoolean("gemfire.BucketRegion.alwaysFireLocalListeners");
 +  // gemfire.BucktRegion.alwaysFireLocalListeners=true
 +  
 +  private volatile AtomicLong5 eventSeqNum = null;
 +  
 +  public AtomicLong5 getEventSeqNum() {
 +    return eventSeqNum;
 +  }
 +
 +  protected final AtomicReference<HoplogOrganizer> hoplog = new AtomicReference<HoplogOrganizer>();
 +  
 +  public BucketRegion(String regionName, RegionAttributes attrs,
 +      LocalRegion parentRegion, GemFireCacheImpl cache,
 +      InternalRegionArguments internalRegionArgs) {
 +    super(regionName, attrs, parentRegion, cache, internalRegionArgs);
 +    if(PartitionedRegion.DISABLE_SECONDARY_BUCKET_ACK) {
 +      Assert.assertTrue(attrs.getScope().isDistributedNoAck());
 +    } 
 +    else {
 +      Assert.assertTrue(attrs.getScope().isDistributedAck());
 +    }
 +    Assert.assertTrue(attrs.getDataPolicy().withReplication());
 +    Assert.assertTrue( ! attrs.getEarlyAck());
 +    Assert.assertTrue(isUsedForPartitionedRegionBucket());
 +    Assert.assertTrue( ! isUsedForPartitionedRegionAdmin());
 +    Assert.assertTrue(internalRegionArgs.getBucketAdvisor() != null);
 +    Assert.assertTrue(internalRegionArgs.getPartitionedRegion() != null);
 +    this.redundancy = internalRegionArgs.getPartitionedRegionBucketRedundancy();
 +    this.partitionedRegion = internalRegionArgs.getPartitionedRegion();
 +  }
 +  
 +  // Attempt to direct the GII process to the primary first
 +  @Override
 +  protected void initialize(InputStream snapshotInputStream,
 +      InternalDistributedMember imageTarget,
 +      InternalRegionArguments internalRegionArgs) 
 +    throws TimeoutException, IOException, ClassNotFoundException
 +  {
 +    // Set this region in the ProxyBucketRegion early so that profile exchange will
 +    // perform the correct fillInProfile method
 +    getBucketAdvisor().getProxyBucketRegion().setBucketRegion(this);
 +    boolean success = false;
 +    try {
 +      if (this.partitionedRegion.isShadowPR()
 +          && this.partitionedRegion.getColocatedWith() != null) {
 +        PartitionedRegion parentPR = ColocationHelper
 +            .getLeaderRegion(this.partitionedRegion);
 +        BucketRegion parentBucket = parentPR.getDataStore().getLocalBucketById(
 +            getId());
 +        // needs to be set only once.
 +        if (parentBucket.eventSeqNum == null) {
 +          parentBucket.eventSeqNum = new AtomicLong5(getId());
 +        }
 +      }
 +      if (this.partitionedRegion.getColocatedWith() == null) {
 +        this.eventSeqNum = new AtomicLong5(getId());
 +      } else {
 +        PartitionedRegion parentPR = ColocationHelper
 +            .getLeaderRegion(this.partitionedRegion);
 +        BucketRegion parentBucket = parentPR.getDataStore().getLocalBucketById(
 +            getId());
 +        if (parentBucket == null && logger.isDebugEnabled()) {
 +          logger.debug("The parentBucket of region {} bucketId {} is NULL", this.partitionedRegion.getFullPath(), getId());
 +        }
 +        Assert.assertTrue(parentBucket != null);
 +        this.eventSeqNum = parentBucket.eventSeqNum;
 +      }
 +      
 +      final InternalDistributedMember primaryHolder = 
 +        getBucketAdvisor().basicGetPrimaryMember();
 +      if (primaryHolder != null && ! primaryHolder.equals(getMyId())) {
 +        // Ignore the provided image target, use an existing primary (if any)
 +        super.initialize(snapshotInputStream, primaryHolder, internalRegionArgs);
 +      } else {
 +        super.initialize(snapshotInputStream, imageTarget, internalRegionArgs);
 +      }
 +      
 +      success = true;
 +    } finally {
 +      if(!success) {
 +        removeFromPeersAdvisors(false);
 +        getBucketAdvisor().getProxyBucketRegion().clearBucketRegion(this);
 +      }
 +    }
 +  }
 +  
 +  
 +
 +  @Override
 +  public void initialized() {
 +    //announce that the bucket is ready
 +    //setHosting performs a profile exchange, so there
 +    //is no need to call super.initialized() here.
 +  }
 +  
 +  @Override
 +  protected DiskStoreImpl findDiskStore(RegionAttributes ra, InternalRegionArguments internalRegionArgs) {
 +    return internalRegionArgs.getPartitionedRegion().getDiskStore();
 +  }
 +
 +  @Override
 +  public void createEventTracker() {
 +    this.eventTracker = new EventTracker(this);
 +    this.eventTracker.start();
 +  }
 +  
 +  @Override
 +  protected CacheDistributionAdvisor createDistributionAdvisor(
 +      InternalRegionArguments internalRegionArgs){
 +    return internalRegionArgs.getBucketAdvisor();
 +  }
 +  
 +  public BucketAdvisor getBucketAdvisor() {
 +    return (BucketAdvisor) getDistributionAdvisor();
 +  }
 +  
 +  public boolean isHosting() {
 +    return getBucketAdvisor().isHosting();
 +  }
 +  
 +  @Override
 +  protected EventID distributeTombstoneGC(Set<Object> keysRemoved) {
 +    EventID eventId = super.distributeTombstoneGC(keysRemoved);
 +    if (keysRemoved != null && keysRemoved.size() > 0 && getFilterProfile() != null) {
 +      // send the GC to members that don't have the bucket but have the PR so they
 +      // can forward the event to clients
 +      PRTombstoneMessage.send(this, keysRemoved, eventId);
 +    }
 +    return eventId;
 +  }
 +
 +  @Override
 +  protected void notifyClientsOfTombstoneGC(Map<VersionSource, Long> regionGCVersions, Set<Object>removedKeys, EventID eventID, FilterInfo routing) {
 +    if (CacheClientNotifier.getInstance() != null) {
 +      // Only route the event to clients interested in the partitioned region.
 +      // We do this by constructing a region-level event and then use it to
 +      // have the filter profile ferret out all of the clients that have interest
 +      // in this region
 +      FilterProfile fp = getFilterProfile();
 +      if ((removedKeys != null && removedKeys.size() > 0) // bug #51877 - NPE in clients
 +          && (routing != null || fp != null)) { // fix for bug #46309 - don't send null/empty key set to clients
 +        RegionEventImpl regionEvent = new RegionEventImpl(getPartitionedRegion(), Operation.REGION_DESTROY, null, true, getMyId()); 
 +        FilterInfo clientRouting = routing;
 +        if (clientRouting == null) {
 +          clientRouting = fp.getLocalFilterRouting(regionEvent);
 +        }
 +        regionEvent.setLocalFilterInfo(clientRouting); 
 +          
 +        ClientUpdateMessage clientMessage = ClientTombstoneMessage.gc(getPartitionedRegion(), removedKeys,
 +            eventID);
 +        CacheClientNotifier.notifyClients(regionEvent, clientMessage);
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Search the CM for keys. If found any, return the first found one
 +   * Otherwise, save the keys into the CM, and return null
 +   * The thread will acquire the lock before searching.
 +   * 
 +   * @param keys
 +   * @return first key found in CM
 +   *         null means not found
 +   */
 +  private LockObject searchAndLock(Object keys[]) {
 +    final boolean isDebugEnabled = logger.isDebugEnabled();
 +    
 +    LockObject foundLock = null;
 +    
 +    synchronized(allKeysMap) {
 +      // check if there's any key in map
 +      for (int i=0; i<keys.length; i++) {
 +        if (allKeysMap.containsKey(keys[i])) {
 +          foundLock = (LockObject)allKeysMap.get(keys[i]);
 +          if (isDebugEnabled) {
 +            logger.debug("LockKeys: found key: {}:{}", keys[i], foundLock.lockedTimeStamp);
 +          }
 +          break;
 +        }
 +      }
 +      
 +      // save the keys when still locked
 +      if (foundLock == null) {
 +        for (int i=0; i<keys.length; i++) {
 +          LockObject lockValue = new LockObject(keys[i], isDebugEnabled?System.currentTimeMillis():0);
 +          allKeysMap.put(keys[i], lockValue);
 +          if (isDebugEnabled) {
 +            logger.debug("LockKeys: add key: {}:{}", keys[i], lockValue.lockedTimeStamp);
 +          }
 +        }
 +      }
 +    } 
 +    
 +    return foundLock;
 +  }
 +
 +  /**
 +   * After processed the keys, this method will remove them from CM. 
 +   * And notifyAll for each key. 
 +   * The thread needs to acquire lock of CM first.
 +   * 
 +   * @param keys
 +   */
 +  public void removeAndNotifyKeys(Object keys[]) {
 +    final boolean isTraceEnabled = logger.isTraceEnabled();
 +    
 +    synchronized(allKeysMap) {
 +      for (int i=0; i<keys.length; i++) {
 +        LockObject lockValue = (LockObject)allKeysMap.remove(keys[i]);
 +        if (lockValue != null) {
 +          // let current thread become the monitor of the key object
 +          synchronized (lockValue) {
 +            lockValue.setRemoved();
 +            if (isTraceEnabled) {
 +              long waitTime = System.currentTimeMillis()-lockValue.lockedTimeStamp;
 +              logger.trace("LockKeys: remove key {}, notifyAll for {}. It waited", keys[i], lockValue, waitTime);
 +            }
 +            lockValue.notifyAll();
 +          }
 +        }
 +      } // for
 +    } 
 +  }
 +  
 +  /**
 +   * Keep checking if CM has contained any key in keys. If yes, wait for notify,
 +   * then retry again. This method will block current thread for long time. 
 +   * It only exits when current thread successfully save its keys into CM. 
 +   * 
 +   * @param keys
 +   */
 +  public void waitUntilLocked(Object keys[]) {
 +    final boolean isDebugEnabled = logger.isDebugEnabled();
 +    
 +    final String title = "BucketRegion.waitUntilLocked:";
 +    while (true) {
 +      LockObject foundLock = searchAndLock(keys);
 +
 +      if (foundLock != null) {
 +        synchronized(foundLock) {
 +          try {
 +            while (!foundLock.isRemoved()) {
 +              this.partitionedRegion.checkReadiness();
 +              foundLock.wait(1000);
 +              // primary could be changed by prRebalancing while waiting here
 +              checkForPrimary();
 +            }
 +          }
 +          catch (InterruptedException e) {
 +            // TODO this isn't a localizable string and it's being logged at info level
 +            if (isDebugEnabled) {
 +              logger.debug("{} interrupted while waiting for {}", title, foundLock, e.getMessage());
 +            }
 +          }
 +          if (isDebugEnabled) {
 +            long waitTime = System.currentTimeMillis()-foundLock.lockedTimeStamp;
 +            logger.debug("{} waited {} ms to lock", title, waitTime, foundLock);
 +          }
 +        }
 +      } else {
 +        // now the keys have been locked by this thread
 +        break;
 +      } // to lock and process
 +    } // while
 +  }
 +  
 +  // Entry (Put/Create) rules
 +  // If this is a primary for the bucket
 +  //  1) apply op locally, aka update or create entry
 +  //  2) distribute op to bucket secondaries and bridge servers with synchrony on local entry
 +  //  3) cache listener with synchrony on entry
 +  // Else not a primary
 +  //  1) apply op locally
 +  //  2) update local bs, gateway
 +  @Override
 +  protected
 +  boolean virtualPut(EntryEventImpl event,
 +                     boolean ifNew,
 +                     boolean ifOld,
 +                     Object expectedOldValue,
 +                     boolean requireOldValue,
 +                     long lastModified,
 +                     boolean overwriteDestroyed)
 + throws TimeoutException,
 +      CacheWriterException {
 +    beginLocalWrite(event);
 +    
 +    try {
 +      if (this.partitionedRegion.isParallelWanEnabled()) {
 +        handleWANEvent(event);
 +      }
 +      if (!hasSeenEvent(event)) {
 +        forceSerialized(event);
 +        RegionEntry oldEntry = this.entries
 +            .basicPut(event, lastModified, ifNew, ifOld, expectedOldValue,
 +                requireOldValue, overwriteDestroyed);
 +        return oldEntry != null;
 +      }
 +      if (event.getDeltaBytes() != null && event.getRawNewValue() == null) {
 +        // This means that this event has delta bytes but no full value.
 +        // Request the full value of this event.
 +        // The value in this vm may not be same as this event's value.
 +        throw new InvalidDeltaException(
 +            "Cache encountered replay of event containing delta bytes for key "
 +                + event.getKey());
 +      }
 +      // Forward the operation and event messages
 +      // to members with bucket copies that may not have seen the event. Their
 +      // EventTrackers will keep them from applying the event a second time if
 +      // they've already seen it.
 +      if (logger.isTraceEnabled(LogMarker.DM)) {
 +        logger.trace(LogMarker.DM, "BR.virtualPut: this cache has already seen this event {}", event);
 +      }
-       distributeUpdateOperation(event, lastModified);
++      if (!getConcurrencyChecksEnabled() || event.hasValidVersionTag()) {
++        distributeUpdateOperation(event, lastModified);
++      }
 +      return true;
 +    } finally {
 +      endLocalWrite(event);
 +    }
 +  }
 +
 +  
 +  public long generateTailKey() {
 +    long key = this.eventSeqNum.addAndGet(this.partitionedRegion
 +        .getTotalNumberOfBuckets());
 +    if (key < 0
 +        || key % getPartitionedRegion().getTotalNumberOfBuckets() != getId()) {
 +      logger
 +          .error(LocalizedMessage
 +              .create(
 +                  LocalizedStrings.GatewaySender_SEQUENCENUMBER_GENERATED_FOR_EVENT_IS_INVALID,
 +                  new Object[] { key, getId() }));
 +    }
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("WAN: On primary bucket {}, setting the seq number as {}",
 +          getId(), this.eventSeqNum.get());
 +    }
 +    return eventSeqNum.get();
 +  }
 +  
 +  public void handleWANEvent(EntryEventImpl event) {
 +    if (this.eventSeqNum == null) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("The bucket corresponding to this user bucket is not created yet. This event will not go to remote wan site. Event: {}", event);
 +      }
 +    }
 +    
 +    if (!(this instanceof AbstractBucketRegionQueue)) {
 +      if (getBucketAdvisor().isPrimary()) {
 +        long key = this.eventSeqNum.addAndGet(this.partitionedRegion.getTotalNumberOfBuckets());
 +        if (key < 0
 +            || key % getPartitionedRegion().getTotalNumberOfBuckets() != getId()) {
 +          logger.error(LocalizedMessage.create(LocalizedStrings.GatewaySender_SEQUENCENUMBER_GENERATED_FOR_EVENT_IS_INVALID,
 +                  new Object[] { key, getId() }));
 +        }
 +        event.setTailKey(key);
 +        if (logger.isDebugEnabled()) {
 +          logger.debug("WAN: On primary bucket {}, setting the seq number as {}", getId(), this.eventSeqNum.get());
 +        }
 +      } else {
 +        // Can there be a race here? Like one thread has done put in primary but
 +        // its update comes later
 +        // in that case its possible that a tail key is missed.
 +        // we can handle that by only incrementing the tailKey and never
 +        // setting it less than the current value.
 +        Atomics.setIfGreater(this.eventSeqNum, event.getTailKey());
 +        if (logger.isDebugEnabled()) {
 +          logger.debug("WAN: On secondary bucket {}, setting the seq number as {}", getId(), event.getTailKey());
 +        }
 +      }
 +    }
 +  }
 +  /**
 +   * Fix for Bug#45917
 +   * We are updating the seqNumber so that new seqNumbers are 
 +   * generated starting from the latest in the system.
 +   * @param l
 +   */
 +
 +  public void updateEventSeqNum(long l) {
 +    Atomics.setIfGreater(this.eventSeqNum, l);
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("WAN: On bucket {}, setting the seq number as {} before GII", getId(), l);
 +    }
 +  }
 +  
 +  protected void distributeUpdateOperation(EntryEventImpl event, long lastModified) {
 +    if (!event.isOriginRemote()
 +        && !event.isNetSearch()
 +        && getBucketAdvisor().isPrimary()) {
 +      if (event.isBulkOpInProgress()) {
 +        // consolidate the UpdateOperation for each entry into a PutAllMessage
 +        // since we did not call basicPutPart3(), so we have to explicitly addEntry here
 +        event.getPutAllOperation().addEntry(event, this.getId());
 +      } else {
 +        new UpdateOperation(event, lastModified).distribute();
 +        if (logger.isDebugEnabled()) {
 +          logger.debug("sent update operation : for region  : {}: with event: {}", this.getName(), event);
 +        }
 +      }
 +    }
 +    if (!event.getOperation().isPutAll()) {  // putAll will invoke listeners later
 +      event.invokeCallbacks(this, true, true);
 +    }
 +  }
 +  
 +  /**
 +   * distribute the operation in basicPutPart2 so the region entry lock is
 +   * held
 +   */
 +  @Override
 +  protected long basicPutPart2(EntryEventImpl event, RegionEntry entry, boolean isInitialized, 
 +      long lastModified, boolean clearConflict)  {
 +    // Assumed this is called with entry synchrony
 +
 +    // Typically UpdateOperation is called with the
 +    // timestamp returned from basicPutPart2, but as a bucket we want to do
 +    // distribution *before* we do basicPutPart2.
 +    final long modifiedTime = event.getEventTime(lastModified);
 +    // Update the get stats if necessary. 
 +    if (this.partitionedRegion.getDataStore().hasClientInterest(event)) {
 +      updateStatsForGet(entry, true);
 +    }
 +    if (!event.isOriginRemote()) {
 +      if (event.getVersionTag() == null || event.getVersionTag().isGatewayTag()) {
 +        boolean eventHasDelta = event.getDeltaBytes() != null;
 +        VersionTag v = entry.generateVersionTag(null, eventHasDelta, this, event);
 +        if (v != null) {
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("generated version tag {} in region {}", v, this.getName());
 +          }
 +        }
 +      }
 +
 +      // This code assumes it is safe ignore token mode (GII in progress) 
 +      // because it assumes when the origin of the event is local,
 +      // the GII has completed and the region is initialized and open for local
 +      // ops
 +      if (!event.isBulkOpInProgress()) {
 +        long start = this.partitionedRegion.getPrStats().startSendReplication();
 +        try {
 +          UpdateOperation op = new UpdateOperation(event, modifiedTime);
 +          op.distribute();
 +        } finally {
 +          this.partitionedRegion.getPrStats().endSendReplication(start);
 +        }
 +      } else {
 +        // consolidate the UpdateOperation for each entry into a PutAllMessage
 +        // basicPutPart3 takes care of this
 +      }
 +    }
 +
 +    return super.basicPutPart2(event, entry, isInitialized, lastModified, clearConflict);
 +  }
 +
 +  protected void notifyGatewaySender(EnumListenerEvent operation,
 +      EntryEventImpl event) {
 +    // We don't need to clone the event for new Gateway Senders.
 +    // Preserve the bucket reference for resetting it later.
 +    LocalRegion bucketRegion = event.getRegion();
 +    try {
 +      event.setRegion(this.partitionedRegion);
 +      this.partitionedRegion.notifyGatewaySender(operation, event);
 +    }
 +    finally {
 +      // reset the event region back to bucket region.
 +      // This should work as gateway queue create GatewaySenderEvent for
 +      // queueing.
 +      event.setRegion(bucketRegion);
 +    }
 +  }
 +
 +  public void checkForPrimary() {
 +    final boolean isp = getBucketAdvisor().isPrimary();
 +    if (! isp){
 +      this.partitionedRegion.checkReadiness();
 +      checkReadiness();
 +      InternalDistributedMember primaryHolder = getBucketAdvisor().basicGetPrimaryMember();    
 +      throw new PrimaryBucketException("Bucket " + getName()
 +          + " is not primary. Current primary holder is "+primaryHolder);
 +    }
 +  }
 +  
 +  /**
 +   * Checks to make sure that this node is primary, and locks the bucket
 +   * to make sure the bucket stays the primary bucket while the write
 +   * is in progress. Any call to this method must be followed with a call
 +   * to endLocalWrite().
 +   * @param event
 +   */
 +  private boolean beginLocalWrite(EntryEventImpl event) {
 +    if(!needWriteLock(event)) {
 +      return false;
 +    }
 +
 +    if (cache.isCacheAtShutdownAll()) {
 +      throw new CacheClosedException("Cache is shutting down");
 +    }
 +
 +    Object keys[] = new Object[1];
 +    keys[0] = event.getKey();
 +    waitUntilLocked(keys); // it might wait for long time
 +
 +    boolean lockedForPrimary = false;
 +    try {
 +      doLockForPrimary(false);
 +      return lockedForPrimary = true;
 +    } finally {
 +      if (!lockedForPrimary) {
 +        removeAndNotifyKeys(keys);
 +      }
 +    }
 +  }
 +
 +  /**
 +   * lock this bucket and, if present, its colocated "parent"
 +   * @param tryLock - whether to use tryLock (true) or a blocking lock (false)
 +   * @return true if locks were obtained and are still held
 +   */
 +  public boolean doLockForPrimary(boolean tryLock) {
 +    boolean locked = lockPrimaryStateReadLock(tryLock);
 +    if(!locked) {
 +      return false;
 +    }
 +    
 +    boolean isPrimary = false;
 +    try {
 +      // Throw a PrimaryBucketException if this VM is assumed to be the
 +      // primary but isn't, preventing update and distribution
 +      checkForPrimary();
 +
 +      if (cache.isCacheAtShutdownAll()) {
 +        throw new CacheClosedException("Cache is shutting down");
 +      }
 +
 +      isPrimary = true;
 +    } finally {
 +      if(!isPrimary) {
 +        doUnlockForPrimary();
 +      }
 +    }
 +    
 +    return true;
 +  }
 +
 +  private boolean lockPrimaryStateReadLock(boolean tryLock) {
 +    Lock activeWriteLock = this.getBucketAdvisor().getActiveWriteLock();
 +    Lock parentLock = this.getBucketAdvisor().getParentActiveWriteLock();
 +    for (;;) {
 +      boolean interrupted = Thread.interrupted();
 +      try {
 +        //Get the lock. If we have to wait here, it's because
 +        //this VM is actively becoming "not primary". We don't want
 +        //to throw an exception until this VM is actually no longer
 +        //primary, so we wait here for not primary to complete. See bug #39963
 +        if (parentLock != null) {
 +          if (tryLock) {
 +            boolean locked = parentLock.tryLock();
 +            if (!locked) {
 +              return false;
 +            }
 +          } else {
 +            parentLock.lockInterruptibly();
 +          }
 +          if (tryLock) {
 +            boolean locked = activeWriteLock.tryLock();
 +            if (!locked) {
 +              parentLock.unlock();
 +              return false;
 +            }
 +          } else {
 +            activeWriteLock.lockInterruptibly();
 +          }
 +        }
 +        else {
 +          if (tryLock) {
 +            boolean locked = activeWriteLock.tryLock();
 +            if (!locked) {
 +              return false;
 +            }
 +          } else {
 +            activeWriteLock.lockInterruptibly();
 +          }
 +        }
 +        break; // success
 +      } catch (InterruptedException e) {
 +        interrupted = true;
 +        cache.getCancelCriterion().checkCancelInProgress(null);
 +        // don't throw InternalGemFireError to fix bug 40102
 +      } finally {
 +        if (interrupted) {
 +          Thread.currentThread().interrupt();
 +        }
 +      }
 +    }
 +    
 +    return true;
 +  }
 +
 +  public void doUnlockForPrimary() {
 +    Lock activeWriteLock = this.getBucketAdvisor().getActiveWriteLock();
 +    activeWriteLock.unlock();
 +    Lock parentLock = this.getBucketAdvisor().getParentActiveWriteLock();
 +    if(parentLock!= null){
 +      parentLock.unlock();
 +    }
 +  }
 +
 +  /**
 +   * Release the lock on the bucket that makes the bucket
 +   * stay the primary during a write.
 +   */
 +  private void endLocalWrite(EntryEventImpl event) {
 +    if(!needWriteLock(event)) {
 +      return;
 +    }
 +    
 +    
 +    doUnlockForPrimary();
 +
 +    Object keys[] = new Object[1];
 +    keys[0] = event.getKey();
 +    removeAndNotifyKeys(keys);
 +  }
 +
 +  protected boolean needWriteLock(EntryEventImpl event) {
 +    return !(event.isOriginRemote()
 +        || event.isNetSearch()
 +        || event.getOperation().isLocal() 
 +        || event.getOperation().isPutAll()
 +        || event.getOperation().isRemoveAll()
 +        || (event.isExpiration() && isEntryEvictDestroyEnabled() 
 +            || event.isPendingSecondaryExpireDestroy()));
 +  }
 +
 +  // this is stubbed out because distribution is done in basicPutPart2 while
 +  // the region entry is still locked
 +  @Override
 +  protected void distributeUpdate(EntryEventImpl event, long lastModified, boolean ifNew, boolean ifOld, Object expectedOldValue, boolean requireOldValue) {
 +  }
 +
 +  // Entry Invalidation rules
 +  // If this is a primary for the bucket
 +  //  1) apply op locally, aka update entry
 +  //  2) distribute op to bucket secondaries and bridge servers with synchrony on local entry
 +  //  3) cache listener with synchrony on entry
 +  //  4) update local bs, gateway
 +  // Else not a primary
 +  //  1) apply op locally
 +  //  2) update local bs, gateway
 +  @Override
 +  void basicInvalidate(EntryEventImpl event) throws EntryNotFoundException
 +  {
 +    basicInvalidate(event, isInitialized(), false);
 +  }
 +  
 +  @Override
 +  void basicInvalidate(final EntryEventImpl event, boolean invokeCallbacks,
 +      boolean forceNewEntry)
 +      throws EntryNotFoundException {
 +    // disallow local invalidation
 +    Assert.assertTrue(! event.isLocalInvalid());
 +    Assert.assertTrue(!isTX());
 +    Assert.assertTrue(event.getOperation().isDistributed());
 +
 +    beginLocalWrite(event);
 +    try {
 +      // increment the tailKey so that invalidate operations are written to HDFS
 +      if (this.partitionedRegion.hdfsStoreName != null) {
 +        /* MergeGemXDHDFSToGFE Disabled this while porting. Is this required? */
 +        //assert this.partitionedRegion.isLocalParallelWanEnabled();
 +        handleWANEvent(event);
 +      }
 +      // which performs the local op.
 +      // The ARM then calls basicInvalidatePart2 with the entry synchronized.
 +      if ( !hasSeenEvent(event) ) {
 +        if (event.getOperation().isExpiration()) { // bug 39905 - invoke listeners for expiration
 +          DistributedSystem sys =   cache.getDistributedSystem(); 
 +          EventID newID = new EventID(sys); 
 +          event.setEventId(newID);
 +          event.setInvokePRCallbacks(getBucketAdvisor().isPrimary());
 +        }
 +        boolean forceCallbacks = isEntryEvictDestroyEnabled(); 
 +        boolean done = this.entries.invalidate(event, invokeCallbacks, forceNewEntry, forceCallbacks); 
 +        ExpirationAction expirationAction = getEntryExpirationAction();
 +        if (done && !getBucketAdvisor().isPrimary() && expirationAction != null
 +            && expirationAction.isInvalidate()) {
 +          synchronized(pendingSecondaryExpires) {
 +            pendingSecondaryExpires.remove(event.getKey());
 +          }
 +        }
 +        return;
 +      }
 +      else {
 +        if (logger.isTraceEnabled(LogMarker.DM)) {
 +          logger.trace(LogMarker.DM, "LR.basicInvalidate: this cache has already seen this event {}", event);
 +        }
-         if (!event.isOriginRemote()
-             && getBucketAdvisor().isPrimary()) {
-           // This cache has processed the event, forward operation
-           // and event messages to backup buckets
-           new InvalidateOperation(event).distribute();
++        if (!getConcurrencyChecksEnabled() || event.hasValidVersionTag()) {
++          if (!event.isOriginRemote()
++              && getBucketAdvisor().isPrimary()) {
++            // This cache has processed the event, forward operation
++            // and event messages to backup buckets
++            new InvalidateOperation(event).distribute();
++          }
++          event.invokeCallbacks(this,true, false);
 +        }
-         event.invokeCallbacks(this,true, false);
 +        return;
 +      }
 +    } finally {
 +      endLocalWrite(event);
 +    }
 +  }
 +  @Override
 +  void basicInvalidatePart2(final RegionEntry re, final EntryEventImpl event,
 +      boolean clearConflict, boolean invokeCallbacks)
 +  {
 +    // Assumed this is called with the entry synchronized
 +    if (!event.isOriginRemote()) {
 +      if (event.getVersionTag() == null || event.getVersionTag().isGatewayTag()) {
 +        VersionTag v = re.generateVersionTag(null, false, this, event);
 +        if (logger.isDebugEnabled() && v != null) {
 +          logger.debug("generated version tag {} in region {}", v, this.getName());
 +        }
 +        event.setVersionTag(v);
 +      }
 +
 +      // This code assumes it is safe ignore token mode (GII in progress) 
 +      // because it assumes when the origin of the event is local,
 +      // the GII has completed and the region is initialized and open for local
 +      // ops
 +      
 +      // This code assumes that this bucket is primary
 +      // distribute op to bucket secondaries and event to other listeners
 +      InvalidateOperation op = new InvalidateOperation(event);
 +      op.distribute();
 +    }
 +    super.basicInvalidatePart2(re, event, clearConflict /*Clear conflict occurred */, invokeCallbacks);
 +  }
 +
 +  @Override
 +  void distributeInvalidate(EntryEventImpl event) {
 +  }
 +
 +  @Override
 +  protected void distributeInvalidateRegion(RegionEventImpl event) {
 +    // switch region in event so that we can have distributed region
 +    // send InvalidateRegion message.
 +    event.region = this;
 +    super.distributeInvalidateRegion(event);
 +    event.region = this.partitionedRegion;
 +  }
 +
 +  @Override
 +  protected boolean shouldDistributeInvalidateRegion(RegionEventImpl event) {
 +    return getBucketAdvisor().isPrimary();
 +  }
 +  
 +  @Override
 +  protected boolean shouldGenerateVersionTag(RegionEntry entry, EntryEventImpl event) {
 +    if (event.getOperation().isLocal()) { // bug #45402 - localDestroy generated a version tag
 +      return false;
 +    }
 +    return this.concurrencyChecksEnabled && ((event.getVersionTag() == null) || event.getVersionTag().isGatewayTag());
 +  }
 +  
 +  @Override
 +  void expireDestroy(EntryEventImpl event, boolean cacheWrite) {
 +    
 +    /* Early out before we throw a PrimaryBucketException because we're not primary */
 +    if(needWriteLock(event) && !getBucketAdvisor().isPrimary()) {
 +      return;
 +    }
 +    try {
 +      super.expireDestroy(event, cacheWrite);
 +      return;
 +    } catch(PrimaryBucketException e) {
 +      //must have concurrently removed the primary
 +      return;
 +    }
 +  }
 +  
 +  @Override
 +  void expireInvalidate(EntryEventImpl event) {
 +    if(!getBucketAdvisor().isPrimary()) {
 +      return;
 +    }
 +    try {
 +      super.expireInvalidate(event);
 +    } catch (PrimaryBucketException e) {
 +      //must have concurrently removed the primary
 +    }
 +  }
 +
 +  @Override
 +  final void performExpiryTimeout(ExpiryTask p_task) throws CacheException
 +  {
 +    ExpiryTask task = p_task;
 +    boolean isEvictDestroy = isEntryEvictDestroyEnabled();
 +    //Fix for bug 43805 - get the primary lock before
 +    //synchronizing on pendingSecondaryExpires, to match the lock
 +    //ordering in other place (like acquiredPrimaryLock)
 +    lockPrimaryStateReadLock(false);
 +    try {
 +      // Why do we care if evict destroy is configured?
 +      // See bug 41096 for the answer.
 +      if(!getBucketAdvisor().isPrimary() && !isEvictDestroy) {
 +        synchronized (this.pendingSecondaryExpires) {
 +          if (task.isPending()) {
 +            Object key = task.getKey();
 +            if (key != null) {
 +              this.pendingSecondaryExpires.put(key, task);
 +            }
 +          }
 +        }
 +      } else {
 +        super.performExpiryTimeout(task);
 +      }
 +    } finally {
 +      doUnlockForPrimary();
 +    }
 +  }
 +
 +  protected boolean isEntryEvictDestroyEnabled() {
 +    return getEvictionAttributes() != null && EvictionAction.LOCAL_DESTROY.equals(getEvictionAttributes().getAction());
 +  }
 +  
 +  protected final void processPendingSecondaryExpires()
 +  {
 +    ExpiryTask[] tasks;
 +    while (true) {
 +      // note we just keep looping until no more pendingExpires exist
 +      synchronized (this.pendingSecondaryExpires) {
 +        if (this.pendingSecondaryExpires.isEmpty()) {
 +          return;
 +        }
 +        tasks = new ExpiryTask[this.pendingSecondaryExpires.size()];
 +        tasks = this.pendingSecondaryExpires.values().toArray(tasks);
 +        this.pendingSecondaryExpires.clear();
 +      }
 +      try {
 +        if (isCacheClosing() || isClosed() || this.isDestroyed) {
 +          return;
 +        }
 +        final boolean isDebugEnabled = logger.isDebugEnabled();
 +        for (int i = 0; i < tasks.length; i++) {
 +          try {
 +            if (isDebugEnabled) {
 +              logger.debug("{} fired at {}", tasks[i], System.currentTimeMillis());
 +            }
 +            tasks[i].basicPerformTimeout(true);
 +            if (isCacheClosing() || isClosed() || isDestroyed()) {
 +              return;
 +            }
 +          }
 +          catch (EntryNotFoundException ignore) {
 +            // ignore and try the next expiry task
 +          }
 +        }
 +      }
 +      catch (RegionDestroyedException re) {
 +        // Ignore - our job is done
 +      }
 +      catch (CancelException ex) {
 +        // ignore
 +      }
 +      catch (VirtualMachineError err) {
 +        SystemFailure.initiateFailure(err);
 +        // If this ever returns, rethrow the error.  We're poisoned
 +        // now, so don't let this thread continue.
 +        throw err;
 +      }
 +      catch (Throwable ex) {
 +        // Whenever you catch Error or Throwable, you must also
 +        // catch VirtualMachineError (see above).  However, there is
 +        // _still_ a possibility that you are dealing with a cascading
 +        // error condition, so you also need to check to see if the JVM
 +        // is still usable:
 +        SystemFailure.checkFailure();
 +        logger.fatal(LocalizedMessage.create(LocalizedStrings.LocalRegion_EXCEPTION_IN_EXPIRATION_TASK), ex);
 +      }
 +    }
 +  }
 +  
 +  /**
 +   * Creates an event for the EVICT_DESTROY operation so that events will fire
 +   * for Partitioned Regions.
 +   * @param key - the key that this event is related to
 +   * @return an event for EVICT_DESTROY
 +   */
 +  @Override
 +  protected EntryEventImpl generateEvictDestroyEvent(Object key) {
 +    EntryEventImpl event = super.generateEvictDestroyEvent(key);
 +    event.setInvokePRCallbacks(true);   //see bug 40797
 +    return event;
 +  }
 +    
 +  // Entry Destruction rules
 +  // If this is a primary for the bucket
 +  //  1) apply op locally, aka destroy entry (REMOVED token)
 +  //  2) distribute op to bucket secondaries and bridge servers with synchrony on local entry
 +  //  3) cache listener with synchrony on local entry
 +  //  4) update local bs, gateway
 +  // Else not a primary
 +  //  1) apply op locally
 +  //  2) update local bs, gateway
 +  @Override
 +  protected
 +  void basicDestroy(final EntryEventImpl event,
 +                       final boolean cacheWrite,
 +                       Object expectedOldValue)
 +  throws EntryNotFoundException, CacheWriterException, TimeoutException {
 +
 +    Assert.assertTrue(!isTX());
 +    Assert.assertTrue(event.getOperation().isDistributed());
 +
 +    beginLocalWrite(event);
 +    try {
 +      // increment the tailKey for the destroy event
 +      if (this.partitionedRegion.isParallelWanEnabled()) {
 +        handleWANEvent(event);
 +      }
 +      // In GemFire EVICT_DESTROY is not distributed, so in order to remove the entry
 +      // from memory, allow the destroy to proceed. fixes #49784
 +      if (event.isLoadedFromHDFS() && !getBucketAdvisor().isPrimary()) {
 +        if (logger.isDebugEnabled()) {
 +          logger.debug("Put the destory event in HDFS queue on secondary "
 +              + "and return as event is HDFS loaded " + event);
 +        }
 +        notifyGatewaySender(EnumListenerEvent.AFTER_DESTROY, event);
 +        return;
 +      }else{
 +        if (logger.isDebugEnabled()) {
 +          logger.debug("Going ahead with the destroy on GemFire system");
 +        }
 +      }
 +      // This call should invoke AbstractRegionMap (aka ARM) destroy method
 +      // which calls the CacheWriter, then performs the local op.
 +      // The ARM then calls basicDestroyPart2 with the entry synchronized.
 +      if ( !hasSeenEvent(event) ) {
 +        if (event.getOperation().isExpiration()) { // bug 39905 - invoke listeners for expiration
 +          DistributedSystem sys =   cache.getDistributedSystem(); 
 +          if (event.getEventId() == null) { // Fix for #47388
 +            EventID newID = new EventID(sys);
 +            event.setEventId(newID);
 +          }
 +          event.setInvokePRCallbacks(getBucketAdvisor().isPrimary());
 +        }
 +        boolean done = mapDestroy(event,
 +                          cacheWrite,
 +                          false, // isEviction //merge44610: In cheetah instead of false event.getOperation().isEviction() is used. We kept the cedar change as it is.
 +                          expectedOldValue);
 +        if(done && !getBucketAdvisor().isPrimary() && isEntryExpiryPossible()) {
 +          synchronized(pendingSecondaryExpires) {
 +            pendingSecondaryExpires.remove(event.getKey());
 +          }
 +        }
 +        return;
 +      }
 +      else {
-         distributeDestroyOperation(event);
++    	if (!getConcurrencyChecksEnabled() || event.hasValidVersionTag()) {
++          distributeDestroyOperation(event);
++    	}
 +        return;
 +      }
 +    } finally {
 +      endLocalWrite(event);
 +    }
 +  }
 +  
 +  protected void distributeDestroyOperation (EntryEventImpl event) {
 +    if (logger.isTraceEnabled(LogMarker.DM)) {
 +      logger.trace(LogMarker.DM, "BR.basicDestroy: this cache has already seen this event {}", event);
 +    }
 +    if (!event.isOriginRemote()
 +        && getBucketAdvisor().isPrimary()) {
 +      if (event.isBulkOpInProgress()) {
 +        // consolidate the DestroyOperation for each entry into a RemoveAllMessage
 +        event.getRemoveAllOperation().addEntry(event, this.getId());
 +      } else {
 +        // This cache has processed the event, forward operation
 +        // and event messages to backup buckets
 +        event.setOldValueFromRegion();
 +        new DestroyOperation(event).distribute();
 +      }
 +    }
 +
 +    if (!event.getOperation().isRemoveAll()) {  // removeAll will invoke listeners later
 +      event.invokeCallbacks(this,true, false);    
 +    }
 +  }
 +
 +  @Override
 +  protected void basicDestroyBeforeRemoval(RegionEntry entry, EntryEventImpl event) {
 +    // Assumed this is called with entry synchrony
 +    if (!event.isOriginRemote()
 +        && !event.isBulkOpInProgress()
 +        && !event.getOperation().isLocal()
 +        && !Operation.EVICT_DESTROY.equals(event.getOperation())
 +        && !(event.isExpiration() && isEntryEvictDestroyEnabled())) {
 +
 +      if (event.getVersionTag() == null || event.getVersionTag().isGatewayTag()) {
 +        VersionTag v = entry.generateVersionTag(null, false, this, event);
 +        if (logger.isDebugEnabled() && v != null) {
 +          logger.debug("generated version tag {} in region {}", v, this.getName());
 +        }
 +      }
 +
 +      // This code assumes it is safe ignore token mode (GII in progress)
 +      // because it assume when the origin of the event is local,
 +      // then GII has completed (the region has been completely initialized)
 +
 +      // This code assumes that this bucket is primary
 +      new DestroyOperation(event).distribute();
 +    }
 +    super.basicDestroyBeforeRemoval(entry, event);
 +  }
 +
 +  @Override
 +  void distributeDestroy(EntryEventImpl event, Object expectedOldValue) {
 +  }
 +  
 +  
 +// impl removed - not needed for listener invocation alterations
 +//  void basicDestroyPart2(RegionEntry re, EntryEventImpl event, boolean inTokenMode, boolean invokeCallbacks)
 +
 +  @Override
 +  protected void validateArguments(Object key, Object value, Object aCallbackArgument)
 +  {
 +    Assert.assertTrue(!isTX());
 +    super.validateArguments(key, value, aCallbackArgument);
 +  }
 +
 +  public void forceSerialized(EntryEventImpl event) {
 +    event.makeSerializedNewValue();
 +//    Object obj = event.getRawNewValue();
 +//    if (obj instanceof byte[]
 +//                            || obj == null
 +//                            || obj instanceof CachedDeserializable
 +//                            || obj == NotAvailable.NOT_AVAILABLE
 +//                            || Token.isInvalidOrRemoved(obj)) {
 +//                          // already serialized
 +//                          return;
 +//                        }
 +//    throw new InternalGemFireError("event did not force serialized: " + event);
 +  }
 +  
 +  /**
 +   * This method is called when a miss from a get ends up
 +   * finding an object through a cache loader or from a server.
 +   * In that case we want to make sure that we don't move
 +   * this bucket while putting the value in the ache.
 +   * @see LocalRegion#basicPutEntry(EntryEventImpl, long) 
 +   */
 +  @Override
 +  protected RegionEntry basicPutEntry(final EntryEventImpl event,
 +      final long lastModified) throws TimeoutException,
 +      CacheWriterException {
 +    beginLocalWrite(event);
 +    try {
 +      event.setInvokePRCallbacks(true);
 +      forceSerialized(event);
 +      return super.basicPutEntry(event, lastModified);
 +    } finally {
 +      endLocalWrite(event);
 +    }
 +  }
 +
 +  @Override
 +  void basicUpdateEntryVersion(EntryEventImpl event)
 +      throws EntryNotFoundException {
 +    
 +    Assert.assertTrue(!isTX());
 +    Assert.assertTrue(event.getOperation().isDistributed());
 +
 +    beginLocalWrite(event);
 +    try {
 +      
 +      if (!hasSeenEvent(event)) {
 +        this.entries.updateEntryVersion(event);
 +      } else {
 +        if (logger.isTraceEnabled(LogMarker.DM)) {
 +          logger.trace(LogMarker.DM, "BR.basicUpdateEntryVersion: this cache has already seen this event {}", event);
 +        }
 +      }
 +      if (!event.isOriginRemote() && getBucketAdvisor().isPrimary()) {
 +        // This cache has processed the event, forward operation
 +        // and event messages to backup buckets
-         new UpdateEntryVersionOperation(event).distribute();
++    	if (!getConcurrencyChecksEnabled() || event.hasValidVersionTag()) {
++          new UpdateEntryVersionOperation(event).distribute();
++    	}
 +      }
 +      return;
 +    } finally {
 +      endLocalWrite(event);
 +    }
 +  }
 +
 +  public int getRedundancyLevel()
 +  {
 +    return this.redundancy;
 +  }
 +
 +  public boolean isPrimary() {
 +    throw new UnsupportedOperationException(LocalizedStrings.BucketRegion_THIS_SHOULD_NEVER_BE_CALLED_ON_0.toLocalizedString(getClass()));
 +  }
 +  
 +  @Override
 +  public boolean isDestroyed() {
 +    //TODO prpersist - Added this if null check for the partitioned region
 +    // because we create the disk store for a bucket *before* in the constructor
 +    // for local region, which is before this final field is assigned. This is why
 +    // we shouldn't do some much work in the constructors! This is a temporary
 +    // hack until I move must of the constructor code to region.initialize.
 +    return isBucketDestroyed()
 +        || (this.partitionedRegion != null
 +            && this.partitionedRegion.isLocallyDestroyed && !isInDestroyingThread()); 
 +  }
 +
 +  /**
 +   * Return true if this bucket has been destroyed.
 +   * Don't bother checking to see if the PR that owns this bucket was destroyed;
 +   * that has already been checked.
 +   * @since 6.0
 +   */
 +  public boolean isBucketDestroyed() {
 +    return super.isDestroyed();
 +  }
 +
 +  @Override
 +  public boolean isHDFSRegion() {
 +    return this.partitionedRegion.isHDFSRegion();
 +  }
 +
 +  @Override
 +  public boolean isHDFSReadWriteRegion() {
 +    return this.partitionedRegion.isHDFSReadWriteRegion();
 +  }
 +
 +  @Override
 +  protected boolean isHDFSWriteOnly() {
 +    return this.partitionedRegion.isHDFSWriteOnly();
 +  }
 +
 +  @Override
 +  public int sizeEstimate() {
 +    if (isHDFSReadWriteRegion()) {
 +      try {
 +        checkForPrimary();
 +        ConcurrentParallelGatewaySenderQueue q = getHDFSQueue();
 +        if (q == null) return 0;
 +        int hdfsBucketRegionSize = q.getBucketRegionQueue(
 +            partitionedRegion, getId()).size();
 +        int hoplogEstimate = (int) getHoplogOrganizer().sizeEstimate();
 +        if (logger.isDebugEnabled()) {
 +          logger.debug("for bucket " + getName() + " estimateSize returning "
 +                  + (hdfsBucketRegionSize + hoplogEstimate));
 +        }
 +        return hdfsBucketRegionSize + hoplogEstimate;
 +      } catch (ForceReattemptException e) {
 +        throw new PrimaryBucketException(e.getLocalizedMessage(), e);
 +      }
 +    }
 +    return size();
 +  }
 +
 +  @Override
 +  public void checkReadiness()
 +  {
 +    super.checkReadiness();
 +    if (isDestroyed()) {
 +      throw new RegionDestroyedException(toString(), getFullPath());
 +    }
 +  }
 +
 +  @Override
 +  public PartitionedRegion getPartitionedRegion(){
 +    return this.partitionedRegion;
 +  }
 +  
 +  /**
 +   * is the current thread involved in destroying the PR that
 +   * owns this region?
 +   */
 +  private final boolean isInDestroyingThread() {
 +    return this.partitionedRegion.locallyDestroyingThread
 +      == Thread.currentThread();
 +  }
 +//  public int getSerialNumber() {
 +//    String s = "This should never be called on " + getClass();
 +//    throw new UnsupportedOperationException(s);
 +//  }
 +
 +  @Override
 +  public void fillInProfile(Profile profile) {
 +    super.fillInProfile(profile);
 +    BucketProfile bp = (BucketProfile) profile;
 +    bp.isInitializing = this.initializationLatchAfterGetInitialImage.getCount() > 0;
 +  }
 +  
 +  /** check to see if the partitioned region is locally destroyed or closed */
 +  public boolean isPartitionedRegionOpen() {
 +    return !this.partitionedRegion.isLocallyDestroyed &&
 +      !this.partitionedRegion.isClosed && !this.partitionedRegion.isDestroyed();
 +  }
 +  
 +  /**
 +   * Horribly plagiarized from the similar method in LocalRegion
 +   * 
 +   * @param key
 +   * @param updateStats
 +   * @param clientEvent holder for client version tag
 +   * @param returnTombstones whether Token.TOMBSTONE should be returned for destroyed entries
 +   * @return serialized form if present, null if the entry is not in the cache,
 +   *         or INVALID or LOCAL_INVALID re is a miss (invalid)
 +   * @throws IOException
 +   *                 if there is a serialization problem
 +   * see LocalRegion#getDeserializedValue(RegionEntry, KeyInfo, boolean, boolean,  boolean, EntryEventImpl, boolean, boolean, boolean)
 +   */
 +  private RawValue getSerialized(Object key, boolean updateStats, boolean doNotLockEntry, EntryEventImpl clientEvent, boolean returnTombstones, boolean allowReadFromHDFS) 
 +      throws EntryNotFoundException, IOException {
 +    RegionEntry re = null;
 +    if (allowReadFromHDFS) {
 +      re = this.entries.getEntry(key);
 +    } else {
 +      re = this.entries.getOperationalEntryInVM(key);
 +    }
 +    if (re == null) {
 +      return NULLVALUE;
 +    }
 +    if (re.isTombstone() && !returnTombstones) {
 +      return NULLVALUE;
 +    }
 +    Object v = null;
 +    
 +    try {
 +      v =re.getValue(this);  // TODO OFFHEAP: todo v ends up in a RawValue. For now this can be a copy of the offheap onto the heap. But it might be easy to track lifetime of RawValue
 +      if(doNotLockEntry) {
 +        if(v == Token.NOT_AVAILABLE || v == null) {
 +          return REQUIRES_ENTRY_LOCK;
 +        }
 +      }
 +      if (clientEvent != null) {
 +        VersionStamp stamp = re.getVersionStamp();
 +        if (stamp != null) {
 +          clientEvent.setVersionTag(stamp.asVersionTag());
 +        }
 +      }
 +    }catch(DiskAccessException dae) {
 +      this.handleDiskAccessException(dae);     
 +      throw dae;
 +    }
 +    
 +    if (v == null) {
 +      return NULLVALUE;
 +    } else { 
 +      if (updateStats) {
 +        updateStatsForGet(re, true);
 +      }
 +      return new RawValue(v);
 +    }
 +  }
 +
 +  /**
 +   * Return serialized form of an entry
 +   * <p>
 +   * Horribly plagiarized from the similar method in LocalRegion
 +   * 
 +   * @param keyInfo
 +   * @param generateCallbacks
 +   * @param clientEvent holder for the entry's version information 
 +   * @param returnTombstones TODO
 +   * @return serialized (byte) form
 +   * @throws IOException if the result is not serializable
 +   * @see LocalRegion#get(Object, Object, boolean, EntryEventImpl)
 +   */
 +  public RawValue getSerialized(KeyInfo keyInfo, boolean generateCallbacks, boolean doNotLockEntry, ClientProxyMembershipID requestingClient, EntryEventImpl clientEvent, boolean returnTombstones, boolean allowReadFromHDFS) throws IOException {
 +    checkReadiness();
 +    checkForNoAccess();
 +    CachePerfStats stats = getCachePerfStats();
 +    long start = stats.startGet();
 +    
 +    boolean miss = true;
 +    try {
 +      RawValue valueBytes = NULLVALUE;
 +      boolean isCreate = false;
 +      RawValue result = getSerialized(keyInfo.getKey(), true, doNotLockEntry, clientEvent, returnTombstones, allowReadFromHDFS);
 +      isCreate = result == NULLVALUE || (result.getRawValue() == Token.TOMBSTONE && !returnTombstones);
 +      miss = (result == NULLVALUE || Token.isInvalid(result.getRawValue()));
 +      if (miss) {
 +        // if scope is local and there is no loader, then
 +        // don't go further to try and get value
 +        if (hasServerProxy() || 
 +            basicGetLoader() != null) {
 +          if(doNotLockEntry) {
 +            return REQUIRES_ENTRY_LOCK;
 +          }
 +          // TODO OFFHEAP: optimze
 +          Object value = nonTxnFindObject(keyInfo, isCreate,
 +              generateCallbacks, result.getRawValue(), true, true, requestingClient, clientEvent, false, allowReadFromHDFS);
 +          if (value != null) {
 +            result = new RawValue(value);
 +          }
 +        }
 +        else { // local scope with no loader, still might need to update stats
 +          if (isCreate) {
 +            recordMiss(null, keyInfo.getKey());
 +          }
 +        }
 +      }
 +      return result;  // changed in 7.0 to return RawValue(Token.INVALID) if the entry is invalid
 +    }
 +    finally {
 +      stats.endGet(start, miss);
 +    }
 +    
 +  } // getSerialized
 +
 +  @Override
 +  public String toString()
 +  {
 +    return new StringBuilder()
 +    .append("BucketRegion")
 +    .append("[path='").append(getFullPath())
 +    .append(";serial=").append(getSerialNumber())
 +    .append(";primary=").append(getBucketAdvisor().getProxyBucketRegion().isPrimary())
 +    .append(";indexUpdater=").append(getIndexUpdater())
 +    .append("]")
 +    .toString();
 +  }
 +
 +  @Override
 +  protected void distributedRegionCleanup(RegionEventImpl event)
 +  {
 +    // No need to close advisor, assume its already closed 
 +    // However we need to remove our listener from the advisor (see bug 43950).
 +    this.distAdvisor.removeMembershipListener(this.advisorListener);
 +  }
 +  
 +  /**
 +   * Tell the peers that this VM has destroyed the region.
 +   * 
 +   * Also marks the local disk files as to be deleted before 
 +   * sending the message to peers.
 +   * 
 +   * 
 +   * @param rebalance true if this is due to a rebalance removing the bucket
 +   */
 +  public void removeFromPeersAdvisors(boolean rebalance) {
 +    if(getPersistenceAdvisor() != null) {
 +      getPersistenceAdvisor().releaseTieLock();
 +    }
 +    
 +    DiskRegion diskRegion = getDiskRegion();
 +    
 +    //Tell our peers whether we are destroying this region
 +    //or just closing it.
 +    boolean shouldDestroy = rebalance || diskRegion == null
 +        || !diskRegion.isRecreated();
 +    Operation op = shouldDestroy ? Operation.REGION_LOCAL_DESTROY
 +        : Operation.REGION_CLOSE;
 +    
 +    RegionEventImpl event = new RegionEventImpl(this, op, null, false,
 +        getMyId(), generateEventID()/* generate EventID */);
 +    // When destroying the whole partitioned region, there's no need to
 +    // distribute the region closure/destruction, the PR RegionAdvisor.close() 
 +    // has taken care of it
 +    if (isPartitionedRegionOpen()) {
 +      
 +      
 +      //Only delete the files on the local disk if
 +      //this is a rebalance, or we are creating the bucket
 +      //for the first time
 +      if (diskRegion != null && shouldDestroy) { 
 +        diskRegion.beginDestroyDataStorage();
 +      }
 +      
 +      //Send out the destroy op to peers
 +      new DestroyRegionOperation(event, true).distribute();
 +    }
 +  }
 +
 +  @Override
 +  protected void distributeDestroyRegion(RegionEventImpl event,
 +                                         boolean notifyOfRegionDeparture) {
 +    //No need to do this when we actually destroy the region,
 +    //we already distributed this info.
 +  }
 +  
 +  EntryEventImpl createEventForPR(EntryEventImpl sourceEvent) {
 +    EntryEventImpl e2 = new EntryEventImpl(sourceEvent);
 +    boolean returned = false;
 +    try {
 +    e2.setRegion(this.partitionedRegion);
 +    if (FORCE_LOCAL_LISTENERS_INVOCATION) {
 +      e2.setInvokePRCallbacks(true);
 +    }
 +    else {
 +      e2.setInvokePRCallbacks(sourceEvent.getInvokePRCallbacks());
 +    }
 +    DistributedMember dm = this.getDistributionManager().getDistributionManagerId();
 +    e2.setOriginRemote(!e2.getDistributedMember().equals(dm));
 +    returned = true;
 +    return e2;
 +    } finally {
 +      if (!returned) {
 +        e2.release();
 +      }
 +    }
 +  }
 +
 +  
 +  
 +  @Override
 +  public void invokeTXCallbacks(
 +      final EnumListenerEvent eventType, final EntryEventImpl event,
 +      final boolean callDispatchListenerEvent)
 +  {
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("BR.invokeTXCallbacks for event {}", event);
 +    }
 +    // bucket events may make it to this point even though the bucket is still
 +    // initializing.  We can't block while initializing or a GII state flush
 +    // may hang, so we avoid notifying the bucket
 +    if (this.isInitialized()) {
 +      boolean callThem = callDispatchListenerEvent;
 +      if (event.isPossibleDuplicate()
 +          && this.eventTracker.isInitialImageProvider(event.getDistributedMember())) {
 +        callThem = false;
 +      }
 +      super.invokeTXCallbacks(eventType, event, callThem);
 +    }
 +    final EntryEventImpl prevent = createEventForPR(event);
 +    try {
 +      this.partitionedRegion.invokeTXCallbacks(eventType, prevent, this.partitionedRegion.isInitialized() ? callDispatchListenerEvent : false);
 +    } finally {
 +      prevent.release();
 +    }
 +  }
 +  
 +  
 +  /* (non-Javadoc)
 +   * @see com.gemstone.gemfire.internal.cache.LocalRegion#invokeDestroyCallbacks(com.gemstone.gemfire.internal.cache.EnumListenerEvent, com.gemstone.gemfire.internal.cache.EntryEventImpl, boolean)
 +   */
 +  @Override
 +  public void invokeDestroyCallbacks(
 +      final EnumListenerEvent eventType, final EntryEventImpl event,
 +      final boolean callDispatchListenerEvent, boolean notifyGateways)
 +  {
 +    // bucket events may make it to this point even though the bucket is still
 +    // initializing.  We can't block while initializing or a GII state flush
 +    // may hang, so we avoid notifying the bucket
 +    if (this.isInitialized()) {
 +      boolean callThem = callDispatchListenerEvent;
 +      if (event.isPossibleDuplicate()
 +          && this.eventTracker.isInitialImageProvider(event.getDistributedMember())) {
 +        callThem = false;
 +      }
 +      super.invokeDestroyCallbacks(eventType, event, callThem, notifyGateways);
 +    }
 +    final EntryEventImpl prevent = createEventForPR(event);
 +    try {
 +      this.partitionedRegion.invokeDestroyCallbacks(eventType, prevent, this.partitionedRegion.isInitialized() ? callDispatchListenerEvent : false, false);
 +    } finally {
 +      prevent.release();
 +    }
 +  }
 +
 +  /* (non-Javadoc)
 +   * @see com.gemstone.gemfire.internal.cache.LocalRegion#invokeInvalidateCallbacks(com.gemstone.gemfire.internal.cache.EnumListenerEvent, com.gemstone.gemfire.internal.cache.EntryEventImpl, boolean)
 +   */
 +  @Override
 +  public void invokeInvalidateCallbacks(
 +      final EnumListenerEvent eventType, final EntryEventImpl event,
 +      final boolean callDispatchListenerEvent)
 +  {
 +    // bucket events may make it to this point even though the bucket is still
 +    // initializing.  We can't block while initializing or a GII state flush
 +    // may hang, so we avoid notifying the bucket
 +    if (this.isInitialized()) {
 +      boolean callThem = callDispatchListenerEvent;
 +      if (event.isPossibleDuplicate()
 +          && this.eventTracker.isInitialImageProvider(event.getDistributedMember())) {
 +        callThem = false;
 +      }
 +      super.invokeInvalidateCallbacks(eventType, event, callThem);
 +    }
 +    final EntryEventImpl prevent = createEventForPR(event);
 +    try {
 +      this.partitionedRegion.invokeInvalidateCallbacks(eventType, prevent, this.partitionedRegion.isInitialized() ? callDispatchListenerEvent : false);
 +    } finally {
 +      prevent.release();
 +    }
 +  }
 +
 +  /* (non-Javadoc)
 +   * @see com.gemstone.gemfire.internal.cache.LocalRegion#invokePutCallbacks(com.gemstone.gemfire.internal.cache.EnumListenerEvent, com.gemstone.gemfire.internal.cache.EntryEventImpl, boolean)
 +   */
 +  @Override
 +  public void invokePutCallbacks(
 +      final EnumListenerEvent eventType, final EntryEventImpl event,
 +      final boolean callDispatchListenerEvent, boolean notifyGateways)
 +  {
 +    if (logger.isTraceEnabled()) {
 +      logger.trace("invoking put callbacks on bucket for event {}", event);
 +    }
 +    // bucket events may make it to this point even though the bucket is still
 +    // initializing.  We can't block while initializing or a GII state flush
 +    // may hang, so we avoid notifying the bucket
 +    if (this.isInitialized()) {
 +      boolean callThem = callDispatchListenerEvent;
 +      if (callThem && event.isPossibleDuplicate()
 +          && this.eventTracker.isInitialImageProvider(event.getDistributedMember())) {
 +        callThem = false;
 +      }
 +      super.invokePutCallbacks(eventType, event, callThem, notifyGateways);
 +    }
 +
 +    final EntryEventImpl prevent = createEventForPR(event);
 +    try {
 +      this.partitionedRegion.invokePutCallbacks(eventType, prevent,
 +              this.partitionedRegion.isInitialized() ? callDispatchListenerEvent : false, false);
 +    } finally {
 +      prevent.release();
 +    }
 +  }
 +  
 +  /**
 +   * perform adjunct messaging for the given operation and return a set of
 +   * members that should be attached to the operation's reply processor (if any)
 +   * @param event the event causing this messaging
 +   * @param cacheOpRecipients set of receiver which got cacheUpdateOperation.
 +   * @param adjunctRecipients recipients that must unconditionally get the event
 +   * @param filterRoutingInfo routing information for all members having the region
 +   * @param processor the reply processor, or null if there isn't one
 +   * @return the set of failed recipients
 +   */
 +  protected Set performAdjunctMessaging(EntryEventImpl event,
 +      Set cacheOpRecipients, Set adjunctRecipients,
 +      FilterRoutingInfo filterRoutingInfo,
 +      DirectReplyProcessor processor,
 +      boolean calculateDelta,
 +      boolean sendDeltaWithFullValue) {
 +    
 +    Set failures = Collections.EMPTY_SET;
 +    PartitionMessage msg = event.getPartitionMessage();
 +    if (calculateDelta) {
 +      setDeltaIfNeeded(event);
 +    }
 +    if (msg != null) {
 +      // The primary bucket member which is being modified remotely by a GemFire
 +      // thread via a received PartitionedMessage
 +      //Asif: Some of the adjunct recepients include those members which 
 +      // are sqlFabricHub & would need old value along with news
 +      msg = msg.getMessageForRelayToListeners(event, adjunctRecipients);
 +      msg.setSender(this.partitionedRegion.getDistributionManager()
 +          .getDistributionManagerId());
 +      msg.setSendDeltaWithFullValue(sendDeltaWithFullValue);
 +      
 +      failures = msg.relayToListeners(cacheOpRecipients, adjunctRecipients,
 +          filterRoutingInfo, event, this.partitionedRegion, processor);
 +    }
 +    else {
 +      // The primary bucket is being modified locally by an application thread locally 
 +      Operation op = event.getOperation();
 +      if (op.isCreate() || op.isUpdate()) {
 +        // note that at this point ifNew/ifOld have been used to update the
 +        // local store, and the event operation should be correct
 +        failures = PutMessage.notifyListeners(cacheOpRecipients,
 +            adjunctRecipients, filterRoutingInfo, this.partitionedRegion, 
 +            event, op.isCreate(), !op.isCreate(), processor,
 +            sendDeltaWithFullValue);
 +      }
 +      else if (op.isDestroy()) {
 +        failures = DestroyMessage.notifyListeners(cacheOpRecipients,
 +            adjunctRecipients, filterRoutingInfo,
 +            this.partitionedRegion, event, processor);
 +      }
 +      else if (op.isInvalidate()) {
 +        failures = InvalidateMessage.notifyListeners(cacheOpRecipients,
 +            adjunctRecipients, filterRoutingInfo, 
 +            this.partitionedRegion, event, processor);
 +      }
 +      else {
 +        failures = adjunctRecipients;
 +      }
 +    }
 +    return failures;
 +  }
 +
 +  private void setDeltaIfNeeded(EntryEventImpl event) {
 +    if (this.partitionedRegion.getSystem().getConfig().getDeltaPropagation()
 +        && event.getOperation().isUpdate() && event.getDeltaBytes() == null) {
 +      @Unretained Object rawNewValue = event.getRawNewValue();
 +      if (!(rawNewValue instanceof CachedDeserializable)) {
 +        return;
 +      }
 +      if (rawNewValue instanceof StoredObject && !((StoredObject) rawNewValue).isSerialized()) {
 +        // it is a byte[]; not a Delta
 +        return;
 +      }
 +      Object instance = ((CachedDeserializable)rawNewValue).getValue();
 +      if (instance instanceof com.gemstone.gemfire.Delta
 +          && ((com.gemstone.gemfire.Delta)instance).hasDelta()) {
 +        try {
 +          HeapDataOutputStream hdos = new HeapDataOutputStream(Version.CURRENT);
 +          long start = DistributionStats.getStatTime();
 +          ((com.gemstone.gemfire.Delta)instance).toDelta(hdos);
 +          event.setDeltaBytes(hdos.toByteArray());
 +          this.partitionedRegion.getCachePerfStats().endDeltaPrepared(start);
 +        }
 +        catch (RuntimeException re) {
 +          throw re;
 +        }
 +        catch (Exception e) {
 +          throw new DeltaSerializationException(
 +              LocalizedStrings.DistributionManager_CAUGHT_EXCEPTION_WHILE_SENDING_DELTA
 +                  .toLocalizedString(), e);
 +        }
 +      }
 +    }
 +  }
 +
 +  /**
 +   * create a PutAllPRMessage for notify-only and send it to all adjunct nodes. 
 +   * return a set of members that should be attached to the operation's reply processor (if any)
 +   * @param dpao DistributedPutAllOperation object for PutAllMessage
 +   * @param cacheOpRecipients set of receiver which got cacheUpdateOperation.
 +   * @param adjunctRecipients recipients that must unconditionally get the event
 +   * @param filterRoutingInfo routing information for all members having the region
 +   * @param processor the reply processor, or null if there isn't one
 +   * @return the set of failed recipients
 +   */
 +  public Set performPutAllAdjunctMessaging(DistributedPutAllOperation dpao,
 +      Set cacheOpRecipients, Set adjunctRecipients, FilterRoutingInfo filterRoutingInfo,
 +      DirectReplyProcessor processor) {
 +    // create a PutAllPRMessage out of PutAllMessage to send to adjunct nodes
 +    PutAllPRMessage prMsg = dpao.createPRMessagesNotifyOnly(getId());
 +    prMsg.initMessage(this.partitionedRegion, adjunctRecipients, true, processor);
 +    prMsg.setSender(this.partitionedRegion.getDistributionManager()
 +        .getDistributionManagerId());
 +    
 +    // find members who have clients subscribed to this event and add them
 +    // to the recipients list.  Also determine if there are any FilterInfo
 +    // routing tables for any of the receivers
 +//    boolean anyWithRouting = false;
 +    Set recipients = null;
 +    Set membersWithRouting = filterRoutingInfo.getMembers();
 +    for (Iterator it=membersWithRouting.iterator(); it.hasNext(); ) {
 +      Object mbr = it.next();
 +      if (!cacheOpRecipients.contains(mbr)) {
 +//        anyWithRouting = true;
 +        if (!adjunctRecipients.contains(mbr)) {
 +          if (recipients == null) {
 +            recipients = new HashSet();
 +            recipients.add(mbr);
 +          }
 +        }
 +      }
 +    }
 +    if (recipients == null) {
 +      recipients = adjunctRecipients;
 +    } else {
 +      recipients.addAll(adjunctRecipients);
 +    }
 +
 +//    Set failures = Collections.EMPTY_SET;
 +
 +//    if (!anyWithRouting) {
 +      Set failures = this.partitionedRegion.getDistributionManager().putOutgoing(prMsg);
 +
 +//  } else {
 +//      // Send message to each member.  We set a FilterRoutingInfo serialization
 +//      // target so that serialization of the PutAllData objects held in the
 +//      // message will only serialize the routing entry for the message recipient
 +//      Iterator rIter = recipients.iterator();
 +//      failures = new HashSet();
 +//      while (rIter.hasNext()){
 +//        InternalDistributedMember member = (InternalDistributedMember)rIter.next();
 +//        FilterRoutingInfo.setSerializationTarget(member);
 +//        try {
 +//          prMsg.resetRecipients();
 +//          prMsg.setRecipient(member);
 +//          Set fs = this.partitionedRegion.getDistributionManager().putOutgoing(prMsg);
 +//          if (fs != null && !fs.isEmpty()) {
 +//            failures.addAll(fs);
 +//          }
 +//        } finally {
 +//          FilterRoutingInfo.clearSerializationTarget();
 +//        }
 +//      }
 +//    }
 +
 +    return failures;
 +  }
 +
 +  /**
 +   * create a RemoveAllPRMessage for notify-only and send it to all adjunct nodes. 
 +   * return a set of members that should be attached to the operation's reply processor (if any)
 +   * @param op DistributedRemoveAllOperation object for RemoveAllMessage
 +   * @param cacheOpRecipients set of receiver which got cacheUpdateOperation.
 +   * @param adjunctRecipients recipients that must unconditionally get the event
 +   * @param filterRoutingInfo routing information for all members having the region
 +   * @param processor the reply processor, or null if there isn't one
 +   * @return the set of failed recipients
 +   */
 +  public Set performRemoveAllAdjunctMessaging(DistributedRemoveAllOperation op,
 +      Set cacheOpRecipients, Set adjunctRecipients, FilterRoutingInfo filterRoutingInfo,
 +      DirectReplyProcessor processor) {
 +    // create a RemoveAllPRMessage out of RemoveAllMessage to send to adjunct nodes
 +    RemoveAllPRMessage prMsg = op.createPRMessagesNotifyOnly(getId());
 +    prMsg.initMessage(this.partitionedRegion, adjunctRecipients, true, processor);
 +    prMsg.setSender(this.partitionedRegion.getDistributionManager()
 +        .getDistributionManagerId());
 +    
 +    // find members who have clients subscribed to this event and add them
 +    // to the recipients list.  Also determine if there are any FilterInfo
 +    // routing tables for any of the receivers
 +    Set recipients = null;
 +    Set membersWithRouting = filterRoutingInfo.getMembers();
 +    for (Iterator it=membersWithRouting.iterator(); it.hasNext(); ) {
 +      Object mbr = it.next();
 +      if (!cacheOpRecipients.contains(mbr)) {
 +//        anyWithRouting = true;
 +        if (!adjunctRecipients.contains(mbr)) {
 +          if (recipients == null) {
 +            recipients = new HashSet();
 +            recipients.add(mbr);
 +          }
 +        }
 +      }
 +    }
 +    if (recipients == null) {
 +      recipients = adjunctRecipients;
 +    } else {
 +      recipients.addAll(adjunctRecipients);
 +    }
 +
 +    Set failures = this.partitionedRegion.getDistributionManager().putOutgoing(prMsg);
 +    return failures;
 +  }
 +
 +  /**
 +   * return the set of recipients for adjunct operations
 +   */
 +  protected Set getAdjunctReceivers(EntryEventImpl event, Set cacheOpReceivers,
 +      Set twoMessages, FilterRoutingInfo routing) {
 +    Operation op = event.getOperation();
 +    if (op.isUpdate() || op.isCreate() || op.isDestroy() || op.isInvalidate()) {
 +      // this method can safely assume that the operation is being distributed from
 +      // the primary bucket holder to other nodes
 +      Set r = this.partitionedRegion.getRegionAdvisor()
 +        .adviseRequiresNotification(event);
 +            
 +      if (r.size() > 0) {
 +        r.removeAll(cacheOpReceivers);
 +      }
 +      
 +      // buckets that are initializing may transition out of token mode during
 +      // message transmission and need both cache-op and adjunct messages to
 +      // ensure that listeners are invoked
 +      if (twoMessages.size() > 0) {
 +        if (r.size() == 0) { // can't add to Collections.EMPTY_SET
 +          r = twoMessages;
 +        }
 +        else {
 +          r.addAll(twoMessages);
 +        }  
 +      }      
 +      if (routing != null) {
 +        // add adjunct messages to members with client routings
 +        for (InternalDistributedMember id: routing.getMembers()) {
 +          if (!cacheOpReceivers.contains(id)) {
 +            if (r.isEmpty()) {
 +              r = new HashSet();
 +            }
 +            r.add(id);
 +          }
 +        }
 +      }
 +      return r;
 +    } 
 +    else {
 +      return Collections.EMPTY_SET;
 +    }
 +  }
 +
 +  public int getId() {
 +    return getBucketAdvisor().getProxyBucketRegion().getId();
 +  }
 +
 +  @Override
 +  protected void cacheWriteBeforePut(EntryEventImpl event, Set netWriteRecipients,
 +      CacheWriter localWriter,
 +      boolean requireOldValue, Object expectedOldValue)
 +  throws CacheWriterException, TimeoutException {
 +    
 +    boolean origRemoteState = false;
 +    try {
 +      if (event.getPartitionMessage() != null || event.hasClientOrigin()) {
 +        origRemoteState=event.isOriginRemote();
 +        event.setOriginRemote(true);
 +      }
 +      event.setRegion(this.partitionedRegion);
 +      this.partitionedRegion.cacheWriteBeforePut(event, netWriteRecipients,
 +          localWriter, requireOldValue, expectedOldValue);
 +    } finally {
 +      if (event.getPartitionMessage() != null || event.hasClientOrigin()) {
 +        event.setOriginRemote(origRemoteState);
 +      }
 +      event.setRegion(this);
 +    }
 +  }
 +
 +  @Override
 +  boolean cacheWriteBeforeDestroy(EntryEventImpl event, Object expectedOldValue)
 +  throws CacheWriterException, EntryNotFoundException, TimeoutException {
 +    
 +    boolean origRemoteState = false;
 +    boolean ret = false;
 +    try {
 +      if (event.getPartitionMessage() != null || event.hasClientOrigin()) {
 +        origRemoteState=event.isOriginRemote();
 +        event.setOriginRemote(true);
 +      }
 +      event.setRegion(this.partitionedRegion);
 +      ret = this.partitionedRegion.cacheWriteBeforeDestroy(event, expectedOldValue);
 +    } finally {
 +      if (event.getPartitionMessage() != null || event.hasClientOrigin()) {
 +        event.setOriginRemote(origRemoteState);
 +      }
 +      event.setRegion(this);
 +    }
 +    return ret;
 +    //  return super.cacheWriteBeforeDestroy(event);
 +  }
 +
 +  @Override
 +  public CacheWriter basicGetWriter() {
 +    return this.partitionedRegion.basicGetWriter();
 +  }
 +   @Override
 +  void cleanUpOnIncompleteOp(EntryEventImpl event,   RegionEntry re, 
 +      boolean eventRecorded, boolean updateStats, boolean isReplace) {
 +     
 +    
 +    if(!eventRecorded || isReplace) {
 +      //No indexes updated so safe to remove.
 +      this.entries.removeEntry(event.getKey(), re, updateStats) ;      
 +    }/*else {
 +      //if event recorded is true, that means as per event tracker entry is in
 +      //system. As per sqlfabric, indexes have been updated. What is not done
 +      // is basicPutPart2( distribution etc). So we do nothing as PR's re-attempt
 +      // will do the required basicPutPart2. If we remove the entry here, than 
 +      //event tracker will not allow re insertion. So either we do nothing or
 +      //if we remove ,than we have to update sqlfindexes as well as undo recording
 +      // of event.
 +       //TODO:OQL indexes? : Hope they get updated during retry. The issue is that oql indexes
 +       // get updated after distribute , so it is entirely possible that oql index are 
 +        // not updated. what if retry fails?
 +       
 +    }*/
 +  }
 +
 +  /* (non-Javadoc)
 +   * @see com.gemstone.gemfire.internal.cache.partitioned.Bucket#getBucketOwners()
 +   * @since gemfire59poc
 +   */
 +  public Set getBucketOwners() {
 +    return getBucketAdvisor().getProxyBucketRegion().getBucketOwners();    
 +  }
 +
 +  public long getCounter() {
 +    return counter.get();
 +  }
 +
 +  public void setCounter(AtomicLong counter) {
 +    this.counter = counter;
 +  }
 +
 +  public void updateCounter(long delta) {
 +    if (delta != 0) {
 +      this.counter.getAndAdd(delta);
 +    }
 +  }
 +
 +  public void resetCounter() {
 +    if (this.counter.get() != 0) {
 +      this.counter.set(0);
 +    }
 +  }
 +
 +  public long getLimit() {
 +    if (this.limit == null) {
 +	  return 0;
 +	}
 +	return limit.get();
 +  }
 +
 +  public void setLimit(long limit) {
 +	// This method can be called before object of this class is created
 +	if (this.limit == null) {
 +	  this.limit = new AtomicLong();
 +	}
 +	this.limit.set(limit);
 +  }
 +
 +  static int calcMemSize(Object value) {
 +    if (value != null && (value instanceof GatewaySenderEventImpl)) {
 +      return ((GatewaySenderEventImpl)value).getSerializedValueSize();
 +    } 
 +    if (value == null || value instanceof Token) {
 +      return 0;
 +    }
 +    if (!(value instanceof byte[]) && !(value instanceof CachedDeserializable)
 +        && !(value instanceof com.gemstone.gemfire.Delta) && !(value instanceof Delta)) {
 +    // ezoerner:20090401 it's possible this value is a Delta
 +      throw new InternalGemFireError("DEBUG: calcMemSize: weird value (class " 
 +          + value.getClass() + "): " + value);
 +    }
 +    
 +    try {
 +      return CachedDeserializableFactory.calcMemSize(value);
 +    } catch (IllegalArgumentException e) {
 +      return 0;
 +    }
 +  }
 +
 +  boolean isDestroyingDiskRegion;
 +
 +  @Override
 +  protected void updateSizeOnClearRegion(int sizeBeforeClear) {
 +    // This method is only called when the bucket is destroyed. If we
 +    // start supporting clear of partitioned regions, this logic needs to change
 +    // we can't just set these counters to zero, because there could be
 +    // concurrent operations that are also updating these stats. For example,
 +    //a destroy could have already been applied to the map, and then updates
 +    //the stat after we reset it, making the state negative.
 +    
 +    final PartitionedRegionDataStore prDs = this.partitionedRegion.getDataStore();
 +    long oldMemValue;
 +
 +    if(this.isDestroyed || this.isDestroyingDiskRegion) {
 +      //If this region is destroyed, mark the stat as destroyed.
 +      oldMemValue = this.bytesInMemory.getAndSet(BUCKET_DESTROYED);
 +            
 +    } else if(!this.isInitialized()) {
 +      //This case is rather special. We clear the region if the GII failed.
 +      //In the case of bucket regions, we know that there will be no concurrent operations
 +      //if GII has failed, because there is not primary. So it's safe to set these
 +      //counters to 0.
 +      oldMemValue = this.bytesInMemory.getAndSet(0);
 +    }
 +    // Gemfire PRs don't support clear. allowing it via a hack for tests
 +    else if (LocalRegion.simulateClearForTests) {
 +      oldMemValue = this.bytesInMemory.getAndSet(0);
 +    }
 +    else {
 +      throw new InternalGemFireError("Trying to clear a bucket region that was not destroyed or in initialization.");
 +    }
 +    if(oldMemValue != BUCKET_DESTROYED) {
 +      this.partitionedRegion.getPrStats().incDataStoreEntryCount(-sizeBeforeClear);
 +      prDs.updateMemoryStats(-oldMemValue);
 +    }
 +  }
 +
 +  @Override
 +  public int calculateValueSize(Object val) {
 +    // Only needed by BucketRegion
 +    return calcMemSize(val);
 +  }
 +  @Override
 +  public int calculateRegionEntryValueSize(RegionEntry re) {
 +    return calcMemSize(re._getValue()); // OFFHEAP _getValue ok
 +  }
 +
 +  @Override
 +  void updateSizeOnPut(Object key, int oldSize, int newSize) {
 +    updateBucket2Size(oldSize, newSize, SizeOp.UPDATE);
 +  }
 +  
 +  @Override
 +  void updateSizeOnCreate(Object key, int newSize) {
 +    this.partitionedRegion.getPrStats().incDataStoreEntryCount(1);
 +    updateBucket2Size(0, newSize, SizeOp.CREATE);
 +  }
 +
 +  @Override
 +  void updateSizeOnRemove(Object key, int oldSize) {
 +    this.partitionedRegion.getPrStats().incDataStoreEntryCount(-1);
 +    updateBucket2Size(oldSize, 0, SizeOp.DESTROY);
 +  }
 +
 +  @Override
 +  int updateSizeOnEvict(Object key, int oldSize) {
 +    int newDiskSize = oldSize;
 +    updateBucket2Size(oldSize, newDiskSize, SizeOp.EVICT);
 +    return newDiskSize;
 +  }
 +
 +  @Override
 +  public void updateSizeOnFaultIn(Object key, int newMemSize, int oldDiskSize) {
 +    updateBucket2Size(oldDiskSize, newMemSize, SizeOp.FAULT_IN);
 +  }
 +  
 +  @Override
 +  public void initializeStats(long numEntriesInVM, long

<TRUNCATED>


[48/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/hdfs/internal/hoplog/HFileSortedOplog.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/hdfs/internal/hoplog/HFileSortedOplog.java
index 8fc9c8e,0000000..5ba20d2
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/hdfs/internal/hoplog/HFileSortedOplog.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/hdfs/internal/hoplog/HFileSortedOplog.java
@@@ -1,853 -1,0 +1,853 @@@
 +/*
 + * 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.cache.hdfs.internal.hoplog;
 +
 +import java.io.ByteArrayInputStream;
 +import java.io.Closeable;
 +import java.io.DataInput;
 +import java.io.DataInputStream;
 +import java.io.IOException;
 +import java.nio.ByteBuffer;
 +import java.util.Arrays;
 +import java.util.EnumMap;
 +import java.util.Map;
 +import java.util.Map.Entry;
 +import java.util.NoSuchElementException;
 +import java.util.concurrent.atomic.AtomicBoolean;
 +
++import com.gemstone.gemfire.internal.hll.HyperLogLog;
++import com.gemstone.gemfire.internal.hll.ICardinality;
 +import org.apache.hadoop.fs.FileSystem;
 +import org.apache.hadoop.fs.Path;
 +import org.apache.hadoop.ipc.RemoteException;
 +import org.apache.hadoop.util.ShutdownHookManager;
 +
 +import com.gemstone.gemfire.cache.CacheClosedException;
 +import com.gemstone.gemfire.cache.hdfs.HDFSIOException;
 +import com.gemstone.gemfire.cache.hdfs.internal.HDFSStoreImpl;
- import com.gemstone.gemfire.cache.hdfs.internal.cardinality.HyperLogLog;
- import com.gemstone.gemfire.cache.hdfs.internal.cardinality.ICardinality;
 +import com.gemstone.gemfire.internal.cache.persistence.soplog.DelegatingSerializedComparator;
 +import com.gemstone.gemfire.internal.cache.persistence.soplog.HFileStoreStatistics;
 +import com.gemstone.gemfire.internal.cache.persistence.soplog.SortedOplogStatistics;
 +import com.gemstone.gemfire.internal.cache.persistence.soplog.SortedOplogStatistics.ScanOperation;
 +import com.gemstone.gemfire.internal.cache.persistence.soplog.SortedReader.SerializedComparator;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.Version;
 +import com.gemstone.gemfire.internal.util.Hex;
 +import com.gemstone.gemfire.internal.util.SingletonValue;
 +import com.gemstone.gemfire.internal.util.SingletonValue.SingletonBuilder;
 +
 +import org.apache.hadoop.hbase.io.hfile.BlockCache;
 +import org.apache.hadoop.hbase.io.hfile.BlockType.BlockCategory;
 +import org.apache.hadoop.hbase.io.hfile.CacheConfig;
 +import org.apache.hadoop.hbase.io.hfile.Compression.Algorithm;
 +import org.apache.hadoop.hbase.io.hfile.HFile;
 +import org.apache.hadoop.hbase.io.hfile.HFile.Reader;
 +import org.apache.hadoop.hbase.io.hfile.HFile.Writer;
 +import org.apache.hadoop.hbase.io.hfile.HFileBlockIndex.BlockIndexReader;
 +import org.apache.hadoop.hbase.io.hfile.HFileScanner;
 +import org.apache.hadoop.hbase.regionserver.StoreFile.BloomType;
 +import org.apache.hadoop.hbase.util.BloomFilterFactory;
 +import org.apache.hadoop.hbase.util.BloomFilterWriter;
 +
 +/**
 + * Implements hfile based {@link Hoplog}
 + */
 +public final class HFileSortedOplog extends AbstractHoplog {
 +
 +//  private static final boolean CACHE_DATA_BLOCKS_ON_READ = !Boolean.getBoolean("gemfire.HFileSortedOplog.DISABLE_CACHE_ON_READ");
 +  private final CacheConfig cacheConf;
 +  private ICardinality entryCountEstimate;
 +  
 +  // a cached reader for the file
 +  private final SingletonValue<HFileReader> reader;
 +
 +  public HFileSortedOplog(HDFSStoreImpl store, Path hfilePath,
 +      BlockCache blockCache, SortedOplogStatistics stats,
 +      HFileStoreStatistics storeStats) throws IOException {
 +    super(store, hfilePath, stats);
 +    cacheConf = getCacheConfInstance(blockCache, stats, storeStats);
 +    reader = getReaderContainer();
 +  }
 +
 +  /**
 +   * THIS METHOD SHOULD BE USED FOR LONER ONLY
 +   */
 +  public static HFileSortedOplog getHoplogForLoner(FileSystem inputFS,
 +      Path hfilePath) throws IOException {
 +    return new HFileSortedOplog(inputFS, hfilePath, null, null, null);
 +  }
 +
 +  private HFileSortedOplog(FileSystem inputFS, Path hfilePath,
 +      BlockCache blockCache, SortedOplogStatistics stats,
 +      HFileStoreStatistics storeStats) throws IOException {
 +    super(inputFS, hfilePath, stats);
 +    cacheConf = getCacheConfInstance(blockCache, stats, storeStats);
 +    reader = getReaderContainer();
 +  }
 +
 +  protected CacheConfig getCacheConfInstance(BlockCache blockCache,
 +      SortedOplogStatistics stats, HFileStoreStatistics storeStats) {
 +    CacheConfig tmpConfig = null;
 +//    if (stats == null) {
 +      tmpConfig = new CacheConfig(conf);
 +//    } else {
 +//      tmpConfig = new CacheConfig(conf, CACHE_DATA_BLOCKS_ON_READ, blockCache,
 +//          HFileSortedOplogFactory.convertStatistics(stats, storeStats));
 +//    }
 +    tmpConfig.shouldCacheBlockOnRead(BlockCategory.ALL_CATEGORIES);
 +    return tmpConfig;
 +  }  
 +
 +  private SingletonValue<HFileReader> getReaderContainer() {
 +    return new SingletonValue<HFileReader>(new SingletonBuilder<HFileReader>() {
 +      @Override
 +      public HFileReader create() throws IOException {
 +        if (logger.isDebugEnabled())
 +          logger.debug("{}Creating hoplog reader", logPrefix);
 +        return new HFileReader();
 +      }
 +
 +      @Override
 +      public void postCreate() {
 +        if (readerListener != null) {
 +          readerListener.readerCreated();
 +        }
 +      }
 +      
 +      @Override
 +      public void createInProgress() {
 +      }
 +    });
 +  }
 +  
 +  @Override
 +  public HoplogReader getReader() throws IOException {
 +    return reader.get();
 +  }
 +  
 +  @Override
 +  public ICardinality getEntryCountEstimate() throws IOException {
 +    ICardinality result = entryCountEstimate;
 +    if (result == null) {
 +      HoplogReader rdr = getReader(); // keep this out of the critical section
 +      synchronized(this) {
 +        result = entryCountEstimate;
 +          if (result == null) {
 +            entryCountEstimate = result = rdr.getCardinalityEstimator();
 +          }
 +        }
 +    }
 +    return result;
 +  }
 +  
 +  @Override
 +  public HoplogWriter createWriter(int keys) throws IOException {
 +    return new HFileSortedOplogWriter(keys);
 +  }
 +
 +  @Override
 +  public boolean isClosed() {
 +    HFileReader rdr = reader.getCachedValue();
 +    return rdr == null || rdr.isClosed();
 +  }
 +  
 +  @Override
 +  public void close() throws IOException {
 +    close(true);
 +  }
 +
 +  @Override
 +  public void close(boolean clearCache) throws IOException {
 +    compareAndClose(null, clearCache);
 +  }
 +  
 +  private void compareAndClose(HFileReader hfileReader, boolean clearCache) throws IOException {
 +    HFileReader rdr ;
 +    if (hfileReader == null) {
 +      rdr = reader.clear(true);
 +    } else {
 +      boolean result = reader.clear(hfileReader, true);
 +      if (! result) {
 +        if (logger.isDebugEnabled())
 +          logger.debug("{}skipping close, provided hfileReader mismatched", logPrefix);
 +        return;
 +      } 
 +      rdr = hfileReader;
 +    }
 +    
 +    if (rdr != null) {
 +      try {
 +        rdr.close(clearCache);
 +      } finally {
 +        if (readerListener != null) {
 +          readerListener.readerClosed();
 +        }
 +      }
 +    }
 +  }
 +  
 +  @Override
 +  public String toString() {
 +    return "HFileSortedOplog[" + getFileName() + "]";
 +  }
 +
 +  private class HFileSortedOplogWriter implements HoplogWriter {
 +    private final Writer writer;
 +    private final BloomFilterWriter bfw;
 +    private final AtomicBoolean closed = new AtomicBoolean(false);
 +
 +    public HFileSortedOplogWriter(int keys) throws IOException {
 +      try {
 +        int hfileBlockSize = Integer.getInteger(
 +            HoplogConfig.HFILE_BLOCK_SIZE_CONF, (1 << 16));
 +
 +        Algorithm compress = Algorithm.valueOf(System.getProperty(HoplogConfig.COMPRESSION,
 +            HoplogConfig.COMPRESSION_DEFAULT));
 +
 +//        ByteComparator bc = new ByteComparator();
 +        writer = HFile.getWriterFactory(conf, cacheConf)
 +            .withPath(fsProvider.getFS(), path)
 +            .withBlockSize(hfileBlockSize)
 +//            .withComparator(bc)
 +            .withCompression(compress)
 +            .create();
 +//        bfw = BloomFilterFactory.createGeneralBloomAtWrite(conf, cacheConf, BloomType.ROW, keys,
 +//            writer, bc);
 +        bfw = BloomFilterFactory.createGeneralBloomAtWrite(conf, cacheConf, BloomType.ROW, keys,
 +            writer);
 +
 +        if (logger.isDebugEnabled())
 +          logger.debug("{}Created hoplog writer with compression " + compress, logPrefix);
 +      } catch (IOException e) {
 +        if (logger.isDebugEnabled())
 +          logger.debug("{}IO Error while creating writer", logPrefix);
 +        throw e;
 +      }
 +    }
 +
 +    @Override
 +    public void append(byte[] key, byte[] value) throws IOException {
 +      writer.append(key, value);
 +      bfw.add(key, 0, key.length);
 +    }
 +
 +    @Override
 +    public void append(ByteBuffer key, ByteBuffer value) throws IOException {
 +      byte[] keyBytes = byteBufferToArray(key);
 +      byte[] valueBytes = byteBufferToArray(value);
 +      writer.append(keyBytes, valueBytes);
 +      bfw.add(keyBytes, 0, keyBytes.length);
 +    }
 +
 +    @Override
 +    public void close() throws IOException {
 +      close(null);
 +    }
 +
 +    @Override
 +    public void close(EnumMap<Meta, byte[]> metadata) throws IOException {
 +      if (closed.get()) {
 +        if (logger.isDebugEnabled())
 +          logger.debug("{}Writer already closed", logPrefix);
 +        return;
 +      }
 +      
 +      bfw.compactBloom();
 +      writer.addGeneralBloomFilter(bfw);
 +
 +      // append system metadata
 +      writer.appendFileInfo(Meta.GEMFIRE_MAGIC.toBytes(), Hoplog.MAGIC);
 +      writer.appendFileInfo(Meta.SORTED_OPLOG_VERSION.toBytes(), HoplogVersion.V1.toBytes());
 +      writer.appendFileInfo(Meta.GEMFIRE_VERSION.toBytes(), Version.CURRENT.toBytes());
 +      
 +      // append comparator info
 +//      if (writer.getComparator() instanceof DelegatingSerializedComparator) {
 +//        ByteArrayOutputStream bos = new ByteArrayOutputStream();
 +//        DataOutput out = new DataOutputStream(bos);
 +//        
 +//        writeComparatorInfo(out, ((DelegatingSerializedComparator) writer.getComparator()).getComparators());
 +//        writer.appendFileInfo(Meta.COMPARATORS.toBytes(), bos.toByteArray());
 +//      }
 +      
 +      // append user metadata
 +      HyperLogLog cachedEntryCountEstimate = null;
 +      if (metadata != null) {
 +        for (Entry<Meta, byte[]> entry : metadata.entrySet()) {
 +          writer.appendFileInfo(entry.getKey().toBytes(), entry.getValue());
 +          if (Meta.LOCAL_CARDINALITY_ESTIMATE_V2.equals(entry.getKey())) {
 +             cachedEntryCountEstimate = HyperLogLog.Builder.build(entry.getValue()); 
 +          }
 +        }
 +      }
 +      
 +      writer.close();
 +      if (logger.isDebugEnabled())
 +        logger.debug("{}Completed closing writer", logPrefix);
 +      closed.set(true);
 +      // cache estimate value to avoid reads later
 +      entryCountEstimate = cachedEntryCountEstimate;
 +    }
 +
 +    @Override
 +    public void hsync() throws IOException {
 +      throw new UnsupportedOperationException("hsync is not supported for HFiles"); 
 +    }
 +
 +    @Override
 +    public long getCurrentSize() throws IOException {
 +      throw new UnsupportedOperationException("getCurrentSize is not supported for HFiles"); 
 +    }
 +    
 +//    private void writeComparatorInfo(DataOutput out, SerializedComparator[] comparators) throws IOException {
 +//      out.writeInt(comparators.length);
 +//      for (SerializedComparator sc : comparators) {
 +//        out.writeUTF(sc.getClass().getName());
 +//        if (sc instanceof DelegatingSerializedComparator) {
 +//          writeComparatorInfo(out, ((DelegatingSerializedComparator) sc).getComparators());
 +//        }
 +//      }
 +//    }
 +  }
 +  
 +  private void handleReadIOError(HFileReader hfileReader, IOException e, boolean skipFailIfSafe) {
 +    if (logger.isDebugEnabled())
 +      logger.debug("Read IO error", e);
 +    boolean safeError = ShutdownHookManager.get().isShutdownInProgress();
 +    if (safeError) {
 +      // IOException because of closed file system. This happens when member is
 +      // shutting down
 +      if (logger.isDebugEnabled())
 +        logger.debug("IO error caused by filesystem shutdown", e);
 +      throw new CacheClosedException("IO error caused by filesystem shutdown", e);
 +    } 
 +    
 +    // expose the error wrapped inside remote exception. Remote exceptions are
 +    // handled by file system client. So let the caller handle this error
 +    if (e instanceof RemoteException) {
 +      e = ((RemoteException) e).unwrapRemoteException();
 +      throw new HDFSIOException(LocalizedStrings.HOPLOG_FAILED_TO_READ_HDFS_FILE.toLocalizedString(path), e);
 +    } 
 +    
 +    FileSystem currentFs = fsProvider.checkFileSystem();
 +    if (hfileReader != null && hfileReader.previousFS != currentFs) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}Detected new FS client, closing old reader", logPrefix);
 +        if (currentFs != null) {
 +          if (logger.isDebugEnabled())
 +            logger.debug("CurrentFs:" + currentFs.getUri() + "-"
 +                + currentFs.hashCode(), logPrefix);
 +        }
 +        if (hfileReader.previousFS != null) {
 +          if (logger.isDebugEnabled())
 +            logger.debug("OldFs:" + hfileReader.previousFS.getUri() + "-"
 +                + hfileReader.previousFS.hashCode() + ", closing old reader", logPrefix);
 +        }
 +      }
 +      try {
 +        HFileSortedOplog.this.compareAndClose(hfileReader, false);
 +      } catch (Exception ex) {
 +        if (logger.isDebugEnabled())
 +          logger.debug("Failed to close reader", ex);
 +      }
 +      if (skipFailIfSafe) {
 +        if (logger.isDebugEnabled())
 +          logger.debug("Not faling after io error since FS client changed");
 +        return;
 +      }
 +    }
 +
 +    // it is not a safe error. let the caller handle it
 +    throw new HDFSIOException(LocalizedStrings.HOPLOG_FAILED_TO_READ_HDFS_FILE.toLocalizedString(path), e);
 +  }
 +
 +  class HFileReader implements HoplogReader, Closeable {
 +    private final Reader reader;
 +    private volatile BloomFilter hoplogBloom;
 +    private final AtomicBoolean closed;
 +    private final Map<byte[], byte[]> fileInfo;
 +    private final HyperLogLog estimator;
 +    private final FileSystem previousFS;
 +    
 +    public HFileReader() throws IOException {
 +      try {
 +        FileSystem fs = fsProvider.getFS();
 +        reader = HFile.createReader(fs, path, cacheConf);
 +        fileInfo = reader.loadFileInfo();
 +        closed = new AtomicBoolean(false);
 +
 +        validate();
 +        if (reader.getComparator() instanceof DelegatingSerializedComparator) {
 +          loadComparators((DelegatingSerializedComparator) reader.getComparator());
 +        }
 +
 +        // read the old HLL if it exists so that a CardinalityMergeException will trigger a Major Compaction
 +        byte[] hll = fileInfo.get(Meta.LOCAL_CARDINALITY_ESTIMATE.toBytes());
 +        if (hll != null) {
 +          entryCountEstimate = estimator = HyperLogLog.Builder.build(hll);
 +        } else if ((hll = fileInfo.get(Meta.LOCAL_CARDINALITY_ESTIMATE_V2.toBytes())) != null) {
 +          entryCountEstimate = estimator = HyperLogLog.Builder.build(hll);
 +        } else {
 +          estimator = new HyperLogLog(HdfsSortedOplogOrganizer.HLL_CONSTANT);
 +        }
 +        
 +        previousFS = fs;
 +      } catch (IOException e) {
 +        if (logger.isDebugEnabled())
 +          logger.debug("IO Error while creating reader", e);
 +        throw e;
 +      }
 +    }
 +
 +    @Override
 +    public byte[] read(byte[] key) throws IOException {
 +      IOException err = null;
 +      HFileReader delegateReader = this;
 +      for (int retry = 1; retry >= 0; retry --) {
 +        try {
 +          return delegateReader.readDelegate(key);
 +        } catch (IOException e) {
 +          err = e;
 +          handleReadIOError(delegateReader, e, retry > 0);
 +          // Current reader may have got closed in error handling. Get the new
 +          // one for retry attempt
 +          try {
 +            delegateReader = (HFileReader) HFileSortedOplog.this.getReader(); 
 +          } catch (IOException ex) {
 +            handleReadIOError(null, e, false);
 +          }
 +        }
 +      }
 +
 +      if (logger.isDebugEnabled())
 +        logger.debug("Throwing err from read delegate ", err);
 +      throw err;
 +    }
 +
 +    private byte[] readDelegate(byte[] key) throws IOException {
 +      try {
 +        if (!getBloomFilter().mightContain(key)) {
 +          // bloom filter check failed, the key is not present in this hoplog
 +          return null;
 +        }
 +      } catch (IllegalArgumentException e) {
 +        if (IOException.class.isAssignableFrom(e.getCause().getClass())) {
 +          throw (IOException) e.getCause();
 +        } else {
 +          throw e;
 +        }
 +      }
 +      
 +      byte[] valueBytes = null;
 +      ByteBuffer bb = get(key);
 +      if (bb != null) {
 +        valueBytes = new byte[bb.remaining()];
 +        bb.get(valueBytes);
 +      } else {
 +        stats.getBloom().falsePositive();
 +      }
 +      return valueBytes;
 +    }
 +
 +    @Override
 +    public ByteBuffer get(byte[] key) throws IOException {
 +      assert key != null;
 +      HFileScanner seek = reader.getScanner(false, true);
 +      if (seek.seekTo(key) == 0) {
 +        return seek.getValue();
 +      }
 +      return null;
 +    }
 +
 +    @Override
 +    public HoplogIterator<byte[], byte[]> scan(byte[] from, boolean fromInclusive, byte[] to,
 +        boolean toInclusive) throws IOException {
 +      IOException err = null;
 +      HFileReader delegateReader = this;
 +      for (int retry = 1; retry >= 0; retry --) {
 +        try {
 +          return delegateReader.scanDelegate(from, fromInclusive, to, toInclusive);
 +        } catch (IOException e) {
 +          err = e;
 +          handleReadIOError(delegateReader, e, retry > 0);
 +          // Current reader may have got closed in error handling. Get the new
 +          // one for retry attempt
 +          try {
 +            delegateReader = (HFileReader) HFileSortedOplog.this.getReader(); 
 +          } catch (IOException ex) {
 +            handleReadIOError(null, e, false);
 +          }
 +        }
 +      }
 +      if (logger.isDebugEnabled())
 +        logger.debug("Throwing err from scan delegate ", err);
 +      throw err;
 +    }
 +
 +    private HoplogIterator<byte[], byte[]> scanDelegate(byte[] from, boolean fromInclusive, byte[] to,
 +        boolean toInclusive) throws IOException {
 +      return new HFileSortedIterator(reader.getScanner(true, false), from,
 +          fromInclusive, to, toInclusive);
 +    }
 +    
 +    @Override
 +    public HoplogIterator<byte[], byte[]> scan(long offset, long length)
 +        throws IOException {
 +      /**
 +       * Identifies the first and last key to be scanned based on offset and
 +       * length. It loads hfile block index and identifies the first hfile block
 +       * starting after offset. The key of that block is from key for scanner.
 +       * Similarly it locates first block starting beyond offset + length range.
 +       * It uses key of that block as the to key for scanner
 +       */
 +
 +      // load block indexes in memory
 +      BlockIndexReader bir = reader.getDataBlockIndexReader();
 +      int blockCount = bir.getRootBlockCount();
 +      
 +      byte[] fromKey = null, toKey = null;
 +
 +      // find from key
 +      int i = 0;
 +      for (; i < blockCount; i++) {
 +        if (bir.getRootBlockOffset(i) < offset) {
 +          // hfile block has offset less than this reader's split offset. check
 +          // the next block
 +          continue;
 +        }
 +
 +        // found the first hfile block starting after offset
 +        fromKey = bir.getRootBlockKey(i);
 +        break;
 +      }
 +
 +      if (fromKey == null) {
 +        // seems no block starts after the offset. return no-op scanner
 +        return new HFileSortedIterator(null, null, false, null, false);
 +      }
 +      
 +      // find to key
 +      for (; i < blockCount; i++) {
 +        if (bir.getRootBlockOffset(i) < (offset + length)) {
 +          // this hfile block lies within the offset+lenght range. check the
 +          // next block for a higher offset
 +          continue;
 +        }
 +
 +        // found the first block starting beyong offset+length range.
 +        toKey = bir.getRootBlockKey(i);
 +        break;
 +      }
 +
 +      // from key is included in scan and to key is excluded
 +      HFileScanner scanner = reader.getScanner(true, false);
 +      return new HFileSortedIterator(scanner, fromKey, true, toKey, false);
 +    }
 +    
 +    @Override
 +    public HoplogIterator<byte[], byte[]> scan() throws IOException {
 +      return scan(null, null);
 +    }
 +
 +    public HoplogIterator<byte[], byte[]> scan(byte[] from, byte[] to)
 +        throws IOException {
 +      return scan(from, true, to, false);
 +    }
 +
 +    @Override
 +    public BloomFilter getBloomFilter() throws IOException {
 +      BloomFilter result = hoplogBloom;
 +      if (result == null) {
 +        synchronized (this) {
 +          result = hoplogBloom;
 +          if (result == null) {
 +            hoplogBloom = result = new BloomFilterImpl();
 +          }
 +        }
 +      }
 +      return result;
 +    }
 +
 +    @Override
 +    public boolean isClosed() {
 +      return closed.get();
 +    }
 +    
 +    @Override
 +    public void close() throws IOException {
 +      close(true);
 +    }
 +    
 +    public void close(boolean clearCache) throws IOException {
 +      if (closed.compareAndSet(false, true)) {
 +        if (logger.isDebugEnabled())
 +          logger.debug("{}Closing reader", logPrefix);
 +        reader.close(clearCache);
 +      }
 +    }
 +
 +    @Override
 +    public long getEntryCount() {
 +      return reader.getEntries();
 +    }
 +
 +    public ICardinality getCardinalityEstimator() {
 +      return estimator;
 +    }
 +
 +    @Override
 +    public long sizeEstimate() {
 +      return getCardinalityEstimator().cardinality();
 +    }
 +
 +    private void validate() throws IOException {
 +      // check magic
 +      byte[] magic = fileInfo.get(Meta.GEMFIRE_MAGIC.toBytes());
 +      if (!Arrays.equals(magic, MAGIC)) {
 +        throw new IOException(LocalizedStrings.Soplog_INVALID_MAGIC.toLocalizedString(Hex.toHex(magic)));
 +      }
 +      
 +      // check version compatibility
 +      byte[] ver = fileInfo.get(Meta.SORTED_OPLOG_VERSION.toBytes());
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}Hoplog version is " + Hex.toHex(ver), logPrefix);
 +      }
 +      
 +      if (!Arrays.equals(ver, HoplogVersion.V1.toBytes())) {
 +        throw new IOException(LocalizedStrings.Soplog_UNRECOGNIZED_VERSION.toLocalizedString(Hex.toHex(ver)));
 +      }
 +    }
 +    
 +    private void loadComparators(DelegatingSerializedComparator comparator) throws IOException {
 +      byte[] raw = fileInfo.get(Meta.COMPARATORS.toBytes());
 +      assert raw != null;
 +
 +      DataInput in = new DataInputStream(new ByteArrayInputStream(raw));
 +      comparator.setComparators(readComparators(in));
 +    }
 +    
 +    private SerializedComparator[] readComparators(DataInput in) throws IOException {
 +      try {
 +        SerializedComparator[] comps = new SerializedComparator[in.readInt()];
 +        assert comps.length > 0;
 +        
 +        for (int i = 0; i < comps.length; i++) {
 +          comps[i] = (SerializedComparator) Class.forName(in.readUTF()).newInstance();
 +          if (comps[i] instanceof DelegatingSerializedComparator) {
 +            ((DelegatingSerializedComparator) comps[i]).setComparators(readComparators(in));
 +          }
 +        }
 +        return comps;
 +        
 +      } catch (Exception e) {
 +        throw new IOException(e);
 +      }
 +    }
 +    
 +    class BloomFilterImpl implements BloomFilter {
 +      private final org.apache.hadoop.hbase.util.BloomFilter hfileBloom;
 +
 +      public BloomFilterImpl() throws IOException {
 +        DataInput bin = reader.getGeneralBloomFilterMetadata();
 +        // instantiate bloom filter if meta present in hfile
 +        if (bin != null) {
 +          hfileBloom = BloomFilterFactory.createFromMeta(bin, reader);
 +          if (reader.getComparator() instanceof DelegatingSerializedComparator) {
 +            loadComparators((DelegatingSerializedComparator) hfileBloom.getComparator());
 +          }
 +        } else {
 +          hfileBloom = null;
 +        }
 +      }
 +
 +      @Override
 +      public boolean mightContain(byte[] key) {
 +        assert key != null;
 +        return mightContain(key, 0, key.length);
 +      }
 +
 +      @Override
 +      public boolean mightContain(byte[] key, int keyOffset, int keyLength) {
 +        assert key != null;
 +        long start = stats.getBloom().begin();
 +        boolean found = hfileBloom == null ? true : hfileBloom.contains(key, keyOffset, keyLength, null);
 +        stats.getBloom().end(start);
 +        return found;
 +      }
 +
 +      @Override
 +      public long getBloomSize() {
 +        return hfileBloom == null ? 0 : hfileBloom.getByteSize();
 +      }
 +    }
 +
 +    // TODO change the KV types to ByteBuffer instead of byte[]
 +    public final class HFileSortedIterator implements HoplogIterator<byte[], byte[]> {
 +      private final HFileScanner scan;
 +      
 +      private final byte[] from;
 +      private final boolean fromInclusive;
 +      
 +      private final byte[] to;
 +      private final boolean toInclusive;
 +      
 +      private ByteBuffer prefetchedKey;
 +      private ByteBuffer prefetchedValue;
 +      private ByteBuffer currentKey;
 +      private ByteBuffer currentValue;
 +      
 +      // variable linked to scan stats
 +      ScanOperation scanStat;
 +      private long scanStart;
 +      
 +      public HFileSortedIterator(HFileScanner scan, byte[] from, boolean fromInclusive, byte[] to, 
 +          boolean toInclusive) throws IOException {
 +        this.scan = scan;
 +        this.from = from;
 +        this.fromInclusive = fromInclusive;
 +        this.to = to;
 +        this.toInclusive = toInclusive;
 +
 +        scanStat = (stats == null) ? new SortedOplogStatistics("", "").new ScanOperation(
 +            0, 0, 0, 0, 0, 0, 0) : stats.getScan();
 +        scanStart = scanStat.begin();
 +
 +        if (scan == null) {
 +          return;
 +        }
 +
 +        assert from == null || to == null
 +            || scan.getReader().getComparator().compare(from, to) <= 0;
 +
 +        initIterator();
 +      }
 +      
 +      /*
 +       * prefetches first key and value from the file for hasnext to work
 +       */
 +      private void initIterator() throws IOException {
 +        long startNext = scanStat.beginIteration();
 +        boolean scanSuccessful = true;
 +        if (from == null) {
 +          scanSuccessful = scan.seekTo();
 +        } else {
 +          int compare = scan.seekTo(from);
 +          if (compare == 0 && !fromInclusive || compare > 0) {
 +            // as from in exclusive and first key is same as from, skip the first key
 +            scanSuccessful = scan.next();
 +          }
 +        }
 +        
 +        populateKV(startNext, scanSuccessful);
 +      }
 +      
 +      @Override
 +      public boolean hasNext() {
 +        return prefetchedKey != null;
 +      }
 +
 +      @Override
 +      public byte[] next() throws IOException {
 +        return byteBufferToArray(nextBB());
 +      }
 +
 +      public ByteBuffer nextBB() throws IOException {
 +        long startNext = scanStat.beginIteration();
 +        if (prefetchedKey == null) {
 +          throw new NoSuchElementException();
 +        }
 +
 +        currentKey = prefetchedKey;
 +        currentValue = prefetchedValue;
 +
 +        prefetchedKey = null;
 +        prefetchedValue = null;
 +
 +        if (scan.next()) {
 +          populateKV(startNext, true);
 +        }
 +        
 +        return currentKey;
 +      }
 +
 +      
 +      private void populateKV(long nextStartTime, boolean scanSuccessful) {
 +        if (!scanSuccessful) {
 +          //end of file reached. collect stats and return
 +          scanStat.endIteration(0, nextStartTime);
 +          return;
 +        }
 +        
 +        prefetchedKey = scan.getKey();
 +        prefetchedValue = scan.getValue();
 +        
 +        if (to != null) {
 +          // TODO Optimization? Perform int comparison instead of byte[]. Identify
 +          // offset of key greater than two.
 +          int compare = -1;
 +          compare = scan.getReader().getComparator().compare
 +              (prefetchedKey.array(), prefetchedKey.arrayOffset(), prefetchedKey.remaining(), to, 0, to.length);
 +          if (compare > 0 || (compare == 0 && !toInclusive)) {
 +            prefetchedKey = null;
 +            prefetchedValue = null;
 +            return;
 +          }
 +        }
 +        
 +        // account for bytes read and time spent
 +        int byteCount = prefetchedKey.remaining() + prefetchedValue.remaining();
 +        scanStat.endIteration(byteCount, nextStartTime);
 +      }
 +      
 +
 +      @Override
 +      public byte[] getKey() {
 +        return byteBufferToArray(getKeyBB());
 +      }
 +      public ByteBuffer getKeyBB() {
 +        return currentKey;
 +      }
 +
 +      @Override
 +      public byte[] getValue() {
 +        return byteBufferToArray(getValueBB());
 +      }
 +      public ByteBuffer getValueBB() {
 +        return currentValue;
 +      }
 +
 +      @Override
 +      public void remove() {
 +        throw new UnsupportedOperationException("Cannot delete a key-value from a hfile sorted oplog");
 +      }
 +      
 +      @Override
 +      public void close() {
 +        scanStat.end(scanStart);
 +      }
 +    }
 +  }
 +  
 +  public static byte[] byteBufferToArray(ByteBuffer bb) {
 +    if (bb == null) {
 +      return null;
 +    }
 +    
 +    byte[] tmp = new byte[bb.remaining()];
 +    bb.duplicate().get(tmp);
 +    return tmp;
 +  }
 +}


[02/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/TXDistributedDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/TXDistributedDUnitTest.java
index 7a306f0,0000000..720da56
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/TXDistributedDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/TXDistributedDUnitTest.java
@@@ -1,1527 -1,0 +1,1527 @@@
 +/*
 + * 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.
 + */
 +
 +/** 
 + * Test various distributed aspects of transactions,
 + * e.g. locking/reservation symantics that do not need multiple Region
 + * configurations. For those tests see
 + * <code>MultiVMRegionTestCase</code>.
 + * 
 + *
 + * @author Mitch Thomas
 + * @since 4.0
 + * @see MultiVMRegionTestCase
 + *
 + */
 +
 +package com.gemstone.gemfire.cache30;
 +
 +
 +//import com.gemstone.gemfire.*;
 +import java.io.IOException;
 +import java.io.Serializable;
 +import java.util.HashSet;
 +import java.util.List;
 +import java.util.Properties;
 +import java.util.concurrent.CountDownLatch;
 +
 +import junit.framework.AssertionFailedError;
 +
 +import org.junit.Ignore;
 +
 +import com.gemstone.gemfire.SystemFailure;
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheLoader;
 +import com.gemstone.gemfire.cache.CacheTransactionManager;
 +import com.gemstone.gemfire.cache.CommitConflictException;
 +import com.gemstone.gemfire.cache.CommitIncompleteException;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.DiskAccessException;
 +import com.gemstone.gemfire.cache.LoaderHelper;
 +import com.gemstone.gemfire.cache.MirrorType;
 +import com.gemstone.gemfire.cache.Operation;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionAttributes;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.TimeoutException;
 +import com.gemstone.gemfire.distributed.internal.ResourceEvent;
 +import com.gemstone.gemfire.distributed.internal.ResourceEventsListener;
 +import com.gemstone.gemfire.distributed.internal.locks.DLockBatch;
 +import com.gemstone.gemfire.distributed.internal.locks.DLockService;
 +import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
 +import com.gemstone.gemfire.distributed.internal.membership.gms.MembershipManagerHelper;
 +import com.gemstone.gemfire.internal.cache.CommitReplyException;
 +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
 +import com.gemstone.gemfire.internal.cache.InternalRegionArguments;
 +import com.gemstone.gemfire.internal.cache.LocalRegion;
 +import com.gemstone.gemfire.internal.cache.RegionEntry;
 +import com.gemstone.gemfire.internal.cache.TXManagerImpl;
 +import com.gemstone.gemfire.internal.cache.TXState;
 +import com.gemstone.gemfire.internal.cache.TXStateInterface;
 +import com.gemstone.gemfire.internal.cache.TXStateProxyImpl;
 +//import com.gemstone.gemfire.internal.cache.locks.TXLockId;
 +import com.gemstone.gemfire.internal.cache.locks.TXLockBatch;
 +import com.gemstone.gemfire.internal.cache.locks.TXLockService;
 +import com.gemstone.gemfire.internal.cache.locks.TXLockServiceImpl;
 +import com.gemstone.gemfire.test.dunit.Assert;
 +import com.gemstone.gemfire.test.dunit.DistributedTestCase;
 +import com.gemstone.gemfire.test.dunit.IgnoredException;
 +import com.gemstone.gemfire.test.dunit.Invoke;
 +import com.gemstone.gemfire.test.dunit.LogWriterUtils;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.SerializableCallable;
 +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;
 +
 +public class TXDistributedDUnitTest extends CacheTestCase {
 +  public TXDistributedDUnitTest(String name) {
 +    super(name);
 +  }
 +
 +  protected RegionAttributes getRegionAttributes() {
 +    return this.getRegionAttributes(Scope.DISTRIBUTED_ACK);
 +  }
 +
 +  protected RegionAttributes getRegionAttributes(Scope scope) {
 +    AttributesFactory factory = new AttributesFactory();
 +    factory.setScope(scope);
 +    if (scope.isDistributedAck()) {
 +      factory.setEarlyAck(false);
 +    }
 +    return factory.create();
 +  }
 +
 +  /**
 +   * Test a remote grantor
 +   */
 +  public void testRemoteGrantor() throws Exception {
 +    IgnoredException.addIgnoredException("killing members ds");
 +    final CacheTransactionManager txMgr = this.getCache().getCacheTransactionManager();
 +    final String rgnName = getUniqueName();
 +    Region rgn = getCache().createRegion(rgnName, getRegionAttributes());
 +    rgn.create("key", null);
 +
 +    Invoke.invokeInEveryVM(new SerializableRunnable("testRemoteGrantor: initial configuration") {
 +        public void run() {
 +          try {
 +            Region rgn1 = getCache().createRegion(rgnName, getRegionAttributes());
 +            rgn1.put("key", "val0");
 +          } catch (CacheException e) {
 +            Assert.fail("While creating region", e);
 +          }
 +        }
 +      });
 +    
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +//    VM vm1 = host.getVM(1);
 +//    VM vm2 = host.getVM(2);
 +
 +    vm0.invoke(new SerializableRunnable("testRemoteGrantor: remote grantor init") {
 +        public void run() {
 +          try {
 +            Region rgn1 = getCache().getRegion(rgnName);
 +            final CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
 +            txMgr2.begin();
 +            rgn1.put("key", "val1");
 +            txMgr2.commit();
 +            assertNotNull(TXLockService.getDTLS());
 +            assertTrue(TXLockService.getDTLS().isLockGrantor());
 +          } catch (CacheException e) {
 +            fail("While performing first transaction");
 +          }
 +        }
 +      });
 +
 +    // fix for bug 38843 causes the DTLS to be created in every TX participant
 +    assertNotNull(TXLockService.getDTLS());
 +    assertFalse(TXLockService.getDTLS().isLockGrantor());
 +    assertEquals("val1", rgn.getEntry("key").getValue());
 +
 +    vm0.invoke(new SerializableRunnable("Disconnect from DS, remote grantor death") {
 +        public void run() {
 +            try {
 +              MembershipManagerHelper.crashDistributedSystem(getSystem());
 +            } finally {
 +                // Allow getCache() to re-establish a ds connection
 +                closeCache();
 +            }
 +        }
 +      });
 +
 +    // Make this VM the remote Grantor
 +    txMgr.begin();
 +    rgn.put("key", "val2");
 +    txMgr.commit();
 +    assertNotNull(TXLockService.getDTLS());
 +    assertTrue(TXLockService.getDTLS().isLockGrantor());
 +
 +    SerializableRunnable remoteComm = 
 +      new SerializableRunnable("testRemoteGrantor: remote grantor commit") {
 +        public void run() {
 +          try {
 +            Cache c = getCache();
 +            CacheTransactionManager txMgr2 = c.getCacheTransactionManager();
 +            Region rgn1 = c.getRegion(rgnName);
 +            if (rgn1 == null) {
 +                // This block should only execute on VM0
 +                rgn1 = c.createRegion(rgnName, getRegionAttributes());
 +            }
 +
 +            txMgr2.begin();
 +            rgn1.put("key", "val3");
 +            txMgr2.commit();
 +
 +            if (TXLockService.getDTLS() != null) {
 +              assertTrue(!TXLockService.getDTLS().isLockGrantor());
 +            }
 +          } catch (CacheException e) {
 +            Assert.fail("While creating region", e);
 +          }
 +        }
 +      };
 +    Invoke.invokeInEveryVM(remoteComm);
 +    // vm1.invoke(remoteComm);
 +    // vm2.invoke(remoteComm);
 +
 +    assertNotNull(TXLockService.getDTLS());
 +    assertTrue(TXLockService.getDTLS().isLockGrantor());
 +    assertEquals("val3", rgn.getEntry("key").getValue());
 +    rgn.destroyRegion();
 +  }
 +
 +  /**
 +   * Test the internal callbacks used for what else... testing
 +   */
 +  public void testInternalCallbacks() throws Exception {
 +    final CacheTransactionManager txMgr = this.getCache().getCacheTransactionManager();
 +    final String rgnName1 = getUniqueName() + "_1";
 +    final String rgnName2 = getUniqueName() + "_2";
 +    final String rgnName3 = getUniqueName() + "_3";
 +    Region rgn1 = getCache().createRegion(rgnName1, getRegionAttributes());
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +
 +    SerializableRunnable createRgn = 
 +      new SerializableRunnable("testInternalCallbacks: initial configuration") {
 +        public void run() {
 +          try {
 +            Region rgn1a = getCache().createRegion(rgnName1, getRegionAttributes());
 +            Region rgn2 = getCache().createRegion(rgnName2, getRegionAttributes());
 +            Region rgn3 = getCache().createRegion(rgnName3, getRegionAttributes(Scope.DISTRIBUTED_NO_ACK));
 +            rgn1a.create("key", null);
 +            rgn2.create("key", null);
 +            rgn3.create("key", null);
 +          } catch (CacheException e) {
 +            Assert.fail("While creating region", e);
 +          }
 +        }
 +      };
 +    vm0.invoke(createRgn);
 +    vm1.invoke(createRgn);
 +   
 +    // Standard commit check
 +    txMgr.begin();
 +    rgn1.put("key", "value0");
 +    txMgr.commit();
 +    SerializableRunnable checkRgn1 = 
 +      new SerializableRunnable("testInternalCallbacks: check rgn1 valus") {
 +        public void run() {
 +          Region rgn1a = getCache().getRegion(rgnName1);
 +          assertNotNull(rgn1a);
 +          assertEquals("value0", rgn1a.getEntry("key").getValue());
 +        }
 +      };
 +    vm0.invoke(checkRgn1);
 +    vm1.invoke(checkRgn1);
 +
 +    {
 +      final byte cbSensors[] = {0,0,0,0,0,0,0,0,0};
 +      txMgr.begin();
 +      ((TXStateProxyImpl)((TXManagerImpl)txMgr).getTXState()).forceLocalBootstrap();
 +      setInternalCallbacks(((TXManagerImpl)txMgr).getTXState(), cbSensors);
 +      rgn1.put("key", "value1");
 +      txMgr.commit();
 +      for(int i=cbSensors.length-3; i>=0; --i) {
 +        assertEquals("Internal callback " + i + " was not called the expected number of times!", 
 +                     (byte) 1, cbSensors[i]);
 +      }
 +      for(int i=cbSensors.length-1; i>cbSensors.length-3; --i) {
 +        assertEquals("Internal \"during\" callback " + i + " invoked an unexpected number of times!", 
 +                     (byte) 2, cbSensors[i]);
 +      }
 +    }
 +    SerializableRunnable checkRgn1Again = 
 +      new SerializableRunnable("testInternalCallbacks: validate remote values") {
 +        public void run() {
 +          Region rgn1a = getCache().getRegion(rgnName1);
 +          assertNotNull(rgn1a);
 +          assertEquals("value1", rgn1a.getEntry("key").getValue());
 +        }
 +      };
 +    vm0.invoke(checkRgn1Again);
 +    vm1.invoke(checkRgn1Again);
 +    
 +    // Try 2 regions
 +    Region rgn2 = getCache().createRegion(rgnName2, getRegionAttributes());
 +    txMgr.begin();
 +    rgn1.put("key", "value2");
 +    rgn2.put("key", "value2");
 +    txMgr.commit();
 +    SerializableRunnable checkRgn12 = 
 +      new SerializableRunnable("testInternalCallbacks: check rgn1 valus") {
 +        public void run() {
 +          Region rgn1a = getCache().getRegion(rgnName1);
 +          assertNotNull(rgn1a);
 +          assertEquals("value2", rgn1a.getEntry("key").getValue());
 +          Region rgn2a = getCache().getRegion(rgnName2);
 +          assertNotNull(rgn2a);
 +          assertEquals("value2", rgn2a.getEntry("key").getValue());
 +        }
 +      };
 +    vm0.invoke(checkRgn12);
 +    vm1.invoke(checkRgn12);
 +
 +    {
 +      final byte cbSensors[] = {0,0,0,0,0,0,0,0,0};
 +      txMgr.begin();
 +      ((TXStateProxyImpl)((TXManagerImpl)txMgr).getTXState()).forceLocalBootstrap();
 +      setInternalCallbacks(((TXManagerImpl)txMgr).getTXState(), cbSensors);
 +      rgn1.put("key", "value3");
 +      rgn2.put("key", "value3");
 +      txMgr.commit();
 +      
 +      for(int i=cbSensors.length-3; i>=0; i--) {
 +        assertEquals("Internal callback " + i + " was not called the expected number of times!", 
 +                     (byte) 1, cbSensors[i]);
 +      }
 +      for(int i=cbSensors.length-1; i> cbSensors.length-3; --i) {
 +        assertEquals("Internal \"during\" callback " + i + " invoked an unexpected number of times!", 
 +                     (byte) 2, cbSensors[i]);
 +      }
 +    }
 +    SerializableRunnable checkRgn12Again = 
 +      new SerializableRunnable("testInternalCallbacks: validate both regions remote values") {
 +        public void run() {
 +          Region rgn1a = getCache().getRegion(rgnName1);
 +          assertNotNull(rgn1a);
 +          assertEquals("value3", rgn1a.getEntry("key").getValue());
 +          Region rgn2a = getCache().getRegion(rgnName2);
 +          assertNotNull(rgn2a);
 +          assertEquals("value3", rgn2a.getEntry("key").getValue());
 +        }
 +      };
 +    vm0.invoke(checkRgn12Again);
 +    vm1.invoke(checkRgn12Again);
 +
 +    // Try a third region (D_NO_ACK)
 +    Region rgn3 = getCache().createRegion(rgnName3, getRegionAttributes(Scope.DISTRIBUTED_NO_ACK));
 +    txMgr.begin();
 +    rgn1.put("key", "value4");
 +    rgn2.put("key", "value4");
 +    rgn3.put("key", "value4");
 +    txMgr.commit();
 +    SerializableRunnable checkRgn123 = 
 +      new SerializableRunnable("testInternalCallbacks: check rgn1 valus") {
 +        public void run() {
 +          Region rgn1a = getCache().getRegion(rgnName1);
 +          assertNotNull(rgn1a);
 +          assertEquals("value4", rgn1a.getEntry("key").getValue());
 +          Region rgn2a = getCache().getRegion(rgnName2);
 +          assertNotNull(rgn2a);
 +          assertEquals("value4", rgn2a.getEntry("key").getValue());
 +          Region rgn3a = getCache().getRegion(rgnName3);
 +          assertNotNull(rgn3a);
 +          assertEquals("value4", rgn3a.getEntry("key").getValue());
 +        }
 +      };
 +    vm0.invoke(checkRgn123);
 +    vm1.invoke(checkRgn123);
 +
 +    {
 +      final byte cbSensors[] = {0,0,0,0,0,0,0,0,0};
 +      txMgr.begin();
 +      ((TXStateProxyImpl)((TXManagerImpl)txMgr).getTXState()).forceLocalBootstrap();
 +      setInternalCallbacks(((TXManagerImpl)txMgr).getTXState(), cbSensors);
 +
 +      rgn1.put("key", "value5");
 +      rgn2.put("key", "value5");
 +      rgn3.put("key", "value5");
 +      txMgr.commit();
 +      
 +      for(int i=cbSensors.length-3; i>=0; i--) {
 +        assertEquals("Internal callback " + i + " was not called the expected number of times!", 
 +                     (byte) 1, cbSensors[i]);
 +      }
 +      for(int i=cbSensors.length-1; i> cbSensors.length-3; --i) {
 +        assertEquals("Internal \"during\" callback " + i + " invoked an unexpected number of times!", 
 +                     (byte) 2, cbSensors[i]);
 +      }
 +    }
 +    SerializableRunnable checkRgn123Again = 
 +      new SerializableRunnable("testInternalCallbacks: validate both regions remote values") {
 +        public void run() {
 +          Region rgn1a = getCache().getRegion(rgnName1);
 +          assertNotNull(rgn1a);
 +          assertEquals("value5", rgn1a.getEntry("key").getValue());
 +          Region rgn2a = getCache().getRegion(rgnName2);
 +          assertNotNull(rgn2a);
 +          assertEquals("value5", rgn2a.getEntry("key").getValue());
 +          Region rgn3a = getCache().getRegion(rgnName3);
 +          assertNotNull(rgn3a);
 +          assertEquals("value5", rgn3a.getEntry("key").getValue());
 +        }
 +      };
 +    vm0.invoke(checkRgn123Again);
 +    vm1.invoke(checkRgn123Again);
 +    
 +    rgn1.destroyRegion();
 +    rgn2.destroyRegion();
 +  }
 +  static final void setInternalCallbacks(TXStateInterface txp, final byte[] cbSensors) {
 +    ((TXStateProxyImpl)txp).forceLocalBootstrap();
 +    TXState tx = (TXState)((TXStateProxyImpl)txp).getRealDeal(null,null);
 +    assertEquals(9, cbSensors.length);
 +    tx.setAfterReservation(new Runnable() {
 +        public void run() {
 +          cbSensors[0]++;
 +        }
 +      });
 +    tx.setAfterConflictCheck(new Runnable() {
 +        public void run() {
 +          cbSensors[1]++;
 +        }
 +      });
 +    tx.setAfterApplyChanges(new Runnable() {
 +        public void run() {
 +          cbSensors[2]++;            
 +        }
 +      });
 +    tx.setAfterReleaseLocalLocks(new Runnable() {
 +        public void run() {
 +          cbSensors[3]++;            
 +        }
 +      });
 +    tx.setAfterIndividualSend(new Runnable() {
 +        public void run() {
 +          cbSensors[4]++;
 +        }
 +      });
 +    tx.setAfterIndividualCommitProcess(new Runnable() {
 +        public void run() {
 +          cbSensors[5]++;
 +        }
 +      });
 +    tx.setAfterSend(new Runnable() {
 +        public void run() {
 +          cbSensors[6]++;
 +        }
 +      });
 +    tx.setDuringIndividualSend(new Runnable() {
 +        public void run() {
 +          cbSensors[7]++;            
 +        }
 +      });
 +    tx.setDuringIndividualCommitProcess(new Runnable() {
 +        public void run() {
 +          cbSensors[8]++;
 +        }
 +      });
 +  }
 +
 +  /** 
 +   * Test distributed ack transactions that consist only of 
 +   * data from loaded values
 +   */
 +  public void testDACKLoadedMessage() throws Exception {
 +    final CacheTransactionManager txMgr = this.getCache().getCacheTransactionManager();
 +    final String rgnName = getUniqueName();
 +    AttributesFactory factory = new AttributesFactory();
 +    factory.setScope(Scope.DISTRIBUTED_ACK);
 +    factory.setEarlyAck(false);
 +    factory.setCacheLoader(new CacheLoader() {
 +        public Object load(LoaderHelper helper) {
 +          return "val" + helper.getArgument();
 +        }
 +        public void close() {}
 +      });
 +    Region rgn = getCache().createRegion(rgnName, factory.create());
 +    
 +    Invoke.invokeInEveryVM(new SerializableRunnable("testDACKLoadedMessage: intial configuration") {
 +        public void run() {
 +          try {
 +            AttributesFactory factory2 = new AttributesFactory();
 +            factory2.setScope(Scope.DISTRIBUTED_ACK);
 +            factory2.setEarlyAck(false);
 +            // factory.setDataPolicy(DataPolicy.REPLICATE);
 +            factory2.setMirrorType(MirrorType.KEYS);
 +            getCache().createRegion(rgnName, factory2.create());
 +          } catch (CacheException e) {
 +            Assert.fail("While creating region", e);
 +          }
 +        }
 +      });
 +
 +    // Confirm the standard case
 +    txMgr.begin();
 +    rgn.put("key1", "val1");
 +    txMgr.commit();
 +    assertEquals("val1", rgn.getEntry("key1").getValue());
 +
 +    Invoke.invokeInEveryVM(new SerializableRunnable("testDACKLoadedMessage: confirm standard case") {
 +        public void run() {
 +          Region rgn1 = getCache().getRegion(rgnName);
 +          assertEquals("val1", rgn1.getEntry("key1").getValue());
 +        }
 +      });
 +
 +    // Confirm loaded value case
 +    txMgr.begin();
 +    rgn.get("key2", new Integer(2));
 +    txMgr.commit();
 +    assertEquals("val2", rgn.getEntry("key2").getValue());
 +    
 +    Invoke.invokeInEveryVM(new SerializableRunnable("testDACKLoadedMessage: confirm standard case") {
 +        public void run() {
 +          Region rgn1 = getCache().getRegion(rgnName);
 +          assertEquals("val2", rgn1.getEntry("key2").getValue());
 +        }
 +      });
 +
 +    // This should use the ack w/ the lockid
 +    txMgr.begin();
 +    rgn.put("key3", "val3");
 +    rgn.get("key4", new Integer(4));
 +    txMgr.commit();
 +
 +    Invoke.invokeInEveryVM(new SerializableRunnable("testDACKLoadedMessage: confirm standard case") {
 +        public void run() {
 +          Region rgn1 = getCache().getRegion(rgnName);
 +          assertEquals("val3", rgn1.getEntry("key3").getValue());
 +          assertEquals("val4", rgn1.getEntry("key4").getValue());
 +        }
 +      });
 +
 +  }
 +  
 +  @Override
 +  public Properties getDistributedSystemProperties() {
 +    Properties p = super.getDistributedSystemProperties();
 +    p.put("log-level", LogWriterUtils.getDUnitLogLevel());
 +    return p;
 +  }
 +
 +  public void testHighAvailabilityFeatures() throws Exception {
 +    IgnoredException.addIgnoredException("DistributedSystemDisconnectedException");
 +//    final CacheTransactionManager txMgr = this.getCache().getCacheTransactionManager();
 +//    final TXManagerImpl txMgrImpl = (TXManagerImpl) txMgr;
 +    final String rgnName = getUniqueName();
 +    AttributesFactory factory = new AttributesFactory();
 +    factory.setScope(Scope.DISTRIBUTED_ACK);
 +    factory.setEarlyAck(false);
 +    Region rgn = getCache().createRegion(rgnName, factory.create());
 +    Invoke.invokeInEveryVM(new SerializableRunnable("testHighAvailabilityFeatures: intial region configuration") {
 +        public void run() {
 +          try {
 +            AttributesFactory factory2 = new AttributesFactory();
 +            factory2.setScope(Scope.DISTRIBUTED_ACK);
 +            factory2.setEarlyAck(false);
 +            factory2.setDataPolicy(DataPolicy.REPLICATE);
 +            getCache().createRegion(rgnName, factory2.create());
 +          } catch (CacheException e) {
 +            Assert.fail("While creating region", e);
 +          }
 +        }
 +      });
 +
 +    // create entries
 +    rgn.put("key0", "val0_0");
 +    rgn.put("key1", "val1_0");
 +
 +    Host host = Host.getHost(0);
 +    // This test assumes that there are at least three VMs; the origin and two recipients
 +    assertTrue(host.getVMCount() >= 3);
 +    final VM originVM = host.getVM(0);
 +
 +    // Test that there is no commit after a partial commit message
 +    // send (only sent to a minority of the recipients)
 +    originVM.invoke(new SerializableRunnable("Flakey DuringIndividualSend Transaction") {
 +        public void run() {
 +          final Region rgn1 = getCache().getRegion(rgnName);
 +          assertNotNull(rgn1);
 +          try {
 +            final CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
 +            final CacheTransactionManager txMgrImpl = txMgr2;
 +            
 +            txMgr2.begin();
 +            // 1. setup an internal callback on originVM that will call
 +            // disconnectFromDS() on the 2nd duringIndividualSend
 +            // call.
 +            ((TXStateProxyImpl)((TXManagerImpl)txMgrImpl).getTXState()).forceLocalBootstrap();
 +            TXState txState = (TXState)((TXStateProxyImpl)((TXManagerImpl)txMgrImpl).getTXState()).getRealDeal(null,null);
 +            txState.setDuringIndividualSend(new Runnable() {
 +                private int numCalled = 0;
 +                public synchronized void run() {
 +                  ++numCalled;
 +                  rgn1.getCache().getLogger().info("setDuringIndividualSend Runnable called " + numCalled + " times");
 +                  if (numCalled > 1) {
 +                    MembershipManagerHelper.crashDistributedSystem(getSystem());
 +                  }
 +                }
 +              });
 +            rgn1.put("key0", "val0_1");
 +            rgn1.put("key1", "val1_1");
 +            // 2. commit a transaction in originVM, it will disconnect from the DS
 +            txMgr2.commit();
 +          } 
 +          catch (VirtualMachineError e) {
 +            SystemFailure.initiateFailure(e);
 +            throw e;
 +          }
 +          catch (Throwable e) {
 +            rgn1.getCache().getLogger().warning("Ignoring Exception", e);
 +          } 
 +          finally {
 +            // Allow this VM to re-connect to the DS upon getCache() call
 +            closeCache();
 +          }
 +        }
 +      });
 +    // 3. verify on all VMs that the transaction was not committed
 +    final SerializableRunnable noChangeValidator = 
 +      new SerializableRunnable("testHighAvailabilityFeatures: validate no change in Region") {
 +        public void run() {
 +          Region rgn1 = getCache().getRegion(rgnName);
 +          if (rgn1 == null) {
 +            // Expect a null region from originVM
 +            try {
 +              AttributesFactory factory2 = new AttributesFactory();
 +              factory2.setScope(Scope.DISTRIBUTED_ACK);
 +              factory2.setEarlyAck(false);
 +              factory2.setDataPolicy(DataPolicy.REPLICATE);
 +              rgn1 = getCache().createRegion(rgnName, factory2.create());
 +            } catch (CacheException e) {
 +              Assert.fail("While creating region", e);
 +            }
 +          }
 +          Region.Entry re = rgn1.getEntry("key0");
 +          assertNotNull(re);
 +          assertEquals("val0_0", re.getValue());
 +          re = rgn1.getEntry("key1");
 +          assertNotNull(re);
 +          assertEquals("val1_0", re.getValue());
 +        }
 +      };
 +    Invoke.invokeInEveryVM(noChangeValidator);
 +
 +    // Test that there is no commit after sending to all recipients
 +    // but prior to sending the "commit process" message
 +    originVM.invoke(new SerializableRunnable("Flakey AfterIndividualSend Transaction") {
 +        public void run() {
 +          final Region rgn1 = getCache().getRegion(rgnName);
 +          assertNotNull(rgn1);
 +          try {
 +            final CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
 +            final CacheTransactionManager txMgrImpl = txMgr2;
 +            
 +            txMgr2.begin();
 +            // 1. setup an internal callback on originVM that will call
 +            // disconnectFromDS() on AfterIndividualSend
 +            ((TXStateProxyImpl)((TXManagerImpl)txMgrImpl).getTXState()).forceLocalBootstrap();
 +            TXState txState = (TXState)((TXStateProxyImpl)((TXManagerImpl)txMgrImpl).getTXState()).getRealDeal(null,null);
 +            txState.setAfterIndividualSend(new Runnable() {
 +                public synchronized void run() {
 +                  MembershipManagerHelper.crashDistributedSystem(getSystem());
 +                }
 +              });
 +            rgn1.put("key0", "val0_2");
 +            rgn1.put("key1", "val1_2");
 +            // 2. commit a transaction in originVM, it will disconnect from the DS
 +            txMgr2.commit();
 +          } 
 +          catch (VirtualMachineError e) {
 +            SystemFailure.initiateFailure(e);
 +            throw e;
 +          }
 +          catch (Throwable e) {
 +            rgn1.getCache().getLogger().warning("Ignoring Exception", e);
 +          } 
 +          finally {
 +            // Allow this VM to re-connect to the DS upon getCache() call
 +            closeCache();
 +          }
 +        }
 +      });
 +    // 3. verify on all VMs, including the origin, that the transaction was not committed
 +    Invoke.invokeInEveryVM(noChangeValidator);
 +
 +    // Test commit success upon a single commit process message received.
 +    originVM.invoke(new SerializableRunnable("Flakey DuringIndividualCommitProcess Transaction") {
 +        public void run() {
 +          final Region rgn1 = getCache().getRegion(rgnName);
 +          assertNotNull(rgn1);
 +          try {
 +            final CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
 +            final CacheTransactionManager txMgrImpl = txMgr2;
 +            
 +            txMgr2.begin();
 +
 +            ((TXStateProxyImpl)((TXManagerImpl)txMgrImpl).getTXState()).forceLocalBootstrap();
 +            TXState txState = (TXState)((TXStateProxyImpl)((TXManagerImpl)txMgrImpl).getTXState()).getRealDeal(null,null);
 +            // 1. setup an internal callback on originVM that will call
 +            // disconnectFromDS() on the 2nd internalDuringIndividualCommitProcess
 +            // call.
 +            txState.setDuringIndividualCommitProcess(new Runnable() {
 +                private int numCalled = 0;
 +                public synchronized void run() {
 +                  ++numCalled;
 +                  rgn1.getCache().getLogger().info("setDuringIndividualCommitProcess Runnable called " + numCalled + " times");
 +                  if (numCalled > 1) {
 +                    MembershipManagerHelper.crashDistributedSystem(getSystem());
 +                  }
 +                }
 +              });
 +            rgn1.put("key0", "val0_3");
 +            rgn1.put("key1", "val1_3");
 +            // 2. commit a transaction in originVM, it will disconnect from the DS
 +            txMgr2.commit();
 +          } 
 +          catch (VirtualMachineError e) {
 +            SystemFailure.initiateFailure(e);
 +            throw e;
 +          }
 +          catch (Throwable e) {
 +            rgn1.getCache().getLogger().warning("Ignoring Exception", e);
 +          } 
 +          finally {
 +            // Allow this VM to re-connect to the DS upon getCache() call
 +            closeCache();
 +          }
 +        }
 +      });
 +    // 3. verify on all VMs that the transaction was committed (including the orgin, due to GII)
 +    SerializableRunnable nonSoloChangeValidator1 = 
 +      new SerializableRunnable("testHighAvailabilityFeatures: validate v1 non-solo Region changes") {
 +        public void run() {
 +          Region rgn1 = getCache().getRegion(rgnName);
 +          if (rgn1 == null) {
 +            // Expect a null region from originVM
 +            try {
 +              AttributesFactory factory2 = new AttributesFactory();
 +              factory2.setScope(Scope.DISTRIBUTED_ACK);
 +              factory2.setEarlyAck(false);
 +              factory2.setDataPolicy(DataPolicy.REPLICATE);
 +              rgn1 = getCache().createRegion(rgnName, factory2.create());
 +            } catch (CacheException e) {
 +              Assert.fail("While creating region", e);
 +            }
 +          }
 +          long giveUp = System.currentTimeMillis() + 10000;
 +          while (giveUp > System.currentTimeMillis()) {
 +            try {
 +              Region.Entry re = rgn1.getEntry("key0");
 +              assertNotNull(re);
 +              assertEquals("val0_3", re.getValue());
 +              re = rgn1.getEntry("key1");
 +              assertNotNull(re);
 +              assertEquals("val1_3", re.getValue());
 +              break;
 +            } catch (AssertionFailedError e) {
 +              if (giveUp > System.currentTimeMillis()) {
 +                throw e;
 +              }
 +            }
 +          }
 +        }
 +      };
 +    Invoke.invokeInEveryVM(nonSoloChangeValidator1);
 +
 +    // Verify successful solo region commit after duringIndividualSend
 +    // (same as afterIndividualSend).
 +    // Create a region that only exists on the origin and another VM
 +    final String soloRegionName = getUniqueName() + "_solo";
 +    SerializableRunnable createSoloRegion = 
 +      new SerializableRunnable("testHighAvailabilityFeatures: solo region configuration") {
 +        public void run() {
 +          try {
 +            AttributesFactory factory2 = new AttributesFactory();
 +            factory2.setScope(Scope.DISTRIBUTED_ACK);
 +            factory2.setEarlyAck(false);
 +            factory2.setDataPolicy(DataPolicy.REPLICATE);
 +            Region rgn1 = getCache().createRegion(soloRegionName, factory2.create());
 +            rgn1.put("soloKey0", "soloVal0_0");
 +            rgn1.put("soloKey1", "soloVal1_0");
 +          } catch (CacheException e) {
 +            Assert.fail("While creating region", e);
 +          }
 +        }
 +      };
 +    final VM soloRegionVM = host.getVM(1);
 +    originVM.invoke(createSoloRegion);
 +    soloRegionVM.invoke(createSoloRegion);
 +    originVM.invoke(new SerializableRunnable("Flakey solo region DuringIndividualSend Transaction") {
 +        public void run() {
 +          final Region soloRgn = getCache().getRegion(soloRegionName);
 +          assertNotNull(soloRgn);
 +          try {
 +            final CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
 +            final CacheTransactionManager txMgrImpl = txMgr2;
 +            
 +            txMgr2.begin();
 +            // 1. setup an internal callback on originVM that will call
 +            // disconnectFromDS() on the 2nd duringIndividualSend
 +            // call.
 +            ((TXStateProxyImpl)((TXManagerImpl)txMgrImpl).getTXState()).forceLocalBootstrap();
 +            TXState txState = (TXState)((TXStateProxyImpl)((TXManagerImpl)txMgrImpl).getTXState()).getRealDeal(null,null);
 +            txState.setDuringIndividualSend(new Runnable() {
 +                private int numCalled = 0;
 +                public synchronized void run() {
 +                  ++numCalled;
 +                  soloRgn.getCache().getLogger().info("setDuringIndividualSend Runnable called " + numCalled + " times");
 +                  if (numCalled > 1) {
 +                    MembershipManagerHelper.crashDistributedSystem(getSystem());
 +                  }
 +                }
 +              });
 +            soloRgn.put("soloKey0", "soloVal0_1");
 +            soloRgn.put("soloKey1", "soloVal1_1");
 +            // 2. commit a transaction in originVM, it will disconnect from the DS
 +            txMgr2.commit();
 +          } 
 +          catch (VirtualMachineError e) {
 +            SystemFailure.initiateFailure(e);
 +            throw e;
 +          }
 +          catch (Throwable e) {
 +            soloRgn.getCache().getLogger().warning("Ignoring Exception", e);
 +          } 
 +          finally {
 +            // Allow this VM to re-connect to the DS upon getCache() call
 +            closeCache();
 +          }
 +        }
 +      });
 +    // 3. verify on the soloRegionVM that the transaction was committed
 +    final SerializableRunnable soloRegionCommitValidator1 = 
 +      new SerializableRunnable("testHighAvailabilityFeatures: validate successful v1 commit in solo Region") {
 +        public void run() {
 +          Region soloRgn = getCache().getRegion(soloRegionName);
 +          if (soloRgn == null) {
 +            // Expect a null region from originVM
 +            try {
 +              AttributesFactory factory2 = new AttributesFactory();         
 +              factory2.setScope(Scope.DISTRIBUTED_ACK);
 +              factory2.setEarlyAck(false);
 +              factory2.setDataPolicy(DataPolicy.REPLICATE);
 +              soloRgn = getCache().createRegion(soloRegionName, factory2.create());
 +            } catch (CacheException e) {
 +              Assert.fail("While creating region ", e);
 +            }
 +          }
 +          Region.Entry re = soloRgn.getEntry("soloKey0");
 +          assertNotNull(re);
 +          assertEquals("soloVal0_1", re.getValue());
 +          re = soloRgn.getEntry("soloKey1");
 +          assertNotNull(re);
 +          assertEquals("soloVal1_1", re.getValue());
 +        }
 +      };
 +    originVM.invoke(soloRegionCommitValidator1);
 +    soloRegionVM.invoke(soloRegionCommitValidator1);
 +    // verify no change in nonSolo region, re-establish region in originVM
 +    Invoke.invokeInEveryVM(nonSoloChangeValidator1);
 +
 +    // Verify no commit for failed send (afterIndividualSend) for solo
 +    // Region combined with non-solo Region
 +    originVM.invoke(new SerializableRunnable("Flakey mixed (solo+non-solo) region DuringIndividualSend Transaction") {
 +        public void run() {
 +          final Region rgn1 = getCache().getRegion(rgnName);
 +          assertNotNull(rgn1);
 +          final Region soloRgn = getCache().getRegion(soloRegionName);
 +          assertNotNull(soloRgn);
 +          try {
 +            final CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
 +            final CacheTransactionManager txMgrImpl = txMgr2;
 +            
 +            txMgr2.begin();
 +            // 1. setup an internal callback on originVM that will call
 +            // disconnectFromDS() on the afterIndividualSend
 +            // call.
 +            ((TXStateProxyImpl)((TXManagerImpl)txMgrImpl).getTXState()).forceLocalBootstrap();
 +            TXState txState = (TXState)((TXStateProxyImpl)((TXManagerImpl)txMgrImpl).getTXState()).getRealDeal(null,null);
 +            txState.setAfterIndividualSend(new Runnable() {
 +                public synchronized void run() {
 +                  MembershipManagerHelper.crashDistributedSystem(getSystem());
 +                }
 +              });
 +
 +            rgn1.put("key0", "val0_4");
 +            rgn1.put("key1", "val1_4");
 +            soloRgn.put("soloKey0", "soloVal0_2");
 +            soloRgn.put("soloKey1", "soloVal1_2");
 +
 +            // 2. commit a transaction in originVM, it will disconnect from the DS
 +            txMgr2.commit();
 +          } 
 +          catch (VirtualMachineError e) {
 +            SystemFailure.initiateFailure(e);
 +            throw e;
 +          }
 +          catch (Throwable e) {
 +            rgn1.getCache().getLogger().warning("Ignoring Exception", e);
 +          } 
 +          finally {
 +            // Allow this VM to re-connect to the DS upon getCache() call
 +            closeCache();
 +          }
 +        }
 +      });
 +    // Origin and Solo Region VM should be the same as last validation
 +    originVM.invoke(soloRegionCommitValidator1);
 +    soloRegionVM.invoke(soloRegionCommitValidator1);
 +    Invoke.invokeInEveryVM(nonSoloChangeValidator1);
 +
 +    // Verify commit after sending a single
 +    // (duringIndividualCommitProcess) commit process for solo Region
 +    // combined with non-solo Region
 +    originVM.invoke(new SerializableRunnable("Flakey mixed (solo+non-solo) region DuringIndividualCommitProcess Transaction") {
 +        public void run() {
 +          final Region rgn1 = getCache().getRegion(rgnName);
 +          assertNotNull(rgn1);
 +          final Region soloRgn = getCache().getRegion(soloRegionName);
 +          assertNotNull(soloRgn);
 +          try {
 +            final CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
 +            final CacheTransactionManager txMgrImpl = txMgr2;
 +            
 +            txMgr2.begin();
 +            // 1. setup an internal callback on originVM that will call
 +            // disconnectFromDS() on the afterIndividualSend
 +            // call.
 +            ((TXStateProxyImpl)((TXManagerImpl)txMgrImpl).getTXState()).forceLocalBootstrap();
 +            TXState txState = (TXState)((TXStateProxyImpl)((TXManagerImpl)txMgrImpl).getTXState()).getRealDeal(null,null);
 +            txState.setAfterIndividualSend(new Runnable() {
 +                private int numCalled = 0;
 +                public synchronized void run() {
 +                  ++numCalled;
 +                  rgn1.getCache().getLogger().info("setDuringIndividualCommitProcess Runnable called " + numCalled + " times");
 +                  if (numCalled > 1) {
 +                    MembershipManagerHelper.crashDistributedSystem(getSystem());
 +                  }
 +                }
 +              });
 +
 +            rgn1.put("key0", "val0_5");
 +            rgn1.put("key1", "val1_5");
 +            soloRgn.put("soloKey0", "soloVal0_3");
 +            soloRgn.put("soloKey1", "soloVal1_3");
 +
 +            // 2. commit a transaction in originVM, it will disconnect from the DS
 +            txMgr2.commit();
 +          } 
 +          catch (VirtualMachineError e) {
 +            SystemFailure.initiateFailure(e);
 +            throw e;
 +          }
 +          catch (Throwable e) {
 +            rgn1.getCache().getLogger().warning("Ignoring Exception", e);
 +          } 
 +          finally {
 +            // Allow this VM to re-connect to the DS upon getCache() call
 +            closeCache();
 +          }
 +        }
 +      });
 +    final SerializableRunnable soloRegionCommitValidator2 = 
 +      new SerializableRunnable("testHighAvailabilityFeatures: validate successful v2 commit in solo Region") {
 +        public void run() {
 +          Region soloRgn = getCache().getRegion(soloRegionName);
 +          if (soloRgn == null) {
 +            // Expect a null region from originVM
 +            try {
 +              AttributesFactory factory2 = new AttributesFactory();         
 +              factory2.setScope(Scope.DISTRIBUTED_ACK);
 +              factory2.setEarlyAck(false);
 +              factory2.setDataPolicy(DataPolicy.REPLICATE);
 +              soloRgn = getCache().createRegion(soloRegionName, factory2.create());
 +            } catch (CacheException e) {
 +              Assert.fail("While creating region ", e);
 +            }
 +          }
 +          Region.Entry re = soloRgn.getEntry("soloKey0");
 +          assertNotNull(re);
 +          assertEquals("soloVal0_3", re.getValue());
 +          re = soloRgn.getEntry("soloKey1");
 +          assertNotNull(re);
 +          assertEquals("soloVal1_3", re.getValue());
 +        }
 +      };
 +    originVM.invoke(soloRegionCommitValidator2);
 +    soloRegionVM.invoke(soloRegionCommitValidator2);
 +    SerializableRunnable nonSoloChangeValidator2 = 
 +      new SerializableRunnable("testHighAvailabilityFeatures: validate v2 non-solo Region changes") {
 +        public void run() {
 +          Region rgn1 = getCache().getRegion(rgnName);
 +          if (rgn1 == null) {
 +            // Expect a null region from originVM
 +            try {
 +              AttributesFactory factory2 = new AttributesFactory();
 +              factory2.setScope(Scope.DISTRIBUTED_ACK);
 +              factory2.setEarlyAck(false);
 +              factory2.setDataPolicy(DataPolicy.REPLICATE);
 +              rgn1 = getCache().createRegion(rgnName, factory2.create());
 +            } catch (CacheException e) {
 +              Assert.fail("While creating region", e);
 +            }
 +          }
 +          Region.Entry re = rgn1.getEntry("key0");
 +          assertNotNull(re);
 +          assertEquals("val0_5", re.getValue());
 +          re = rgn1.getEntry("key1");
 +          assertNotNull(re);
 +          assertEquals("val1_5", re.getValue());
 +        }
 +      };
 +    Invoke.invokeInEveryVM(nonSoloChangeValidator2);
 +  }
 +  
 +  /** 
 +   * A class used in testLockBatchParticipantsUpdate to pause a transaction in the
 +   * afterResrvation and afterSend states.
 +   */
 +  static public class PausibleTX implements Runnable {
 +    public boolean isRunning = false;
 +    public String rgnName = null;
 +    public Cache myCache = null;
 +    public Object key = null;
 +    public Object value = null;
 +
 +    public boolean getIsRunning() {
 +      return this.isRunning;
 +    }
 +
 +    public void run() {
 +      Region rgn = this.myCache.getRegion(this.rgnName);
 +      final CacheTransactionManager txMgr = this.myCache.getCacheTransactionManager();
 +      txMgr.begin();
 +      ((TXStateProxyImpl)((TXManagerImpl)txMgr).getTXState()).forceLocalBootstrap();
 +      TXState txState = (TXState)((TXStateProxyImpl)((TXManagerImpl)txMgr).getTXState()).getRealDeal(null,null);
 +      txState.setAfterReservation(new Runnable() {
 +        public void run() {
 +          try {
 +            synchronized (PausibleTX.class) {
 +              PausibleTX.this.isRunning = true;
 +              // Notify the thread that created this, that we are ready
 +              PausibleTX.class.notifyAll();
 +              // Wait for the controller to start a GII and let us proceed
 +              PausibleTX.class.wait();
 +            }
 +          }
 +          catch (InterruptedException ie) {
 +//            PausibleTX.this.myCache.getLogger().info("Why was I interrupted? " + ie);
 +            fail("interrupted");
 +          }
 +        }
 +      });
 +      txState.setAfterSend(new Runnable() {
 +        public void run() {
 +          try {
 +            synchronized (PausibleTX.class) {
 +              // Notify the controller that we have sent the TX data (and the
 +              // update)
 +              PausibleTX.class.notifyAll();
 +              // Wait until the controller has determined in fact the update
 +              // took place
 +              PausibleTX.class.wait();
 +            }
 +          }
 +          catch (InterruptedException ie) {
 +//            PausibleTX.this.myCache.getLogger().info("Why was I interrupted? " + ie);
 +            fail("interrupted");
 +          }
 +        }
 +      });
 +      try { 
 +        rgn.put(key, value);
 +        txMgr.commit();
 +      } catch (CommitConflictException cce) {
 +        fail("Did not expect commit conflict exception when sending updates to new members in PausibleTX" + cce);
 +//      } catch (CacheException ce) {
 +//        fail("Did not expect cache exception when sending updates to new members in PausibleTX" + ce);
 +      }
 +    }
 +  }
 +  
 +  /**
 +   * Returns the GemFire system ID of the VM on which this method is run
 +   */
 +  public static Serializable getSystemId() {
 +    Serializable ret = null;
 +    if (DistributedTestCase.system != null) {
 +      ret = DistributedTestCase.system.getDistributionManager().getId();
 +    }
 +    return ret;
 +  }
 +  static HashSet preTXSystemIds;
 +  public static void setPreTXSystemIds(HashSet ids) {
 +    TXDistributedDUnitTest.preTXSystemIds = ids;
 +  }
 +  static HashSet postTXSystemIds;
 +  public static void setPostTXSystemIds(HashSet ids) {
 +    TXDistributedDUnitTest.postTXSystemIds = ids;
 +  }
 +  static Serializable txHostId;
 +  public static void setTXHostSystemId(Serializable id) {
 +    TXDistributedDUnitTest.txHostId = id;
 +  }
 +  /**
 +   * Test update of lock batch participants (needed when new members are
 +   * discovered between a commit's locking phase and the applicatoin of the
 +   * Region's data. See bug 32999
 +   */
 +  public void testLockBatchParticipantsUpdate() throws Exception {
 +//    final CacheTransactionManager txMgr = this.getCache().getCacheTransactionManager();
 +    final String rgnName = getUniqueName();
 +    Region rgn = getCache().createRegion(rgnName, getRegionAttributes());
 +    rgn.create("key", null);
 +
 +    Host host = Host.getHost(0);
 +    VM vm0 = host.getVM(0);
 +    VM vm1 = host.getVM(1);
 +    VM vm2 = host.getVM(2);
 +    SerializableRunnable initRegions = 
 +      new SerializableRunnable("testLockBatchParticipantsUpdate: initial configuration") {
 +      public void run() {
 +        try {
 +          Region rgn1 = getCache().createRegion(rgnName, getRegionAttributes());
 +          rgn1.create("key", null);
 +        } catch (CacheException e) {
 +          Assert.fail("While creating region", e);
 +        }
 +      }
 +    };
 +    vm0.invoke(initRegions);
 +    vm1.invoke(initRegions);
 +    rgn.put("key", "val1");
 +
 +    // Connect vm2 also since it may have been shutdown when logPerTest
 +    // is turned on
 +    vm2.invoke(new SerializableRunnable("connect vm2 if not connected") {
 +      public void run() {
 +        getCache();
 +      }
 +    });
 +
 +    // Make VM0 the Grantor 
 +    vm0.invoke(new SerializableRunnable("testLockBatchParticipantsUpdate: remote grantor init") {
 +      public void run() {
 +        try {
 +          Region rgn1 = getCache().getRegion(rgnName);
 +          final CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
 +          assertEquals("val1", rgn1.getEntry("key").getValue());
 +          txMgr2.begin();
 +          rgn1.put("key", "val2");
 +          txMgr2.commit();
 +          assertNotNull(TXLockService.getDTLS());
 +          assertTrue(TXLockService.getDTLS().isLockGrantor());
 +        } catch (CacheException e) {
 +          fail("While performing first transaction");
 +        }
 +      }
 +    });
 +    
 +    // fix for bug 38843 causes the DTLS to be created in every TX participant
 +    assertNotNull(TXLockService.getDTLS());
 +    assertFalse(TXLockService.getDTLS().isLockGrantor());
 +    assertEquals("val2", rgn.getEntry("key").getValue());
 +
 +    // Build sets of System Ids and set them up on VM0 for future batch member checks
 +    HashSet txMembers = new HashSet(4);
 +    txMembers.add(getSystemId());
-     txMembers.add(vm0.invoke(TXDistributedDUnitTest.class, "getSystemId"));
-     vm0.invoke(TXDistributedDUnitTest.class, "setPreTXSystemIds", new Object[] {txMembers});
-     txMembers.add(vm2.invoke(TXDistributedDUnitTest.class, "getSystemId"));
-     vm0.invoke(TXDistributedDUnitTest.class, "setPostTXSystemIds", new Object[] {txMembers});
++    txMembers.add(vm0.invoke(() -> TXDistributedDUnitTest.getSystemId()));
++    vm0.invoke(() -> TXDistributedDUnitTest.setPreTXSystemIds(txMembers));
++    txMembers.add(vm2.invoke(() -> TXDistributedDUnitTest.getSystemId()));
++    vm0.invoke(() -> TXDistributedDUnitTest.setPostTXSystemIds(txMembers));
 +
 +    // Don't include the tx host in the batch member set(s)
-     Serializable vm1HostId = (Serializable) vm1.invoke(TXDistributedDUnitTest.class, "getSystemId");
-     vm0.invoke(TXDistributedDUnitTest.class, "setTXHostSystemId", new Object[] {vm1HostId});
++    Serializable vm1HostId = (Serializable) vm1.invoke(() -> TXDistributedDUnitTest.getSystemId());
++    vm0.invoke(() -> TXDistributedDUnitTest.setTXHostSystemId(vm1HostId));
 +
 +    // Create a TX on VM1 (such that it will ask for locks on VM0) that uses the callbacks
 +    // to pause and give us time to start a GII process on another VM
 +    vm1.invoke(new SerializableRunnable("testLockBatchParticipantsUpdate: slow tx (one that detects new member)") {
 +      public void run() {
 +        // fix for bug 38843 causes the DTLS to be created in every TX participant
 +        assertNotNull(TXLockService.getDTLS());
 +        assertFalse(TXLockService.getDTLS().isLockGrantor());
 +        
 +        PausibleTX pauseTXRunnable = new PausibleTX();
 +        pauseTXRunnable.rgnName = rgnName;
 +        pauseTXRunnable.myCache = getCache();
 +        pauseTXRunnable.key = "key";
 +        pauseTXRunnable.value = "val3";
 +        new Thread(pauseTXRunnable, "PausibleTX Thread").start();
 +        synchronized(PausibleTX.class) {
 +          while(!pauseTXRunnable.getIsRunning()) {
 +            try {
 +              PausibleTX.class.wait();
 +            }
 +            catch (InterruptedException ie) {
 +              fail("Did not expect " + ie);
 +            }
 +          }
 +        }
 +      }
 +    });
 +
 +    // Verify that the lock batch exists VM0 and has the size we expect
 +    vm0.invoke(new SerializableRunnable("testLockBatchParticipantsUpdate: Verify lock batch exists on VM0 with expected size") {
 +      public void run() {
 +        getCache().getRegion(rgnName);
 +        TXLockServiceImpl dtls = (TXLockServiceImpl) TXLockService.getDTLS();
 +        assertNotNull(dtls);
 +        assertTrue(dtls.isLockGrantor());
 +        DLockService dLockSvc = dtls.getInternalDistributedLockService();
 +        assertNotNull(TXDistributedDUnitTest.txHostId);
 +        DLockBatch[] batches = dLockSvc.getGrantor().getLockBatches((InternalDistributedMember)TXDistributedDUnitTest.txHostId);
 +        assertEquals(batches.length, 1);
 +        TXLockBatch txLockBatch = (TXLockBatch) batches[0];
 +        assertNotNull(txLockBatch);
 +        assertNotNull(TXDistributedDUnitTest.preTXSystemIds);
 +        assertTrue("Members in lock batch " + txLockBatch.getParticipants() + " not the same as " + TXDistributedDUnitTest.preTXSystemIds, 
 +                   txLockBatch.getParticipants().equals(TXDistributedDUnitTest.preTXSystemIds));
 +      }
 +    });
 +    
 +    // Start a GII process on VM2
 +    vm2.invoke(new SerializableRunnable("testLockBatchParticipantsUpdate: start GII") {
 +      public void run() {
 +        try {
 +          AttributesFactory factory = new AttributesFactory();
 +          factory.setScope(Scope.DISTRIBUTED_ACK);
 +          factory.setEarlyAck(false);
 +          factory.setDataPolicy(DataPolicy.REPLICATE);
 +          getCache().createRegion(rgnName, factory.create());
 +        } catch (CacheException e) {
 +          Assert.fail("While creating region", e);
 +        }
 +      }
 +    });
 +
 +    // Notify TX on VM1 so that it can continue
 +    vm1.invoke(new SerializableRunnable("testLockBatchParticipantsUpdate: Notfiy VM1 TX to continue") {
 +      public void run() {
 +        synchronized(PausibleTX.class) {
 +          // Notify VM1 that it should proceed to the TX send
 +          PausibleTX.class.notifyAll();
 +          // Wait until VM1 has sent the TX
 +          try {
 +            PausibleTX.class.wait();
 +          }
 +          catch (InterruptedException ie) {
 +            fail("Did not expect " + ie);
 +          }
 +        }
 +      }
 +    });
 +    
 +    // Verify that the batch on VM0 has added VM2 into the set
 +    vm0.invoke(new SerializableRunnable("testLockBatchParticipantsUpdate: Verify lock batch contains VM2") {
 +      public void run() {
 +        getCache().getRegion(rgnName);
 +        TXLockServiceImpl dtls = (TXLockServiceImpl) TXLockService.getDTLS();
 +        assertNotNull(dtls);
 +        assertTrue(dtls.isLockGrantor());
 +        DLockService dLockSvc = dtls.getInternalDistributedLockService();
 +        assertNotNull(TXDistributedDUnitTest.txHostId);
 +        DLockBatch[] batches = dLockSvc.getGrantor().getLockBatches((InternalDistributedMember)TXDistributedDUnitTest.txHostId);
 +        assertEquals(batches.length, 1);
 +        TXLockBatch txLockBatch = (TXLockBatch) batches[0];
 +        assertNotNull(txLockBatch);
 +        assertNotNull(TXDistributedDUnitTest.preTXSystemIds);
 +        assertTrue("Members in lock batch " + txLockBatch.getParticipants() + " not the same as " + TXDistributedDUnitTest.postTXSystemIds, 
 +                   txLockBatch.getParticipants().equals(TXDistributedDUnitTest.postTXSystemIds));
 +      }
 +    });
 +    // fix for bug 38843 causes the DTLS to be created in every TX participant
 +    assertNotNull(TXLockService.getDTLS());
 +    assertFalse(TXLockService.getDTLS().isLockGrantor());
 +    assertEquals("val3", rgn.getEntry("key").getValue());
 +    
 +    
 +    // Notify TX on VM1 that it can go ahead and complete the TX
 +    vm1.invoke(new SerializableRunnable("testLockBatchParticipantsUpdate: Notfiy VM1 TX to finish") {
 +      public void run() {
 +        synchronized(PausibleTX.class) {
 +          // Notify VM1 that it should finish the TX
 +          PausibleTX.class.notifyAll();
 +        }
 +      }
 +    });
 +    
 +    
 +    rgn.destroyRegion();
 +  }
 +
 +  /**
 +   * Hitachi bug 38809:  Applying an exception to a remote VM fails due to
 +   * an IOException on a Region configured for LRU Overflow
 +   */
 +  public static final String TROUBLE_KEY = "GoBoom";
 +  public static class TXTroubleMaker implements LocalRegion.TestCallable {
 +    // private final Region r;
 +    public void call(LocalRegion r, Operation op, RegionEntry re) {
 +      if (TROUBLE_KEY.equals(re.getKey())) {
 +        throw new DiskAccessException(TROUBLE_KEY, r);
 +      }
 +    }
 +  }
 +
 +  public static class ShutdownListener implements ResourceEventsListener {
 +    CountDownLatch latch = new CountDownLatch(1);
 +    @Override
 +    public void handleEvent(ResourceEvent event, Object resource) {
 +      if (event.equals(ResourceEvent.CACHE_REMOVE)) {
 +        try {
 +          latch.await();
 +        } catch (InterruptedException e) {
 +          e.printStackTrace();
 +        }
 +      }
 +    }
 +    public void unblockShutdown() {
 +      this.latch.countDown();
 +    }
 +  }
 +  
 +  @Ignore("Disabled for 51260")
 +  public void DISABLED_testRemoteCommitFailure() {
 +    try {
 +    disconnectAllFromDS();
 +    final String rgnName1= getUniqueName()  + "_1";
 +    final String rgnName2 = getUniqueName() + "_2";
 +    final String diskStoreName = getUniqueName() + "_store";
 +    Host host = Host.getHost(0);
 +    VM origin = host.getVM(0);
 +    VM trouble1 = host.getVM(1);
 +    VM trouble2 = host.getVM(2);
 +    VM noTrouble = host.getVM(3);
 +    CacheSerializableRunnable initRegions =
 +      new CacheSerializableRunnable("Initialize no trouble regions") {
 +      @Override
 +      public void run2() {
 +        getCache().createDiskStoreFactory().setDiskDirs(getDiskDirs()).create(diskStoreName);
 +        TXManagerImpl.ALLOW_PERSISTENT_TRANSACTIONS = true;
 +        AttributesFactory af = new AttributesFactory();
 +        af.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
 +        af.setScope(Scope.DISTRIBUTED_ACK);
 +        af.setDiskStoreName(diskStoreName);
 +        getCache().createRegion(rgnName1, af.create());
 +        getCache().createRegion(rgnName2, af.create());
 +      }
 +    };
 +    origin.invoke(initRegions);
 +    noTrouble.invoke(initRegions);
 +    SerializableRunnable initTroulbeRegions =
 +      new CacheSerializableRunnable("Initialize regions that cause trouble") {
 +      @Override
 +      public void run2() {
 +        GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
 +        InternalRegionArguments ira = new InternalRegionArguments().setTestCallable(new TXTroubleMaker());
 +        try {
 +          getCache().createDiskStoreFactory().setDiskDirs(getDiskDirs()).create(diskStoreName);
 +          TXManagerImpl.ALLOW_PERSISTENT_TRANSACTIONS = true;
 +          AttributesFactory af = new AttributesFactory();
 +          af.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
 +          af.setScope(Scope.DISTRIBUTED_ACK);
 +          af.setDiskStoreName(diskStoreName);
 +          gfc.createVMRegion(rgnName1, af.create(), ira);
 +          gfc.createVMRegion(rgnName2, af.create(), ira);
 +          gfc.getDistributedSystem().addResourceListener(new ShutdownListener());
 +        } catch (IOException ioe) {
 +          fail(ioe.toString());
 +        }
 +        catch (TimeoutException e) {
 +          fail(e.toString());
 +        }
 +        catch (ClassNotFoundException e) {
 +          fail(e.toString());
 +        }
 +      }
 +    };
 +    trouble1.invoke(initTroulbeRegions);
 +    trouble2.invoke(initTroulbeRegions);
 +
 +    SerializableRunnable doTransaction =
 +      new CacheSerializableRunnable("Run failing transaction") {
 +      @Override
 +      public void run2() {
 +        Cache c = getCache();
 +        Region r1 = c.getRegion(rgnName1);
 +        assertNotNull(r1);
 +        Region r2 = c.getRegion(rgnName2);
 +        assertNotNull(r2);
 +        CacheTransactionManager txmgr = c.getCacheTransactionManager();
 +        txmgr.begin();
 +        r1.put("k1", "k1");
 +        r1.put("k2", "k2");
 +        r1.put(TROUBLE_KEY, TROUBLE_KEY);
 +        r2.put("k1", "k1");
 +        r2.put("k2", "k2");
 +        r2.put(TROUBLE_KEY, TROUBLE_KEY);
 +        try {
 +          txmgr.commit();
 +          fail("Expected an tx incomplete exception");
 +        } catch (CommitIncompleteException yay) {
 +          String msg = yay.getMessage();
 +//          getLogWriter().info("failing exception", yay);
 +          // Each region on a trouble VM should be mentioned (two regions per trouble VM)
 +          int ind=0, match=0;
 +          while((ind = msg.indexOf(rgnName1, ind)) >= 0) {
 +            ind++; match++;
 +          }
 +          assertEquals(2, match);
 +          ind=match=0;
 +          while((ind = msg.indexOf(rgnName2, ind)) >= 0) {
 +            ind++; match++;
 +          }
 +          assertEquals(2, match);
 +          // DiskAccessExcpetions should be mentioned four times
 +          ind=match=0;
 +          while((ind = msg.indexOf(DiskAccessException.class.getName(), ind)) >= 0) {
 +            ind++; match++;
 +          }
 +          assertEquals(4, match);
 +        }
 +      }
 +    };
 +    IgnoredException ee = null;
 +    try {
 +      ee = IgnoredException.addIgnoredException(DiskAccessException.class.getName() + "|" +
 +          CommitIncompleteException.class.getName() + "|" +
 +          CommitReplyException.class.getName());
 +      origin.invoke(doTransaction);
 +    } finally {
 +      if (ee!=null) ee.remove();
 +    }
 +
 +    SerializableCallable allowCacheToShutdown = new SerializableCallable() {
 +      @Override
 +      public Object call() throws Exception {
 +        GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
 +        List<ResourceEventsListener> listeners = cache.getDistributedSystem().getResourceListeners();
 +        for (ResourceEventsListener l : listeners) {
 +          if (l instanceof ShutdownListener) {
 +            ShutdownListener shutListener = (ShutdownListener) l;
 +            shutListener.unblockShutdown();
 +          }
 +        }
 +        return null;
 +      }
 +    };
 +    trouble1.invoke(allowCacheToShutdown);
 +    trouble2.invoke(allowCacheToShutdown);
 +
 +    // Assert proper content on failing VMs
 +    SerializableRunnable assertTroubledContent =
 +      new CacheSerializableRunnable("Assert partail commit data") {
 +      @Override
 +      public void run2() {
 +        final Cache c = getCache();
 +        Wait.waitForCriterion(new WaitCriterion() {
 +          @Override
 +          public boolean done() {
 +            return c.getRegion(rgnName1) == null;
 +          }
 +          
 +          @Override
 +          public String description() {
 +            return null;
 +          }
 +        }, 30000, 1000, true);
 +        Region r2 = c.getRegion(rgnName2);
 +        assertNull(r2);
 +      }
 +    };
 +    trouble1.invoke(assertTroubledContent);
 +    trouble2.invoke(assertTroubledContent);
 +
 +    // Assert proper content on successful VMs
 +    SerializableRunnable assertSuccessfulContent =
 +      new CacheSerializableRunnable("Assert complete commit of data on successful VMs") {
 +      @Override
 +      public void run2() {
 +        Cache c = getCache();
 +        {
 +          Region r1 = c.getRegion(rgnName1);
 +          assertNotNull(r1);
 +          assertEquals("k1", r1.getEntry("k1").getValue());
 +          assertEquals("k2", r1.getEntry("k2").getValue());
 +          assertEquals(TROUBLE_KEY, r1.getEntry(TROUBLE_KEY).getValue());
 +        }
 +        {
 +          Region r2 = c.getRegion(rgnName2);
 +          assertNotNull(r2);
 +          assertEquals("k1", r2.getEntry("k1").getValue());
 +          assertEquals("k2", r2.getEntry("k2").getValue());
 +          assertEquals(TROUBLE_KEY, r2.getEntry(TROUBLE_KEY).getValue());
 +        }
 +      }
 +    };
 +    noTrouble.invoke(assertSuccessfulContent);
 +    
 +    // Assert no content on originating VM
 +    SerializableRunnable assertNoContent =
 +      new CacheSerializableRunnable("Assert data survives on origin VM") {
 +      @Override
 +      public void run2() {
 +        Cache c = getCache();
 +        {
 +          Region r1 = c.getRegion(rgnName1);
 +          assertNotNull(r1);
 +          assertNotNull(r1.getEntry("k1"));
 +          assertNotNull(r1.getEntry("k2"));
 +          assertNotNull(r1.getEntry(TROUBLE_KEY));
 +        }
 +        {
 +          Region r2 = c.getRegion(rgnName2);
 +          assertNotNull(r2);
 +          assertNotNull(r2.getEntry("k1"));
 +          assertNotNull(r2.getEntry("k2"));
 +          assertNotNull(r2.getEntry(TROUBLE_KEY));
 +        }
 +      }
 +    };
 +    origin.invoke(assertNoContent);
 +    } finally {
 +      Invoke.invokeInEveryVM(new SerializableCallable() {
 +        @Override
 +        public Object call() throws Exception {
 +          TXManagerImpl.ALLOW_PERSISTENT_TRANSACTIONS = false;
 +          return null;
 +        }
 +      });
 +    }
 +  }
 +}
 +

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/TXOrderDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/TXOrderDUnitTest.java
index a253f09,0000000..b1d7bab
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/TXOrderDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/TXOrderDUnitTest.java
@@@ -1,434 -1,0 +1,434 @@@
 +/*
 + * 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.cache30;
 +
 +import java.util.ArrayList;
 +import java.util.Arrays;
 +import java.util.Iterator;
 +import java.util.List;
 +
 +import javax.naming.Context;
 +import javax.transaction.UserTransaction;
 +
 +import com.gemstone.gemfire.CopyHelper;
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.CacheEvent;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheListener;
 +import com.gemstone.gemfire.cache.CacheLoader;
 +import com.gemstone.gemfire.cache.CacheLoaderException;
 +import com.gemstone.gemfire.cache.CacheTransactionManager;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.EntryEvent;
 +import com.gemstone.gemfire.cache.LoaderHelper;
 +import com.gemstone.gemfire.cache.PartitionAttributes;
 +import com.gemstone.gemfire.cache.PartitionAttributesFactory;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionShortcut;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.TransactionEvent;
 +import com.gemstone.gemfire.cache.TransactionListener;
 +import com.gemstone.gemfire.cache.query.IndexType;
 +import com.gemstone.gemfire.cache.query.Query;
 +import com.gemstone.gemfire.cache.query.QueryService;
 +import com.gemstone.gemfire.cache.query.SelectResults;
 +import com.gemstone.gemfire.cache.query.transaction.Person;
 +import com.gemstone.gemfire.cache.util.CacheListenerAdapter;
 +import com.gemstone.gemfire.cache.util.TransactionListenerAdapter;
 +import com.gemstone.gemfire.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.LogWriterUtils;
 +import com.gemstone.gemfire.test.dunit.SerializableCallable;
 +import com.gemstone.gemfire.test.dunit.VM;
 +
 +/**
 + * Test the order of operations done on the farside of a tx.
 + *
 + * @author darrel
 + * @since 5.0
 + */
 +public class TXOrderDUnitTest extends CacheTestCase {
 +
 +  private transient Region r;
 +  private transient DistributedMember otherId;
 +  protected transient int invokeCount;
 +  
 +  public TXOrderDUnitTest(String name) {
 +    super(name);
 +  }
 +
 +  private VM getOtherVm() {
 +    Host host = Host.getHost(0);
 +    return host.getVM(0);
 +  }
 +    
 +  private void initOtherId() {
 +    VM vm = getOtherVm();
 +    vm.invoke(new CacheSerializableRunnable("Connect") {
 +        public void run2() throws CacheException {
 +          getCache();
 +        }
 +      });
-     this.otherId = (DistributedMember)vm.invoke(TXOrderDUnitTest.class, "getVMDistributedMember");
++    this.otherId = (DistributedMember)vm.invoke(() -> TXOrderDUnitTest.getVMDistributedMember());
 +  }
 +  private void doCommitOtherVm() {
 +    VM vm = getOtherVm();
 +    vm.invoke(new CacheSerializableRunnable("create root") {
 +        public void run2() throws CacheException {
 +          AttributesFactory af = new AttributesFactory();
 +          af.setScope(Scope.DISTRIBUTED_ACK);
 +          Region r1 = createRootRegion("r1", af.create());
 +          Region r2 = r1.createSubregion("r2", af.create());
 +          Region r3 = r2.createSubregion("r3", af.create());
 +          CacheTransactionManager ctm =  getCache().getCacheTransactionManager();
 +          ctm.begin();
 +          r2.put("b", "value1");
 +          r3.put("c", "value2");
 +          r1.put("a", "value3");
 +          r1.put("a2", "value4");
 +          r3.put("c2", "value5");
 +          r2.put("b2", "value6");
 +          ctm.commit();
 +        }
 +      });
 +  }
 +
 +  public static DistributedMember getVMDistributedMember() {
 +    return InternalDistributedSystem.getAnyInstance().getDistributedMember();
 +  }
 +  
 +  //////////////////////  Test Methods  //////////////////////
 +
 +  List expectedKeys;
 +  int clCount = 0;
 +
 +  Object getCurrentExpectedKey() {
 +    Object result = this.expectedKeys.get(this.clCount);
 +    this.clCount += 1;
 +    return result;
 +  }
 +  /**
 +   * make sure listeners get invoked in correct order on far side of tx
 +   */
 +  public void testFarSideOrder() throws CacheException {
 +    initOtherId();
 +    AttributesFactory af = new AttributesFactory();
 +    af.setDataPolicy(DataPolicy.REPLICATE);
 +    af.setScope(Scope.DISTRIBUTED_ACK);
 +    CacheListener cl1 = new CacheListenerAdapter() {
 +        public void afterCreate(EntryEvent e) {
 +          assertEquals(getCurrentExpectedKey(), e.getKey());
 +        }
 +      };
 +    af.addCacheListener(cl1);
 +    Region r1 = createRootRegion("r1", af.create());
 +    Region r2 = r1.createSubregion("r2", af.create());
 +    r2.createSubregion("r3", af.create());
 +
 +    TransactionListener tl1 = new TransactionListenerAdapter() {
 +        public void afterCommit(TransactionEvent e) {
 +          assertEquals(6, e.getEvents().size());
 +          ArrayList keys = new ArrayList();
 +          Iterator it = e.getEvents().iterator();
 +          while (it.hasNext()) {
 +            EntryEvent ee = (EntryEvent)it.next();
 +            keys.add(ee.getKey());
 +            assertEquals(null, ee.getCallbackArgument());
 +            assertEquals(true, ee.isCallbackArgumentAvailable());
 +          }
 +          assertEquals(TXOrderDUnitTest.this.expectedKeys, keys);
 +          TXOrderDUnitTest.this.invokeCount = 1;
 +        }
 +      };
 +    CacheTransactionManager ctm =  getCache().getCacheTransactionManager();
 +    ctm.addListener(tl1);
 +
 +    this.invokeCount = 0;
 +    this.clCount = 0;
 +    this.expectedKeys = Arrays.asList(new String[]{"b", "c", "a", "a2", "c2", "b2"});
 +    doCommitOtherVm();
 +    assertEquals(1, this.invokeCount);
 +    assertEquals(6, this.clCount);
 +  }
 +
 +  /**
 +   * test bug#40870
 +   * @throws Exception
 +   */
 +  public void _testFarSideOpForLoad() throws Exception {
 +    Host host = Host.getHost(0);
 +    VM vm1 = host.getVM(0);
 +    VM vm2 = host.getVM(1);
 +    vm1.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        AttributesFactory af = new AttributesFactory();
 +        af.setDataPolicy(DataPolicy.REPLICATE);
 +        af.setScope(Scope.DISTRIBUTED_ACK);
 +        CacheListener cl1 = new CacheListenerAdapter() {
 +          public void afterCreate(EntryEvent e) {
 +            assertTrue(e.getOperation().isLocalLoad());
 +          }
 +        };
 +        af.addCacheListener(cl1);
 +        CacheLoader cl = new CacheLoader() {
 +          public Object load(LoaderHelper helper) throws CacheLoaderException {
 +            LogWriterUtils.getLogWriter().info("Loading value:"+helper.getKey()+"_value");
 +            return helper.getKey()+"_value";
 +          }
 +          public void close() {
 +          }
 +        };
 +        af.setCacheLoader(cl);
 +        createRootRegion("r1", af.create());
 +        return null;
 +      }
 +    });
 +
 +    vm2.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        AttributesFactory af = new AttributesFactory();
 +        af.setDataPolicy(DataPolicy.REPLICATE);
 +        af.setScope(Scope.DISTRIBUTED_ACK);
 +        CacheListener cl1 = new CacheListenerAdapter() {
 +          public void afterCreate(EntryEvent e) {
 +            LogWriterUtils.getLogWriter().info("op:"+e.getOperation().toString());
 +            assertTrue(!e.getOperation().isLocalLoad());
 +          }
 +        };
 +        af.addCacheListener(cl1);
 +        createRootRegion("r1", af.create());
 +        return null;
 +      }
 +    });
 +
 +    vm1.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        Region r = getRootRegion("r1");
 +        getCache().getCacheTransactionManager().begin();
 +        r.get("obj_2");
 +        getCache().getCacheTransactionManager().commit();
 +        return null;
 +      }
 +    });
 +  }
 +
 +  public void testInternalRegionNotExposed() {
 +    Host host = Host.getHost(0);
 +    VM vm1 = host.getVM(0);
 +    VM vm2 = host.getVM(1);
 +    SerializableCallable createRegion = new SerializableCallable() {
 +      public Object call() throws Exception {
 +        ExposedRegionTransactionListener tl = new ExposedRegionTransactionListener();
 +        CacheTransactionManager ctm = getCache().getCacheTransactionManager();
 +        ctm.addListener(tl);
 +        ExposedRegionCacheListener cl = new ExposedRegionCacheListener();
 +        AttributesFactory af = new AttributesFactory();
 +        PartitionAttributes pa = new PartitionAttributesFactory()
 +          .setRedundantCopies(1)
 +          .setTotalNumBuckets(1)
 +          .create();
 +        af.setPartitionAttributes(pa);
 +        af.addCacheListener(cl);
 +        Region pr = createRootRegion("testTxEventForRegion", af.create());
 +        return null;
 +      }
 +    };
 +    vm1.invoke(createRegion);
 +    vm2.invoke(createRegion);
 +    vm1.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        Region pr = getRootRegion("testTxEventForRegion");
 +        CacheTransactionManager ctm = getCache().getCacheTransactionManager();
 +        pr.put(2, "tw");
 +        pr.put(3, "three");
 +        pr.put(4, "four");
 +        ctm.begin();
 +        pr.put(1, "one");
 +        pr.put(2, "two");
 +        pr.invalidate(3);
 +        pr.destroy(4);
 +        ctm.commit();
 +        return null;
 +      }
 +    });
 +    SerializableCallable verifyListener = new SerializableCallable() {
 +      public Object call() throws Exception {
 +        Region pr = getRootRegion("testTxEventForRegion");
 +        CacheTransactionManager ctm = getCache().getCacheTransactionManager();
 +        ExposedRegionTransactionListener tl = (ExposedRegionTransactionListener)ctm.getListeners()[0];
 +        ExposedRegionCacheListener cl = (ExposedRegionCacheListener)pr.getAttributes().getCacheListeners()[0];
 +        assertFalse(tl.exceptionOccurred);
 +        assertFalse(cl.exceptionOccurred);
 +        return null;
 +      }
 +    };
 +    vm1.invoke(verifyListener);
 +    vm2.invoke(verifyListener);
 +  }
 +  
 +  class ExposedRegionTransactionListener extends TransactionListenerAdapter {
 +    private boolean exceptionOccurred = false;
 +    @Override
 +    public void afterCommit(TransactionEvent event) {
 +      List<CacheEvent<?, ?>> events = event.getEvents();
 +      for (CacheEvent<?, ?>e : events) {
 +        if (!"/testTxEventForRegion".equals(e.getRegion().getFullPath())) {
 +          exceptionOccurred = true;
 +        }
 +      }
 +    }
 +  }
 +  class ExposedRegionCacheListener extends CacheListenerAdapter {
 +    private boolean exceptionOccurred = false;
 +    @Override
 +    public void afterCreate(EntryEvent event) {
 +      verifyRegion(event);
 +    }
 +    @Override
 +    public void afterUpdate(EntryEvent event) {
 +      verifyRegion(event);
 +    }
 +    private void verifyRegion(EntryEvent event) {
 +      if (!"/testTxEventForRegion".equals(event.getRegion().getFullPath())) {
 +        exceptionOccurred = true;
 +      }
 +    }
 +  }
 +  
 +  private final int TEST_PUT = 0;
 +  private final int TEST_INVALIDATE = 1;
 +  private final int TEST_DESTROY = 2;
 +  /**
 +   * verify that queries on indexes work with transaction
 +   * @throws Exception
 +   */
 +  public void testFarSideIndexOnPut() throws Exception {
 +    doTest(TEST_PUT);
 +  }
 +
 +  public void testFarSideIndexOnInvalidate() throws Exception {
 +    doTest(TEST_INVALIDATE);
 +  }
 +
 +  public void testFarSideIndexOnDestroy() throws Exception {
 +    doTest(TEST_DESTROY);
 +  }
 +
 +  private void doTest(final int op) throws Exception {
 +    Host host = Host.getHost(0);
 +    VM vm1 = host.getVM(0);
 +    VM vm2 = host.getVM(1);
 +    SerializableCallable createRegionAndIndex = new SerializableCallable() {
 +      public Object call() throws Exception {
 +        AttributesFactory af = new AttributesFactory();
 +        af.setDataPolicy(DataPolicy.REPLICATE);
 +        af.setScope(Scope.DISTRIBUTED_ACK);
 +        Region region = createRootRegion("sample", af.create());
 +        QueryService qs = getCache().getQueryService();
 +        qs.createIndex("foo", IndexType.FUNCTIONAL, "age", "/sample");
 +        return null;
 +      }
 +    };
 +    vm1.invoke(createRegionAndIndex);
 +    vm2.invoke(createRegionAndIndex);
 +    
 +    //do transactional puts in vm1
 +    vm1.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        Context ctx = getCache().getJNDIContext();
 +        UserTransaction utx = (UserTransaction)ctx.lookup("java:/UserTransaction");
 +        Region region = getRootRegion("sample");
 +        Integer x = new Integer(0);
 +        utx.begin();
 +        region.create(x, new Person("xyz", 45));
 +        utx.commit();
 +        QueryService qs = getCache().getQueryService();
 +        Query q = qs.newQuery("select * from /sample where age < 50");
 +        assertEquals(1, ((SelectResults)q.execute()).size());
 +        Person dsample = (Person)CopyHelper.copy(region.get(x));
 +        dsample.setAge(55);
 +        utx.begin();
 +        switch (op) {
 +        case TEST_PUT:
 +          region.put(x, dsample);
 +          break;
 +        case TEST_INVALIDATE:
 +          region.invalidate(x);
 +          break;
 +        case TEST_DESTROY:
 +          region.destroy(x);
 +          break;
 +        default:
 +          fail("unknown op");
 +        }
 +        utx.commit();
 +        assertEquals(0, ((SelectResults)q.execute()).size());
 +        return null;
 +      }
 +    });
 +    
 +    //run query and verify results in other vm
 +    vm2.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        QueryService qs = getCache().getQueryService();
 +        Query q = qs.newQuery("select * from /sample where age < 50");
 +        assertEquals(0, ((SelectResults)q.execute()).size());
 +        return null;
 +      }
 +    });
 +  }
 +  
 +  public void testBug43353() {
 +    Host host = Host.getHost(0);
 +    VM vm1 = host.getVM(0);
 +    VM vm2 = host.getVM(1);
 +    
 +    SerializableCallable createRegion = new SerializableCallable() {
 +      public Object call() throws Exception {
 +        getCache().createRegionFactory(RegionShortcut.REPLICATE).create(getTestMethodName());
 +        return null;
 +      }
 +    };
 +    
 +    vm1.invoke(createRegion);
 +    vm2.invoke(createRegion);
 +    
 +    vm1.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        Region r = getCache().getRegion(getTestMethodName());
 +        r.put("ikey", "value");
 +        getCache().getCacheTransactionManager().begin();
 +        r.put("key1", new byte[20]);
 +        r.invalidate("ikey");
 +        getCache().getCacheTransactionManager().commit();
 +        return null;
 +      }
 +    });
 +    
 +    vm2.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        Region r = getCache().getRegion(getTestMethodName());
 +        Object v = r.get("key1");
 +        assertNotNull(v);
 +        assertTrue(v instanceof byte[]);
 +        assertNull(r.get("ikey"));
 +        return null;
 +      }
 +    });
 +  }
 +}


[41/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/DistributionStats.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/DistributionStats.java
index 28ca380,0000000..6750a54
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/DistributionStats.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/DistributionStats.java
@@@ -1,2051 -1,0 +1,2065 @@@
 +/*
 + * 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.distributed.internal;
 +
 +import org.apache.logging.log4j.Logger;
 +
 +import com.gemstone.gemfire.StatisticDescriptor;
 +import com.gemstone.gemfire.Statistics;
 +import com.gemstone.gemfire.StatisticsFactory;
 +import com.gemstone.gemfire.StatisticsType;
 +import com.gemstone.gemfire.StatisticsTypeFactory;
 +import com.gemstone.gemfire.internal.NanoTimer;
 +import com.gemstone.gemfire.internal.StatisticsTypeFactoryImpl;
 +import com.gemstone.gemfire.internal.logging.LogService;
 +//import java.io.*;
 +import com.gemstone.gemfire.internal.tcp.Buffers;
 +import com.gemstone.gemfire.internal.util.Breadcrumbs;
 +
 +/**
 + * This class maintains statistics in GemFire about the distribution
 + * manager and distribution in general.
 + *
 + * @author Darrel Schneider
 + * @author Bruce Schuchardt
 + *
 + */
 +public class DistributionStats implements DMStats {
 +  private static final Logger logger = LogService.getLogger();
 +  
 +  public static boolean enableClockStats = false;
 +
 +
 +  //////////////////  Statistic "Id" Fields  //////////////////
 +
 +  private final static StatisticsType type;
 +  private final static int sentMessagesId;
 +  private final static int sentCommitMessagesId;
 +  private final static int commitWaitsId;
 +  private final static int sentMessagesTimeId;
 +  private final static int sentMessagesMaxTimeId;
 +  private final static int broadcastMessagesId;
 +  private final static int broadcastMessagesTimeId;
 +  private final static int receivedMessagesId;
 +  private final static int receivedBytesId;
 +  private final static int sentBytesId;
 +  private final static int processedMessagesId;
 +  private final static int processedMessagesTimeId;
 +  private final static int messageProcessingScheduleTimeId;
 +  private final static int messageChannelTimeId;
++  private final static int udpDispatchRequestTimeId;
 +  private final static int replyMessageTimeId;
 +  private final static int distributeMessageTimeId;
 +  private final static int nodesId;
 +  private final static int overflowQueueSizeId;
 +  private final static int processingThreadsId;
 +  private final static int serialThreadsId;
 +  private final static int waitingThreadsId;
 +  private final static int highPriorityThreadsId;
 +  private final static int partitionedRegionThreadsId;
 +  private final static int functionExecutionThreadsId;
 +  private final static int partitionedRegionThreadJobsId;
 +  private final static int functionExecutionThreadJobsId;
 +  private final static int waitingQueueSizeId;
 +  private final static int overflowQueueThrottleTimeId;
 +  private final static int overflowQueueThrottleCountId;
 +  private final static int highPriorityQueueSizeId;
 +  private final static int highPriorityQueueThrottleTimeId;
 +  private final static int highPriorityQueueThrottleCountId;
 +  private final static int partitionedRegionQueueSizeId;
 +  private final static int partitionedRegionQueueThrottleTimeId;
 +  private final static int partitionedRegionQueueThrottleCountId;
 +  private final static int functionExecutionQueueSizeId;
 +  private final static int functionExecutionQueueThrottleCountId;
 +  private final static int functionExecutionQueueThrottleTimeId;
 +  private final static int serialQueueSizeId;
 +  private final static int serialQueueBytesId;
 +  private final static int serialPooledThreadId;
 +  private final static int serialQueueThrottleTimeId;
 +  private final static int serialQueueThrottleCountId;
 +  private final static int replyWaitsInProgressId;
 +  private final static int replyWaitsCompletedId;
 +  private final static int replyWaitTimeId;
 +  private final static int replyTimeoutsId;
 +  private final static int replyWaitMaxTimeId;
 +  private final static int receiverConnectionsId;
 +  private final static int failedAcceptsId;
 +  private final static int failedConnectsId;
 +  private final static int reconnectAttemptsId;
 +  private final static int lostConnectionLeaseId;
 +  private final static int sharedOrderedSenderConnectionsId;
 +  private final static int sharedUnorderedSenderConnectionsId;
 +  private final static int threadOrderedSenderConnectionsId;
 +  private final static int threadUnorderedSenderConnectionsId;
 +
 +  private final static int syncSocketWritesInProgressId;
 +  private final static int syncSocketWriteTimeId;
 +  private final static int syncSocketWritesId;
 +  private final static int syncSocketWriteBytesId;
 +
 +  private final static int ucastReadsId;
 +  private final static int ucastReadBytesId;
 +  private final static int ucastWritesId;
 +  private final static int ucastWriteBytesId;
 +  private final static int ucastRetransmitsId;
 +
 +  private final static int mcastReadsId;
 +  private final static int mcastReadBytesId;
 +  private final static int mcastWritesId;
 +  private final static int mcastWriteBytesId;
 +  private final static int mcastRetransmitsId;
 +  private final static int mcastRetransmitRequestsId;
 +
 +  private final static int serializationTimeId;
 +  private final static int serializationsId;
 +  private final static int serializedBytesId;
 +
 +  private final static int pdxSerializationsId;
 +  private final static int pdxSerializedBytesId;
 +
 +  private final static int deserializationTimeId;
 +  private final static int deserializationsId;
 +  private final static int deserializedBytesId;
 +  private final static int pdxDeserializationsId;
 +  private final static int pdxDeserializedBytesId;
 +  private final static int pdxInstanceDeserializationsId;
 +  private final static int pdxInstanceDeserializationTimeId;
 +  private final static int pdxInstanceCreationsId;
 +
 +  private final static int msgSerializationTimeId;
 +  private final static int msgDeserializationTimeId;
 +
 +  private final static int batchSendTimeId;
 +  private final static int batchCopyTimeId;
 +  private final static int batchWaitTimeId;
 +  private final static int batchFlushTimeId;
 +
 +  private final static int threadOwnedReceiversId;
 +  private final static int threadOwnedReceiversId2;
 +
 +  private final static int asyncSocketWritesInProgressId;
 +  private final static int asyncSocketWritesId;
 +  private final static int asyncSocketWriteRetriesId;
 +  private final static int asyncSocketWriteTimeId;
 +  private final static int asyncSocketWriteBytesId;
 +
 +  private final static int socketLocksInProgressId;
 +  private final static int socketLocksId;
 +  private final static int socketLockTimeId;
 +
 +  private final static int bufferAcquiresInProgressId;
 +  private final static int bufferAcquiresId;
 +  private final static int bufferAcquireTimeId;
 +
 +  private final static int asyncQueueAddTimeId;
 +  private final static int asyncQueueRemoveTimeId;
 +
 +  private static final int asyncQueuesId;
 +  private static final int asyncQueueFlushesInProgressId;
 +  private static final int asyncQueueFlushesCompletedId;
 +  private static final int asyncQueueFlushTimeId;
 +  private static final int asyncQueueTimeoutExceededId;
 +  private static final int asyncQueueSizeExceededId;
 +  private static final int asyncDistributionTimeoutExceededId;
 +  private static final int asyncQueueSizeId;
 +  private static final int asyncQueuedMsgsId;
 +  private static final int asyncDequeuedMsgsId;
 +  private static final int asyncConflatedMsgsId;
 +
 +  private static final int asyncThreadsId;
 +  private static final int asyncThreadInProgressId;
 +  private static final int asyncThreadCompletedId;
 +  private static final int asyncThreadTimeId;
 +
 +  private static final int receiverDirectBufferSizeId;
 +  private static final int receiverHeapBufferSizeId;
 +  private static final int senderDirectBufferSizeId;
 +  private static final int senderHeapBufferSizeId;
 +
 +  private static final int messagesBeingReceivedId;
 +  private static final int messageBytesBeingReceivedId;
 +
 +  private static final int serialThreadStartsId;
 +  private static final int viewThreadStartsId;
 +  private static final int processingThreadStartsId;
 +  private static final int highPriorityThreadStartsId;
 +  private static final int waitingThreadStartsId;
 +  private static final int partitionedRegionThreadStartsId;
 +  private static final int functionExecutionThreadStartsId;
 +  private static final int serialPooledThreadStartsId;
 +  private static final int TOSentMsgId;
 +
 +  private static final int replyHandoffTimeId;
 +
 +  private final static int viewThreadsId;
 +  private final static int serialThreadJobsId;
 +  private final static int viewProcessorThreadJobsId;
 +  private final static int serialPooledThreadJobsId;
 +  private final static int pooledMessageThreadJobsId;
 +  private final static int highPriorityThreadJobsId;
 +  private final static int waitingPoolThreadJobsId;
 +
 +  private final static int eldersId;
 +  private final static int initialImageMessagesInFlightId;
 +  private final static int initialImageRequestsInProgressId;
 +  
 +  //For GMSHealthMonitor
 +  private final static int heartbeatRequestsSentId;  
 +  private final static int heartbeatRequestsReceivedId;  
 +  private final static int heartbeatsSentId;  
 +  private final static int heartbeatsReceivedId;  
 +  private final static int suspectsSentId;  
 +  private final static int suspectsReceivedId;
 +  private final static int finalCheckRequestsSentId;  
 +  private final static int finalCheckRequestsReceivedId;  
 +  private final static int finalCheckResponsesSentId;  
 +  private final static int finalCheckResponsesReceivedId;  
 +  private final static int tcpFinalCheckRequestsSentId; 
 +  private final static int tcpFinalCheckRequestsReceivedId;  
 +  private final static int tcpFinalCheckResponsesSentId;  
 +  private final static int tcpFinalCheckResponsesReceivedId;
 +  private final static int udpFinalCheckRequestsSentId;
 +  private final static int udpFinalCheckRequestsReceivedId;
 +  private final static int udpFinalCheckResponsesSentId;
 +  private final static int udpFinalCheckResponsesReceivedId;
 +
 +  static {
 +    String statName = "DistributionStats";
 +    String statDescription = "Statistics on the gemfire distribution layer.";
 +
 +    final String sentMessagesDesc = "The number of distribution messages that this GemFire system has sent. This includes broadcastMessages.";
 +    final String sentCommitMessagesDesc = "The number of transaction commit messages that this GemFire system has created to be sent. Note that it is possible for a commit to only create one message even though it will end up being sent to multiple recipients.";
 +    final String commitWaitsDesc = "The number of transaction commits that had to wait for a response before they could complete.";
 +    final String sentMessagesTimeDesc = "The total amount of time this distribution manager has spent sending messages. This includes broadcastMessagesTime.";
 +    final String sentMessagesMaxTimeDesc = "The highest amount of time this distribution manager has spent distributing a single message to the network.";
 +    final String broadcastMessagesDesc = "The number of distribution messages that this GemFire system has broadcast. A broadcast message is one sent to every other manager in the group.";
 +    final String broadcastMessagesTimeDesc = "The total amount of time this distribution manager has spent broadcasting messages. A broadcast message is one sent to every other manager in the group.";
 +    final String receivedMessagesDesc = "The number of distribution messages that this GemFire system has received.";
 +    final String receivedBytesDesc = "The number of distribution message bytes that this GemFire system has received.";
 +    final String sentBytesDesc = "The number of distribution message bytes that this GemFire system has sent.";
 +    final String processedMessagesDesc = "The number of distribution messages that this GemFire system has processed.";
 +    final String processedMessagesTimeDesc = "The amount of time this distribution manager has spent in message.process().";
 +    final String messageProcessingScheduleTimeDesc = "The amount of time this distribution manager has spent dispatching message to processor threads.";
 +    final String overflowQueueSizeDesc = "The number of normal distribution messages currently waiting to be processed.";
 +    final String waitingQueueSizeDesc = "The number of distribution messages currently waiting for some other resource before they can be processed.";
 +    final String overflowQueueThrottleTimeDesc = "The total amount of time, in nanoseconds, spent delayed by the overflow queue throttle.";
 +    final String overflowQueueThrottleCountDesc = "The total number of times a thread was delayed in adding a normal message to the overflow queue.";
 +    final String highPriorityQueueSizeDesc = "The number of high priority distribution messages currently waiting to be processed.";
 +    final String highPriorityQueueThrottleTimeDesc = "The total amount of time, in nanoseconds, spent delayed by the high priority queue throttle.";
 +    final String highPriorityQueueThrottleCountDesc = "The total number of times a thread was delayed in adding a normal message to the high priority queue.";
 +    final String serialQueueSizeDesc = "The number of serial distribution messages currently waiting to be processed.";
 +    final String serialQueueBytesDesc = "The approximate number of bytes consumed by serial distribution messages currently waiting to be processed.";
 +    final String serialPooledThreadDesc = "The number of threads created in the SerialQueuedExecutorPool.";
 +    final String serialQueueThrottleTimeDesc = "The total amount of time, in nanoseconds, spent delayed by the serial queue throttle.";
 +    final String serialQueueThrottleCountDesc = "The total number of times a thread was delayed in adding a ordered message to the serial queue.";
 +    final String serialThreadsDesc = "The number of threads currently processing serial/ordered messages.";
 +    final String processingThreadsDesc = "The number of threads currently processing normal messages.";
 +    final String highPriorityThreadsDesc = "The number of threads currently processing high priority messages.";
 +    final String partitionedRegionThreadsDesc = "The number of threads currently processing partitioned region messages.";
 +    final String functionExecutionThreadsDesc = "The number of threads currently processing function execution messages.";
 +    final String waitingThreadsDesc = "The number of threads currently processing messages that had to wait for a resource.";
 +    final String messageChannelTimeDesc = "The total amount of time received messages spent in the distribution channel";
++    final String udpDispatchRequestTimeDesc = "The total amount of time spent deserializing and dispatching UDP messages in the message-reader thread.";
 +    final String replyMessageTimeDesc = "The amount of time spent processing reply messages. This includes both processedMessagesTime and messageProcessingScheduleTime.";
 +    final String distributeMessageTimeDesc = "The amount of time it takes to prepare a message and send it on the network.  This includes sentMessagesTime.";
 +    final String nodesDesc = "The current number of nodes in this distributed system.";
 +    final String replyWaitsInProgressDesc = "Current number of threads waiting for a reply.";
 +    final String replyWaitsCompletedDesc = "Total number of times waits for a reply have completed.";
 +    final String replyWaitTimeDesc = "Total time spent waiting for a reply to a message.";
 +    final String replyWaitMaxTimeDesc = "Maximum time spent transmitting and then waiting for a reply to a message. See sentMessagesMaxTime for related information";
 +    final String replyTimeoutsDesc =
 +      "Total number of message replies that have timed out.";
 +    final String receiverConnectionsDesc =
 +      "Current number of sockets dedicated to receiving messages.";
 +    final String failedAcceptsDesc =
 +      "Total number of times an accept (receiver creation) of a connect from some other member has failed";
 +    final String failedConnectsDesc =
 +      "Total number of times a connect (sender creation) to some other member has failed.";
 +    final String reconnectAttemptsDesc =
 +      "Total number of times an established connection was lost and a reconnect was attempted.";
 +    final String lostConnectionLeaseDesc =
 +      "Total number of times an unshared sender socket has remained idle long enough that its lease expired.";
 +    final String sharedOrderedSenderConnectionsDesc =
 +      "Current number of shared sockets dedicated to sending ordered messages.";
 +    final String sharedUnorderedSenderConnectionsDesc =
 +      "Current number of shared sockets dedicated to sending unordered messages.";
 +    final String threadOrderedSenderConnectionsDesc =
 +      "Current number of thread sockets dedicated to sending ordered messages.";
 +    final String threadUnorderedSenderConnectionsDesc =
 +      "Current number of thread sockets dedicated to sending unordered messages.";
 +
 +    final String asyncQueuesDesc = "The current number of queues for asynchronous messaging.";
 +    final String asyncQueueFlushesInProgressDesc = "Current number of asynchronous queues being flushed.";
 +    final String asyncQueueFlushesCompletedDesc = "Total number of asynchronous queue flushes completed.";
 +    final String asyncQueueFlushTimeDesc = "Total time spent flushing asynchronous queues.";
 +    final String asyncQueueTimeoutExceededDesc = "Total number of asynchronous queues that have timed out by being blocked for more than async-queue-timeout milliseconds.";
 +    final String asyncQueueSizeExceededDesc = "Total number of asynchronous queues that have exceeded max size.";
 +    final String asyncDistributionTimeoutExceededDesc = "Total number of times the async-distribution-timeout has been exceeded during a socket write.";
 +    final String asyncQueueSizeDesc = "The current size in bytes used for asynchronous queues.";
 +    final String asyncQueuedMsgsDesc = "The total number of queued messages used for asynchronous queues.";
 +    final String asyncDequeuedMsgsDesc = "The total number of queued messages that have been removed from the queue and successfully sent.";
 +    final String asyncConflatedMsgsDesc = "The total number of queued conflated messages used for asynchronous queues.";
 +
 +    final String asyncThreadsDesc = "Total number of asynchronous message queue threads.";
 +    final String asyncThreadInProgressDesc = "Current iterations of work performed by asynchronous message queue threads.";
 +    final String asyncThreadCompletedDesc = "Total number of iterations of work performed by asynchronous message queue threads.";
 +    final String asyncThreadTimeDesc = "Total time spent by asynchronous message queue threads performing iterations.";
 +    final String receiverDirectBufferSizeDesc = "Current number of bytes allocated from direct memory as buffers for incoming messages.";
 +    final String receiverHeapBufferSizeDesc = "Current number of bytes allocated from Java heap memory as buffers for incoming messages.";
 +    final String senderDirectBufferSizeDesc = "Current number of bytes allocated from direct memory as buffers for outgoing messages.";
 +    final String senderHeapBufferSizeDesc = "Current number of bytes allocated from Java heap memory as buffers for outoing messages.";
 +
 +    final String replyHandoffTimeDesc = "Total number of seconds to switch thread contexts from processing thread to application thread.";
 +
 +    final String partitionedRegionThreadJobsDesc = "The number of messages currently being processed by partitioned region threads";
 +    final String functionExecutionThreadJobsDesc = "The number of messages currently being processed by function execution threads";
 +    final String viewThreadsDesc = "The number of threads currently processing view messages.";
 +    final String serialThreadJobsDesc = "The number of messages currently being processed by serial threads.";
 +    final String viewThreadJobsDesc = "The number of messages currently being processed by view threads.";
 +    final String serialPooledThreadJobsDesc = "The number of messages currently being processed by pooled serial processor threads.";
 +    final String processingThreadJobsDesc = "The number of messages currently being processed by pooled message processor threads.";
 +    final String highPriorityThreadJobsDesc = "The number of messages currently being processed by high priority processor threads.";
 +    final String waitingThreadJobsDesc = "The number of messages currently being processed by waiting pooly processor threads.";
 +
 +    final String eldersDesc = "Current number of system elders hosted in this member.";
 +    final String initialImageMessagesInFlightDesc = "The number of messages with initial image data sent from this member that have not yet been acknowledged.";
 +    final String initialImageRequestsInProgressDesc = "The number of initial images this member is currently receiving.";
 +
 +    //For GMSHealthMonitor
 +    final String heartbeatRequestsSentDesc = "Heartbeat request messages that this member has sent.";
 +    final String heartbeatRequestsReceivedDesc = "Heartbeat request messages that this member has received.";
 +    
 +    final String heartbeatsSentDesc = "Heartbeat messages that this member has sent.";
 +    final String heartbeatsReceivedDesc = "Heartbeat messages that this member has received.";
 +    
 +    final String suspectsSentDesc = "Suspect member messages that this member has sent.";
 +    final String suspectsReceivedDesc = "Suspect member messages that this member has received.";
 +    
 +    final String finalCheckRequestsSentDesc = "Final check requests that this member has sent.";
 +    final String finalCheckRequestsReceivedDesc = "Final check requests that this member has received.";
 +    
 +    final String finalCheckResponsesSentDesc = "Final check responses that this member has sent.";
 +    final String finalCheckResponsesReceivedDesc = "Final check responses that this member has received.";    
 +    
 +    final String tcpFinalCheckRequestsSentDesc = "TCP final check requests that this member has sent.";
 +    final String tcpFinalCheckRequestsReceivedDesc = "TCP final check requests that this member has received.";
 +    
 +    final String tcpFinalCheckResponsesSentDesc = "TCP final check responses that this member has sent.";
 +    final String tcpFinalCheckResponsesReceivedDesc = "TCP final check responses that this member has received.";
 +
 +    final String udpFinalCheckRequestsSentDesc = "UDP final check requests that this member has sent.";
 +    final String udpFinalCheckRequestsReceivedDesc = "UDP final check requests that this member has received.";
 +    
 +    final String udpFinalCheckResponsesSentDesc = "UDP final check responses that this member has sent.";
 +    final String udpFinalCheckResponsesReceivedDesc = "UDP final check responses that this member has received.";
 +
 +    StatisticsTypeFactory f = StatisticsTypeFactoryImpl.singleton();
 +
 +    type = f.createType(
 +      statName,
 +      statDescription,
 +      new StatisticDescriptor[] {
 +        f.createLongCounter("sentMessages", sentMessagesDesc, "messages"),
 +        f.createLongCounter("commitMessages", sentCommitMessagesDesc, "messages"),
 +        f.createLongCounter("commitWaits", commitWaitsDesc, "messages"),
 +        f.createLongCounter("sentMessagesTime", sentMessagesTimeDesc, "nanoseconds", false),
 +        f.createLongGauge("sentMessagesMaxTime", sentMessagesMaxTimeDesc, "milliseconds", false),
 +        f.createLongCounter("broadcastMessages", broadcastMessagesDesc, "messages"),
 +        f.createLongCounter("broadcastMessagesTime", broadcastMessagesTimeDesc, "nanoseconds", false),
 +        f.createLongCounter("receivedMessages", receivedMessagesDesc, "messages"),
 +        f.createLongCounter("receivedBytes", receivedBytesDesc, "bytes"),
 +        f.createLongCounter("sentBytes", sentBytesDesc, "bytes"),
 +        f.createLongCounter("processedMessages", processedMessagesDesc, "messages"),
 +        f.createLongCounter("processedMessagesTime", processedMessagesTimeDesc, "nanoseconds", false),
 +        f.createLongCounter("messageProcessingScheduleTime", messageProcessingScheduleTimeDesc, "nanoseconds", false),
 +        f.createIntGauge("overflowQueueSize", overflowQueueSizeDesc, "messages"),
 +        f.createIntGauge("waitingQueueSize", waitingQueueSizeDesc, "messages"),
 +        f.createIntGauge("overflowQueueThrottleCount", overflowQueueThrottleCountDesc, "delays"),
 +        f.createLongCounter("overflowQueueThrottleTime", overflowQueueThrottleTimeDesc, "nanoseconds", false),
 +        f.createIntGauge("highPriorityQueueSize", highPriorityQueueSizeDesc, "messages"),
 +        f.createIntGauge("highPriorityQueueThrottleCount", highPriorityQueueThrottleCountDesc, "delays"),
 +        f.createLongCounter("highPriorityQueueThrottleTime", highPriorityQueueThrottleTimeDesc, "nanoseconds", false),
 +        f.createIntGauge("partitionedRegionQueueSize", highPriorityQueueSizeDesc, "messages"),
 +        f.createIntGauge("partitionedRegionQueueThrottleCount", highPriorityQueueThrottleCountDesc, "delays"),
 +        f.createLongCounter("partitionedRegionQueueThrottleTime", highPriorityQueueThrottleTimeDesc, "nanoseconds", false),
 +        f.createIntGauge("functionExecutionQueueSize", highPriorityQueueSizeDesc, "messages"),
 +        f.createIntGauge("functionExecutionQueueThrottleCount", highPriorityQueueThrottleCountDesc, "delays"),
 +        f.createLongCounter("functionExecutionQueueThrottleTime", highPriorityQueueThrottleTimeDesc, "nanoseconds", false),
 +        f.createIntGauge("serialQueueSize", serialQueueSizeDesc, "messages"),
 +        f.createIntGauge("serialQueueBytes", serialQueueBytesDesc, "bytes"),
 +        f.createIntCounter("serialPooledThread", serialPooledThreadDesc, "threads"),
 +        f.createIntGauge("serialQueueThrottleCount", serialQueueThrottleCountDesc, "delays"),
 +        f.createLongCounter("serialQueueThrottleTime", serialQueueThrottleTimeDesc, "nanoseconds", false),
 +        f.createIntGauge("serialThreads", serialThreadsDesc, "threads"),
 +        f.createIntGauge("processingThreads", processingThreadsDesc, "threads"),
 +        f.createIntGauge("highPriorityThreads", highPriorityThreadsDesc, "threads"),
 +        f.createIntGauge("partitionedRegionThreads", partitionedRegionThreadsDesc, "threads"),
 +        f.createIntGauge("functionExecutionThreads", functionExecutionThreadsDesc, "threads"),
 +        f.createIntGauge("waitingThreads", waitingThreadsDesc, "threads"),
 +        f.createLongCounter("messageChannelTime", messageChannelTimeDesc, "nanoseconds", false),
++        f.createLongCounter("udpDispatchRequestTime", udpDispatchRequestTimeDesc, "nanoseconds", false),
 +        f.createLongCounter("replyMessageTime", replyMessageTimeDesc, "nanoseconds", false),
 +        f.createLongCounter("distributeMessageTime", distributeMessageTimeDesc, "nanoseconds", false),
 +        f.createIntGauge("nodes", nodesDesc, "nodes"),
 +        f.createIntGauge("replyWaitsInProgress", replyWaitsInProgressDesc, "operations"),
 +        f.createIntCounter("replyWaitsCompleted", replyWaitsCompletedDesc, "operations"),
 +        f.createLongCounter("replyWaitTime", replyWaitTimeDesc, "nanoseconds", false),
 +        f.createLongGauge("replyWaitMaxTime", replyWaitMaxTimeDesc, "milliseconds", false),
 +        f.createLongCounter("replyTimeouts", replyTimeoutsDesc, "timeouts", false),
 +        f.createIntGauge("receivers", receiverConnectionsDesc, "sockets"),
 +        f.createIntGauge("sendersSO", sharedOrderedSenderConnectionsDesc, "sockets"),
 +        f.createIntGauge("sendersSU", sharedUnorderedSenderConnectionsDesc, "sockets"),
 +        f.createIntGauge("sendersTO", threadOrderedSenderConnectionsDesc, "sockets"),
 +        f.createIntGauge("sendersTU", threadUnorderedSenderConnectionsDesc, "sockets"),
 +        f.createIntCounter("failedAccepts", failedAcceptsDesc, "accepts"),
 +        f.createIntCounter("failedConnects", failedConnectsDesc, "connects"),
 +        f.createIntCounter("reconnectAttempts", reconnectAttemptsDesc, "connects"),
 +        f.createIntCounter("senderTimeouts", lostConnectionLeaseDesc, "expirations"),
 +
 +        f.createIntGauge("syncSocketWritesInProgress", "Current number of synchronous/blocking socket write calls in progress.", "writes"),
 +        f.createLongCounter("syncSocketWriteTime", "Total amount of time, in nanoseconds, spent in synchronous/blocking socket write calls.", "nanoseconds"),
 +        f.createIntCounter("syncSocketWrites", "Total number of completed synchronous/blocking socket write calls.", "writes"),
 +        f.createLongCounter("syncSocketWriteBytes", "Total number of bytes sent out in synchronous/blocking mode on sockets.", "bytes"),
 +
 +        f.createIntCounter("ucastReads", "Total number of unicast datagrams received", "datagrams"),
 +        f.createLongCounter("ucastReadBytes", "Total number of bytes received in unicast datagrams", "bytes"),
 +        f.createIntCounter("ucastWrites", "Total number of unicast datagram socket write calls.", "writes"),
 +        f.createLongCounter("ucastWriteBytes", "Total number of bytes sent out on unicast datagram sockets.", "bytes"),
 +        f.createIntCounter("ucastRetransmits", "Total number of unicast datagram socket retransmissions", "writes"),
 +
 +        f.createIntCounter("mcastReads", "Total number of multicast datagrams received", "datagrams"),
 +        f.createLongCounter("mcastReadBytes", "Total number of bytes received in multicast datagrams", "bytes"),
 +        f.createIntCounter("mcastWrites", "Total number of multicast datagram socket write calls.", "writes"),
 +        f.createLongCounter("mcastWriteBytes", "Total number of bytes sent out on multicast datagram sockets.", "bytes"),
 +        f.createIntCounter("mcastRetransmits", "Total number of multicast datagram socket retransmissions", "writes"),
 +        f.createIntCounter("mcastRetransmitRequests", "Total number of multicast datagram socket retransmission requests sent to other processes", "requests"),
 +
 +        f.createLongCounter("serializationTime", "Total amount of time, in nanoseconds, spent serializing objects. This includes pdx serializations.", "nanoseconds"),
 +        f.createIntCounter("serializations", "Total number of object serialization calls. This includes pdx serializations.", "ops"),
 +        f.createLongCounter("serializedBytes", "Total number of bytes produced by object serialization. This includes pdx serializations.", "bytes"),
 +        f.createIntCounter("pdxSerializations", "Total number of pdx serializations.", "ops"),
 +        f.createLongCounter("pdxSerializedBytes", "Total number of bytes produced by pdx serialization.", "bytes"),
 +        f.createLongCounter("deserializationTime", "Total amount of time, in nanoseconds, spent deserializing objects. This includes deserialization that results in a PdxInstance.", "nanoseconds"),
 +        f.createIntCounter("deserializations", "Total number of object deserialization calls. This includes deserialization that results in a PdxInstance.", "ops"),
 +        f.createLongCounter("deserializedBytes", "Total number of bytes read by object deserialization. This includes deserialization that results in a PdxInstance.", "bytes"),
 +        f.createIntCounter("pdxDeserializations", "Total number of pdx deserializations.", "ops"),
 +        f.createLongCounter("pdxDeserializedBytes", "Total number of bytes read by pdx deserialization.", "bytes"),
 +        f.createLongCounter("msgSerializationTime", "Total amount of time, in nanoseconds, spent serializing messages.", "nanoseconds"),
 +        f.createLongCounter("msgDeserializationTime", "Total amount of time, in nanoseconds, spent deserializing messages.", "nanoseconds"),
 +        f.createIntCounter("pdxInstanceDeserializations", "Total number of times getObject has been called on a PdxInstance.", "ops"),
 +        f.createLongCounter("pdxInstanceDeserializationTime", "Total amount of time, in nanoseconds, spent deserializing PdxInstances by calling getObject.", "nanoseconds"),
 +        f.createIntCounter("pdxInstanceCreations", "Total number of times a deserialization created a PdxInstance.", "ops"),
 +
 +        f.createLongCounter("batchSendTime", "Total amount of time, in nanoseconds, spent queueing and flushing message batches", "nanoseconds"),
 +        f.createLongCounter("batchWaitTime", "Reserved for future use", "nanoseconds"),
 +        f.createLongCounter("batchCopyTime", "Total amount of time, in nanoseconds, spent copying messages for batched transmission", "nanoseconds"),
 +        f.createLongCounter("batchFlushTime", "Total amount of time, in nanoseconds, spent flushing batched messages to the network", "nanoseconds"),
 +
 +        f.createIntGauge("asyncSocketWritesInProgress", "Current number of non-blocking socket write calls in progress.", "writes"),
 +        f.createIntCounter("asyncSocketWrites", "Total number of non-blocking socket write calls completed.", "writes"),
 +        f.createIntCounter("asyncSocketWriteRetries", "Total number of retries needed to write a single block of data using non-blocking socket write calls.", "writes"),
 +        f.createLongCounter("asyncSocketWriteTime", "Total amount of time, in nanoseconds, spent in non-blocking socket write calls.", "nanoseconds"),
 +        f.createLongCounter("asyncSocketWriteBytes", "Total number of bytes sent out on non-blocking sockets.", "bytes"),
 +
 +        f.createLongCounter("asyncQueueAddTime", "Total amount of time, in nanoseconds, spent in adding messages to async queue.", "nanoseconds"),
 +        f.createLongCounter("asyncQueueRemoveTime", "Total amount of time, in nanoseconds, spent in removing messages from async queue.", "nanoseconds"),
 +
 +        f.createIntGauge("asyncQueues", asyncQueuesDesc, "queues"),
 +        f.createIntGauge("asyncQueueFlushesInProgress", asyncQueueFlushesInProgressDesc, "operations"),
 +        f.createIntCounter("asyncQueueFlushesCompleted", asyncQueueFlushesCompletedDesc, "operations"),
 +        f.createLongCounter("asyncQueueFlushTime", asyncQueueFlushTimeDesc, "nanoseconds", false),
 +        f.createIntCounter("asyncQueueTimeoutExceeded", asyncQueueTimeoutExceededDesc, "timeouts"),
 +        f.createIntCounter("asyncQueueSizeExceeded", asyncQueueSizeExceededDesc, "operations"),
 +        f.createIntCounter("asyncDistributionTimeoutExceeded", asyncDistributionTimeoutExceededDesc, "operations"),
 +        f.createLongGauge("asyncQueueSize", asyncQueueSizeDesc, "bytes"),
 +        f.createLongCounter("asyncQueuedMsgs", asyncQueuedMsgsDesc, "msgs"),
 +        f.createLongCounter("asyncDequeuedMsgs", asyncDequeuedMsgsDesc, "msgs"),
 +        f.createLongCounter("asyncConflatedMsgs", asyncConflatedMsgsDesc, "msgs"),
 +
 +        f.createIntGauge("asyncThreads", asyncThreadsDesc, "threads"),
 +        f.createIntGauge("asyncThreadInProgress", asyncThreadInProgressDesc, "operations"),
 +        f.createIntCounter("asyncThreadCompleted", asyncThreadCompletedDesc, "operations"),
 +        f.createLongCounter("asyncThreadTime", asyncThreadTimeDesc, "nanoseconds", false),
 +
 +        f.createLongGauge("receiversTO", "Number of receiver threads owned by non-receiver threads in other members.", "threads"),
 +        f.createLongGauge("receiversTO2", "Number of receiver threads owned in turn by receiver threads in other members", "threads"),
 +
 +        f.createLongGauge("receiverDirectBufferSize", receiverDirectBufferSizeDesc, "bytes"),
 +        f.createLongGauge("receiverHeapBufferSize", receiverHeapBufferSizeDesc, "bytes"),
 +        f.createLongGauge("senderDirectBufferSize", senderDirectBufferSizeDesc, "bytes"),
 +        f.createLongGauge("senderHeapBufferSize", senderHeapBufferSizeDesc, "bytes"),
 +        f.createIntGauge("socketLocksInProgress", "Current number of threads waiting to lock a socket", "threads", false),
 +        f.createIntCounter("socketLocks", "Total number of times a socket has been locked.", "locks"),
 +        f.createLongCounter("socketLockTime", "Total amount of time, in nanoseconds, spent locking a socket", "nanoseconds", false),
 +        f.createIntGauge("bufferAcquiresInProgress", "Current number of threads waiting to acquire a buffer", "threads", false),
 +        f.createIntCounter("bufferAcquires", "Total number of times a buffer has been acquired.", "operations"),
 +        f.createLongCounter("bufferAcquireTime", "Total amount of time, in nanoseconds, spent acquiring a socket", "nanoseconds", false),
 +
 +        f.createIntGauge("messagesBeingReceived", "Current number of message being received off the network or being processed after reception.", "messages"),
 +        f.createLongGauge("messageBytesBeingReceived", "Current number of bytes consumed by messages being received or processed.", "bytes"),
 +
 +        f.createLongCounter("serialThreadStarts", "Total number of times a thread has been created for the serial message executor.", "starts", false),
 +        f.createLongCounter("viewThreadStarts", "Total number of times a thread has been created for the view message executor.", "starts", false),
 +        f.createLongCounter("processingThreadStarts", "Total number of times a thread has been created for the pool processing normal messages.", "starts", false),
 +        f.createLongCounter("highPriorityThreadStarts", "Total number of times a thread has been created for the pool handling high priority messages.", "starts", false),
 +        f.createLongCounter("waitingThreadStarts", "Total number of times a thread has been created for the waiting pool.", "starts", false),
 +        f.createLongCounter("partitionedRegionThreadStarts", "Total number of times a thread has been created for the pool handling partitioned region messages.", "starts", false),
 +        f.createLongCounter("functionExecutionThreadStarts", "Total number of times a thread has been created for the pool handling function execution messages.", "starts", false),
 +        f.createLongCounter("serialPooledThreadStarts", "Total number of times a thread has been created for the serial pool(s).", "starts", false),
 +        f.createLongCounter("TOSentMsgs", "Total number of messages sent on thread owned senders", "messages", false),
 +        f.createLongCounter("replyHandoffTime", replyHandoffTimeDesc, "nanoseconds"),
 +
 +        f.createIntGauge("partitionedRegionThreadJobs", partitionedRegionThreadJobsDesc, "messages"),
 +        f.createIntGauge("functionExecutionThreadJobs", functionExecutionThreadJobsDesc, "messages"),
 +        f.createIntGauge("viewThreads", viewThreadsDesc, "threads"),
 +        f.createIntGauge("serialThreadJobs", serialThreadJobsDesc, "messages"),
 +        f.createIntGauge("viewThreadJobs", viewThreadJobsDesc, "messages"),
 +        f.createIntGauge("serialPooledThreadJobs", serialPooledThreadJobsDesc, "messages"),
 +        f.createIntGauge("processingThreadJobs", processingThreadJobsDesc, "messages"),
 +        f.createIntGauge("highPriorityThreadJobs", highPriorityThreadJobsDesc, "messages"),
 +        f.createIntGauge("waitingThreadJobs", waitingThreadJobsDesc, "messages"),
 +
 +        f.createIntGauge("elders", eldersDesc, "elders"),
 +        f.createIntGauge("initialImageMessagesInFlight", initialImageMessagesInFlightDesc, "messages"),
 +        f.createIntGauge("initialImageRequestsInProgress", initialImageRequestsInProgressDesc, "requests"),
 +        
 +        //For GMSHealthMonitor
 +        f.createLongCounter("heartbeatRequestsSent", heartbeatRequestsSentDesc, "messages"),
 +        f.createLongCounter("heartbeatRequestsReceived", heartbeatRequestsReceivedDesc, "messages"),
 +        f.createLongCounter("heartbeatsSent", heartbeatsSentDesc, "messages"),
 +        f.createLongCounter("heartbeatsReceived", heartbeatsReceivedDesc, "messages"),
 +        f.createLongCounter("suspectsSent", suspectsSentDesc, "messages"),
 +        f.createLongCounter("suspectsReceived", suspectsReceivedDesc, "messages"),
 +        f.createLongCounter("finalCheckRequestsSent", finalCheckRequestsSentDesc, "messages"),
 +        f.createLongCounter("finalCheckRequestsReceived", finalCheckRequestsReceivedDesc, "messages"),
 +        f.createLongCounter("finalCheckResponsesSent", finalCheckResponsesSentDesc, "messages"),
 +        f.createLongCounter("finalCheckResponsesReceived", finalCheckResponsesReceivedDesc, "messages"),
 +        f.createLongCounter("tcpFinalCheckRequestsSent", tcpFinalCheckRequestsSentDesc, "nanoseconds", false),
 +        f.createLongCounter("tcpFinalCheckRequestsReceived", tcpFinalCheckRequestsReceivedDesc, "nanoseconds", false),
 +        f.createLongCounter("tcpFinalCheckResponsesSent", tcpFinalCheckResponsesSentDesc, "nanoseconds", false),
 +        f.createLongCounter("tcpFinalCheckResponsesReceived", tcpFinalCheckResponsesReceivedDesc, "nanoseconds", false),
 +        f.createLongCounter("udpFinalCheckRequestsSent", udpFinalCheckRequestsSentDesc, "messages"),
 +        f.createLongCounter("udpFinalCheckRequestsReceived", udpFinalCheckRequestsReceivedDesc, "messages"),
 +        f.createLongCounter("udpFinalCheckResponsesSent", udpFinalCheckResponsesSentDesc, "messages"),
 +        f.createLongCounter("udpFinalCheckResponsesReceived", udpFinalCheckResponsesReceivedDesc, "messages"),
 +      }
 +    );
 +
 +    // Initialize id fields
 +    sentMessagesId = type.nameToId("sentMessages");
 +    sentCommitMessagesId = type.nameToId("commitMessages");
 +    commitWaitsId = type.nameToId("commitWaits");
 +    sentMessagesTimeId = type.nameToId("sentMessagesTime");
 +    sentMessagesMaxTimeId = type.nameToId("sentMessagesMaxTime");
 +    broadcastMessagesId = type.nameToId("broadcastMessages");
 +    broadcastMessagesTimeId = type.nameToId("broadcastMessagesTime");
 +    receivedMessagesId = type.nameToId("receivedMessages");
 +    receivedBytesId = type.nameToId("receivedBytes");
 +    sentBytesId = type.nameToId("sentBytes");
 +    processedMessagesId = type.nameToId("processedMessages");
 +    processedMessagesTimeId = type.nameToId("processedMessagesTime");
 +    messageProcessingScheduleTimeId =
 +      type.nameToId("messageProcessingScheduleTime");
 +    messageChannelTimeId = type.nameToId("messageChannelTime");
++    udpDispatchRequestTimeId = type.nameToId("udpDispatchRequestTime");
 +    replyMessageTimeId = type.nameToId("replyMessageTime");
 +    distributeMessageTimeId = type.nameToId("distributeMessageTime");
 +    nodesId = type.nameToId("nodes");
 +    overflowQueueSizeId = type.nameToId("overflowQueueSize");
 +    waitingQueueSizeId = type.nameToId("waitingQueueSize");
 +    overflowQueueThrottleTimeId = type.nameToId("overflowQueueThrottleTime");
 +    overflowQueueThrottleCountId = type.nameToId("overflowQueueThrottleCount");
 +    highPriorityQueueSizeId = type.nameToId("highPriorityQueueSize");
 +    highPriorityQueueThrottleTimeId = type.nameToId("highPriorityQueueThrottleTime");
 +    highPriorityQueueThrottleCountId = type.nameToId("highPriorityQueueThrottleCount");
 +    partitionedRegionQueueSizeId = type.nameToId("partitionedRegionQueueSize");
 +    partitionedRegionQueueThrottleTimeId = type.nameToId("partitionedRegionQueueThrottleTime");
 +    partitionedRegionQueueThrottleCountId = type.nameToId("partitionedRegionQueueThrottleCount");
 +    functionExecutionQueueSizeId = type.nameToId("functionExecutionQueueSize");
 +    functionExecutionQueueThrottleTimeId = type.nameToId("functionExecutionQueueThrottleTime");
 +    functionExecutionQueueThrottleCountId = type.nameToId("functionExecutionQueueThrottleCount");
 +    serialQueueSizeId = type.nameToId("serialQueueSize");
 +    serialQueueBytesId = type.nameToId("serialQueueBytes");
 +    serialPooledThreadId = type.nameToId("serialPooledThread");
 +    serialQueueThrottleTimeId = type.nameToId("serialQueueThrottleTime");
 +    serialQueueThrottleCountId = type.nameToId("serialQueueThrottleCount");
 +    serialThreadsId = type.nameToId("serialThreads");
 +    processingThreadsId = type.nameToId("processingThreads");
 +    highPriorityThreadsId = type.nameToId("highPriorityThreads");
 +    partitionedRegionThreadsId = type.nameToId("partitionedRegionThreads");
 +    functionExecutionThreadsId = type.nameToId("functionExecutionThreads");
 +    waitingThreadsId = type.nameToId("waitingThreads");
 +    replyWaitsInProgressId = type.nameToId("replyWaitsInProgress");
 +    replyWaitsCompletedId = type.nameToId("replyWaitsCompleted");
 +    replyWaitTimeId = type.nameToId("replyWaitTime");
 +    replyTimeoutsId = type.nameToId("replyTimeouts");
 +    replyWaitMaxTimeId = type.nameToId("replyWaitMaxTime");
 +    receiverConnectionsId = type.nameToId("receivers");
 +    failedAcceptsId = type.nameToId("failedAccepts");
 +    failedConnectsId = type.nameToId("failedConnects");
 +    reconnectAttemptsId = type.nameToId("reconnectAttempts");
 +    lostConnectionLeaseId = type.nameToId("senderTimeouts");
 +    sharedOrderedSenderConnectionsId = type.nameToId("sendersSO");
 +    sharedUnorderedSenderConnectionsId = type.nameToId("sendersSU");
 +    threadOrderedSenderConnectionsId = type.nameToId("sendersTO");
 +    threadUnorderedSenderConnectionsId = type.nameToId("sendersTU");
 +
 +    syncSocketWritesInProgressId = type.nameToId("syncSocketWritesInProgress");
 +    syncSocketWriteTimeId = type.nameToId("syncSocketWriteTime");
 +    syncSocketWritesId = type.nameToId("syncSocketWrites");
 +    syncSocketWriteBytesId = type.nameToId("syncSocketWriteBytes");
 +
 +    ucastReadsId = type.nameToId("ucastReads");
 +    ucastReadBytesId = type.nameToId("ucastReadBytes");
 +    ucastWritesId = type.nameToId("ucastWrites");
 +    ucastWriteBytesId = type.nameToId("ucastWriteBytes");
 +    ucastRetransmitsId = type.nameToId("ucastRetransmits");
 +
 +    mcastReadsId = type.nameToId("mcastReads");
 +    mcastReadBytesId = type.nameToId("mcastReadBytes");
 +    mcastWritesId = type.nameToId("mcastWrites");
 +    mcastWriteBytesId = type.nameToId("mcastWriteBytes");
 +    mcastRetransmitsId = type.nameToId("mcastRetransmits");
 +    mcastRetransmitRequestsId = type.nameToId("mcastRetransmitRequests");
 +
 +    serializationTimeId = type.nameToId("serializationTime");
 +    serializationsId = type.nameToId("serializations");
 +    serializedBytesId = type.nameToId("serializedBytes");
 +    deserializationTimeId = type.nameToId("deserializationTime");
 +    deserializationsId = type.nameToId("deserializations");
 +    deserializedBytesId = type.nameToId("deserializedBytes");
 +    pdxSerializationsId = type.nameToId("pdxSerializations");
 +    pdxSerializedBytesId = type.nameToId("pdxSerializedBytes");
 +    pdxDeserializationsId = type.nameToId("pdxDeserializations");
 +    pdxDeserializedBytesId = type.nameToId("pdxDeserializedBytes");
 +    pdxInstanceDeserializationsId = type.nameToId("pdxInstanceDeserializations");
 +    pdxInstanceDeserializationTimeId = type.nameToId("pdxInstanceDeserializationTime");
 +    pdxInstanceCreationsId = type.nameToId("pdxInstanceCreations");
 +
 +    msgSerializationTimeId = type.nameToId("msgSerializationTime");
 +    msgDeserializationTimeId = type.nameToId("msgDeserializationTime");
 +
 +    batchSendTimeId = type.nameToId("batchSendTime");
 +    batchCopyTimeId = type.nameToId("batchCopyTime");
 +    batchWaitTimeId = type.nameToId("batchWaitTime");
 +    batchFlushTimeId = type.nameToId("batchFlushTime");
 +
 +    asyncSocketWritesInProgressId = type.nameToId("asyncSocketWritesInProgress");
 +    asyncSocketWritesId = type.nameToId("asyncSocketWrites");
 +    asyncSocketWriteRetriesId = type.nameToId("asyncSocketWriteRetries");
 +    asyncSocketWriteTimeId = type.nameToId("asyncSocketWriteTime");
 +    asyncSocketWriteBytesId = type.nameToId("asyncSocketWriteBytes");
 +
 +    asyncQueueAddTimeId = type.nameToId("asyncQueueAddTime");
 +    asyncQueueRemoveTimeId = type.nameToId("asyncQueueRemoveTime");
 +
 +    asyncQueuesId = type.nameToId("asyncQueues");
 +    asyncQueueFlushesInProgressId = type.nameToId("asyncQueueFlushesInProgress");
 +    asyncQueueFlushesCompletedId = type.nameToId("asyncQueueFlushesCompleted");
 +    asyncQueueFlushTimeId = type.nameToId("asyncQueueFlushTime");
 +    asyncQueueTimeoutExceededId = type.nameToId("asyncQueueTimeoutExceeded");
 +    asyncQueueSizeExceededId = type.nameToId("asyncQueueSizeExceeded");
 +    asyncDistributionTimeoutExceededId = type.nameToId("asyncDistributionTimeoutExceeded");
 +    asyncQueueSizeId = type.nameToId("asyncQueueSize");
 +    asyncQueuedMsgsId = type.nameToId("asyncQueuedMsgs");
 +    asyncDequeuedMsgsId = type.nameToId("asyncDequeuedMsgs");
 +    asyncConflatedMsgsId = type.nameToId("asyncConflatedMsgs");
 +
 +    asyncThreadsId = type.nameToId("asyncThreads");
 +    asyncThreadInProgressId = type.nameToId("asyncThreadInProgress");
 +    asyncThreadCompletedId = type.nameToId("asyncThreadCompleted");
 +    asyncThreadTimeId = type.nameToId("asyncThreadTime");
 +
 +    threadOwnedReceiversId = type.nameToId("receiversTO");
 +    threadOwnedReceiversId2 = type.nameToId("receiversTO2");
 +
 +    receiverDirectBufferSizeId = type.nameToId("receiverDirectBufferSize");
 +    receiverHeapBufferSizeId = type.nameToId("receiverHeapBufferSize");
 +    senderDirectBufferSizeId = type.nameToId("senderDirectBufferSize");
 +    senderHeapBufferSizeId = type.nameToId("senderHeapBufferSize");
 +
 +    socketLocksInProgressId = type.nameToId("socketLocksInProgress");
 +    socketLocksId = type.nameToId("socketLocks");
 +    socketLockTimeId = type.nameToId("socketLockTime");
 +
 +    bufferAcquiresInProgressId = type.nameToId("bufferAcquiresInProgress");
 +    bufferAcquiresId = type.nameToId("bufferAcquires");
 +    bufferAcquireTimeId = type.nameToId("bufferAcquireTime");
 +    messagesBeingReceivedId = type.nameToId("messagesBeingReceived");
 +    messageBytesBeingReceivedId = type.nameToId("messageBytesBeingReceived");
 +
 +    serialThreadStartsId = type.nameToId("serialThreadStarts");
 +    viewThreadStartsId = type.nameToId("viewThreadStarts");
 +    processingThreadStartsId = type.nameToId("processingThreadStarts");
 +    highPriorityThreadStartsId = type.nameToId("highPriorityThreadStarts");
 +    waitingThreadStartsId = type.nameToId("waitingThreadStarts");
 +    partitionedRegionThreadStartsId = type.nameToId("partitionedRegionThreadStarts");
 +    functionExecutionThreadStartsId = type.nameToId("functionExecutionThreadStarts");
 +    serialPooledThreadStartsId = type.nameToId("serialPooledThreadStarts");
 +    TOSentMsgId = type.nameToId("TOSentMsgs");
 +    replyHandoffTimeId = type.nameToId("replyHandoffTime");
 +    partitionedRegionThreadJobsId = type.nameToId("partitionedRegionThreadJobs");
 +    functionExecutionThreadJobsId = type.nameToId("functionExecutionThreadJobs");
 +    viewThreadsId = type.nameToId("viewThreads");
 +    serialThreadJobsId = type.nameToId("serialThreadJobs");
 +    viewProcessorThreadJobsId = type.nameToId("viewThreadJobs");
 +    serialPooledThreadJobsId = type.nameToId("serialPooledThreadJobs");
 +    pooledMessageThreadJobsId = type.nameToId("processingThreadJobs");
 +    highPriorityThreadJobsId = type.nameToId("highPriorityThreadJobs");
 +    waitingPoolThreadJobsId = type.nameToId("waitingThreadJobs");
 +
 +    eldersId = type.nameToId("elders");
 +    initialImageMessagesInFlightId = type.nameToId("initialImageMessagesInFlight");
 +    initialImageRequestsInProgressId = type.nameToId("initialImageRequestsInProgress");
 +    
 +    //For GMSHealthMonitor
 +    heartbeatRequestsSentId = type.nameToId("heartbeatRequestsSent");
 +    heartbeatRequestsReceivedId = type.nameToId("heartbeatRequestsReceived");
 +    heartbeatsSentId = type.nameToId("heartbeatsSent");
 +    heartbeatsReceivedId = type.nameToId("heartbeatsReceived");
 +    suspectsSentId = type.nameToId("suspectsSent");
 +    suspectsReceivedId = type.nameToId("suspectsReceived");
 +    finalCheckRequestsSentId = type.nameToId("finalCheckRequestsSent");
 +    finalCheckRequestsReceivedId = type.nameToId("finalCheckRequestsReceived");
 +    finalCheckResponsesSentId = type.nameToId("finalCheckResponsesSent");
 +    finalCheckResponsesReceivedId = type.nameToId("finalCheckResponsesReceived");
 +    tcpFinalCheckRequestsSentId = type.nameToId("tcpFinalCheckRequestsSent");
 +    tcpFinalCheckRequestsReceivedId = type.nameToId("tcpFinalCheckRequestsReceived");
 +    tcpFinalCheckResponsesSentId = type.nameToId("tcpFinalCheckResponsesSent");
 +    tcpFinalCheckResponsesReceivedId = type.nameToId("tcpFinalCheckResponsesReceived");
 +    udpFinalCheckRequestsSentId = type.nameToId("udpFinalCheckRequestsSent");
 +    udpFinalCheckRequestsReceivedId = type.nameToId("udpFinalCheckRequestsReceived");
 +    udpFinalCheckResponsesSentId = type.nameToId("udpFinalCheckResponsesSent");
 +    udpFinalCheckResponsesReceivedId = type.nameToId("udpFinalCheckResponsesReceived");
 +  }
 +
 +  /** The Statistics object that we delegate most behavior to */
 +  private final Statistics stats;
 +
 +//  private final HistogramStats replyHandoffHistogram;
 +//  private final HistogramStats replyWaitHistogram;
 +
 +  ////////////////////////  Constructors  ////////////////////////
 +
 +  /**
 +   * Creates a new <code>DistributionStats</code> and registers itself
 +   * with the given statistics factory.
 +   */
 +  public DistributionStats(StatisticsFactory f, long statId) {
 +    this.stats = f.createAtomicStatistics(type, "distributionStats", statId);
 +//    this.replyHandoffHistogram = new HistogramStats("ReplyHandOff", "nanoseconds", f,
 +//        new long[] {100000, 200000, 300000, 400000, 500000, 600000, 700000, 800000, 900000, 1000000}, false);
 +//    this.replyWaitHistogram = new HistogramStats("ReplyWait", "nanoseconds", f,
 +//        new long[] {100000, 200000, 300000, 400000, 500000, 600000, 700000, 800000, 900000, 1000000}, false);
 +    Buffers.initBufferStats(this);
 +  }
 +  /**
 +   * Used by tests to create an instance given its already existings stats.
 +   */
 +  public DistributionStats(Statistics stats) {
 +    this.stats = stats;
 +//    this.replyHandoffHistogram = null;
 +//    this.replyWaitHistogram = null;
 +  }
 +
 +  /**
 +   * Returns the current NanoTime or, if clock stats are disabled, zero.
 +   * @since 5.0
 +   */
 +  public static long getStatTime() {
 +    return enableClockStats? NanoTimer.getTime() : 0;
 +  }
 +
 +  //////////////////////  Instance Methods  //////////////////////
 +
 +  public void close() {
 +    this.stats.close();
 +  }
 +
 + /**
 +   * Returns the total number of messages sent by the distribution
 +   * manager
 +   */
 +  public long getSentMessages() {
 +    return this.stats.getLong(sentMessagesId);
 +  }
 +  public void incTOSentMsg() {
 +    this.stats.incLong(TOSentMsgId, 1);
 +  }
 +
 +  public long getSentCommitMessages() {
 +    return this.stats.getLong(sentCommitMessagesId);
 +  }
 +
 +  public long getCommitWaits() {
 +    return this.stats.getLong(commitWaitsId);
 +  }
 +
 +  /**
 +   * Increments the total number of messages sent by the distribution
 +   * manager
 +   */
 +  public void incSentMessages(long messages) {
 +    this.stats.incLong(sentMessagesId, messages);
 +  }
 +  /**
 +   * Increments the total number of transactino commit messages
 +   * sent by the distribution manager
 +   */
 +  public void incSentCommitMessages(long messages) {
 +    this.stats.incLong(sentCommitMessagesId, messages);
 +  }
 +  public void incCommitWaits() {
 +    this.stats.incLong(commitWaitsId, 1);
 +  }
 +
 +  /**
 +   * Returns the total number of nanoseconds spent sending messages.
 +   */
 +  public long getSentMessagesTime() {
 +    return this.stats.getLong(sentMessagesTimeId);
 +  }
 +
 +  /**
 +   * Increments the total number of nanoseconds spend sending messages.<p>
 +   * This also sets the sentMessagesMaxTime, if appropriate
 +   */
 +  public void incSentMessagesTime(long nanos) {
 +    if (enableClockStats) {
 +      this.stats.incLong(sentMessagesTimeId, nanos);
 +      long millis = nanos / 1000000;
 +      if (getSentMessagesMaxTime() < millis) {
 +        this.stats.setLong(sentMessagesMaxTimeId, millis);
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Returns the longest time required to distribute a message, in nanos
 +   */
 +  public long getSentMessagesMaxTime() {
 +    return this.stats.getLong(sentMessagesMaxTimeId);
 +  }
 +
 +
 +  /**
 +   * Returns the total number of messages broadcast by the distribution
 +   * manager
 +   */
 +  public long getBroadcastMessages() {
 +    return this.stats.getLong(broadcastMessagesId);
 +  }
 +
 +  /**
 +   * Increments the total number of messages broadcast by the distribution
 +   * manager
 +   */
 +  public void incBroadcastMessages(long messages) {
 +    this.stats.incLong(broadcastMessagesId, messages);
 +  }
 +
 +  /**
 +   * Returns the total number of nanoseconds spent sending messages.
 +   */
 +  public long getBroadcastMessagesTime() {
 +    return this.stats.getLong(broadcastMessagesTimeId);
 +  }
 +
 +  /**
 +   * Increments the total number of nanoseconds spend sending messages.
 +   */
 +  public void incBroadcastMessagesTime(long nanos) {
 +    if (enableClockStats) {
 +      this.stats.incLong(broadcastMessagesTimeId, nanos);
 +    }
 +  }
 +
 +  /**
 +   * Returns the total number of messages received by the distribution
 +   * manager
 +   */
 +  public long getReceivedMessages() {
 +    return this.stats.getLong(receivedMessagesId);
 +  }
 +
 +  /**
 +   * Increments the total number of messages received by the distribution
 +   * manager
 +   */
 +  public void incReceivedMessages(long messages) {
 +    this.stats.incLong(receivedMessagesId, messages);
 +  }
 +
 +  /**
 +   * Returns the total number of bytes received by the distribution
 +   * manager
 +   */
 +  public long getReceivedBytes() {
 +    return this.stats.getLong(receivedBytesId);
 +  }
 +
 +  public void incReceivedBytes(long bytes) {
 +    this.stats.incLong(receivedBytesId, bytes);
 +  }
 +
 +  public void incSentBytes(long bytes) {
 +    this.stats.incLong(sentBytesId, bytes);
 +  }
 +
 +  /**
 +   * Returns the total number of messages processed by the distribution
 +   * manager
 +   */
 +  public long getProcessedMessages() {
 +    return this.stats.getLong(processedMessagesId);
 +  }
 +
 +  /**
 +   * Increments the total number of messages processed by the distribution
 +   * manager
 +   */
 +  public void incProcessedMessages(long messages) {
 +    this.stats.incLong(processedMessagesId, messages);
 +  }
 +
 +  /**
 +   * Returns the total number of nanoseconds spent processing messages.
 +   */
 +  public long getProcessedMessagesTime() {
 +    return this.stats.getLong(processedMessagesTimeId);
 +  }
 +
 +  /**
 +   * Increments the total number of nanoseconds spend processing messages.
 +   */
 +  public void incProcessedMessagesTime(long start) {
 +    if (enableClockStats) {
 +      this.stats.incLong(processedMessagesTimeId, getStatTime()-start);
 +    }
 +  }
 +
 +  /**
 +   * Returns the total number of nanoseconds spent scheduling messages to be processed.
 +   */
 +  public long getMessageProcessingScheduleTime() {
 +    return this.stats.getLong(messageProcessingScheduleTimeId);
 +  }
 +
 +  /**
 +   * Increments the total number of nanoseconds spent scheduling messages to be processed.
 +   */
 +  public void incMessageProcessingScheduleTime(long elapsed) {
 +    if (enableClockStats) {
 +      this.stats.incLong(messageProcessingScheduleTimeId, elapsed);
 +    }
 +  }
 +
 +  public int getOverflowQueueSize() {
 +    return this.stats.getInt(overflowQueueSizeId);
 +  }
 +
 +  public void incOverflowQueueSize(int messages) {
 +    this.stats.incInt(overflowQueueSizeId, messages);
 +  }
 +
 +  protected void incWaitingQueueSize(int messages) {
 +    this.stats.incInt(waitingQueueSizeId, messages);
 +  }
 +
 +  protected void incOverflowQueueThrottleCount(int delays) {
 +    this.stats.incInt(overflowQueueThrottleCountId, delays);
 +  }
 +  protected void incOverflowQueueThrottleTime(long nanos) {
 +    if (enableClockStats) {
 +      this.stats.incLong(overflowQueueThrottleTimeId, nanos);
 +    }
 +  }
 +
 +  protected void incHighPriorityQueueSize(int messages) {
 +    this.stats.incInt(highPriorityQueueSizeId, messages);
 +  }
 +  protected void incHighPriorityQueueThrottleCount(int delays) {
 +    this.stats.incInt(highPriorityQueueThrottleCountId, delays);
 +  }
 +  protected void incHighPriorityQueueThrottleTime(long nanos) {
 +    if (enableClockStats) {
 +      this.stats.incLong(highPriorityQueueThrottleTimeId, nanos);
 +    }
 +  }
 +
 +  protected void incPartitionedRegionQueueSize(int messages) {
 +    this.stats.incInt(partitionedRegionQueueSizeId, messages);
 +  }
 +  protected void incPartitionedRegionQueueThrottleCount(int delays) {
 +    this.stats.incInt(partitionedRegionQueueThrottleCountId, delays);
 +  }
 +  protected void incPartitionedRegionQueueThrottleTime(long nanos) {
 +    if (enableClockStats) {
 +      this.stats.incLong(partitionedRegionQueueThrottleTimeId, nanos);
 +    }
 +  }
 +
 +  protected void incFunctionExecutionQueueSize(int messages) {
 +    this.stats.incInt(functionExecutionQueueSizeId, messages);
 +  }
 +  protected void incFunctionExecutionQueueThrottleCount(int delays) {
 +    this.stats.incInt(functionExecutionQueueThrottleCountId, delays);
 +  }
 +  protected void incFunctionExecutionQueueThrottleTime(long nanos) {
 +    if (enableClockStats) {
 +      this.stats.incLong(functionExecutionQueueThrottleTimeId, nanos);
 +    }
 +  }
 +
 +  protected void incSerialQueueSize(int messages) {
 +    this.stats.incInt(serialQueueSizeId, messages);
 +  }
 +  protected void incSerialQueueBytes(int amount) {
 +    this.stats.incInt(serialQueueBytesId, amount);
 +  }
 +  public int getSerialQueueBytes() {
 +    return this.stats.getInt(serialQueueBytesId);
 +  }
 +  protected void incSerialPooledThread() {
 +    this.stats.incInt(serialPooledThreadId, 1);
 +  }
 +
 +  protected void incSerialQueueThrottleCount(int delays) {
 +    this.stats.incInt(serialQueueThrottleCountId, delays);
 +  }
 +  protected void incSerialQueueThrottleTime(long nanos) {
 +    if (enableClockStats) {
 +      this.stats.incLong(serialQueueThrottleTimeId, nanos);
 +    }
 +  }
 +  public int getNumProcessingThreads() {
 +    return this.stats.getInt(processingThreadsId);
 +  }
 +
 +  public void incNumProcessingThreads(int threads) {
 +    this.stats.incInt(processingThreadsId, threads);
 +  }
 +
 +  public int getNumSerialThreads() {
 +    return this.stats.getInt(serialThreadsId);
 +  }
 +
 +  public void incNumSerialThreads(int threads) {
 +    this.stats.incInt(serialThreadsId, threads);
 +  }
 +  protected void incWaitingThreads(int threads) {
 +    this.stats.incInt(waitingThreadsId, threads);
 +  }
 +
 +  protected void incHighPriorityThreads(int threads) {
 +    this.stats.incInt(highPriorityThreadsId, threads);
 +  }
 +
 +  protected void incPartitionedRegionThreads(int threads) {
 +    this.stats.incInt(partitionedRegionThreadsId, threads);
 +  }
 +
 +  protected void incFunctionExecutionThreads(int threads) {
 +    this.stats.incInt(functionExecutionThreadsId, threads);
 +  }
 +
 +  public void incMessageChannelTime(long delta) {
 +    if (enableClockStats) {
 +      this.stats.incLong(messageChannelTimeId, delta);
 +    }
 +  }
++  
++  public void incUDPDispatchRequestTime(long delta) {
++    if (enableClockStats) {
++      this.stats.incLong(udpDispatchRequestTimeId, delta);
++    }
++  }
 +
++  public long getUDPDispatchRequestTime() {
++    return this.stats.getLong(udpDispatchRequestTimeId);
++  }
++  
 +  public long getReplyMessageTime() {
 +    return this.stats.getLong(replyMessageTimeId);
 +  }
 +  public void incReplyMessageTime(long val) {
 +    if (enableClockStats) {
 +      this.stats.incLong(replyMessageTimeId, val);
 +    }
 +  }
 +
 +  public long getDistributeMessageTime() {
 +    return this.stats.getLong(distributeMessageTimeId);
 +  }
 +  public void incDistributeMessageTime(long val) {
 +    if (enableClockStats) {
 +      this.stats.incLong(distributeMessageTimeId, val);
 +    }
 +  }
 +
 +  public int getNodes() {
 +    return this.stats.getInt(nodesId);
 +  }
 +  public void setNodes(int val) {
 +    this.stats.setInt(nodesId, val);
 +  }
 +  public void incNodes(int val) {
 +    this.stats.incInt(nodesId, val);
 +  }
 +  public int getReplyWaitsInProgress() {
 +    return stats.getInt(replyWaitsInProgressId);
 +  }
 +  public int getReplyWaitsCompleted() {
 +    return stats.getInt(replyWaitsCompletedId);
 +  }
 +  public long getReplyWaitTime() {
 +    return stats.getLong(replyWaitTimeId);
 +  }
 +  public long getReplyWaitMaxTime() {
 +    return stats.getLong(replyWaitMaxTimeId);
 +  }
 +  public long startSocketWrite(boolean sync) {
 +    if (sync) {
 +      stats.incInt(syncSocketWritesInProgressId, 1);
 +    } else {
 +      stats.incInt(asyncSocketWritesInProgressId, 1);
 +    }
 +    return getStatTime();
 +  }
 +  public void endSocketWrite(boolean sync, long start, int bytesWritten, int retries) {
 +    final long now = getStatTime();
 +    if (sync) {
 +      stats.incInt(syncSocketWritesInProgressId, -1);
 +      stats.incInt(syncSocketWritesId, 1);
 +      stats.incLong(syncSocketWriteBytesId, bytesWritten);
 +      if (enableClockStats) {
 +        stats.incLong(syncSocketWriteTimeId, now-start);
 +      }
 +    } else {
 +      stats.incInt(asyncSocketWritesInProgressId, -1);
 +      stats.incInt(asyncSocketWritesId, 1);
 +      if (retries != 0) {
 +        stats.incInt(asyncSocketWriteRetriesId, retries);
 +      }
 +      stats.incLong(asyncSocketWriteBytesId, bytesWritten);
 +      if (enableClockStats) {
 +        stats.incLong(asyncSocketWriteTimeId, now-start);
 +      }
 +    }
 +  }
 +  public long startSocketLock() {
 +    stats.incInt(socketLocksInProgressId, 1);
 +    return getStatTime();
 +  }
 +  public void endSocketLock(long start) {
 +    long ts = getStatTime();
 +    stats.incInt(socketLocksInProgressId, -1);
 +    stats.incInt(socketLocksId, 1);
 +    stats.incLong(socketLockTimeId, ts-start);
 +  }
 +  public long startBufferAcquire() {
 +    stats.incInt(bufferAcquiresInProgressId, 1);
 +    return getStatTime();
 +  }
 +  public void endBufferAcquire(long start) {
 +    long ts = getStatTime();
 +    stats.incInt(bufferAcquiresInProgressId, -1);
 +    stats.incInt(bufferAcquiresId, 1);
 +    stats.incLong(bufferAcquireTimeId, ts-start);
 +  }
 +
 +  public void incUcastWriteBytes(int bytesWritten) {
 +    stats.incInt(ucastWritesId, 1);
 +    stats.incLong(ucastWriteBytesId, bytesWritten);
 +  }
 +
 +  public void incMcastWriteBytes(int bytesWritten) {
 +    stats.incInt(mcastWritesId, 1);
 +    stats.incLong(mcastWriteBytesId, bytesWritten);
 +  }
 +
 +  public int getMcastWrites() {
 +    return stats.getInt(mcastWritesId);
 +  }
 +  public int getMcastReads() {
 +    return stats.getInt(mcastReadsId);
 +  }
 +  public void incMcastReadBytes(int amount) {
 +    stats.incInt(mcastReadsId, 1);
 +    stats.incLong(mcastReadBytesId, amount);
 +  }
 +  public void incUcastReadBytes(int amount) {
 +    stats.incInt(ucastReadsId, 1);
 +    stats.incLong(ucastReadBytesId, amount);
 +  }
 +  public long startSerialization() {
 +    return getStatTime();
 +  }
 +  public void endSerialization(long start, int bytes) {
 +    if (enableClockStats) {
 +      stats.incLong(serializationTimeId, getStatTime()-start);
 +    }
 +    stats.incInt(serializationsId, 1);
 +    stats.incLong(serializedBytesId, bytes);
 +  }
 +  public long startPdxInstanceDeserialization() {
 +    return getStatTime();
 +  }
 +  public void endPdxInstanceDeserialization(long start) {
 +    if (enableClockStats) {
 +      stats.incLong(pdxInstanceDeserializationTimeId, getStatTime()-start);
 +    }
 +    stats.incInt(pdxInstanceDeserializationsId, 1);
 +  }
 +  public void incPdxSerialization(int bytes) {
 +    stats.incInt(pdxSerializationsId, 1);
 +    stats.incLong(pdxSerializedBytesId, bytes);
 +  }
 +  public void incPdxDeserialization(int bytes) {
 +    stats.incInt(pdxDeserializationsId, 1);
 +    stats.incLong(pdxDeserializedBytesId, bytes);
 +  }
 +  public void incPdxInstanceCreations() {
 +    stats.incInt(pdxInstanceCreationsId, 1);
 +  }
 +  public long startDeserialization() {
 +    return getStatTime();
 +  }
 +  public void endDeserialization(long start, int bytes) {
 +    if (enableClockStats) {
 +      stats.incLong(deserializationTimeId, getStatTime()-start);
 +    }
 +    stats.incInt(deserializationsId, 1);
 +    stats.incLong(deserializedBytesId, bytes);
 +  }
 +  public long startMsgSerialization() {
 +    return getStatTime();
 +  }
 +  public void endMsgSerialization(long start) {
 +    if (enableClockStats) {
 +      stats.incLong(msgSerializationTimeId, getStatTime()-start);
 +    }
 +  }
 +  public long startMsgDeserialization() {
 +    return getStatTime();
 +  }
 +  public void endMsgDeserialization(long start) {
 +    if (enableClockStats) {
 +      stats.incLong(msgDeserializationTimeId, getStatTime()-start);
 +    }
 +  }
 +  /**
 +   * @return the timestamp that marks the start of the operation
 +   */
 +  public long startReplyWait() {
 +    stats.incInt(replyWaitsInProgressId, 1);
 +    return getStatTime();
 +  }
 +  public void endReplyWait(long startNanos, long initTime) {
 +    if (enableClockStats) {
 +      stats.incLong(replyWaitTimeId, getStatTime()-startNanos);
 +//      this.replyWaitHistogram.endOp(delta);
 +    }
 +    if (initTime != 0) {
 +      long mswait = System.currentTimeMillis() - initTime;
 +      if (mswait > getReplyWaitMaxTime()) {
 +        stats.setLong(replyWaitMaxTimeId, mswait);
 +      }
 +    }
 +    stats.incInt(replyWaitsInProgressId, -1);
 +    stats.incInt(replyWaitsCompletedId, 1);
 +    
 +    Breadcrumbs.setSendSide(null); // clear any recipient breadcrumbs set by the message
 +    Breadcrumbs.setProblem(null); // clear out reply-wait errors
 +  }
 +
 +  public void incReplyTimeouts() {
 +    stats.incLong(replyTimeoutsId, 1L);
 +  }
 +
 +  public long getReplyTimeouts() {
 +    return stats.getLong(replyTimeoutsId);
 +  }
 +
 +  public void incReceivers() {
 +    stats.incInt(receiverConnectionsId, 1);
 +  }
 +  public void decReceivers() {
 +    stats.incInt(receiverConnectionsId, -1);
 +  }
 +  public void incFailedAccept() {
 +    stats.incInt(failedAcceptsId, 1);
 +  }
 +  public void incFailedConnect() {
 +    stats.incInt(failedConnectsId, 1);
 +  }
 +  public void incReconnectAttempts() {
 +    stats.incInt(reconnectAttemptsId, 1);
 +  }
 +  public void incLostLease() {
 +    stats.incInt(lostConnectionLeaseId, 1);
 +  }
 +  public void incSenders(boolean shared, boolean preserveOrder) {
 +    if (shared) {
 +      if (preserveOrder) {
 +        stats.incInt(sharedOrderedSenderConnectionsId, 1);
 +      } else {
 +        stats.incInt(sharedUnorderedSenderConnectionsId, 1);
 +      }
 +    } else {
 +      if (preserveOrder) {
 +        stats.incInt(threadOrderedSenderConnectionsId, 1);
 +      } else {
 +        stats.incInt(threadUnorderedSenderConnectionsId, 1);
 +      }
 +    }
 +  }
 +  public int getSendersSU() {
 +    return stats.getInt(sharedUnorderedSenderConnectionsId);
 +  }
 +  public void decSenders(boolean shared, boolean preserveOrder) {
 +    if (shared) {
 +      if (preserveOrder) {
 +        stats.incInt(sharedOrderedSenderConnectionsId, -1);
 +      } else {
 +        stats.incInt(sharedUnorderedSenderConnectionsId, -1);
 +      }
 +    } else {
 +      if (preserveOrder) {
 +        stats.incInt(threadOrderedSenderConnectionsId, -1);
 +      } else {
 +        stats.incInt(threadUnorderedSenderConnectionsId, -1);
 +      }
 +    }
 +  }
 +
 +  public int getAsyncSocketWritesInProgress() {
 +     return stats.getInt(asyncSocketWritesInProgressId);
 +  }
 +  public int getAsyncSocketWrites() {
 +     return stats.getInt(asyncSocketWritesId);
 +  }
 +  public int getAsyncSocketWriteRetries() {
 +    return stats.getInt(asyncSocketWriteRetriesId);
 +  }
 +  public long getAsyncSocketWriteBytes() {
 +     return stats.getLong(asyncSocketWriteBytesId);
 +  }
 +  public long getAsyncSocketWriteTime() {
 +     return stats.getLong(asyncSocketWriteTimeId);
 +  }
 +  public long getAsyncQueueAddTime() {
 +     return stats.getLong(asyncQueueAddTimeId);
 +  }
 +  public void incAsyncQueueAddTime(long inc) {
 +    if (enableClockStats) {
 +      stats.incLong(asyncQueueAddTimeId, inc);
 +    }
 +  }
 +  public long getAsyncQueueRemoveTime() {
 +     return stats.getLong(asyncQueueRemoveTimeId);
 +  }
 +  public void incAsyncQueueRemoveTime(long inc) {
 +    if (enableClockStats) {
 +      stats.incLong(asyncQueueRemoveTimeId, inc);
 +    }
 +  }
 +
 +  public int getAsyncQueues() {
 +     return stats.getInt(asyncQueuesId);
 +  }
 +  public void incAsyncQueues(int inc) {
 +    stats.incInt(asyncQueuesId, inc);
 +  }
 +  public int getAsyncQueueFlushesInProgress() {
 +     return stats.getInt(asyncQueueFlushesInProgressId);
 +  }
 +  public int getAsyncQueueFlushesCompleted() {
 +     return stats.getInt(asyncQueueFlushesCompletedId);
 +  }
 +  public long getAsyncQueueFlushTime() {
 +     return stats.getLong(asyncQueueFlushTimeId);
 +  }
 +  public long startAsyncQueueFlush() {
 +    stats.incInt(asyncQueueFlushesInProgressId, 1);
 +    return getStatTime();
 +  }
 +  public void endAsyncQueueFlush(long start) {
 +    stats.incInt(asyncQueueFlushesInProgressId, -1);
 +    stats.incInt(asyncQueueFlushesCompletedId, 1);
 +    if (enableClockStats) {
 +      stats.incLong(asyncQueueFlushTimeId, getStatTime()-start);
 +    }
 +  }
 +  public int getAsyncQueueTimeouts() {
 +     return stats.getInt(asyncQueueTimeoutExceededId);
 +  }
 +  public void incAsyncQueueTimeouts(int inc) {
 +    stats.incInt(asyncQueueTimeoutExceededId, inc);
 +  }
 +  public int getAsyncQueueSizeExceeded() {
 +     return stats.getInt(asyncQueueSizeExceededId);
 +  }
 +  public void incAsyncQueueSizeExceeded(int inc) {
 +    stats.incInt(asyncQueueSizeExceededId, inc);
 +  }
 +  public int getAsyncDistributionTimeoutExceeded() {
 +     return stats.getInt(asyncDistributionTimeoutExceededId);
 +  }
 +  public void incAsyncDistributionTimeoutExceeded() {
 +    stats.incInt(asyncDistributionTimeoutExceededId, 1);
 +  }
 +
 +  public long getAsyncQueueSize() {
 +     return stats.getLong(asyncQueueSizeId);
 +  }
 +  public void incAsyncQueueSize(long inc) {
 +    stats.incLong(asyncQueueSizeId, inc);
 +  }
 +  public long getAsyncQueuedMsgs() {
 +     return stats.getLong(asyncQueuedMsgsId);
 +  }
 +  public void incAsyncQueuedMsgs() {
 +    stats.incLong(asyncQueuedMsgsId, 1);
 +  }
 +  public long getAsyncDequeuedMsgs() {
 +     return stats.getLong(asyncDequeuedMsgsId);
 +  }
 +  public void incAsyncDequeuedMsgs() {
 +    stats.incLong(asyncDequeuedMsgsId, 1);
 +  }
 +  public long getAsyncConflatedMsgs() {
 +     return stats.getLong(asyncConflatedMsgsId);
 +  }
 +  public void incAsyncConflatedMsgs() {
 +    stats.incLong(asyncConflatedMsgsId, 1);
 +  }
 +
 +  public int getAsyncThreads() {
 +     return stats.getInt(asyncThreadsId);
 +  }
 +  public void incAsyncThreads(int inc) {
 +    stats.incInt(asyncThreadsId, inc);
 +  }
 +  public int getAsyncThreadInProgress() {
 +     return stats.getInt(asyncThreadInProgressId);
 +  }
 +  public int getAsyncThreadCompleted() {
 +     return stats.getInt(asyncThreadCompletedId);
 +  }
 +  public long getAsyncThreadTime() {
 +     return stats.getLong(asyncThreadTimeId);
 +  }
 +  public long startAsyncThread() {
 +    stats.incInt(asyncThreadInProgressId, 1);
 +    return getStatTime();
 +  }
 +  public void endAsyncThread(long start) {
 +    stats.incInt(asyncThreadInProgressId, -1);
 +    stats.incInt(asyncThreadCompletedId, 1);
 +    if (enableClockStats) {
 +      stats.incLong(asyncThreadTimeId, getStatTime()-start);
 +    }
 +  }
 +
 +  /**
 +   * Returns a helper object so that the overflow queue can record its
 +   * stats to the proper distribution stats.
 +   * @since 3.5
 +   */
 +  public ThrottledQueueStatHelper getOverflowQueueHelper() {
 +    return new ThrottledQueueStatHelper() {
 +        public void incThrottleCount() {
 +          incOverflowQueueThrottleCount(1);
 +        }
 +        public void throttleTime(long nanos) {
 +          incOverflowQueueThrottleTime(nanos);
 +        }
 +        public void add() {
 +          incOverflowQueueSize(1);
 +        }
 +        public void remove() {
 +          incOverflowQueueSize(-1);
 +        }
 +        public void remove(int count) {
 +          incOverflowQueueSize(-count);
 +        }
 +      };
 +  }
 +
 +  /**
 +   * Returns a helper object so that the waiting queue can record its
 +   * stats to the proper distribution stats.
 +   * @since 3.5
 +   */
 +  public QueueStatHelper getWaitingQueueHelper() {
 +    return new QueueStatHelper() {
 +        public void add() {
 +          incWaitingQueueSize(1);
 +        }
 +        public void remove() {
 +          incWaitingQueueSize(-1);
 +        }
 +        public void remove(int count) {
 +          incWaitingQueueSize(-count);
 +        }
 +      };
 +  }
 +
 +  /**
 +   * Returns a helper object so that the high priority queue can record its
 +   * stats to the proper distribution stats.
 +   * @since 3.5
 +   */
 +  public ThrottledQueueStatHelper getHighPriorityQueueHelper() {
 +    return new ThrottledQueueStatHelper() {
 +        public void incThrottleCount() {
 +          incHighPriorityQueueThrottleCount(1);
 +        }
 +        public void throttleTime(long nanos) {
 +          incHighPriorityQueueThrottleTime(nanos);
 +        }
 +        public void add() {
 +          incHighPriorityQueueSize(1);
 +        }
 +        public void remove() {
 +          incHighPriorityQueueSize(-1);
 +        }
 +        public void remove(int count) {
 +          incHighPriorityQueueSize(-count);
 +        }
 +      };
 +  }
 +
 +  /**
 +   * Returns a helper object so that the partitioned region queue can record its
 +   * stats to the proper distribution stats.
 +   * @since 5.0
 +   */
 +  public ThrottledQueueStatHelper getPartitionedRegionQueueHelper() {
 +    return new ThrottledQueueStatHelper() {
 +        public void incThrottleCount() {
 +          incPartitionedRegionQueueThrottleCount(1);
 +        }
 +        public void throttleTime(long nanos) {
 +          incPartitionedRegionQueueThrottleTime(nanos);
 +        }
 +        public void add() {
 +          incPartitionedRegionQueueSize(1);
 +        }
 +        public void remove() {
 +          incPartitionedRegionQueueSize(-1);
 +        }
 +        public void remove(int count) {
 +          incPartitionedRegionQueueSize(-count);
 +        }
 +      };
 +  }
 +  /**
 +   * Returns a helper object so that the partitioned region pool can record its
 +   * stats to the proper distribution stats.
 +   * @since 5.0.2
 +   */
 +  public PoolStatHelper getPartitionedRegionPoolHelper() {
 +    return new PoolStatHelper() {
 +        public void startJob() {
 +          incPartitionedRegionThreadJobs(1);
 +        }
 +        public void endJob() {
 +          incPartitionedRegionThreadJobs(-1);
 +        }
 +      };
 +  }
 +
 +  /**
 +   * Returns a helper object so that the function execution queue can record its
 +   * stats to the proper distribution stats.
 +   * @since 6.0
 +   */
 +  public ThrottledQueueStatHelper getFunctionExecutionQueueHelper() {
 +    return new ThrottledQueueStatHelper() {
 +        public void incThrottleCount() {
 +          incFunctionExecutionQueueThrottleCount(1);
 +        }
 +        public void throttleTime(long nanos) {
 +          incFunctionExecutionQueueThrottleTime(nanos);
 +        }
 +        public void add() {
 +          incFunctionExecutionQueueSize(1);
 +        }
 +        public void remove() {
 +          incFunctionExecutionQueueSize(-1);
 +        }
 +        public void remove(int count) {
 +          incFunctionExecutionQueueSize(-count);
 +        }
 +      };
 +  }
 +  /**
 +   * Returns a helper object so that the function execution pool can record its
 +   * stats to the proper distribution stats.
 +   * @since 6.0
 +   */
 +  public PoolStatHelper getFunctionExecutionPoolHelper() {
 +    return new PoolStatHelper() {
 +        public void startJob() {
 +          incFunctionExecutionThreadJobs(1);
 +        }
 +        public void endJob() {
 +          incFunctionExecutionThreadJobs(-1);
 +        }
 +      };
 +  }
 +
 +  /**
 +   * Returns a helper object so that the serial queue can record its
 +   * stats to the proper distribution stats.
 +   * @since 3.5
 +   */
 +  public ThrottledMemQueueStatHelper getSerialQueueHelper() {
 +    return new ThrottledMemQueueStatHelper() {
 +        public void incThrottleCount() {
 +          incSerialQueueThrottleCount(1);
 +        }
 +        public void throttleTime(long nanos) {
 +          incSerialQueueThrottleTime(nanos);
 +        }
 +        public void add() {
 +          incSerialQueueSize(1);
 +        }
 +        public void remove() {
 +          incSerialQueueSize(-1);
 +        }
 +        public void remove(int count) {
 +          incSerialQueueSize(-count);
 +        }
 +        public void addMem(int amount) {
 +          incSerialQueueBytes(amount);
 +        }
 +        public void removeMem(int amount) {
 +          incSerialQueueBytes(amount * (-1));
 +        }
 +      };
 +  }
 +
 +  /**
 +   * Returns a helper object so that the normal pool can record its
 +   * stats to the proper distribution stats.
 +   * @since 3.5
 +   */
 +  public PoolStatHelper getNormalPoolHelper() {
 +    return new PoolStatHelper() {
 +        public void startJob() {
 +          incNormalPoolThreadJobs(1);
 +        }
 +        public void endJob() {
 +          incNormalPoolThreadJobs(-1);
 +        }
 +      };
 +  }
 +
 +  /**
 +   * Returns a helper object so that the waiting pool can record its
 +   * stats to the proper distribution stats.
 +   * @since 3.5
 +   */
 +  public PoolStatHelper getWaitingPoolHelper() {
 +    return new PoolStatHelper() {
 +        public void startJob() {
 +          incWaitingPoolThreadJobs(1);
 +        }
 +        public void endJob() {
 +          incWaitingPoolThreadJobs(-1);
 +        }
 +      };
 +  }
 +
 +  /**
 +   * Returns a helper object so that the highPriority pool can record its
 +   * stats to the proper distribution stats.
 +   * @since 3.5
 +   */
 +  public PoolStatHelper getHighPriorityPoolHelper() {
 +    return new PoolStatHelper() {
 +        public void startJob() {
 +          incHighPriorityThreadJobs(1);
 +        }
 +        public void endJob() {
 +          incHighPriorityThreadJobs(-1);
 +        }
 +      };
 +  }
 +
 +  public void incBatchSendTime(long start) {
 +    if (enableClockStats) {
 +      stats.incLong(batchSendTimeId, getStatTime()-start);
 +    }
 +  }
 +  public void incBatchCopyTime(long start) {
 +    if (enableClockStats) {
 +      stats.incLong(batchCopyTimeId, getStatTime()-start);
 +    }
 +  }
 +  public void incBatchWaitTime(long start) {
 +    if (enableClockStats) {
 +      stats.incLong(batchWaitTimeId, getStatTime()-start);
 +    }
 +  }
 +  public void incBatchFlushTime(long start) {
 +    if (enableClockStats) {
 +      stats.incLong(batchFlushTimeId, getStatTime()-start);
 +    }
 +  }
 +  public void incUcastRetransmits() {
 +    stats.incInt(ucastRetransmitsId, 1);
 +  }
 +  public void incMcastRetransmits() {
 +    stats.incInt(mcastRetransmitsId, 1);
 +  }
 +  public void incMcastRetransmitRequests() {
 +    stats.incInt(mcastRetransmitRequestsId, 1);
 +  }
 +  public int getMcastRetransmits() {
 +    return stats.getInt(mcastRetransmitsId);
 +  }
 +
 +  public void incThreadOwnedReceivers(long value, int dominoCount)
 +  {
 +    if (dominoCount < 2) {
 +      stats.incLong(threadOwnedReceiversId, value);
 +    } else {
 +      stats.incLong(threadOwnedReceiversId2, value);
 +    }
 +  }
 +
 +  /**
 +   * @since 5.0.2.4
 +   */
 +  public void incReceiverBufferSize(int inc, boolean direct) {
 +    if (direct) {
 +      stats.incLong(receiverDirectBufferSizeId, inc);
 +    } else {
 +      stats.incLong(receiverHeapBufferSizeId, inc);
 +    }
 +  }
 +  /**
 +   * @since 5.0.2.4
 +   */
 +  public void incSenderBufferSize(int inc, boolean direct) {
 +    if (direct) {
 +      stats.incLong(senderDirectBufferSizeId, inc);
 +    } else {
 +      stats.incLong(senderHeapBufferSizeId, inc);
 +    }
 +  }
 +  public void incMessagesBeingReceived(boolean newMsg, int bytes) {
 +    if (newMsg) {
 +      stats.incInt(messagesBeingReceivedId, 1);
 +    }
 +    stats.incLong(messageBytesBeingReceivedId, bytes);
 +  }
 +  public void decMessagesBeingReceived(int bytes) {
 +    stats.incInt(messagesBeingReceivedId, -1);
 +    stats.incLong(messageBytesBeingReceivedId, -bytes);
 +  }
 +  public void incSerialThreadStarts() {
 +    stats.incLong(serialThreadStartsId, 1);
 +  }
 +  public void incViewThreadStarts() {
 +    stats.incLong(viewThreadStartsId, 1);
 +  }
 +  public void incProcessingThreadStarts() {
 +    stats.incLong(processingThreadStartsId, 1);
 +  }
 +  public void incHighPriorityThreadStarts() {
 +    stats.incLong(highPriorityThreadStartsId, 1);
 +  }
 +  public void incWaitingThreadStarts() {
 +    stats.incLong(waitingThreadStartsId, 1);
 +  }
 +  public void incPartitionedRegionThreadStarts() {
 +    stats.incLong(partitionedRegionThreadStartsId, 1);
 +  }
 +  public void incFunctionExecutionThreadStarts() {
 +    stats.incLong(functionExecutionThreadStartsId, 1);
 +  }
 +  public void incSerialPooledThreadStarts() {
 +    stats.incLong(serialPooledThreadStartsId, 1);
 +  }
 +
 +  public void incReplyHandOffTime(long start) {
 +    if (enableClockStats) {
 +      long delta = getStatTime() - start;
 +      stats.incLong(replyHandoffTimeId, delta);
 +//      this.replyHandoffHistogram.endOp(delta);
 +    }
 +  }
 +
 +  protected void incPartitionedRegionThreadJobs(int i) {
 +    this.stats.incInt(partitionedRegionThreadJobsId, i);
 +  }
 +
 +  protected void incFunctionExecutionThreadJobs(int i) {
 +    this.stats.incInt(functionExecutionThreadJobsId, i);
 +  }
 +
 +  public void incNumViewThreads(int threads) {
 +    this.stats.incInt(viewThreadsId, threads);
 +  }
 +
 +  public PoolStatHelper getSerialProcessorHelper() {
 +    return new PoolStatHelper() {
 +      public void startJob() {
 +        incNumSerialThreadJobs(1);
 +        if (logger.isTraceEnabled()) {
 +          logger.trace("[DM.SerialQueuedExecutor.execute] numSerialThreads={}", getNumSerialThreads());
 +        }
 +      }
 +      public void endJob() {
 +        incNumSerialThreadJobs(-1);
 +      }
 +    };
 +  }
 +
 +  protected void incNumSerialThreadJobs(int jobs) {
 +    this.stats.incInt(serialThreadJobsId, jobs);
 +  }
 +
 +  public PoolStatHelper getViewProcessorHelper() {
 +    return new PoolStatHelper() {
 +      public void startJob() {
 +        incViewProcessorThreadJobs(1);
 +        if (logger.isTraceEnabled()) {
 +          logger.trace("[DM.SerialQueuedExecutor.execute] numViewThreads={}", getNumViewThreads());
 +        }
 +      }
 +      public void endJob() {
 +        incViewProcessorThreadJobs(-1);
 +      }
 +    };
 +  }
 +
 +  public int getNumViewThreads() {
 +    return this.stats.getInt(viewThreadsId);
 +  }
 +
 +  protected void incViewProcessorThreadJobs(int jobs) {
 +    this.stats.incInt(viewProcessorT

<TRUNCATED>


[39/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/LonerDistributionManager.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/LonerDistributionManager.java
index 419c096,0000000..c958028
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/LonerDistributionManager.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/LonerDistributionManager.java
@@@ -1,997 -1,0 +1,1001 @@@
 +/*
 + * 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.distributed.internal;
 +
 +import java.io.Serializable;
 +import java.net.InetAddress;
 +import java.net.UnknownHostException;
 +import java.util.ArrayList;
 +import java.util.Collection;
 +import java.util.Collections;
 +import java.util.HashSet;
 +import java.util.Iterator;
 +import java.util.List;
 +import java.util.Map;
 +import java.util.NoSuchElementException;
 +import java.util.Set;
 +import java.util.Vector;
 +import java.util.concurrent.Callable;
 +import java.util.concurrent.ConcurrentHashMap;
 +import java.util.concurrent.ConcurrentMap;
 +import java.util.concurrent.ExecutionException;
 +import java.util.concurrent.ExecutorService;
 +import java.util.concurrent.Future;
 +import java.util.concurrent.TimeUnit;
 +import java.util.concurrent.TimeoutException;
 +
 +import com.gemstone.gemfire.CancelCriterion;
 +import com.gemstone.gemfire.InternalGemFireError;
 +import com.gemstone.gemfire.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.DurableClientAttributes;
 +import com.gemstone.gemfire.distributed.Role;
 +import com.gemstone.gemfire.distributed.internal.locks.ElderState;
 +import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
 +import com.gemstone.gemfire.distributed.internal.membership.MemberAttributes;
 +import com.gemstone.gemfire.distributed.internal.membership.MembershipManager;
 +import com.gemstone.gemfire.i18n.LogWriterI18n;
 +import com.gemstone.gemfire.internal.SocketCreator;
 +import com.gemstone.gemfire.internal.Version;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.logging.InternalLogWriter;
 +
 +/**
 + * A <code>LonerDistributionManager</code> is a dm that never communicates
 + * with anyone else.
 + *
 + * @author Darrel
 + *
 + *
 + * @since 3.5
 + */
 +public class LonerDistributionManager implements DM {
 +  private final InternalDistributedSystem system;
 +  private final InternalLogWriter logger;
 +  private ElderState elderState;
 +  
 +  
 +  ////////////////////////  Constructors  ////////////////////////
 +
 +  /**
 +   * Creates a new local distribution manager
 +   *
 +   * @param system
 +   *        The distributed system to which this distribution manager
 +   *        will send messages.
 +   *
 +   */
 +  public LonerDistributionManager(InternalDistributedSystem system,
 +                                  InternalLogWriter logger) {
 +    this.system = system;
 +    this.logger = logger;
 +    this.id = generateMemberId();
 +    this.allIds = Collections.singleton(id);
 +    this.viewMembers = new ArrayList<InternalDistributedMember>(allIds);
 +    DistributionStats.enableClockStats = this.system.getConfig().getEnableTimeStatistics();
 +  }
 +
 +  //////////////////////  Instance Methods  //////////////////////
 +
 +  protected void startThreads() {
 +    // no threads needed
 +  }
 +
 +  protected void shutdown() {
 +  }
 +
 +  private final InternalDistributedMember id;
 +
 +  /*static {
 +    // Make the id a little unique
 +    String host;
 +    try {
 +      host = InetAddress.getLocalHost().getCanonicalHostName();
 +      MemberAttributes.setDefaults(65535,
 +              com.gemstone.gemfire.internal.OSProcess.getId(),
 +              DistributionManager.LONER_DM_TYPE,
 +              MemberAttributes.parseRoles(system.getConfig().getRoles()));
 +      id = new InternalDistributedMember(host, 65535); // noise value for port number
 +
 +    } catch (UnknownHostException ex) {
 +      throw new InternalError(LocalizedStrings.LonerDistributionManager_CANNOT_RESOLVE_LOCAL_HOST_NAME_TO_AN_IP_ADDRESS.toLocalizedString());
 +    }
 +
 +  }*/
 +
 +  private final Set<InternalDistributedMember> allIds;// = Collections.singleton(id);
 +  private final List<InternalDistributedMember> viewMembers;
 +  private ConcurrentMap<InternalDistributedMember, InternalDistributedMember> canonicalIds = new ConcurrentHashMap();
 +  static private final DummyDMStats stats = new DummyDMStats();
 +  static private final DummyExecutor executor = new DummyExecutor();
 +
 +  @Override
 +  public long cacheTimeMillis() {
 +    return this.system.getClock().cacheTimeMillis();
 +  }
 +  public InternalDistributedMember getDistributionManagerId() {
 +    return id;
 +  }
 +
 +  public Set getDistributionManagerIds() {
 +    return allIds;
 +  }
 +
 +  public Set getDistributionManagerIdsIncludingAdmin() {
 +    return allIds;
 +  }
 +  public Serializable[] getDirectChannels(InternalDistributedMember[] ids) {
 +    return ids;
 +  }
 +  
 +  public InternalDistributedMember getCanonicalId(DistributedMember dmid) {
 +    InternalDistributedMember iid = (InternalDistributedMember)dmid;
 +    InternalDistributedMember result = this.canonicalIds.putIfAbsent(iid,iid);
 +    if (result != null) {
 +      return result;
 +    }
 +    return iid;
 +  }
 +
 +  public Set getOtherDistributionManagerIds() {
 +    return Collections.EMPTY_SET;
 +  }
 +  @Override
 +  public Set getOtherNormalDistributionManagerIds() {
 +    return Collections.EMPTY_SET;
 +  }
 +  public Set getAllOtherMembers() {
 +    return Collections.EMPTY_SET;
 +  }
 +  
 +  @Override // DM method
 +  public void retainMembersWithSameOrNewerVersion(Collection<InternalDistributedMember> members, Version version) {
 +    for (Iterator<InternalDistributedMember> it = members.iterator(); it.hasNext(); ) {
 +      InternalDistributedMember id = it.next();
 +      if (id.getVersionObject().compareTo(version) < 0) {
 +        it.remove();
 +      }
 +    }
 +  }
 +  
 +  @Override // DM method
 +  public void removeMembersWithSameOrNewerVersion(Collection<InternalDistributedMember> members, Version version) {
 +    for (Iterator<InternalDistributedMember> it = members.iterator(); it.hasNext(); ) {
 +      InternalDistributedMember id = it.next();
 +      if (id.getVersionObject().compareTo(version) >= 0) {
 +        it.remove();
 +      }
 +    }
 +  }
 +  
 +
 +  public Set addMembershipListenerAndGetDistributionManagerIds(MembershipListener l) {
 +    //return getOtherDistributionManagerIds();
 +    return allIds;
 +  }
 +  public Set addAllMembershipListenerAndGetAllIds(MembershipListener l) {
 +    return allIds;
 +  }
 +  public int getDistributionManagerCount() {
 +    return 0;
 +  }
 +  public InternalDistributedMember getId() {
 +    return getDistributionManagerId();
 +  }
 +  public boolean isAdam() {
 +    return true;
 +  }
 +  public InternalDistributedMember getElderId() {
 +    return getId();
 +  }
 +  public boolean isElder() {
 +    return true;
 +  }
 +  public boolean isLoner() {
 +    return true;
 +  }
 +
 +  public synchronized ElderState getElderState(boolean force, boolean useTryLock) {
 +    // loners are always the elder
 +    if (this.elderState == null) {
 +      this.elderState = new ElderState(this);
 +    }
 +    return this.elderState;
 +  }
 +
 +  public long getChannelId() {
 +    return 0;
 +  }
 +
 +  public Set putOutgoingUserData(final DistributionMessage message) {
 +    if (message.forAll() || message.getRecipients().length == 0) {
 +      // do nothing
 +      return null;
 +    } else {
 +      throw new RuntimeException(LocalizedStrings.LonerDistributionManager_LONER_TRIED_TO_SEND_MESSAGE_TO_0.toLocalizedString(message.getRecipientsDescription()));
 +    }
 +  }
 +  public InternalDistributedSystem getSystem() {
 +    return this.system;
 +  }
 +  public void addMembershipListener(MembershipListener l) {}
 +
 +  public void removeMembershipListener(MembershipListener l) {}
 +  public void removeAllMembershipListener(MembershipListener l) {}
 +
 +  public void addAdminConsole(InternalDistributedMember p_id) {}
 +
 +  public DMStats getStats() {
 +    return stats;
 +  }
 +  public DistributionConfig getConfig() {
 +    DistributionConfig result = null;
 +    if (getSystem() != null) {
 +      result = getSystem().getConfig();
 +    }
 +    return result;
 +  }
 +
 +  public void handleManagerDeparture(InternalDistributedMember p_id, 
 +      boolean crashed, String reason) {}
 +
 +  public LogWriterI18n getLoggerI18n() {
 +    return this.logger;
 +  }
 +  public InternalLogWriter getInternalLogWriter() {
 +    return this.logger;
 +  }
 +  public ExecutorService getThreadPool() {
 +    return executor;
 +  }
 +  public ExecutorService getHighPriorityThreadPool() {
 +    return executor;
 +  }
 +  public ExecutorService getWaitingThreadPool() {
 +    return executor;
 +  }
 +  public ExecutorService getPrMetaDataCleanupThreadPool() {
 +    return executor;
 +  }
 +  public Map getChannelMap() {
 +    return null;
 +  }
 +  public Map getMemberMap() {
 +    return null;
 +  }
 +  public void close() {
 +  }
 +  public void restartCommunications() {
 +
 +  }
 +  
 +  @Override
 +  public List<InternalDistributedMember> getViewMembers() {
 +    return viewMembers;
 +  }
 +
 +  public DistributedMember getOldestMember(Collection members) throws NoSuchElementException {
 +    if (members.size() == 1) {
 +      DistributedMember member = (DistributedMember)members.iterator().next();
 +      if (member.equals(viewMembers.get(0))) {
 +        return member;
 +      }
 +    }
 +    throw new NoSuchElementException(LocalizedStrings.LonerDistributionManager_MEMBER_NOT_FOUND_IN_MEMBERSHIP_SET.toLocalizedString());
 +  }
 +
 +  public Set getAdminMemberSet(){ return Collections.EMPTY_SET; }
 +
 +  public static class DummyDMStats implements DMStats {
 +    @Override
 +    public long getSentMessages() {return 0;}
 +    @Override
 +    public void incSentMessages(long messages) {}
 +    @Override
 +    public void incTOSentMsg() {}
 +    @Override
 +    public long getSentCommitMessages() {return 0;}
 +    @Override
 +    public void incSentCommitMessages(long messages) {}
 +    @Override
 +    public long getCommitWaits() {return 0;}
 +    @Override
 +    public void incCommitWaits() {}
 +    @Override
 +    public long getSentMessagesTime() {return 0;}
 +    @Override
 +    public void incSentMessagesTime(long nanos) {}
 +    @Override
 +    public long getBroadcastMessages() {return 0;}
 +    @Override
 +    public void incBroadcastMessages(long messages) {}
 +    @Override
 +    public long getBroadcastMessagesTime() {return 0;}
 +    @Override
 +    public void incBroadcastMessagesTime(long nanos) {}
 +    @Override
 +    public long getReceivedMessages() {return 0;}
 +    @Override
 +    public void incReceivedMessages(long messages) {}
 +    @Override
 +    public long getReceivedBytes() {return 0;}
 +    @Override
 +    public void incReceivedBytes(long bytes) {}
 +    @Override
 +    public void incSentBytes(long bytes) {}
 +    @Override
 +    public long getProcessedMessages() {return 0;}
 +    @Override
 +    public void incProcessedMessages(long messages) {}
 +    @Override
 +    public long getProcessedMessagesTime() {return 0;}
 +    @Override
 +    public void incProcessedMessagesTime(long nanos) {}
 +    @Override
 +    public long getMessageProcessingScheduleTime() {return 0;}
 +    @Override
 +    public void incMessageProcessingScheduleTime(long nanos) {}
 +    @Override
 +    public int getOverflowQueueSize() {return 0;}
 +    @Override
 +    public void incOverflowQueueSize(int messages) {}
 +    @Override
 +    public int getNumProcessingThreads() {return 0;}
 +    @Override
 +    public void incNumProcessingThreads(int threads) {}
 +    @Override
 +    public int getNumSerialThreads() {return 0;}
 +    @Override
 +    public void incNumSerialThreads(int threads) {}
 +    @Override
 +    public void incMessageChannelTime(long val) {}
 +    @Override
++    public void incUDPDispatchRequestTime(long val) {};
++    @Override
++    public long getUDPDispatchRequestTime() {return 0;};
++    @Override
 +    public long getReplyMessageTime() {return 0;}
 +    @Override
 +    public void incReplyMessageTime(long val) {}
 +    @Override
 +    public long getDistributeMessageTime() {return 0;}
 +    @Override
 +    public void incDistributeMessageTime(long val) {}
 +    @Override
 +    public int getNodes() {return 0;}
 +    @Override
 +    public void setNodes(int val) {}
 +    @Override
 +    public void incNodes(int val) {}
 +    @Override
 +    public int getReplyWaitsInProgress() {return 0;}
 +    @Override
 +    public int getReplyWaitsCompleted() {return 0;}
 +    @Override
 +    public long getReplyWaitTime() {return 0;}
 +    @Override
 +    public long startReplyWait() {return 0;}
 +    @Override
 +    public void endReplyWait(long startNanos, long startMillis) {}
 +    @Override
 +    public void incReplyTimeouts() { }
 +    @Override
 +    public long getReplyTimeouts() { return 0; }
 +    @Override
 +    public void incReceivers() {}
 +    @Override
 +    public void decReceivers() {}
 +    @Override
 +    public void incFailedAccept() {}
 +    @Override
 +    public void incFailedConnect() {}
 +    @Override
 +    public void incReconnectAttempts() {}
 +    @Override
 +    public void incLostLease() {}
 +    @Override
 +    public void incSenders(boolean shared, boolean preserveOrder) {}
 +    @Override
 +    public void decSenders(boolean shared, boolean preserveOrder) {}
 +    @Override
 +    public int getSendersSU() { return 0; }
 +    @Override
 +    public long startSocketWrite(boolean sync) {return 0; }
 +    @Override
 +    public void endSocketWrite(boolean sync, long start, int bytesWritten, int retries) {}
 +    @Override
 +    public long startSerialization() {return 0;}
 +    @Override
 +    public void endSerialization(long start, int bytes) {}
 +    @Override
 +    public long startDeserialization() {return 0;}
 +    @Override
 +    public void endDeserialization(long start, int bytes) {}
 +    @Override
 +    public long startMsgSerialization() {return 0;}
 +    @Override
 +    public void endMsgSerialization(long start) {}
 +    @Override
 +    public long startMsgDeserialization() {return 0;}
 +    @Override
 +    public void endMsgDeserialization(long start) {}
 +    @Override
 +    public void incBatchSendTime(long start) {}
 +    @Override
 +    public void incBatchCopyTime(long start) {}
 +    @Override
 +    public void incBatchWaitTime(long start) {}
 +    @Override
 +    public void incBatchFlushTime(long start) {}
 +    @Override
 +    public void incUcastWriteBytes(int bytesWritten) {}
 +    @Override
 +    public void incMcastWriteBytes(int bytesWritten) {}
 +    @Override
 +    public void incUcastRetransmits() {}
 +    @Override
 +    public void incMcastRetransmits() {}
 +    @Override
 +    public void incMcastRetransmitRequests() {}
 +    @Override
 +    public int getMcastRetransmits() { return 0; }
 +    @Override
 +    public int getMcastWrites() { return 0; }
 +    @Override
 +    public int getMcastReads() { return 0; }
 +    @Override
 +    public void incUcastReadBytes(int amount) {}
 +    @Override
 +    public void incMcastReadBytes(int amount) {}
 +    @Override
 +    public int getAsyncSocketWritesInProgress() {return 0;}
 +    @Override
 +    public int getAsyncSocketWrites() {return 0;}
 +    @Override
 +    public int getAsyncSocketWriteRetries() {return 0;}
 +    @Override
 +    public long getAsyncSocketWriteBytes() {return 0;}
 +    @Override
 +    public long getAsyncSocketWriteTime() {return 0;}
 +    @Override
 +    public int getAsyncQueues() {return 0;}
 +    @Override
 +    public void incAsyncQueues(int inc) {}
 +    @Override
 +    public int getAsyncQueueFlushesInProgress() {return 0;}
 +    @Override
 +    public int getAsyncQueueFlushesCompleted() {return 0;}
 +    @Override
 +    public long getAsyncQueueFlushTime() {return 0;}
 +    @Override
 +    public long startAsyncQueueFlush() {return 0;}
 +    @Override
 +    public void endAsyncQueueFlush(long start) {}
 +    @Override
 +    public int getAsyncQueueTimeouts() {return 0;}
 +    @Override
 +    public void incAsyncQueueTimeouts(int inc) {}
 +    @Override
 +    public int getAsyncQueueSizeExceeded() {return 0;}
 +    @Override
 +    public void incAsyncQueueSizeExceeded(int inc) {}
 +    @Override
 +    public int getAsyncDistributionTimeoutExceeded() {return 0;}
 +    @Override
 +    public void incAsyncDistributionTimeoutExceeded() {}
 +    @Override
 +    public long getAsyncQueueSize() {return 0;}
 +    @Override
 +    public void incAsyncQueueSize(long inc) {}
 +    @Override
 +    public long getAsyncQueuedMsgs() {return 0;}
 +    @Override
 +    public void incAsyncQueuedMsgs() {}
 +    @Override
 +    public long getAsyncDequeuedMsgs() {return 0;}
 +    @Override
 +    public void incAsyncDequeuedMsgs() {}
 +    @Override
 +    public long getAsyncConflatedMsgs() {return 0;}
 +    @Override
 +    public void incAsyncConflatedMsgs() {}
 +    @Override
 +    public int getAsyncThreads() {return 0;}
 +    @Override
 +    public void incAsyncThreads(int inc) {}
 +    @Override
 +    public int getAsyncThreadInProgress() {return 0;}
 +    @Override
 +    public int getAsyncThreadCompleted() {return 0;}
 +    @Override
 +    public long getAsyncThreadTime() {return 0;}
 +    @Override
 +    public long startAsyncThread() {return 0;}
 +    @Override
 +    public void endAsyncThread(long start) {}
 +    @Override
 +    public long getAsyncQueueAddTime() {return 0;}
 +    @Override
 +    public void incAsyncQueueAddTime(long inc) {}
 +    @Override
 +    public long getAsyncQueueRemoveTime() {return 0;}
 +    @Override
 +    public void incAsyncQueueRemoveTime(long inc) {}
 +    @Override
 +    public void incReceiverBufferSize(int inc, boolean direct) {}
 +    @Override
 +    public void incSenderBufferSize(int inc, boolean direct) {}
 +    @Override
 +    public long startSocketLock() {return 0;}
 +    @Override
 +    public void endSocketLock(long start) {}
 +    @Override
 +    public long startBufferAcquire() {return 0;}
 +    @Override
 +    public void endBufferAcquire(long start) {}
 +    @Override
 +    public void incMessagesBeingReceived(boolean newMsg, int bytes) {}
 +    @Override
 +    public void decMessagesBeingReceived(int bytes) {}
 +    @Override
 +    public void incReplyHandOffTime(long start) {}
 +    @Override
 +    public int getElders() {return 0;}
 +    @Override
 +    public void incElders(int val) {}
 +    @Override
 +    public int getInitialImageMessagesInFlight() {return 0;}
 +    @Override
 +    public void incInitialImageMessagesInFlight(int val) {}
 +    @Override
 +    public int getInitialImageRequestsInProgress() {return 0;}
 +    @Override
 +    public void incInitialImageRequestsInProgress(int val) {}
 +    @Override
 +    public void incPdxSerialization(int bytesWritten) {}
 +    @Override
 +    public void incPdxDeserialization(int i) {}
 +    @Override
 +    public long startPdxInstanceDeserialization() {return 0;}
 +    @Override
 +    public void endPdxInstanceDeserialization(long start) {}
 +    @Override
 +    public void incPdxInstanceCreations() {}
 +    @Override
 +    public void incThreadOwnedReceivers(long value, int dominoCount) {
 +    }
 +    @Override
 +    public long getHeartbeatRequestsSent() {return 0;}
 +    @Override
 +    public void incHeartbeatRequestsSent() {}
 +    @Override
 +    public long getHeartbeatRequestsReceived() {return 0;}
 +    @Override
 +    public void incHeartbeatRequestsReceived() {}
 +    @Override
 +    public long getHeartbeatsSent() {return 0;}
 +    @Override
 +    public void incHeartbeatsSent() {}
 +    @Override
 +    public long getHeartbeatsReceived() {return 0;}
 +    @Override
 +    public void incHeartbeatsReceived() {}
 +    @Override
 +    public long getSuspectsSent() {return 0;}
 +    @Override
 +    public void incSuspectsSent() {}
 +    @Override
 +    public long getSuspectsReceived() {return 0;}
 +    @Override
 +    public void incSuspectsReceived() {}
 +    @Override
 +    public long getFinalCheckRequestsSent() {return 0;}
 +    @Override
 +    public void incFinalCheckRequestsSent() {}
 +    @Override
 +    public long getFinalCheckRequestsReceived() {return 0;}
 +    @Override
 +    public void incFinalCheckRequestsReceived() {}
 +    @Override
 +    public long getFinalCheckResponsesSent() {return 0;}
 +    @Override
 +    public void incFinalCheckResponsesSent() {}
 +    @Override
 +    public long getFinalCheckResponsesReceived() {return 0;}
 +    @Override
 +    public void incFinalCheckResponsesReceived() {}
 +    @Override    
 +    public long getTcpFinalCheckRequestsSent() {return 0;}
 +    @Override
 +    public void incTcpFinalCheckRequestsSent() {}
 +    @Override
 +    public long getTcpFinalCheckRequestsReceived() {return 0;}
 +    @Override
 +    public void incTcpFinalCheckRequestsReceived() {}
 +    @Override
 +    public long getTcpFinalCheckResponsesSent() {return 0;}
 +    @Override
 +    public void incTcpFinalCheckResponsesSent() {}
 +    @Override
 +    public long getTcpFinalCheckResponsesReceived() {return 0;}
 +    @Override
 +    public void incTcpFinalCheckResponsesReceived() {}
 +    @Override
 +    public long getUdpFinalCheckRequestsSent() {return 0;}
 +    @Override
 +    public void incUdpFinalCheckRequestsSent() {}
 +    @Override
 +    public long getUdpFinalCheckResponsesReceived() {return 0;}
 +    @Override
 +    public void incUdpFinalCheckResponsesReceived() {}
 +  }
 +  protected static class DummyExecutor implements ExecutorService {
 +    @Override
 +    public void execute(Runnable command) {
 +      command.run();
 +    }
 +
 +    @Override
 +    public void shutdown() {
 +    }
 +
 +    @Override
 +    public List<Runnable> shutdownNow() {
 +      return Collections.emptyList();
 +    }
 +
 +    @Override
 +    public boolean isShutdown() {
 +      return false;
 +    }
 +
 +    @Override
 +    public boolean isTerminated() {
 +      return false;
 +    }
 +
 +    @Override
 +    public boolean awaitTermination(long timeout, TimeUnit unit)
 +        throws InterruptedException {
 +      return true;
 +    }
 +
 +    @Override
 +    public <T> Future<T> submit(final Callable<T> task) {
 +      Exception ex = null;
 +      T result = null;
 +      try {
 +        result = task.call();
 +      } catch (Exception e) {
 +        ex = e;
 +      }
 +      return new CompletedFuture<T>(result, ex);
 +    }
 +
 +    @Override
 +    public <T> Future<T> submit(final Runnable task, final T result) {
 +      return submit(new Callable<T>() {
 +        @Override
 +        public T call() throws Exception {
 +          task.run();
 +          return result;
 +        }
 +      });
 +    }
 +
 +    @Override
 +    public Future<?> submit(Runnable task) {
 +      return submit(task, null);
 +    }
 +
 +    @Override
 +    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
 +        throws InterruptedException {
 +      List<Future<T>> results = new ArrayList<Future<T>>();
 +      for (Callable<T> task : tasks) {
 +        results.add(submit(task));
 +      }
 +      return results;
 +    }
 +
 +    @Override
 +    public <T> List<Future<T>> invokeAll(
 +        Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
 +        throws InterruptedException {
 +      return invokeAll(tasks);
 +    }
 +
 +    @Override
 +    public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
 +        throws InterruptedException, ExecutionException {
 +
 +      ExecutionException ex = null;
 +      for (Callable<T> task : tasks) {
 +        try {
 +          return submit(task).get();
 +        } catch (ExecutionException e) {
 +          ex = e;
 +        }
 +      }
 +      throw (ExecutionException) ex.fillInStackTrace();
 +    }
 +
 +    @Override
 +    public <T> T invokeAny(Collection<? extends Callable<T>> tasks,
 +        long timeout, TimeUnit unit) throws InterruptedException,
 +        ExecutionException, TimeoutException {
 +      return invokeAny(tasks);
 +    }
 +  }
 +  
 +  private static class CompletedFuture<T> implements Future<T> {
 +    private final T result;
 +    private final Exception ex;
 +    
 +    public CompletedFuture(T result, Exception ex) {
 +      this.result = result;
 +      this.ex = ex;
 +    }
 +    
 +    @Override
 +    public boolean cancel(boolean mayInterruptIfRunning) {
 +      return false;
 +    }
 +
 +    @Override
 +    public boolean isCancelled() {
 +      return false;
 +    }
 +
 +    @Override
 +    public boolean isDone() {
 +      return true;
 +    }
 +
 +    @Override
 +    public T get() throws InterruptedException, ExecutionException {
 +      if (ex != null) {
 +        throw new ExecutionException(ex);
 +      }
 +      return result;
 +    }
 +
 +    @Override
 +    public T get(long timeout, TimeUnit unit) throws InterruptedException,
 +        ExecutionException, TimeoutException {
 +      return get();
 +    }
 +  }
 +  
 +  public void throwIfDistributionStopped() {
 +    stopper.checkCancelInProgress(null);
 +  }
 +
 +  /** Returns count of members filling the specified role */
 +  public int getRoleCount(Role role) {
 +    return id.getRoles().contains(role) ? 1 : 0;
 +  }
 +
 +  /** Returns true if at least one member is filling the specified role */
 +  public boolean isRolePresent(Role role) {
 +    return id.getRoles().contains(role);
 +  }
 +
 +  /** Returns a set of all roles currently in the distributed system. */
 +  public Set getAllRoles() {
 +    return id.getRoles();
 +  }
 +
 +  private int lonerPort = 0;
 +
 +  //private static final int CHARS_32KB = 16384;
 +  private InternalDistributedMember generateMemberId() {
 +    InternalDistributedMember result = null;
 +    String host;
 +    try {
 +      // create string of the current millisecond clock time
 +      StringBuffer sb = new StringBuffer();
 +      // use low four bytes for backward compatibility
 +      long time = System.currentTimeMillis() & 0xffffffffL;
 +      for (int i = 0; i < 4; i++) {
 +        String hex = Integer.toHexString((int)(time & 0xff));
 +        if (hex.length() < 2) {
 +          sb.append('0');
 +        }
 +        sb.append(hex);
 +        time = time / 0x100;
 +      }
 +      String uniqueString = sb.toString();
 +
 +      String name = this.system.getName();
 +
 +      host = SocketCreator.getLocalHost().getCanonicalHostName();
 +      DistributionConfig config = system.getConfig();
 +      DurableClientAttributes dac = null;
 +      if (config.getDurableClientId() != null) {
 +        dac = new DurableClientAttributes(config.getDurableClientId(), config
 +            .getDurableClientTimeout());
 +      }
 +      result = new InternalDistributedMember(host, lonerPort, name, uniqueString, DistributionManager.LONER_DM_TYPE,
 +          MemberAttributes.parseGroups(config.getRoles(), config.getGroups()), dac);
 +
 +    } catch (UnknownHostException ex) {
 +      throw new InternalGemFireError(LocalizedStrings.LonerDistributionManager_CANNOT_RESOLVE_LOCAL_HOST_NAME_TO_AN_IP_ADDRESS.toLocalizedString());
 +    }
 +    return result;
 +  }
 +
 +  /**
 +   * update the loner port with an integer that may be more unique than the 
 +   * default port (zero).  This updates the ID in place and establishes new
 +   * default settings for the manufacture of new IDs.
 +   * 
 +   * @param newPort the new port to use
 +   */
 +  public void updateLonerPort(int newPort) {
 +    this.lonerPort = newPort;
 +    this.getId().setPort(this.lonerPort);
 +    this.logger.config(LocalizedStrings.LonerDistributionmanager_CHANGING_PORT_FROM_TO,
 +        new Object[]{this.lonerPort, newPort, getId()});
 +  }
 +  public boolean isCurrentMember(InternalDistributedMember p_id) {
 +    return getId().equals(p_id);
 +  }
 +
 +  public Set putOutgoing(DistributionMessage msg)
 +  {
 +    return null;
 +  }
 +
 +  public boolean shutdownInProgress() {
 +    return false;
 +  }
 +
 +  public void removeUnfinishedStartup(InternalDistributedMember m, boolean departed) {
 +  }
 +
 +  public void setUnfinishedStartups(Collection s) {
 +  }
 +
 +  protected static class Stopper extends CancelCriterion {
 +
 +    @Override
 +    public String cancelInProgress() {
 +      checkFailure();
 +      return null;
 +    }
 +
 +    @Override
 +    public RuntimeException generateCancelledException(Throwable e) {
 +      return null;
 +    }
 +  }
 +  private final Stopper stopper = new Stopper();
 +
 +  public CancelCriterion getCancelCriterion() {
 +    return stopper;
 +  }
 +
 +  /* (non-Javadoc)
 +   * @see com.gemstone.gemfire.distributed.internal.DM#getMembershipManager()
 +   */
 +  public MembershipManager getMembershipManager() {
 +    // no membership manager
 +    return null;
 +  }
 +
 +  /* (non-Javadoc)
 +   * @see com.gemstone.gemfire.distributed.internal.DM#getRootCause()
 +   */
 +  public Throwable getRootCause() {
 +    return null;
 +  }
 +
 +  /* (non-Javadoc)
 +   * @see com.gemstone.gemfire.distributed.internal.DM#setRootCause(java.lang.Throwable)
 +   */
 +  public void setRootCause(Throwable t) {
 +  }
 +
 +  /* (non-Javadoc)
 +   * @see com.gemstone.gemfire.distributed.internal.DM#getMembersOnThisHost()
 +   * @since gemfire59poc
 +   */
 +  public Set<InternalDistributedMember> getMembersInThisZone() {
 +    return this.allIds;
 +  }
 +
 +  public void acquireGIIPermitUninterruptibly() {
 +  }
 +
 +  public void releaseGIIPermit() {
 +  }
 +
 +  public int getDistributedSystemId() {
 +    return getSystem().getConfig().getDistributedSystemId();
 +  }
 +
 +  public boolean enforceUniqueZone() {
 +    return system.getConfig().getEnforceUniqueHost() || system.getConfig().getRedundancyZone() != null;
 +  }
 +
 +  public boolean areInSameZone(InternalDistributedMember member1,
 +      InternalDistributedMember member2) {
 +    return false;
 +  }
 +
 +  public boolean areOnEquivalentHost(InternalDistributedMember member1,
 +                                     InternalDistributedMember member2) {
 +    return member1 == member2;
 +  }
 +  
 +  public Set<InternalDistributedMember> getMembersInSameZone(
 +      InternalDistributedMember acceptedMember) {
 +    return Collections.singleton(acceptedMember);
 +  }
 +  
 +  public Set<InetAddress> getEquivalents(InetAddress in) {
 +    Set<InetAddress> value = new HashSet<InetAddress>();
 +    value.add(this.getId().getInetAddress());
 +    return value;
 +  }
 +
 +  public Set<DistributedMember> getGroupMembers(String group) {
 +    if (getDistributionManagerId().getGroups().contains(group)) {
 +      return Collections.singleton((DistributedMember)getDistributionManagerId());
 +    } else {
 +      return Collections.emptySet();
 +    }
 +  }
 +
 +  public void addHostedLocators(InternalDistributedMember member, Collection<String> locators, boolean isSharedConfigurationEnabled) {
 +    // no-op
 +  }
 +  
 +  public Collection<String> getHostedLocators(InternalDistributedMember member) {
 +    return Collections.<String>emptyList();
 +  }
 +  
 +  public Map<InternalDistributedMember, Collection<String>> getAllHostedLocators() {
 +    return Collections.<InternalDistributedMember, Collection<String>>emptyMap();
 +  }
 +
 +  @Override
 +  public Set getNormalDistributionManagerIds() {
 +    return getDistributionManagerIds();
 +  }
 +
 +  @Override
 +  public Map<InternalDistributedMember, Collection<String>> getAllHostedLocatorsWithSharedConfiguration() {
 +    return Collections.<InternalDistributedMember, Collection<String>>emptyMap();
 +  }
 +
 +  @Override
 +  public void forceUDPMessagingForCurrentThread() {
 +    // no-op for loners
 +  }
 +
 +  @Override
 +  public void releaseUDPMessagingForCurrentThread() {
 +    // no-op for loners
 +  }
 +
 +  @Override
 +  public boolean isSharedConfigurationServiceEnabledForDS() {
 +    //return false for loner
 +    return false;
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/InternalDistributedMember.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/InternalDistributedMember.java
index 8b5c279,0000000..81058f8
mode 100755,000000..100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/InternalDistributedMember.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/InternalDistributedMember.java
@@@ -1,1304 -1,0 +1,1304 @@@
 +/*
 + * 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.distributed.internal.membership;
 +
 +import java.io.DataInput;
 +import java.io.DataOutput;
 +import java.io.EOFException;
 +import java.io.Externalizable;
 +import java.io.IOException;
 +import java.io.ObjectInput;
 +import java.io.ObjectOutput;
 +import java.net.Inet4Address;
 +import java.net.InetAddress;
 +import java.net.UnknownHostException;
 +import java.util.Arrays;
 +import java.util.Collections;
 +import java.util.HashSet;
 +import java.util.List;
 +import java.util.Set;
 +
 +import com.gemstone.gemfire.DataSerializer;
 +import com.gemstone.gemfire.InternalGemFireError;
 +import com.gemstone.gemfire.cache.UnsupportedVersionException;
 +import com.gemstone.gemfire.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.DurableClientAttributes;
 +import com.gemstone.gemfire.distributed.Role;
 +import com.gemstone.gemfire.distributed.internal.DistributionAdvisor.ProfileId;
 +import com.gemstone.gemfire.distributed.internal.DistributionManager;
 +import com.gemstone.gemfire.internal.Assert;
 +import com.gemstone.gemfire.internal.DataSerializableFixedID;
 +import com.gemstone.gemfire.internal.InternalDataSerializer;
 +import com.gemstone.gemfire.internal.OSProcess;
 +import com.gemstone.gemfire.internal.SocketCreator;
 +import com.gemstone.gemfire.internal.Version;
 +import com.gemstone.gemfire.internal.cache.versions.VersionSource;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +
 +/**
 + * This is the fundamental representation of a member of a GemFire distributed
 + * system.
 + */
- public final class InternalDistributedMember
++public class InternalDistributedMember
 + implements DistributedMember,
 +    Externalizable, DataSerializableFixedID, ProfileId,
 +    VersionSource<DistributedMember>
 +{
 +  private final static long serialVersionUID = -2785249969777296507L;
 +  
 +  // whether to show NetMember components in toString()
 +  private final boolean SHOW_NETMEMBER = Boolean.getBoolean("gemfire.show_netmembers");
 +  
 +  protected NetMember netMbr; // the underlying member object, e.g. from JGroups
 +
 +  /**
 +   * This is the direct channel port. The underlying NetMember must be able to
 +   * serialize and deliver this value.
 +   */
 +  private int dcPort = -1;
 +
 +  /**
 +   * This is the process id of this member on its machine. The underlying
 +   * NetMember must be able to serialize and deliver this value.
 +   */
 +  private int vmPid = -1;
 +
 +  /**
 +   * This is a representation of the type of VM. The underlying NetMember must
 +   * be able to serialize and deliver this value.
 +   */
 +  private int vmKind = DistributionManager.NORMAL_DM_TYPE;
 +  
 +  /**
 +   * This is the view identifier where this ID was born, or zero if this is
 +   * a loner member
 +   */
 +  private int vmViewId = -1;
 +  
 +  /**
 +   * whether this is a partial member ID (without roles, durable attributes).
 +   * We use partial IDs in EventID objects to reduce their size.  It would be
 +   * better to use canonical IDs but there is currently no central mechanism
 +   * that would allow that for both server and client identifiers
 +   */
 +  private boolean isPartial;
 +
 +  /** Internal list of group/role names for this member. */
 +  private String[] groups;
 +
 +  /**
 +   * The roles, if any, of this member. Lazily created first time getRoles()
 +   * is called.
 +   */
 +  private volatile Set<Role> rolesSet = null;
 +
 +  /** lock object used when getting/setting roles/rolesSet fields */
 +  private final Object rolesLock = new Object();
 +
 +  /**
 +   * The name of this member's distributed system connection.
 +   * @see com.gemstone.gemfire.distributed.DistributedSystem#getName
 +   */
 +  private String name = null;
 +
 +  /**
 +   * Unique tag (such as randomly generated bytes) to help enforce uniqueness.
 +   * Note: this should be displayable.
 +   */
 +  private String uniqueTag = null;
 +
 +  /** serialization bit mask */
 +  private static final int SB_ENABLED_MASK = 0x1;
 +
 +  /** serialization bit mask */
 +  private static final int COORD_ENABLED_MASK = 0x2;
 +
 +  /** partial ID bit mask */
 +  private static final int PARTIAL_ID_MASK = 0x4;
 +
 +  /** product version bit mask */
 +  private static final int VERSION_MASK = 0x8;
 +
 +  /**
 +   * Representing the host name of this member.
 +   */
 +  private String hostName = null;
 +
 +  private transient short version = Version.CURRENT_ORDINAL;
 +  private transient Version versionObj = Version.CURRENT;
 +
 +  /**
 +   * User-defined attributes (id and timeout) used by durable clients.
 +   */
 +  private DurableClientAttributes durableClientAttributes = null;
 +
 +  /** The versions in which this message was modified */
 +  private static final Version[] dsfidVersions = new Version[] {
 +        Version.GFE_71, Version.GFE_90 };
 +
 +  private void defaultToCurrentHost() {
 +    this.vmPid = OSProcess.getId();
 +    try {
 +      if (SocketCreator.resolve_dns) {
 +        this.hostName = SocketCreator.getHostName(SocketCreator.getLocalHost());
 +      }
 +      else {
 +        this.hostName = SocketCreator.getLocalHost().getHostAddress();
 +      }
 +    }
 +    catch(UnknownHostException ee){
 +      throw new InternalGemFireError(ee);
 +    }
 +    synchPayload();
 +  }
 +
 +
 +  // Used only by Externalization
 +  public InternalDistributedMember() {
 +    this.groups = new String[0];
 +  }
 +
 +  /**
 +   * Construct a InternalDistributedMember.  All fields are specified.<p>
 +   *
 +   * This, and the following constructor are the only valid ways to create an ID
 +   * for a distributed member for use
 +   * in the P2P cache.  Use of other constructors can break network-partition-detection.
 +   *
 +   * @param i
 +   * @param p
 +   *        the membership port
 +   * @param splitBrainEnabled whether this feature is enabled for the member
 +   * @param canBeCoordinator whether the member is eligible to be the membership coordinator
 +   * @param attr
 +   *        the member's attributes
 +   */
 +  public InternalDistributedMember(InetAddress i, int p, 
 +      boolean splitBrainEnabled, boolean canBeCoordinator, MemberAttributes attr) {
 +    this.dcPort = attr.getPort();
 +    this.vmPid = attr.getVmPid();
 +    this.vmKind = attr.getVmKind();
 +    this.vmViewId = attr.getVmViewId();
 +    this.name = attr.getName();
 +    this.groups = attr.getGroups();
 +    this.durableClientAttributes = attr.getDurableClientAttributes();
 +    this.netMbr = MemberFactory.newNetMember(i, p, splitBrainEnabled, canBeCoordinator, Version.CURRENT_ORDINAL, attr);
 +    this.hostName = SocketCreator.resolve_dns? SocketCreator.getHostName(i) : i.getHostAddress();
 +    this.version = netMbr.getVersionOrdinal();
 +    try {
 +      this.versionObj = Version.fromOrdinal(version, false);
 +    } catch (UnsupportedVersionException e) {
 +      this.versionObj = Version.CURRENT;
 +    }
 +//    checkHostName();
 +  }
 +
 +  
 +  /**
 +   * Construct a InternalDistributedMember based on the given NetMember.<p>
 +   * This is not the preferred way of creating an instance since the NetMember
 +   * may not have all required information (e.g., a JGroups address without
 +   * direct-port and other information).
 +   * @param m
 +   */
 +  public InternalDistributedMember(NetMember m) {
 +    netMbr = m;
 +
 +    MemberAttributes attr = m.getAttributes();
 +    this.hostName = SocketCreator.resolve_dns? SocketCreator.getHostName(m.getInetAddress()) :
 +      m.getInetAddress().getHostAddress();
 +//    checkHostName();
 +    if (attr == null) {
 +      // no extended information available, so this address is crippled
 +    }
 +    else {
 +      this.dcPort = attr.getPort();
 +      this.vmPid = attr.getVmPid();
 +      this.vmKind = attr.getVmKind();
 +      this.vmViewId = attr.getVmViewId();
 +      this.name = attr.getName();
 +      this.groups = attr.getGroups();
 +      this.durableClientAttributes = attr.getDurableClientAttributes();
 +    }
 +    this.version = m.getVersionOrdinal();
 +    try {
 +      this.versionObj = Version.fromOrdinal(version, false);
 +    } catch (UnsupportedVersionException e) {
 +      this.versionObj = Version.CURRENT;
 +    }
 +    cachedToString = null;
 +  }
 +
 +  //  private void checkHostName() {
 +//    // bug #44858: debug method to find who is putting a host name instead of addr into an ID
 +//    if (!SocketCreator.resolve_dns
 +//        && this.hostName != null && this.hostName.length() > 0
 +//        && !Character.isDigit(this.hostName.charAt(0))) {
 +//      throw new RuntimeException("found hostname that doesn't start with a digit: " + this.hostName);
 +//    }
 +//  }
 +
 +  /**
 +   * Create a InternalDistributedMember referring to the current host (as defined by the given
 +   * string).<p>
 +   *
 +   * <b>
 +   * [bruce]THIS METHOD IS FOR TESTING ONLY.  DO NOT USE IT TO CREATE IDs FOR
 +   * USE IN THE PRODUCT.  IT DOES NOT PROPERLY INITIALIZE ATTRIBUTES NEEDED
 +   * FOR P2P FUNCTIONALITY.
 +   * </b>
 +   *
 +   * 
 +   * @param i
 +   *          the hostname, must be for the current host
 +   * @param p
 +   *          the membership listening port
 +   * @throws UnknownHostException if the given hostname cannot be resolved
 +   */
 +  public InternalDistributedMember(String i, int p) throws UnknownHostException {
 +    this(i, p, Version.CURRENT);
 +  }
 +  
 +  /**
 +   * Create a InternalDistributedMember referring to the current host (as defined by the given
 +   * string).<p>
 +   *
 +   * <b>
 +   * [bruce]THIS METHOD IS FOR TESTING ONLY.  DO NOT USE IT TO CREATE IDs FOR
 +   * USE IN THE PRODUCT.  IT DOES NOT PROPERLY INITIALIZE ATTRIBUTES NEEDED
 +   * FOR P2P FUNCTIONALITY.
 +   * </b>
 +   *
 +   * 
 +   * @param i
 +   *          the hostname, must be for the current host
 +   * @param p
 +   *          the membership listening port
 +   * @param version
 +   *          the version of this member
 +   * @throws UnknownHostException if the given hostname cannot be resolved
 +   */
 +  public InternalDistributedMember(String i, int p, Version version) throws UnknownHostException {
 +    this (i, p, version, MemberFactory.newNetMember(i, p));
 +  }
 +  
 +  /**
 +   * Create a InternalDistributedMember referring to the current host (as defined by the given
 +   * string).<p>
 +   *
 +   * <b>
 +   * THIS METHOD IS FOR TESTING ONLY.  DO NOT USE IT TO CREATE IDs FOR
 +   * USE IN THE PRODUCT.  IT DOES NOT PROPERLY INITIALIZE ATTRIBUTES NEEDED
 +   * FOR P2P FUNCTIONALITY.
 +   * </b>
 +   **/
 +  public InternalDistributedMember(String i, int p, Version version, NetMember netMember) throws UnknownHostException {
 +    netMbr = netMember;
 +    defaultToCurrentHost();
 +    this.vmKind = DistributionManager.NORMAL_DM_TYPE;
 +    this.versionObj = version;
 +  }
 +
 +  /**
 +   * Create a InternalDistributedMember referring to the current host
 +   * (as defined by the given string) with additional info including optional
 +   * connection name and an optional unique string. Currently these two
 +   * optional fields (and this constructor) are only used by the
 +   * LonerDistributionManager.<p>
 +   *
 +   * < b>
 +   * [bruce]DO NOT USE THIS METHOD TO CREATE ANYTHING OTHER THAN A LONER ID
 +   * WITHOUT TALKING TO ME FIRST.  IT DOES NOT PROPERLY INITIALIZE THE ID.
 +   * </b>
 +   *
 +   * @param host
 +   *          the hostname, must be for the current host
 +   * @param p
 +   *          the membership listening port
 +   * @param n
 +   *          gemfire properties connection name
 +   * @param u
 +   *          unique string used make the member more unique
 +   * @param vmKind the dmType
 +   * @param groups the server groups / roles
 +   * @param attr durable client attributes, if any
 +   * 
 +   * @throws UnknownHostException if the given hostname cannot be resolved
 +   */
 +  public InternalDistributedMember(String host, int p, String n, String u,
 +      int vmKind, String[] groups, DurableClientAttributes attr) throws UnknownHostException {
 +    MemberAttributes mattr = new MemberAttributes(p,
 +        com.gemstone.gemfire.internal.OSProcess.getId(),
 +        vmKind, -1,
 +        n,
 +        groups, attr);
 +    InetAddress addr = SocketCreator.toInetAddress(host);
 +    netMbr = MemberFactory.newNetMember(addr, p, false, true, Version.CURRENT_ORDINAL, mattr);
 +    defaultToCurrentHost();
 +    this.name = n;
 +    this.uniqueTag = u;
 +    this.vmKind = vmKind;
 +    this.dcPort = p;
 +    this.durableClientAttributes = attr;
 +    this.hostName = host;
 +    this.vmPid = OSProcess.getId();
 +    this.groups = groups;
 +  }
 +
 +  /**
 +   * Create a InternalDistributedMember  referring to the current host (as defined by the given
 +   * address).<p>
 +   *
 +   * <b>
 +   * [bruce]THIS METHOD IS FOR TESTING ONLY.  DO NOT USE IT TO CREATE IDs FOR
 +   * USE IN THE PRODUCT.  IT DOES NOT PROPERLY INITIALIZE ATTRIBUTES NEEDED
 +   * FOR P2P FUNCTIONALITY.
 +   * </b>
 +   *
 +   * 
 +   * @param i
 +   *          the hostname, must be for the current host
 +   * @param p
 +   *          the membership listening port
 +   */
 +  public InternalDistributedMember(InetAddress i, int p) {
 +    netMbr = MemberFactory.newNetMember(i, p);
 +    defaultToCurrentHost();
 +  }
 +
 +  /**
 +   * Create a InternalDistributedMember as defined by the given address.
 +   * <p>
 +   * 
 +   * <b>
 +   * [bruce]THIS METHOD IS FOR TESTING ONLY.  DO NOT USE IT TO CREATE IDs FOR
 +   * USE IN THE PRODUCT.  IT DOES NOT PROPERLY INITIALIZE ATTRIBUTES NEEDED
 +   * FOR P2P FUNCTIONALITY.
 +   * </b>
 +   * 
 +   * @param addr 
 +   *        address of the server
 +   * @param p
 +   *        the listening port of the server
 +   * @param isCurrentHost
 +   *        true if the given host refers to the current host (bridge and
 +   *        gateway use false to create a temporary id for the OTHER side of a
 +   *        connection)
 +   */
 +  public InternalDistributedMember(InetAddress addr,
 +                                   int p,
 +                                   boolean isCurrentHost) {
 +    netMbr = MemberFactory.newNetMember(addr, p);
 +    if (isCurrentHost) {
 +      defaultToCurrentHost();
 +    }
 +  }
 +
 +  /**
 +   * Return the underlying host address
 +   *
 +   * @return the underlying host address
 +   */
 +  public InetAddress getInetAddress()
 +  {
 +    return netMbr.getInetAddress();
 +  }
 +
 +  public NetMember getNetMember() {
 +    return netMbr;
 +  }
 +
 +  /**
 +   * Return the underlying port (membership port)
 +   * @return the underlying membership port
 +   */
 +  public int getPort()
 +  {
 +    return netMbr.getPort();
 +  }
 +
 +
 +  /**
 +   * Returns the port on which the direct channel runs
 +   */
 +  public int getDirectChannelPort()
 +  {
 +    assert !this.isPartial;
 +    return dcPort;
 +  }
 +
 +  /**
 +   * [GemStone] Returns the kind of VM that hosts the distribution manager with
 +   * this address.
 +   *
 +   * @see com.gemstone.gemfire.distributed.internal.DistributionManager#getDMType()
 +   * @see com.gemstone.gemfire.distributed.internal.DistributionManager#NORMAL_DM_TYPE
 +   */
 +  public int getVmKind()
 +  {
 +    return vmKind;
 +  }
 +  
 +  /**
 +   * Returns the membership view ID that this member was born in. For
 +   * backward compatibility reasons this is limited to 16 bits.
 +   */
 +  public int getVmViewId() {
 +    return this.vmViewId;
 +  }
 +
 +  /**
 +   * Returns an unmodifiable Set of this member's Roles.
 +   */
 +  public Set<Role> getRoles() {
 +    Set<Role> tmpRolesSet = this.rolesSet;
 +    if (tmpRolesSet != null) {
 +      return tmpRolesSet;
 +    }
 +    assert !this.isPartial;
 +    synchronized (this.rolesLock) {
 +      tmpRolesSet = this.rolesSet;
 +      if (tmpRolesSet == null) {
 +        final String[] tmpRoles = this.groups;
 +        // convert array of string role names to array of Roles...
 +        if (tmpRoles == null  ||  tmpRoles.length == 0) {
 +          tmpRolesSet = Collections.emptySet();
 +        }
 +        else {
 +          tmpRolesSet = new HashSet<Role>(tmpRoles.length);
 +          for (int i = 0; i < tmpRoles.length; i++) {
 +            tmpRolesSet.add(InternalRole.getRole(tmpRoles[i]));
 +          }
 +          tmpRolesSet = Collections.unmodifiableSet(tmpRolesSet);
 +        }
 +        this.rolesSet = tmpRolesSet;
 +      }
 +    }
 +    Assert.assertTrue(tmpRolesSet != null);
 +    return tmpRolesSet;
 +  }
 +  public List<String> getGroups() {
 +    return Collections.unmodifiableList(Arrays.asList(this.groups));
 +  }
 +
 +  public void setGroups(String[] newGroups) {
 +    assert !this.isPartial;
 +    assert newGroups != null;
 +    synchronized (this.rolesLock) {
 +      this.groups = newGroups;
 +      synchPayload();
 +      this.rolesSet = null;
 +      this.cachedToString = null;
 +    }
 +  }
 +
 +  private void synchPayload() {
 +    netMbr.setAttributes(new MemberAttributes(dcPort, vmPid, vmKind, 
 +        vmViewId, name, groups, durableClientAttributes));
 +  }
 +
 +  public void setVmKind(int p)
 +  {
 +    vmKind = p;
 +    synchPayload();
 +    cachedToString = null;
 +  }
 +  
 +  public void setVmViewId(int p) {
 +    this.vmViewId = p;
 +    synchPayload();
 +    cachedToString = null;
 +  }
 +
 +  /**
 +   * [GemStone] Returns the process id of the VM that hosts the distribution
 +   * manager with this address.
 +   *
 +   * @since 4.0
 +   */
 +  public int getVmPid()
 +  {
 +    return vmPid;
 +  }
 +
 +  /**
 +   * [GemStone] Sets the process id of the VM that hosts the distribution
 +   * manager with this address.
 +   *
 +   * @since 4.0
 +   */
 +  public void setVmPid(int p)
 +  {
 +    this.vmPid = p;
 +    synchPayload();
 +    cachedToString = null;
 +  }
 +
 +  /**
 +   * Returns the name of this member's distributed system connection or null
 +   * if no name was specified.
 +   * @see com.gemstone.gemfire.distributed.DistributedSystem#getName
 +   */
 +  public String getName() {
 +    String result = this.name;
 +    if (result == null) {
 +      result = "";
 +    }
 +    return result;
 +  }
 +
 +  /**
 +   * Returns this member's unique tag (such as randomly generated bytes) or
 +   * null if no unique tag was created.
 +   */
 +  public String getUniqueTag() {
 +    return this.uniqueTag;
 +  }
 +
 +  /**
 +   * Returns this client member's durable attributes or null if no durable
 +   * attributes were created.
 +   */
 +  public DurableClientAttributes getDurableClientAttributes() {
 +    assert !this.isPartial;
 +    return this.durableClientAttributes;
 +  }
 +
 +  /**
 +   * implements the java.lang.Comparable interface
 +   *
 +   * @see java.lang.Comparable
 +   * @param o -
 +   *          the Object to be compared
 +   * @return a negative integer, zero, or a positive integer as this object is
 +   *         less than, equal to, or greater than the specified object.
 +   * @exception java.lang.ClassCastException -
 +   *              if the specified object's type prevents it from being compared
 +   *              to this Object.
 +   */
 +  public int compareTo(DistributedMember o) {
 +    return compareTo(o, true);
 +  }
 +  
 +  public int compareTo(DistributedMember o, boolean checkNetMembersIfEqual) {
 +    if (this == o) {
 +      return 0;
 +    }
 +    // obligatory type check
 +    if ((o == null) || !(o instanceof InternalDistributedMember))
 +      throw new ClassCastException(LocalizedStrings.InternalDistributedMember_INTERNALDISTRIBUTEDMEMBERCOMPARETO_COMPARISON_BETWEEN_DIFFERENT_CLASSES.toLocalizedString());
 +    InternalDistributedMember other = (InternalDistributedMember)o;
 +
 +    int myPort = getPort();
 +    int otherPort = other.getPort();
 +    if (myPort < otherPort)
 +      return -1;
 +    if (myPort > otherPort)
 +      return 1;
 +
 +
 +    InetAddress myAddr = getInetAddress();
 +    InetAddress otherAddr = other.getInetAddress();
 +
 +    // Discard null cases
 +    if (myAddr == null && otherAddr == null) {
 +      if (myPort < otherPort)
 +        return -1;
 +      else if (myPort > otherPort)
 +        return 1;
 +      else
 +        return 0;
 +    }
 +    else if (myAddr == null) {
 +      return -1;
 +    }
 +    else if (otherAddr == null)
 +      return 1;
 +
 +    byte[] myBytes = myAddr.getAddress();
 +    byte[] otherBytes = otherAddr.getAddress();
 +
 +    if (myBytes != otherBytes) {
 +      for (int i = 0; i < myBytes.length; i++) {
 +        if (i >= otherBytes.length)
 +          return -1; // same as far as they go, but shorter...
 +        if (myBytes[i] < otherBytes[i])
 +          return -1;
 +        if (myBytes[i] > otherBytes[i])
 +          return 1;
 +      }
 +      if (myBytes.length > otherBytes.length)
 +        return 1; // same as far as they go, but longer...
 +    }
 +
 +    if (this.name == null && other.name == null) {
 +      // do nothing
 +    } else if (this.name == null) {
 +      return -1;
 +    }
 +    else if (other.name == null) {
 +      return 1;
 +    }
 +    else {
 +      int i = this.name.compareTo(other.name);
 +      if (i != 0) {
 +        return i;
 +      }
 +    }
 +
 +    if (this.uniqueTag == null && other.uniqueTag == null) {
 +      // not loners, so look at P2P view ID
 +      if (this.vmViewId >= 0 && other.vmViewId >= 0) {
 +        if (this.vmViewId < other.vmViewId) {
 +          return -1;
 +        } else if (this.vmViewId > other.vmViewId) {
 +          return 1;
 +        } // else they're the same, so continue
 +      }
 +    } else if (this.uniqueTag == null) {
 +      return -1;
 +    }
 +    else if (other.uniqueTag == null) {
 +      return 1;
 +    }
 +    else {
 +      int i = this.uniqueTag.compareTo(other.uniqueTag);
 +      if (i != 0) {
 +        return i;
 +      }
 +    }
 +    
 +    if (checkNetMembersIfEqual
 +        && this.netMbr != null && other.netMbr != null) {
 +      return this.netMbr.compareTo(other.netMbr);
 +    } else {
 +      return 0;
 +    }
 +    
 +    // purposely avoid comparing roles
 +    // @todo Add durableClientAttributes to compare
 +  }
 +
 +  @Override
 +  public boolean equals(Object obj)
 +  {
 +    if (this == obj) {
 +      return true;
 +    }
 +    // GemStone fix for 29125
 +    if ((obj == null) || !(obj instanceof InternalDistributedMember)) {
 +      return false;
 +    }
 +    return compareTo((InternalDistributedMember)obj) == 0;
 +  }
 +
 +  @Override
 +  public int hashCode()
 +  {
 +    int result = 0;
 +     result = result + netMbr.getInetAddress().hashCode();
 +    result = result + getPort();
 +    return result;
 +  }
 +
 +  private String shortName(String hostname)
 +  {
 +    if (hostname == null)
 +      return "<null inet_addr hostname>";
 +    int index = hostname.indexOf('.');
 +
 +    if (index > 0 && !Character.isDigit(hostname.charAt(0)))
 +      return hostname.substring(0, index);
 +    else
 +      return hostname;
 +  }
 +
 +
 +  /** the cached string description of this object */
 +  private transient String cachedToString;
 +
 +  @Override
 +  public String toString()
 +  {
 +    String result = cachedToString;
 +    if (result == null) {
 +      String host;
 +
 +      InetAddress add = getInetAddress();
 +        if (add.isMulticastAddress())
 +          host = add.getHostAddress();
 +        else {
 +         // host = shortName(add.getHostName());
 +          host = SocketCreator.resolve_dns? shortName(this.hostName) : this.hostName;
 +        }
 +      final StringBuilder sb = new StringBuilder();
 +
 +      sb.append(host);
 +
 +      String myName = getName();
 +      if (vmPid > 0 || vmKind != DistributionManager.NORMAL_DM_TYPE || !"".equals(myName)) {
 +        sb.append("(");
 +
 +        if (!"".equals(myName)) {
 +          sb.append(myName);
 +          if (vmPid > 0) {
 +            sb.append(':');
 +          }
 +        }
 +
 +        if (vmPid > 0)
 +          sb.append(Integer.toString(vmPid));
 +
 +        String vmStr = "";
 +        switch (vmKind) {
 +        case DistributionManager.NORMAL_DM_TYPE:
 +  //        vmStr = ":local"; // let this be silent
 +          break;
 +        case DistributionManager.LOCATOR_DM_TYPE:
 +          vmStr = ":locator";
 +          break;
 +        case DistributionManager.ADMIN_ONLY_DM_TYPE:
 +          vmStr = ":admin";
 +          break;
 +        case DistributionManager.LONER_DM_TYPE:
 +          vmStr = ":loner";
 +          break;
 +        default:
 +          vmStr = ":<unknown:" + vmKind + ">";
 +          break;
 +        }
 +        sb.append(vmStr);
 +        sb.append(")");
 +      }
 +      if (vmKind != DistributionManager.LONER_DM_TYPE && netMbr.preferredForCoordinator()) {
 +        sb.append("<ec>");
 +      }
 +      if (this.vmViewId >= 0) {
 +        sb.append("<v" + this.vmViewId + ">");
 +      }
 +      sb.append(":");
 +      sb.append(getPort());
 +
 +//      if (dcPort > 0 && vmKind != DistributionManager.LONER_DM_TYPE) {
 +//        sb.append("/");
 +//        sb.append(Integer.toString(dcPort));
 +//      }
 +
 +      if (vmKind == DistributionManager.LONER_DM_TYPE) {
 +        // add some more info that was added in 4.2.1 for loner bridge clients
 +        // impact on non-bridge loners is ok
 +        if (this.uniqueTag != null && this.uniqueTag.length() != 0) {
 +          sb.append(":").append(this.uniqueTag);
 +        }
 +        if (this.name != null && this.name.length() != 0) {
 +          sb.append(":").append(this.name);
 +        }
 +      }
 +
 +      // add version if not current
 +      if (this.version != Version.CURRENT.ordinal()) {
 +        sb.append("(version:").append(Version.toString(this.version))
 +            .append(')');
 +      }
 +
 +      if (SHOW_NETMEMBER) {
 +        sb.append("[[").append(this.netMbr).append("]]");
 +      }
 +
 +      // leave out Roles on purpose
 +      
 +//      if (netMbr instanceof GMSMember) {
 +//        sb.append("(UUID=").append(((GMSMember)netMbr).getUUID()).append(")");
 +//      }
 +
 +      result = sb.toString();
 +      cachedToString = result;
 +    }
 +    return result;
 +  }
 +
 +  private void readVersion(int flags, DataInput in) throws IOException {
 +    if ((flags & VERSION_MASK) != 0) {
 +      this.version = Version.readOrdinal(in);
 +      this.versionObj = Version.fromOrdinalNoThrow(this.version, false);
 +    } else {
 +      // prior to 7.1 member IDs did not serialize their version information
 +      Version v = InternalDataSerializer.getVersionForDataStreamOrNull(in);
 +      if (v != null) {
 +        this.versionObj = v;
 +        this.version = v.ordinal();
 +      }
 +    }
 +  }
 +
 +  /**
 +   * For Externalizable
 +   *
 +   * @see Externalizable
 +   */
 +  public void writeExternal(ObjectOutput out) throws IOException {
 +    Assert.assertTrue(vmKind > 0);
 +
 +    // do it the way we like
 +    byte[] address = getInetAddress().getAddress();
 +
 +    out.writeInt(address.length); // IPv6 compatible
 +    out.write(address);
 +    out.writeInt(getPort());
 +
 +    DataSerializer.writeString(this.hostName, out);
 +    
 +    int flags = 0;
 +    if (netMbr.splitBrainEnabled()) flags |= SB_ENABLED_MASK;
 +    if (netMbr.preferredForCoordinator()) flags |= COORD_ENABLED_MASK;
 +    if (this.isPartial) flags |= PARTIAL_ID_MASK;
 +    // always write product version but enable reading from older versions
 +    // that do not have it
 +    flags |= VERSION_MASK;
 +    out.writeByte((byte)(flags & 0xff));
 +
 +    out.writeInt(dcPort);
 +    out.writeInt(vmPid);
 +    out.writeInt(vmKind);
 +    out.writeInt(vmViewId);
 +    DataSerializer.writeStringArray(this.groups, out);
 +
 +    DataSerializer.writeString(this.name, out);
 +    DataSerializer.writeString(this.uniqueTag, out);
 +    DataSerializer.writeString(this.durableClientAttributes==null ? "" : this.durableClientAttributes.getId(), out);
 +    DataSerializer.writeInteger(Integer.valueOf(this.durableClientAttributes==null ? 300 : this.durableClientAttributes.getTimeout()), out);
 +    Version.writeOrdinal(out, this.version, true);
 +    netMbr.writeAdditionalData(out);
 +  }
 +
 +  /**
 +    * For Externalizable
 +    *
 +    * @see Externalizable
 +    */
 +   public void readExternal(ObjectInput in)
 +   throws IOException, ClassNotFoundException {
 +     int len = in.readInt(); // IPv6 compatible
 +     byte addr[] = new byte[len];
 +     in.readFully(addr);
 +     InetAddress inetAddr = InetAddress.getByAddress(addr);
 +     int port = in.readInt();
 +     
 +     this.hostName = DataSerializer.readString(in);
 +
 +     int flags = in.readUnsignedByte();
 +     boolean sbEnabled = (flags & SB_ENABLED_MASK) != 0;
 +     boolean elCoord = (flags & COORD_ENABLED_MASK) != 0;
 +     this.isPartial = (flags & PARTIAL_ID_MASK) != 0;
 +     
 +     this.dcPort = in.readInt();
 +     this.vmPid = in.readInt();
 +     this.vmKind = in.readInt();
 +     this.vmViewId = in.readInt();
 +     this.groups = DataSerializer.readStringArray(in);
 +
 +     this.name = DataSerializer.readString(in);
 +     this.uniqueTag = DataSerializer.readString(in);
 +     String durableId = DataSerializer.readString(in);
 +     int durableTimeout = DataSerializer.readInteger(in).intValue();
 +     this.durableClientAttributes = new DurableClientAttributes(durableId, durableTimeout);
 +
 +     readVersion(flags, in);
 +
 +     netMbr = MemberFactory.newNetMember(inetAddr, port, sbEnabled, elCoord, version,
 +         new MemberAttributes(dcPort, vmPid, vmKind, vmViewId, name, groups, durableClientAttributes));
 +     if (this.version >= Version.GFE_90.ordinal()) {
 +       try {
 +         netMbr.readAdditionalData(in);
 +       } catch (java.io.EOFException e) {
 +         // old version quand-meme
 +       }
 +     }
 +
 +     Assert.assertTrue(this.vmKind > 0);
 +   }
 +
 +  public int getDSFID() {
 +    return DISTRIBUTED_MEMBER;
 +  }
 +
 +  public void toData(DataOutput out) throws IOException {
 +    toDataPre_GFE_9_0_0_0(out);
 +    if (this.version >= Version.GFE_90.ordinal()) {
 +      getNetMember().writeAdditionalData(out);
 +    }
 +  }
 +  
 +  
 +  public void toDataPre_GFE_9_0_0_0(DataOutput out) throws IOException {
 +    Assert.assertTrue(vmKind > 0);
 +    // NOTE: If you change the serialized format of this class
 +    //       then bump Connection.HANDSHAKE_VERSION since an
 +    //       instance of this class is sent during Connection handshake.
 +    DataSerializer.writeInetAddress(getInetAddress(), out);
 +    out.writeInt(getPort());
 +
 +    DataSerializer.writeString(this.hostName, out);
 +
 +    int flags = 0;
 +    if (netMbr.splitBrainEnabled()) flags |= SB_ENABLED_MASK;
 +    if (netMbr.preferredForCoordinator()) flags |= COORD_ENABLED_MASK;
 +    if (this.isPartial) flags |= PARTIAL_ID_MASK;
 +    // always write product version but enable reading from older versions
 +    // that do not have it
 +    flags |= VERSION_MASK;
 +    out.writeByte((byte)(flags & 0xff));
 +    
 +    out.writeInt(dcPort);
 +    out.writeInt(vmPid);
 +    out.writeByte(vmKind);
 +    DataSerializer.writeStringArray(this.groups, out);
 +
 +    DataSerializer.writeString(this.name, out);
 +    if (this.vmKind == DistributionManager.LONER_DM_TYPE) {
 +      DataSerializer.writeString(this.uniqueTag, out);
 +    } else {  // added in 6.5 for unique identifiers in P2P
 +      DataSerializer.writeString(String.valueOf(this.vmViewId), out);
 +    }
 +    DataSerializer.writeString(this.durableClientAttributes==null ? "" : this.durableClientAttributes.getId(), out);
 +    DataSerializer.writeInteger(Integer.valueOf(this.durableClientAttributes==null ? 300 : this.durableClientAttributes.getTimeout()), out);
 +    Version.writeOrdinal(out, this.version, true);
 +  }
 +
 +  public void toDataPre_GFE_7_1_0_0(DataOutput out) throws IOException {
 +      Assert.assertTrue(vmKind > 0);
 +    // [bruce] disabled to allow post-connect setting of the port for loner systems
 +//    Assert.assertTrue(getPort() > 0);
 +//    if (this.getPort() == 0) {
 +//      InternalDistributedSystem.getLoggerI18n().warning(LocalizedStrings.DEBUG,
 +//          "Serializing ID with zero port", new Exception("Stack trace"));
 +//    }
 +
 +    // NOTE: If you change the serialized format of this class
 +    //       then bump Connection.HANDSHAKE_VERSION since an
 +    //       instance of this class is sent during Connection handshake.
 +    DataSerializer.writeInetAddress(getInetAddress(), out);
 +    out.writeInt(getPort());
 +
 +    DataSerializer.writeString(this.hostName, out);
 +
 +    int flags = 0;
 +    if (netMbr.splitBrainEnabled()) flags |= SB_ENABLED_MASK;
 +    if (netMbr.preferredForCoordinator()) flags |= COORD_ENABLED_MASK;
 +    if (this.isPartial) flags |= PARTIAL_ID_MASK;
 +    out.writeByte((byte)(flags & 0xff));
 +    
 +    out.writeInt(dcPort);
 +    out.writeInt(vmPid);
 +    out.writeByte(vmKind);
 +    DataSerializer.writeStringArray(this.groups, out);
 +
 +    DataSerializer.writeString(this.name, out);
 +    if (this.vmKind == DistributionManager.LONER_DM_TYPE) {
 +      DataSerializer.writeString(this.uniqueTag, out);
 +    } else {  // added in 6.5 for unique identifiers in P2P
 +      DataSerializer.writeString(String.valueOf(this.vmViewId), out);
 +    }
 +    DataSerializer.writeString(this.durableClientAttributes==null ? "" : this.durableClientAttributes.getId(), out);
 +    DataSerializer.writeInteger(Integer.valueOf(this.durableClientAttributes==null ? 300 : this.durableClientAttributes.getTimeout()), out);
 + 
 +  }
 +  
 +  public void fromData(DataInput in)
 +  throws IOException, ClassNotFoundException {
 +    fromDataPre_GFE_9_0_0_0(in);
 +    // just in case this is just a non-versioned read
 +    // from a file we ought to check the version
 +    if (this.version >= Version.GFE_90.ordinal()) {
 +      try {
 +        netMbr.readAdditionalData(in);
 +      } catch (EOFException e) {
 +        // nope - it's from a pre-GEODE client or WAN site
 +      }
 +    }
 +  }
 +  
 +  public void fromDataPre_GFE_9_0_0_0(DataInput in)
 +  throws IOException, ClassNotFoundException {
 +    InetAddress inetAddr = DataSerializer.readInetAddress(in);
 +    int port = in.readInt();
 +
 +    this.hostName = DataSerializer.readString(in);
 +    this.hostName = SocketCreator.resolve_dns? SocketCreator.getCanonicalHostName(inetAddr, hostName) : inetAddr.getHostAddress();
 +
 +    int flags = in.readUnsignedByte();
 +    boolean sbEnabled = (flags & SB_ENABLED_MASK) != 0;
 +    boolean elCoord = (flags & COORD_ENABLED_MASK) != 0;
 +    this.isPartial = (flags & PARTIAL_ID_MASK) != 0;
 +
 +    this.dcPort = in.readInt();
 +    this.vmPid = in.readInt();
 +    this.vmKind = in.readUnsignedByte();
 +    this.groups = DataSerializer.readStringArray(in);
 +
 +    this.name = DataSerializer.readString(in);
 +    if (this.vmKind == DistributionManager.LONER_DM_TYPE) {
 +      this.uniqueTag = DataSerializer.readString(in);
 +    } else {
 +      String str = DataSerializer.readString(in);
 +      if (str != null) { // backward compatibility from earlier than 6.5
 +        this.vmViewId = Integer.parseInt(str);
 +      }
 +    }
 +
 +    String durableId = DataSerializer.readString(in);
 +    int durableTimeout = DataSerializer.readInteger(in).intValue();
 +    this.durableClientAttributes = new DurableClientAttributes(durableId, durableTimeout);
 +
 +    readVersion(flags, in);
 +
 +    MemberAttributes attr = new MemberAttributes(this.dcPort, this.vmPid,
 +        this.vmKind, this.vmViewId, this.name, this.groups, this.durableClientAttributes);
 +    netMbr = MemberFactory.newNetMember(inetAddr, port, sbEnabled, elCoord, version, attr);
 +
 +    synchPayload();
 +
 +    Assert.assertTrue(this.vmKind > 0);
 +//    Assert.assertTrue(getPort() > 0);
 +  }
 +
 +  public void fromDataPre_GFE_7_1_0_0(DataInput in)  throws IOException, ClassNotFoundException {
 +    InetAddress inetAddr = DataSerializer.readInetAddress(in);
 +    int port = in.readInt();
 +
 +    this.hostName = DataSerializer.readString(in);
 +    this.hostName = SocketCreator.resolve_dns? SocketCreator.getCanonicalHostName(inetAddr, hostName) : inetAddr.getHostAddress();
 +
 +    int flags = in.readUnsignedByte();
 +    boolean sbEnabled = (flags & SB_ENABLED_MASK) != 0;
 +    boolean elCoord = (flags & COORD_ENABLED_MASK) != 0;
 +    this.isPartial = (flags & PARTIAL_ID_MASK) != 0;
 +
 +    this.dcPort = in.readInt();
 +    this.vmPid = in.readInt();
 +    this.vmKind = in.readUnsignedByte();
 +    this.groups = DataSerializer.readStringArray(in);
 +
 +    this.name = DataSerializer.readString(in);
 +    if (this.vmKind == DistributionManager.LONER_DM_TYPE) {
 +      this.uniqueTag = DataSerializer.readString(in);
 +    } else {
 +      String str = DataSerializer.readString(in);
 +      if (str != null) { // backward compatibility from earlier than 6.5
 +        this.vmViewId = Integer.parseInt(str);
 +      }
 +    }
 +
 +    String durableId = DataSerializer.readString(in);
 +    int durableTimeout = DataSerializer.readInteger(in).intValue();
 +    this.durableClientAttributes = new DurableClientAttributes(durableId, durableTimeout);
 +
 +    MemberAttributes attr = new MemberAttributes(this.dcPort, this.vmPid,
 +        this.vmKind, this.vmViewId, this.name, this.groups, this.durableClientAttributes);
 +    netMbr = MemberFactory.newNetMember(inetAddr, port, sbEnabled, elCoord, 
 +        InternalDataSerializer.getVersionForDataStream(in).ordinal(), attr);
 +
 +    synchPayload();
 +
 +    Assert.assertTrue(this.vmKind > 0);
 +  }
 +
 +  /** this writes just the parts of the ID that are needed for comparisons and communications */
 +   public static InternalDistributedMember readEssentialData(DataInput in)
 +     throws IOException, ClassNotFoundException {
 +     final InternalDistributedMember mbr = new InternalDistributedMember();
 +     mbr._readEssentialData(in);
 +     return mbr;
 +   }
 +   
 +   private void _readEssentialData(DataInput in)
 +     throws IOException, ClassNotFoundException {
 +     this.isPartial = true;
 +     InetAddress inetAddr = DataSerializer.readInetAddress(in);
 +     int port = in.readInt();
 +
 +     this.hostName = SocketCreator.resolve_dns? SocketCreator.getHostName(inetAddr) : inetAddr.getHostAddress();
 +
 +     int flags = in.readUnsignedByte();
 +     boolean sbEnabled = (flags & SB_ENABLED_MASK) != 0;
 +     boolean elCoord = (flags & COORD_ENABLED_MASK) != 0;
 +
 +     this.vmKind = in.readUnsignedByte();
 +     
 +
 +     if (this.vmKind == DistributionManager.LONER_DM_TYPE) {
 +       this.uniqueTag = DataSerializer.readString(in);
 +     } else {
 +       String str = DataSerializer.readString(in);
 +       if (str != null) { // backward compatibility from earlier than 6.5
 +         this.vmViewId = Integer.parseInt(str);
 +       }
 +     }
 +
 +     this.name = DataSerializer.readString(in);
 +
 +     MemberAttributes attr = new MemberAttributes(this.dcPort, this.vmPid,
 +         this.vmKind, this.vmViewId, this.name, this.groups, this.durableClientAttributes);
 +     netMbr = MemberFactory.newNetMember(inetAddr, port, sbEnabled, elCoord, 
 +         InternalDataSerializer.getVersionForDataStream(in).ordinal(), attr);
 +
 +     synchPayload();
 +
 +     if (InternalDataSerializer.getVersionForDataStream(in).compareTo(Version.GFE_90)>=0) {
 +       netMbr.readAdditionalData(in);
 +     }
 +   }
 +
 +
 +   public void writeEssentialData(DataOutput out) throws IOException {
 +     Assert.assertTrue(vmKind > 0);
 +     DataSerializer.writeInetAddress(getInetAddress(), out);
 +     out.writeInt(getPort());
 +
 +     int flags = 0;
 +     if (netMbr.splitBrainEnabled()) flags |= SB_ENABLED_MASK;
 +     if (netMbr.preferredForCoordinator()) flags |= COORD_ENABLED_MASK;
 +     flags |= PARTIAL_ID_MASK;
 +     out.writeByte((byte)(flags & 0xff));
 +     
 +//     out.writeInt(dcPort);
 +     out.writeByte(vmKind);
 +
 +     if (this.vmKind == DistributionManager.LONER_DM_TYPE) {
 +       DataSerializer.writeString(this.uniqueTag, out);
 +     } else {  // added in 6.5 for unique identifiers in P2P
 +       DataSerializer.writeString(String.valueOf(this.vmViewId), out);
 +     }
 +     // write name last to fix bug 45160
 +     DataSerializer.writeString(this.name, out);
 +
 +     if (InternalDataSerializer.getVersionForDataStream(out).compareTo(Version.GFE_90)>=0) {
 +       netMbr.writeAdditionalData(out);
 +     }
 +   }
 +
 +  /**
 +   * [GemStone] Set the direct channel port
 +   */
 +  public void setDirectChannelPort(int p)
 +  {
 +    dcPort = p;
 +    synchPayload();
 +  }
 +  
 +  /**
 +   * Set the membership port.  This is done in loner systems using
 +   * client/server connection information to help form a unique ID
 +   */
 +  public void setPort(int p) {
 +    assert this.vmKind == DistributionManager.LONER_DM_TYPE;
 +    this.netMbr.setPort(p);
 +    synchPayload();
 +    cachedToString = null;
 +  }
 +  
 +  /** drop the cached toString rep of this ID */
 +  public void dropCachedString() {
 +    this.cachedToString = null;
 +  }
 +
 +  public String getHost() {
 +    return this.netMbr.getInetAddress().getCanonicalHostName();
 +  }
 +
 +  public int getProcessId() {
 +    return this.vmPid;
 +  }
 +
 +  public String getId() {
 +    return toString();
 +  }
 +    /*if (this.ipAddr == null) {
 +      return "<null>";
 +    }
 +    else {
 +      StringBuffer sb = new StringBuffer();
 +      InetAddress addr = this.ipAddr.getIpAddress();
 +      if(addr.isMulticastAddress()) {
 +        sb.append(addr.getHostAddress());
 +      } else {
 +        appendShortName(addr.getHostName(), sb);
 +      }
 +      if (this.vmPid != 0) {
 +        sb.append("(");
 +        sb.append(this.vmPid);
 +        sb.append(")");
 +      }
 +      sb.append(":");
 +      sb.append(this.ipAddr.getPort());
 +      return sb.toString();
 +    }
 +  }
 +
 +  // Helper method for getId()... copied from IpAddress.
 +  private void appendShortName(String hostname, StringBuffer sb) {
 +    if (hostname == null) return;
 +    int index = hostname.indexOf('.');
 +    if(index > 0 && !Character.isDigit(hostname.charAt(0))) {
 +      sb.append(hostname.substring(0, index));
 +    } else {
 +      sb.append(hostname);
 +    }
 +  }*/
 +  
 +  public final void setVersionObjectForTest(Version v) {
 +    this.version = v.ordinal();
 +    this.versionObj = v;
 +    netMbr.setVersion(v);
 +  }
 +
 +  public final Version getVersionObject() {
 +    return this.versionObj;
 +  }
 +  
 +  @Override
 +  public Version[] getSerializationVersions() {
 +    return dsfidVersions;
 +  }
 +
 +
 +  @Override
 +  public int getSizeInBytes() {
 +  
 +    int size = 0;
 +  
 +    // ipaddr:  1 byte length + 4 bytes (IPv4) or 16 bytes (IPv6)
 +    if (netMbr.getInetAddress() instanceof Inet4Address){
 +      size += 5;
 +    } else {
 +      size += 17;
 +    }
 +    
 +    // port:  4 bytes
 +    // flags: 1 byte
 +    //vmKind: 1 byte
 +    size += 6;
 +    
 +    // viewID:  String(1+1+numchars)
 +    size += (2+ String.valueOf(this.vmViewId).length());
 +    
 +    // empty name: String(1+1)
 +    size += 2;
 +    
 +    return size;
 +  }
 +}


[40/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/InternalDistributedSystem.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/InternalDistributedSystem.java
index 276efec,0000000..a193699
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/InternalDistributedSystem.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/distributed/internal/InternalDistributedSystem.java
@@@ -1,3056 -1,0 +1,3079 @@@
 +/*
 + * 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.distributed.internal;
 +
 +import java.io.File;
 +import java.io.IOException;
 +import java.io.Reader;
 +import java.lang.reflect.Array;
 +import java.net.InetAddress;
 +import java.util.ArrayList;
 +import java.util.Date;
 +import java.util.HashSet;
 +import java.util.Iterator;
 +import java.util.LinkedHashSet;
 +import java.util.List;
 +import java.util.Properties;
 +import java.util.Set;
 +import java.util.SortedSet;
 +import java.util.StringTokenizer;
 +import java.util.TreeSet;
 +import java.util.concurrent.ConcurrentHashMap;
 +import java.util.concurrent.CopyOnWriteArrayList;
 +import java.util.concurrent.TimeUnit;
 +import java.util.concurrent.atomic.AtomicReference;
 +
 +import org.apache.logging.log4j.Logger;
 +
 +import com.gemstone.gemfire.CancelCriterion;
 +import com.gemstone.gemfire.CancelException;
 +import com.gemstone.gemfire.ForcedDisconnectException;
 +import com.gemstone.gemfire.GemFireConfigException;
 +import com.gemstone.gemfire.GemFireIOException;
 +import com.gemstone.gemfire.LogWriter;
 +import com.gemstone.gemfire.StatisticDescriptor;
 +import com.gemstone.gemfire.Statistics;
 +import com.gemstone.gemfire.StatisticsType;
 +import com.gemstone.gemfire.StatisticsTypeFactory;
 +import com.gemstone.gemfire.SystemConnectException;
 +import com.gemstone.gemfire.SystemFailure;
 +import com.gemstone.gemfire.admin.AlertLevel;
 +import com.gemstone.gemfire.cache.CacheClosedException;
 +import com.gemstone.gemfire.cache.CacheFactory;
 +import com.gemstone.gemfire.cache.execute.internal.FunctionServiceManager;
++import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.DistributedSystem;
 +import com.gemstone.gemfire.distributed.DistributedSystemDisconnectedException;
 +import com.gemstone.gemfire.distributed.DurableClientAttributes;
 +import com.gemstone.gemfire.distributed.internal.locks.GrantorRequestProcessor;
 +import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
 +import com.gemstone.gemfire.distributed.internal.membership.MembershipManager;
 +import com.gemstone.gemfire.distributed.internal.membership.QuorumChecker;
 +import com.gemstone.gemfire.distributed.internal.membership.gms.Services;
 +import com.gemstone.gemfire.distributed.internal.membership.gms.mgr.GMSMembershipManager;
 +import com.gemstone.gemfire.i18n.LogWriterI18n;
 +import com.gemstone.gemfire.internal.Assert;
 +import com.gemstone.gemfire.internal.DSFIDFactory;
 +import com.gemstone.gemfire.internal.DummyStatisticsImpl;
 +import com.gemstone.gemfire.internal.GemFireStatSampler;
 +import com.gemstone.gemfire.internal.InternalDataSerializer;
 +import com.gemstone.gemfire.internal.InternalInstantiator;
 +import com.gemstone.gemfire.internal.LinuxProcFsStatistics;
 +import com.gemstone.gemfire.internal.LocalStatisticsImpl;
 +import com.gemstone.gemfire.internal.OsStatisticsFactory;
 +import com.gemstone.gemfire.internal.SocketCreator;
 +import com.gemstone.gemfire.internal.StatisticsImpl;
 +import com.gemstone.gemfire.internal.StatisticsManager;
 +import com.gemstone.gemfire.internal.StatisticsTypeFactoryImpl;
 +import com.gemstone.gemfire.internal.SystemTimer;
 +import com.gemstone.gemfire.internal.admin.remote.DistributionLocatorId;
 +import com.gemstone.gemfire.internal.cache.CacheServerImpl;
 +import com.gemstone.gemfire.internal.cache.CacheConfig;
 +import com.gemstone.gemfire.internal.cache.EventID;
 +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
 +import com.gemstone.gemfire.internal.cache.execute.FunctionServiceStats;
 +import com.gemstone.gemfire.internal.cache.execute.FunctionStats;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.HandShake;
 +import com.gemstone.gemfire.internal.cache.xmlcache.CacheServerCreation;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.logging.InternalLogWriter;
 +import com.gemstone.gemfire.internal.logging.LogService;
 +import com.gemstone.gemfire.internal.logging.LogWriterFactory;
 +import com.gemstone.gemfire.internal.logging.LoggingThreadGroup;
 +import com.gemstone.gemfire.internal.logging.log4j.AlertAppender;
 +import com.gemstone.gemfire.internal.logging.log4j.LocalizedMessage;
 +import com.gemstone.gemfire.internal.logging.log4j.LogWriterAppender;
 +import com.gemstone.gemfire.internal.logging.log4j.LogWriterAppenders;
 +import com.gemstone.gemfire.internal.offheap.MemoryAllocator;
 +import com.gemstone.gemfire.internal.offheap.OffHeapStorage;
 +import com.gemstone.gemfire.internal.tcp.ConnectionTable;
 +import com.gemstone.gemfire.internal.util.concurrent.StoppableCondition;
 +import com.gemstone.gemfire.internal.util.concurrent.StoppableReentrantLock;
 +import com.gemstone.gemfire.management.ManagementException;
 +import com.gemstone.gemfire.security.GemFireSecurityException;
 +
 +/**
 + * The concrete implementation of {@link DistributedSystem} that
 + * provides internal-only functionality.
 + *
 + * @author David Whitlock
 + * @since 3.0
 + *
 + */
 +public class InternalDistributedSystem
 +  extends DistributedSystem
 +  implements OsStatisticsFactory, StatisticsManager
 +{
 +  private static final Logger logger = LogService.getLogger();
 +  
 +  public static final String DISABLE_MANAGEMENT_PROPERTY = "gemfire.disableManagement";
 +  
 +  /**
 +   * If auto-reconnect is going on this will hold a reference to it
 +   */
 +  public static volatile DistributedSystem systemAttemptingReconnect;
 +
 +  public static final CreationStackGenerator DEFAULT_CREATION_STACK_GENERATOR = new CreationStackGenerator() {
 +    @Override
 +    public Throwable generateCreationStack(final DistributionConfig config) {
 +      return null;
 +    }
 +  };
 +
 +  // the following is overridden from DistributedTestCase to fix #51058
 +  public static final AtomicReference<CreationStackGenerator> TEST_CREATION_STACK_GENERATOR = new AtomicReference<CreationStackGenerator>(DEFAULT_CREATION_STACK_GENERATOR);
 +  
 +  /** The distribution manager that is used to communicate with the
 +   * distributed system. */
 +  protected DM dm;
 +
 +  private final GrantorRequestProcessor.GrantorRequestContext grc;
 +  public GrantorRequestProcessor.GrantorRequestContext getGrantorRequestContext() {
 +    return grc;
 +  }
 +  
 +  /** Numeric id that identifies this node in a DistributedSystem */
 +  private long id;
 +
 +  /** The log writer used to log information messages */
 +  protected InternalLogWriter logWriter = null;
 +
 +  /** The log writer used to log security related messages */
 +  protected InternalLogWriter securityLogWriter = null;
 + 
 +  /** Distributed System clock */
 +  private DSClock clock;
 +
 +//  /** The log writer was provided by an external entity */
 +//  private boolean externalLogWriterProvided = false;
 +//
 +  private LogWriterAppender logWriterAppender = null;
 +
 +  private LogWriterAppender securityLogWriterAppender = null;
 +  
 +  /** Time this system was created */
 +  private final long startTime;
 +
 +  /**
 +   * Guards access to {@link #isConnected}
 +   */
 +  protected final Object isConnectedMutex = new Object();
 +  
 +  /** Is this <code>DistributedSystem</code> connected to a
 +   * distributed system? 
 +   *
 +   * Concurrency: volatile for reads and protected by synchronization of {@link #isConnectedMutex} for writes
 +   */
 +  protected volatile boolean isConnected;
 +
 +  /**
 +   * Set to true if this distributed system is a singleton; it will
 +   * always be the only member of the system.
 +   */
 +  private boolean isLoner = false;
 +
 +  /** The sampler for this DistributedSystem.
 +   */
 +  private GemFireStatSampler sampler = null;
 +
 +  /** A set of listeners that are invoked when this connection to the
 +   * distributed system is disconnected */
 +  private final Set listeners = new LinkedHashSet(); // needs to be ordered
 +
 +  /** Set of listeners that are invoked whenever a connection is created to
 +   * the distributed system */
 +  private static Set connectListeners = new LinkedHashSet(); // needs to be ordered
 +  
 +  /** auto-reconnect listeners */
 +  private static List<ReconnectListener> reconnectListeners = new ArrayList<ReconnectListener>();
 +  
 +  /**
 +   * whether this DS is one created to reconnect to the distributed 
 +   * system after a forced-disconnect.  This state is cleared once reconnect
 +   * is successful.
 +   */
 +  private boolean isReconnectingDS;
 +  
 +  /**
 +   * During a reconnect attempt this is used to perform quorum checks
 +   * before allowing a location service to be started up in this JVM.
 +   * If quorum checks fail then we delay starting location services until
 +   * a live locator can be contacted.
 +   */
 +  private QuorumChecker quorumChecker;
 +  
 +  /** sqlfire disconnect listener */
 +  private DisconnectListener sqlfDisconnectListener;
 +
 +  /**
 +   * A Constant that matches the ThreadGroup name of the shutdown hook.
 +   * This constant is used to insure consistency with LoggingThreadGroup.
 +   * Due to Bug 38407, be careful about moving this to another class.
 +   */
 +  public final static String SHUTDOWN_HOOK_NAME = "Distributed system shutdown hook";
 +  /**
 +   * A property to prevent shutdown hooks from being registered with the VM.
 +   * This is regarding bug 38407
 +   */
 +  public final static String DISABLE_SHUTDOWN_HOOK_PROPERTY = "gemfire.disableShutdownHook";
 +
 +  /**
 +   * A property to append to existing log-file instead of truncating it.
 +   */
 +  public final static String APPEND_TO_LOG_FILE = "gemfire.append-log";
 +
 +  ////////////////////  Configuration Fields  ////////////////////
 +
 +  /** The config object used to create this distributed system */
 +  private final DistributionConfig originalConfig;
 +
 +  /** The config object to which most configuration work is delegated */
 +  private DistributionConfig config;
 +
 +  private final boolean statsDisabled = Boolean.getBoolean("gemfire.statsDisabled");
 +
 +  private volatile boolean shareSockets = DistributionConfig.DEFAULT_CONSERVE_SOCKETS;
 +
 +  /** if this distributed system starts a locator, it is stored here */
 +  private InternalLocator startedLocator;
 +  
 +  private List<ResourceEventsListener> resourceListeners;
 +  
 +  private final boolean disableManagement = Boolean.getBoolean(DISABLE_MANAGEMENT_PROPERTY);
 +
 +  /**
 +   * Stack trace showing the creation of this instance of InternalDistributedSystem.
 +   */
 +  private final Throwable creationStack;
 +  
 +  /////////////////////  Static Methods  /////////////////////
 +
 +  /**
 +   * Creates a new instance of <code>InternalDistributedSystem</code>
 +   * with the given configuration.
 +   */
 +  public static InternalDistributedSystem newInstance(Properties config) {
 +    boolean success = false;
 +    InternalDataSerializer.checkSerializationVersion();
 +    try {
 +      SystemFailure.startThreads();
 +    InternalDistributedSystem newSystem = new InternalDistributedSystem(config);
 +    newSystem.initialize();
 +    reconnectAttemptCounter = 0; // reset reconnect count since we just got a new connection
 +    notifyConnectListeners(newSystem);
 +    success = true;
 +    return newSystem;
 +    } finally {
 +      if (!success) {
 +        LoggingThreadGroup.cleanUpThreadGroups(); // bug44365 - logwriters accumulate, causing mem leak
 +        SystemFailure.stopThreads();
 +      }
 +    }
 +  }
 +  
 +  
 +  /**
 +   * creates a non-functional instance for testing
 +   * @param nonDefault - non-default distributed system properties
 +   */
 +  public static InternalDistributedSystem newInstanceForTesting(DM dm, Properties nonDefault) {
 +    InternalDistributedSystem sys = new InternalDistributedSystem(nonDefault);
 +    sys.config = new RuntimeDistributionConfigImpl(sys);
 +    sys.dm = dm;
 +    sys.isConnected = true;
 +    return sys;
 +  }
 +
 +  /**
 +   * Returns a connection to the distributed system that is suitable
 +   * for administration.  For administration, we are not as strict
 +   * when it comes to existing connections.
 +   *
 +   * @since 4.0
 +   */
 +  public static DistributedSystem connectForAdmin(Properties props) {
 +    return DistributedSystem.connectForAdmin(props);
 +  }
 +
 +  /**
 +   * Returns a connected distributed system for this VM, or null
 +   * if there is no connected distributed system in this VM.
 +   * This method synchronizes on the existingSystems collection.
 +   *
 +   * <p>author bruce
 +   * @since 5.0
 +   */
 +  public static InternalDistributedSystem getConnectedInstance() {
 +    InternalDistributedSystem result = null;
 +    synchronized (existingSystemsLock) {
 +      if (!existingSystems.isEmpty()) {
 +        InternalDistributedSystem existingSystem =
 +          (InternalDistributedSystem) existingSystems.get(0);
 +        if (existingSystem.isConnected())
 +          result = existingSystem;
 +      }
 +    }
 +    return result;
 +  }
 +
 +  /**
 +   * Returns the current distributed system, if there is one.
 +   * Note: this method is no longer unsafe size existingSystems uses copy-on-write.
 +   * <p>author bruce
 +   * @since 5.0
 +   */
 +  public static InternalDistributedSystem unsafeGetConnectedInstance() {
 +    InternalDistributedSystem result = getAnyInstance();
 +    if (result != null) {
 +      if (!result.isConnected()) {
 +        result = null;
 +      }
 +    }
 +    return result;
 +  }
 +
 +  /**
 +   * @return distribution stats, or null if there is no distributed system available
 +   */
 +  public static DMStats getDMStats() {
 +    InternalDistributedSystem sys = getAnyInstance();
 +    if (sys != null && sys.dm != null) {
 +      return sys.dm.getStats();
 +    }
 +    return null;
 +  }
 +
 +  /**
 +   * @return a log writer, or null if there is no distributed system available
 +   */
 +  public static LogWriterI18n getLoggerI18n() {
 +    InternalDistributedSystem sys = getAnyInstance();
 +    if (sys != null && sys.logWriter != null) {
 +      return sys.logWriter.convertToLogWriterI18n();
 +    }
 +    return null;
 +  }
 +  
 +  public static InternalLogWriter getStaticInternalLogWriter() {
 +    InternalDistributedSystem sys = getAnyInstance();
 +    if (sys != null) {
 +      return sys.logWriter;
 +    }
 +    return null;
 +  }
 +  
 +  public InternalLogWriter getInternalLogWriter() {
 +    return this.logWriter;
 +  }
 +  
 +  public static InternalLogWriter getStaticSecurityInternalLogWriter() {
 +    InternalDistributedSystem sys = getAnyInstance();
 +    if (sys != null) {
 +      return sys.securityLogWriter;
 +    }
 +    return null;
 +  }
 +  
 +  public InternalLogWriter getSecurityInternalLogWriter() {
 +    InternalDistributedSystem sys = getAnyInstance();
 +    if (sys != null) {
 +      return sys.securityLogWriter;
 +    }
 +    return null;
 +  }
 +  
 +  /** reset the reconnectAttempt counter for a new go at reconnecting */
 +  private static void resetReconnectAttemptCounter() {
 +    reconnectAttemptCounter = 0;
 +  }
 +
 +
 +  //////////////////////  Constructors  //////////////////////
 +
 +  /**
 +   * Creates a new <code>InternalDistributedSystem</code> with the
 +   * given configuration properties.  Does all of the magic of finding
 +   * the "default" values of properties.  See {@link
 +   * DistributedSystem#connect} for a list of exceptions that may be
 +   * thrown.
 +   *
 +   * @param nonDefault
 +   *        The non-default configuration properties specified by the
 +   *        caller
 +   *
 +   * @see DistributedSystem#connect
 +   */
 +  private InternalDistributedSystem(Properties nonDefault) {
 +
 +    // register DSFID types first; invoked explicitly so that all message type
 +    // initializations do not happen in first deserialization on a possibly
 +    // "precious" thread
 +    DSFIDFactory.registerTypes();
 +
 +    Object o = nonDefault.remove(DistributionConfig.DS_RECONNECTING_NAME);
 +    if (o instanceof Boolean) {
 +      this.isReconnectingDS = ((Boolean)o).booleanValue();
 +    } else {
 +      this.isReconnectingDS = false;
 +    }
 +    
 +    o = nonDefault.remove(DistributionConfig.DS_QUORUM_CHECKER_NAME);
 +    if (o instanceof QuorumChecker) {
 +      this.quorumChecker = (QuorumChecker)o;
 +    }
 +
 +    o = nonDefault.remove(DistributionConfig.DS_CONFIG_NAME);
 +    if (o instanceof DistributionConfigImpl) {
 +      this.originalConfig = (DistributionConfigImpl) o;
 +    } else {
 +      this.originalConfig = new DistributionConfigImpl(nonDefault);
 +    }
 +    
 +    ((DistributionConfigImpl)this.originalConfig).checkForDisallowedDefaults(); // throws IllegalStateEx
 +    this.shareSockets = this.originalConfig.getConserveSockets();
 +    this.startTime = System.currentTimeMillis();
 +    this.grc = new GrantorRequestProcessor.GrantorRequestContext(stopper);
 +    
 +    elderLock = new StoppableReentrantLock(stopper);
 +    elderLockCondition = elderLock.newCondition();
 +
 +    this.creationStack = TEST_CREATION_STACK_GENERATOR.get().generateCreationStack(this.originalConfig);
 +
 +//    if (DistributionConfigImpl.multicastTest) {
 +//      this.logger.warning("Use of multicast has been forced");
 +//    }
 +//    if (DistributionConfigImpl.forceDisableTcp) {
 +//      this.logger.warning("Use of UDP has been forced");
 +//    }
 +//    if (com.gemstone.gemfire.distributed.internal.membership.jgroup.JGroupMembershipManager.multicastTest) {
 +//      this.logger.warning("Use of multicast for all distributed cache operations has been forced");
 +//    }
 +
 +  }
 +
 +  ////////////////////  Instance Methods  ////////////////////
 +  
 +  /**
 +   * Registers a listener to the system
 +   * 
 +   * @param listener
 +   *          listener to be added
 +   */
 +  public void addResourceListener(ResourceEventsListener listener) {
 +    resourceListeners.add(listener);
 +  }
 +  
 +  /**
 +   * Un-Registers a listener to the system
 +   * 
 +   * @param listener
 +   *          listener to be removed
 +   */
 +  public void removeResourceListener(ResourceEventsListener listener) {
 +    resourceListeners.remove(listener);
 +  }
 +
 +  /**
 +   * @return the listeners registered with the system
 +   */
 +  public List<ResourceEventsListener> getResourceListeners() {
 +    return resourceListeners;
 +  }
 +
 +  /**
 +   * Handles a particular event associated with a resource
 +   * 
 +   * @param event
 +   *          Resource event
 +   * @param resource
 +   *          resource on which event is generated
 +   */
 +  public void handleResourceEvent(ResourceEvent event, Object resource) {
 +    if (disableManagement) {
 +      return;
 +    }
 +    if (resourceListeners.size() == 0) {
 +      return;
 +    }
 +    notifyResourceEventListeners(event, resource);
 +  }
 +
 +  /** Returns true if system is a loner (for testing) */
 +  public boolean isLoner() {
 +    return this.isLoner;
 +  }
 +
 +  private MemoryAllocator offHeapStore = null;
 +  
 +  public MemoryAllocator getOffHeapStore() {
 +    return this.offHeapStore;
 +  }
 +  
 +  /**
 +   * Initializes this connection to a distributed system with the
 +   * current configuration state.
 +   */
 +  private void initialize() {
 +    if (this.originalConfig.getLocators().equals("")) {
 +      if (this.originalConfig.getMcastPort() != 0) {
 +        throw new GemFireConfigException("The "
 +                                          + DistributionConfig.LOCATORS_NAME
 +                                          + " attribute can not be empty when the "
 +                                          + DistributionConfig.MCAST_PORT_NAME
 +                                          + " attribute is non-zero.");
 +      } else {
 +        // no distribution
 +        this.isLoner = true;
 +      }
 +    }
 +
 +    this.config = new RuntimeDistributionConfigImpl(this);
 +    if (!this.isLoner) {
 +      this.attemptingToReconnect = (reconnectAttemptCounter > 0);
 +    }
 +    try {
 +    SocketCreator.getDefaultInstance(this.config);
 +
 +    // LOG: create LogWriterAppender(s) if log-file or security-log-file is specified
 +    final boolean hasLogFile = this.config.getLogFile() != null && !this.config.getLogFile().equals(new File(""));
 +    final boolean hasSecurityLogFile = this.config.getSecurityLogFile() != null && !this.config.getSecurityLogFile().equals(new File(""));
 +    LogService.configureLoggers(hasLogFile, hasSecurityLogFile);
 +    if (hasLogFile || hasSecurityLogFile) {
 +      
 +      // main log file
 +      if (hasLogFile) {
 +        // if log-file then create logWriterAppender
 +        this.logWriterAppender = LogWriterAppenders.getOrCreateAppender(LogWriterAppenders.Identifier.MAIN, this.isLoner, this.config, true);
 +      }
 +      
 +      // security log file
 +      if (hasSecurityLogFile) {
 +        // if security-log-file then create securityLogWriterAppender
 +        this.securityLogWriterAppender = LogWriterAppenders.getOrCreateAppender(LogWriterAppenders.Identifier.SECURITY, this.isLoner, this.config, false);
 +      } else {
 +        // let security route to regular log-file or stdout
 +      }
 +    }
 +    
 +    // LOG: create LogWriterLogger(s) for backwards compatibility of getLogWriter and getSecurityLogWriter
 +    if (this.logWriter == null) {
 +      this.logWriter = LogWriterFactory.createLogWriterLogger(this.isLoner, false, this.config, true);
 +      this.logWriter.fine("LogWriter is created.");
 +    }
 +    
 +//    logWriter.info("Created log writer for IDS@"+System.identityHashCode(this));
 +    
 +    if (this.securityLogWriter == null) {
 +      // LOG: whole new LogWriterLogger instance for security
 +      this.securityLogWriter = LogWriterFactory.createLogWriterLogger(this.isLoner, true, this.config, false);
 +      this.securityLogWriter.fine("SecurityLogWriter is created.");
 +    }
 +    
 +    Services.setLogWriter(this.logWriter);
 +    Services.setSecurityLogWriter(this.securityLogWriter);
 +
 +    this.clock = new DSClock(this.isLoner);
 +    
 +    if (this.attemptingToReconnect && logger.isDebugEnabled()) {
 +      logger.debug("This thread is initializing a new DistributedSystem in order to reconnect to other members");
 +    }
 +    // Note we need loners to load the license in case they are a
 +    // bridge server and will need to enforce the member limit
 +    if (Boolean.getBoolean(InternalLocator.FORCE_LOCATOR_DM_TYPE)) {
 +      this.locatorDMTypeForced = true;
 +    }
 +
 +    // Initialize the Diffie-Hellman and public/private keys
 +    try {
 +      HandShake.initCertsMap(this.config.getSecurityProps());
 +      HandShake.initPrivateKey(this.config.getSecurityProps());
 +      HandShake.initDHKeys(this.config);
 +    }
 +    catch (Exception ex) {
 +      throw new GemFireSecurityException(
 +        LocalizedStrings.InternalDistributedSystem_PROBLEM_IN_INITIALIZING_KEYS_FOR_CLIENT_AUTHENTICATION.toLocalizedString(), ex);
 +    }
 +
 +    final long offHeapMemorySize = OffHeapStorage.parseOffHeapMemorySize(getConfig().getOffHeapMemorySize());
 +
 +    this.offHeapStore = OffHeapStorage.createOffHeapStorage(getLogWriter(), this, offHeapMemorySize, this);
 +    
 +    // Note: this can only happen on a linux system
 +    if (getConfig().getLockMemory()) {
 +      // This calculation is not exact, but seems fairly close.  So far we have
 +      // not loaded much into the heap and the current RSS usage is already 
 +      // included the available memory calculation.
 +      long avail = LinuxProcFsStatistics.getAvailableMemory(logger);
 +      long size = offHeapMemorySize + Runtime.getRuntime().totalMemory();
 +      if (avail < size) {
 +        if (GemFireCacheImpl.ALLOW_MEMORY_LOCK_WHEN_OVERCOMMITTED) {
 +          logger.warn(LocalizedMessage.create(LocalizedStrings.InternalDistributedSystem_MEMORY_OVERCOMMIT_WARN, size - avail));
 +        } else {
 +          throw new IllegalStateException(LocalizedStrings.InternalDistributedSystem_MEMORY_OVERCOMMIT.toLocalizedString(avail, size));
 +        }
 +      }
 +      
 +      logger.info("Locking memory. This may take a while...");
 +      GemFireCacheImpl.lockMemory();
 +      logger.info("Finished locking memory.");
 +    }
 +
 +    try {
 +      startInitLocator();
 +    } catch (InterruptedException e) {
 +      throw new SystemConnectException("Startup has been interrupted", e);
 +    }
 +
 +    synchronized (this.isConnectedMutex) {
 +      this.isConnected = true;
 +    }
 +    
 +    if (!this.isLoner) {
 +      try {
 +        if (this.quorumChecker != null) {
 +          this.quorumChecker.suspend();
 +        }
 +        this.dm = DistributionManager.create(this);
 +        // fix bug #46324
 +        if (InternalLocator.hasLocator()) {
 +          InternalLocator locator = InternalLocator.getLocator();
 +          getDistributionManager().addHostedLocators(getDistributedMember(), InternalLocator.getLocatorStrings(), locator.isSharedConfigurationEnabled());
 +        }
 +      }
 +      finally {
 +        if (this.dm == null && this.quorumChecker != null) {
 +          this.quorumChecker.resume();
 +        }
 +        setDisconnected();
 +      }
 +    }
 +    else {
 +      this.dm = new LonerDistributionManager(this, this.logWriter);
 +    }
 +    
 +    Assert.assertTrue(this.dm != null);
 +    Assert.assertTrue(this.dm.getSystem() == this);
 +
 +    try {
 +      this.id = this.dm.getChannelId();
 +    } catch (DistributedSystemDisconnectedException e) {
 +      // bug #48144 - The dm's channel threw an NPE.  It now throws this exception
 +      // but during startup we should instead throw a SystemConnectException
 +      throw new SystemConnectException(
 +          LocalizedStrings.InternalDistributedSystem_DISTRIBUTED_SYSTEM_HAS_DISCONNECTED
 +            .toLocalizedString(), e);
 +    }
 +
 +    synchronized (this.isConnectedMutex) {
 +      this.isConnected = true;
 +    }
 +    if (attemptingToReconnect  &&  (this.startedLocator == null)) {
 +      try {
 +        startInitLocator();
 +      } catch (InterruptedException e) {
 +        throw new SystemConnectException("Startup has been interrupted", e);
 +      }
 +    }
 +    try {
 +      endInitLocator();
 +    }
 +    catch (IOException e) {
 +      throw new GemFireIOException("Problem finishing a locator service start", e);
 +    }
 +
 +      if (!statsDisabled) {
 +        // to fix bug 42527 we need a sampler
 +        // even if sampling is not enabled.
 +      this.sampler = new GemFireStatSampler(this);
 +      this.sampler.start();
 +    }
 +
 +    if (this.logWriterAppender != null) {
 +      LogWriterAppenders.startupComplete(LogWriterAppenders.Identifier.MAIN);
 +    }
 +    if (this.securityLogWriterAppender != null) {
 +      LogWriterAppenders.startupComplete(LogWriterAppenders.Identifier.SECURITY);
 +    }
 +    
 +    //this.logger.info("ds created", new RuntimeException("DEBUG: STACK"));
 +
 +    //Log any instantiators that were registered before the log writer
 +    //was created
 +    InternalInstantiator.logInstantiators();
 +    }
 +    catch (RuntimeException ex) {
 +      this.config.close();
 +      throw ex;
 +    }
 +    
 +    resourceListeners = new CopyOnWriteArrayList<ResourceEventsListener>();
 +    this.reconnected = this.attemptingToReconnect;
 +    this.attemptingToReconnect = false;
 +  }
 +
 +  /**
 +   * @since 5.7
 +   */
 +  private void startInitLocator() throws InterruptedException {
 +    String locatorString = this.originalConfig.getStartLocator();
 +    if (locatorString.length() > 0) {
 +      // when reconnecting we don't want to join with a colocated locator unless
 +      // there is a quorum of the old members available
 +      if (attemptingToReconnect && !this.isConnected) {
 +        if (this.quorumChecker != null) {
 +          logger.info("performing a quorum check to see if location services can be started early");
 +          if (!quorumChecker.checkForQuorum(3*this.config.getMemberTimeout())) {
 +            logger.info("quorum check failed - not allowing location services to start early");
 +            return;
 +          }
 +          logger.info("Quorum check passed - allowing location services to start early");
 +        }
 +      }
 +      DistributionLocatorId locId = new DistributionLocatorId(locatorString);
 +      try {
 +        this.startedLocator = InternalLocator.createLocator(
 +            locId.getPort(), 
 +            null,
 +            null,
 +            this.logWriter, // LOG: this is after IDS has created LogWriterLoggers and Appenders 
 +            this.securityLogWriter, // LOG: this is after IDS has created LogWriterLoggers and Appenders
 +            locId.getHost(),
 +            locId.getHostnameForClients(),
 +            this.originalConfig.toProperties(), false);
 +        if(locId.isPeerLocator()) {
 +          boolean startedPeerLocation = false;
 +          try {
 +            this.startedLocator.startPeerLocation(true);
 +            startedPeerLocation = true;
 +          } finally {
 +            if (!startedPeerLocation) {
 +              this.startedLocator.stop();
 +            }
 +          }
 +        }
 +      } catch (IOException e) {
 +        throw new GemFireIOException( LocalizedStrings.
 +          InternalDistributedSystem_PROBLEM_STARTING_A_LOCATOR_SERVICE
 +            .toLocalizedString(), e);
 +      }
 +    }
 +  }
 +  
 +  /**
 +   * @since 5.7
 +   */
 +  private void endInitLocator() throws IOException {
 +    InternalLocator loc = this.startedLocator;
 +    if (loc != null) {
 +      String locatorString = this.originalConfig.getStartLocator();
 +//      DistributionLocatorId locId = new DistributionLocatorId(locatorString);
 +      boolean finished = false;
 +      try {
 +        // during the period when the product is using only paper licenses we always
 +        // start server location services in order to be able to log information
 +        // about the use of cache servers
 +        //      if(locId.isServerLocator()) {
 +        loc.startServerLocation(this);
 +        //      }
 +      
 +        loc.endStartLocator(this);
 +        finished = true;
 +      } finally {
 +        if (!finished) {
 +          loc.stop();
 +        }
 +      }
 +    }
 +  }
 +
 +  /** record a locator as a dependent of this distributed system */
 +  public void setDependentLocator(InternalLocator theLocator) {
 +    this.startedLocator = theLocator;
 +  }
 +  
 +  /** Used by DistributionManager to fix bug 33362
 +   */
 +  void setDM(DM dm) {
 +    this.dm = dm;
 +  }
 +
 +  /**
 +   * Checks whether or not this connection to a distributed system is
 +   * closed.
 +   *
 +   * @throws DistributedSystemDisconnectedException
 +   *         This connection has been {@link #disconnect(boolean, String, boolean) disconnected}
 +   */
 +  private void checkConnected() {
 +    if (!isConnected()) {
 +      throw new DistributedSystemDisconnectedException(LocalizedStrings.InternalDistributedSystem_THIS_CONNECTION_TO_A_DISTRIBUTED_SYSTEM_HAS_BEEN_DISCONNECTED.toLocalizedString(),
 +          dm.getRootCause());
 +    }
 +  }
 +
 +  @Override
 +  public boolean isConnected() {
 +    if (this.dm == null) {
 +      return false;
 +    }
 +    if (this.dm.getCancelCriterion().cancelInProgress() != null) {
 +      return false;
 +    }
 +    if (this.isDisconnecting) {
 +      return false;
 +    }
 +    return this.isConnected;
 +  }
 +  
 +  /**
 +   * This class defers to the DM.  If we don't have a DM, we're dead.
 +   * @author jpenney
 +   */
 +  protected class Stopper extends CancelCriterion {
 +    @Override
 +    public String cancelInProgress() {
 +      checkFailure();
 +      if (dm == null) {
 +        return "No dm";
 +      }
 +      return dm.getCancelCriterion().cancelInProgress();
 +    }
 +    
 +    @Override
 +    public RuntimeException generateCancelledException(Throwable e) {
 +      if (dm == null) {
 +        return new DistributedSystemDisconnectedException("no dm", e);
 +      }
 +      return dm.getCancelCriterion().generateCancelledException(e);
 +    }
 +  }
 +  
 +  /**
 +   * Handles all cancellation queries for this distributed system
 +   */
 +  private final Stopper stopper = new Stopper();
 +  
 +  @Override
 +  public CancelCriterion getCancelCriterion() {
 +    return stopper;
 +  }
 +  
 +  public boolean isDisconnecting() {
 +    if (this.dm == null) {
 +      return true;
 +    }
 +    if (this.dm.getCancelCriterion().cancelInProgress() != null) {
 +      return true;
 +    }
 +    if (!this.isConnected) {
 +      return true;
 +    }
 +    return this.isDisconnecting;
 +    }
 +
 +  @Override
 +  public LogWriter getLogWriter() {
 +    return this.logWriter;
 +  }
 +  
 +  public DSClock getClock() {
 +    return this.clock;
 +  }
 +
 +  @Override
 +  public LogWriter getSecurityLogWriter() {
 +    return this.securityLogWriter;
 +  }
 +
 +  /*
 +  public Cache myCache;
 +
 +  public void setCache(Cache cache){
 +	  myCache=cache;
 +  }
 +  public Cache getCache(){
 +	  return myCache;
 +  }
 +  */
 +  /**
 +   * Returns the stat sampler
 +   */
 +  public GemFireStatSampler getStatSampler() {
 +    return this.sampler;
 +  }
 +  
 +  /** Has this system started the disconnect process? */
 +  protected volatile boolean isDisconnecting = false;
 +  
 +  /**
 +   * Disconnects this VM from the distributed system.  Shuts down the
 +   * distribution manager, and if necessary,
 +   */
 +  @Override
 +  public void disconnect() {
 +    disconnect(false, LocalizedStrings.InternalDistributedSystem_NORMAL_DISCONNECT.toLocalizedString(), false);
 +  }
 +  
 +  /**
 +   * Disconnects this member from the distributed system when an internal
 +   * error has caused distribution to fail (e.g., this member was shunned)
 +   * @param reason a string describing why the disconnect is occurring
 +   * @param cause an optional exception showing the reason for abnormal disconnect
 +   * @param shunned whether this member was shunned by the membership coordinator
 +   */
 +  public void disconnect(String reason, Throwable cause, boolean shunned) {
 +    boolean isForcedDisconnect = dm.getRootCause() instanceof ForcedDisconnectException;
 +    boolean reconnected = false;
 +    this.reconnected = false;
 +    if (isForcedDisconnect) {
 +      this.forcedDisconnect = true;
 +      resetReconnectAttemptCounter();
 +    
 +     reconnected = tryReconnect(true, reason, GemFireCacheImpl.getInstance());
 +    }
 +    if (!reconnected) {
 +      disconnect(false, reason, shunned);
 +    }
 +  }
 +  
 +  /**
 +   * This is how much time, in milliseconds to allow a disconnect listener
 +   * to run before we interrupt it.
 +   */
 +  static private final long MAX_DISCONNECT_WAIT =
 +    Long.getLong("DistributionManager.DISCONNECT_WAIT", 
 +        10 * 1000).longValue();
 +
 +  /**
 +   * Run a disconnect listener, checking for errors and
 +   * honoring the timeout {@link #MAX_DISCONNECT_WAIT}.
 +   *
 +   * @param dc the listener to run
 +   */
 +  private void runDisconnect(final DisconnectListener dc,
 +      ThreadGroup tg) {
 +    // Create a general handler for running the disconnect
 +    Runnable r = new Runnable() {
 +      public void run() {
 +        try {
 +          disconnectListenerThread.set(Boolean.TRUE);
 +          dc.onDisconnect(InternalDistributedSystem.this);
 +        }
 +        catch (CancelException e) {
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("Disconnect listener <{}> thwarted by cancellation: {}", dc, e, logger.isTraceEnabled() ? e : null);
 +          }
 +        }
 +      }
 +    };
 +
 +    // Launch it and wait a little bit
 +    Thread t = new Thread(tg, r, dc.toString());
 +    try {
 +      t.start();
 +      t.join(MAX_DISCONNECT_WAIT);
 +    }
 +    catch (InterruptedException e) {
 +      Thread.currentThread().interrupt();
 +      logger.warn(LocalizedMessage.create(LocalizedStrings.InternalDistributedSystem_INTERRUPTED_WHILE_PROCESSING_DISCONNECT_LISTENER), e);
 +    }
 +
 +    // Make sure the listener gets the cue to die
 +    if (t.isAlive()) {
 +      logger.warn(LocalizedMessage.create(LocalizedStrings.InternalDistributedSystem_DISCONNECT_LISTENER_STILL_RUNNING__0, dc));
 +      t.interrupt();
 +
 +      try {
 +        t.join(MAX_DISCONNECT_WAIT);
 +      }
 +      catch (InterruptedException e) {
 +        Thread.currentThread().interrupt();
 +      }
 +      
 +      if (t.isAlive()) {
 +        logger.warn(LocalizedMessage.create(LocalizedStrings.InternalDistributedSystem_DISCONNECT_LISTENER_IGNORED_ITS_INTERRUPT__0, dc));
 +      }
 +    }
 +    
 +  }
 +  
 +  public boolean isDisconnectListenerThread() {
 +    Boolean disconnectListenerThreadBoolean = 
 +      (Boolean) this.disconnectListenerThread.get();
 +
 +    return disconnectListenerThreadBoolean != null &&
 +           disconnectListenerThreadBoolean.booleanValue();
 +  }
 +  
 +  /**
 +   * Run a disconnect listener in the same thread sequence as the reconnect.
 +   * @param dc the listener to run
 +   * @param tg the thread group to run the listener in
 +   */
 +
 +  private void runDisconnectForReconnect(final DisconnectListener dc,
 +      ThreadGroup tg){
 +    try {
 +      dc.onDisconnect(InternalDistributedSystem.this);
 +    } catch (DistributedSystemDisconnectedException e) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("Disconnect listener <{}> thwarted by shutdown: {}", dc, e, logger.isTraceEnabled() ? e : null);
 +      }
 +    }
 +  }
 +
 +  /**
 +   * A logging thread group for the disconnect and shutdown listeners
 +   */
 +  private final ThreadGroup disconnectListenerThreadGroup = 
 +    LoggingThreadGroup.createThreadGroup("Disconnect Listeners");
 +  
 +  /**
 +   * Disconnect cache, run disconnect listeners.
 +   * 
 +   * @param doReconnect whether a reconnect will be done
 +   * @param reason the reason that the system is disconnecting
 +   * 
 +   * @return a collection of shutdownListeners
 +   */
 +  private HashSet doDisconnects(boolean doReconnect, String reason) {
 +    // Make a pass over the disconnect listeners, asking them _politely_
 +    // to clean up.
 +    HashSet shutdownListeners = new HashSet();
 +    for (;;) {
 +      DisconnectListener listener = null;
 +      synchronized (this.listeners) {
 +        Iterator itr = listeners.iterator();
 +        if (!itr.hasNext()) {
 +          break;
 +        }
 +        listener = (DisconnectListener)itr.next();
 +        if (listener instanceof ShutdownListener) {
 +          shutdownListeners.add(listener);
 +        }
 +        itr.remove();
 +      } // synchronized
 +     
 +      if (doReconnect){
 +        runDisconnectForReconnect(listener, disconnectListenerThreadGroup);
 +      } else {
 +        runDisconnect(listener, disconnectListenerThreadGroup);
 +      }
 +    } // for
 +    return shutdownListeners;
 +  }
 +  
 +  /**
 +   * Process the shutdown listeners.  It is essential that the DM has been
 +   * shut down before calling this step, to ensure that no new listeners are
 +   * registering.
 +   * 
 +   * @param shutdownListeners shutdown listeners initially registered with us
 +   */
 +  private void doShutdownListeners(HashSet shutdownListeners) {
 +    if (shutdownListeners == null) {
 +      return;
 +    }
 +
 +    // Process any shutdown listeners we reaped during first pass
 +    Iterator it = shutdownListeners.iterator();
 +    while (it.hasNext()) {
 +      ShutdownListener s = (ShutdownListener)it.next();
 +      try {
 +        s.onShutdown(this);
 +      }
 +      catch (VirtualMachineError err) {
 +        SystemFailure.initiateFailure(err);
 +        // If this ever returns, rethrow the error.  We're poisoned
 +        // now, so don't let this thread continue.
 +        throw err;
 +      }
 +      catch (Throwable t) {
 +        // Whenever you catch Error or Throwable, you must also
 +        // catch VirtualMachineError (see above).  However, there is
 +        // _still_ a possibility that you are dealing with a cascading
 +        // error condition, so you also need to check to see if the JVM
 +        // is still usable:
 +        SystemFailure.checkFailure();
 +        // things could break since we continue, but we want to disconnect!
 +        logger.fatal(LocalizedMessage.create(LocalizedStrings.InternalDistributedSystem_SHUTDOWNLISTENER__0__THREW, s), t);
 +      }
 +    }
 +
 +    // During the window while we were running disconnect listeners, new
 +    // disconnect listeners may have appeared. After messagingDisabled is
 +    // set, no new ones will be created.  However, we must process any
 +    // that appeared in the interim.
 +    for (;;) {
 +      // Pluck next listener from the list
 +      DisconnectListener dcListener = null;
 +      ShutdownListener sdListener = null;
 +      synchronized (this.listeners) {
 +        Iterator itr = listeners.iterator();
 +        if (!itr.hasNext())
 +          break;
 +        dcListener = (DisconnectListener)itr.next();
 +        itr.remove();
 +        if (dcListener instanceof ShutdownListener)
 +          sdListener = (ShutdownListener)dcListener;
 +      }
 +
 +      // Run the disconnect
 +      runDisconnect(dcListener, disconnectListenerThreadGroup);
 +
 +      // Run the shutdown, if any
 +      if (sdListener != null) {
 +        try {
 +          // TODO: should we make sure this times out?
 +          sdListener.onShutdown(this);
 +        }
 +        catch (VirtualMachineError err) {
 +          SystemFailure.initiateFailure(err);
 +          // If this ever returns, rethrow the error.  We're poisoned
 +          // now, so don't let this thread continue.
 +          throw err;
 +        }
 +        catch (Throwable t) {
 +          // Whenever you catch Error or Throwable, you must also
 +          // catch VirtualMachineError (see above).  However, there is
 +          // _still_ a possibility that you are dealing with a cascading
 +          // error condition, so you also need to check to see if the JVM
 +          // is still usable:
 +          SystemFailure.checkFailure();
 +          // things could break since we continue, but we want to disconnect!
 +          logger.fatal(LocalizedMessage.create(LocalizedStrings.InternalDistributedSystem_DISCONNECTLISTENERSHUTDOWN_THREW), t);
 +        }
 +      }
 +    } // for
 +  }
 +  
 +  /**
 +   * break any potential circularity in {@link #loadEmergencyClasses()}
 +   */
 +  private static volatile boolean emergencyClassesLoaded = false;
 +  
 +  /**
 +   * Ensure that the MembershipManager class gets loaded.
 +   * 
 +   * @see SystemFailure#loadEmergencyClasses()
 +   */
 +  static public void loadEmergencyClasses() {
 +    if (emergencyClassesLoaded) return;
 +    emergencyClassesLoaded = true;
 +    GMSMembershipManager.loadEmergencyClasses();
 +  }
 +  
 +  /**
 +   * Closes the membership manager
 +   * 
 +   * @see SystemFailure#emergencyClose()
 +   */
 +  public void emergencyClose() {
 +    final boolean DEBUG = SystemFailure.TRACE_CLOSE;
 +    if (dm != null) {
 +      MembershipManager mm = dm.getMembershipManager();
 +      if (mm != null) {
 +        if (DEBUG) {
 +          System.err.println("DEBUG: closing membership manager");
 +        }
 +        mm.emergencyClose();
 +        if (DEBUG) {
 +          System.err.println("DEBUG: back from closing membership manager");
 +        }
 +      }
 +    }
 +    
 +    // Garbage collection
 +    // Leave dm alone; its CancelCriterion will help people die
 +    this.isConnected = false;
 +    if (dm != null) {
 +      dm.setRootCause(SystemFailure.getFailure());
 +    }
 +    this.isDisconnecting = true;
 +    this.listeners.clear();
 +    if (DEBUG) {
 +      System.err.println("DEBUG: done with InternalDistributedSystem#emergencyClose");
 +    }
 +  }
 +  
 +  private void setDisconnected() {
 +    synchronized (this.isConnectedMutex) {
 +      this.isConnected = false;
 +      isConnectedMutex.notifyAll();
 +    }
 +  }
 +
 +  private void waitDisconnected() {
 +    synchronized (this.isConnectedMutex) {
 +      while (this.isConnected) {
 +        boolean interrupted = Thread.interrupted();
 +        try {
 +          this.isConnectedMutex.wait();
 +        }
 +        catch (InterruptedException e) {
 +          interrupted = true;
 +          getLogWriter().convertToLogWriterI18n().warning(
 +            LocalizedStrings.InternalDistributedSystem_DISCONNECT_WAIT_INTERRUPTED, e);
 +        }
 +        finally {
 +          if (interrupted) {
 +            Thread.currentThread().interrupt();
 +          }
 +        }
 +      } // while
 +    }
 +  }
 +
 +  /**
 +   * Disconnects this VM from the distributed system. Shuts down the
 +   * distribution manager.
 +   * 
 +   * @param preparingForReconnect
 +   *          true if called by a reconnect operation
 +   * @param reason
 +   *          the reason the disconnect is being performed
 +   * @param keepAlive
 +   *          true if user requested durable subscriptions are to be retained at
 +   *          server.
 +   */
 +  protected void disconnect(boolean preparingForReconnect, String reason, boolean keepAlive) {
 +    boolean isShutdownHook = (shutdownHook != null)
 +                             && (Thread.currentThread() == shutdownHook);
 +
 +    if (!preparingForReconnect) {
 +//      logger.info("disconnecting IDS@"+System.identityHashCode(this));
 +      synchronized(reconnectListeners) {
 +        reconnectListeners.clear();
 +      }
 +      cancelReconnect();
 +    }
 +    
 +    final boolean isDebugEnabled = logger.isDebugEnabled();
 +    try {
 +      HashSet shutdownListeners = null;
 +      try {
 +        if (isDebugEnabled) {
 +          logger.debug("DistributedSystem.disconnect invoked on {}", this);
 +        }
 +        synchronized (GemFireCacheImpl.class) {
 +          // bug 36955, 37014: don't use a disconnect listener on the cache;
 +          // it takes too long.
 +          //
 +          // However, make sure cache is completely closed before starting
 +          // the distributed system close.
 +          GemFireCacheImpl currentCache = GemFireCacheImpl.getInstance();
 +          if (currentCache != null && !currentCache.isClosed()) {
 +            disconnectListenerThread.set(Boolean.TRUE); // bug #42663 - this must be set while closing the cache
 +            try {
 +              currentCache.close(reason, dm.getRootCause(), keepAlive, true); // fix for 42150
 +            } 
 +            catch (VirtualMachineError e) {
 +              SystemFailure.initiateFailure(e);
 +              throw e;
 +            }
 +            catch (Throwable e) {
 +              SystemFailure.checkFailure();
 +              // Whenever you catch Error or Throwable, you must also
 +              // check for fatal JVM error (see above).  However, there is
 +              logger.warn(LocalizedMessage.create(LocalizedStrings.InternalDistributedSystem_EXCEPTION_TRYING_TO_CLOSE_CACHE), e);
 +            }
 +            finally {
 +              disconnectListenerThread.set(Boolean.FALSE);
 +            }
 +          } 
 +
 +          // While still holding the lock, make sure this instance is
 +          // marked as shutting down
 +          synchronized (this) {
 +            if (this.isDisconnecting) {
 +              // It's already started, but don't return
 +              // to the caller until it has completed.
 +              waitDisconnected();
 +              return;
 +            } // isDisconnecting
 +            this.isDisconnecting = true;
 +
 +            if (!preparingForReconnect) {
 +              // move cancelReconnect above this synchronized block fix for bug 35202
 +              if (this.reconnectDS != null) {
 +                // break recursion
 +                if (isDebugEnabled) {
 +                  logger.debug("disconnecting reconnected DS: {}", this.reconnectDS);
 +                }
 +                InternalDistributedSystem r = this.reconnectDS;
 +                this.reconnectDS = null;
 +                r.disconnect(false, null, false);
 +              }
 +            } // !reconnect
 +          } // synchronized (this)
 +        } // synchronized (GemFireCache.class)
 +
 +        if (!isShutdownHook) {
 +          shutdownListeners = doDisconnects(attemptingToReconnect, reason);
 +        }
 +    
 +        if (!this.attemptingToReconnect) {
 +          if (this.logWriterAppender != null) {
 +            LogWriterAppenders.stop(LogWriterAppenders.Identifier.MAIN);
 +          }
 +          if (this.securityLogWriterAppender != null) {
 +            // LOG:SECURITY: old code did NOT invoke this
 +            LogWriterAppenders.stop(LogWriterAppenders.Identifier.SECURITY);
 +          }
 +        }
 +        
 +        AlertAppender.getInstance().shuttingDown();
 +        
 +      }
 +      finally { // be ABSOLUTELY CERTAIN that dm closed
 +        try {
 +          // Do the bulk of the close...
 +          this.dm.close();
 +          // we close the locator after the DM so that when split-brain detection
 +          // is enabled, loss of the locator doesn't cause the DM to croak
 +          if (this.startedLocator != null) {
 +            this.startedLocator.stop(forcedDisconnect, preparingForReconnect, false);
 +            this.startedLocator = null;
 +          }
 +        } finally { // timer canceled
 +          // bug 38501: this has to happen *after*
 +          // the DM is closed :-(
 +          if (!preparingForReconnect) {
 +            SystemTimer.cancelSwarm(this);
 +          }
 +        } // finally timer cancelled
 +      } // finally dm closed
 +
 +      if (!isShutdownHook) {
 +        doShutdownListeners(shutdownListeners);
 +      }
 +
 +      // closing the Aggregate stats
 +      if(functionServiceStats != null){
 +        functionServiceStats.close();
 +      }
 +      // closing individual function stats
 +      for (FunctionStats functionstats : functionExecutionStatsMap.values()) {
 +        functionstats.close();
 +      }
 +
 +      (new FunctionServiceManager()).unregisterAllFunctions();
 +
 +      if (this.sampler != null) {
 +        this.sampler.stop();
 +        this.sampler = null;
 +      }
 +
 +      if (!this.attemptingToReconnect) {
 +        if (this.logWriterAppender != null) {
 +          LogWriterAppenders.destroy(LogWriterAppenders.Identifier.MAIN);
 +        }
 +        if (this.securityLogWriterAppender != null) {
 +          LogWriterAppenders.destroy(LogWriterAppenders.Identifier.SECURITY);
 +        }
 +      }
 +
 +      // NOTE: no logging after this point :-)
 +
 +      LoggingThreadGroup.cleanUpThreadGroups(); // bug35388 - logwriters accumulate, causing mem leak
 +      EventID.unsetDS();
 +
 +    }
 +    finally {
 +      try {
 +        if (getOffHeapStore() != null) {
 +          getOffHeapStore().close();
 +        }
 +      } finally {
 +      try {
 +        removeSystem(this);
 +        // Close the config object
 +        this.config.close();
 +      }
 +      finally {
 +        // Finally, mark ourselves as disconnected
 +        setDisconnected();
 +        SystemFailure.stopThreads();
 +      }
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Returns the distribution manager for accessing this distributed system.
 +   */
 +  public DM getDistributionManager() {
 +    checkConnected();
 +    return this.dm;
 +  }
 +
 +  /**
 +   * Returns the distribution manager without checking for connected or not so
 +   * can also return null.
 +   */
 +  public final DM getDM() {
 +    return this.dm;
 +  }
 +  
 +  /**
 +   * If this DistributedSystem is attempting to reconnect to the distributed system
 +   * this will return the quorum checker created by the old MembershipManager for
 +   * checking to see if a quorum of old members can be reached.
 +   * @return the quorum checking service
 +   */
 +  public final QuorumChecker getQuorumChecker() {
 +    return this.quorumChecker;
 +  }
 +  
 +  /**
 +   * Returns true if this DS has been attempting to reconnect but
 +   * the attempt has been cancelled.
 +   */
 +  public boolean isReconnectCancelled() {
 +    return this.reconnectCancelled;
 +  }
 +
 +  /**
 +   * Returns whether or not this distributed system has the same
 +   * configuration as the given set of properties.
 +   *
 +   * @see DistributedSystem#connect
 +   */
 +  public boolean sameAs(Properties props, boolean isConnected) {
 +    return originalConfig.sameAs(DistributionConfigImpl.produce(props, isConnected));
 +  }
 +
 +  public boolean threadOwnsResources() {
 +    Boolean b = ConnectionTable.getThreadOwnsResourcesRegistration();
 +    if (b == null) {
 +      // thread does not have a preference so return default
 +      return !this.shareSockets;
 +    } else {
 +      return b.booleanValue();
 +    }
 +  }
 +
 +  /**
 +   * Returns whether or not the given configuration properties refer
 +   * to the same distributed system as this
 +   * <code>InternalDistributedSystem</code> connection.
 +   *
 +   * @since 4.0
 +   */
 +  public boolean sameSystemAs(Properties props) {
 +    DistributionConfig other = DistributionConfigImpl.produce(props);
 +    DistributionConfig me = this.getConfig();
 +
 +    if (!me.getBindAddress().equals(other.getBindAddress())) {
 +      return false;
 +    }
 +
 +    // @todo Do we need to compare SSL properties?
 +
 +    // locators
 +    String myLocators = me.getLocators();
 +    String otherLocators = other.getLocators();
 +
 +    // quick check
 +    if (myLocators.equals(otherLocators)) {
 +      return true;
 +
 +    } else {
 +      myLocators = canonicalizeLocators(myLocators);
 +      otherLocators = canonicalizeLocators(otherLocators);
 +
 +      return myLocators.equals(otherLocators);
 +    }
 +  }
 +
 +  /**
 +   * Canonicalizes a locators string so that they may be compared.
 +   *
 +   * @since 4.0
 +   */
 +  private static String canonicalizeLocators(String locators) {
 +    SortedSet sorted = new TreeSet();
 +    StringTokenizer st = new StringTokenizer(locators, ",");
 +    while (st.hasMoreTokens()) {
 +      String l = st.nextToken();
 +      StringBuffer canonical = new StringBuffer();
 +      DistributionLocatorId locId = new DistributionLocatorId(l);
 +      String addr = locId.getBindAddress();
 +      if (addr != null && addr.trim().length() > 0) {
 +        canonical.append(addr);
 +      }
 +      else {
 +        canonical.append(locId.getHost().getHostAddress());
 +      }
 +      canonical.append("[");
 +      canonical.append(String.valueOf(locId.getPort()));
 +      canonical.append("]");
 +      sorted.add(canonical.toString());
 +    }
 +
 +    StringBuffer sb = new StringBuffer();
 +    for (Iterator iter = sorted.iterator(); iter.hasNext(); ) {
 +      sb.append((String) iter.next());
 +      if (iter.hasNext()) {
 +        sb.append(",");
 +      }
 +    }
 +    return sb.toString();
 +  }
 +
 +  private final StoppableReentrantLock elderLock;
 +  private final StoppableCondition elderLockCondition;
 +  
 +  public StoppableReentrantLock getElderLock() {
 +    return elderLock;
 +  }
 +  public StoppableCondition getElderLockCondition() {
 +    return elderLockCondition;
 +  }
 +  
 +  /**
 +   * Returns the current configuration of this distributed system.
 +   */
 +  public DistributionConfig getConfig() {
 +    return this.config;
 +  }
 +
 +  /**
 +   * Returns the id of this connection to the distributed system.
 +   * This is actually the port of the distribution manager's
 +   * distribution channel.
 +   *
 +   * @see com.gemstone.gemfire.distributed.internal.DistributionChannel#getId
 +   */
 +  @Override
 +  public long getId() {
 +    return this.id;
 +  }
 +
 +  /**
 +   * Returns the string value of the distribution manager's id.
 +   */
 +  @Override
 +  public String getMemberId() {
 +    return String.valueOf(this.dm.getId());
 +  }
 +
 +  @Override
 +  public InternalDistributedMember getDistributedMember() {
 +    return this.dm.getId();
 +  }
 +  @Override
 +  public Set<DistributedMember> getAllOtherMembers() {
 +    return dm.getAllOtherMembers();
 +  }
 +  @Override
 +  public Set<DistributedMember> getGroupMembers(String group) {
 +    return dm.getGroupMembers(group);
 +  }
 +  
 +  
 +
 +  @Override
 +  public Set<DistributedMember> findDistributedMembers(InetAddress address) {
 +    Set<InternalDistributedMember> allMembers = dm.getDistributionManagerIdsIncludingAdmin();
 +    Set<DistributedMember> results = new HashSet<DistributedMember>(2);
 +    
 +    //Search through the set of all members
 +    for(InternalDistributedMember member: allMembers) {
 +      
 +      Set<InetAddress> equivalentAddresses = dm.getEquivalents(member.getInetAddress());
 +      //Check to see if the passed in address is matches one of the addresses on
 +      //the given member.
 +      if(address.equals(member.getInetAddress()) || equivalentAddresses.contains(address)) {
 +        results.add(member);
 +      }
 +    }
 +    
 +    return results;
 +  }
 +
 +  @Override
 +  public DistributedMember findDistributedMember(String name) {
 +    Set<DistributedMember> allMembers = dm.getDistributionManagerIdsIncludingAdmin();
 +    for(DistributedMember member : allMembers) {
 +      if(member.getName().equals(name)) {
 +        return member;
 +      }
 +    }
 +    return null;
 +  }
 +
 +  /**
 +   * Returns the configuration this distributed system was created with.
 +   */
 +  public DistributionConfig getOriginalConfig() {
 +    return this.originalConfig;
 +  }
 +  @Override
 +  public String getName() {
 +    return getOriginalConfig().getName();
 +  }
 +
 +  ///////////////////////  Utility Methods  ///////////////////////
 +
 +  /**
 +   * Since {@link DistributedSystem#connect} guarantees that there is
 +   * a canonical instance of <code>DistributedSystem</code> for each
 +   * configuration, we can use the default implementation of
 +   * <code>equals</code>.
 +   *
 +   * @see #sameAs
 +   */
 +  @Override
 +  public boolean equals(Object o) {
 +    return super.equals(o);
 +  }
 +
 +  /**
 +   * Since we use the default implementation of {@link #equals
 +   * equals}, we can use the default implementation of
 +   * <code>hashCode</code>.
 +   */
 +  @Override
 +  public int hashCode() {
 +    return super.hashCode();
 +  }
 +
 +  /**
 +   * Returns a string describing this connection to distributed system
 +   * (including highlights of its configuration).
 +   */
 +  @Override
 +  public String toString() {
 +    StringBuffer sb = new StringBuffer();
 +    sb.append("Connected ");
 +    String name = this.getName();
 +    if (name != null && !name.equals("")) {
 +      sb.append("\"");
 +      sb.append(name);
 +      sb.append("\" ");
 +    }
 +    sb.append("(id=");
 +    sb.append(Integer.toHexString(System.identityHashCode(this)));
 +    sb.append(") ");
 +
 +    sb.append("to distributed system using ");
 +    int port = this.config.getMcastPort();
 +    if (port != 0) {
 +      sb.append("multicast port ");
 +      sb.append(port);
 +      sb.append(" ");
 +
 +    } else {
 +      sb.append("locators \"");
 +      sb.append(this.config.getLocators());
 +      sb.append("\" ");
 +    }
 +
 +    File logFile = this.config.getLogFile();
 +    sb.append("logging to ");
 +    if (logFile == null || logFile.equals(new File(""))) {
 +      sb.append("standard out ");
 +
 +    } else {
 +      sb.append(logFile);
 +      sb.append(" ");
 +    }
 +
 +    sb.append(" started at ");
 +    sb.append((new Date(this.startTime)).toString());
 +    
 +    if (!this.isConnected()) {
 +      sb.append(" (closed)");
 +    }
 +
 +    return sb.toString().trim();
 +  }
 +
 +  private final ArrayList<Statistics> statsList = new ArrayList<Statistics>();
 +  private int statsListModCount = 0;
 +  private long statsListUniqueId = 1;
 +  private final Object statsListUniqueIdLock = new Object();
 +  
 +  // As the function execution stats can be lot in number, its better to put
 +  // them in a map so that it will be accessible immediately
 +  private final ConcurrentHashMap<String, FunctionStats>  functionExecutionStatsMap = new ConcurrentHashMap<String, FunctionStats>();
 +  private FunctionServiceStats functionServiceStats = null;
 +  
 +  public int getStatListModCount() {
 +    return this.statsListModCount;
 +  }
 +  public List<Statistics> getStatsList() {
 +    return this.statsList;
 +  }
 +
 +  @Override
 +  public final int getStatisticsCount() {
 +    int result = 0;
 +    List<Statistics> statsList = this.statsList;
 +    if (statsList != null) {
 +      result = statsList.size();
 +    }
 +    return result;
 +  }
 +  
 +  @Override
 +  public final Statistics findStatistics(long id) {
 +    List<Statistics> statsList = this.statsList;
 +    synchronized (statsList) {
 +      for (Statistics s : statsList) {
 +        if (s.getUniqueId() == id) {
 +          return s;
 +        }
 +      }
 +    }
 +    throw new RuntimeException(LocalizedStrings.PureStatSampler_COULD_NOT_FIND_STATISTICS_INSTANCE.toLocalizedString());
 +  }
 +  
 +  @Override
 +  public final boolean statisticsExists(long id) {
 +    List<Statistics> statsList = this.statsList;
 +    synchronized (statsList) {
 +      for (Statistics s : statsList) {
 +        if (s.getUniqueId() == id) {
 +          return true;
 +        }
 +      }
 +    }
 +    return false;
 +  }
 +
 +  @Override
 +  public final Statistics[] getStatistics() {
 +    List<Statistics> statsList = this.statsList;
 +    synchronized (statsList) {
 +      return (Statistics[])statsList.toArray(new Statistics[statsList.size()]);
 +    }
 +  }
 +  
 +  // StatisticsFactory methods
 +  public Statistics createStatistics(StatisticsType type) {
 +    return createOsStatistics(type, null, 0, 0);
 +  }
 +  public Statistics createStatistics(StatisticsType type, String textId) {
 +    return createOsStatistics(type, textId, 0, 0);
 +  }
 +  public Statistics createStatistics(StatisticsType type, String textId, long numericId) {
 +    return createOsStatistics(type, textId, numericId, 0);
 +  }
 +  public Statistics createOsStatistics(StatisticsType type, String textId, long numericId, int osStatFlags) {
 +    if (this.statsDisabled) {
 +      return new DummyStatisticsImpl(type, textId, numericId);
 +    }
 +    long myUniqueId;
 +    synchronized (statsListUniqueIdLock) {
 +      myUniqueId = statsListUniqueId++; // fix for bug 30597
 +    }
 +    Statistics result = new LocalStatisticsImpl(type, textId, numericId, myUniqueId, false, osStatFlags, this);
 +    synchronized (statsList) {
 +      statsList.add(result);
 +      statsListModCount++;
 +    }
 +    return result;
 +  }
 +
 +  public FunctionStats getFunctionStats(String textId) {
 +    FunctionStats stats = (FunctionStats)functionExecutionStatsMap.get(textId);
 +    if (stats == null) {
 +      stats = new FunctionStats(this, textId);
 +      FunctionStats oldStats = functionExecutionStatsMap.putIfAbsent(textId,
 +          stats);
 +      if (oldStats != null) {
 +        stats.close();
 +        stats = oldStats;
 +      }
 +    }
 +    return stats;
 +  }
 +
 +  
 +  public FunctionServiceStats getFunctionServiceStats() {
 +    if (functionServiceStats == null) {
 +      synchronized (this) {
 +        if(functionServiceStats == null){
 +          functionServiceStats = new FunctionServiceStats(this, "FunctionExecution");
 +        }
 +      }
 +    }
 +    return functionServiceStats;
 +  }
 +
 +  /**
 +   * For every registered statistic instance call the specified visitor.
 +   * This method was added to fix bug 40358
 +   */
 +  public void visitStatistics(StatisticsVisitor visitor) {
 +    synchronized (this.statsList) {
 +      for (Statistics s: this.statsList) {
 +        visitor.visit(s);
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Used to "visit" each instance of Statistics registered with
 +   * @see #visitStatistics
 +   */
 +  public interface StatisticsVisitor {
 +    public void visit(Statistics stat);
 +  }
 +  
 +  public Set<String> getAllFunctionExecutionIds() {
 +    return functionExecutionStatsMap.keySet();
 +  }
 +  
 +  
 +  public Statistics[] findStatisticsByType(final StatisticsType type) {
 +    final ArrayList hits = new ArrayList();
 +    visitStatistics(new StatisticsVisitor() {
 +        public void visit(Statistics s) {
 +          if (type == s.getType()) {
 +            hits.add(s);
 +          }
 +        }
 +      });
 +    Statistics[] result = new Statistics[hits.size()];
 +    return (Statistics[])hits.toArray(result);
 +  }
 +  
 +  public Statistics[] findStatisticsByTextId(final String textId) {
 +    final ArrayList hits = new ArrayList();
 +    visitStatistics(new StatisticsVisitor() {
 +        public void visit(Statistics s) {
 +          if (s.getTextId().equals(textId)) {
 +            hits.add(s);
 +          }
 +        }
 +      });
 +    Statistics[] result = new Statistics[hits.size()];
 +    return (Statistics[])hits.toArray(result);
 +  }
 +  public Statistics[] findStatisticsByNumericId(final long numericId) {
 +    final ArrayList hits = new ArrayList();
 +    visitStatistics(new StatisticsVisitor() {
 +        public void visit(Statistics s) {
 +          if (numericId == s.getNumericId()) {
 +            hits.add(s);
 +          }
 +        }
 +      });
 +    Statistics[] result = new Statistics[hits.size()];
 +    return (Statistics[])hits.toArray(result);
 +  }
 +  public Statistics findStatisticsByUniqueId(final long uniqueId) {
 +    synchronized (this.statsList) {
 +      for (Statistics s: this.statsList) {
 +        if (uniqueId == s.getUniqueId()) {
 +          return s;
 +        }
 +      }
 +    }
 +    return null;
 +  }
 +
 +  /** for internal use only. Its called by {@link LocalStatisticsImpl#close}. */
 +  public void destroyStatistics(Statistics stats) {
 +    synchronized (statsList) {
 +      if (statsList.remove(stats)) {
 +        statsListModCount++;
 +      }
 +    }
 +  }
 +
 +  public Statistics createAtomicStatistics(StatisticsType type) {
 +    return createAtomicStatistics(type, null, 0);
 +  }
 +  public Statistics createAtomicStatistics(StatisticsType type, String textId) {
 +    return createAtomicStatistics(type, textId, 0);
 +  }
 +  public Statistics createAtomicStatistics(StatisticsType type, String textId, long numericId) {
 +    if (this.statsDisabled) {
 +      return new DummyStatisticsImpl(type, textId, numericId);
 +    }
 +    
 +    long myUniqueId;
 +    synchronized (statsListUniqueIdLock) {
 +      myUniqueId = statsListUniqueId++; // fix for bug 30597
 +    }
 +    Statistics result = StatisticsImpl.createAtomicNoOS(type, textId, numericId, myUniqueId, this);
 +    synchronized (statsList) {
 +      statsList.add(result);
 +      statsListModCount++;
 +    }
 +    return result;
 +  }
 +
 +
 +  // StatisticsTypeFactory methods
 +  private final static StatisticsTypeFactory tf = StatisticsTypeFactoryImpl.singleton();
 +
 +  /**
 +   * Creates or finds a StatisticType for the given shared class.
 +   */
 +  public StatisticsType createType(String name, String description,
 +                                   StatisticDescriptor[] stats) {
 +    return tf.createType(name, description, stats);
 +  }
 +  public StatisticsType findType(String name) {
 +    return tf.findType(name);
 +  }
 +  public StatisticsType[] createTypesFromXml(Reader reader)
 +    throws IOException {
 +    return tf.createTypesFromXml(reader);
 +  }
 +
 +  public StatisticDescriptor createIntCounter(String name, String description,
 +                                              String units) {
 +    return tf.createIntCounter(name, description, units);
 +  }
 +  public StatisticDescriptor createLongCounter(String name, String description,
 +                                               String units) {
 +    return tf.createLongCounter(name, description, units);
 +  }
 +  public StatisticDescriptor createDoubleCounter(String name, String description,
 +                                                 String units) {
 +    return tf.createDoubleCounter(name, description, units);
 +  }
 +  public StatisticDescriptor createIntGauge(String name, String description,
 +                                            String units) {
 +    return tf.createIntGauge(name, description, units);
 +  }
 +  public StatisticDescriptor createLongGauge(String name, String description,
 +                                             String units) {
 +    return tf.createLongGauge(name, description, units);
 +  }
 +  public StatisticDescriptor createDoubleGauge(String name, String description,
 +                                               String units) {
 +    return tf.createDoubleGauge(name, description, units);
 +  }
 +  public StatisticDescriptor createIntCounter(String name, String description,
 +                                              String units, boolean largerBetter) {
 +    return tf.createIntCounter(name, description, units, largerBetter);
 +  }
 +  public StatisticDescriptor createLongCounter(String name, String description,
 +                                               String units, boolean largerBetter) {
 +    return tf.createLongCounter(name, description, units, largerBetter);
 +  }
 +  public StatisticDescriptor createDoubleCounter(String name, String description,
 +                                                 String units, boolean largerBetter) {
 +    return tf.createDoubleCounter(name, description, units, largerBetter);
 +  }
 +  public StatisticDescriptor createIntGauge(String name, String description,
 +                                            String units, boolean largerBetter) {
 +    return tf.createIntGauge(name, description, units, largerBetter);
 +  }
 +  public StatisticDescriptor createLongGauge(String name, String description,
 +                                             String units, boolean largerBetter) {
 +    return tf.createLongGauge(name, description, units, largerBetter);
 +  }
 +  public StatisticDescriptor createDoubleGauge(String name, String description,
 +                                               String units, boolean largerBetter) {
 +    return tf.createDoubleGauge(name, description, units, largerBetter);
 +  }
 +
 +  public long getStartTime() {
 +    return this.startTime;
 +  }
 +
 +  /**
 +   * Makes note of a <code>ConnectListener</code> whose
 +   * <code>onConnect</code> method will be invoked when a connection is
 +   * created to a distributed system.
 +   * @return set of currently existing system connections
 +   */
 +  public static List addConnectListener(ConnectListener listener) {
 +    synchronized (existingSystemsLock) {
 +      synchronized (connectListeners) {
 +        connectListeners.add(listener);
 +        return existingSystems;
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Makes note of a <code>ReconnectListener</code> whose
 +   * <code>onReconnect</code> method will be invoked when a connection is
 +   * recreated to a distributed system during auto-reconnect.<p>
 +   * 
 +   * The ReconnectListener set is cleared after a disconnect.
 +   */
 +  public static void addReconnectListener(ReconnectListener listener) {
 +//    (new ManagerLogWriter(LogWriterImpl.FINE_LEVEL, System.out)).fine("registering reconnect listener: " + listener);
 +    synchronized (existingSystemsLock) {
 +      synchronized (reconnectListeners) {
 +        reconnectListeners.add(listener);
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Removes a <code>ConnectListener</code> from the list of
 +   * listeners that will be notified when a connection is created to
 +   * a distributed system.
 +   * @return true if listener was in the list
 +   */
 +  public static boolean removeConnectListener(ConnectListener listener) {
 +    synchronized (connectListeners) {
 +      return connectListeners.remove(listener);
 +    }
 +  }
 +
 +  /**
 +   * Notifies all registered <code>ConnectListener</code>s that a
 +   * connection to a distributed system has been created.
 +   */
 +  private static void notifyConnectListeners(InternalDistributedSystem sys) {
 +    synchronized (connectListeners) {
 +      for (Iterator iter = connectListeners.iterator(); iter.hasNext();) {
 +        try {
 +          ConnectListener listener = (ConnectListener) iter.next();
 +          listener.onConnect(sys);
 +        }
 +        catch (VirtualMachineError err) {
 +          SystemFailure.initiateFailure(err);
 +          // If this ever returns, rethrow the error.  We're poisoned
 +          // now, so don't let this thread continue.
 +          throw err;
 +        }
 +        catch (Throwable t) {
 +          // Whenever you catch Error or Throwable, you must also
 +          // catch VirtualMachineError (see above).  However, there is
 +          // _still_ a possibility that you are dealing with a cascading
 +          // error condition, so you also need to check to see if the JVM
 +          // is still usable:
 +          SystemFailure.checkFailure();
 +          sys.getLogWriter().convertToLogWriterI18n().severe(LocalizedStrings.InternalDistributedSystem_CONNECTLISTENER_THREW, t);
 +        }
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Removes a <code>ReconnectListener</code> from the list of
 +   * listeners that will be notified when a connection is recreated to
 +   * a distributed system.
 +   */
 +  public static void removeReconnectListener(ReconnectListener listener) {
 +    synchronized (reconnectListeners) {
 +      reconnectListeners.remove(listener);
 +    }
 +  }
 +
 +  /**
 +   * Notifies all registered <code>ReconnectListener</code>s that a
 +   * connection to a distributed system has been recreated.
 +   */
 +  private static void notifyReconnectListeners(InternalDistributedSystem oldsys, InternalDistributedSystem newsys, boolean starting) {
 +    List<ReconnectListener> listeners;
 +    synchronized (reconnectListeners) {
 +      listeners = new ArrayList<ReconnectListener>(reconnectListeners);
 +    }
 +    for (ReconnectListener listener: listeners) {
 +      try {
 +        if (starting) {
 +          listener.reconnecting(oldsys);
 +        } else {
 +          listener.onReconnect(oldsys, newsys);
 +        }
 +      } catch (Throwable t) {
 +        Error err;
 +        if (t instanceof OutOfMemoryError || t instanceof UnknownError) {
 +          err = (Error)t;
 +          SystemFailure.initiateFailure(err);
 +          // If this ever returns, rethrow the error. We're poisoned
 +          // now, so don't let this thread continue.
 +          throw err;
 +        }
 +        // Whenever you catch Error or Throwable, you must also
 +        // check for fatal JVM error (see above).  However, there is
 +        // _still_ a possibility that you are dealing with a cascading
 +        // error condition, so you also need to check to see if the JVM
 +        // is still usable:
 +        SystemFailure.checkFailure();
 +        logger.fatal(LocalizedMessage.create(LocalizedStrings.InternalDistributedSystem_CONNECTLISTENER_THREW), t);
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Notifies all resource event listeners. All exceptions are caught here and
 +   * only a warning message is printed in the log
 +   * 
 +   * @param event
 +   *          Enumeration depicting particular resource event
 +   * @param resource
 +   *          the actual resource object.
 +   */
 +  private void notifyResourceEventListeners(ResourceEvent event, Object resource) {
 +    for (Iterator<ResourceEventsListener> iter = resourceListeners.iterator(); iter
 +        .hasNext();) {
 +      try {
 +        ResourceEventsListener listener = (ResourceEventsListener) iter.next();
 +        listener.handleEvent(event, resource);
 +      } catch(CancelException e) {
 +        //ignore
 +      } catch (ManagementException ex) {
 +        if (event == ResourceEvent.CACHE_CREATE) {
 +          throw ex;
 +        } else {
 +          logger.warn(ex.getMessage(), ex);
 +        }
 +      } catch (Exception err) {
 +        logger.warn(err.getMessage(), err);
 +      } catch (VirtualMachineError e) {
 +        SystemFailure.initiateFailure(e);
 +        throw e;
 +      } catch (Throwable t) {
 +        SystemFailure.checkFailure();
 +        logger.warn(t.getMessage(), t);
 +      }
 +    }
 +  }
 +  
 +  /**
 +   * sqlfire's disconnect listener is invoked before the cache is closed when
 +   * there is a forced disconnect
 +   */
 +  public void setSqlfForcedDisconnectListener(DisconnectListener listener) {
 +    synchronized(this.listeners) { 
 +      this.sqlfDisconnectListener = listener;
 +    }
 +  }
 +  
 +  private void notifySqlfForcedDisconnectListener() {
 +    if (this.sqlfDisconnectListener != null) {
 +      final boolean isDebugEnabled = logger.isDebugEnabled();
 +      try {
 +        if (isDebugEnabled) {
 +          logger.debug("notifying sql disconnect listener");
 +        }
 +        this.sqlfDisconnectListener.onDisconnect(this);
 +      } catch (VirtualMachineError e) {
 +        SystemFailure.initiateFailure(e);
 +        throw e;
 +      } catch (Throwable e) {
 +        SystemFailure.checkFailure();
 +        // TODO: should these be logged or ignored?  We need to see them
 +        logger.info("", e);
 +      }
 +      if (isDebugEnabled) {
 +        logger.debug("finished notifying sql disconnect listener");
 +      }
 +    }
 +  }
 +  
 +  
 +
 +  /**
 +   * Makes note of a <code>DisconnectListener</code> whose
 +   * <code>onDisconnect</code> method will be invoked when this
 +   * connection to the distributed system is disconnected.
 +   */
 +  public void addDisconnectListener(DisconnectListener listener) {
 +    synchronized (this.listeners) {
 +      this.listeners.add(listener);
 +
 +      Boolean disconnectListenerThreadBoolean = 
 +        (Boolean) disconnectListenerThread.get();
 +
 +      if (disconnectListenerThreadBoolean == null ||
 +          !disconnectListenerThreadBoolean.booleanValue()) {
 +        // Don't add disconnect listener after messaging has been disabled.
 +        // Do this test _after_ adding the listener to narrow the window.
 +        // It's possible to miss it still and never invoke the listener, but
 +        // other shutdown conditions will presumably get flagged.
 +        String reason = this.stopper.cancelInProgress();
 +        if (reason != null) {
 +          this.listeners.remove(listener); // don't leave in the list!
 +          throw new DistributedSystemDisconnectedException(LocalizedStrings.InternalDistributedSystem_NO_LISTENERS_PERMITTED_AFTER_SHUTDOWN_0.toLocalizedString(reason), dm.getRootCause());
 +        }
 +      }
 +    } // synchronized
 +  }
 +  
 +  /**
 +   * A non-null value of Boolean.TRUE will identify a thread being used to
 +   * execute disconnectListeners. {@link #addDisconnectListener} will
 +   * not throw ShutdownException if the value is Boolean.TRUE.
 +   */
 +  final ThreadLocal disconnectListenerThread = new ThreadLocal();
 +
 +  /**
 +   * Removes a <code>DisconnectListener</code> from the list of
 +   * listeners that will be notified when this connection to the
 +   * distributed system is disconnected.
 +   * @return true if listener was in the list
 +   */
 +  public boolean removeDisconnectListener(DisconnectListener listener) {
 +    synchronized (this.listeners) {
 +      return this.listeners.remove(listener);
 +    }
 +  }
 +
 +  /**
 +   * Returns any existing <code>InternalDistributedSystem</code> instance.
 +   * Returns <code>null</code> if no instance exists.
 +   */
 +  public static InternalDistributedSystem getAnyInstance() {
 +    List l = existingSystems;
 +    if (l.isEmpty()) {
 +      return null;
 +    }
 +    else {
 +      return (InternalDistributedSystem)l.get(0);
 +    }
 +  }
 +  /**
 +   * Test hook
 +   */
 +  public static List getExistingSystems() {
 +    return existingSystems;
 +  }
 +
 +  @Override
 +  public Properties getProperties() {
 +    return this.config.toProperties();
 +  }
 +  
 +  @Override
 +  public Properties getSecurityProperties() {
 +    return this.config.getSecurityProps();
 +  }
 +
 +  /**
 +   * Fires an "informational" <code>SystemMembershipEvent</code> in
 +   * admin VMs.
 +   *
 +   * @since 4.0
 +   */
 +  public void fireInfoEvent(Object callback) {
 +    throw new UnsupportedOperationException(LocalizedStrings.InternalDistributedSystem_NOT_IMPLEMENTED_YET.toLocalizedString());
 +  }
 +
 +  /**
 +   * Installs a shutdown hook to ensure
 +   * that we are disconnected if an application VM shuts down
 +   * without first calling disconnect itself.
 +     */
 +  public static final Thread shutdownHook;
 +
 +  static {
 +    // Create a shutdown hook to cleanly close connection if
 +    // VM shuts down with an open connection.
 +    ThreadGroup tg = LoggingThreadGroup.createThreadGroup(SHUTDOWN_HOOK_NAME);
 +    Thread tmp_shutdownHook = null;
 +    try {
 +      //Added for bug 38407
 +      if( ! Boolean.getBoolean(DISABLE_SHUTDOWN_HOOK_PROPERTY)) {
 +        tmp_shutdownHook = new Thread(tg, new Runnable() {
 +          public void run() {
 +            DistributedSystem ds = InternalDistributedSystem.getAnyInstance();
 +            setThreadsSocketPolicy(true /* conserve sockets */);
 +            if (ds != null && ds.isConnected()) {
 +              LogWriterI18n log = ((InternalDistributedSystem)ds).getInternalLogWriter();
 +              log.info(LocalizedStrings.InternalDistributedSystem_shutdownHook_shuttingdown);
 +              DurableClientAttributes dca = ((InternalDistributedSystem)ds).getDistributedMember().getDurableClientAttributes();
 +              boolean isDurableClient = false;
 +              
 +              if(dca != null) {
 +                  isDurableClient = ((dca.getId() == null || dca.getId().isEmpty()) ? false : true);
 +              }
 +                            	 
 +              ((InternalDistributedSystem)ds).disconnect(false,
 +                  LocalizedStrings.InternalDistributedSystem_NORMAL_DISCONNECT
 +                      .toLocalizedString(), isDurableClient/*keep alive drive from this*/);
 +              // this was how we wanted to do it for 5.7, but there were shutdown
 +              // issues in PR/dlock (see bug 39287)
 +//              InternalDistributedSystem ids = (InternalDistributedSystem)ds;
 +//              if (ids.getDistributionManager() != null &&
 +//                  ids.getDistributionManager().getMembershipManager() != null) {
 +//                ids.getDistributionManager().getMembershipManager()
 +//                  .uncleanShutdown("VM is exiting", null);
 +//              }
 +            }
 +          }
 +        }, SHUTDOWN_HOOK_NAME);
 +        Runtime.getRuntime().addShutdownHook(tmp_shutdownHook);
 +      }
 +    } finally {
 +      shutdownHook = tmp_shutdownHook;
 +    }
 +  }
 +  ///////////////////////  Inner Classes  ///////////////////////
 +
 +  /**
 +   * A listener that gets invoked before this connection to the
 +   * distributed system is disconnected.
 +   */
 +  public interface DisconnectListener {
 +
 +    /**
 +     * Invoked before a connection to the distributed system is
 +     * disconnected.
 +     *
 +     * @param sys the the system we are disconnecting from
 +     * process should take before returning.
 +     */
 +    public void onDisconnect(InternalDistributedSystem sys);
 +
 +  }
 +  
 +  /**
 +   * A listener that gets invoked before and after a successful auto-reconnect
 +   */
 +  public interface ReconnectListener {
 +    /**
 +     * Invoked when reconnect attempts are initiated
 +     * 
 +     * @param oldSystem the old DS, which is in a partially disconnected state
 +     * and cannot be used for messaging
 +     */
 +    public void reconnecting(InternalDistributedSystem oldSystem);
 +    
 +    /**
 +     * Invoked after a reconnect to the distributed system
 +     * @param oldSystem the old DS
 +     * @param newSystem the new DS
 +     */
 +    public void onReconnect(InternalDistributedSystem oldSystem, InternalDistributedSystem newSystem);
 +  }
 +
 +  /**
 +   * A listener that gets invoked after this connection to the
 +   * distributed system is disconnected
 +   * @author jpenney
 +   *
 +   */
 +  public interface ShutdownListener extends DisconnectListener {
 +    /**
 +     * Invoked after the connection to the distributed system has
 +     * been disconnected
 +     * @param sys
 +     */
 +    public void onShutdown(InternalDistributedSystem sys);
 +  }
 +
 +  /**
 +   * Integer representing number of tries already made
 +   * to reconnect and that failed.
 +   * */
 +  private volatile static int reconnectAttemptCounter = 0;
 +  public static int getReconnectAttemptCounter() {
 +    return reconnectAttemptCounter;
 +  }
 +  
 +  /**
 +   * The time at which reconnect attempts last began
 +   */
 +  private static long reconnectAttemptTime;
 +
 +  /**
 +   * Boolean indicating if DS needs to reconnect and reconnect
 +   * is in progress.
 +   * */
 +  private volatile boolean attemptingToReconnect = false;
 +  
 +  /**
 +   * Boolean indicating this DS joined through a reconnect attempt
 +   */
 +  private volatile boolean reconnected = false;
 +  
 +  /**
 +   * Boolean indicating that this member has been shunned by other members
 +   * or a network partition has occurred
 +   */
 +  private volatile boolean forcedDisconnect = false;
 +
 +  /**
 +   * Used to keep track of the DS created by doing an reconnect on this.
 +   */
 +  private volatile InternalDistributedSystem reconnectDS;
 +  /**
 +   * Was this distributed system started with FORCE_LOCATOR_DM_TYPE=true?
 +   * We need to know when reconnecting.
 +   */
 +  private boolean locatorDMTypeForced;
 +  
 +  
 +  /**
 +   * Returns true if we are reconnecting the distributed system or
 +   * reconnect has completed.  If this returns true it means that
 +   * this instance of the DS is now disconnected and unusable.
 +   */
 +  public boolean isReconnecting(){
 +    return attemptingToReconnect || (reconnectDS != null);
 +  }
 +  
 +  /**
 +   * Returns true if we are reconnecting the distributed system
 +   * and this instance was created for one of the connection
 +   * attempts.  If the connection succeeds this state is cleared
 +   * and this method will commence to return false.
 +   */
 +  public boolean isReconnectingDS() {
 +    return this.isReconnectingDS;
 +  }
 +  
 +  /**
 +   * returns the membership socket of the old
 +   * distributed system, if available, when
 +   * isReconnectingDS returns true.  This is
 +   * used to connect the new DM to the distributed
 +   * system through RemoteTransportConfig.
 +   */
 +  public Object oldDSMembershipInfo() {
 +    if (this.quorumChecker != null) {
 +      return this.quorumChecker.getMembershipInfo();
 +    }
 +    return null;
 +  }
 +  
 +  /**
 +   * Returns true if this DS reconnected to the distributed system after
 +   * a forced disconnect or loss of required-roles
 +   */
 +  public boolean reconnected() {
 +    return this.reconnected;
 +  }
 +  
 +  /**
 +   * Returns true if this DS has been kicked out of the distributed system
 +   */
 +  public boolean forcedDisconnect() {
 +    return this.forcedDisconnect;
 +  }
 +
 +  /**
 +   * If true then this DS will never reconnect.
 +   */
 +  private boolean reconnectCancelled = false;
 +  private Object reconnectCancelledLock = new Object();
 +
 +  /** Make sure this instance of DS never does a reconnect.
 +   * Also if reconnect is in progress cancel it.
 +   */
 +  public void cancelReconnect() {
 +//    (new ManagerLogWriter(LogWriterImpl.FINE_LEVEL, System.out)).fine("cancelReconnect invoked", new Exception("stack trace"));
 +    synchronized(this.reconnectCancelledLock) {
 +      this.reconnectCancelled = t

<TRUNCATED>


[23/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/hll/HyperLogLogPlus.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/hll/HyperLogLogPlus.java
index 0000000,0000000..86b74ef
new file mode 100755
--- /dev/null
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/hll/HyperLogLogPlus.java
@@@ -1,0 -1,0 +1,1053 @@@
++/*
++ * Copyright (C) 2011 Clearspring Technologies, Inc.
++ *
++ * Licensed 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.hll;
++
++import com.gemstone.gemfire.internal.redis.executor.hll.Varint;
++
++import java.io.ByteArrayInputStream;
++import java.io.ByteArrayOutputStream;
++import java.io.DataInput;
++import java.io.DataInputStream;
++import java.io.DataOutput;
++import java.io.DataOutputStream;
++import java.io.Externalizable;
++import java.io.IOException;
++import java.io.ObjectInput;
++import java.io.ObjectOutput;
++import java.io.Serializable;
++import java.util.ArrayList;
++import java.util.Collections;
++import java.util.Comparator;
++import java.util.List;
++import java.util.SortedMap;
++import java.util.TreeMap;
++
++
++/**
++ * Implementation of HyperLogLog++ described in
++ * <p/>
++ * <p>
++ * http://static.googleusercontent.com/external_content/untrusted_dlcp/research.google.com/en/us/pubs/archive/40671.pdf
++ * </p>
++ * <p/>
++ * Brief HyperLogLog++ Overview
++ * <p/>
++ * Uses 64 bit hashing instead of 32
++ * Has two representation modes: sparse and normal
++ * <p/>
++ * 'normal' is approximately the same as regular hyperloglog (still uses 64 bits)
++ * <p/>
++ * 'sparse' handles lower cardinality values with a highly accurate but poorly scaling
++ * strategy and leverages data compression to compete with 'normal' for as long as possible
++ * (sparse has the advantage on accuracy per unit of memory at low cardinality but quickly falls behind).
++ */
++public class HyperLogLogPlus implements ICardinality, Serializable {
++
++  private static final long serialVersionUID = 7504952025744337762L;
++
++  private enum Format {
++    SPARSE, NORMAL
++  }
++
++  /**
++   * used to mark codec version for serialization
++   */
++  private static final int VERSION = 2;
++
++  // threshold and bias data taken from google's bias correction data set:  https://docs.google.com/document/d/1gyjfMHy43U9OWBXxfaeG-3MjGzejW1dlpyMwEYAAWEI/view?fullscreen#
++  static final double[] thresholdData = {10, 20, 40, 80, 220, 400, 900, 1800, 3100, 6500, 15500, 20000, 50000, 120000, 350000};
++
++  static final double[][] rawEstimateData = {
++    // precision 4
++    {11, 11.717, 12.207, 12.7896, 13.2882, 13.8204, 14.3772, 14.9342, 15.5202, 16.161, 16.7722, 17.4636, 18.0396, 18.6766, 19.3566, 20.0454, 20.7936, 21.4856, 22.2666, 22.9946, 23.766, 24.4692, 25.3638, 26.0764, 26.7864, 27.7602, 28.4814, 29.433, 30.2926, 31.0664, 31.9996, 32.7956, 33.5366, 34.5894, 35.5738, 36.2698, 37.3682, 38.0544, 39.2342, 40.0108, 40.7966, 41.9298, 42.8704, 43.6358, 44.5194, 45.773, 46.6772, 47.6174, 48.4888, 49.3304, 50.2506, 51.4996, 52.3824, 53.3078, 54.3984, 55.5838, 56.6618, 57.2174, 58.3514, 59.0802, 60.1482, 61.0376, 62.3598, 62.8078, 63.9744, 64.914, 65.781, 67.1806, 68.0594, 68.8446, 69.7928, 70.8248, 71.8324, 72.8598, 73.6246, 74.7014, 75.393, 76.6708, 77.2394,},
++    // precision 5
++    {23, 23.1194, 23.8208, 24.2318, 24.77, 25.2436, 25.7774, 26.2848, 26.8224, 27.3742, 27.9336, 28.503, 29.0494, 29.6292, 30.2124, 30.798, 31.367, 31.9728, 32.5944, 33.217, 33.8438, 34.3696, 35.0956, 35.7044, 36.324, 37.0668, 37.6698, 38.3644, 39.049, 39.6918, 40.4146, 41.082, 41.687, 42.5398, 43.2462, 43.857, 44.6606, 45.4168, 46.1248, 46.9222, 47.6804, 48.447, 49.3454, 49.9594, 50.7636, 51.5776, 52.331, 53.19, 53.9676, 54.7564, 55.5314, 56.4442, 57.3708, 57.9774, 58.9624, 59.8796, 60.755, 61.472, 62.2076, 63.1024, 63.8908, 64.7338, 65.7728, 66.629, 67.413, 68.3266, 69.1524, 70.2642, 71.1806, 72.0566, 72.9192, 73.7598, 74.3516, 75.5802, 76.4386, 77.4916, 78.1524, 79.1892, 79.8414, 80.8798, 81.8376, 82.4698, 83.7656, 84.331, 85.5914, 86.6012, 87.7016, 88.5582, 89.3394, 90.3544, 91.4912, 92.308, 93.3552, 93.9746, 95.2052, 95.727, 97.1322, 98.3944, 98.7588, 100.242, 101.1914, 102.2538, 102.8776, 103.6292, 105.1932, 105.9152, 107.0868, 107.6728, 108.7144, 110.3114, 110.8716, 111.245,
  112.7908, 113.7064, 114.636, 115.7464, 116.1788, 117.7464, 118.4896, 119.6166, 120.5082, 121.7798, 122.9028, 123.4426, 124.8854, 125.705, 126.4652, 128.3464, 128.3462, 130.0398, 131.0342, 131.0042, 132.4766, 133.511, 134.7252, 135.425, 136.5172, 138.0572, 138.6694, 139.3712, 140.8598, 141.4594, 142.554, 143.4006, 144.7374, 146.1634, 146.8994, 147.605, 147.9304, 149.1636, 150.2468, 151.5876, 152.2096, 153.7032, 154.7146, 155.807, 156.9228, 157.0372, 158.5852,},
++    // precision 6
++    {46, 46.1902, 47.271, 47.8358, 48.8142, 49.2854, 50.317, 51.354, 51.8924, 52.9436, 53.4596, 54.5262, 55.6248, 56.1574, 57.2822, 57.837, 58.9636, 60.074, 60.7042, 61.7976, 62.4772, 63.6564, 64.7942, 65.5004, 66.686, 67.291, 68.5672, 69.8556, 70.4982, 71.8204, 72.4252, 73.7744, 75.0786, 75.8344, 77.0294, 77.8098, 79.0794, 80.5732, 81.1878, 82.5648, 83.2902, 84.6784, 85.3352, 86.8946, 88.3712, 89.0852, 90.499, 91.2686, 92.6844, 94.2234, 94.9732, 96.3356, 97.2286, 98.7262, 100.3284, 101.1048, 102.5962, 103.3562, 105.1272, 106.4184, 107.4974, 109.0822, 109.856, 111.48, 113.2834, 114.0208, 115.637, 116.5174, 118.0576, 119.7476, 120.427, 122.1326, 123.2372, 125.2788, 126.6776, 127.7926, 129.1952, 129.9564, 131.6454, 133.87, 134.5428, 136.2, 137.0294, 138.6278, 139.6782, 141.792, 143.3516, 144.2832, 146.0394, 147.0748, 148.4912, 150.849, 151.696, 153.5404, 154.073, 156.3714, 157.7216, 158.7328, 160.4208, 161.4184, 163.9424, 165.2772, 166.411, 168.1308, 168.769, 170.9258, 172.6828, 173.
 7502, 175.706, 176.3886, 179.0186, 180.4518, 181.927, 183.4172, 184.4114, 186.033, 188.5124, 189.5564, 191.6008, 192.4172, 193.8044, 194.997, 197.4548, 198.8948, 200.2346, 202.3086, 203.1548, 204.8842, 206.6508, 206.6772, 209.7254, 210.4752, 212.7228, 214.6614, 215.1676, 217.793, 218.0006, 219.9052, 221.66, 223.5588, 225.1636, 225.6882, 227.7126, 229.4502, 231.1978, 232.9756, 233.1654, 236.727, 238.1974, 237.7474, 241.1346, 242.3048, 244.1948, 245.3134, 246.879, 249.1204, 249.853, 252.6792, 253.857, 254.4486, 257.2362, 257.9534, 260.0286, 260.5632, 262.663, 264.723, 265.7566, 267.2566, 267.1624, 270.62, 272.8216, 273.2166, 275.2056, 276.2202, 278.3726, 280.3344, 281.9284, 283.9728, 284.1924, 286.4872, 287.587, 289.807, 291.1206, 292.769, 294.8708, 296.665, 297.1182, 299.4012, 300.6352, 302.1354, 304.1756, 306.1606, 307.3462, 308.5214, 309.4134, 310.8352, 313.9684, 315.837, 316.7796, 318.9858,},
++    // precision 7
++    {92, 93.4934, 94.9758, 96.4574, 97.9718, 99.4954, 101.5302, 103.0756, 104.6374, 106.1782, 107.7888, 109.9522, 111.592, 113.2532, 114.9086, 116.5938, 118.9474, 120.6796, 122.4394, 124.2176, 125.9768, 128.4214, 130.2528, 132.0102, 133.8658, 135.7278, 138.3044, 140.1316, 142.093, 144.0032, 145.9092, 148.6306, 150.5294, 152.5756, 154.6508, 156.662, 159.552, 161.3724, 163.617, 165.5754, 167.7872, 169.8444, 172.7988, 174.8606, 177.2118, 179.3566, 181.4476, 184.5882, 186.6816, 189.0824, 191.0258, 193.6048, 196.4436, 198.7274, 200.957, 203.147, 205.4364, 208.7592, 211.3386, 213.781, 215.8028, 218.656, 221.6544, 223.996, 226.4718, 229.1544, 231.6098, 234.5956, 237.0616, 239.5758, 242.4878, 244.5244, 248.2146, 250.724, 252.8722, 255.5198, 258.0414, 261.941, 264.9048, 266.87, 269.4304, 272.028, 274.4708, 278.37, 281.0624, 283.4668, 286.5532, 289.4352, 293.2564, 295.2744, 298.2118, 300.7472, 304.1456, 307.2928, 309.7504, 312.5528, 315.979, 318.2102, 322.1834, 324.3494, 327.325, 330.6614, 3
 32.903, 337.2544, 339.9042, 343.215, 345.2864, 348.0814, 352.6764, 355.301, 357.139, 360.658, 363.1732, 366.5902, 369.9538, 373.0828, 375.922, 378.9902, 382.7328, 386.4538, 388.1136, 391.2234, 394.0878, 396.708, 401.1556, 404.1852, 406.6372, 409.6822, 412.7796, 416.6078, 418.4916, 422.131, 424.5376, 428.1988, 432.211, 434.4502, 438.5282, 440.912, 444.0448, 447.7432, 450.8524, 453.7988, 456.7858, 458.8868, 463.9886, 466.5064, 468.9124, 472.6616, 475.4682, 478.582, 481.304, 485.2738, 488.6894, 490.329, 496.106, 497.6908, 501.1374, 504.5322, 506.8848, 510.3324, 513.4512, 516.179, 520.4412, 522.6066, 526.167, 528.7794, 533.379, 536.067, 538.46, 542.9116, 545.692, 547.9546, 552.493, 555.2722, 557.335, 562.449, 564.2014, 569.0738, 571.0974, 574.8564, 578.2996, 581.409, 583.9704, 585.8098, 589.6528, 594.5998, 595.958, 600.068, 603.3278, 608.2016, 609.9632, 612.864, 615.43, 620.7794, 621.272, 625.8644, 629.206, 633.219, 634.5154, 638.6102,},
++    // precision 8
++    {184.2152, 187.2454, 190.2096, 193.6652, 196.6312, 199.6822, 203.249, 206.3296, 210.0038, 213.2074, 216.4612, 220.27, 223.5178, 227.4412, 230.8032, 234.1634, 238.1688, 241.6074, 245.6946, 249.2664, 252.8228, 257.0432, 260.6824, 264.9464, 268.6268, 272.2626, 276.8376, 280.4034, 284.8956, 288.8522, 292.7638, 297.3552, 301.3556, 305.7526, 309.9292, 313.8954, 318.8198, 322.7668, 327.298, 331.6688, 335.9466, 340.9746, 345.1672, 349.3474, 354.3028, 358.8912, 364.114, 368.4646, 372.9744, 378.4092, 382.6022, 387.843, 392.5684, 397.1652, 402.5426, 407.4152, 412.5388, 417.3592, 422.1366, 427.486, 432.3918, 437.5076, 442.509, 447.3834, 453.3498, 458.0668, 463.7346, 469.1228, 473.4528, 479.7, 484.644, 491.0518, 495.5774, 500.9068, 506.432, 512.1666, 517.434, 522.6644, 527.4894, 533.6312, 538.3804, 544.292, 550.5496, 556.0234, 562.8206, 566.6146, 572.4188, 579.117, 583.6762, 590.6576, 595.7864, 601.509, 607.5334, 612.9204, 619.772, 624.2924, 630.8654, 636.1836, 642.745, 649.1316, 655.0386, 
 660.0136, 666.6342, 671.6196, 678.1866, 684.4282, 689.3324, 695.4794, 702.5038, 708.129, 713.528, 720.3204, 726.463, 732.7928, 739.123, 744.7418, 751.2192, 756.5102, 762.6066, 769.0184, 775.2224, 781.4014, 787.7618, 794.1436, 798.6506, 805.6378, 811.766, 819.7514, 824.5776, 828.7322, 837.8048, 843.6302, 849.9336, 854.4798, 861.3388, 867.9894, 873.8196, 880.3136, 886.2308, 892.4588, 899.0816, 905.4076, 912.0064, 917.3878, 923.619, 929.998, 937.3482, 943.9506, 947.991, 955.1144, 962.203, 968.8222, 975.7324, 981.7826, 988.7666, 994.2648, 1000.3128, 1007.4082, 1013.7536, 1020.3376, 1026.7156, 1031.7478, 1037.4292, 1045.393, 1051.2278, 1058.3434, 1062.8726, 1071.884, 1076.806, 1082.9176, 1089.1678, 1095.5032, 1102.525, 1107.2264, 1115.315, 1120.93, 1127.252, 1134.1496, 1139.0408, 1147.5448, 1153.3296, 1158.1974, 1166.5262, 1174.3328, 1175.657, 1184.4222, 1190.9172, 1197.1292, 1204.4606, 1210.4578, 1218.8728, 1225.3336, 1226.6592, 1236.5768, 1241.363, 1249.4074, 1254.6566, 1260.8014, 1266
 .5454, 1274.5192,},
++    // precision 9
++    {369, 374.8294, 381.2452, 387.6698, 394.1464, 400.2024, 406.8782, 413.6598, 420.462, 427.2826, 433.7102, 440.7416, 447.9366, 455.1046, 462.285, 469.0668, 476.306, 483.8448, 491.301, 498.9886, 506.2422, 513.8138, 521.7074, 529.7428, 537.8402, 545.1664, 553.3534, 561.594, 569.6886, 577.7876, 585.65, 594.228, 602.8036, 611.1666, 620.0818, 628.0824, 637.2574, 646.302, 655.1644, 664.0056, 672.3802, 681.7192, 690.5234, 700.2084, 708.831, 718.485, 728.1112, 737.4764, 746.76, 756.3368, 766.5538, 775.5058, 785.2646, 795.5902, 804.3818, 814.8998, 824.9532, 835.2062, 845.2798, 854.4728, 864.9582, 875.3292, 886.171, 896.781, 906.5716, 916.7048, 927.5322, 937.875, 949.3972, 958.3464, 969.7274, 980.2834, 992.1444, 1003.4264, 1013.0166, 1024.018, 1035.0438, 1046.34, 1057.6856, 1068.9836, 1079.0312, 1091.677, 1102.3188, 1113.4846, 1124.4424, 1135.739, 1147.1488, 1158.9202, 1169.406, 1181.5342, 1193.2834, 1203.8954, 1216.3286, 1226.2146, 1239.6684, 1251.9946, 1262.123, 1275.4338, 1285.7378, 129
 6.076, 1308.9692, 1320.4964, 1333.0998, 1343.9864, 1357.7754, 1368.3208, 1380.4838, 1392.7388, 1406.0758, 1416.9098, 1428.9728, 1440.9228, 1453.9292, 1462.617, 1476.05, 1490.2996, 1500.6128, 1513.7392, 1524.5174, 1536.6322, 1548.2584, 1562.3766, 1572.423, 1587.1232, 1596.5164, 1610.5938, 1622.5972, 1633.1222, 1647.7674, 1658.5044, 1671.57, 1683.7044, 1695.4142, 1708.7102, 1720.6094, 1732.6522, 1747.841, 1756.4072, 1769.9786, 1782.3276, 1797.5216, 1808.3186, 1819.0694, 1834.354, 1844.575, 1856.2808, 1871.1288, 1880.7852, 1893.9622, 1906.3418, 1920.6548, 1932.9302, 1945.8584, 1955.473, 1968.8248, 1980.6446, 1995.9598, 2008.349, 2019.8556, 2033.0334, 2044.0206, 2059.3956, 2069.9174, 2082.6084, 2093.7036, 2106.6108, 2118.9124, 2132.301, 2144.7628, 2159.8422, 2171.0212, 2183.101, 2193.5112, 2208.052, 2221.3194, 2233.3282, 2247.295, 2257.7222, 2273.342, 2286.5638, 2299.6786, 2310.8114, 2322.3312, 2335.516, 2349.874, 2363.5968, 2373.865, 2387.1918, 2401.8328, 2414.8496, 2424.544, 2436.7592
 , 2447.1682, 2464.1958, 2474.3438, 2489.0006, 2497.4526, 2513.6586, 2527.19, 2540.7028, 2553.768,},
++    // precision 10
++    {738.1256, 750.4234, 763.1064, 775.4732, 788.4636, 801.0644, 814.488, 827.9654, 841.0832, 854.7864, 868.1992, 882.2176, 896.5228, 910.1716, 924.7752, 938.899, 953.6126, 968.6492, 982.9474, 998.5214, 1013.1064, 1028.6364, 1044.2468, 1059.4588, 1075.3832, 1091.0584, 1106.8606, 1123.3868, 1139.5062, 1156.1862, 1172.463, 1189.339, 1206.1936, 1223.1292, 1240.1854, 1257.2908, 1275.3324, 1292.8518, 1310.5204, 1328.4854, 1345.9318, 1364.552, 1381.4658, 1400.4256, 1419.849, 1438.152, 1456.8956, 1474.8792, 1494.118, 1513.62, 1532.5132, 1551.9322, 1570.7726, 1590.6086, 1610.5332, 1630.5918, 1650.4294, 1669.7662, 1690.4106, 1710.7338, 1730.9012, 1750.4486, 1770.1556, 1791.6338, 1812.7312, 1833.6264, 1853.9526, 1874.8742, 1896.8326, 1918.1966, 1939.5594, 1961.07, 1983.037, 2003.1804, 2026.071, 2047.4884, 2070.0848, 2091.2944, 2114.333, 2135.9626, 2158.2902, 2181.0814, 2202.0334, 2224.4832, 2246.39, 2269.7202, 2292.1714, 2314.2358, 2338.9346, 2360.891, 2384.0264, 2408.3834, 2430.1544, 2454.8
 684, 2476.9896, 2501.4368, 2522.8702, 2548.0408, 2570.6738, 2593.5208, 2617.0158, 2640.2302, 2664.0962, 2687.4986, 2714.2588, 2735.3914, 2759.6244, 2781.8378, 2808.0072, 2830.6516, 2856.2454, 2877.2136, 2903.4546, 2926.785, 2951.2294, 2976.468, 3000.867, 3023.6508, 3049.91, 3073.5984, 3098.162, 3121.5564, 3146.2328, 3170.9484, 3195.5902, 3221.3346, 3242.7032, 3271.6112, 3296.5546, 3317.7376, 3345.072, 3369.9518, 3394.326, 3418.1818, 3444.6926, 3469.086, 3494.2754, 3517.8698, 3544.248, 3565.3768, 3588.7234, 3616.979, 3643.7504, 3668.6812, 3695.72, 3719.7392, 3742.6224, 3770.4456, 3795.6602, 3819.9058, 3844.002, 3869.517, 3895.6824, 3920.8622, 3947.1364, 3973.985, 3995.4772, 4021.62, 4046.628, 4074.65, 4096.2256, 4121.831, 4146.6406, 4173.276, 4195.0744, 4223.9696, 4251.3708, 4272.9966, 4300.8046, 4326.302, 4353.1248, 4374.312, 4403.0322, 4426.819, 4450.0598, 4478.5206, 4504.8116, 4528.8928, 4553.9584, 4578.8712, 4603.8384, 4632.3872, 4655.5128, 4675.821, 4704.6222, 4731.9862, 4755.41
 74, 4781.2628, 4804.332, 4832.3048, 4862.8752, 4883.4148, 4906.9544, 4935.3516, 4954.3532, 4984.0248, 5011.217, 5035.3258, 5057.3672, 5084.1828,},
++    // precision 11
++    {1477, 1501.6014, 1526.5802, 1551.7942, 1577.3042, 1603.2062, 1629.8402, 1656.2292, 1682.9462, 1709.9926, 1737.3026, 1765.4252, 1793.0578, 1821.6092, 1849.626, 1878.5568, 1908.527, 1937.5154, 1967.1874, 1997.3878, 2027.37, 2058.1972, 2089.5728, 2120.1012, 2151.9668, 2183.292, 2216.0772, 2247.8578, 2280.6562, 2313.041, 2345.714, 2380.3112, 2414.1806, 2447.9854, 2481.656, 2516.346, 2551.5154, 2586.8378, 2621.7448, 2656.6722, 2693.5722, 2729.1462, 2765.4124, 2802.8728, 2838.898, 2876.408, 2913.4926, 2951.4938, 2989.6776, 3026.282, 3065.7704, 3104.1012, 3143.7388, 3181.6876, 3221.1872, 3261.5048, 3300.0214, 3339.806, 3381.409, 3421.4144, 3461.4294, 3502.2286, 3544.651, 3586.6156, 3627.337, 3670.083, 3711.1538, 3753.5094, 3797.01, 3838.6686, 3882.1678, 3922.8116, 3967.9978, 4009.9204, 4054.3286, 4097.5706, 4140.6014, 4185.544, 4229.5976, 4274.583, 4316.9438, 4361.672, 4406.2786, 4451.8628, 4496.1834, 4543.505, 4589.1816, 4632.5188, 4678.2294, 4724.8908, 4769.0194, 4817.052, 4861.458
 8, 4910.1596, 4956.4344, 5002.5238, 5048.13, 5093.6374, 5142.8162, 5187.7894, 5237.3984, 5285.6078, 5331.0858, 5379.1036, 5428.6258, 5474.6018, 5522.7618, 5571.5822, 5618.59, 5667.9992, 5714.88, 5763.454, 5808.6982, 5860.3644, 5910.2914, 5953.571, 6005.9232, 6055.1914, 6104.5882, 6154.5702, 6199.7036, 6251.1764, 6298.7596, 6350.0302, 6398.061, 6448.4694, 6495.933, 6548.0474, 6597.7166, 6646.9416, 6695.9208, 6742.6328, 6793.5276, 6842.1934, 6894.2372, 6945.3864, 6996.9228, 7044.2372, 7094.1374, 7142.2272, 7192.2942, 7238.8338, 7288.9006, 7344.0908, 7394.8544, 7443.5176, 7490.4148, 7542.9314, 7595.6738, 7641.9878, 7694.3688, 7743.0448, 7797.522, 7845.53, 7899.594, 7950.3132, 7996.455, 8050.9442, 8092.9114, 8153.1374, 8197.4472, 8252.8278, 8301.8728, 8348.6776, 8401.4698, 8453.551, 8504.6598, 8553.8944, 8604.1276, 8657.6514, 8710.3062, 8758.908, 8807.8706, 8862.1702, 8910.4668, 8960.77, 9007.2766, 9063.164, 9121.0534, 9164.1354, 9218.1594, 9267.767, 9319.0594, 9372.155, 9419.7126, 9474
 .3722, 9520.1338, 9572.368, 9622.7702, 9675.8448, 9726.5396, 9778.7378, 9827.6554, 9878.1922, 9928.7782, 9978.3984, 10026.578, 10076.5626, 10137.1618, 10177.5244, 10229.9176,},
++    // precision 12
++    {2954, 3003.4782, 3053.3568, 3104.3666, 3155.324, 3206.9598, 3259.648, 3312.539, 3366.1474, 3420.2576, 3474.8376, 3530.6076, 3586.451, 3643.38, 3700.4104, 3757.5638, 3815.9676, 3875.193, 3934.838, 3994.8548, 4055.018, 4117.1742, 4178.4482, 4241.1294, 4304.4776, 4367.4044, 4431.8724, 4496.3732, 4561.4304, 4627.5326, 4693.949, 4761.5532, 4828.7256, 4897.6182, 4965.5186, 5034.4528, 5104.865, 5174.7164, 5244.6828, 5316.6708, 5387.8312, 5459.9036, 5532.476, 5604.8652, 5679.6718, 5753.757, 5830.2072, 5905.2828, 5980.0434, 6056.6264, 6134.3192, 6211.5746, 6290.0816, 6367.1176, 6447.9796, 6526.5576, 6606.1858, 6686.9144, 6766.1142, 6847.0818, 6927.9664, 7010.9096, 7091.0816, 7175.3962, 7260.3454, 7344.018, 7426.4214, 7511.3106, 7596.0686, 7679.8094, 7765.818, 7852.4248, 7936.834, 8022.363, 8109.5066, 8200.4554, 8288.5832, 8373.366, 8463.4808, 8549.7682, 8642.0522, 8728.3288, 8820.9528, 8907.727, 9001.0794, 9091.2522, 9179.988, 9269.852, 9362.6394, 9453.642, 9546.9024, 9640.6616, 9732.6
 622, 9824.3254, 9917.7484, 10007.9392, 10106.7508, 10196.2152, 10289.8114, 10383.5494, 10482.3064, 10576.8734, 10668.7872, 10764.7156, 10862.0196, 10952.793, 11049.9748, 11146.0702, 11241.4492, 11339.2772, 11434.2336, 11530.741, 11627.6136, 11726.311, 11821.5964, 11918.837, 12015.3724, 12113.0162, 12213.0424, 12306.9804, 12408.4518, 12504.8968, 12604.586, 12700.9332, 12798.705, 12898.5142, 12997.0488, 13094.788, 13198.475, 13292.7764, 13392.9698, 13486.8574, 13590.1616, 13686.5838, 13783.6264, 13887.2638, 13992.0978, 14081.0844, 14189.9956, 14280.0912, 14382.4956, 14486.4384, 14588.1082, 14686.2392, 14782.276, 14888.0284, 14985.1864, 15088.8596, 15187.0998, 15285.027, 15383.6694, 15495.8266, 15591.3736, 15694.2008, 15790.3246, 15898.4116, 15997.4522, 16095.5014, 16198.8514, 16291.7492, 16402.6424, 16499.1266, 16606.2436, 16697.7186, 16796.3946, 16902.3376, 17005.7672, 17100.814, 17206.8282, 17305.8262, 17416.0744, 17508.4092, 17617.0178, 17715.4554, 17816.758, 17920.1748, 18012.9236
 , 18119.7984, 18223.2248, 18324.2482, 18426.6276, 18525.0932, 18629.8976, 18733.2588, 18831.0466, 18940.1366, 19032.2696, 19131.729, 19243.4864, 19349.6932, 19442.866, 19547.9448, 19653.2798, 19754.4034, 19854.0692, 19965.1224, 20065.1774, 20158.2212, 20253.353, 20366.3264, 20463.22,},
++    // precision 13
++    {5908.5052, 6007.2672, 6107.347, 6208.5794, 6311.2622, 6414.5514, 6519.3376, 6625.6952, 6732.5988, 6841.3552, 6950.5972, 7061.3082, 7173.5646, 7287.109, 7401.8216, 7516.4344, 7633.3802, 7751.2962, 7870.3784, 7990.292, 8110.79, 8233.4574, 8356.6036, 8482.2712, 8607.7708, 8735.099, 8863.1858, 8993.4746, 9123.8496, 9255.6794, 9388.5448, 9522.7516, 9657.3106, 9792.6094, 9930.5642, 10068.794, 10206.7256, 10347.81, 10490.3196, 10632.0778, 10775.9916, 10920.4662, 11066.124, 11213.073, 11358.0362, 11508.1006, 11659.1716, 11808.7514, 11959.4884, 12112.1314, 12265.037, 12420.3756, 12578.933, 12734.311, 12890.0006, 13047.2144, 13207.3096, 13368.5144, 13528.024, 13689.847, 13852.7528, 14018.3168, 14180.5372, 14346.9668, 14513.5074, 14677.867, 14846.2186, 15017.4186, 15184.9716, 15356.339, 15529.2972, 15697.3578, 15871.8686, 16042.187, 16216.4094, 16389.4188, 16565.9126, 16742.3272, 16919.0042, 17094.7592, 17273.965, 17451.8342, 17634.4254, 17810.5984, 17988.9242, 18171.051, 18354.7938, 185
 39.466, 18721.0408, 18904.9972, 19081.867, 19271.9118, 19451.8694, 19637.9816, 19821.2922, 20013.1292, 20199.3858, 20387.8726, 20572.9514, 20770.7764, 20955.1714, 21144.751, 21329.9952, 21520.709, 21712.7016, 21906.3868, 22096.2626, 22286.0524, 22475.051, 22665.5098, 22862.8492, 23055.5294, 23249.6138, 23437.848, 23636.273, 23826.093, 24020.3296, 24213.3896, 24411.7392, 24602.9614, 24805.7952, 24998.1552, 25193.9588, 25389.0166, 25585.8392, 25780.6976, 25981.2728, 26175.977, 26376.5252, 26570.1964, 26773.387, 26962.9812, 27163.0586, 27368.164, 27565.0534, 27758.7428, 27961.1276, 28163.2324, 28362.3816, 28565.7668, 28758.644, 28956.9768, 29163.4722, 29354.7026, 29561.1186, 29767.9948, 29959.9986, 30164.0492, 30366.9818, 30562.5338, 30762.9928, 30976.1592, 31166.274, 31376.722, 31570.3734, 31770.809, 31974.8934, 32179.5286, 32387.5442, 32582.3504, 32794.076, 32989.9528, 33191.842, 33392.4684, 33595.659, 33801.8672, 34000.3414, 34200.0922, 34402.6792, 34610.0638, 34804.0084, 35011.13, 
 35218.669, 35418.6634, 35619.0792, 35830.6534, 36028.4966, 36229.7902, 36438.6422, 36630.7764, 36833.3102, 37048.6728, 37247.3916, 37453.5904, 37669.3614, 37854.5526, 38059.305, 38268.0936, 38470.2516, 38674.7064, 38876.167, 39068.3794, 39281.9144, 39492.8566, 39684.8628, 39898.4108, 40093.1836, 40297.6858, 40489.7086, 40717.2424,},
++    // precision 14
++    {11817.475, 12015.0046, 12215.3792, 12417.7504, 12623.1814, 12830.0086, 13040.0072, 13252.503, 13466.178, 13683.2738, 13902.0344, 14123.9798, 14347.394, 14573.7784, 14802.6894, 15033.6824, 15266.9134, 15502.8624, 15741.4944, 15980.7956, 16223.8916, 16468.6316, 16715.733, 16965.5726, 17217.204, 17470.666, 17727.8516, 17986.7886, 18247.6902, 18510.9632, 18775.304, 19044.7486, 19314.4408, 19587.202, 19862.2576, 20135.924, 20417.0324, 20697.9788, 20979.6112, 21265.0274, 21550.723, 21841.6906, 22132.162, 22428.1406, 22722.127, 23020.5606, 23319.7394, 23620.4014, 23925.2728, 24226.9224, 24535.581, 24845.505, 25155.9618, 25470.3828, 25785.9702, 26103.7764, 26420.4132, 26742.0186, 27062.8852, 27388.415, 27714.6024, 28042.296, 28365.4494, 28701.1526, 29031.8008, 29364.2156, 29704.497, 30037.1458, 30380.111, 30723.8168, 31059.5114, 31404.9498, 31751.6752, 32095.2686, 32444.7792, 32794.767, 33145.204, 33498.4226, 33847.6502, 34209.006, 34560.849, 34919.4838, 35274.9778, 35635.1322, 35996.
 3266, 36359.1394, 36722.8266, 37082.8516, 37447.7354, 37815.9606, 38191.0692, 38559.4106, 38924.8112, 39294.6726, 39663.973, 40042.261, 40416.2036, 40779.2036, 41161.6436, 41540.9014, 41921.1998, 42294.7698, 42678.5264, 43061.3464, 43432.375, 43818.432, 44198.6598, 44583.0138, 44970.4794, 45353.924, 45729.858, 46118.2224, 46511.5724, 46900.7386, 47280.6964, 47668.1472, 48055.6796, 48446.9436, 48838.7146, 49217.7296, 49613.7796, 50010.7508, 50410.0208, 50793.7886, 51190.2456, 51583.1882, 51971.0796, 52376.5338, 52763.319, 53165.5534, 53556.5594, 53948.2702, 54346.352, 54748.7914, 55138.577, 55543.4824, 55941.1748, 56333.7746, 56745.1552, 57142.7944, 57545.2236, 57935.9956, 58348.5268, 58737.5474, 59158.5962, 59542.6896, 59958.8004, 60349.3788, 60755.0212, 61147.6144, 61548.194, 61946.0696, 62348.6042, 62763.603, 63162.781, 63560.635, 63974.3482, 64366.4908, 64771.5876, 65176.7346, 65597.3916, 65995.915, 66394.0384, 66822.9396, 67203.6336, 67612.2032, 68019.0078, 68420.0388, 68821.22,
  69235.8388, 69640.0724, 70055.155, 70466.357, 70863.4266, 71276.2482, 71677.0306, 72080.2006, 72493.0214, 72893.5952, 73314.5856, 73714.9852, 74125.3022, 74521.2122, 74933.6814, 75341.5904, 75743.0244, 76166.0278, 76572.1322, 76973.1028, 77381.6284, 77800.6092, 78189.328, 78607.0962, 79012.2508, 79407.8358, 79825.725, 80238.701, 80646.891, 81035.6436, 81460.0448, 81876.3884,},
++    // precision 15
++    {23635.0036, 24030.8034, 24431.4744, 24837.1524, 25246.7928, 25661.326, 26081.3532, 26505.2806, 26933.9892, 27367.7098, 27805.318, 28248.799, 28696.4382, 29148.8244, 29605.5138, 30066.8668, 30534.2344, 31006.32, 31480.778, 31962.2418, 32447.3324, 32938.0232, 33432.731, 33930.728, 34433.9896, 34944.1402, 35457.5588, 35974.5958, 36497.3296, 37021.9096, 37554.326, 38088.0826, 38628.8816, 39171.3192, 39723.2326, 40274.5554, 40832.3142, 41390.613, 41959.5908, 42532.5466, 43102.0344, 43683.5072, 44266.694, 44851.2822, 45440.7862, 46038.0586, 46640.3164, 47241.064, 47846.155, 48454.7396, 49076.9168, 49692.542, 50317.4778, 50939.65, 51572.5596, 52210.2906, 52843.7396, 53481.3996, 54127.236, 54770.406, 55422.6598, 56078.7958, 56736.7174, 57397.6784, 58064.5784, 58730.308, 59404.9784, 60077.0864, 60751.9158, 61444.1386, 62115.817, 62808.7742, 63501.4774, 64187.5454, 64883.6622, 65582.7468, 66274.5318, 66976.9276, 67688.7764, 68402.138, 69109.6274, 69822.9706, 70543.6108, 71265.5202, 7198
 3.3848, 72708.4656, 73433.384, 74158.4664, 74896.4868, 75620.9564, 76362.1434, 77098.3204, 77835.7662, 78582.6114, 79323.9902, 80067.8658, 80814.9246, 81567.0136, 82310.8536, 83061.9952, 83821.4096, 84580.8608, 85335.547, 86092.5802, 86851.6506, 87612.311, 88381.2016, 89146.3296, 89907.8974, 90676.846, 91451.4152, 92224.5518, 92995.8686, 93763.5066, 94551.2796, 95315.1944, 96096.1806, 96881.0918, 97665.679, 98442.68, 99229.3002, 100011.0994, 100790.6386, 101580.1564, 102377.7484, 103152.1392, 103944.2712, 104730.216, 105528.6336, 106324.9398, 107117.6706, 107890.3988, 108695.2266, 109485.238, 110294.7876, 111075.0958, 111878.0496, 112695.2864, 113464.5486, 114270.0474, 115068.608, 115884.3626, 116673.2588, 117483.3716, 118275.097, 119085.4092, 119879.2808, 120687.5868, 121499.9944, 122284.916, 123095.9254, 123912.5038, 124709.0454, 125503.7182, 126323.259, 127138.9412, 127943.8294, 128755.646, 129556.5354, 130375.3298, 131161.4734, 131971.1962, 132787.5458, 133588.1056, 134431.351, 
 135220.2906, 136023.398, 136846.6558, 137667.0004, 138463.663, 139283.7154, 140074.6146, 140901.3072, 141721.8548, 142543.2322, 143356.1096, 144173.7412, 144973.0948, 145794.3162, 146609.5714, 147420.003, 148237.9784, 149050.5696, 149854.761, 150663.1966, 151494.0754, 152313.1416, 153112.6902, 153935.7206, 154746.9262, 155559.547, 156401.9746, 157228.7036, 158008.7254, 158820.75, 159646.9184, 160470.4458, 161279.5348, 162093.3114, 162918.542, 163729.2842,},
++    // precision 16
++    {47271, 48062.3584, 48862.7074, 49673.152, 50492.8416, 51322.9514, 52161.03, 53009.407, 53867.6348, 54734.206, 55610.5144, 56496.2096, 57390.795, 58297.268, 59210.6448, 60134.665, 61068.0248, 62010.4472, 62962.5204, 63923.5742, 64895.0194, 65876.4182, 66862.6136, 67862.6968, 68868.8908, 69882.8544, 70911.271, 71944.0924, 72990.0326, 74040.692, 75100.6336, 76174.7826, 77252.5998, 78340.2974, 79438.2572, 80545.4976, 81657.2796, 82784.6336, 83915.515, 85059.7362, 86205.9368, 87364.4424, 88530.3358, 89707.3744, 90885.9638, 92080.197, 93275.5738, 94479.391, 95695.918, 96919.2236, 98148.4602, 99382.3474, 100625.6974, 101878.0284, 103141.6278, 104409.4588, 105686.2882, 106967.5402, 108261.6032, 109548.1578, 110852.0728, 112162.231, 113479.0072, 114806.2626, 116137.9072, 117469.5048, 118813.5186, 120165.4876, 121516.2556, 122875.766, 124250.5444, 125621.2222, 127003.2352, 128387.848, 129775.2644, 131181.7776, 132577.3086, 133979.9458, 135394.1132, 136800.9078, 138233.217, 139668.5308, 
 141085.212, 142535.2122, 143969.0684, 145420.2872, 146878.1542, 148332.7572, 149800.3202, 151269.66, 152743.6104, 154213.0948, 155690.288, 157169.4246, 158672.1756, 160160.059, 161650.6854, 163145.7772, 164645.6726, 166159.1952, 167682.1578, 169177.3328, 170700.0118, 172228.8964, 173732.6664, 175265.5556, 176787.799, 178317.111, 179856.6914, 181400.865, 182943.4612, 184486.742, 186033.4698, 187583.7886, 189148.1868, 190688.4526, 192250.1926, 193810.9042, 195354.2972, 196938.7682, 198493.5898, 200079.2824, 201618.912, 203205.5492, 204765.5798, 206356.1124, 207929.3064, 209498.7196, 211086.229, 212675.1324, 214256.7892, 215826.2392, 217412.8474, 218995.6724, 220618.6038, 222207.1166, 223781.0364, 225387.4332, 227005.7928, 228590.4336, 230217.8738, 231805.1054, 233408.9, 234995.3432, 236601.4956, 238190.7904, 239817.2548, 241411.2832, 243002.4066, 244640.1884, 246255.3128, 247849.3508, 249479.9734, 251106.8822, 252705.027, 254332.9242, 255935.129, 257526.9014, 259154.772, 260777.625, 2
 62390.253, 264004.4906, 265643.59, 267255.4076, 268873.426, 270470.7252, 272106.4804, 273722.4456, 275337.794, 276945.7038, 278592.9154, 280204.3726, 281841.1606, 283489.171, 285130.1716, 286735.3362, 288364.7164, 289961.1814, 291595.5524, 293285.683, 294899.6668, 296499.3434, 298128.0462, 299761.8946, 301394.2424, 302997.6748, 304615.1478, 306269.7724, 307886.114, 309543.1028, 311153.2862, 312782.8546, 314421.2008, 316033.2438, 317692.9636, 319305.2648, 320948.7406, 322566.3364, 324228.4224, 325847.1542,},
++    // precision 17
++    {94542, 96125.811, 97728.019, 99348.558, 100987.9705, 102646.7565, 104324.5125, 106021.7435, 107736.7865, 109469.272, 111223.9465, 112995.219, 114787.432, 116593.152, 118422.71, 120267.2345, 122134.6765, 124020.937, 125927.2705, 127851.255, 129788.9485, 131751.016, 133726.8225, 135722.592, 137736.789, 139770.568, 141821.518, 143891.343, 145982.1415, 148095.387, 150207.526, 152355.649, 154515.6415, 156696.05, 158887.7575, 161098.159, 163329.852, 165569.053, 167837.4005, 170121.6165, 172420.4595, 174732.6265, 177062.77, 179412.502, 181774.035, 184151.939, 186551.6895, 188965.691, 191402.8095, 193857.949, 196305.0775, 198774.6715, 201271.2585, 203764.78, 206299.3695, 208818.1365, 211373.115, 213946.7465, 216532.076, 219105.541, 221714.5375, 224337.5135, 226977.5125, 229613.0655, 232270.2685, 234952.2065, 237645.3555, 240331.1925, 243034.517, 245756.0725, 248517.6865, 251232.737, 254011.3955, 256785.995, 259556.44, 262368.335, 265156.911, 267965.266, 270785.583, 273616.0495, 276487
 .4835, 279346.639, 282202.509, 285074.3885, 287942.2855, 290856.018, 293774.0345, 296678.5145, 299603.6355, 302552.6575, 305492.9785, 308466.8605, 311392.581, 314347.538, 317319.4295, 320285.9785, 323301.7325, 326298.3235, 329301.3105, 332301.987, 335309.791, 338370.762, 341382.923, 344431.1265, 347464.1545, 350507.28, 353619.2345, 356631.2005, 359685.203, 362776.7845, 365886.488, 368958.2255, 372060.6825, 375165.4335, 378237.935, 381328.311, 384430.5225, 387576.425, 390683.242, 393839.648, 396977.8425, 400101.9805, 403271.296, 406409.8425, 409529.5485, 412678.7, 415847.423, 419020.8035, 422157.081, 425337.749, 428479.6165, 431700.902, 434893.1915, 438049.582, 441210.5415, 444379.2545, 447577.356, 450741.931, 453959.548, 457137.0935, 460329.846, 463537.4815, 466732.3345, 469960.5615, 473164.681, 476347.6345, 479496.173, 482813.1645, 486025.6995, 489249.4885, 492460.1945, 495675.8805, 498908.0075, 502131.802, 505374.3855, 508550.9915, 511806.7305, 515026.776, 518217.0005, 521523.9855
 , 524705.9855, 527950.997, 531210.0265, 534472.497, 537750.7315, 540926.922, 544207.094, 547429.4345, 550666.3745, 553975.3475, 557150.7185, 560399.6165, 563662.697, 566916.7395, 570146.1215, 573447.425, 576689.6245, 579874.5745, 583202.337, 586503.0255, 589715.635, 592910.161, 596214.3885, 599488.035, 602740.92, 605983.0685, 609248.67, 612491.3605, 615787.912, 619107.5245, 622307.9555, 625577.333, 628840.4385, 632085.2155, 635317.6135, 638691.7195, 641887.467, 645139.9405, 648441.546, 651666.252, 654941.845,},
++    // precision 18
++    {189084, 192250.913, 195456.774, 198696.946, 201977.762, 205294.444, 208651.754, 212042.099, 215472.269, 218941.91, 222443.912, 225996.845, 229568.199, 233193.568, 236844.457, 240543.233, 244279.475, 248044.27, 251854.588, 255693.2, 259583.619, 263494.621, 267445.385, 271454.061, 275468.769, 279549.456, 283646.446, 287788.198, 291966.099, 296181.164, 300431.469, 304718.618, 309024.004, 313393.508, 317760.803, 322209.731, 326675.061, 331160.627, 335654.47, 340241.442, 344841.833, 349467.132, 354130.629, 358819.432, 363574.626, 368296.587, 373118.482, 377914.93, 382782.301, 387680.669, 392601.981, 397544.323, 402529.115, 407546.018, 412593.658, 417638.657, 422762.865, 427886.169, 433017.167, 438213.273, 443441.254, 448692.421, 453937.533, 459239.049, 464529.569, 469910.083, 475274.03, 480684.473, 486070.26, 491515.237, 496995.651, 502476.617, 507973.609, 513497.19, 519083.233, 524726.509, 530305.505, 535945.728, 541584.404, 547274.055, 552967.236, 558667.862, 564360.216, 570128.1
 48, 575965.08, 581701.952, 587532.523, 593361.144, 599246.128, 605033.418, 610958.779, 616837.117, 622772.818, 628672.04, 634675.369, 640574.831, 646585.739, 652574.547, 658611.217, 664642.684, 670713.914, 676737.681, 682797.313, 688837.897, 694917.874, 701009.882, 707173.648, 713257.254, 719415.392, 725636.761, 731710.697, 737906.209, 744103.074, 750313.39, 756504.185, 762712.579, 768876.985, 775167.859, 781359, 787615.959, 793863.597, 800245.477, 806464.582, 812785.294, 819005.925, 825403.057, 831676.197, 837936.284, 844266.968, 850642.711, 856959.756, 863322.774, 869699.931, 876102.478, 882355.787, 888694.463, 895159.952, 901536.143, 907872.631, 914293.672, 920615.14, 927130.974, 933409.404, 939922.178, 946331.47, 952745.93, 959209.264, 965590.224, 972077.284, 978501.961, 984953.19, 991413.271, 997817.479, 1004222.658, 1010725.676, 1017177.138, 1023612.529, 1030098.236, 1036493.719, 1043112.207, 1049537.036, 1056008.096, 1062476.184, 1068942.337, 1075524.95, 1081932.864, 1088426.
 025, 1094776.005, 1101327.448, 1107901.673, 1114423.639, 1120884.602, 1127324.923, 1133794.24, 1140328.886, 1146849.376, 1153346.682, 1159836.502, 1166478.703, 1172953.304, 1179391.502, 1185950.982, 1192544.052, 1198913.41, 1205430.994, 1212015.525, 1218674.042, 1225121.683, 1231551.101, 1238126.379, 1244673.795, 1251260.649, 1257697.86, 1264320.983, 1270736.319, 1277274.694, 1283804.95, 1290211.514, 1296858.568, 1303455.691,}
++  };
++
++  static final double[][] biasData = {
++    // precision 4
++    {10, 9.717, 9.207, 8.7896, 8.2882, 7.8204, 7.3772, 6.9342, 6.5202, 6.161, 5.7722, 5.4636, 5.0396, 4.6766, 4.3566, 4.0454, 3.7936, 3.4856, 3.2666, 2.9946, 2.766, 2.4692, 2.3638, 2.0764, 1.7864, 1.7602, 1.4814, 1.433, 1.2926, 1.0664, 0.999600000000001, 0.7956, 0.5366, 0.589399999999998, 0.573799999999999, 0.269799999999996, 0.368200000000002, 0.0544000000000011, 0.234200000000001, 0.0108000000000033, -0.203400000000002, -0.0701999999999998, -0.129600000000003, -0.364199999999997, -0.480600000000003, -0.226999999999997, -0.322800000000001, -0.382599999999996, -0.511200000000002, -0.669600000000003, -0.749400000000001, -0.500399999999999, -0.617600000000003, -0.6922, -0.601599999999998, -0.416200000000003, -0.338200000000001, -0.782600000000002, -0.648600000000002, -0.919800000000002, -0.851799999999997, -0.962400000000002, -0.6402, -1.1922, -1.0256, -1.086, -1.21899999999999, -0.819400000000002, -0.940600000000003, -1.1554, -1.2072, -1.1752, -1.16759999999999, -1.14019999999999, -
 1.3754, -1.29859999999999, -1.607, -1.3292, -1.7606,},
++    // precision 5
++    {22, 21.1194, 20.8208, 20.2318, 19.77, 19.2436, 18.7774, 18.2848, 17.8224, 17.3742, 16.9336, 16.503, 16.0494, 15.6292, 15.2124, 14.798, 14.367, 13.9728, 13.5944, 13.217, 12.8438, 12.3696, 12.0956, 11.7044, 11.324, 11.0668, 10.6698, 10.3644, 10.049, 9.6918, 9.4146, 9.082, 8.687, 8.5398, 8.2462, 7.857, 7.6606, 7.4168, 7.1248, 6.9222, 6.6804, 6.447, 6.3454, 5.9594, 5.7636, 5.5776, 5.331, 5.19, 4.9676, 4.7564, 4.5314, 4.4442, 4.3708, 3.9774, 3.9624, 3.8796, 3.755, 3.472, 3.2076, 3.1024, 2.8908, 2.7338, 2.7728, 2.629, 2.413, 2.3266, 2.1524, 2.2642, 2.1806, 2.0566, 1.9192, 1.7598, 1.3516, 1.5802, 1.43859999999999, 1.49160000000001, 1.1524, 1.1892, 0.841399999999993, 0.879800000000003, 0.837599999999995, 0.469800000000006, 0.765600000000006, 0.331000000000003, 0.591399999999993, 0.601200000000006, 0.701599999999999, 0.558199999999999, 0.339399999999998, 0.354399999999998, 0.491200000000006, 0.308000000000007, 0.355199999999996, -0.0254000000000048, 0.205200000000005, -0.27299999999999
 6, 0.132199999999997, 0.394400000000005, -0.241200000000006, 0.242000000000004, 0.191400000000002, 0.253799999999998, -0.122399999999999, -0.370800000000003, 0.193200000000004, -0.0848000000000013, 0.0867999999999967, -0.327200000000005, -0.285600000000002, 0.311400000000006, -0.128399999999999, -0.754999999999995, -0.209199999999996, -0.293599999999998, -0.364000000000004, -0.253600000000006, -0.821200000000005, -0.253600000000006, -0.510400000000004, -0.383399999999995, -0.491799999999998, -0.220200000000006, -0.0972000000000008, -0.557400000000001, -0.114599999999996, -0.295000000000002, -0.534800000000004, 0.346399999999988, -0.65379999999999, 0.0398000000000138, 0.0341999999999985, -0.995800000000003, -0.523400000000009, -0.489000000000004, -0.274799999999999, -0.574999999999989, -0.482799999999997, 0.0571999999999946, -0.330600000000004, -0.628800000000012, -0.140199999999993, -0.540600000000012, -0.445999999999998, -0.599400000000003, -0.262599999999992, 0.163399999999996, -0
 .100599999999986, -0.39500000000001, -1.06960000000001, -0.836399999999998, -0.753199999999993, -0.412399999999991, -0.790400000000005, -0.29679999999999, -0.28540000000001, -0.193000000000012, -0.0772000000000048, -0.962799999999987, -0.414800000000014,},
++    // precision 6
++    {45, 44.1902, 43.271, 42.8358, 41.8142, 41.2854, 40.317, 39.354, 38.8924, 37.9436, 37.4596, 36.5262, 35.6248, 35.1574, 34.2822, 33.837, 32.9636, 32.074, 31.7042, 30.7976, 30.4772, 29.6564, 28.7942, 28.5004, 27.686, 27.291, 26.5672, 25.8556, 25.4982, 24.8204, 24.4252, 23.7744, 23.0786, 22.8344, 22.0294, 21.8098, 21.0794, 20.5732, 20.1878, 19.5648, 19.2902, 18.6784, 18.3352, 17.8946, 17.3712, 17.0852, 16.499, 16.2686, 15.6844, 15.2234, 14.9732, 14.3356, 14.2286, 13.7262, 13.3284, 13.1048, 12.5962, 12.3562, 12.1272, 11.4184, 11.4974, 11.0822, 10.856, 10.48, 10.2834, 10.0208, 9.637, 9.51739999999999, 9.05759999999999, 8.74760000000001, 8.42700000000001, 8.1326, 8.2372, 8.2788, 7.6776, 7.79259999999999, 7.1952, 6.9564, 6.6454, 6.87, 6.5428, 6.19999999999999, 6.02940000000001, 5.62780000000001, 5.6782, 5.792, 5.35159999999999, 5.28319999999999, 5.0394, 5.07480000000001, 4.49119999999999, 4.84899999999999, 4.696, 4.54040000000001, 4.07300000000001, 4.37139999999999, 3.7216, 3.7328, 3.
 42080000000001, 3.41839999999999, 3.94239999999999, 3.27719999999999, 3.411, 3.13079999999999, 2.76900000000001, 2.92580000000001, 2.68279999999999, 2.75020000000001, 2.70599999999999, 2.3886, 3.01859999999999, 2.45179999999999, 2.92699999999999, 2.41720000000001, 2.41139999999999, 2.03299999999999, 2.51240000000001, 2.5564, 2.60079999999999, 2.41720000000001, 1.80439999999999, 1.99700000000001, 2.45480000000001, 1.8948, 2.2346, 2.30860000000001, 2.15479999999999, 1.88419999999999, 1.6508, 0.677199999999999, 1.72540000000001, 1.4752, 1.72280000000001, 1.66139999999999, 1.16759999999999, 1.79300000000001, 1.00059999999999, 0.905200000000008, 0.659999999999997, 1.55879999999999, 1.1636, 0.688199999999995, 0.712600000000009, 0.450199999999995, 1.1978, 0.975599999999986, 0.165400000000005, 1.727, 1.19739999999999, -0.252600000000001, 1.13460000000001, 1.3048, 1.19479999999999, 0.313400000000001, 0.878999999999991, 1.12039999999999, 0.853000000000009, 1.67920000000001, 0.856999999999999,
  0.448599999999999, 1.2362, 0.953399999999988, 1.02859999999998, 0.563199999999995, 0.663000000000011, 0.723000000000013, 0.756599999999992, 0.256599999999992, -0.837600000000009, 0.620000000000005, 0.821599999999989, 0.216600000000028, 0.205600000000004, 0.220199999999977, 0.372599999999977, 0.334400000000016, 0.928400000000011, 0.972800000000007, 0.192400000000021, 0.487199999999973, -0.413000000000011, 0.807000000000016, 0.120600000000024, 0.769000000000005, 0.870799999999974, 0.66500000000002, 0.118200000000002, 0.401200000000017, 0.635199999999998, 0.135400000000004, 0.175599999999974, 1.16059999999999, 0.34620000000001, 0.521400000000028, -0.586599999999976, -1.16480000000001, 0.968399999999974, 0.836999999999989, 0.779600000000016, 0.985799999999983,},
++    // precision 7
++    {91, 89.4934, 87.9758, 86.4574, 84.9718, 83.4954, 81.5302, 80.0756, 78.6374, 77.1782, 75.7888, 73.9522, 72.592, 71.2532, 69.9086, 68.5938, 66.9474, 65.6796, 64.4394, 63.2176, 61.9768, 60.4214, 59.2528, 58.0102, 56.8658, 55.7278, 54.3044, 53.1316, 52.093, 51.0032, 49.9092, 48.6306, 47.5294, 46.5756, 45.6508, 44.662, 43.552, 42.3724, 41.617, 40.5754, 39.7872, 38.8444, 37.7988, 36.8606, 36.2118, 35.3566, 34.4476, 33.5882, 32.6816, 32.0824, 31.0258, 30.6048, 29.4436, 28.7274, 27.957, 27.147, 26.4364, 25.7592, 25.3386, 24.781, 23.8028, 23.656, 22.6544, 21.996, 21.4718, 21.1544, 20.6098, 19.5956, 19.0616, 18.5758, 18.4878, 17.5244, 17.2146, 16.724, 15.8722, 15.5198, 15.0414, 14.941, 14.9048, 13.87, 13.4304, 13.028, 12.4708, 12.37, 12.0624, 11.4668, 11.5532, 11.4352, 11.2564, 10.2744, 10.2118, 9.74720000000002, 10.1456, 9.2928, 8.75040000000001, 8.55279999999999, 8.97899999999998, 8.21019999999999, 8.18340000000001, 7.3494, 7.32499999999999, 7.66140000000001, 6.90300000000002, 7.25439
 999999998, 6.9042, 7.21499999999997, 6.28640000000001, 6.08139999999997, 6.6764, 6.30099999999999, 5.13900000000001, 5.65800000000002, 5.17320000000001, 4.59019999999998, 4.9538, 5.08280000000002, 4.92200000000003, 4.99020000000002, 4.7328, 5.4538, 4.11360000000002, 4.22340000000003, 4.08780000000002, 3.70800000000003, 4.15559999999999, 4.18520000000001, 3.63720000000001, 3.68220000000002, 3.77960000000002, 3.6078, 2.49160000000001, 3.13099999999997, 2.5376, 3.19880000000001, 3.21100000000001, 2.4502, 3.52820000000003, 2.91199999999998, 3.04480000000001, 2.7432, 2.85239999999999, 2.79880000000003, 2.78579999999999, 1.88679999999999, 2.98860000000002, 2.50639999999999, 1.91239999999999, 2.66160000000002, 2.46820000000002, 1.58199999999999, 1.30399999999997, 2.27379999999999, 2.68939999999998, 1.32900000000001, 3.10599999999999, 1.69080000000002, 2.13740000000001, 2.53219999999999, 1.88479999999998, 1.33240000000001, 1.45119999999997, 1.17899999999997, 2.44119999999998, 1.606599999999
 96, 2.16700000000003, 0.77940000000001, 2.37900000000002, 2.06700000000001, 1.46000000000004, 2.91160000000002, 1.69200000000001, 0.954600000000028, 2.49300000000005, 2.2722, 1.33500000000004, 2.44899999999996, 1.20140000000004, 3.07380000000001, 2.09739999999999, 2.85640000000001, 2.29960000000005, 2.40899999999999, 1.97040000000004, 0.809799999999996, 1.65279999999996, 2.59979999999996, 0.95799999999997, 2.06799999999998, 2.32780000000002, 4.20159999999998, 1.96320000000003, 1.86400000000003, 1.42999999999995, 3.77940000000001, 1.27200000000005, 1.86440000000005, 2.20600000000002, 3.21900000000005, 1.5154, 2.61019999999996,},
++    // precision 8
++    {183.2152, 180.2454, 177.2096, 173.6652, 170.6312, 167.6822, 164.249, 161.3296, 158.0038, 155.2074, 152.4612, 149.27, 146.5178, 143.4412, 140.8032, 138.1634, 135.1688, 132.6074, 129.6946, 127.2664, 124.8228, 122.0432, 119.6824, 116.9464, 114.6268, 112.2626, 109.8376, 107.4034, 104.8956, 102.8522, 100.7638, 98.3552, 96.3556, 93.7526, 91.9292, 89.8954, 87.8198, 85.7668, 83.298, 81.6688, 79.9466, 77.9746, 76.1672, 74.3474, 72.3028, 70.8912, 69.114, 67.4646, 65.9744, 64.4092, 62.6022, 60.843, 59.5684, 58.1652, 56.5426, 55.4152, 53.5388, 52.3592, 51.1366, 49.486, 48.3918, 46.5076, 45.509, 44.3834, 43.3498, 42.0668, 40.7346, 40.1228, 38.4528, 37.7, 36.644, 36.0518, 34.5774, 33.9068, 32.432, 32.1666, 30.434, 29.6644, 28.4894, 27.6312, 26.3804, 26.292, 25.5496000000001, 25.0234, 24.8206, 22.6146, 22.4188, 22.117, 20.6762, 20.6576, 19.7864, 19.509, 18.5334, 17.9204, 17.772, 16.2924, 16.8654, 15.1836, 15.745, 15.1316, 15.0386, 14.0136, 13.6342, 12.6196, 12.1866, 12.4281999999999, 11.3324
 , 10.4794000000001, 11.5038, 10.129, 9.52800000000002, 10.3203999999999, 9.46299999999997, 9.79280000000006, 9.12300000000005, 8.74180000000001, 9.2192, 7.51020000000005, 7.60659999999996, 7.01840000000004, 7.22239999999999, 7.40139999999997, 6.76179999999999, 7.14359999999999, 5.65060000000005, 5.63779999999997, 5.76599999999996, 6.75139999999999, 5.57759999999996, 3.73220000000003, 5.8048, 5.63019999999995, 4.93359999999996, 3.47979999999995, 4.33879999999999, 3.98940000000005, 3.81960000000004, 3.31359999999995, 3.23080000000004, 3.4588, 3.08159999999998, 3.4076, 3.00639999999999, 2.38779999999997, 2.61900000000003, 1.99800000000005, 3.34820000000002, 2.95060000000001, 0.990999999999985, 2.11440000000005, 2.20299999999997, 2.82219999999995, 2.73239999999998, 2.7826, 3.76660000000004, 2.26480000000004, 2.31280000000004, 2.40819999999997, 2.75360000000001, 3.33759999999995, 2.71559999999999, 1.7478000000001, 1.42920000000004, 2.39300000000003, 2.22779999999989, 2.34339999999997, 0.
 87259999999992, 3.88400000000001, 1.80600000000004, 1.91759999999999, 1.16779999999994, 1.50320000000011, 2.52500000000009, 0.226400000000012, 2.31500000000005, 0.930000000000064, 1.25199999999995, 2.14959999999996, 0.0407999999999902, 2.5447999999999, 1.32960000000003, 0.197400000000016, 2.52620000000002, 3.33279999999991, -1.34300000000007, 0.422199999999975, 0.917200000000093, 1.12920000000008, 1.46060000000011, 1.45779999999991, 2.8728000000001, 3.33359999999993, -1.34079999999994, 1.57680000000005, 0.363000000000056, 1.40740000000005, 0.656600000000026, 0.801400000000058, -0.454600000000028, 1.51919999999996,},
++    // precision 9
++    {368, 361.8294, 355.2452, 348.6698, 342.1464, 336.2024, 329.8782, 323.6598, 317.462, 311.2826, 305.7102, 299.7416, 293.9366, 288.1046, 282.285, 277.0668, 271.306, 265.8448, 260.301, 254.9886, 250.2422, 244.8138, 239.7074, 234.7428, 229.8402, 225.1664, 220.3534, 215.594, 210.6886, 205.7876, 201.65, 197.228, 192.8036, 188.1666, 184.0818, 180.0824, 176.2574, 172.302, 168.1644, 164.0056, 160.3802, 156.7192, 152.5234, 149.2084, 145.831, 142.485, 139.1112, 135.4764, 131.76, 129.3368, 126.5538, 122.5058, 119.2646, 116.5902, 113.3818, 110.8998, 107.9532, 105.2062, 102.2798, 99.4728, 96.9582, 94.3292, 92.171, 89.7809999999999, 87.5716, 84.7048, 82.5322, 79.875, 78.3972, 75.3464, 73.7274, 71.2834, 70.1444, 68.4263999999999, 66.0166, 64.018, 62.0437999999999, 60.3399999999999, 58.6856, 57.9836, 55.0311999999999, 54.6769999999999, 52.3188, 51.4846, 49.4423999999999, 47.739, 46.1487999999999, 44.9202, 43.4059999999999, 42.5342000000001, 41.2834, 38.8954000000001, 38.3286000000001, 36.2146, 
 36.6684, 35.9946, 33.123, 33.4338, 31.7378000000001, 29.076, 28.9692, 27.4964, 27.0998, 25.9864, 26.7754, 24.3208, 23.4838, 22.7388000000001, 24.0758000000001, 21.9097999999999, 20.9728, 19.9228000000001, 19.9292, 16.617, 17.05, 18.2996000000001, 15.6128000000001, 15.7392, 14.5174, 13.6322, 12.2583999999999, 13.3766000000001, 11.423, 13.1232, 9.51639999999998, 10.5938000000001, 9.59719999999993, 8.12220000000002, 9.76739999999995, 7.50440000000003, 7.56999999999994, 6.70440000000008, 6.41419999999994, 6.71019999999999, 5.60940000000005, 4.65219999999999, 6.84099999999989, 3.4072000000001, 3.97859999999991, 3.32760000000007, 5.52160000000003, 3.31860000000006, 2.06940000000009, 4.35400000000004, 1.57500000000005, 0.280799999999999, 2.12879999999996, -0.214799999999968, -0.0378000000000611, -0.658200000000079, 0.654800000000023, -0.0697999999999865, 0.858400000000074, -2.52700000000004, -2.1751999999999, -3.35539999999992, -1.04019999999991, -0.651000000000067, -2.14439999999991, -1.9
 6659999999997, -3.97939999999994, -0.604400000000169, -3.08260000000018, -3.39159999999993, -5.29640000000018, -5.38920000000007, -5.08759999999984, -4.69900000000007, -5.23720000000003, -3.15779999999995, -4.97879999999986, -4.89899999999989, -7.48880000000008, -5.94799999999987, -5.68060000000014, -6.67180000000008, -4.70499999999993, -7.27779999999984, -4.6579999999999, -4.4362000000001, -4.32139999999981, -5.18859999999995, -6.66879999999992, -6.48399999999992, -5.1260000000002, -4.4032000000002, -6.13500000000022, -5.80819999999994, -4.16719999999987, -4.15039999999999, -7.45600000000013, -7.24080000000004, -9.83179999999993, -5.80420000000004, -8.6561999999999, -6.99940000000015, -10.5473999999999, -7.34139999999979, -6.80999999999995, -6.29719999999998, -6.23199999999997,},
++    // precision 10
++    {737.1256, 724.4234, 711.1064, 698.4732, 685.4636, 673.0644, 660.488, 647.9654, 636.0832, 623.7864, 612.1992, 600.2176, 588.5228, 577.1716, 565.7752, 554.899, 543.6126, 532.6492, 521.9474, 511.5214, 501.1064, 490.6364, 480.2468, 470.4588, 460.3832, 451.0584, 440.8606, 431.3868, 422.5062, 413.1862, 404.463, 395.339, 386.1936, 378.1292, 369.1854, 361.2908, 353.3324, 344.8518, 337.5204, 329.4854, 321.9318, 314.552, 306.4658, 299.4256, 292.849, 286.152, 278.8956, 271.8792, 265.118, 258.62, 252.5132, 245.9322, 239.7726, 233.6086, 227.5332, 222.5918, 216.4294, 210.7662, 205.4106, 199.7338, 194.9012, 188.4486, 183.1556, 178.6338, 173.7312, 169.6264, 163.9526, 159.8742, 155.8326, 151.1966, 147.5594, 143.07, 140.037, 134.1804, 131.071, 127.4884, 124.0848, 120.2944, 117.333, 112.9626, 110.2902, 107.0814, 103.0334, 99.4832000000001, 96.3899999999999, 93.7202000000002, 90.1714000000002, 87.2357999999999, 85.9346, 82.8910000000001, 80.0264000000002, 78.3834000000002, 75.1543999999999, 73.86
 83999999998, 70.9895999999999, 69.4367999999999, 64.8701999999998, 65.0408000000002, 61.6738, 59.5207999999998, 57.0158000000001, 54.2302, 53.0962, 50.4985999999999, 52.2588000000001, 47.3914, 45.6244000000002, 42.8377999999998, 43.0072, 40.6516000000001, 40.2453999999998, 35.2136, 36.4546, 33.7849999999999, 33.2294000000002, 32.4679999999998, 30.8670000000002, 28.6507999999999, 28.9099999999999, 27.5983999999999, 26.1619999999998, 24.5563999999999, 23.2328000000002, 21.9484000000002, 21.5902000000001, 21.3346000000001, 17.7031999999999, 20.6111999999998, 19.5545999999999, 15.7375999999999, 17.0720000000001, 16.9517999999998, 15.326, 13.1817999999998, 14.6925999999999, 13.0859999999998, 13.2754, 10.8697999999999, 11.248, 7.3768, 4.72339999999986, 7.97899999999981, 8.7503999999999, 7.68119999999999, 9.7199999999998, 7.73919999999998, 5.6224000000002, 7.44560000000001, 6.6601999999998, 5.9058, 4.00199999999995, 4.51699999999983, 4.68240000000014, 3.86220000000003, 5.13639999999987, 5.
 98500000000013, 2.47719999999981, 2.61999999999989, 1.62800000000016, 4.65000000000009, 0.225599999999758, 0.831000000000131, -0.359400000000278, 1.27599999999984, -2.92559999999958, -0.0303999999996449, 2.37079999999969, -2.0033999999996, 0.804600000000391, 0.30199999999968, 1.1247999999996, -2.6880000000001, 0.0321999999996478, -1.18099999999959, -3.9402, -1.47940000000017, -0.188400000000001, -2.10720000000038, -2.04159999999956, -3.12880000000041, -4.16160000000036, -0.612799999999879, -3.48719999999958, -8.17900000000009, -5.37780000000021, -4.01379999999972, -5.58259999999973, -5.73719999999958, -7.66799999999967, -5.69520000000011, -1.1247999999996, -5.58520000000044, -8.04560000000038, -4.64840000000004, -11.6468000000004, -7.97519999999986, -5.78300000000036, -7.67420000000038, -10.6328000000003, -9.81720000000041,},
++    // precision 11
++    {1476, 1449.6014, 1423.5802, 1397.7942, 1372.3042, 1347.2062, 1321.8402, 1297.2292, 1272.9462, 1248.9926, 1225.3026, 1201.4252, 1178.0578, 1155.6092, 1132.626, 1110.5568, 1088.527, 1066.5154, 1045.1874, 1024.3878, 1003.37, 982.1972, 962.5728, 942.1012, 922.9668, 903.292, 884.0772, 864.8578, 846.6562, 828.041, 809.714, 792.3112, 775.1806, 757.9854, 740.656, 724.346, 707.5154, 691.8378, 675.7448, 659.6722, 645.5722, 630.1462, 614.4124, 600.8728, 585.898, 572.408, 558.4926, 544.4938, 531.6776, 517.282, 505.7704, 493.1012, 480.7388, 467.6876, 456.1872, 445.5048, 433.0214, 420.806, 411.409, 400.4144, 389.4294, 379.2286, 369.651, 360.6156, 350.337, 342.083, 332.1538, 322.5094, 315.01, 305.6686, 298.1678, 287.8116, 280.9978, 271.9204, 265.3286, 257.5706, 249.6014, 242.544, 235.5976, 229.583, 220.9438, 214.672, 208.2786, 201.8628, 195.1834, 191.505, 186.1816, 178.5188, 172.2294, 167.8908, 161.0194, 158.052, 151.4588, 148.1596, 143.4344, 138.5238, 133.13, 127.6374, 124.8162, 118.7894, 1
 17.3984, 114.6078, 109.0858, 105.1036, 103.6258, 98.6018000000004, 95.7618000000002, 93.5821999999998, 88.5900000000001, 86.9992000000002, 82.8800000000001, 80.4539999999997, 74.6981999999998, 74.3644000000004, 73.2914000000001, 65.5709999999999, 66.9232000000002, 65.1913999999997, 62.5882000000001, 61.5702000000001, 55.7035999999998, 56.1764000000003, 52.7596000000003, 53.0302000000001, 49.0609999999997, 48.4694, 44.933, 46.0474000000004, 44.7165999999997, 41.9416000000001, 39.9207999999999, 35.6328000000003, 35.5276000000003, 33.1934000000001, 33.2371999999996, 33.3864000000003, 33.9228000000003, 30.2371999999996, 29.1373999999996, 25.2272000000003, 24.2942000000003, 19.8338000000003, 18.9005999999999, 23.0907999999999, 21.8544000000002, 19.5176000000001, 15.4147999999996, 16.9314000000004, 18.6737999999996, 12.9877999999999, 14.3688000000002, 12.0447999999997, 15.5219999999999, 12.5299999999997, 14.5940000000001, 14.3131999999996, 9.45499999999993, 12.9441999999999, 3.91139999999
 996, 13.1373999999996, 5.44720000000052, 9.82779999999912, 7.87279999999919, 3.67760000000089, 5.46980000000076, 5.55099999999948, 5.65979999999945, 3.89439999999922, 3.1275999999998, 5.65140000000065, 6.3062000000009, 3.90799999999945, 1.87060000000019, 5.17020000000048, 2.46680000000015, 0.770000000000437, -3.72340000000077, 1.16400000000067, 8.05340000000069, 0.135399999999208, 2.15940000000046, 0.766999999999825, 1.0594000000001, 3.15500000000065, -0.287399999999252, 2.37219999999979, -2.86620000000039, -1.63199999999961, -2.22979999999916, -0.15519999999924, -1.46039999999994, -0.262199999999211, -2.34460000000036, -2.8078000000005, -3.22179999999935, -5.60159999999996, -8.42200000000048, -9.43740000000071, 0.161799999999857, -10.4755999999998, -10.0823999999993,},
++    // precision 12
++    {2953, 2900.4782, 2848.3568, 2796.3666, 2745.324, 2694.9598, 2644.648, 2595.539, 2546.1474, 2498.2576, 2450.8376, 2403.6076, 2357.451, 2311.38, 2266.4104, 2221.5638, 2176.9676, 2134.193, 2090.838, 2048.8548, 2007.018, 1966.1742, 1925.4482, 1885.1294, 1846.4776, 1807.4044, 1768.8724, 1731.3732, 1693.4304, 1657.5326, 1621.949, 1586.5532, 1551.7256, 1517.6182, 1483.5186, 1450.4528, 1417.865, 1385.7164, 1352.6828, 1322.6708, 1291.8312, 1260.9036, 1231.476, 1201.8652, 1173.6718, 1145.757, 1119.2072, 1092.2828, 1065.0434, 1038.6264, 1014.3192, 988.5746, 965.0816, 940.1176, 917.9796, 894.5576, 871.1858, 849.9144, 827.1142, 805.0818, 783.9664, 763.9096, 742.0816, 724.3962, 706.3454, 688.018, 667.4214, 650.3106, 633.0686, 613.8094, 597.818, 581.4248, 563.834, 547.363, 531.5066, 520.455400000001, 505.583199999999, 488.366, 476.480799999999, 459.7682, 450.0522, 434.328799999999, 423.952799999999, 408.727000000001, 399.079400000001, 387.252200000001, 373.987999999999, 360.852000000001, 351
 .6394, 339.642, 330.902400000001, 322.661599999999, 311.662200000001, 301.3254, 291.7484, 279.939200000001, 276.7508, 263.215200000001, 254.811400000001, 245.5494, 242.306399999999, 234.8734, 223.787200000001, 217.7156, 212.0196, 200.793, 195.9748, 189.0702, 182.449199999999, 177.2772, 170.2336, 164.741, 158.613600000001, 155.311, 147.5964, 142.837, 137.3724, 132.0162, 130.0424, 121.9804, 120.451800000001, 114.8968, 111.585999999999, 105.933199999999, 101.705, 98.5141999999996, 95.0488000000005, 89.7880000000005, 91.4750000000004, 83.7764000000006, 80.9698000000008, 72.8574000000008, 73.1615999999995, 67.5838000000003, 62.6263999999992, 63.2638000000006, 66.0977999999996, 52.0843999999997, 58.9956000000002, 47.0912000000008, 46.4956000000002, 48.4383999999991, 47.1082000000006, 43.2392, 37.2759999999998, 40.0283999999992, 35.1864000000005, 35.8595999999998, 32.0998, 28.027, 23.6694000000007, 33.8266000000003, 26.3736000000008, 27.2008000000005, 21.3245999999999, 26.4115999999995, 23
 .4521999999997, 19.5013999999992, 19.8513999999996, 10.7492000000002, 18.6424000000006, 13.1265999999996, 18.2436000000016, 6.71860000000015, 3.39459999999963, 6.33759999999893, 7.76719999999841, 0.813999999998487, 3.82819999999992, 0.826199999999517, 8.07440000000133, -1.59080000000176, 5.01780000000144, 0.455399999998917, -0.24199999999837, 0.174800000000687, -9.07640000000174, -4.20160000000033, -3.77520000000004, -4.75179999999818, -5.3724000000002, -8.90680000000066, -6.10239999999976, -5.74120000000039, -9.95339999999851, -3.86339999999836, -13.7304000000004, -16.2710000000006, -7.51359999999841, -3.30679999999847, -13.1339999999982, -10.0551999999989, -6.72019999999975, -8.59660000000076, -10.9307999999983, -1.8775999999998, -4.82259999999951, -13.7788, -21.6470000000008, -10.6735999999983, -15.7799999999988,},
++    // precision 13
++    {5907.5052, 5802.2672, 5697.347, 5593.5794, 5491.2622, 5390.5514, 5290.3376, 5191.6952, 5093.5988, 4997.3552, 4902.5972, 4808.3082, 4715.5646, 4624.109, 4533.8216, 4444.4344, 4356.3802, 4269.2962, 4183.3784, 4098.292, 4014.79, 3932.4574, 3850.6036, 3771.2712, 3691.7708, 3615.099, 3538.1858, 3463.4746, 3388.8496, 3315.6794, 3244.5448, 3173.7516, 3103.3106, 3033.6094, 2966.5642, 2900.794, 2833.7256, 2769.81, 2707.3196, 2644.0778, 2583.9916, 2523.4662, 2464.124, 2406.073, 2347.0362, 2292.1006, 2238.1716, 2182.7514, 2128.4884, 2077.1314, 2025.037, 1975.3756, 1928.933, 1879.311, 1831.0006, 1783.2144, 1738.3096, 1694.5144, 1649.024, 1606.847, 1564.7528, 1525.3168, 1482.5372, 1443.9668, 1406.5074, 1365.867, 1329.2186, 1295.4186, 1257.9716, 1225.339, 1193.2972, 1156.3578, 1125.8686, 1091.187, 1061.4094, 1029.4188, 1000.9126, 972.3272, 944.004199999999, 915.7592, 889.965, 862.834200000001, 840.4254, 812.598399999999, 785.924200000001, 763.050999999999, 741.793799999999, 721.466, 699.040
 799999999, 677.997200000002, 649.866999999998, 634.911800000002, 609.8694, 591.981599999999, 570.2922, 557.129199999999, 538.3858, 521.872599999999, 502.951400000002, 495.776399999999, 475.171399999999, 459.751, 439.995200000001, 426.708999999999, 413.7016, 402.3868, 387.262599999998, 372.0524, 357.050999999999, 342.5098, 334.849200000001, 322.529399999999, 311.613799999999, 295.848000000002, 289.273000000001, 274.093000000001, 263.329600000001, 251.389599999999, 245.7392, 231.9614, 229.7952, 217.155200000001, 208.9588, 199.016599999999, 190.839199999999, 180.6976, 176.272799999999, 166.976999999999, 162.5252, 151.196400000001, 149.386999999999, 133.981199999998, 130.0586, 130.164000000001, 122.053400000001, 110.7428, 108.1276, 106.232400000001, 100.381600000001, 98.7668000000012, 86.6440000000002, 79.9768000000004, 82.4722000000002, 68.7026000000005, 70.1186000000016, 71.9948000000004, 58.998599999999, 59.0492000000013, 56.9818000000014, 47.5338000000011, 42.9928, 51.1591999999982,
  37.2740000000013, 42.7220000000016, 31.3734000000004, 26.8090000000011, 25.8934000000008, 26.5286000000015, 29.5442000000003, 19.3503999999994, 26.0760000000009, 17.9527999999991, 14.8419999999969, 10.4683999999979, 8.65899999999965, 9.86720000000059, 4.34139999999752, -0.907800000000861, -3.32080000000133, -0.936199999996461, -11.9916000000012, -8.87000000000262, -6.33099999999831, -11.3366000000024, -15.9207999999999, -9.34659999999712, -15.5034000000014, -19.2097999999969, -15.357799999998, -28.2235999999975, -30.6898000000001, -19.3271999999997, -25.6083999999973, -24.409599999999, -13.6385999999984, -33.4473999999973, -32.6949999999997, -28.9063999999998, -31.7483999999968, -32.2935999999972, -35.8329999999987, -47.620600000002, -39.0855999999985, -33.1434000000008, -46.1371999999974, -37.5892000000022, -46.8164000000033, -47.3142000000007, -60.2914000000019, -37.7575999999972,},
++    // precision 14
++    {11816.475, 11605.0046, 11395.3792, 11188.7504, 10984.1814, 10782.0086, 10582.0072, 10384.503, 10189.178, 9996.2738, 9806.0344, 9617.9798, 9431.394, 9248.7784, 9067.6894, 8889.6824, 8712.9134, 8538.8624, 8368.4944, 8197.7956, 8031.8916, 7866.6316, 7703.733, 7544.5726, 7386.204, 7230.666, 7077.8516, 6926.7886, 6778.6902, 6631.9632, 6487.304, 6346.7486, 6206.4408, 6070.202, 5935.2576, 5799.924, 5671.0324, 5541.9788, 5414.6112, 5290.0274, 5166.723, 5047.6906, 4929.162, 4815.1406, 4699.127, 4588.5606, 4477.7394, 4369.4014, 4264.2728, 4155.9224, 4055.581, 3955.505, 3856.9618, 3761.3828, 3666.9702, 3575.7764, 3482.4132, 3395.0186, 3305.8852, 3221.415, 3138.6024, 3056.296, 2970.4494, 2896.1526, 2816.8008, 2740.2156, 2670.497, 2594.1458, 2527.111, 2460.8168, 2387.5114, 2322.9498, 2260.6752, 2194.2686, 2133.7792, 2074.767, 2015.204, 1959.4226, 1898.6502, 1850.006, 1792.849, 1741.4838, 1687.9778, 1638.1322, 1589.3266, 1543.1394, 1496.8266, 1447.8516, 1402.7354, 1361.9606, 1327.0692, 1285
 .4106, 1241.8112, 1201.6726, 1161.973, 1130.261, 1094.2036, 1048.2036, 1020.6436, 990.901400000002, 961.199800000002, 924.769800000002, 899.526400000002, 872.346400000002, 834.375, 810.432000000001, 780.659800000001, 756.013800000001, 733.479399999997, 707.923999999999, 673.858, 652.222399999999, 636.572399999997, 615.738599999997, 586.696400000001, 564.147199999999, 541.679600000003, 523.943599999999, 505.714599999999, 475.729599999999, 461.779600000002, 449.750800000002, 439.020799999998, 412.7886, 400.245600000002, 383.188199999997, 362.079599999997, 357.533799999997, 334.319000000003, 327.553399999997, 308.559399999998, 291.270199999999, 279.351999999999, 271.791400000002, 252.576999999997, 247.482400000001, 236.174800000001, 218.774599999997, 220.155200000001, 208.794399999999, 201.223599999998, 182.995600000002, 185.5268, 164.547400000003, 176.5962, 150.689599999998, 157.8004, 138.378799999999, 134.021200000003, 117.614399999999, 108.194000000003, 97.0696000000025, 89.60420000
 00016, 95.6030000000028, 84.7810000000027, 72.635000000002, 77.3482000000004, 59.4907999999996, 55.5875999999989, 50.7346000000034, 61.3916000000027, 50.9149999999936, 39.0384000000049, 58.9395999999979, 29.633600000001, 28.2032000000036, 26.0078000000067, 17.0387999999948, 9.22000000000116, 13.8387999999977, 8.07240000000456, 14.1549999999988, 15.3570000000036, 3.42660000000615, 6.24820000000182, -2.96940000000177, -8.79940000000352, -5.97860000000219, -14.4048000000039, -3.4143999999942, -13.0148000000045, -11.6977999999945, -25.7878000000055, -22.3185999999987, -24.409599999999, -31.9756000000052, -18.9722000000038, -22.8678000000073, -30.8972000000067, -32.3715999999986, -22.3907999999938, -43.6720000000059, -35.9038, -39.7492000000057, -54.1641999999993, -45.2749999999942, -42.2989999999991, -44.1089999999967, -64.3564000000042, -49.9551999999967, -42.6116000000038,},
++    // precision 15
++    {23634.0036, 23210.8034, 22792.4744, 22379.1524, 21969.7928, 21565.326, 21165.3532, 20770.2806, 20379.9892, 19994.7098, 19613.318, 19236.799, 18865.4382, 18498.8244, 18136.5138, 17778.8668, 17426.2344, 17079.32, 16734.778, 16397.2418, 16063.3324, 15734.0232, 15409.731, 15088.728, 14772.9896, 14464.1402, 14157.5588, 13855.5958, 13559.3296, 13264.9096, 12978.326, 12692.0826, 12413.8816, 12137.3192, 11870.2326, 11602.5554, 11340.3142, 11079.613, 10829.5908, 10583.5466, 10334.0344, 10095.5072, 9859.694, 9625.2822, 9395.7862, 9174.0586, 8957.3164, 8738.064, 8524.155, 8313.7396, 8116.9168, 7913.542, 7718.4778, 7521.65, 7335.5596, 7154.2906, 6968.7396, 6786.3996, 6613.236, 6437.406, 6270.6598, 6107.7958, 5945.7174, 5787.6784, 5635.5784, 5482.308, 5337.9784, 5190.0864, 5045.9158, 4919.1386, 4771.817, 4645.7742, 4518.4774, 4385.5454, 4262.6622, 4142.74679999999, 4015.5318, 3897.9276, 3790.7764, 3685.13800000001, 3573.6274, 3467.9706, 3368.61079999999, 3271.5202, 3170.3848, 3076.4656, 29
 82.38400000001, 2888.4664, 2806.4868, 2711.9564, 2634.1434, 2551.3204, 2469.7662, 2396.61139999999, 2318.9902, 2243.8658, 2171.9246, 2105.01360000001, 2028.8536, 1960.9952, 1901.4096, 1841.86079999999, 1777.54700000001, 1714.5802, 1654.65059999999, 1596.311, 1546.2016, 1492.3296, 1433.8974, 1383.84600000001, 1339.4152, 1293.5518, 1245.8686, 1193.50659999999, 1162.27959999999, 1107.19439999999, 1069.18060000001, 1035.09179999999, 999.679000000004, 957.679999999993, 925.300199999998, 888.099400000006, 848.638600000006, 818.156400000007, 796.748399999997, 752.139200000005, 725.271200000003, 692.216, 671.633600000001, 647.939799999993, 621.670599999998, 575.398799999995, 561.226599999995, 532.237999999998, 521.787599999996, 483.095799999996, 467.049599999998, 465.286399999997, 415.548599999995, 401.047399999996, 380.607999999993, 377.362599999993, 347.258799999996, 338.371599999999, 310.096999999994, 301.409199999995, 276.280799999993, 265.586800000005, 258.994399999996, 223.91599999999
 7, 215.925399999993, 213.503800000006, 191.045400000003, 166.718200000003, 166.259000000005, 162.941200000001, 148.829400000002, 141.645999999993, 123.535399999993, 122.329800000007, 89.473399999988, 80.1962000000058, 77.5457999999926, 59.1056000000099, 83.3509999999951, 52.2906000000075, 36.3979999999865, 40.6558000000077, 42.0003999999899, 19.6630000000005, 19.7153999999864, -8.38539999999921, -0.692799999989802, 0.854800000000978, 3.23219999999856, -3.89040000000386, -5.25880000001052, -24.9052000000083, -22.6837999999989, -26.4286000000138, -34.997000000003, -37.0216000000073, -43.430400000012, -58.2390000000014, -68.8034000000043, -56.9245999999985, -57.8583999999973, -77.3097999999882, -73.2793999999994, -81.0738000000129, -87.4530000000086, -65.0254000000132, -57.296399999992, -96.2746000000043, -103.25, -96.081600000005, -91.5542000000132, -102.465200000006, -107.688599999994, -101.458000000013, -109.715800000005,},
++    // precision 16
++    {47270, 46423.3584, 45585.7074, 44757.152, 43938.8416, 43130.9514, 42330.03, 41540.407, 40759.6348, 39988.206, 39226.5144, 38473.2096, 37729.795, 36997.268, 36272.6448, 35558.665, 34853.0248, 34157.4472, 33470.5204, 32793.5742, 32127.0194, 31469.4182, 30817.6136, 30178.6968, 29546.8908, 28922.8544, 28312.271, 27707.0924, 27114.0326, 26526.692, 25948.6336, 25383.7826, 24823.5998, 24272.2974, 23732.2572, 23201.4976, 22674.2796, 22163.6336, 21656.515, 21161.7362, 20669.9368, 20189.4424, 19717.3358, 19256.3744, 18795.9638, 18352.197, 17908.5738, 17474.391, 17052.918, 16637.2236, 16228.4602, 15823.3474, 15428.6974, 15043.0284, 14667.6278, 14297.4588, 13935.2882, 13578.5402, 13234.6032, 12882.1578, 12548.0728, 12219.231, 11898.0072, 11587.2626, 11279.9072, 10973.5048, 10678.5186, 10392.4876, 10105.2556, 9825.766, 9562.5444, 9294.2222, 9038.2352, 8784.848, 8533.2644, 8301.7776, 8058.30859999999, 7822.94579999999, 7599.11319999999, 7366.90779999999, 7161.217, 6957.53080000001, 6736.212
 , 6548.21220000001, 6343.06839999999, 6156.28719999999, 5975.15419999999, 5791.75719999999, 5621.32019999999, 5451.66, 5287.61040000001, 5118.09479999999, 4957.288, 4798.4246, 4662.17559999999, 4512.05900000001, 4364.68539999999, 4220.77720000001, 4082.67259999999, 3957.19519999999, 3842.15779999999, 3699.3328, 3583.01180000001, 3473.8964, 3338.66639999999, 3233.55559999999, 3117.799, 3008.111, 2909.69140000001, 2814.86499999999, 2719.46119999999, 2624.742, 2532.46979999999, 2444.7886, 2370.1868, 2272.45259999999, 2196.19260000001, 2117.90419999999, 2023.2972, 1969.76819999999, 1885.58979999999, 1833.2824, 1733.91200000001, 1682.54920000001, 1604.57980000001, 1556.11240000001, 1491.3064, 1421.71960000001, 1371.22899999999, 1322.1324, 1264.7892, 1196.23920000001, 1143.8474, 1088.67240000001, 1073.60380000001, 1023.11660000001, 959.036400000012, 927.433199999999, 906.792799999996, 853.433599999989, 841.873800000001, 791.1054, 756.899999999994, 704.343200000003, 672.495599999995, 622.7
 90399999998, 611.254799999995, 567.283200000005, 519.406599999988, 519.188400000014, 495.312800000014, 451.350799999986, 443.973399999988, 431.882199999993, 392.027000000002, 380.924200000009, 345.128999999986, 298.901400000002, 287.771999999997, 272.625, 247.253000000026, 222.490600000019, 223.590000000026, 196.407599999977, 176.425999999978, 134.725199999986, 132.4804, 110.445599999977, 86.7939999999944, 56.7038000000175, 64.915399999998, 38.3726000000024, 37.1606000000029, 46.170999999973, 49.1716000000015, 15.3362000000197, 6.71639999997569, -34.8185999999987, -39.4476000000141, 12.6830000000191, -12.3331999999937, -50.6565999999875, -59.9538000000175, -65.1054000000004, -70.7576000000117, -106.325200000021, -126.852200000023, -110.227599999984, -132.885999999999, -113.897200000007, -142.713800000027, -151.145399999979, -150.799200000009, -177.756200000003, -156.036399999983, -182.735199999996, -177.259399999981, -198.663600000029, -174.577600000019, -193.84580000001,},
++    // precision 17
++    {94541, 92848.811, 91174.019, 89517.558, 87879.9705, 86262.7565, 84663.5125, 83083.7435, 81521.7865, 79977.272, 78455.9465, 76950.219, 75465.432, 73994.152, 72546.71, 71115.2345, 69705.6765, 68314.937, 66944.2705, 65591.255, 64252.9485, 62938.016, 61636.8225, 60355.592, 59092.789, 57850.568, 56624.518, 55417.343, 54231.1415, 53067.387, 51903.526, 50774.649, 49657.6415, 48561.05, 47475.7575, 46410.159, 45364.852, 44327.053, 43318.4005, 42325.6165, 41348.4595, 40383.6265, 39436.77, 38509.502, 37594.035, 36695.939, 35818.6895, 34955.691, 34115.8095, 33293.949, 32465.0775, 31657.6715, 30877.2585, 30093.78, 29351.3695, 28594.1365, 27872.115, 27168.7465, 26477.076, 25774.541, 25106.5375, 24452.5135, 23815.5125, 23174.0655, 22555.2685, 21960.2065, 21376.3555, 20785.1925, 20211.517, 19657.0725, 19141.6865, 18579.737, 18081.3955, 17578.995, 17073.44, 16608.335, 16119.911, 15651.266, 15194.583, 14749.0495, 14343.4835, 13925.639, 13504.509, 13099.3885, 12691.2855, 12328.018, 11969.0345, 1
 1596.5145, 11245.6355, 10917.6575, 10580.9785, 10277.8605, 9926.58100000001, 9605.538, 9300.42950000003, 8989.97850000003, 8728.73249999998, 8448.3235, 8175.31050000002, 7898.98700000002, 7629.79100000003, 7413.76199999999, 7149.92300000001, 6921.12650000001, 6677.1545, 6443.28000000003, 6278.23450000002, 6014.20049999998, 5791.20299999998, 5605.78450000001, 5438.48800000001, 5234.2255, 5059.6825, 4887.43349999998, 4682.935, 4496.31099999999, 4322.52250000002, 4191.42499999999, 4021.24200000003, 3900.64799999999, 3762.84250000003, 3609.98050000001, 3502.29599999997, 3363.84250000003, 3206.54849999998, 3079.70000000001, 2971.42300000001, 2867.80349999998, 2727.08100000001, 2630.74900000001, 2496.6165, 2440.902, 2356.19150000002, 2235.58199999999, 2120.54149999999, 2012.25449999998, 1933.35600000003, 1820.93099999998, 1761.54800000001, 1663.09350000002, 1578.84600000002, 1509.48149999999, 1427.3345, 1379.56150000001, 1306.68099999998, 1212.63449999999, 1084.17300000001, 1124.164500000
 01, 1060.69949999999, 1007.48849999998, 941.194499999983, 879.880500000028, 836.007500000007, 782.802000000025, 748.385499999975, 647.991500000004, 626.730500000005, 570.776000000013, 484.000500000024, 513.98550000001, 418.985499999952, 386.996999999974, 370.026500000036, 355.496999999974, 356.731499999994, 255.92200000002, 259.094000000041, 205.434499999974, 165.374500000034, 197.347500000033, 95.718499999959, 67.6165000000037, 54.6970000000438, 31.7395000000251, -15.8784999999916, 8.42500000004657, -26.3754999999655, -118.425500000012, -66.6629999999423, -42.9745000000112, -107.364999999991, -189.839000000036, -162.611499999999, -164.964999999967, -189.079999999958, -223.931499999948, -235.329999999958, -269.639500000048, -249.087999999989, -206.475499999942, -283.04449999996, -290.667000000016, -304.561499999953, -336.784499999951, -380.386500000022, -283.280499999993, -364.533000000054, -389.059499999974, -364.454000000027, -415.748000000021, -417.155000000028,},
++    // precision 18
++    {189083, 185696.913, 182348.774, 179035.946, 175762.762, 172526.444, 169329.754, 166166.099, 163043.269, 159958.91, 156907.912, 153906.845, 150924.199, 147996.568, 145093.457, 142239.233, 139421.475, 136632.27, 133889.588, 131174.2, 128511.619, 125868.621, 123265.385, 120721.061, 118181.769, 115709.456, 113252.446, 110840.198, 108465.099, 106126.164, 103823.469, 101556.618, 99308.004, 97124.508, 94937.803, 92833.731, 90745.061, 88677.627, 86617.47, 84650.442, 82697.833, 80769.132, 78879.629, 77014.432, 75215.626, 73384.587, 71652.482, 69895.93, 68209.301, 66553.669, 64921.981, 63310.323, 61742.115, 60205.018, 58698.658, 57190.657, 55760.865, 54331.169, 52908.167, 51550.273, 50225.254, 48922.421, 47614.533, 46362.049, 45098.569, 43926.083, 42736.03, 41593.473, 40425.26, 39316.237, 38243.651, 37170.617, 36114.609, 35084.19, 34117.233, 33206.509, 32231.505, 31318.728, 30403.404, 29540.0550000001, 28679.236, 27825.862, 26965.216, 26179.148, 25462.08, 24645.952, 23922.523, 23198.144
 , 22529.128, 21762.4179999999, 21134.779, 20459.117, 19840.818, 19187.04, 18636.3689999999, 17982.831, 17439.7389999999, 16874.547, 16358.2169999999, 15835.684, 15352.914, 14823.681, 14329.313, 13816.897, 13342.874, 12880.882, 12491.648, 12021.254, 11625.392, 11293.7610000001, 10813.697, 10456.209, 10099.074, 9755.39000000001, 9393.18500000006, 9047.57900000003, 8657.98499999999, 8395.85900000005, 8033, 7736.95900000003, 7430.59699999995, 7258.47699999996, 6924.58200000005, 6691.29399999999, 6357.92500000005, 6202.05700000003, 5921.19700000004, 5628.28399999999, 5404.96799999999, 5226.71100000001, 4990.75600000005, 4799.77399999998, 4622.93099999998, 4472.478, 4171.78700000001, 3957.46299999999, 3868.95200000005, 3691.14300000004, 3474.63100000005, 3341.67200000002, 3109.14000000001, 3071.97400000005, 2796.40399999998, 2756.17799999996, 2611.46999999997, 2471.93000000005, 2382.26399999997, 2209.22400000005, 2142.28399999999, 2013.96100000001, 1911.18999999994, 1818.27099999995, 1668
 .47900000005, 1519.65800000005, 1469.67599999998, 1367.13800000004, 1248.52899999998, 1181.23600000003, 1022.71900000004, 1088.20700000005, 959.03600000008, 876.095999999903, 791.183999999892, 703.337000000058, 731.949999999953, 586.86400000006, 526.024999999907, 323.004999999888, 320.448000000091, 340.672999999952, 309.638999999966, 216.601999999955, 102.922999999952, 19.2399999999907, -0.114000000059605, -32.6240000000689, -89.3179999999702, -153.497999999905, -64.2970000000205, -143.695999999996, -259.497999999905, -253.017999999924, -213.948000000091, -397.590000000084, -434.006000000052, -403.475000000093, -297.958000000101, -404.317000000039, -528.898999999976, -506.621000000043, -513.205000000075, -479.351000000024, -596.139999999898, -527.016999999993, -664.681000000099, -680.306000000099, -704.050000000047, -850.486000000034, -757.43200000003, -713.308999999892,}
++  };
++
++  private Format format;
++  private RegisterSet registerSet;
++  private final int m;
++  private final int p;
++
++  //Sparse versions of m and p
++  private int sm;
++  private int sp;
++
++  private final double alphaMM;
++
++  //How big the sparse set is allowed to get before we convert to 'normal'
++  private int sparseSetThreshold;
++  //How big the temp list is allowed to get before we batch merge it into the sparse set
++  private int sortThreshold;
++
++  private int[] tmpSet;
++  private int tmpIndex = 0;
++  private int[] sparseSet;
++
++  /**
++   * This constructor disables the sparse set.  If the counter is likely to exceed
++   * the sparse set thresholds than using this constructor will help avoid the
++   * extra memory pressure created by maintaining the sparse set until that threshold is
++   * breached.
++   *
++   * @param p - the precision value for the normal set
++   */
++  public HyperLogLogPlus(int p) {
++    this(p, 0);
++  }
++
++  /**
++   * Basic constructor for creating a instance that supports sparse and normal
++   * representations. The values of <code>p</code> and
++   * <code>sp</code> define the precision of the Normal and Sparse set
++   * representations for the data structure.  <code>p</code> must be a value
++   * between 4 and <code>sp</code> and <code>sp</code> must be less than 32.
++   *
++   * @param p  - the precision value for the normal set
++   * @param sp - the precision value for the sparse set
++   */
++  public HyperLogLogPlus(int p, int sp) {
++    this(p, sp, null, null);
++  }
++
++  /**
++   * Constructor to support instances serialized with the legacy sparse
++   * encoding scheme.
++   *
++   * @param p            - the precision value for the normal set
++   * @param sp           - the precision value for the sparse set
++   * @param deltaByteSet - a list of varint byte arrays encoded using a delta encoding scheme
++   */
++  public HyperLogLogPlus(int p, int sp, List<byte[]> deltaByteSet) {
++    this(p, sp);
++    sparseSet = new int[deltaByteSet.size()];
++    int previousValue = 0;
++    for (int i = 0; i < deltaByteSet.size(); i++) {
++      int nextValue = Varint.readUnsignedVarInt(deltaByteSet.get(i));
++      sparseSet[i] = nextValue + previousValue;
++      previousValue = sparseSet[i];
++    }
++  }
++
++  // for constructing a sparse mode hllp
++  private HyperLogLogPlus(int p, int sp, int[] sparseSet) {
++    this(p, sp, sparseSet, null);
++  }
++
++  // for constructing a normal mode hllp
++  private HyperLogLogPlus(int p, int sp, RegisterSet registerSet) {
++    this(p, sp, null, registerSet);
++  }
++
++  private HyperLogLogPlus(int p, int sp, int[] sparseSet, RegisterSet registerSet) {
++    if (p < 4 || (p > sp && sp != 0)) {
++      throw new IllegalArgumentException("p must be between 4 and sp (inclusive)");
++    }
++    if (sp > 32) {
++      throw new IllegalArgumentException("sp values greater than 32 not supported");
++    }
++
++    this.p = p;
++    m = (int) Math.pow(2, p);
++    format = Format.NORMAL;
++    this.registerSet = registerSet;
++    if (registerSet == null) {
++      if (sp > 0) // Use sparse representation
++      {
++        format = Format.SPARSE;
++        this.sp = sp;
++        sm = (int) Math.pow(2, sp);
++        this.sparseSet = sparseSet;
++        sparseSetThreshold = (int) (m * 0.75);
++        sortThreshold = sparseSetThreshold / 4;
++      } else {
++        this.registerSet = new RegisterSet((int) Math.pow(2, p));
++      }
++    }
++
++    this.alphaMM = HyperLogLog.getAlphaMM(p, m);
++  }
++
++  @Override
++  public boolean offerHashed(long hashedLong) {
++    switch (format) {
++    case NORMAL:
++      // find first p bits of x
++      final long idx = hashedLong >>> (64 - p);
++      //Ignore the first p bits (the idx), and then find the number of leading zeros
++      //Push a 1 to where the bit string would have ended if we didnt just push the idx out of the way
++      //A one is always added to runLength for estimation calculation purposes
++      final int runLength = Long.numberOfLeadingZeros((hashedLong << this.p) | (1 << (this.p - 1))) + 1;
++      return registerSet.updateIfGreater((int) idx, runLength);
++    case SPARSE:
++      //Call the sparse encoding scheme which attempts to stuff as much helpful data into 32 bits as possible
++      int k = encodeHash(hashedLong, p, sp);
++      if (tmpSet == null) {
++        tmpSet = new int[sortThreshold + 1];
++      }
++      //Put the encoded data into the temp set
++      tmpSet[tmpIndex++] = k;
++      if (sparseSet != null && sparseSet.length > sparseSetThreshold) {
++        mergeTempList();
++        convertToNormal();
++      } else if (tmpIndex > sortThreshold) {
++        mergeTempList();
++      }
++      return true;
++    }
++    return false;
++  }
++
++  @Override
++  public boolean offerHashed(int hashedInt) {
++    throw new UnsupportedOperationException();
++  }
++
++  /**
++   * Add data to estimator based on the mode it is in
++   *
++   * @param o stream element
++   * @return Will almost always return true for sparse mode because the additions are batched in
++   */
++  @Override
++  public boolean offer(Object o) {
++    long x = MurmurHash.hash64(o);
++    return offerHashed(x);
++  }
++
++  /**
++   * Converts the mode of this estimator from 'sparse' to 'normal'.
++   * <p/>
++   * Each member of the set has its longer 'sparse precision (sp)' length idx
++   * truncated to length p and the associated run length is placed into a register.
++   * Collisions are resolved by merely taking the max.
++   */
++
++  private void convertToNormal() {
++    mergeTempList();
++    this.registerSet = new RegisterSet((int) Math.pow(2, p));
++    for (int k : sparseSet) {
++      int idx = getIndex(k, p);
++      int r = decodeRunLength(k);
++      registerSet.updateIfGreater(idx, r);
++    }
++    format = Format.NORMAL;
++    tmpSet = null;
++    sparseSet = null;
++  }
++
++  /**
++   * Encode the sp length idx and, if necessary, the run length.
++   * <p/>
++   * Start with the 64 bit hash as x.
++   * <p/>
++   * Find all the bits that belong in the first sp (roughly 25) bits. (This is idx')
++   * Get rid of the first p (roughly 18) bits of those. (Those were idx (not prime))
++   * <p/>
++   * If all the remaining bits are zero then we are going to need to find and encode the
++   * full run length of leading zeros, but this only happens once in 2 ^ (sp - p) or roughly
++   * 2 ^ 7 times.
++   * <p/>
++   * If at least one of them is not zero, then since the run length is determined by bits
++   * after p and the idx' contains the first (sp - p) bits of those, then just by putting idx'
++   * in the encoding, we will also be giving it all the information it needs to find the run length.
++   * <p/>
++   * The relationship looks like this:
++   * <p/>
++   * *******************************************************   <- hashed length of bits
++   * | p bits = idx ||     look for leading zeros here     |
++   * |      sp bits = idx'     |
++   * | all 0s? |
++   * <p/>
++   * If we have idx', we could theoretically scan it (as per zeroTest) when unencoding and therefore know whether
++   * to look for the extra run length information. However, for now we have followed the authors of
++   * the paper and put this information in a flag bit at the end of the encoding.
++   * <p/>
++   * Based on this flag, we know whether to adjust for the missing run length bits. We could also
++   * use this flag to compress all the zeros in "| all 0s? |", but saving a byte or so once in 128
++   * times is less than the 120 bits spent on the flag elsewhere. Of course, after compression, the losses
++   * are not quite so large either way.
++   * <p/>
++   * The encoding scheme now looks like:
++   * <p/>
++   * ********************************* <- smaller length of bits (half, but not to scale)
++   * | empty ||       sp bits     ||F|
++   * |  p bits  || has 1 ||0|
++   * <p/>
++   * <p/>
++   * or if the run length was needed (ie 'all 0s?' was indeed all 0s):
++   * <p/>
++   * *********************************
++   * |      sp bits    || run len ||F|
++   * |  p bits ||  0s  |           |1|
++   * <p/>
++   * <p/>
++   * The other notable encoding feature is the inversion of the run length, which just lets the lists
++   * be sorted in a convenient way. (Could alternatively sort in reverse, and use descending deltas).
++   *
++   * @param x  the hash bits
++   * @param p  the 'normal' mode precision
++   * @param sp the 'sparse' mode precision
++   * @return the encoded data as an integer
++   */
++  private int encodeHash(long x, int p, int sp) {
++    //Get the idx' (the first sp bits) by pushing the rest to the right (into oblivion >:D)
++    int idx = (int) (x >>> (64 - sp));
++    //Push to the left for all the spaces you know are between the start of your bits and the left 'wall'
++    //then push p bits off as well so we have just our friend "all 0s?"
++    int zeroTest = 0;
++    if (p < sp) {
++      zeroTest = idx << ((32 - sp) + p);
++    }
++    if (zeroTest == 0) {
++      //See offer
++      final int runLength = Long.numberOfLeadingZeros((x << this.p) | (1 << (this.p - 1))) + 1;
++      //Invert run length by xoring it with a bunch of 1s
++      int invrl = runLength ^ 63;
++      return (((idx
++          << 6)       //push the idx left 6 times to make room to put in the run length
++          | invrl)    //then merge in the run length
++          << 1)       //move left again to make room for the flag bit
++          | 1;        //merge in the flag bit (set to one because we needed the run length)
++    } else {
++      //Just push left once. A zero will appear by default and that's the flag we want.
++      return idx << 1;
++    }
++  }
++
++  /**
++   * More of less the opposite of the encoding function but just for getting out run lengths.
++   *
++   * @param k encoded int
++   * @return run length
++   */
++  private int decodeRunLength(int k) {
++    if ((k & 1) == 1) //checking the flag bit
++    {
++      //Smoosh the flag bit; it has served its purpose
++      //Then & with 63 to delete everything but the run length
++      //Then invert again to undo the inversion from before
++      return ((k >>> 1) & 63) ^ 63;
++    } else {
++      //In one of the encode diagrams there is a substring of bits
++      //labeled 'has 1'. This is where we find that one!
++
++      //First push left to clear out the empty space (that is 31-sp places)
++      //Then push left some more cause bits in precision p don't count for run length
++      //That is, push left p times.
++      //Lastly we add one because we love adding one to run lengths. Its our JAM
++      return Integer.numberOfLeadingZeros(k << p + (31 - sp)) + 1;
++    }
++  }
++
++  /**
++   * returns the hash portion of the of the integer value
++   *
++   * @param k - the encoded integer to extract the hash from
++   * @return the encoded hash value
++   */
++  private int getEncodedHash(int k) {
++    return (k << p + (31 - sp));
++  }
++
++  /**
++   * Get the idx' from an encoding
++   *
++   * @param k encoded data
++   * @return idx'
++   */
++  private static int getSparseIndex(int k) {
++    if ((k & 1) == 1) {
++      return k >>> 7;
++    } else {
++      return k >>> 1;
++    }
++  }
++
++  /**
++   * Gets the idx from an encoding (note this is idx and not idx prime)
++   *
++   * @param k encoded data
++   * @param p 'normal' precision
++   * @return index
++   */
++  private int getIndex(int k, int p) {
++    k = getSparseIndex(k);
++    return (k >>> (sp - p));
++  }
++
++  /**
++   * Gather the cardinality estimate from this estimator.
++   * <p/>
++   * Has two procedures based on current mode. 'Normal' mode works similar to HLL but has some
++   * new bias corrections. 'Sparse' mode is linear counting.
++   *
++   * @return cardinality
++   */
++  @Override
++  public long cardinality() {
++    switch (format) {
++    case NORMAL:
++      double registerSum = 0;
++      int count = registerSet.count;
++      double zeros = 0;
++      for (int j = 0; j < registerSet.count; j++) {
++        int val = registerSet.get(j);
++        registerSum += 1.0 / (1 << val);
++        if (val == 0) {
++          zeros++;
++        }
++      }
++
++      double estimate = alphaMM * (1 / registerSum);
++      double estimatePrime = estimate;
++      if (estimate <= (5 * m)) {
++        estimatePrime = estimate - getEstimateBias(estimate, p);
++      }
++      double H;
++      if (zeros > 0) {
++        H = HyperLogLog.linearCounting(count, zeros);
++      } else {
++        H = estimatePrime;
++      }
++      // when p is large the threshold is just 5*m
++      if ((p <= 18 && H < thresholdData[p - 4]) || (p > 18 && estimate <= 5 * m)) {
++        return Math.round(H);
++      } else {
++        return Math.round(estimatePrime);
++      }
++    case SPARSE:
++      mergeTempList();
++      return Math.round(HyperLogLog.linearCounting(sm, (sm - sparseSet.length)));
++    }
++    return 0;
++  }
++
++  private static double getEstimateBias(double estimate, int p) {
++    // get nearest neighbors for this estimate and precision
++    // above p = 18 there is no bias correction
++    if (p > 18) {
++      return 0;
++    }
++    double[] estimateVector = rawEstimateData[p - 4];
++    SortedMap<Double, Integer> estimateDistances = calcDistances(estimate, estimateVector);
++    int[] nearestNeighbors = getNearestNeighbors(estimateDistances);
++    return getBias(nearestNeighbors, p);
++  }
++
++  private static double getBias(int[] nearestNeighbors, int p) {
++    double[] biasVector = biasData[p - 4];
++    double biasTotal = 0.0d;
++    for (int nearestNeighbor : nearestNeighbors) {
++      biasTotal += biasVector[nearestNeighbor];
++    }
++    return biasTotal / (nearestNeighbors.length);
++  }
++
++  private static int[] getNearestNeighbors(SortedMap<Double, Integer> distanceMap) {
++    int[] nearest = new int[6];
++    int i = 0;
++    for (Integer index : distanceMap.values()) {
++      nearest[i++] = index;
++

<TRUNCATED>


[36/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/BucketAdvisor.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/cache/BucketAdvisor.java
index 98e72bd,0000000..a6649c3
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/BucketAdvisor.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/BucketAdvisor.java
@@@ -1,3002 -1,0 +1,3002 @@@
 +/*
 + * 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;
 +
 +import java.io.DataInput;
 +import java.io.DataOutput;
 +import java.io.IOException;
 +import java.io.ObjectInputStream;
 +import java.io.Serializable;
 +import java.util.AbstractSet;
 +import java.util.Collection;
 +import java.util.HashSet;
 +import java.util.Iterator;
 +import java.util.List;
 +import java.util.Map;
 +import java.util.Queue;
 +import java.util.Random;
 +import java.util.Set;
 +import java.util.concurrent.ConcurrentHashMap;
 +import java.util.concurrent.Semaphore;
 +import java.util.concurrent.TimeUnit;
 +import java.util.concurrent.atomic.AtomicReference;
 +import java.util.concurrent.locks.Lock;
 +import java.util.concurrent.locks.ReadWriteLock;
 +import java.util.concurrent.locks.ReentrantReadWriteLock;
 +
 +import org.apache.logging.log4j.Logger;
 +
 +import com.gemstone.gemfire.CancelException;
 +import com.gemstone.gemfire.DataSerializer;
 +import com.gemstone.gemfire.cache.CacheClosedException;
 +import com.gemstone.gemfire.cache.RegionDestroyedException;
 +import com.gemstone.gemfire.cache.client.internal.locator.SerializationHelper;
 +import com.gemstone.gemfire.cache.partition.PartitionListener;
 +import com.gemstone.gemfire.distributed.DistributedLockService;
 +import com.gemstone.gemfire.distributed.DistributedSystemDisconnectedException;
 +import com.gemstone.gemfire.distributed.LockNotHeldException;
 +import com.gemstone.gemfire.distributed.LockServiceDestroyedException;
 +import com.gemstone.gemfire.distributed.internal.DM;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 +import com.gemstone.gemfire.distributed.internal.MembershipListener;
 +import com.gemstone.gemfire.distributed.internal.ReplyProcessor21;
 +import com.gemstone.gemfire.distributed.internal.locks.DLockService;
 +import com.gemstone.gemfire.distributed.internal.locks.DistributedMemberLock;
 +import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
 +import com.gemstone.gemfire.internal.Assert;
 +import com.gemstone.gemfire.internal.cache.partitioned.Bucket;
 +import com.gemstone.gemfire.internal.cache.partitioned.BucketProfileUpdateMessage;
 +import com.gemstone.gemfire.internal.cache.partitioned.DeposePrimaryBucketMessage;
 +import com.gemstone.gemfire.internal.cache.partitioned.DeposePrimaryBucketMessage.DeposePrimaryBucketResponse;
 +import com.gemstone.gemfire.internal.cache.partitioned.RegionAdvisor;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.logging.LogService;
 +import com.gemstone.gemfire.internal.logging.log4j.LocalizedMessage;
 +import com.gemstone.gemfire.internal.logging.log4j.LogMarker;
 +import com.gemstone.gemfire.internal.util.StopWatch;
 +
 +/**
 + * Specialized {@link CacheDistributionAdvisor} for {@link BucketRegion 
 + * BucketRegions}. The <code>BucketAdvisor</code> is owned by a {@link 
 + * ProxyBucketRegion} and may outlive a <code>BucketRegion</code>.
 + * 
 + * @author Kirk Lund
 + */
 +@SuppressWarnings("synthetic-access")
- public final class BucketAdvisor extends CacheDistributionAdvisor  {
++public class BucketAdvisor extends CacheDistributionAdvisor  {
 +  private static final Logger logger = LogService.getLogger();
 +
 +  public static final boolean ENFORCE_SAFE_CLOSE = false;
 +    //TODO: Boolean.getBoolean("gemfire.BucketAdvisor.debug.enforceSafeClose");
 +  
 +  /** Reference to the InternalDistributedMember that is primary. */
 +  private final AtomicReference primaryMember = new AtomicReference();
 +  
 +  /** 
 +   * Advice requests for {@link #adviseProfileUpdate()} delegate to the 
 +   * partitioned region's <code>RegionAdvisor</code> to include members with 
 +   * {@link ProxyBucketRegion}s as well as real {@link BucketRegion}s. 
 +   */
 +  protected final RegionAdvisor regionAdvisor;
 +
 +  /** 
 +   * The bucket primary will be holding this distributed lock. Protected by
 +   * synchronized(this). 
 +   */
 +  private DistributedMemberLock primaryLock;
 +
 +  //private static final byte MASK_HOSTING       = 1; // 0001 
 +  //private static final byte MASK_VOLUNTEERING  = 2; // 0010
 +  //private static final byte MASK_OTHER_PRIMARY = 4; // 0100
 +  //private static final byte MASK_IS_PRIMARY    = 8; // 1000
 +  
 +  private static final byte NO_PRIMARY_NOT_HOSTING    = 0;  // 0000_0000
 +  private static final byte NO_PRIMARY_HOSTING        = 1;  // 0000_0001
 +  private static final byte OTHER_PRIMARY_NOT_HOSTING = 4;  // 0000_0100
 +  private static final byte OTHER_PRIMARY_HOSTING     = 5;  // 0000_0101
 +  private static final byte VOLUNTEERING_HOSTING      = 3;  // 0000_0011
 +  private static final byte BECOMING_HOSTING          = 15; // 0000_1111
 +  private static final byte IS_PRIMARY_HOSTING        = 9;  // 0000_1001
 +  private static final byte CLOSED                    = 16; // 0001_0000
 +  
 +  /** 
 +   * The current state of this BucketAdvisor which tracks which member is
 +   * primary and whether or not this member is hosting a real Bucket.
 +   */
 +  private byte primaryState = NO_PRIMARY_NOT_HOSTING;
 +  
 +  /**
 +   * This delegate handles all volunteering for primary status. Lazily created.
 +   * Protected by synchronization(this).
 +   */
 +  private VolunteeringDelegate volunteeringDelegate;
 +
 +  /**
 +   * A random number generator
 +   * 
 +   * @see #getPreferredNode()
 +   */
 +  static private final Random myRand = new Random();
 +  
 +  /** 
 +   * Used by {@link #updateRedundancy()} to determine if stat change is
 +   * required. Access and mutation are done while synchronized on this
 +   * advisor.
 +   * 
 +   * @guarded.By this
 +   */
 +  private boolean redundancySatisfied = true;
 +  
 +  /** 
 +   * Used by {@link #incLowRedundancyBucketCount(int)} to determine if 
 +   * redundancy for this bucket has ever been satisfied. Only buckets which 
 +   * lose redundancy after having redundancy will generate a redundancy loss 
 +   * alert. 
 +   * <p>
 +   * Access and mutation are done while synchronized on this advisor.
 +   * 
 +   * @guarded.By this
 +   */
 +  private boolean redundancyEverSatisfied = false; 
 +  
 +  /**
 +   * A read/write lock to prevent making this bucket not primary while a write
 +   * is in progress on the bucket.
 +   */
 +  private final ReadWriteLock primaryMoveLock = new ReentrantReadWriteLock();
 +  private final Lock activeWriteLock = primaryMoveLock.readLock();
 +  private final Lock activePrimaryMoveLock = primaryMoveLock.writeLock();
 +  
 +  /**
 +   * The advisor for the bucket region that we are colocated with, if this region
 +   * is a colocated region.
 +   */
 +  private BucketAdvisor parentAdvisor;
 +
 +  private volatile int redundancy = -1;
 +  
 +  /**
 +   * The member that is responsible for choosing the primary
 +   * for this bucket. While this field is set and this member
 +   * exists, this bucket won't try to become primary.
 +   */
 +  private volatile InternalDistributedMember primaryElector;
 +  
 +  private volatile BucketProfile localProfile;
 +  
 +  private volatile boolean everHadPrimary = false;
 +
 +  private BucketAdvisor startingBucketAdvisor;
 +  
 +  private PartitionedRegion pRegion;
 +
 +  private volatile boolean shadowBucketDestroyed;
 +
 +  /** 
 +   * Constructs a new BucketAdvisor for the Bucket owned by RegionAdvisor.
 +   * 
 +   * @param bucket the bucket to provide metadata and advice for
 +   * @param regionAdvisor advisor for the PartitionedRegion
 +   */
 +  private BucketAdvisor(Bucket bucket, 
 +                       RegionAdvisor regionAdvisor) {
 +    super(bucket);
 +    this.regionAdvisor = regionAdvisor;
 +    this.pRegion = this.regionAdvisor.getPartitionedRegion();
 +    resetParentAdvisor(bucket.getId());
 +  }
 +  
 +  public static BucketAdvisor createBucketAdvisor(Bucket bucket, RegionAdvisor regionAdvisor) {
 +    BucketAdvisor advisor = new BucketAdvisor(bucket, regionAdvisor);
 +    advisor.initialize();
 +    return advisor;
 +  }
 +
 +  // For SQLFabric ALTER TABLE that may change colocation
 +  public void resetParentAdvisor(int bucketId) {
 +    PartitionedRegion colocatedRegion = ColocationHelper
 +        .getColocatedRegion(this.pRegion);
 +    if (colocatedRegion != null) {
 +      if (colocatedRegion.isFixedPartitionedRegion()) {
 +        List<FixedPartitionAttributesImpl> fpas = colocatedRegion
 +            .getFixedPartitionAttributesImpl();
 +        if (fpas != null) {
 +          for (FixedPartitionAttributesImpl fpa : fpas) {
 +            if (fpa.hasBucket(bucketId)) {
 +              this.parentAdvisor = colocatedRegion.getRegionAdvisor()
 +                  .getBucketAdvisor(fpa.getStartingBucketID());
 +              break;
 +            }
 +          }
 +        }
 +      }
 +      else {
 +        this.parentAdvisor = colocatedRegion.getRegionAdvisor()
 +            .getBucketAdvisor(bucketId);
 +      }
 +    }
 +    else {
 +      this.parentAdvisor = null;
 +    }
 +  }
 +
 +  private void assignStartingBucketAdvisor() {
 +    if (this.pRegion.isFixedPartitionedRegion()) {
 +      List<FixedPartitionAttributesImpl> fpas = this.pRegion
 +          .getFixedPartitionAttributesImpl();
 +      if (fpas != null) {
 +        int bucketId = getBucket().getId();
 +        for (FixedPartitionAttributesImpl fpa : fpas) {
 +          if (fpa.hasBucket(bucketId) && bucketId != fpa.getStartingBucketID()) {
 +            startingBucketAdvisor = this.regionAdvisor.getBucketAdvisor(
 +                fpa.getStartingBucketID());
 +            break;
 +          }
 +        }
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Returns the lock that prevents the primary from moving while active writes
 +   * are in progress. This should be locked before checking if the local bucket
 +   * is primary.
 +   *  
 +   * @return the lock for in-progress write operations
 +   */
 +  public Lock getActiveWriteLock() {
 +    return this.activeWriteLock;
 +  }
 +  
 +  /**
 +   * Returns the lock that prevents the parent's primary from moving while
 +   * active writes are in progress. This should be locked before checking if the
 +   * local bucket is primary.
 +   * 
 +   * @return the lock for in-progress write operations
 +   */  
 +  Lock getParentActiveWriteLock() {
 +    if (this.parentAdvisor != null) {
 +      return this.parentAdvisor.getActiveWriteLock();
 +    }
 +    return null;
 +  }
 +
 +  /**
 +   * Try to lock the primary bucket to make sure no operation is on-going at
 +   * current bucket.  
 +   * 
 +   */
 +  public void tryLockIfPrimary() {
 +    if (isPrimary()) {
 +      try {
 +        this.activePrimaryMoveLock.lock();
 +      } finally {
 +        this.activePrimaryMoveLock.unlock();
 +      }
 +    }
 +  }
 +  
 +  /**
 +   * Makes this <code>BucketAdvisor</code> give up being a primary and become
 +   * a secondary. Does nothing if not currently the primary.
 +   * 
 +   * @return true if this advisor has been deposed as primary
 +   */
 +  public boolean deposePrimary() {
 +    if (isPrimary()) {
 +      this.activePrimaryMoveLock.lock();
 +      boolean needToSendProfileUpdate = false;
 +      try {
 +        removePrimary(getDistributionManager().getId());
 +        synchronized(this) {
 +          if (!isPrimary()) {
 +            //releasePrimaryLock();
 +            needToSendProfileUpdate = true;
 +            return true;
 +          } else {
 +            return false; // failed for some reason
 +          }
 +        }
 +      } finally {
 +        this.activePrimaryMoveLock.unlock();
 +        if (needToSendProfileUpdate) {
 +          if (this.getBucket() instanceof BucketRegionQueue) {
 +            BucketRegionQueue brq = (BucketRegionQueue)this.getBucket();
 +            brq.decQueueSize(brq.size());
 +          }
 +          sendProfileUpdate();
 +        }
 +      }
 +    } else {
 +      sendProfileUpdate();
 +      return true;
 +    }
 +  }
 +  
 +  /**
 +   * This calls deposePrimary on every colocated child that is directly
 +   * colocated to this bucket's PR. Those each in turn do the same to their
 +   * child buckets and so on before returning. Each depose will send a dlock
 +   * release message to the grantor, wait for reply, and then also send a
 +   * profile update.
 +   * <p>
 +   * Caller must synchronize on this BucketAdvisor.
 +
 +   * @return true if children were all deposed as primaries
 +   * @guarded.By this
 +   */
 +  private boolean deposePrimaryForColocatedChildren() {
 +    boolean deposedChildPrimaries = true;
 +    
 +    // getColocatedChildRegions returns only the child PRs directly colocated
 +    // with thisPR...
 +    List<PartitionedRegion> colocatedChildPRs = 
 +        ColocationHelper.getColocatedChildRegions(this.pRegion);
 +    if (colocatedChildPRs != null) {
 +      for (PartitionedRegion pr : colocatedChildPRs) {
 +        Bucket b = pr.getRegionAdvisor().getBucket(getBucket().getId());
 +        if (b != null) {
 +          BucketAdvisor ba = b.getBucketAdvisor();
 +          deposedChildPrimaries = ba.deposePrimary() && deposedChildPrimaries;
 +        }
 +      }
 +    }    
 +    return deposedChildPrimaries;
 +  }
 +
 +  private boolean deposeOtherPrimaryBucketForFixedPartition() {
 +    boolean deposedOtherPrimaries = true;
 +    int bucketId = getBucket().getId();
 +    List<FixedPartitionAttributesImpl> fpas = this.pRegion
 +        .getFixedPartitionAttributesImpl();
 +    if (fpas != null) {
 +      for (FixedPartitionAttributesImpl fpa : fpas) {
 +        if (fpa.getStartingBucketID() == bucketId) {
 +          for (int i = (bucketId+1); i <= fpa.getLastBucketID(); i++) {
 +            Bucket b = regionAdvisor.getBucket(i);
 +            if (b != null) {
 +              BucketAdvisor ba = b.getBucketAdvisor();
 +              deposedOtherPrimaries = ba.deposePrimary()
 +                  && deposedOtherPrimaries;
 +            }
 +          }
 +        }
 +        else {
 +          continue;
 +        }
 +      }
 +    }
 +    return deposedOtherPrimaries;
 +  }
 +  
 +  void removeBucket() {
 +    setHosting(false);
 +  }
 +  
 +  /**
 +   * Increment or decrement lowRedundancyBucketCount stat and generate 
 +   * alert only once per loss of redundancy for PR but only if redundancy
 +   * has ever been satisfied.
 +   * <p>
 +   * Caller must synchronize on this BucketAdvisor.
 +   * 
 +   * @param val the value to increment or decrement by
 +   * @guarded.By this
 +   */
 +  private void incLowRedundancyBucketCount(int val) {
 +    final int HAS_LOW_REDUNDANCY = 0;
 +    final int ALREADY_GENERATED_WARNING = 1;
 +    
 +    final PartitionedRegionStats stats = getPartitionedRegionStats();
 +    final boolean[] lowRedundancyFlags = 
 +        this.regionAdvisor.getLowRedundancyFlags();
 +    final int configuredRedundancy =
 +        this.pRegion.getRedundantCopies();
 +    
 +    synchronized (lowRedundancyFlags) {
 +      stats.incLowRedundancyBucketCount(val);
 +      
 +      if (stats.getLowRedundancyBucketCount() == 0) {
 +        // all buckets are fully redundant
 +        lowRedundancyFlags[HAS_LOW_REDUNDANCY] = false; // reset
 +        lowRedundancyFlags[ALREADY_GENERATED_WARNING] = false; // reset
 +        stats.setActualRedundantCopies(configuredRedundancy);
 +      }
 +      
 +      else {
 +        // one or more buckets are not fully redundant
 +        int numBucketHosts = getBucketRedundancy() + 1;
 +        int actualRedundancy = Math.max(numBucketHosts - 1, 0); // zero or more
 +        
 +        if (actualRedundancy < stats.getActualRedundantCopies()) {
 +          // need to generate an alert for this lower actual redundancy
 +          lowRedundancyFlags[ALREADY_GENERATED_WARNING] = false;
 +        }
 +        
 +        if (!lowRedundancyFlags[HAS_LOW_REDUNDANCY] || 
 +            !lowRedundancyFlags[ALREADY_GENERATED_WARNING]) {
 +          // either we have lower redundancy or we never generated an alert
 +          
 +          lowRedundancyFlags[HAS_LOW_REDUNDANCY] = true;
 +          stats.setActualRedundantCopies(actualRedundancy);
 +          
 +          // this bucket will only generate alert if redundancyEverSatisfied
 +          if (!lowRedundancyFlags[ALREADY_GENERATED_WARNING] && 
 +              this.redundancyEverSatisfied) {
 +            
 +            lowRedundancyFlags[ALREADY_GENERATED_WARNING] = true;
 +            logger.warn(LocalizedMessage.create(
 +                LocalizedStrings.BucketAdvisor_REDUNDANCY_HAS_DROPPED_BELOW_0_CONFIGURED_COPIES_TO_1_ACTUAL_COPIES_FOR_2,
 +                new Object[] { Integer.valueOf(configuredRedundancy), Integer.valueOf(actualRedundancy), this.pRegion.getFullPath()}));
 +          }
 +        }
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Return (and possibly choose) a thread-sticky member from whose data store
 +   * this bucket's values should be read
 +   * @return member to use for reads, null if none available
 +   */
 +  public InternalDistributedMember getPreferredNode() {
 +
 +    if (isHosting()) {
 +      getPartitionedRegionStats().incPreferredReadLocal();
 +      return getDistributionManager().getId();      
 +    }
 +    
 +    Profile locProfiles[] = this.profiles; // volatile read
 +    if (locProfiles.length == 0) {
 +      return null;
 +    }
 +    getPartitionedRegionStats().incPreferredReadRemote();
 +  
 +    if (locProfiles.length == 1) { // only one choice!
 +      return locProfiles[0].peerMemberId; 
 +    }
 +    
 +    // Pick one at random.
 +    int i = myRand.nextInt(locProfiles.length);
 +    return locProfiles[i].peerMemberId;
 +    }
 +  
 +  /**
 +   * Returns the thread-safe queue of primary volunteering tasks for the
 +   * parent Partitioned Region.
 +   * 
 +   * @return the queue of primary volunteering tasks
 +   */
 +  Queue getVolunteeringQueue() {
 +    return this.regionAdvisor.getVolunteeringQueue();
 +  }
 +  
 +  /**
 +   * Returns the semaphore which controls the number of threads allowed to
 +   * consume from the {@link #getVolunteeringQueue volunteering queue}.
 +   * 
 +   * @return the semaphore which controls the number of volunteering threads
 +   */
 +  Semaphore getVolunteeringSemaphore() {
 +    return this.regionAdvisor.getVolunteeringSemaphore();
 +  }
 +  
 +  /** 
 +   * Returns the PartitionedRegionStats.
 +   * 
 +   * @return the PartitionedRegionStats
 +   */ 
 +  PartitionedRegionStats getPartitionedRegionStats() {
 +    return this.regionAdvisor.getPartitionedRegionStats();
 +  }
 +  
 +  /**
 +   * Concurrency: protected by synchronizing on *this*
 +   */
 +  @Override
 +  protected void profileCreated(Profile profile) {
 +    this.regionAdvisor.incrementBucketCount(profile);
 +    super.profileCreated(profile);
 +    if (updateRedundancy() > 0) {
 +      // wake up any threads in waitForRedundancy or waitForPrimary
 +      this.notifyAll(); 
 +    }
 +    this.regionAdvisor.updateBucketStatus(this.getBucket().getId(),
 +        profile.peerMemberId, false);
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("Profile added {} Profile : {}", getBucket().getFullPath(), profile);
 +    }
 +    synchronized (this){
 +      updateServerBucketProfile();
 +    }
 +  }
 +
 +  /**
 +   * Concurrency: protected by synchronizing on *this*
 +   */
 +  @Override
 +  protected void profileUpdated(Profile profile) {
 +    super.profileUpdated(profile);
 +    if (updateRedundancy() > 0) {
 +      // wake up any threads in waitForRedundancy or waitForPrimary
 +      this.notifyAll(); 
 +    }
 +    this.regionAdvisor.updateBucketStatus(this.getBucket().getId(), 
 +        profile.peerMemberId, false);
 +    
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("Profile updated {} Profile : {}", getBucket().getFullPath(), profile);
 +    }
 +    synchronized (this) {
 +      updateServerBucketProfile();
 +    }
 +  }
 +
 +  /**
 +   * Concurrency: protected by synchronizing on *this*
 +   */
 +  @Override
 +  protected void profileRemoved(Profile profile) {
 +    if (profile != null) {
 +      this.regionAdvisor.updateBucketStatus(this.getBucket().getId(), 
 +          profile.getDistributedMember(), true);
 +      this.regionAdvisor.decrementsBucketCount(profile);
 +    }
 +    updateRedundancy();
 +    
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("Profile removed {} the member lost {} Profile : {}", getBucket().getFullPath(),
 +          profile.getDistributedMember(), profile);
 +    }
 +    synchronized (this) {
 +      updateServerBucketProfile();
 +    }
 +  }
 +
 +  @Override
 +  public boolean shouldSyncForCrashedMember(InternalDistributedMember id) {
 +    BucketProfile profile = (BucketProfile)getProfile(id);
 +    return (profile != null) && (profile.isPrimary);
 +  }
 +  
 +  @Override
 +  public DistributedRegion getRegionForDeltaGII() {
 +    DistributedRegion result = super.getRegionForDeltaGII();
 +    if (result == null && getAdvisee() instanceof ProxyBucketRegion) {
 +      result = ((ProxyBucketRegion)getAdvisee()).getHostedBucketRegion();
 +    }
 +    return result;
 +  }
 +
 +
 +
 +  
 +  /**
 +   * Called by the RegionAdvisor.profileRemoved, this method
 +   * tests to see if the missing member is the primary elector
 +   * for this bucket.
 +   * 
 +   * We can't call this method from BucketAdvisor.profileRemoved,
 +   * because the primaryElector may not actually host the bucket.
 +   * @param profile
 +   */
 +  public void checkForLostPrimaryElector(Profile profile) {
 +    //If the member that went away was in the middle of creating
 +    //the bucket, finish the bucket creation.
 +    if(this.primaryElector != null && this.primaryElector.equals(profile.getDistributedMember())) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("Bucket {} lost the member responsible for electing the primary. Finishing bucket creation", getBucket().getFullPath());
 +      }
 +      this.primaryElector = getBucket().getDistributionManager().getId();
 +      this.getBucket().getDistributionManager().getWaitingThreadPool().execute(new Runnable() {
 +        public void run() {
 +          getBucket().getPartitionedRegion().getRedundancyProvider().finishIncompleteBucketCreation(getBucket().getId());
 +        }
 +      });
 +    }
 +  }
 +
 +  /**
 +   * Only allows profiles that actually hosting this bucket. If the profile is
 +   * primary, then primaryMember will be set to that member but only
 +   * if we are not already the primary.
 +   * 
 +   * @param profile the profile to add (must be a BucketProfile)
 +   * @param forceProfile true will force profile to be added even if member is
 +   * not in distributed view
 +   * 
 +   * @see #adviseProfileUpdate() 
 +   */
 +  @Override
 +  public boolean putProfile(Profile profile, boolean forceProfile) {
 +    assert profile instanceof BucketProfile;
 +    BucketProfile bp = (BucketProfile) profile;
 +
 +    // Only hosting buckets will be initializing, the isInitializing boolean is to
 +    // allow for early entry into the advisor for GII purposes
 +    if ( ! bp.isHosting && ! bp.isInitializing) {
 +      if (logger.isTraceEnabled(LogMarker.DA)) {
 +        logger.trace(LogMarker.DA, "BucketAdvisor#putProfile early out");
 +      }
 +      return false;  // Do not allow introduction of proxy profiles, they don't provide anything useful
 +      // isHosting = false, isInitializing = false
 +    }
 +    if (logger.isTraceEnabled(LogMarker.DA)) {
 +      logger.trace(LogMarker.DA, "BucketAdvisor#putProfile profile=<{}> force={}; profile = {}", profile, forceProfile, bp);
 +    }
 +    // isHosting = false, isInitializing = true
 +    // isHosting = true,  isInitializing = false
 +    // isHosting = true,  isInitializing = true... (false state)
 +    
 +    final boolean applied;
 +    synchronized(this) {
 +      // force new membership version in the advisor so that the
 +      // state flush mechanism can capture any updates to the bucket
 +      // MIN_VALUE is intended as a somewhat unique value for potential debug purposes
 +      profile.initialMembershipVersion = Long.MIN_VALUE;
 +      applied = super.putProfile(profile, forceProfile);
 +      // skip following block if isPrimary to avoid race where we process late
 +      // arriving OTHER_PRIMARY profile after we've already become primary
 +      if (applied && !isPrimary()) {  // TODO is it safe to change the bucket state if the profile was not applied?  -- mthomas 2/13/08
 +        if (bp.isPrimary) {
 +          setPrimaryMember(bp.getDistributedMember());
 +        } else {
 +          notPrimary(bp.getDistributedMember());
 +        }
 +      } // if: !isPrimary
 +      
 +    } // synchronized
 +    return applied;
 +  }
 +
 +  private static <E> Set<E> newSetFromMap(Map<E, Boolean> map) {
 +    if (map.isEmpty()) {
 +      return new SetFromMap<E>(map);
 +    }
 +    throw new IllegalArgumentException();
 +  }
 +
 +  private static class SetFromMap<E> extends AbstractSet<E> implements
 +      Serializable {
 +    private static final long serialVersionUID = 2454657854757543876L;
 +
 +    // must named as it, to pass serialization compatibility test.
 +    private Map<E, Boolean> m;
 +
 +    private transient Set<E> backingSet;
 +
 +    SetFromMap(final Map<E, Boolean> map) {
 +      super();
 +      m = map;
 +      backingSet = map.keySet();
 +    }
 +
 +    @Override
 +    public boolean equals(Object object) {
 +      return backingSet.equals(object);
 +    }
 +
 +    @Override
 +    public int hashCode() {
 +      return backingSet.hashCode();
 +    }
 +
 +    @Override
 +    public boolean add(E object) {
 +      return m.put(object, Boolean.TRUE) == null;
 +    }
 +
 +    @Override
 +    public void clear() {
 +      m.clear();
 +    }
 +
 +    @Override
 +    public String toString() {
 +      return backingSet.toString();
 +    }
 +
 +    @Override
 +    public boolean contains(Object object) {
 +      return backingSet.contains(object);
 +    }
 +
 +    @Override
 +    public boolean containsAll(Collection<?> collection) {
 +      return backingSet.containsAll(collection);
 +    }
 +
 +    @Override
 +    public boolean isEmpty() {
 +      return m.isEmpty();
 +    }
 +
 +    @Override
 +    public boolean remove(Object object) {
 +      return m.remove(object) != null;
 +    }
 +
 +    @Override
 +    public boolean retainAll(Collection<?> collection) {
 +      return backingSet.retainAll(collection);
 +    }
 +
 +    @Override
 +    public Object[] toArray() {
 +      return backingSet.toArray();
 +    }
 +
 +    @Override
 +    public <T> T[] toArray(T[] contents) {
 +      return backingSet.toArray(contents);
 +    }
 +
 +    @Override
 +    public Iterator<E> iterator() {
 +      return backingSet.iterator();
 +    }
 +
 +    @Override
 +    public int size() {
 +      return m.size();
 +    }
 +
 +    @SuppressWarnings("unchecked")
 +    private void readObject(ObjectInputStream stream) throws IOException,
 +        ClassNotFoundException {
 +      stream.defaultReadObject();
 +      backingSet = m.keySet();
 +    }
 +  }
 +
 +  private void updateServerBucketProfile() {
 +    // check to see if it is clientBucketProfile add it to the RegionAdvisor
 +    // data structure which maintains the newly created clientBucketProfile
 +    // don't add more than once, but replace existing profile
 +    int bucketId = this.getBucket().getId();
 +    Set<ServerBucketProfile> serverProfiles = this.regionAdvisor
 +        .getClientBucketProfiles(bucketId);
 +    if (serverProfiles == null) {
 +      serverProfiles = newSetFromMap(new ConcurrentHashMap<ServerBucketProfile, Boolean>());
 +      this.regionAdvisor.setClientBucketProfiles(bucketId, serverProfiles);
 +    }
 +    serverProfiles.clear();
 +    for (Profile p : this.profiles) {
 +      if (p instanceof ServerBucketProfile) {
 +        serverProfiles.add((ServerBucketProfile)p);
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Only for local profile.
 +   * @param p
 +   */
 +  public synchronized void updateServerBucketProfile(BucketProfile p) {
 +    this.localProfile = p;
 +  }
 +
 +  public BucketProfile getLocalProfile() {
 +    return this.localProfile;
 +  }
 +  
 +  @Override
 +  public boolean removeId(ProfileId memberId, 
 +                          boolean crashed,
 +                          boolean destroyed,
 +                          boolean fromMembershipListener) {
 +    boolean hadBucketRegion = super.removeId(memberId, crashed, destroyed, fromMembershipListener);
 +    if (hadBucketRegion) {
 +      // do NOT call notPrimary under synchronization
 +      try {
 +        notPrimary((InternalDistributedMember)memberId);
 +      } catch (CancelException e) {
 +        // must be closing the cache - no need to try to become primary
 +      }
 +    }
 +    return hadBucketRegion;
 +  }
 +
 +  /**
 +   * Removes the profile for the specified member. If that profile is marked
 +   * as primary, this will call {@link #notPrimary(InternalDistributedMember)}.
 +   * 
 +   * @param memberId the member to remove the profile for
 +   * @param serialNum specific serial number to remove
 +   * @return true if a matching profile for the member was found
 +   */
 +  @Override
 +  public boolean removeIdWithSerial(InternalDistributedMember memberId,
 +                                    int serialNum, boolean regionDestroyed) {
 +    boolean hadBucketRegion = super.removeIdWithSerial(memberId, serialNum, regionDestroyed);
 +    if (hadBucketRegion) {
 +      // do NOT call notPrimary under synchronization
 +      notPrimary(memberId);
 +    }
 +    forceNewMembershipVersion();
 +    return hadBucketRegion;
 +  }
 +
 +  @Override
 +  public Set adviseProfileExchange() {
 +    // delegate up to RegionAdvisor to include members that might have 
 +    // ProxyBucketRegion without a real BucketRegion
 +    Assert.assertTrue(this.regionAdvisor.isInitialized());
 +    return this.regionAdvisor.adviseBucketProfileExchange();
 +  }
 +
 +  @Override
 +  public Set adviseProfileUpdate() {
 +    // delegate up to RegionAdvisor to include members that might have 
 +    // ProxyBucketRegion without a real BucketRegion
 +    return this.regionAdvisor.adviseGeneric();
 +  }
 +  
 +  /**
 +   * Sets hosting to false and returns without closing. Calling closeAdvisor
 +   * will actually close this advisor.
 +   */
 +  @Override
 +  public void close() {
 +    // ok, so BucketRegion extends DistributedRegion which calls close on the
 +    // advisor, BUT the ProxyBucketRegion truly owns the advisor and only it
 +    // should be able to close the advisor...
 +    //
 +    // if a BucketRegion closes, it will call this method and we want to change
 +    // our state to NOT_HOSTING
 +    //
 +    // see this.closeAdvisor()
 +    setHosting(false);
 +  }
 +  
 +  /** 
 +   * Blocks until there is a known primary and return that member, but only
 +   * if there are real bucket regions that exist. If there are no real
 +   * bucket regions within the distribution config's member-timeout setting
 +   * * 3 (time required to eject a member) + 15000, then 
 +   * this returns null.
 +   *
 +   * kbanks: reworked this method to avoid JIT issue #40639 
 +   *
 +   * @return the member who is primary for this bucket
 +   */
 +  public final InternalDistributedMember getPrimary() {
 +    InternalDistributedMember primary = getExistingPrimary();
 +    if(primary == null){
 +        primary = waitForNewPrimary();
 +    }
 +    return primary;
 +  }
 +
 +  /** 
 +   * This method was split out from getPrimary() due to bug #40639
 +   * and is only intended to be called from within that method.
 +   * @see #getPrimary()
 +   * @return the existing primary (if it is still in the view) otherwise null
 +   */
 +  private final InternalDistributedMember getExistingPrimary() {
 +    return basicGetPrimaryMember();
 +  }
 +  
 +  /**
 +   * If the current member is primary for this bucket return true, otherwise, 
 +   * give some time for the current member to become primary and
 +   * then return whether it is a primary (true/false).
 +   */
 +  public final boolean isPrimaryWithWait() {
 +    if (this.isPrimary()) {
 +      return true;
 +    }
 +    // wait for the current member to become primary holder
 +    InternalDistributedMember primary = waitForNewPrimary(); 
 +    if(primary != null) {
 +        return true;
 +    }
 +    return false;
 +  }
 +
 +  /** 
 +   * This method was split out from getPrimary() due to bug #40639
 +   * and is only intended to be called from within that method.
 +   * @see #getPrimary()
 +   * @return the new primary
 +   */
 +  private final InternalDistributedMember waitForNewPrimary() {
 +    DM dm = this.regionAdvisor.getDistributionManager();
 +    DistributionConfig config = dm.getConfig();
 +    // failure detection period
 +    long timeout = config.getMemberTimeout() * 3;
 +    // plus time for a new member to become primary
 +    timeout += Long.getLong("gemfire.BucketAdvisor.getPrimaryTimeout",
 +                            15 * 1000);
 +    InternalDistributedMember newPrimary = waitForPrimaryMember(timeout);
 +    return newPrimary;
 +  }
 +  
 +   /** 
 +   * Marks member as not primary. Initiates volunteerForPrimary if this
 +   * member is hosting a real bucket. This method does nothing
 +   * if the member parameter is the current member.
 +   * 
 +   * @param member the member who is not primary
 +   */
 +  public void notPrimary(InternalDistributedMember member) {
 +    //Fix for 43569. Only the deposePrimary call should
 +    //make the local member drop the primary lock.
 +    if(!member.equals(getDistributionManager().getId())) {
 +      removePrimary(member);
 +    }
 +  }
 + 
 +  /** 
 +   * Marks member as not primary. Initiates volunteerForPrimary if this
 +   * member is hosting a real bucket.
 +   * 
 +   * @param member the member who is not primary
 +   */
 +  public void removePrimary(InternalDistributedMember member) {
 +    boolean needToVolunteerForPrimary = false;
 +    if (!isClosed()) { // hole: requestPrimaryState not hosting
 +      initializationGate();
 +    }
 +    boolean lostPrimary = false;
 +    try {
 +      synchronized(this) {
 +        boolean wasPrimary = isPrimary() && this.getDistributionManager().getId().equals(member);
 +        final InternalDistributedMember currentPrimary = (InternalDistributedMember) this.primaryMember.get();
 +        if (currentPrimary != null && currentPrimary.equals(member)) {
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("[BucketAdvisor.notPrimary] {} for {}", member, this);
 +          }
 +          this.primaryMember.set(null);
 +        } 
 +        else {
 +          return;
 +        }
 +        
 +        if (isClosed()) { 
 +          // possibly closed if caller comes from outside this advisor
 +          return; // return quietly
 +        }
 +        // member is primary... need to change state to NO_PRIMARY_xxx
 +        if (isHosting()) {
 +          requestPrimaryState(NO_PRIMARY_HOSTING);
 +          if (this.pRegion.isFixedPartitionedRegion()) {
 +            InternalDistributedMember primaryMember = this.regionAdvisor
 +                .adviseFixedPrimaryPartitionDataStore(this.getBucket().getId());
 +            if (primaryMember == null || primaryMember.equals(member)) {
 +              needToVolunteerForPrimary = true;
 +            }
 +            else {
 +              needToVolunteerForPrimary = false;
 +            }
 +          }
 +          else {
 +            needToVolunteerForPrimary = true;
 +          }
 +        }
 +        else {
 +          requestPrimaryState(NO_PRIMARY_NOT_HOSTING);
 +        }
 +        if(wasPrimary) {
 +          lostPrimary = true;
 +        }
 +
 +        // try to fix yet another cause of bug 36881
 +        findAndSetPrimaryMember();
 +      }
 +    } finally {
 +      if (lostPrimary) {
 +        Bucket br = this.regionAdvisor.getBucket(getBucket().getId());
 +        if( br != null && br instanceof BucketRegion) {
 +          ((BucketRegion)br).beforeReleasingPrimaryLockDuringDemotion();
 +        }
 +
 +        releasePrimaryLock();
 +        // this was a deposePrimary call so we need to depose children as well
 +        deposePrimaryForColocatedChildren();
 +        if (this.pRegion.isFixedPartitionedRegion()) {
 +          deposeOtherPrimaryBucketForFixedPartition();
 +        }
 +      }
 +    }
 +    
 +    if (needToVolunteerForPrimary) {
 +      volunteerForPrimary();
 +    }
 +  }
 +  
 +  /** 
 +   * Returns the ProxyBucketRegion which owns this advisor.
 +   * 
 +   * @return the ProxyBucketRegion which owns this advisor
 +   */
 +  public ProxyBucketRegion getProxyBucketRegion() {
 +    return (ProxyBucketRegion)getAdvisee();
 +  }
 +
 +  /** 
 +   * Actually close this advisor for real. Called by ProxyBucketRegion only. 
 +   * Calling this method actually closes this advisor whereas {@link #close()}
 +   * only sets hosting to false.
 +   */
 +  protected void closeAdvisor() {
 +    boolean wasPrimary;
 +    synchronized(this) {
 +      if (isClosed()) {
 +        return;
 +      }
 +      wasPrimary = isPrimary();
 +      super.close();
 +      this.requestPrimaryState(CLOSED);
 +      if (!this.redundancySatisfied){
 +        incLowRedundancyBucketCount(-1);
 +        this.redundancySatisfied = true;
 +      }
 +      this.localProfile = null;
 +    }
 +    if(wasPrimary) {
 +      releasePrimaryLock();
 +    }
 +  }
 +  
 +  /** 
 +   * Returns true if this advisor has been closed.
 +   * 
 +   * @return true if this advisor has been closed
 +   */
 +  protected boolean isClosed() {
 +    synchronized(this) {
 +      return this.primaryState == CLOSED;
 +    }
 +  }
 +
 +  /** 
 +   * Returns true if this member is currently marked as primary.
 +   * 
 +   * @return true if this member is currently marked as primary
 +   */
 +  public boolean isPrimary() {
 +    synchronized(this) {
 +      return this.primaryState == IS_PRIMARY_HOSTING;
 +    }
 +  }
 +  
 +  /** 
 +   * Returns true if this member is currently volunteering for primary. 
 +   * 
 +   * @return true if this member is currently volunteering for primary
 +   */
 +  protected boolean isVolunteering() {
 +    synchronized(this) {
 +      return this.primaryState == VOLUNTEERING_HOSTING;
 +    }
 +  }
 +
 +  /** 
 +   * Returns true if this member is currently attempting to become primary. 
 +   * 
 +   * @return true if this member is currently attempting to become primary
 +   */
 +  protected boolean isBecomingPrimary() {
 +    synchronized(this) {
 +      return this.primaryState == BECOMING_HOSTING && 
 +             this.volunteeringDelegate != null &&
 +             this.volunteeringDelegate.isAggressive();
 +    }
 +  }
 +
 +  /** 
 +   * Returns true if this member is currently hosting real bucket.
 +   * 
 +   * @return true if this member is currently hosting real bucket
 +   */
 +  public boolean isHosting() {
 +    synchronized(this) {
 +      return this.primaryState == NO_PRIMARY_HOSTING ||
 +             this.primaryState == OTHER_PRIMARY_HOSTING ||
 +             this.primaryState == VOLUNTEERING_HOSTING ||
 +             this.primaryState == BECOMING_HOSTING ||
 +             this.primaryState == IS_PRIMARY_HOSTING;
 +    }
 +  }
 +  
 +  /**
 +   * Attempt to acquire lock for primary until a primary exists. Caller hands
 +   * off responsibility to an executor (waiting pool) and returns early.
 +   */
 +  public void volunteerForPrimary() {
 +    if (primaryElector != null) {
 +      return;
 +    }
 +    initializationGate();
 +
 +    synchronized (this) {
 +      if (isVolunteering() || isClosed() || !isHosting()) {
 +        // only one thread should be attempting to volunteer at one time
 +        return;
 +      }
 +      // if member is still not initialized then don't volunteer for primary
 +      final GemFireCacheImpl cache = (GemFireCacheImpl)getBucket().getCache();
 +      if (!cache.doVolunteerForPrimary(this)) {
 +        return;
 +      }
 +      if (this.volunteeringDelegate == null) {
 +        this.volunteeringDelegate = new VolunteeringDelegate();
 +      }
 +      this.volunteeringDelegate.volunteerForPrimary();
 +    }
 +  }
 +
 +  /**
 +   * Makes this <code>BucketAdvisor</code> become the primary if it is already
 +   * a secondary.
 +   * 
 +   * @param isRebalance true if directed to become primary by rebalancing
 +   * @return true if this advisor succeeds in becoming the primary
 +   */
 +  public boolean becomePrimary(boolean isRebalance) {
 +    initializationGate();
 +
 +    long startTime 
 +        = getPartitionedRegionStats().startPrimaryTransfer(isRebalance);
 +    try {
 +      long waitTime = 2000; // time each iteration will wait
 +      while (!isPrimary()) {
 +        this.getAdvisee().getCancelCriterion().checkCancelInProgress(null);
 +        boolean attemptToBecomePrimary = false;
 +        boolean attemptToDeposePrimary = false;
 +        
 +        if (Thread.currentThread().isInterrupted()) {
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("Breaking from becomePrimary loop due to thread interrupt flag being set");
 +          }
 +          break;
 +        }
 +        if (isClosed() || !isHosting()) {
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("Breaking from becomePrimary loop because {} is closed or not hosting", this);
 +          }
 +          break;
 +        }
 +        
 +        VolunteeringDelegate vDelegate = null;
 +        synchronized (this) {
 +          if (isVolunteering()) {
 +            // standard volunteering attempt already in progress...
 +            if (logger.isDebugEnabled()) {
 +              logger.debug("Waiting for volunteering thread {}. Time left: {} ms", this, waitTime);
 +            }
 +            this.wait(waitTime); // spurious wakeup ok
 +            continue;
 +            
 +          } else if (isBecomingPrimary()) {
 +            // reattempt to depose otherPrimary...
 +            attemptToDeposePrimary = true;
 +            
 +          } else {
 +              // invoke becomePrimary AFTER sync is released in this thread...
 +              vDelegate = this.volunteeringDelegate;
 +              if (vDelegate == null) {
 +                vDelegate = new VolunteeringDelegate();
 +                this.volunteeringDelegate = vDelegate;
 +              }
 +          } // else
 +        } // synchronized
 +        
 +        if (vDelegate != null) {
 +          // Use the snapshot 'vDelegate' instead of 'this.volunteeringDelegate' since we are not synced here.
 +          attemptToBecomePrimary = vDelegate.reserveForBecomePrimary(); // no sync! 
 +        }
 +          
 +        // release synchronization and then call becomePrimary
 +        if (attemptToBecomePrimary) {
 +          synchronized (this) {
 +            if (this.volunteeringDelegate == null) {
 +              this.volunteeringDelegate = new VolunteeringDelegate();
 +            }
 +            this.volunteeringDelegate.volunteerForPrimary();
 +            attemptToDeposePrimary = true;
 +          } // synchronized
 +          Thread.sleep(10);
 +        } // attemptToBecomePrimary
 +          
 +        // RACE: slight race condition with thread that's actually requesting the lock
 +
 +        if (attemptToDeposePrimary) {
 +          InternalDistributedMember otherPrimary = getPrimary();
 +          if (otherPrimary != null && 
 +              !getDistributionManager().getId().equals(otherPrimary)) {
 +            if (logger.isDebugEnabled()) {
 +              logger.debug("Attempting to depose primary on {} for {}", otherPrimary, this);
 +            }
 +            DeposePrimaryBucketResponse response = 
 +                DeposePrimaryBucketMessage.send(
 +                    otherPrimary, 
 +                    this.pRegion, 
 +                    getBucket().getId());
 +            if (response != null) {
 +              response.waitForRepliesUninterruptibly();
 +              if (logger.isDebugEnabled()) {
 +                logger.debug("Deposed primary on {}", otherPrimary);
 +              }
 +            }
 +          }
 +          Thread.sleep(10);
 +        } // attemptToDeposePrimary
 +        
 +      } // while
 +      
 +    } catch (InterruptedException e) {
 +      // abort and return null
 +      Thread.currentThread().interrupt();
 +      
 +    } finally {
 +      getPartitionedRegionStats().endPrimaryTransfer(
 +          startTime, isPrimary(), isRebalance);
 +    }
 +    
 +    return isPrimary();
 +  }
 +  
 +  /**
 +   * Check the primary member shortcut.  Does not query the advisor.  Should
 +   * only be used when the advisor should not be consulted directly.
 +   *  
 +   * @return the member or null if no primary exists
 +   */
 +  public final InternalDistributedMember basicGetPrimaryMember() {
 +    return (InternalDistributedMember) this.primaryMember.get();
 +  }
 +  
 +  /** 
 +   * Invoked when the primary lock has been acquired by this VM.
 +   * 
 +   * @return true if successfully changed state to IS_PRIMARY
 +   */
 +  protected boolean acquiredPrimaryLock() {
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("Acquired primary lock for BucketID {} PR : {}", getBucket().getId(), regionAdvisor.getPartitionedRegion().getFullPath());
 +    }
 +    boolean changedStateToIsPrimary = false;
 +    //Hold the primary move lock until we send a 
 +    //profile update. This will prevent writes
 +    //from occurring until all members know that
 +    //this member is now the primary.
 +    boolean shouldInvokeListeners = false;
 +    activePrimaryMoveLock.lock();
 +    try {
 +      synchronized (this) {
 +        if (isHosting() && (isVolunteering() || isBecomingPrimary())) {
 +          Bucket br = this.regionAdvisor.getBucket(getBucket().getId());
 +          if (br != null && br instanceof BucketRegion) {
 +            ((BucketRegion)br).beforeAcquiringPrimaryState();
 +          }
 +          if (requestPrimaryState(IS_PRIMARY_HOSTING)) {
 +            if (logger.isDebugEnabled()) {
 +              logger.debug("Acquired primary lock for setting primary now BucketID {} PR : {}",
 +                  getBucket().getId(), regionAdvisor.getPartitionedRegion().getFullPath());
 +            }
 +            setPrimaryMember(getDistributionManager().getId());
 +            changedStateToIsPrimary = true;
 +            if (hasPrimary() && isPrimary()) {
 +              shouldInvokeListeners = true;
 +            }
 +          }
 +        }
 +      }
 +
 +      if(shouldInvokeListeners) {
 +        invokePartitionListeners();
 +      }
 +      return changedStateToIsPrimary;
 +    }
 +    finally {
 +      try {
 +        if (changedStateToIsPrimary) {
 +          // send profile update AFTER releasing sync
 +          sendProfileUpdate();
 +
 +          Bucket br = this.regionAdvisor.getBucket(getBucket().getId());
 +          if( br != null && br instanceof BucketRegion) {
 +            ((BucketRegion)br).processPendingSecondaryExpires();
 +          }
 +          if (br instanceof BucketRegionQueue) { // Shouldn't it be AbstractBucketRegionQueue
 +            // i.e. this stats is not getting incremented for HDFSBucketRegionQueue!!
 +            BucketRegionQueue brq = (BucketRegionQueue)br;
 +            brq.incQueueSize(brq.size());
 +          }
 +          if( br != null && br instanceof BucketRegion) {
 +            ((BucketRegion)br).afterAcquiringPrimaryState();
 +          }
 +        }
 +        else {
 +          // release primary lock AFTER releasing sync
 +          releasePrimaryLock();
 +        }
 +      } finally {
 +        activePrimaryMoveLock.unlock();
 +      }
 +    }
 +  }
 +
 +  private void invokePartitionListeners() {
 +    PartitionListener[] listeners = this.pRegion
 +        .getPartitionListeners();
 +    if (listeners == null || listeners.length == 0) {
 +      return;
 +    }
 +    for (int i = 0; i < listeners.length; i++) {
 +      PartitionListener listener = listeners[i];
 +      if (listener != null) {
 +        listener.afterPrimary(getBucket().getId());
 +      }
 +    }
 +  }
 +  
 +  /**
 +   * Lazily gets the lock for acquiring primary lock. Caller must handle null.
 +   * If DLS, Cache, or DistributedSystem are shutting down then null will be
 +   * returned. If DLS does not yet exist and createDLS is false then null will
 +   * be returned.
 +   * 
 +   * @param createDLS true will create DLS if it does not exist
 +   * @return distributed lock indicating primary member or null
 +   */
 +  DistributedMemberLock getPrimaryLock(boolean createDLS) {
 +    synchronized(this) {
 +      if (this.primaryLock == null) {
 +        DistributedLockService dls = DistributedLockService.getServiceNamed(
 +            PartitionedRegionHelper.PARTITION_LOCK_SERVICE_NAME);
 +        if (dls == null) {
 +          if (!createDLS || getProxyBucketRegion().getCache().isClosed()) {
 +            return null;  // cache closure has destroyed the DLS
 +          }
 +          try { // TODO: call GemFireCache#getPartitionedRegionLockService
 +            dls = DLockService.create(
 +                PartitionedRegionHelper.PARTITION_LOCK_SERVICE_NAME, 
 +                getAdvisee().getSystem(), 
 +                true /*distributed*/, 
 +                true /*destroyOnDisconnect*/,
 +                true /*automateFreeResources*/);
 +          }
 +          catch (IllegalArgumentException e) {
 +            // indicates that the DLS is already created
 +            dls = DistributedLockService.getServiceNamed(
 +                PartitionedRegionHelper.PARTITION_LOCK_SERVICE_NAME);
 +            if (dls == null) {
 +              // another thread destroyed DLS after this thread called create
 +              return null; // ok, caller will loop if necessary
 +            }
 +          }
 +          // TODO: we need a good NotConnectedException to replace 
 +          //       IllegalStateException and ShutdownException
 +          //       perhaps: DistributedSystemUnavailableException
 +          catch (IllegalStateException e) {
 +            // create still throws IllegalStateException if isDisconnecting is true
 +            return null;
 +          }
 +          catch (DistributedSystemDisconnectedException e) {
 +            // this would certainly prevent us from creating a DLS... messy
 +            return null;
 +          }
 +        }
 +        this.primaryLock = new DistributedMemberLock(
 +            dls, 
 +            getAdvisee().getName(), DistributedMemberLock.NON_EXPIRING_LEASE,
 +            DistributedMemberLock.LockReentryPolicy.PREVENT_SILENTLY);
 +      }
 +      return this.primaryLock;
 +    }
 +  }
 +
 +  protected void acquirePrimaryRecursivelyForColocated() {
 +    final List<PartitionedRegion> colocatedWithList = ColocationHelper
 +        .getColocatedChildRegions(regionAdvisor.getPartitionedRegion());
 +    if (colocatedWithList != null) {
 +      for (PartitionedRegion childPR : colocatedWithList) {
 +        Bucket b = childPR.getRegionAdvisor().getBucket(getBucket().getId());
 +        BucketAdvisor childBA = b.getBucketAdvisor();
 +        Assert.assertHoldsLock(childBA, false);
 +        boolean acquireForChild = false;
 +
 +        if (logger.isDebugEnabled()) {
 +          logger.debug("BucketAdvisor.acquirePrimaryRecursivelyForColocated: about to take lock for bucket: {} of PR: {} with isHosting={}",
 +              getBucket().getId(), childPR.getFullPath(), childBA.isHosting());
 +        }
 +        childBA.activePrimaryMoveLock.lock();
 +        try {
 +          if (childBA.isHosting()) {
 +            if (isPrimary()) {
 +              if (!childBA.isPrimary()) {
 +                childBA.setVolunteering();
 +                boolean acquired = childBA.acquiredPrimaryLock();
 +                acquireForChild = true;
 +                if (acquired && this.pRegion.isFixedPartitionedRegion()) {
 +                  childBA.acquirePrimaryForRestOfTheBucket();
 +                }
 +              }else {
 +                acquireForChild = true;
 +              }
 +            }
 +          } // if isHosting
 +          if (acquireForChild) {
 +            childBA.acquirePrimaryRecursivelyForColocated();
 +          }
 +        }
 +        finally {
 +          childBA.activePrimaryMoveLock.unlock();
 +        }
 +      }
 +    }
 +  }
 +
 +  protected void acquirePrimaryForRestOfTheBucket() {
 +    List<FixedPartitionAttributesImpl> fpas = this.pRegion
 +        .getFixedPartitionAttributesImpl();
 +    if (fpas != null) {
 +      int bucketId = getBucket().getId();
 +      for (FixedPartitionAttributesImpl fpa : fpas) {
 +        if (fpa.getStartingBucketID() == bucketId) {
 +          for (int i = bucketId + 1; i <= fpa.getLastBucketID();) {
 +            Bucket b = regionAdvisor.getBucket(i++);
 +            if (b != null) {
 +              BucketAdvisor ba = b.getBucketAdvisor();
 +              ba.activePrimaryMoveLock.lock();
 +              try {
 +                if (ba.isHosting()) {
 +                  if (!ba.isPrimary()) {
 +                    ba.setVolunteering();
 +                    ba.acquiredPrimaryLock();
 +                  }
 +                }
 +              } finally {
 +                ba.activePrimaryMoveLock.unlock();
 +              }
 +            }
 +          }
 +        }
 +        else {
 +          continue;
 +        }
 +      }
 +    }
 +  }
 +  
 +  /**
 +   * Sets volunteering to true. Returns true if the state of volunteering was
 +   * changed. Returns false if voluntering was already equal to true. Caller
 +   * should do nothing if false is returned.
 +   */
 +  protected boolean setVolunteering() {
 +    synchronized(this) {
 +      return requestPrimaryState(VOLUNTEERING_HOSTING);
 +    }
 +  }
 +
 +  /**
 +   * Sets becoming primary to true. Returns true if the state of becoming was
 +   * changed. Returns false if becoming was already equal to true. Caller
 +   * should do nothing if false is returned.
 +   */
 +  protected boolean setBecoming() {
 +    synchronized(this) {
 +      return requestPrimaryState(BECOMING_HOSTING);
 +    }
 +  }
 +  
 +  /** 
 +   * Wait briefly for a primary member to be identified.
 +   * 
 +   * @param timeout time in milliseconds to wait for a primary
 +   * @return the primary bucket host
 +   */
 +  protected InternalDistributedMember waitForPrimaryMember(long timeout) {
 +    synchronized (this) {
 +      // let's park this thread and wait for a primary!
 +      StopWatch timer = new StopWatch(true);
 +      long warnTime = getDistributionManager().getConfig().getAckWaitThreshold() * 1000L;
 +      boolean loggedWarning = false;
 +      try {
 +        for (;;) {
 +          // bail out if the system starts closing
 +          this.getAdvisee().getCancelCriterion().checkCancelInProgress(null);
 +          final GemFireCacheImpl cache = (GemFireCacheImpl)getBucket().getCache();
 +          if (cache != null && cache.isCacheAtShutdownAll()) {
 +            throw new CacheClosedException("Cache is shutting down");
 +          }
 +
 +          if (getBucketRedundancy() == -1 ) {
 +            // there are no real buckets in other vms... no reason to wait
 +            return null;
 +          }
 +          getProxyBucketRegion().getPartitionedRegion().checkReadiness();
 +          if (isClosed()) {
 +            break;
 +          }
 +          long elapsed = timer.elapsedTimeMillis();
 +          long timeLeft = timeout - elapsed;
 +          if (timeLeft <= 0) {
 +            break;
 +          }
 +          if (getBucketRedundancy() == -1 || isClosed() ) {
 +            break; // early out... all bucket regions are gone or we closed
 +          }
 +          InternalDistributedMember primary = basicGetPrimaryMember();
 +          if (primary != null) {
 +            return primary;
 +          }
 +  
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("Waiting for bucket {}. Time left :{} ms", this, timeLeft);
 +          }
 +          
 +          //Log a warning if we have waited for the ack wait threshold time.
 +          if(!loggedWarning) {
 +            long timeUntilWarning = warnTime - elapsed;
 +            if(timeUntilWarning <= 0 ) {
 +              logger.warn(LocalizedMessage.create(LocalizedStrings.BucketAdvisor_WAITING_FOR_PRIMARY,
 +                  new Object[] {warnTime / 1000L, this, this.adviseInitialized()}));
 +              //log a warning;
 +              loggedWarning=true;
 +            } else {
 +              timeLeft = timeLeft > timeUntilWarning ? timeUntilWarning : timeLeft;
 +            }
 +          }
 +          this.wait(timeLeft); // spurious wakeup ok
 +        }
 +      }
 +      catch (InterruptedException e) {
 +        // abort and return null
 +        Thread.currentThread().interrupt();
 +      } finally {
 +        if(loggedWarning) {
 +          logger.info(LocalizedMessage.create(LocalizedStrings.BucketAdvisor_WAITING_FOR_PRIMARY_DONE));
 +        }
 +      }
 +      return null;
 +    }
 +  }
 +
 +  /**
 +   * How long to wait, in millisecs, for redundant buckets to exist
 +   */
 +  private final static long BUCKET_REDUNDANCY_WAIT = 15000L; // 15 seconds
 +
 +  /** 
 +   * Wait the desired redundancy to be met.
 +   * @param minRedundancy the amount of desired redundancy.
 +   * @return true if desired redundancy is detected
 +   */
 +  public boolean waitForRedundancy(int minRedundancy) {
 +    synchronized (this) {
 +      // let's park this thread and wait for redundancy!
 +      StopWatch timer = new StopWatch(true);
 +      try {
 +        for (;;) {
 +          if (getBucketRedundancy() >= minRedundancy) {
 +            return true;
 +          }
 +          getProxyBucketRegion().getPartitionedRegion().checkReadiness();
 +          if (isClosed()) {
 +            return false;
 +          }
 +          long timeLeft = BUCKET_REDUNDANCY_WAIT - timer.elapsedTimeMillis();
 +          if (timeLeft <= 0) {
 +            return false;
 +          }
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("Waiting for bucket {}", this);
 +          }
 +          this.wait(timeLeft); // spurious wakeup ok
 +        }
 +      }
 +      catch (InterruptedException e) {
 +        // abort and return null
 +        Thread.currentThread().interrupt();
 +      }
 +      return false;
 +    }
 +  }
 +
 +  private final static long BUCKET_STORAGE_WAIT = Long.getLong("gemfire.BUCKET_STORAGE_WAIT", 15000).longValue(); // 15 seconds
 +  
 +  public boolean waitForStorage() {
 +    synchronized (this) {
 +      // let's park this thread and wait for storage!
 +      StopWatch timer = new StopWatch(true);
 +      try {
 +        for (;;) {
 +          if (this.regionAdvisor.isBucketLocal(getBucket().getId())) {
 +            return true;
 +          }
 +          getProxyBucketRegion().getPartitionedRegion().checkReadiness();
 +          if (isClosed()) {
 +            return false;
 +          }
 +          long timeLeft = BUCKET_STORAGE_WAIT - timer.elapsedTimeMillis();
 +          if (timeLeft <= 0) {
 +            return false;
 +          }
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("Waiting for bucket storage" + this);
 +          }
 +          this.wait(timeLeft); // spurious wakeup ok
 +        }
 +      }
 +      catch (InterruptedException e) {
 +        // abort and return null
 +        Thread.currentThread().interrupt();
 +      }
 +      return false;
 +    }
 +  }
 +  public void clearPrimaryElector() {
 +    synchronized(this) {
 +      primaryElector = null;
 +    }
 +  }
 +  
 +  public void setPrimaryElector(InternalDistributedMember newPrimaryElector) {
 +    synchronized(this) {
 +      //Only set the new primary elector if we have not yet seen
 +      //a primary for this bucket.
 +      if(primaryElector != null) {
 +        this.primaryElector = newPrimaryElector;
 +      }
 +    }
 +  }
 +  
 +  
 +  public synchronized void initializePrimaryElector(InternalDistributedMember primaryElector) {
 +    //For child buckets, we want the parent bucket to take care'
 +    //of finishing an incomplete bucket creation, so only set the elector for
 +    //the leader region.
 +    if(parentAdvisor == null) {
 +      this.primaryElector = primaryElector;
 +    }
 +  }
 +  
 +  /** 
 +   * Invoked when real bucket is created for hosting in this VM.
 +   * 
 +   * @param value true to begin hosting; false to end hosting
 +   */
 +  protected void setHosting(boolean value) {
 +    //boolean needToNotPrimarySelf = false;
 +    boolean needToVolunteerForPrimary = false;
 +    boolean wasPrimary = false;
 +    synchronized(this) {
 +      wasPrimary = isPrimary();
 +      if (isClosed()) {
 +        return;
 +      }     
 +      if (value) { // setting to HOSTING...
 +        if (hasPrimary()) {
 +          requestPrimaryState(OTHER_PRIMARY_HOSTING);
 +        }
 +        else {
 +          requestPrimaryState(NO_PRIMARY_HOSTING);
 +          needToVolunteerForPrimary = true;
 +        }
 +      }
 +
 +      else { // setting to NOT_HOSTING...
 +        if (hasPrimary()) { // has primary...
 +          if (isPrimary()) {
 +            requestPrimaryState(NO_PRIMARY_NOT_HOSTING);
 +            this.primaryMember.set(null);
 +            findAndSetPrimaryMember();
 +          }
 +          else {
 +            requestPrimaryState(OTHER_PRIMARY_NOT_HOSTING);
 +          }
 +        }
 +        else { // no primary...
 +          // acquiredPrimaryLock will check isHosting and release if not hosting
 +          requestPrimaryState(NO_PRIMARY_NOT_HOSTING);
 +        }
 +      }
 +      this.volunteeringDelegate = null;
 +
 +      //Note - checkRedundancy has the side effect that it updates the stats.
 +      //We need to invoke checkRedundancy here, regardless of whether we
 +      //need this notify.
 +      if (updateRedundancy() > 0 && isHosting()) {
 +        // wake up any threads in waitForRedundancy or waitForPrimary
 +        this.notifyAll(); 
 +      }
 +    }
 +    if(wasPrimary) {
 +      releasePrimaryLock();
 +    }
 +
 +    if (logger.isTraceEnabled()) {
 +      logger.trace("setHosting: {} needToVolunteerForPrimary={} primaryElector: {}", this, needToVolunteerForPrimary, primaryElector);
 +    }
 +    /*if (needToNotPrimarySelf) {
 +      notPrimary(getAdvisee().getDistributionManager().getId());
 +    }*/
 +    if (needToVolunteerForPrimary) {
 +      if(this.primaryElector == null) {
 +        volunteerForPrimary();
 +      }
 +    }
 +    
 +    sendProfileUpdate();
 +  }
 +  
 +  /**
 +   * Sends updated profile for this member to every member with the 
 +   * <code>PartitionedRegion</code>.
 +   * <p>
 +   * Never call this method while synchronized on this BucketAdvisor. This will
 +   * result in distributed deadlocks.
 +   */
 +  private void sendProfileUpdate() {
 +    if (this.getDistributionManager().getSystem().isLoner()) { 
 +      // no one to send the profile update... return to prevent bug 39760 
 +      return; 
 +    } 
 +    // make sure caller is not synchronized or we'll deadlock
 +    Assert.assertTrue( ! Thread.holdsLock(this), 
 +        "Attempting to sendProfileUpdate while synchronized may result in deadlock");
 +    // NOTE: if this assert fails, you COULD use the WaitingThreadPool in DM
 +
 +    final int partitionedRegionId = this.pRegion.getPRId();
 +    final int bucketId = ((ProxyBucketRegion) getAdvisee()).getBucketId();
 +    
 +    BucketProfile bp = (BucketProfile) createProfile();
 +    updateServerBucketProfile(bp);
 +    InternalDistributedMember primary = basicGetPrimaryMember();
 +    HashSet hostsAndProxyMembers = new HashSet();
 +    if (primary != null && !primary.equals(getDistributionManager().getId())) {
 +      hostsAndProxyMembers.add(primary); // Add the primary
 +    }
 +    hostsAndProxyMembers.addAll(adviseGeneric()); // Add all members hosting the bucket
 +    hostsAndProxyMembers.addAll(adviseProfileUpdate()); // Add all proxy instances that could use the bucket
 +    ReplyProcessor21 reply = BucketProfileUpdateMessage.send(hostsAndProxyMembers, getDistributionManager(),
 +        partitionedRegionId, bucketId, bp, true);
 +    if(reply != null) {
 +      reply.waitForRepliesUninterruptibly();
 +    }
 +  }
 +  
 +  /** 
 +   * Returns true if the a primary is known. 
 +   */
 +  private boolean hasPrimary() {
 +    synchronized(this) {
 +      return this.primaryState == OTHER_PRIMARY_NOT_HOSTING || 
 +      this.primaryState == OTHER_PRIMARY_HOSTING ||
 +      this.primaryState ==IS_PRIMARY_HOSTING;
 +    }
 +  }
 +
 +  @Override
 +  protected Profile instantiateProfile(InternalDistributedMember memberId,
 +      int version) {
 +    if (!this.pRegion.isShadowPR()) {
 +      GemFireCacheImpl c = getProxyBucketRegion().getCache();
 +      List servers = null;
 +      servers = c.getCacheServers();
 +
 +      HashSet<BucketServerLocation66> serverLocations = new HashSet<BucketServerLocation66>();
 +      for (Object object : servers) {
 +        CacheServerImpl server = (CacheServerImpl)object;
 +        if (server.isRunning() && (server.getExternalAddress() != null)) {
 +          BucketServerLocation66 location = new BucketServerLocation66(
 +              getBucket().getId(), server.getPort(), server
 +                  .getExternalAddress()
 +              /* .getExternalAddress(false/ checkServerRunning ) */,
 +              getBucket().isPrimary(), Integer.valueOf(version).byteValue(),
 +              server.getCombinedGroups());
 +          serverLocations.add(location);
 +        }
 +      }
 +      if (serverLocations.size() > 0) {
 +        return new ServerBucketProfile(memberId, version, getBucket(),
 +            serverLocations);
 +      }
 +    }
 +    return new BucketProfile(memberId, version, getBucket());
 +  }
 +  
 +  /** 
 +   * Sets primaryMember and notifies all. Caller must be synced on this.
 +   * 
 +   * @param id the member to use as primary for this bucket
 +   */
 +  void setPrimaryMember(InternalDistributedMember id) {
 +    if (!getDistributionManager().getId().equals(id)) {
 +      // volunteerForPrimary handles primary state change if its our id
 +      if (isHosting()) {
 +        requestPrimaryState(OTHER_PRIMARY_HOSTING);
 +      }
 +      else {
 +        requestPrimaryState(OTHER_PRIMARY_NOT_HOSTING);
 +      }
 +    }
 +    this.primaryMember.set(id);
 +    this.everHadPrimary = true;
 +    
 +    if(id != null && id.equals(primaryElector)) {
 +      primaryElector = null;
 +    }
 +    this.notifyAll(); // wake up any threads in waitForPrimaryMember
 +  }
 +  
 +  public void setHadPrimary() {
 +    this.everHadPrimary = true;
 +  }
 +  
 +  public boolean getHadPrimary() {
 +    return this.everHadPrimary;
 +  }
 +  
 +  public InternalDistributedMember getPrimaryElector() {
 +    return primaryElector;
 +  }
 +
 +  /**
 +   * Determine if there has been a change in redundancy and alter the
 +   * lowRedundancyBucketCount stat as needed.
 +   * 
 +   * Also updates a counter used to track the redundancy of this member
 +   * 
 +   * @return current number of hosts for this bucket 
 +   * @see #redundancySatisfied
 +   * @see PartitionedRegionStats#incLowRedundancyBucketCount(int)
 +   * @guarded.By this
 +   */
 +  private int updateRedundancy() {
 +    int desiredRedundancy = 
 +        this.pRegion.getRedundantCopies();
 +    int numBucketHosts = getNumInitializedBuckets();
 +    if (isClosed()) {
 +      return numBucketHosts;
 +    }
 +    int actualRedundancy = numBucketHosts - 1;
 +    this.redundancy = actualRedundancy;
 +    if (this.redundancySatisfied && 
 +        numBucketHosts > 0 &&
 +        actualRedundancy < desiredRedundancy) {
 +      incLowRedundancyBucketCount(1);
 +      this.redundancySatisfied = false;
 +    }
 +    else if (!this.redundancySatisfied &&
 +             numBucketHosts > 0 &&
 +             actualRedundancy >= desiredRedundancy){
 +      incLowRedundancyBucketCount(-1);
 +      this.redundancySatisfied = true;
 +      this.redundancyEverSatisfied = true;
 +    }
 +    return numBucketHosts;
 +  }
 +  
 +  /**
 +   * Returns all {@link InternalDistributedMember}s currently
 +   * flagged as primary.
 +   * <p>
 +   * Since profile messages may arrive out of order from different members,
 +   * more than one member may temporarily be flagged as primary.
 +   * <p>
 +   * The user of this BucketAdvisor should simply assume that the first
 +   * profile is primary until the dust settles, leaving only one primary
 +   * profile.
 +   * 
 +   * @return zero or greater array of primary members
 +   */
 +  private InternalDistributedMember[] findPrimaryMembers() {
 +    Set primaryMembers = adviseFilter(new Filter() {
 +      public boolean include(Profile profile) {
 +        assert profile instanceof BucketProfile;
 +        BucketProfile srp = (BucketProfile) profile;
 +        return srp.isPrimary;
 +      }
 +    });
 +    if (primaryMembers.size() > 1 && logger.isDebugEnabled()) {
 +      logger.debug("[findPrimaryProfiles] found the following primary members for {}: {}", getAdvisee().getName(), primaryMembers);
 +    }
 +    return (InternalDistributedMember[]) primaryMembers.toArray(
 +        new InternalDistributedMember[primaryMembers.size()]);
 +  }
 +
 +  /**
 +   * Searches through profiles to find first profile that is flagged as 
 +   * primary and sets {@link #primaryMember} to it. Caller must synchronize
 +   * on this BucketAdvisor.
 +   * 
 +   * @return true if a primary member was found and used
 +   * @see #findAndSetPrimaryMember()
 +   */
 +  boolean findAndSetPrimaryMember() {
 +    if(isPrimary()) {
 +      setPrimaryMember(this.getDistributionManager().getDistributionManagerId());
 +      return true;
 +    }
 +    InternalDistributedMember[] primaryMembers = findPrimaryMembers();
 +    if (primaryMembers.length > 0) {
 +      setPrimaryMember(primaryMembers[0]);
 +      return true;
 +    }
 +    else {
 +      return false;
 +    }
 +  }
 +  
 +  /**
 +   * Returns the current redundancy of the this bucket, including the locally
 +   * hosted bucket if it exists.
 +   * 
 +   * @return current number of hosts of this bucket ; -1 if there are no hosts
 +   */
 +  public final int getBucketRedundancy() {
 +    return redundancy;
 +  }
 +  
 +  public Set<InternalDistributedMember> adviseInitialized() {
 +    return adviseFilter(new Filter() {
 +      public boolean include(Profile profile) {
 +        assert profile instanceof BucketProfile;
 +        BucketProfile bucketProfile = (BucketProfile) profile;
 +        return bucketProfile.isHosting;
 +      }
 +    });
 +    
 +  }
 +  
 +  public Set<InternalDistributedMember> adviseRecoveredFromDisk() {
 +    return regionAdvisor.adviseInitializedDataStore();
 +  }
 +  
 +  /**
 +   * Get the number of members that are hosting the bucket, and have
 +   * finished initialization.
 +   * 
 +   * This method is currently only used to check the bucket
 +   * redundancy just before creating the bucket. If it is used
 +   * more frequently, it might be better to cache this count.
 +   */
 +  private int getNumInitializedBuckets() {
 +    Profile[] locProfiles = this.profiles; // grab current profiles
 +    int count = 0;
 +    for(Profile profile: locProfiles) {
 +      BucketProfile bucketProfile = (BucketProfile) profile;
 +      if(bucketProfile.isHosting) {
 +        count++;
 +      }
 +    }
 +    if(isHosting()) {
 +      count++;
 +    }
 +    return count;
 +  }
 +  
 +  private Bucket getBucket() {
 +    return (Bucket)getAdvisee();
 +  }
 +  
 +  /**
 +   * Releases the primary lock for this bucket.
 +   */
 +  protected void releasePrimaryLock() {
 +    //We don't have a lock if we have a parent advisor
 +    if(parentAdvisor != null) {
 +      return;
 +    }
 +    if (startingBucketAdvisor == null) {
 +      assignStartingBucketAdvisor();
 +      if (startingBucketAdvisor != null) {
 +        return;
 +      }
 +    } else {
 +      return;
 +    }
 +    // TODO fix this method to not release any locks if the  
 +    // redundancy is zero, since no locks are grabbed. 
 +    try {
 +      DistributedMemberLock thePrimaryLock = getPrimaryLock(false);
 +      if (thePrimaryLock != null) {
 +        thePrimaryLock.unlock();
 +      }
 +      else {
 +        // InternalDistributedSystem.isDisconnecting probably prevented us from
 +        // creating the DLS... hope there's a thread closing this advisor but
 +        // it's probably not safe to assert that it already happened
 +      }
 +    } 
 +    catch (LockNotHeldException e) {      
 +      Assert.assertTrue(!isHosting(), 
 +          "Got LockNotHeldException for Bucket = " + this);
 +    }
 +    catch (LockServiceDestroyedException e) {
 +      Assert.assertTrue(isClosed(), 
 +          "BucketAdvisor was not closed before destroying PR lock service");
 +    }
 +  }
 +  
 +  private String primaryStateToString() {
 +    return primaryStateToString(this.primaryState);
 +  }
 +
 +  /**
 +   * Returns string representation of the primary state value.
 +   * @param value the primary state to return string for
 +   * @return string representation of primaryState
 +   */
 +  private String primaryStateToString(byte value) {
 +    switch (value) {
 +      case NO_PRIMARY_NOT_HOSTING:
 +        return "NO_PRIMARY_NOT_HOSTING";
 +      case NO_PRIMARY_HOSTING:
 +        return "NO_PRIMARY_HOSTING";
 +      case OTHER_PRIMARY_NOT_HOSTING:
 +        return "OTHER_PRIMARY_NOT_HOSTING";
 +      case OTHER_PRIMARY_HOSTING:
 +        return "OTHER_PRIMARY_HOSTING";
 +      case VOLUNTEERING_HOSTING:
 +        return "VOLUNTEERING_HOSTING";
 +      case BECOMING_HOSTING:
 +        return "BECOMING_HOSTING";
 +      case IS_PRIMARY_HOSTING:
 +        return "IS_PRIMARY_HOSTING";
 +      case CLOSED:
 +        return "CLOSED";
 +      default:
 +        return "<unhandled primaryState " + value + " >";
 +    }
 +  }
 +  
 +  /**
 +   * Requests change to the requested primary state. Controls all state
 +   * changes pertaining to primary state. Caller must be synchronized on this.
 +   * 
 +   * @param requestedState primaryState to change to
 +   * @return true if the requestedState change was completed
 +   * @throws IllegalStateException if an illegal state change was attempted
 +   */
 +  private boolean requestPrimaryState(byte requestedState) {
 +    final byte fromState = this.primaryState;
 +    switch (fromState) {
 +      case NO_PRIMARY_NOT_HOSTING:
 +        switch (requestedState) {
 +          case NO_PRIMARY_NOT_HOSTING:
 +            // race condition ok, return false
 +            return false;
 +          case NO_PRIMARY_HOSTING:
 +            this.primaryState = requestedState;
 +            break;
 +          case OTHER_PRIMARY_NOT_HOSTING:
 +            this.primaryState = requestedState;
 +            break;
 +          case OTHER_PRIMARY_HOSTING:
 +            this.primaryState = requestedState;
 +            break;
 +          case BECOMING_HOSTING:
 +            // race condition during close is ok, return false
 +            return false;
 +          case VOLUNTEERING_HOSTING:
 +            // race condition during close is ok, return false
 +            return false;
 +          case CLOSED:
 +            this.primaryState = requestedState;
 +            break;
 +          default:
 +            throw new IllegalStateException(LocalizedStrings.BucketAdvisor_CANNOT_CHANGE_FROM_0_TO_1.toLocalizedString(new Object[] {this.primaryStateToString(), this.primaryStateToString(requestedState)}));
 +        }
 +        break;
 +      case NO_PRIMARY_HOSTING:
 +        switch (requestedState) {
 +          case NO_PRIMARY_NOT_HOSTING:
 +            this.primaryState = requestedState;
 +            break;
 +//          case OTHER_PRIMARY_NOT_HOSTING: -- enable for bucket migration
 +//            this.primaryState = requestedState;
 +//            break;
 +          case NO_PRIMARY_HOSTING:
 +            // race condition ok, return false
 +            return false;
 +          case VOLUNTEERING_HOSTING:
 +            this.primaryState = requestedState;
 +            {
 +              PartitionedRegionStats stats = getPartitionedRegionStats();
 +              stats.putStartTime(this, stats.startVolunteering());
 +            }
 +            break;
 +          case BECOMING_HOSTING:
 +            this.primaryState = requestedState;
 +            {
 +              PartitionedRegionStats stats = getPartitionedRegionStats();
 +              stats.putStartTime(this, stats.startVolunteering());
 +            }
 +            break;
 +          case OTHER_PRIMARY_HOSTING:
 +            this.primaryState = requestedState;
 +            break;
 +          case CLOSED:
 +            this.primaryState = requestedState;
 +            break;
 +          default:
 +            throw new IllegalStateException(LocalizedStrings.BucketAdvisor_CANNOT_CHANGE_FROM_0_TO_1.toLocalizedString(new Object[] {this.primaryStateToString(), this.primaryStateToString(requestedState)}));
 +        }
 +        break;
 +      case OTHER_PRIMARY_NOT_HOSTING:
 +        switch (requestedState) {
 +          case NO_PRIMARY_NOT_HOSTING:
 +            this.primaryState = requestedState;
 +            break;
 +          case OTHER_PRIMARY_NOT_HOSTING:
 +            // race condition ok, return false
 +            return false;
 +          case OTHER_PRIMARY_HOSTING:
 +            this.primaryState = requestedState;
 +            break;
 +          case BECOMING_HOSTING:
 +            // race condition during close is ok, return false
 +            return false;
 +          case VOLUNTEERING_HOSTING:
 +            // race condition during close is ok, return false
 +            return false;
 +          case CLOSED:
 +            this.primaryState = requestedState;
 +            break;
 +          default:
 +            throw new IllegalStateException(LocalizedStrings.BucketAdvisor_CANNOT_CHANGE_FROM_0_TO_1.toLocalizedString(new Object[] {this.primaryStateToString(), this.primaryStateToString(requestedState)}));
 +        }
 +        break;
 +      case OTHER_PRIMARY_HOSTING:
 +        switch (requestedState) {
 +//          case NO_PRIMARY_NOT_HOSTING: -- enable for bucket migration
 +//            this.primaryState = requestedState;
 +//            break;
 +          case OTHER_PRIMARY_NOT_HOSTING:
 +            // May occur when setHosting(false) is called
 +            this.primaryState = requestedState;
 +            break;
 +          case OTHER_PRIMARY_HOSTING:
 +            // race condition ok, return false
 +            return false;
 +          case NO_PRIMARY_HOSTING:
 +            this.primaryState = requestedState;
 +            break;
 +          case CLOSED:
 +            this.primaryState = requestedState;
 +            break;
 +          case VOLUNTEERING_HOSTING:
 +            // race condition ok, return false to abort volunteering
 +            return false;
 +          case BECOMING_HOSTING:
 +            this.primaryState = requestedState;
 +            {
 +              PartitionedRegionStats stats = getPartitionedRegionStats();
 +              stats.putStartTime(this, stats.startVolunteering());
 +            }
 +            break;
 +          case IS_PRIMARY_HOSTING:
 +            // race condition ok, probably race in HA where other becomes 
 +            // primary and immediately leaves while we have try-lock message
 +            // enroute to grantor
 +            this.primaryState = requestedState;
 +            break;
 +          default:
 +            throw new IllegalStateException(LocalizedStrings.BucketAdvisor_CANNOT_CHANGE_FROM_0_TO_1.toLocalizedString(new Object[] {this.primaryStateToString(), this.primaryStateToString(requestedState)}));
 +        }
 +        break;
 +      case VOLUNTEERING_HOSTING:
 +        switch (requestedState) {
 +          case NO_PRIMARY_NOT_HOSTING:
 +            // May occur when setHosting(false) is called
 +            this.primaryState = requestedState;
 +            {
 +              PartitionedRegionStats stats = getPartitionedRegionStats();
 +              stats.endVolunteeringClosed(stats.removeStartTime(this));
 +            }
 +            break;
 +          case OTHER_PRIMARY_NOT_HOSTING:
 +            // May occur when setHosting(false) is called
 +            // Profile update for other primary may have slipped in
 +            this.primaryState = requestedState;
 +            {
 +              PartitionedRegionStats stats = getPartitionedRegionStats();
 +              stats.endVolunteeringClosed(stats.removeStartTime(this));
 +            }
 +            break;
 +          case NO_PRIMARY_HOSTING:
 +            // race condition occurred, return false and stay in volunteering
 +            return false;
 +          case IS_PRIMARY_HOSTING:
 +            this.primaryState = requestedState;
 +            {
 +              PartitionedRegionStats stats = getPartitionedRegionStats();
 +              stats.incPrimaryBucketCount(1);
 +              stats.endVolunteeringBecamePrimary(stats.removeStartTime(this));
 +            }
 +            break;
 +          case OTHER_PRIMARY_HOSTING:
 +            this.primaryState = requestedState;
 +            {
 +              PartitionedRegionStats stats = getPartitionedRegionStats();
 +              stats.endVolunteeringOtherPrimary(stats.removeStartTime(this));
 +            }
 +            break;
 +          case VOLUNTEERING_HOSTING:
 +            // race condition ok, return false to abort volunteering
 +            return false;
 +          case BECOMING_HOSTING:
 +            // race condition ok, return false to abort volunteering
 +            return false;
 +          case CLOSED:
 +            this.primaryState = requestedState;
 +            {
 +              PartitionedRegionStats stats = getPartitionedRegionStats();
 +              stats.endVolunteeringClosed(stats.removeStartTime(this));
 +            }
 +            break;
 +          default:
 +            throw new IllegalStateException(LocalizedStrings.BucketAdvisor_CANNOT_CHANGE_FROM_0_TO_1.toLocalizedString(new Object[] {this.primaryStateToString(), this.primaryStateToString(requestedState)}));
 +        }
 +        break;
 +      case BECOMING_HOSTING:
 +        switch (requestedState) {
 +          case NO_PRIMARY_NOT_HOSTING:
 +            // May occur when setHosting(false) is called
 +            this.primaryState = requestedState;
 +            {
 +              PartitionedRegionStats stats = getPartitionedRegionStats();
 +              stats.endVolunteeringClosed(stats.removeStartTime(this));
 +            }
 +            break;
 +          case OTHER_PRIMARY_NOT_HOSTING:
 +            // May occur when setHosting(false) is called
 +            // Profile update for other primary may have slipped in
 +            this.primaryState = requestedState;
 +            {
 +              PartitionedRegionStats stats = getPartitionedRegionStats();
 +              stats.endVolunteeringClosed(stats.removeStartTime(this));
 +            }
 +            break;
 +          case NO_PRIMARY_HOSTING:
 +            // race condition occurred, return false and stay in volunteering
 +            return false;
 +          case IS_PRIMARY_HOSTING:
 +            this.primaryState = requestedState;
 +            {
 +              PartitionedRegionStats stats = getPartitionedRegionStats();
 +              stats.incPrimaryBucketCount(1);
 +              stats.endVolunteeringBecamePrimary(stats.removeStartTime(this));
 +            }
 +            break;
 +          case OTHER_PRIMARY_HOSTING:
 +            return false;
 +          case VOLUNTEERING_HOSTING:
 +            // race condition ok, return false to abort volunteering
 +            return false;
 +          case BECOMING_HOSTING:
 +            // race condition ok, return false to abort volunteering
 +            return false;
 +          case CLOSED:
 +            this.primaryState = requestedState;
 +            {
 +              PartitionedRegionStats stats = getPartitionedRegionStats();
 +              stats.endVolunteeringClosed(stats.removeStartTime(this));
 +            }
 +            break;
 +          default:
 +            throw new IllegalStateException(LocalizedStrings.BucketAdvisor_CANNOT_CHANGE_FROM_0_TO_1.toLocalizedString(new Object[] {this.primaryStateToString(), this.primaryStateToString(requestedState)}));
 +        }
 +        break;
 +      case IS_PRIMARY_HOSTING:
 +        switch (requestedState) {
 +          case NO_PRIMARY_HOSTING:
 +            // rebalancing must have moved the primary
 +            changeFromPrimaryTo(requestedState);
 +            break;
 +//          case OTHER_PRIMARY_HOSTING: -- enable for bucket migration
 +//            // rebalancing must have moved the primary
 +//            changeFromPrimaryTo(requestedState);
 +//            break;
 +          case OTHER_PRIMARY_NOT_HOSTING:
 +            // rebalancing must have moved the primary and primary
 +            changeFromPrimaryTo(requestedState);
 +            break;
 +          case NO_PRIMARY_NOT_HOSTING:
 +            // May occur when setHosting(false) is called due to closing
 +            changeFromPrimaryTo(requestedState);
 +            break;
 +          case VOLUNTEERING_HOSTING:
 +            // race condition ok, return false to abort volunteering
 +            return false;
 +          case BECOMING_HOSTING:
 +            // race condition ok, return false to abort volunteering
 +            return false;
 +          case CLOSED:
 +            changeFromPrimaryTo(requestedState);
 +            break;
 +          default:
 +            throw new IllegalStateException(
 +                "Cannot change from " + this.primaryStateToString() + 
 +                " to " + this.primaryStateToString(requestedState));
 +        }
 +        break;
 +      case CLOSED:
 +        switch (requestedState) {
 +          case CLOSED:
 +            Exception e = new Exception(LocalizedStrings.BucketAdvisor_ATTEMPTED_TO_CLOSE_BUCKETADVISOR_THAT_IS_ALREADY_CLOSED.toLocalizedString());
 +            logger.warn(LocalizedMessage.create(LocalizedStrings.BucketAdvisor_ATTEMPTED_TO_CLOSE_BUCKETADVISOR_THAT_IS_ALREADY_CLOSED), e);
 +            break;
 +          case VOLUNTEERING_HOSTING:
 +            // race condition ok, return false to abort volunteering
 +            return false;
 +          case BECOMING_HOSTING:
 +            // race condition ok, return false to abort volunteering
 +            return false;
 +          case IS_PRIMARY_HOSTING:
 +            // Commonly occurs when closing and volunteering thread is still running
 +            return false;
 +          case OTHER_PRIMARY_NOT_HOSTING:
 +            // Commonly occurs when a putProfile occurs during closure
 +            return false;
 +          default:
 +            throw new IllegalStateException(LocalizedStrings.BucketAdvisor_CANNOT_CHANGE_FROM_0_TO_1_FOR_BUCKET_2.toLocalizedString(new Object[] {this.primaryStateToString(), this.primaryStateToString(requestedState), getAdvisee().getName()}));
 +        }
 +    }
 +    return this.primaryState == requestedState;
 +  }
 +
 +  private void changeFromPrimaryTo(byte requestedState) {
 +    try {
 +      this.primaryState = requestedState;
 +    }
 +    finally {
 +      getPartitionedRegionStats().incPrimaryBucketCount(-1);
 +    }
 +  }
 +  
 +  @Override
 +  public Set adviseDestroyRegion() {
 +    // fix for bug 37604 - tell all owners of the pr that the bucket is being
 +    // destroyed.  This is needed when bucket cleanup is performed
 +    return this.regionAdvisor.adviseAllPRNodes();
 +  }
 +  
 +  /**
 +   * returns the set of all the members in the system which require both
 +   * DistributedCacheOperation messages and notification-only partition
 +   * messages
 +

<TRUNCATED>


[25/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/xmlcache/CacheXmlGenerator.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/cache/xmlcache/CacheXmlGenerator.java
index 47c341c,0000000..4ba1409
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/xmlcache/CacheXmlGenerator.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/xmlcache/CacheXmlGenerator.java
@@@ -1,2850 -1,0 +1,2877 @@@
 +/*
 + * 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 static com.gemstone.gemfire.internal.cache.xmlcache.XmlGeneratorUtils.*;
 +import static com.gemstone.gemfire.management.internal.configuration.utils.XmlConstants.*;
 +import static javax.xml.XMLConstants.*;
 +
 +import java.io.File;
 +import java.io.FileWriter;
 +import java.io.IOException;
 +import java.io.PrintWriter;
 +import java.net.InetSocketAddress;
 +import java.util.Arrays;
 +import java.util.Collection;
 +import java.util.Comparator;
 +import java.util.HashSet;
 +import java.util.Iterator;
 +import java.util.List;
 +import java.util.Map;
 +import java.util.Properties;
 +import java.util.Set;
 +import java.util.TreeSet;
 +
 +import javax.xml.transform.OutputKeys;
 +import javax.xml.transform.Result;
 +import javax.xml.transform.Source;
 +import javax.xml.transform.Transformer;
 +import javax.xml.transform.TransformerFactory;
 +import javax.xml.transform.sax.SAXSource;
 +import javax.xml.transform.stream.StreamResult;
 +
++import com.gemstone.gemfire.cache.wan.*;
 +import org.xml.sax.Attributes;
 +import org.xml.sax.ContentHandler;
 +import org.xml.sax.DTDHandler;
 +import org.xml.sax.EntityResolver;
 +import org.xml.sax.ErrorHandler;
 +import org.xml.sax.InputSource;
 +import org.xml.sax.SAXException;
 +import org.xml.sax.SAXNotRecognizedException;
 +import org.xml.sax.SAXNotSupportedException;
 +import org.xml.sax.XMLReader;
 +import org.xml.sax.ext.LexicalHandler;
 +import org.xml.sax.helpers.AttributesImpl;
 +
 +import com.gemstone.gemfire.InternalGemFireException;
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheListener;
 +import com.gemstone.gemfire.cache.CacheTransactionManager;
 +import com.gemstone.gemfire.cache.CustomExpiry;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.Declarable;
 +import com.gemstone.gemfire.cache.DiskStore;
 +import com.gemstone.gemfire.cache.DiskStoreFactory;
 +import com.gemstone.gemfire.cache.DiskWriteAttributes;
 +import com.gemstone.gemfire.cache.DynamicRegionFactory;
 +import com.gemstone.gemfire.cache.EvictionAction;
 +import com.gemstone.gemfire.cache.EvictionAlgorithm;
 +import com.gemstone.gemfire.cache.EvictionAttributes;
 +import com.gemstone.gemfire.cache.ExpirationAction;
 +import com.gemstone.gemfire.cache.ExpirationAttributes;
 +import com.gemstone.gemfire.cache.FixedPartitionAttributes;
 +import com.gemstone.gemfire.cache.InterestPolicy;
 +import com.gemstone.gemfire.cache.MembershipAttributes;
 +import com.gemstone.gemfire.cache.MirrorType;
 +import com.gemstone.gemfire.cache.PartitionAttributes;
 +import com.gemstone.gemfire.cache.PartitionAttributesFactory;
 +import com.gemstone.gemfire.cache.PartitionResolver;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionAttributes;
 +import com.gemstone.gemfire.cache.RegionShortcut;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.SubscriptionAttributes;
 +import com.gemstone.gemfire.cache.TransactionListener;
 +import com.gemstone.gemfire.cache.asyncqueue.AsyncEventListener;
 +import com.gemstone.gemfire.cache.asyncqueue.AsyncEventQueue;
 +import com.gemstone.gemfire.cache.client.ClientCache;
 +import com.gemstone.gemfire.cache.client.ClientRegionShortcut;
 +import com.gemstone.gemfire.cache.client.Pool;
 +import com.gemstone.gemfire.cache.client.PoolFactory;
 +import com.gemstone.gemfire.cache.client.PoolManager;
 +import com.gemstone.gemfire.cache.client.internal.PoolImpl;
 +import com.gemstone.gemfire.cache.execute.Function;
 +import com.gemstone.gemfire.cache.execute.FunctionService;
 +import com.gemstone.gemfire.cache.partition.PartitionListener;
 +import com.gemstone.gemfire.cache.query.Index;
 +import com.gemstone.gemfire.cache.query.internal.index.HashIndex;
 +import com.gemstone.gemfire.cache.query.internal.index.PrimaryKeyIndex;
 +import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.cache.server.ServerLoadProbe;
 +import com.gemstone.gemfire.cache.util.ObjectSizer;
- import com.gemstone.gemfire.cache.wan.GatewayEventFilter;
- import com.gemstone.gemfire.cache.wan.GatewayReceiver;
- import com.gemstone.gemfire.cache.wan.GatewaySender;
- import com.gemstone.gemfire.cache.wan.GatewayTransportFilter;
 +import com.gemstone.gemfire.distributed.Role;
 +import com.gemstone.gemfire.internal.Assert;
 +import com.gemstone.gemfire.internal.cache.AbstractRegion;
 +import com.gemstone.gemfire.internal.cache.CacheConfig;
 +import com.gemstone.gemfire.internal.cache.ClientSubscriptionConfigImpl;
 +import com.gemstone.gemfire.internal.cache.ColocationHelper;
 +import com.gemstone.gemfire.internal.cache.DiskWriteAttributesImpl;
 +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
 +import com.gemstone.gemfire.internal.cache.LocalRegion;
 +import com.gemstone.gemfire.internal.cache.PartitionAttributesImpl;
 +import com.gemstone.gemfire.internal.cache.PartitionedRegion;
 +import com.gemstone.gemfire.internal.cache.control.MemoryThresholds;
 +import com.gemstone.gemfire.internal.cache.extension.Extensible;
 +import com.gemstone.gemfire.internal.cache.extension.Extension;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.size.SizeClassOnceObjectSizer;
 +import com.gemstone.gemfire.management.internal.configuration.utils.XmlConstants;
 +import com.gemstone.gemfire.pdx.ReflectionBasedAutoSerializer;
 +
 +/**
 + * Generates a declarative XML file that describes a given {@link
 + * Cache} instance.  This class was developed for testing purposes,
 + * but it is conceivable that it could be used in the product as well.
 + *
 + * @author David Whitlock
 + *
 + * @since 3.0
 + */
 +@SuppressWarnings("deprecation")
 +public class CacheXmlGenerator extends CacheXml implements XMLReader {
 +
 +  /** An empty <code>Attributes</code> */
 +  private static final Attributes EMPTY = new AttributesImpl();
 +
 +  /** The content handler to which SAX events are generated */
 +  private ContentHandler handler;
 +
 +  /////////////////////////  Instance Fields  ////////////////////////
 +
 +  /** The Cache that we're generating XML for */
 +  final private Cache cache;
 +
 +  /** Will the generated XML file reference an XML schema instead of
 +   * the DTD? */
 +  private boolean useSchema = true;
 +  private boolean includeKeysValues = true;
 +  private final boolean generateDefaults;
 +
 +//  final private int cacheLockLease;
 +//  final private int cacheLockTimeout;
 +//  final private int cacheSearchTimeout;
 +//  final private boolean isServer;
 +//  final private boolean copyOnRead;
 +
 +  /** The <code>CacheCreation</code> from which XML is generated */
 +  private final CacheCreation creation;
 +
 +  ///////////////////////  Static Methods  ///////////////////////
 +
 +
 +  /**
 +   * Examines the given <code>Cache</code> and from it generates XML
 +   * data that is written to the given <code>PrintWriter</code>.  The
 +   * schema/dtd for the current version of GemFire is used.
 +   *
 +   * @param useSchema
 +   *        Should the generated XML reference a schema (as opposed to
 +   *        a DTD)? For versions 8.1 or newer this should be true,
 +   *        otherwise false.
 +   * @param version
 +   *        The version of GemFire whose DTD/schema should be used in
 +   *        the generated XML.  See {@link #VERSION_4_0}.
 +   *
 +   * @since 4.0
 +   */
 +  public static void generate(Cache cache, PrintWriter pw,
 +                              boolean useSchema, String version) {
 +    (new CacheXmlGenerator(cache, useSchema, version, true)).generate(pw);
 +  }
 +
 +  /**
 +   * Examines the given <code>Cache</code> and from it generates XML
 +   * data that is written to the given <code>PrintWriter</code>.  The
 +   * schema/dtd for the current version of GemFire is used.
 +   *
 +   * @param useSchema
 +   *        Should the generated XML reference a schema (as opposed to
 +   *        a DTD)? As of 8.1 this value is ignored and always true.
 +   */
 +  public static void generate(Cache cache, PrintWriter pw,
 +                              boolean useSchema) {
 +    (new CacheXmlGenerator(cache, true /*latest version always true*/, VERSION_LATEST, true)).generate(pw);
 +  }
 +
 +  /**
 +   * Examines the given <code>Cache</code> and from it generates XML
 +   * data that is written to the given <code>PrintWriter</code>.  The
 +   * schema/dtd for the current version of GemFire is used.
 +   *
 +   * @param useSchema
 +   *        Should the generated XML reference a schema (as opposed to
 +   *        a DTD)? As of 8.1 this value is ignored and always true.
 +   * @param includeKeysValues true if the xml should include keys and values
 +   *                          false otherwise
 +   */
 +  public static void generate(Cache cache, PrintWriter pw,
 +                              boolean useSchema, boolean includeKeysValues) {
 +    (new CacheXmlGenerator(cache, true /*latest version always true*/, VERSION_LATEST, includeKeysValues)).generate(pw);
 +  }
 +  /**
 +   * @param useSchema
 +   *        Should the generated XML reference a schema (as opposed to
 +   *        a DTD)? As of 8.1 this value is ignored and always true.
 +   * @param includeDefaults set to false to cause generated xml to not have defaults values.
 +   */
 +  public static void generate(Cache cache, PrintWriter pw,
 +      boolean useSchema, boolean includeKeysValues, boolean includeDefaults) {
 +    (new CacheXmlGenerator(cache, true /*latest version always true*/, VERSION_LATEST, includeKeysValues, includeDefaults)).generate(pw);
 +  }
 +
 +  /**
 +   * Examines the given <code>Cache</code> and from it generates XML
 +   * data that is written to the given <code>PrintWriter</code>.
 +   */
 +  public static void generate(Cache cache, PrintWriter pw) {
 +    generate(cache, pw, true /* useSchema */);
 +  }
 +
 +  /**
 +   * Examines the given <code>ClientCache</code> and from it generates XML
 +   * data that is written to the given <code>PrintWriter</code>.  The
 +   * schema/dtd for the current version of GemFire is used.
 +   *
 +   * @param useSchema
 +   *        Should the generated XML reference a schema (as opposed to
 +   *        a DTD)? For versions 8.1 or newer this should be true,
 +   *        otherwise false.
 +   * @param version
 +   *        The version of GemFire whose DTD/schema should be used in
 +   *        the generated XML.  See {@link #VERSION_4_0}.
 +   *
 +   * @since 6.5
 +   */
 +  public static void generate(ClientCache cache, PrintWriter pw,
 +                              boolean useSchema, String version) {
 +    (new CacheXmlGenerator(cache, useSchema, version, true)).generate(pw);
 +  }
 +
 +  /**
 +   * Examines the given <code>ClientCache</code> and from it generates XML
 +   * data that is written to the given <code>PrintWriter</code>.  The
 +   * schema/dtd for the current version of GemFire is used.
 +   *
 +   * @param useSchema
 +   *        Should the generated XML reference a schema (as opposed to
 +   *        a DTD)? As of 8.1 this value is ignored and always true.
 +   */
 +  public static void generate(ClientCache cache, PrintWriter pw,
 +                              boolean useSchema) {
 +    (new CacheXmlGenerator(cache, true /*latest version always true*/, VERSION_LATEST, true)).generate(pw);
 +  }
 +
 +  /**
 +   * Examines the given <code>ClientCache</code> and from it generates XML
 +   * data that is written to the given <code>PrintWriter</code>.  The
 +   * schema/dtd for the current version of GemFire is used.
 +   *
 +   * @param useSchema
 +   *        Should the generated XML reference a schema (as opposed to
 +   *        a DTD)? As of 8.1 this value is ignored and always true.
 +   * @param includeKeysValues true if the xml should include keys and values
 +   *                          false otherwise
 +   */
 +  public static void generate(ClientCache cache, PrintWriter pw,
 +                              boolean useSchema, boolean includeKeysValues) {
 +    (new CacheXmlGenerator(cache, true /*latest version always true*/, VERSION_LATEST, includeKeysValues)).generate(pw);
 +  }
 +
 +  /**
 +   * Examines the given <code>Cache</code> and from it generates XML
 +   * data that is written to the given <code>PrintWriter</code>.
 +   */
 +  public static void generate(ClientCache cache, PrintWriter pw) {
 +    generate(cache, pw, true /* useSchema */);
 +  }
 +  /**
 +   * Writes a default cache.xml to pw.
 +   */
 +  public static void generateDefault(PrintWriter pw) {
 +    (new CacheXmlGenerator()).generate(pw);
 +  }
 +
 +
 +  ////////////////////////  Constructors  ////////////////////////
 +
 +  /**
 +   * Creates a new <code>CacheXmlGenerator</code> that generates XML
 +   * for a given <code>Cache</code>.
 +   */
 +  private CacheXmlGenerator(Cache cache, boolean useSchema,
 +                            String version, boolean includeKeysValues) {
 +    this(cache, useSchema, version, includeKeysValues, true);
 +  }
 +  private CacheXmlGenerator(Cache cache, boolean useSchema,
 +        String version, boolean includeKeysValues, boolean generateDefaults) {
 +    this.cache = cache;
 +    this.useSchema = useSchema;
 +    this.version = CacheXmlVersion.valueForVersion(version);
 +    this.includeKeysValues = includeKeysValues;
 +    this.generateDefaults = generateDefaults;
 +
 +    if (cache instanceof CacheCreation) {
 +      this.creation = (CacheCreation) cache;
 +      this.creation.startingGenerate();
 +
 +    } else if (cache instanceof GemFireCacheImpl) {
 +      if (((GemFireCacheImpl)cache).isClient()) {
 +        this.creation = new ClientCacheCreation();
 +        if (generateDefaults() || cache.getCopyOnRead()) {
 +          this.creation.setCopyOnRead(cache.getCopyOnRead());
 +        }
 +      } else {
 +        // if we are not generating defaults then create the CacheCreation for parsing
 +        // so that we can fetch the actual PoolManager and not a fake.
 +        this.creation = new CacheCreation(!generateDefaults);
 +        if (generateDefaults() || cache.getLockLease() != GemFireCacheImpl.DEFAULT_LOCK_LEASE) {
 +          this.creation.setLockLease(cache.getLockLease());
 +        }
 +        if (generateDefaults() || cache.getLockTimeout() != GemFireCacheImpl.DEFAULT_LOCK_TIMEOUT) {
 +          this.creation.setLockTimeout(cache.getLockTimeout());
 +        }
 +        if (generateDefaults() || cache.getSearchTimeout() != GemFireCacheImpl.DEFAULT_SEARCH_TIMEOUT) {
 +          this.creation.setSearchTimeout(cache.getSearchTimeout());
 +        }
 +        if (generateDefaults() || cache.isServer()) {
 +          this.creation.setIsServer(cache.isServer());
 +        }
 +        if (generateDefaults() || cache.getCopyOnRead()) {
 +          this.creation.setCopyOnRead(cache.getCopyOnRead());
 +        }
 +      }
 +    } else {
 +      // if we are not generating defaults then create the CacheCreation for parsing
 +      // so that we can fetch the actual PoolManager and not a fake.
 +      this.creation = new CacheCreation(!generateDefaults);
 +      if (generateDefaults() || cache.getLockLease() != GemFireCacheImpl.DEFAULT_LOCK_LEASE) {
 +        this.creation.setLockLease(cache.getLockLease());
 +      }
 +      if (generateDefaults() || cache.getLockTimeout() != GemFireCacheImpl.DEFAULT_LOCK_TIMEOUT) {
 +        this.creation.setLockTimeout(cache.getLockTimeout());
 +      }
 +      if (generateDefaults() || cache.getSearchTimeout() != GemFireCacheImpl.DEFAULT_SEARCH_TIMEOUT) {
 +        this.creation.setSearchTimeout(cache.getSearchTimeout());
 +      }
 +      if (generateDefaults() || cache.isServer()) {
 +        this.creation.setIsServer(cache.isServer());
 +      }
 +      if (generateDefaults() || cache.getCopyOnRead()) {
 +        this.creation.setCopyOnRead(cache.getCopyOnRead());
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Creates a new <code>CacheXmlGenerator</code> that generates XML
 +   * for a given <code>ClientCache</code>.
 +   */
 +  private CacheXmlGenerator(ClientCache cache, boolean useSchema,
 +                            String version, boolean includeKeysValues) {
 +    this.cache = (Cache)cache;
 +    this.useSchema = useSchema;
 +    this.version = CacheXmlVersion.valueForVersion(version);
 +    this.includeKeysValues = includeKeysValues;
 +    this.generateDefaults = true;
 +
 +    if (cache instanceof ClientCacheCreation) {
 +      this.creation = (ClientCacheCreation) cache;
 +      this.creation.startingGenerate();
 +
 +    } else {
 +      this.creation = new ClientCacheCreation();
 +      if (generateDefaults() || cache.getCopyOnRead()) {
 +        this.creation.setCopyOnRead(cache.getCopyOnRead());
 +      }
 +    }
 +  }
 +  /**
 +   * return true if default values should be generated.
 +   */
 +  private boolean generateDefaults() {
 +    return this.generateDefaults;
 +  }
 +  /**
 +   * Creates a generator for a default cache.
 +   */
 +  private CacheXmlGenerator() {
 +    this.cache = null;
 +    this.useSchema = true;
 +    this.version = CacheXmlVersion.valueForVersion(VERSION_LATEST);
 +    this.generateDefaults = true;
 +
 +    this.creation = new CacheCreation();
 +    creation.setLockLease(GemFireCacheImpl.DEFAULT_LOCK_LEASE);
 +    creation.setLockTimeout(GemFireCacheImpl.DEFAULT_LOCK_TIMEOUT);
 +    creation.setSearchTimeout(GemFireCacheImpl.DEFAULT_SEARCH_TIMEOUT);
 +    // No cache proxy
 +    creation.setIsServer(false);
 +    creation.setCopyOnRead(GemFireCacheImpl.DEFAULT_COPY_ON_READ);
 +  }
 +
 +  //////////////////////  Instance Methods  //////////////////////
 +
 +  /**
 +   * Writes the generator's state to pw
 +   */
 +  private void generate(PrintWriter pw) {
 +    // Use JAXP's transformation API to turn SAX events into pretty
 +    // XML text
 +    try {
 +      Source src = new SAXSource(this, new InputSource());
 +      Result res = new StreamResult(pw);
 +
 +      TransformerFactory xFactory = TransformerFactory.newInstance();
 +      Transformer xform = xFactory.newTransformer();
 +      xform.setOutputProperty(OutputKeys.METHOD, "xml");
 +      xform.setOutputProperty(OutputKeys.INDENT, "yes");
 +      if (!useSchema) {
 +        // set the doctype system and public ids from version for older DTDs.
 +        xform.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, version.getSystemId());
 +        xform.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, version.getPublicId());
 +      }
 +      xform.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
 +      xform.transform(src, res);
 +      pw.flush();
 +
 +    } catch (Exception ex) {
 +      RuntimeException ex2 = new RuntimeException(LocalizedStrings.CacheXmlGenerator_AN_EXCEPTION_WAS_THROWN_WHILE_GENERATING_XML.toLocalizedString());
 +      ex2.initCause(ex);
 +      throw ex2;
 +    }
 +  }
 +
 +  /**
 +   * Called by the transformer to parse the "input source".  We ignore
 +   * the input source and, instead, generate SAX events to the {@link
 +   * #setContentHandler ContentHandler}.
 +   */
 +  public void parse(InputSource input) throws SAXException {
 +    Assert.assertTrue(this.handler != null);
 +
 +    boolean isClientCache = this.creation instanceof ClientCacheCreation;
 +
 +    handler.startDocument();
 +
 +    AttributesImpl atts = new AttributesImpl();
 +    if (this.useSchema) {
 +      if (null == version.getSchemaLocation()) {
 +        // TODO jbarrett - localize
 +        throw new IllegalStateException("No schema for version " + version.getVersion());
 +      }
 +      // add schema location for cache schema.
 +      handler.startPrefixMapping(W3C_XML_SCHEMA_INSTANCE_PREFIX, W3C_XML_SCHEMA_INSTANCE_NS_URI);
 +      addAttribute(atts, W3C_XML_SCHEMA_INSTANCE_PREFIX, W3C_XML_SCHEMA_INSTANCE_ATTRIBUTE_SCHEMA_LOCATION, NAMESPACE + " " + version.getSchemaLocation());
 +      
 +      // add cache schema to default prefix.
 +      handler.startPrefixMapping(XmlConstants.DEFAULT_PREFIX, NAMESPACE);
 +      addAttribute(atts, VERSION, this.version.getVersion());
 +    }
 +
 +    // Don't generate XML for attributes that are not set.
 +
 +    if (creation.hasLockLease()) {
 +      atts.addAttribute("", "", LOCK_LEASE, "",
 +                        String.valueOf(creation.getLockLease()));
 +    }
 +    if (creation.hasLockTimeout()) {
 +      atts.addAttribute("", "", LOCK_TIMEOUT, "",
 +                        String.valueOf(creation.getLockTimeout()));
 +    }
 +    if (creation.hasSearchTimeout()) {
 +      atts.addAttribute("", "", SEARCH_TIMEOUT, "",
 +                        String.valueOf(creation.getSearchTimeout()));
 +    }
 +    if (this.version.compareTo(CacheXmlVersion.VERSION_5_5) >= 0) {
 +      // TODO
 +    }
 +    if (this.version.compareTo(CacheXmlVersion.VERSION_5_7) >= 0) {
 +      // TODO
 +    }
 +    if (this.version.compareTo(CacheXmlVersion.VERSION_5_8) >= 0) {
 +      // TODO
 +    }
 +    if (this.version.compareTo(CacheXmlVersion.VERSION_6_0) >= 0) {
 +      // TODO
 +    }
 +    if (this.version.compareTo(CacheXmlVersion.VERSION_6_5) >= 0) {
 +      // TODO
 +    }
 +    if (this.version.compareTo(CacheXmlVersion.VERSION_7_0) >= 0) {
 +      // TODO
 +    }
 +    if (this.version.compareTo(CacheXmlVersion.VERSION_7_0) >= 0) {
 +      // TODO
 +    }
 +    if (this.version.compareTo(CacheXmlVersion.VERSION_5_1) >= 0) {
 +      if (creation.hasMessageSyncInterval()) {
 +        atts.addAttribute("", "", MESSAGE_SYNC_INTERVAL, "", String
 +            .valueOf(creation.getMessageSyncInterval()));
 +      }
 +    }
 +    if (this.version.compareTo(CacheXmlVersion.VERSION_4_0) >= 0) {
 +      if (creation.hasServer()) {
 +        atts.addAttribute("", "", IS_SERVER, "",
 +                          String.valueOf(creation.isServer()));
 +      }
 +      if (creation.hasCopyOnRead()) {
 +        atts.addAttribute("", "", COPY_ON_READ, "",
 +            String.valueOf(creation.getCopyOnRead()));
 +      }      
 +    }
 +    if (isClientCache) {
 +      handler.startElement("", CLIENT_CACHE, CLIENT_CACHE, atts);
 +    } else {
 +      handler.startElement("", CACHE, CACHE, atts);
 +    }
 +    if (this.cache != null) {
 +      if (!isClientCache) {
 +          generate(this.cache.getCacheTransactionManager());
 +      } else if(this.version.compareTo(CacheXmlVersion.VERSION_6_6) >= 0) {
 +        generate(this.cache.getCacheTransactionManager());
 +      }
 +
 +      generateDynamicRegionFactory(this.cache);
 +
 +      if (!isClientCache) {
 +        if (this.version.compareTo(CacheXmlVersion.VERSION_7_0) >= 0) {
 +          Set<GatewaySender> senderSet = cache.getGatewaySenders();
 +          for (GatewaySender sender : senderSet) {
 +            generateGatewaySender(sender);
 +          }
 +          generateGatewayReceiver(this.cache);
 +          generateAsyncEventQueue(this.cache);
 +        }
 +      }
 +      
 +      if (!isClientCache && this.version.compareTo(CacheXmlVersion.VERSION_7_0) >= 0) {
 +        if (this.cache.getGatewayConflictResolver() != null) {
 +          generate(GATEWAY_CONFLICT_RESOLVER, this.cache.getGatewayConflictResolver());
 +        }
 +      }
 +
 +      if (!isClientCache) {
 +        for (Iterator iter = this.cache.getCacheServers().iterator();
 +             iter.hasNext(); ) {
 +          CacheServer bridge = (CacheServer) iter.next();
 +          generate(bridge);
 +        }
 +      }
 +      
 +      if (this.version.compareTo(CacheXmlVersion.VERSION_5_7) >= 0) {
 +        Iterator pools;
 +        if (this.cache instanceof GemFireCacheImpl) {
 +          pools = PoolManager.getAll().values().iterator();
 +        } else {
 +          pools = this.creation.getPools().values().iterator();
 +        }
 +        while (pools.hasNext()) {
 +          Pool cp = (Pool)pools.next();
 +          generate(cp);
 +        }
 +      }
 +      
 +      if (this.version.compareTo(CacheXmlVersion.VERSION_6_5) >= 0) {
 +        if (this.cache instanceof GemFireCacheImpl) {
 +          GemFireCacheImpl gfc = (GemFireCacheImpl)this.cache;
 +          for (DiskStore ds: gfc.listDiskStores()) {
 +            generate(ds);
 +          }
 +        } else {
 +          for (DiskStore ds: this.creation.listDiskStores()) {
 +            generate(ds);
 +          }
 +        }
 +      }
 +      if(this.version.compareTo(CacheXmlVersion.VERSION_6_6) >= 0) {
 +        generatePdx();
 +      }
 +      
 +      if (this.version.compareTo(CacheXmlVersion.VERSION_4_1) >= 0) {
 +        Map namedAttributes = this.cache.listRegionAttributes();
 +        for (Iterator iter = namedAttributes.entrySet().iterator();
 +             iter.hasNext(); ) {
 +          Map.Entry entry = (Map.Entry) iter.next();
 +          String id = (String) entry.getKey();
 +          RegionAttributes attrs = (RegionAttributes) entry.getValue();
 +          // Since CacheCreation predefines these even in later versions
 +          // we need to exclude them in all versions.
 +          // It would be better if CacheCreation could only predefine them
 +          // for versions 6.5 and later but that is not easy to do
 +          /*if (this.version.compareTo(CacheXmlVersion.VERSION_6_5) >= 0)*/ {
 +            if (this.creation instanceof ClientCacheCreation) {
 +              try {
 +                ClientRegionShortcut.valueOf(id);
 +                // skip this guy since id mapped to one of the enum types
 +                continue; 
 +              } catch (IllegalArgumentException ignore) {
 +                // id is not a shortcut so go ahead and call generate
 +              }
 +            } else {
 +              try {
 +                RegionShortcut.valueOf(id);
 +                // skip this guy since id mapped to one of the enum types
 +                continue; 
 +              } catch (IllegalArgumentException ignore) {
 +                // id is not a shortcut so go ahead and call generate
 +              }
 +            }
 +          }
 +          generate(id, attrs);
 +        }
 +      }
 +
 +      if (cache instanceof GemFireCacheImpl) { 
 +    	  generateRegions(); 
 +      }
 +      else { 
 +        TreeSet rSet = new TreeSet(new RegionComparator());
 +        rSet.addAll(this.cache.rootRegions());
 +        Iterator it = rSet.iterator();
 +    	  while (it.hasNext()) { 
 +    		  Region root = (Region)it.next(); 
 +    		  generateRegion(root); 
 +    	  }
 +      }
 +
 +      if (this.version.compareTo(CacheXmlVersion.VERSION_5_8) >= 0) {
 +        generateFunctionService();
 +      }
 +      if (this.version.compareTo(CacheXmlVersion.VERSION_6_0) >= 0) {
 +        generateResourceManager();
 +        generateSerializerRegistration();
 +      }
 +      if (!isClientCache) {
 +        if (this.version.compareTo(CacheXmlVersion.VERSION_6_5) >= 0) {
 +          if (this.cache instanceof GemFireCacheImpl) {
 +            GemFireCacheImpl gfc = (GemFireCacheImpl)this.cache;
 +            for (File file : gfc.getBackupFiles()) {
 +              generateBackupFile(file);
 +            }
 +          } else {
 +            for (File file: this.creation.getBackupFiles()) {
 +              generateBackupFile(file);
 +            }
 +          }
 +        }
 +      }
 +      if(this.version.compareTo(CacheXmlVersion.VERSION_6_6) >= 0) {
 +        generateInitializer();
 +      }
 +    } else {
 +      if (handler instanceof LexicalHandler) {
 +        //LexicalHandler lex = (LexicalHandler) handler;
 +       //lex.comment(comment.toCharArray(), 0, comment.length());
 +      }
 +
 +    }
 +    
 +    if (cache instanceof Extensible) {
 +      @SuppressWarnings("unchecked")
 +      final Extensible<Cache> extensible = (Extensible<Cache>) cache;
 +      generate(extensible);
 +    }
 +    
 +    if (isClientCache) {
 +      handler.endElement("", CLIENT_CACHE, CLIENT_CACHE);
 +    } else {
 +      handler.endElement("", CACHE, CACHE);
 +    }
 +    handler.endDocument();
 +  }
 +
 +  private void generatePdx() throws SAXException {
 +    AttributesImpl atts = new AttributesImpl();
 +    CacheConfig config;
 +    if(this.cache instanceof CacheCreation) {
 +      config = ((CacheCreation) cache).getCacheConfig();
 +    } else {
 +      config = ((GemFireCacheImpl) cache).getCacheConfig();
 +    }
 +    if(config.pdxReadSerializedUserSet) {
 +      if (generateDefaults() || this.cache.getPdxReadSerialized())
 +      atts.addAttribute("", "", READ_SERIALIZED, "", Boolean.toString(this.cache.getPdxReadSerialized()));
 +    }
 +    if(config.pdxIgnoreUnreadFieldsUserSet) {
 +      if (generateDefaults() || this.cache.getPdxIgnoreUnreadFields())
 +      atts.addAttribute("", "", IGNORE_UNREAD_FIELDS, "", Boolean.toString(this.cache.getPdxIgnoreUnreadFields()));
 +    }
 +    if(config.pdxPersistentUserSet) {
 +      if (generateDefaults() || this.cache.getPdxPersistent())
 +      atts.addAttribute("", "", PERSISTENT, "", Boolean.toString(this.cache.getPdxPersistent()));
 +    }
 +    if(config.pdxDiskStoreUserSet) {
 +      if (generateDefaults() || this.cache.getPdxDiskStore() != null && !this.cache.getPdxDiskStore().equals(""))
 +      atts.addAttribute("", "", DISK_STORE_NAME, "", this.cache.getPdxDiskStore());
 +    }
 +    if(!generateDefaults() && this.cache.getPdxSerializer() == null && atts.getLength() == 0) {
 +      return;
 +    }
 +    handler.startElement("", PDX, PDX, atts);
 +    
 +    if(this.cache.getPdxSerializer() != null) {
 +      generate(PDX_SERIALIZER, this.cache.getPdxSerializer());
 +    }
 +    handler.endElement("", PDX, PDX);
 +  }
 +  
 +  private void generateInitializer() throws SAXException {
 +    if (this.cache.getInitializer() != null) {
 +      generate(INITIALIZER, this.cache.getInitializer(), this.cache.getInitializerProps());
 +    }
 +  }
 +
 +  private void generateRegion(Region root) throws SAXException {
 +    if (this.version.compareTo(CacheXmlVersion.VERSION_5_0) >= 0) {
 +      generate(root, REGION);
 +    }
 +    else {
 +      generate(root, VM_ROOT_REGION);
 +    }
 +  }
 +  
 +  private void generateRegions() throws SAXException {
 +    Set<Region> colocatedChildRegions = new HashSet<Region>();
 +    Set<Region> generatedRegions = new HashSet<Region>();
 +
 +    //Merge from persist_Nov10 - iterate the regions in order for persistent recovery.
 +    TreeSet rSet = new TreeSet(new RegionComparator());
 +    rSet.addAll(this.cache.rootRegions());
 +    Iterator it = rSet.iterator();
 +    while (it.hasNext()) {
 +      Region root = (Region)it.next();
 +      Assert.assertTrue(root instanceof LocalRegion);
 +      if (root instanceof PartitionedRegion) {
 +        PartitionedRegion pr = (PartitionedRegion)root;
 +        if (pr.getColocatedWith() != null) {
 +          colocatedChildRegions.add(root);
 +        }
 +        else {
 +          generateRegion(root); // normal PR or root in colocated chain
 +          generatedRegions.add(root);
 +        }
 +      }
 +      else {
 +        // normal non pr regions, but they can have PR as subregions
 +        boolean found = false;
 +        for (Object object : root.subregions(false)) {
 +          Region subregion = (Region)object;
 +          Assert.assertTrue(subregion instanceof LocalRegion);
 +          if (subregion instanceof PartitionedRegion) {
 +            PartitionedRegion pr = (PartitionedRegion)subregion;
 +            if (pr.getColocatedWith() != null) {
 +              colocatedChildRegions.add(root);
 +              found = true;
 +              break;
 +            }
 +          }
 +        }
 +        if (!found) {
 +          generateRegion(root); // normal non pr regions
 +          generatedRegions.add(root);
 +        }
 +      }
 +    }
 +    TreeSet rColSet = new TreeSet(new RegionComparator());
 +    rColSet.addAll(colocatedChildRegions);
 +    Iterator colIter = rColSet.iterator();
 +    while (colIter.hasNext()) {
 +      Region root = (Region) colIter.next();
 +      Assert.assertTrue(root instanceof LocalRegion);
 +      if (root instanceof PartitionedRegion) {
 +        PartitionedRegion pr = (PartitionedRegion)root;
 +        PartitionedRegion colocatedWithPr = ColocationHelper
 +            .getColocatedRegion(pr);
 +        if (colocatedWithPr != null
 +            && !generatedRegions.contains(colocatedWithPr)) {
 +          generateRegion(colocatedWithPr);
 +          generatedRegions.add(colocatedWithPr);
 +        }
 +        if (!generatedRegions.contains(root)) {
 +          generateRegion(root);
 +          generatedRegions.add(root);
 +        }
 +      }
 +      else {
 +        generateRegion(root);
 +        generatedRegions.add(root);
 +      }
 +    }
 +  }
 +  /**
 +   * Generate a resource-manager element 
 +   */
 +  private void generateResourceManager() throws SAXException {
 +    AttributesImpl atts = new AttributesImpl();
 +    if (this.cache instanceof CacheCreation && this.creation.hasResourceManager()) {
 +      boolean generateIt = false;
 +      if (this.creation.getResourceManager().hasCriticalHeap()) {
 +        float chp = this.creation.getResourceManager().getCriticalHeapPercentage();
 +        if (generateDefaults() || chp != MemoryThresholds.DEFAULT_CRITICAL_PERCENTAGE) {
 +        atts.addAttribute("", "", CRITICAL_HEAP_PERCENTAGE, "",
 +            String.valueOf(chp));
 +        generateIt = true;
 +        }
 +      }
 +      if (this.creation.getResourceManager().hasEvictionHeap()) {
 +        float ehp = this.creation.getResourceManager().getEvictionHeapPercentage();
 +        if (generateDefaults() || ehp != MemoryThresholds.DEFAULT_EVICTION_PERCENTAGE) {
 +        atts.addAttribute("", "", EVICTION_HEAP_PERCENTAGE, "",
 +            String.valueOf(ehp));
 +        generateIt = true;
 +        }
 +      }
 +      
 +      if (this.version.compareTo(CacheXmlVersion.VERSION_9_0) >= 0) {
 +        if (this.creation.getResourceManager().hasCriticalOffHeap()) {
 +          float chp = this.creation.getResourceManager().getCriticalOffHeapPercentage();
 +          if (generateDefaults() || chp != MemoryThresholds.DEFAULT_CRITICAL_PERCENTAGE) {
 +            atts.addAttribute("", "", CRITICAL_OFF_HEAP_PERCENTAGE, "", String.valueOf(chp));
 +            generateIt = true;
 +          }
 +        }
 +        if (this.creation.getResourceManager().hasEvictionOffHeap()) {
 +          float ehp = this.creation.getResourceManager().getEvictionOffHeapPercentage();
 +          if (generateDefaults() || ehp != MemoryThresholds.DEFAULT_EVICTION_PERCENTAGE) {
 +            atts.addAttribute("", "", EVICTION_OFF_HEAP_PERCENTAGE, "", String.valueOf(ehp));
 +            generateIt = true;
 +          }
 +        }
 +      }
 +      if (generateIt) {
 +        generateResourceManagerElement(atts);
 +      }
 +    } else if (this.cache instanceof GemFireCacheImpl) {
 +      {
 +        int chp = (int)this.cache.getResourceManager().getCriticalHeapPercentage();
 +        if (generateDefaults() || chp != MemoryThresholds.DEFAULT_CRITICAL_PERCENTAGE)
 +
 +        atts.addAttribute("", "", CRITICAL_HEAP_PERCENTAGE, "",
 +            String.valueOf(chp));
 +      }
 +      {
 +        int ehp = (int)this.cache.getResourceManager().getEvictionHeapPercentage();
 +        if (generateDefaults() || ehp != MemoryThresholds.DEFAULT_EVICTION_PERCENTAGE)
 +        atts.addAttribute("", "", EVICTION_HEAP_PERCENTAGE, "",
 +            String.valueOf(ehp));
 +      }
 +      
 +      if (this.version.compareTo(CacheXmlVersion.VERSION_9_0) >= 0) {
 +        {
 +          int chp = (int)this.cache.getResourceManager().getCriticalOffHeapPercentage();
 +          if (generateDefaults() || chp != MemoryThresholds.DEFAULT_CRITICAL_PERCENTAGE)
 +  
 +          atts.addAttribute("", "", CRITICAL_OFF_HEAP_PERCENTAGE, "",
 +              String.valueOf(chp));
 +        }
 +        {
 +          int ehp = (int)this.cache.getResourceManager().getEvictionOffHeapPercentage();
 +          if (generateDefaults() || ehp != MemoryThresholds.DEFAULT_EVICTION_PERCENTAGE)
 +          atts.addAttribute("", "", EVICTION_OFF_HEAP_PERCENTAGE, "",
 +              String.valueOf(ehp));
 +        }
 +      }
 +      if (generateDefaults() || atts.getLength() > 0)
 +      generateResourceManagerElement(atts);
 +    }
 +  }
 +  private void generateResourceManagerElement(AttributesImpl atts) throws SAXException {
 +    handler.startElement("", RESOURCE_MANAGER, RESOURCE_MANAGER, atts);
 +    handler.endElement("", RESOURCE_MANAGER, RESOURCE_MANAGER);
 +  }
 +  
 +  private void generateBackupFile(File file) throws SAXException {
 +    handler.startElement("", BACKUP, BACKUP, EMPTY);
 +    handler.characters(file.getPath().toCharArray(), 0, file.getPath().length());
 +    handler.endElement("", BACKUP, BACKUP);
 +  }
 +
 +  /**
 +   * Generates the <code>serializer-registration</code> element.
 +   * @throws SAXException
 +   */
 +  private void generateSerializerRegistration() throws SAXException {
 +    final SerializerCreation sc = this.creation.getSerializerCreation();
 +    if(sc == null){
 +      return;
 +    }
 +    
 +    handler.startElement("", TOP_SERIALIZER_REGISTRATION, TOP_SERIALIZER_REGISTRATION, EMPTY);
 +    for(Class c : sc.getSerializerRegistrations()) {
 +      handler.startElement("", SERIALIZER_REGISTRATION, SERIALIZER_REGISTRATION, EMPTY);
 +      handler.startElement("", CLASS_NAME, CLASS_NAME, EMPTY);
 +      handler.characters(c.getName().toCharArray(), 0, c.getName().length());
 +      handler.endElement("", CLASS_NAME, CLASS_NAME);
 +      handler.endElement("", SERIALIZER_REGISTRATION, SERIALIZER_REGISTRATION);
 +    }
 +    
 +    for(Map.Entry<Class, Integer> e : sc.getInstantiatorRegistrations().entrySet()) {
 +      Class c = e.getKey();
 +      Integer i = e.getValue();
 +      
 +      AttributesImpl atts = new AttributesImpl();
 +      atts.addAttribute("", "", ID, "", i.toString());
 +      handler.startElement("", INSTANTIATOR_REGISTRATION, INSTANTIATOR_REGISTRATION, atts);
 +      handler.startElement("", CLASS_NAME, CLASS_NAME, EMPTY);
 +      handler.characters(c.getName().toCharArray(), 0, c.getName().length());
 +      handler.endElement("", CLASS_NAME, CLASS_NAME);
 +      handler.endElement("", INSTANTIATOR_REGISTRATION, INSTANTIATOR_REGISTRATION);      
 +    }
 +    
 +    handler.endElement("", TOP_SERIALIZER_REGISTRATION, TOP_SERIALIZER_REGISTRATION);
 +  }
 +
 +  /**
 +   * @throws SAXException 
 +   */
 +  private void generateFunctionService() throws SAXException {
 +    Map<String, Function> functions = FunctionService.getRegisteredFunctions();
 +    if (!generateDefaults() && functions.isEmpty()) {
 +      return;
 +    }
 +    handler.startElement("", FUNCTION_SERVICE, FUNCTION_SERVICE, EMPTY);
 +    for (Function function : functions.values()) {
 +      if (function instanceof Declarable) {
 +        handler.startElement("", FUNCTION, FUNCTION, EMPTY);
 +        generate((Declarable) function, false);
 +        handler.endElement("", FUNCTION, FUNCTION);
 +      }
 +    }
 +    handler.endElement("", FUNCTION_SERVICE, FUNCTION_SERVICE);
 +  }
 +
 +  /**
 +   * Generates XML for the client-subscription tag
 +   * @param bridge instance of <code>CacheServer</code>
 +   * 
 +   * @since 5.7
 +   */
 +  
 +  private void generateClientHaQueue(CacheServer bridge) throws SAXException {
 +    AttributesImpl atts = new AttributesImpl();
 +    ClientSubscriptionConfigImpl csc = (ClientSubscriptionConfigImpl)bridge.getClientSubscriptionConfig();
 +    try {
 +        atts.addAttribute("", "", CLIENT_SUBSCRIPTION_EVICTION_POLICY, "", csc.getEvictionPolicy());
 +        atts.addAttribute("", "", CLIENT_SUBSCRIPTION_CAPACITY, "", String.valueOf(csc.getCapacity()));
 +        if (this.version.compareTo(CacheXmlVersion.VERSION_6_5) >= 0) {
 +          String dsVal = csc.getDiskStoreName();
 +          if (dsVal != null) {
 +            atts.addAttribute("", "", DISK_STORE_NAME, "", dsVal);
 +          }
 +        }
 +        if (csc.getDiskStoreName() == null && csc.hasOverflowDirectory()) {
 +          atts.addAttribute("", "", OVERFLOW_DIRECTORY, "", csc.getOverflowDirectory());
 +        }
 +        handler.startElement("", CLIENT_SUBSCRIPTION, CLIENT_SUBSCRIPTION, atts);
 +        handler.endElement("", CLIENT_SUBSCRIPTION, CLIENT_SUBSCRIPTION);
 +      
 +    } catch (Exception ex) {
 +      ex.printStackTrace();
 +    }     
 +  }
 +
 +  /**
 +   * Generates XML for the given cache server
 +   *
 +   * @since 4.0
 +   */
 +  private void generate(CacheServer bridge) throws SAXException {
 +    if (this.version.compareTo(CacheXmlVersion.VERSION_4_0) < 0) {
 +      return;
 +    }
 +    AttributesImpl atts = new AttributesImpl();
 +    try {
 +      if (generateDefaults() || bridge.getPort() != CacheServer.DEFAULT_PORT)
 +      atts.addAttribute("", "", PORT, "",
 +          String.valueOf(bridge.getPort()));
 +      
 +      if (this.version.compareTo(CacheXmlVersion.VERSION_4_1) < 0) {
 +        return;
 +      }
 +      if (generateDefaults() || bridge.getMaximumTimeBetweenPings() != CacheServer.DEFAULT_MAXIMUM_TIME_BETWEEN_PINGS)
 +      atts.addAttribute("", "", MAXIMUM_TIME_BETWEEN_PINGS, "",
 +          String.valueOf(bridge.getMaximumTimeBetweenPings()));
 +
 +      if (generateDefaults() || bridge.getNotifyBySubscription() != CacheServer.DEFAULT_NOTIFY_BY_SUBSCRIPTION)
 +      atts.addAttribute("", "", NOTIFY_BY_SUBSCRIPTION, "",
 +          String.valueOf(bridge.getNotifyBySubscription()));
 +
 +      if (generateDefaults() || bridge.getSocketBufferSize() != CacheServer.DEFAULT_SOCKET_BUFFER_SIZE)
 +      atts.addAttribute("", "", SOCKET_BUFFER_SIZE, "",
 +          String.valueOf(bridge.getSocketBufferSize()));
 +      
 +      if (this.version.compareTo(CacheXmlVersion.VERSION_5_0) < 0) {
 +        return;
 +      }
 +      
 +      if (generateDefaults() || bridge.getMaxConnections() != CacheServer.DEFAULT_MAX_CONNECTIONS)
 +      atts.addAttribute("", "", MAX_CONNECTIONS, "",
 +          String.valueOf(bridge.getMaxConnections()));
 +      
 +      if (this.version.compareTo(CacheXmlVersion.VERSION_5_1) < 0) {
 +        return;
 +      }
 +
 +      if (generateDefaults() || bridge.getMaxThreads() != CacheServer.DEFAULT_MAX_THREADS)
 +      atts.addAttribute("", "", MAX_THREADS, "",
 +          String.valueOf(bridge.getMaxThreads()));
 +      if (generateDefaults() || bridge.getMaximumMessageCount() != CacheServer.DEFAULT_MAXIMUM_MESSAGE_COUNT)
 +      atts.addAttribute("", "", MAXIMUM_MESSAGE_COUNT, "",
 +          String.valueOf(bridge.getMaximumMessageCount()));
 +      
 +      if (generateDefaults() || bridge.getMessageTimeToLive() != CacheServer.DEFAULT_MESSAGE_TIME_TO_LIVE)
 +      atts.addAttribute("", "", MESSAGE_TIME_TO_LIVE, "",
 +          String.valueOf(bridge.getMessageTimeToLive()));
 +      
 +      
 +      
 +      if (this.version.compareTo(CacheXmlVersion.VERSION_5_7) < 0) {
 +        return;
 +      }
 +      
 +      if(bridge.getBindAddress() != null) {
 +        if (generateDefaults() || bridge.getBindAddress() != CacheServer.DEFAULT_BIND_ADDRESS)
 +        atts.addAttribute("","",BIND_ADDRESS,"",bridge.getBindAddress());
 +      }
 +  
 +      if (bridge.getHostnameForClients() != null
 +          && !bridge.getHostnameForClients().equals("")) {
 +        atts.addAttribute("", "", HOSTNAME_FOR_CLIENTS, "", bridge.getHostnameForClients());
 +      }
 +      if (generateDefaults() || bridge.getLoadPollInterval() != CacheServer.DEFAULT_LOAD_POLL_INTERVAL)
 +      atts.addAttribute("", "", LOAD_POLL_INTERVAL, "", String.valueOf(bridge.getLoadPollInterval()));
 +      
 +      if (this.version.compareTo(CacheXmlVersion.VERSION_8_0) < 0) {
 +        return;
 +      }
 +
 +      if (generateDefaults() || bridge.getTcpNoDelay() != CacheServer.DEFAULT_TCP_NO_DELAY) {
 +        atts.addAttribute("", "", TCP_NO_DELAY, "", ""+bridge.getTcpNoDelay());
 +      }
 +
 +    } finally {
 +      if (this.version.compareTo(CacheXmlVersion.VERSION_5_7) >= 0) {
 +        handler.startElement("", CACHE_SERVER, CACHE_SERVER, atts);
 +      } else {
 +        handler.startElement("", BRIDGE_SERVER, BRIDGE_SERVER, atts);
 +      }
 +      
 +      if (this.version.compareTo(CacheXmlVersion.VERSION_5_7) >= 0) {
 +        String[] groups = bridge.getGroups();
 +        if (groups.length > 0) {
 +          for (int i = 0; i < groups.length; i++) {
 +            String group = groups[i];
 +            handler.startElement("", GROUP, GROUP, EMPTY);
 +            handler.characters(group.toCharArray(), 0, group.length());
 +            handler.endElement("", GROUP, GROUP);
 +          }
 +        }
 +        
 +        if(!bridge.getClientSubscriptionConfig().getEvictionPolicy().equals("none")){
 +          generateClientHaQueue(bridge);
 +        }
 +        
 +        ServerLoadProbe probe = bridge.getLoadProbe();
 +        if (generateDefaults() || !probe.equals(CacheServer.DEFAULT_LOAD_PROBE)) {
 +          generate(LOAD_PROBE, probe);
 +        }
 +        
 +        
 +      }
 +      if (this.version.compareTo(CacheXmlVersion.VERSION_5_7) >= 0) {
 +        handler.endElement("", "", CACHE_SERVER);
 +      } else {
 +        handler.endElement("", "", BRIDGE_SERVER);
 +      }
 +    } 
 +  }
 +  
 +  /**
 +   * Generates XML for the given disk store
 +   *
 +   * @since prPersistSprint2
 +   */
 +  private void generate(DiskStore ds) throws SAXException {
 +    if (this.version.compareTo(CacheXmlVersion.VERSION_6_5) < 0) {
 +      return;
 +    }
 +    AttributesImpl atts = new AttributesImpl();
 +    try {
 +      atts.addAttribute("", "", NAME, "", ds.getName());
 +
 +      if ((!(ds instanceof DiskStoreAttributesCreation) ||
 +          ((DiskStoreAttributesCreation) ds).hasAutoCompact())) {
 +        if (generateDefaults() || ds.getAutoCompact() != DiskStoreFactory.DEFAULT_AUTO_COMPACT)
 +        atts.addAttribute("", "", AUTO_COMPACT, "", 
 +            String.valueOf(ds.getAutoCompact()));
 +      }
 +
 +      if ((!(ds instanceof DiskStoreAttributesCreation) ||
 +          ((DiskStoreAttributesCreation) ds).hasAllowForceCompaction())) {
 +        if (generateDefaults() || ds.getAllowForceCompaction() != DiskStoreFactory.DEFAULT_ALLOW_FORCE_COMPACTION)
 +        atts.addAttribute("", "", ALLOW_FORCE_COMPACTION, "", 
 +            String.valueOf(ds.getAllowForceCompaction()));
 +      }
 +
 +      if ((!(ds instanceof DiskStoreAttributesCreation) ||
 +          ((DiskStoreAttributesCreation) ds).hasCompactionThreshold())) {
 +        if (generateDefaults() || ds.getCompactionThreshold() != DiskStoreFactory.DEFAULT_COMPACTION_THRESHOLD)
 +        atts.addAttribute("", "", COMPACTION_THRESHOLD, "", 
 +            String.valueOf(ds.getCompactionThreshold()));
 +      }
 +      
 +      if ((!(ds instanceof DiskStoreAttributesCreation) ||
 +          ((DiskStoreAttributesCreation) ds).hasMaxOplogSize())) {
 +        if (generateDefaults() || ds.getMaxOplogSize() != DiskStoreFactory.DEFAULT_MAX_OPLOG_SIZE)
 +        atts.addAttribute("", "", MAX_OPLOG_SIZE, "", 
 +            String.valueOf(ds.getMaxOplogSize()));
 +      }
 +
 +      if ((!(ds instanceof DiskStoreAttributesCreation) ||
 +          ((DiskStoreAttributesCreation) ds).hasTimeInterval())) {
 +        if (generateDefaults() || ds.getTimeInterval() != DiskStoreFactory.DEFAULT_TIME_INTERVAL)
 +        atts.addAttribute("", "", TIME_INTERVAL, "", 
 +            String.valueOf(ds.getTimeInterval()));
 +      }
 +
 +      if ((!(ds instanceof DiskStoreAttributesCreation) ||
 +          ((DiskStoreAttributesCreation) ds).hasWriteBufferSize())) {
 +        if (generateDefaults() || ds.getWriteBufferSize() != DiskStoreFactory.DEFAULT_WRITE_BUFFER_SIZE)
 +        atts.addAttribute("", "", WRITE_BUFFER_SIZE, "", 
 +            String.valueOf(ds.getWriteBufferSize()));
 +      }
 +      
 +      if ((!(ds instanceof DiskStoreAttributesCreation) ||
 +          ((DiskStoreAttributesCreation) ds).hasQueueSize())) {
 +        if (generateDefaults() || ds.getQueueSize() != DiskStoreFactory.DEFAULT_QUEUE_SIZE)
 +        atts.addAttribute("", "", QUEUE_SIZE, "", 
 +            String.valueOf(ds.getQueueSize()));
 +      }
 +      
 +      if (this.version.compareTo(CacheXmlVersion.VERSION_8_0) >= 0) {
 +        if ((!(ds instanceof DiskStoreAttributesCreation) ||
 +            ((DiskStoreAttributesCreation) ds).hasDiskUsageWarningPercentage())) {
 +          if (generateDefaults() || ds.getDiskUsageWarningPercentage() != DiskStoreFactory.DEFAULT_DISK_USAGE_WARNING_PERCENTAGE)
 +          atts.addAttribute("", "", DISK_USAGE_WARNING_PERCENTAGE, "", 
 +              String.valueOf(ds.getDiskUsageWarningPercentage()));
 +        }
 +        
 +        if ((!(ds instanceof DiskStoreAttributesCreation) ||
 +            ((DiskStoreAttributesCreation) ds).hasDiskUsageCriticalPercentage())) {
 +          if (generateDefaults() || ds.getDiskUsageCriticalPercentage() != DiskStoreFactory.DEFAULT_DISK_USAGE_CRITICAL_PERCENTAGE)
 +          atts.addAttribute("", "", DISK_USAGE_CRITICAL_PERCENTAGE, "", 
 +              String.valueOf(ds.getDiskUsageCriticalPercentage()));
 +        }
 +      }
 +    } finally {
 +      handler.startElement("", DISK_STORE, DISK_STORE, atts);
 +      
 +      if ((!(ds instanceof DiskStoreAttributesCreation) ||
 +          ((DiskStoreAttributesCreation) ds).hasDiskDirs())) {
 +        File[] diskDirs = ds.getDiskDirs();
 +        int[] diskSizes = ds.getDiskDirSizes();
 +        if (diskDirs != null && diskDirs.length > 0) {
 +          if (generateDefaults() || !Arrays.equals(diskDirs, DiskStoreFactory.DEFAULT_DISK_DIRS)
 +              || !Arrays.equals(diskSizes, DiskStoreFactory.DEFAULT_DISK_DIR_SIZES)) {
 +          handler.startElement("", DISK_DIRS, DISK_DIRS, EMPTY);
 +          for (int i = 0; i < diskDirs.length; i++) {
 +            AttributesImpl diskAtts = new AttributesImpl();
 +            if (diskSizes[i] != DiskStoreFactory.DEFAULT_DISK_DIR_SIZE) {
 +              diskAtts.addAttribute("", "", DIR_SIZE, "", String
 +                  .valueOf(diskSizes[i]));
 +            }
 +            handler.startElement("", DISK_DIR, DISK_DIR, diskAtts);
 +            File dir = diskDirs[i];
 +            String name = generateDefaults() ? dir.getAbsolutePath() : dir.getPath();
 +            handler.characters(name.toCharArray(), 0, name.length());
 +            handler.endElement("", DISK_DIR, DISK_DIR);
 +          }
 +          handler.endElement("", DISK_DIRS, DISK_DIRS);
 +          }
 +        }
 +      }
 +
 +      handler.endElement("", "", DISK_STORE);
 +    } 
 +  }
 +  
 +  /** Compare regions by name 
 +   * 
 +   * @author lynn
 +   *
 +   */
 +  class RegionComparator implements Comparator {
 +    public int compare(Object o1, Object o2) {
 +       return (((Region)o1).getFullPath().compareTo(((Region)o2).getFullPath()));
 +    }
 +    public boolean equals(Object anObj) {
 +       return ((Region)this).getFullPath().equals(((Region)anObj).getFullPath());
 +    }
 + }
 +
 +
 +  /**
 +   * Generates XML for the given connection pool
 +   *
 +   * @since 5.7
 +   */
 +  private void generate(Pool cp) throws SAXException {
 +    if (this.version.compareTo(CacheXmlVersion.VERSION_5_7) < 0) {
 +      return;
 +    }
 +    if (((PoolImpl)cp).isUsedByGateway()) {
 +      // no need to generate xml for gateway pools
 +      return;
 +    }
 +    AttributesImpl atts = new AttributesImpl();
 +    try {
 +      atts.addAttribute("", "", NAME, "", cp.getName());
 +      if (generateDefaults() || cp.getFreeConnectionTimeout() != PoolFactory.DEFAULT_FREE_CONNECTION_TIMEOUT)
 +      atts.addAttribute("", "", FREE_CONNECTION_TIMEOUT, "",
 +                        String.valueOf(cp.getFreeConnectionTimeout()));
 +      if (generateDefaults() || cp.getLoadConditioningInterval() != PoolFactory.DEFAULT_LOAD_CONDITIONING_INTERVAL)
 +      atts.addAttribute("", "", LOAD_CONDITIONING_INTERVAL, "",
 +                        String.valueOf(cp.getLoadConditioningInterval()));
 +      if (generateDefaults() || cp.getMinConnections() != PoolFactory.DEFAULT_MIN_CONNECTIONS)
 +      atts.addAttribute("", "", MIN_CONNECTIONS, "",
 +                        String.valueOf(cp.getMinConnections()));
 +      if (generateDefaults() || cp.getMaxConnections() != PoolFactory.DEFAULT_MAX_CONNECTIONS)
 +      atts.addAttribute("", "", MAX_CONNECTIONS, "",
 +          String.valueOf(cp.getMaxConnections()));
 +      if (generateDefaults() || cp.getRetryAttempts() != PoolFactory.DEFAULT_RETRY_ATTEMPTS)
 +      atts.addAttribute("", "", RETRY_ATTEMPTS, "",
 +          String.valueOf(cp.getRetryAttempts()));
 +      if (generateDefaults() || cp.getIdleTimeout() != PoolFactory.DEFAULT_IDLE_TIMEOUT)
 +      atts.addAttribute("", "", IDLE_TIMEOUT, "",
 +          String.valueOf(cp.getIdleTimeout()));
 +      if (generateDefaults() || cp.getPingInterval() != PoolFactory.DEFAULT_PING_INTERVAL)
 +      atts.addAttribute("", "", PING_INTERVAL, "",
 +          String.valueOf(cp.getPingInterval()));
 +      if (generateDefaults() || cp.getStatisticInterval() != PoolFactory.DEFAULT_STATISTIC_INTERVAL)
 +      atts.addAttribute("", "", STATISTIC_INTERVAL, "",
 +          String.valueOf(cp.getStatisticInterval()));
 +      if (generateDefaults() || cp.getSubscriptionAckInterval() != PoolFactory.DEFAULT_SUBSCRIPTION_ACK_INTERVAL)
 +      atts.addAttribute("", "", SUBSCRIPTION_ACK_INTERVAL, "",
 +          String.valueOf(cp.getSubscriptionAckInterval()));
 +      if (generateDefaults() || cp.getSubscriptionEnabled() != PoolFactory.DEFAULT_SUBSCRIPTION_ENABLED)
 +      atts.addAttribute("", "", SUBSCRIPTION_ENABLED, "",
 +                        String.valueOf(cp.getSubscriptionEnabled()));
 +      if (generateDefaults() || cp.getSubscriptionMessageTrackingTimeout() != PoolFactory.DEFAULT_SUBSCRIPTION_MESSAGE_TRACKING_TIMEOUT)
 +      atts.addAttribute("", "", SUBSCRIPTION_MESSAGE_TRACKING_TIMEOUT, "",
 +                        String.valueOf(cp.getSubscriptionMessageTrackingTimeout()));
 +      if (generateDefaults() || cp.getSubscriptionRedundancy() != PoolFactory.DEFAULT_SUBSCRIPTION_REDUNDANCY)
 +      atts.addAttribute("", "", SUBSCRIPTION_REDUNDANCY, "",
 +                        String.valueOf(cp.getSubscriptionRedundancy()));
 +      if (generateDefaults() || cp.getReadTimeout() != PoolFactory.DEFAULT_READ_TIMEOUT)
 +      atts.addAttribute("", "", READ_TIMEOUT, "",
 +                        String.valueOf(cp.getReadTimeout()));
 +      if (cp.getServerGroup() != null && !cp.getServerGroup().equals("")) {
 +        atts.addAttribute("", "", SERVER_GROUP, "", cp.getServerGroup());
 +      }
 +      if (generateDefaults() || cp.getSocketBufferSize() != PoolFactory.DEFAULT_SOCKET_BUFFER_SIZE)
 +      atts.addAttribute("", "", SOCKET_BUFFER_SIZE, "",
 +                        String.valueOf(cp.getSocketBufferSize()));
 +      if (generateDefaults() || cp.getThreadLocalConnections() != PoolFactory.DEFAULT_THREAD_LOCAL_CONNECTIONS)
 +      atts.addAttribute("", "", THREAD_LOCAL_CONNECTIONS, "",
 +                        String.valueOf(cp.getThreadLocalConnections()));
 +
 +      if (this.version.compareTo(CacheXmlVersion.VERSION_6_1) > 0) {
 +        if (generateDefaults() || cp.getPRSingleHopEnabled() != PoolFactory.DEFAULT_PR_SINGLE_HOP_ENABLED)
 +        atts.addAttribute("", "", PR_SINGLE_HOP_ENABLED, "",
 +            String.valueOf(cp.getPRSingleHopEnabled()));  
 +      }
 +
 +      if (this.version.compareTo(CacheXmlVersion.VERSION_6_1) > 0) {
 +        if (generateDefaults() || cp.getMultiuserAuthentication() != PoolFactory.DEFAULT_MULTIUSER_AUTHENTICATION)
 +        atts.addAttribute("", "", MULTIUSER_SECURE_MODE_ENABLED, "", String.valueOf(cp
 +            .getMultiuserAuthentication()));
 +      }
 +    } finally {
 +      handler.startElement("", CONNECTION_POOL, CONNECTION_POOL, atts);
 +      {
 +        Iterator/*<InetSocketAddress>*/ locators = cp.getLocators().iterator();
 +        while (locators.hasNext()) {
 +          InetSocketAddress addr = (InetSocketAddress)locators.next();
 +          AttributesImpl sAtts = new AttributesImpl();
 +          sAtts.addAttribute("", "", HOST, "", addr.getHostName());
 +          sAtts.addAttribute("", "", PORT, "", String.valueOf(addr.getPort()));
 +          handler.startElement("", LOCATOR, LOCATOR, sAtts);
 +          handler.endElement("", LOCATOR, LOCATOR);
 +        }
 +      }
 +      {
 +        Iterator/*<InetSocketAddress>*/ servers = cp.getServers().iterator();
 +        while (servers.hasNext()) {
 +          InetSocketAddress addr = (InetSocketAddress)servers.next();
 +          AttributesImpl sAtts = new AttributesImpl();
 +          sAtts.addAttribute("", "", HOST, "", addr.getHostName());
 +          sAtts.addAttribute("", "", PORT, "", String.valueOf(addr.getPort()));
 +          handler.startElement("", SERVER, SERVER, sAtts);
 +          handler.endElement("", SERVER, SERVER);
 +        }
 +      }
 +      handler.endElement("", "", CONNECTION_POOL);
 +    } 
 +  }
 +
 +  /**
 +   * Generates XML for a CacheTransactionManager
 +   *
 +   * @since 4.0
 +   */
 +  private void generate(CacheTransactionManager txMgr) throws SAXException {
 +    if (this.version.compareTo(CacheXmlVersion.VERSION_4_0) < 0) {
 +      return;
 +    }
 +
 +    if (txMgr == null) {
 +      return;
 +    }
 +    if (!generateDefaults() && txMgr.getWriter() == null
 +        && txMgr.getListeners().length == 0) {
 +      return;
 +    }
 +
 +    handler.startElement("", TRANSACTION_MANAGER, TRANSACTION_MANAGER, EMPTY);
 +    {
 +      TransactionListener[] listeners = txMgr.getListeners();
 +      for (int i=0; i < listeners.length; i++) {
 +        generate(TRANSACTION_LISTENER, listeners[i]);
 +      }
 +      if(txMgr.getWriter()!=null) {
 +        generate(TRANSACTION_WRITER, txMgr.getWriter());
 +      }
 +    }
 +    handler.endElement("", TRANSACTION_MANAGER, TRANSACTION_MANAGER);
 +  }
 +
 +  /**
 +   * Generates XML for a DynamicRegionFactory.Config
 +   *
 +   * @since 4.3
 +   */
 +  private void generateDynamicRegionFactory(Cache c) throws SAXException {
 +    if (this.version.compareTo(CacheXmlVersion.VERSION_4_1) < 0) {
 +      return;
 +    }
 +    DynamicRegionFactory.Config cfg;
 +    if (c instanceof CacheCreation) {
 +      cfg = ((CacheCreation)c).getDynamicRegionFactoryConfig();
 +    } else {
 +      DynamicRegionFactory drf = DynamicRegionFactory.get();
 +      if (drf == null || drf.isClosed()) {
 +        return;
 +      }
 +      cfg = drf.getConfig();
 +    }
 +    if (cfg == null) {
 +      return;
 +    }
 +    AttributesImpl atts = new AttributesImpl();
 +    if (!cfg.getPersistBackup())
 +      atts.addAttribute("", "", DISABLE_PERSIST_BACKUP, "", "true");
 +    if (!cfg.getRegisterInterest())
 +      atts.addAttribute("", "", DISABLE_REGISTER_INTEREST, "", "true");
 +    if(cfg.getPoolName() != null) {
 +      atts.addAttribute("", "", POOL_NAME, "", cfg.getPoolName());
 +    }
 +    handler.startElement("", DYNAMIC_REGION_FACTORY, DYNAMIC_REGION_FACTORY, atts);
 +    {
 +      File dir = cfg.getDiskDir();
 +      if (dir != null) {
 +        handler.startElement("", DISK_DIR, DISK_DIR, EMPTY);
 +        String name = generateDefaults() ? dir.getAbsolutePath() : dir.getPath();
 +        handler.characters(name.toCharArray(), 0, name.length());
 +        handler.endElement("", DISK_DIR, DISK_DIR);
 +      }
 +    }
 +    handler.endElement("", DYNAMIC_REGION_FACTORY, DYNAMIC_REGION_FACTORY);
 +  }
 +
 +  private void generateGatewaySender(GatewaySender sender) throws SAXException {
 +      AttributesImpl atts = new AttributesImpl();
 +      // id
 +      atts.addAttribute("", "", ID, "", sender.getId());
 +      // remote-distributed-system
 +      atts.addAttribute("", "", REMOTE_DISTRIBUTED_SYSTEM_ID, "", String
 +        .valueOf(sender.getRemoteDSId()));
 +      // parallel
 +      if (generateDefaults() || sender.isParallel() != GatewaySender.DEFAULT_IS_PARALLEL)
 +      atts.addAttribute("", "", PARALLEL, "", String.valueOf(sender.isParallel()));
 +      // manual-start
 +      if (generateDefaults() || sender.isManualStart() != GatewaySender.DEFAULT_MANUAL_START)
 +      atts.addAttribute("", "", MANUAL_START, "", String.valueOf(sender.isManualStart()));
 +      // socket-buffer-size
 +      if (generateDefaults() || sender.getSocketBufferSize() != GatewaySender.DEFAULT_SOCKET_BUFFER_SIZE)
 +      atts.addAttribute("", "", SOCKET_BUFFER_SIZE, "", String.valueOf(sender
 +          .getSocketBufferSize()));
 +      // socket-read-timeout
 +      if (generateDefaults() || sender.getSocketReadTimeout() != GatewaySender.DEFAULT_SOCKET_READ_TIMEOUT)
 +      atts.addAttribute("", "", SOCKET_READ_TIMEOUT, "", String.valueOf(sender
 +          .getSocketReadTimeout()));
 +      // enable-batch-conflation
 +      if (generateDefaults() || sender.isBatchConflationEnabled() != GatewaySender.DEFAULT_BATCH_CONFLATION)
 +      atts.addAttribute("", "", ENABLE_BATCH_CONFLATION, "", String.valueOf(sender
 +          .isBatchConflationEnabled())); // Should we use ENABLE-CONFLATION
 +      // batch-size
 +      if (generateDefaults() || sender.getBatchSize() != GatewaySender.DEFAULT_BATCH_SIZE)
 +      atts.addAttribute("", "", BATCH_SIZE, "", String.valueOf(sender
 +          .getBatchSize()));
 +      // batch-time-interval
 +      if (generateDefaults() || sender.getBatchTimeInterval() != GatewaySender.DEFAULT_BATCH_TIME_INTERVAL)
 +      atts.addAttribute("", "", BATCH_TIME_INTERVAL, "", String.valueOf(sender
 +          .getBatchTimeInterval()));
 +      // enable-persistence
 +      if (generateDefaults() || sender.isPersistenceEnabled() != GatewaySender.DEFAULT_PERSISTENCE_ENABLED)
 +      atts.addAttribute("", "", ENABLE_PERSISTENCE, "", String.valueOf(sender
 +          .isPersistenceEnabled()));
 +      // disk-store-name
 +      if (generateDefaults() || sender.getDiskStoreName() != null && !sender.getDiskStoreName().equals(""))
 +      atts.addAttribute("", "", DISK_STORE_NAME, "", String.valueOf(sender
 +          .getDiskStoreName()));
 +      // disk-synchronous
 +      if (generateDefaults() || sender.isDiskSynchronous() != GatewaySender.DEFAULT_DISK_SYNCHRONOUS)
 +      atts.addAttribute("", "", DISK_SYNCHRONOUS, "", String.valueOf(sender
 +          .isDiskSynchronous()));
 +      // maximum-queue-memory
 +      if (generateDefaults() || sender.getMaximumQueueMemory() != GatewaySender.DEFAULT_MAXIMUM_QUEUE_MEMORY)
 +      atts.addAttribute("", "", MAXIMUM_QUEUE_MEMORY, "", String.valueOf(sender
 +          .getMaximumQueueMemory()));
 +      // alert-threshold
 +      if (generateDefaults() || sender.getAlertThreshold() != GatewaySender.DEFAULT_ALERT_THRESHOLD)
 +      atts.addAttribute("", "", ALERT_THRESHOLD, "", String.valueOf(sender
 +          .getAlertThreshold()));
 +
 +      // dispatcher-threads
 +      if (generateDefaults() || sender.getDispatcherThreads() != GatewaySender.DEFAULT_DISPATCHER_THREADS)
 +      atts.addAttribute("", "", DISPATCHER_THREADS, "", String.valueOf(sender
 +          .getDispatcherThreads()));
 +      // order-policy
 +      if (sender.getOrderPolicy() != null) {
 +        if (generateDefaults() || !sender.getOrderPolicy().equals(GatewaySender.DEFAULT_ORDER_POLICY))
 +        atts.addAttribute("", "", ORDER_POLICY, "", String.valueOf(sender
 +          .getOrderPolicy()));
 +      }
 +      
 +      handler.startElement("", GATEWAY_SENDER, GATEWAY_SENDER, atts);
 +      
 +      for (GatewayEventFilter gef : sender.getGatewayEventFilters()) {
 +         generateGatewayEventFilter(gef);
 +      }
 +
++      if (this.version.compareTo(CacheXmlVersion.VERSION_8_0) >= 0) {
++        if (sender.getGatewayEventSubstitutionFilter() != null) {
++          generateGatewayEventSubstitutionFilter(sender.getGatewayEventSubstitutionFilter());
++        }
++      }
++
 +      for (GatewayTransportFilter gsf : sender.getGatewayTransportFilters()) {
 +        generateGatewayTransportFilter(gsf);
 +     }
 +
 +      handler.endElement("", GATEWAY_SENDER, GATEWAY_SENDER);
 +  }
 +
 +  private void generateAsyncEventQueue(Cache cache) throws SAXException {
 +    Set<AsyncEventQueue> asyncEventQueues = cache.getAsyncEventQueues();
 +    for (AsyncEventQueue asyncEventQueue : asyncEventQueues) {
 +      AttributesImpl atts = new AttributesImpl();
 +      // id
 +      atts.addAttribute("", "", ID, "", asyncEventQueue.getId());
 +      // parallel
 +      if (generateDefaults() || asyncEventQueue.isParallel() != GatewaySender.DEFAULT_IS_PARALLEL)
 +      atts.addAttribute("", "", PARALLEL, "", String.valueOf(asyncEventQueue.isParallel()));
 +      // batch-size
 +      if (generateDefaults() || asyncEventQueue.getBatchSize() != GatewaySender.DEFAULT_BATCH_SIZE)
 +      atts.addAttribute("", "", BATCH_SIZE, "", String.valueOf(asyncEventQueue
 +        .getBatchSize()));
 +      // batch-time-interval
 +      if (generateDefaults() || asyncEventQueue.getBatchTimeInterval() != GatewaySender.DEFAULT_BATCH_TIME_INTERVAL)
 +      atts.addAttribute("", "", BATCH_TIME_INTERVAL, "", String.valueOf(asyncEventQueue
 +          .getBatchTimeInterval()));
 +      // enable-batch-conflation
 +      if (generateDefaults() || asyncEventQueue.isBatchConflationEnabled() != GatewaySender.DEFAULT_BATCH_CONFLATION)
 +      atts.addAttribute("", "", ENABLE_BATCH_CONFLATION, "", String.valueOf(asyncEventQueue
 +          .isBatchConflationEnabled()));
 +      // maximum-queue-memory
 +      if (generateDefaults() || asyncEventQueue.getMaximumQueueMemory() != GatewaySender.DEFAULT_MAXIMUM_QUEUE_MEMORY)
 +      atts.addAttribute("", "", MAXIMUM_QUEUE_MEMORY, "", String.valueOf(asyncEventQueue
 +        .getMaximumQueueMemory()));
 +      // enable-persistence
 +      if (generateDefaults() || asyncEventQueue.isPersistent() != GatewaySender.DEFAULT_PERSISTENCE_ENABLED)
 +      atts.addAttribute("", "", PERSISTENT, "", String.valueOf(asyncEventQueue
 +        .isPersistent()));
 +      if (asyncEventQueue.isPersistent()) {
 +        //disk-store-name
 +        if (generateDefaults() || (asyncEventQueue.getDiskStoreName() != null && !asyncEventQueue.getDiskStoreName().equals("")))
 +        atts.addAttribute("", "", DISK_STORE_NAME, "", String.valueOf(asyncEventQueue
 +            .getDiskStoreName()));
 +      }
 +      // dispatcher-threads
 +      if (generateDefaults() || asyncEventQueue.getDispatcherThreads() != GatewaySender.DEFAULT_DISPATCHER_THREADS)
 +      atts.addAttribute("", "", DISPATCHER_THREADS, "", String.valueOf(asyncEventQueue
 +          .getDispatcherThreads()));
 +      // order-policy
 +      if (asyncEventQueue.getOrderPolicy() != null) {
 +        if (generateDefaults() || !asyncEventQueue.getOrderPolicy().equals(GatewaySender.DEFAULT_ORDER_POLICY))
 +        atts.addAttribute("", "", ORDER_POLICY, "", String.valueOf(asyncEventQueue
 +          .getOrderPolicy()));
 +      }
 +      // disk-synchronous
 +      if (generateDefaults() || asyncEventQueue.isDiskSynchronous() != GatewaySender.DEFAULT_DISK_SYNCHRONOUS)
 +      atts.addAttribute("", "", DISK_SYNCHRONOUS, "", String.valueOf(asyncEventQueue
 +          .isDiskSynchronous()));
 +      handler.startElement("", ASYNC_EVENT_QUEUE, ASYNC_EVENT_QUEUE, atts);
 +    
 +      List<GatewayEventFilter> eventFilters = asyncEventQueue.getGatewayEventFilters();
 +      if (eventFilters != null) {
 +    	  for (GatewayEventFilter eventFilter : eventFilters) {
 +    		generateGatewayEventFilter(eventFilter);
 +    	  }
 +      }
-       
++
++      if (this.version.compareTo(CacheXmlVersion.VERSION_8_0) >= 0) {
++        if (asyncEventQueue.getGatewayEventSubstitutionFilter() != null) {
++          generateGatewayEventSubstitutionFilter(asyncEventQueue.getGatewayEventSubstitutionFilter());
++        }
++      }
++
 +      AsyncEventListener asyncListener = asyncEventQueue.getAsyncEventListener();
 +      if (asyncListener != null) {
 +        generate(ASYNC_EVENT_LISTENER, asyncListener);
 +      }
 +      
 +      handler.endElement("", ASYNC_EVENT_QUEUE, ASYNC_EVENT_QUEUE);
 +    }
 +}
 +
 +  
 +  private void generateGatewayReceiver(Cache cache) throws SAXException {
 +    Set<GatewayReceiver> receiverList = cache.getGatewayReceivers();
 +    for (GatewayReceiver receiver : receiverList) {
 +      AttributesImpl atts = new AttributesImpl();
 +      try {
 +        // start port
 +        if (generateDefaults()
 +            || receiver.getStartPort() != GatewayReceiver.DEFAULT_START_PORT)
 +          atts.addAttribute("", "", START_PORT, "",
 +              String.valueOf(receiver.getStartPort()));
 +        // end port
 +        if (generateDefaults()
 +            || receiver.getEndPort() != GatewayReceiver.DEFAULT_END_PORT)
 +          atts.addAttribute("", "", END_PORT, "",
 +              String.valueOf(receiver.getEndPort()));
 +        // bind-address
 +        if (generateDefaults()
 +            || (receiver.getBindAddress() != null && !receiver.getBindAddress()
 +                .equals(GatewayReceiver.DEFAULT_BIND_ADDRESS)))
 +          atts.addAttribute("", "", BIND_ADDRESS, "", receiver.getBindAddress());
 +        // maximum-time-between-pings
 +        if (generateDefaults()
 +            || receiver.getMaximumTimeBetweenPings() != GatewayReceiver.DEFAULT_MAXIMUM_TIME_BETWEEN_PINGS)
 +          atts.addAttribute("", "", MAXIMUM_TIME_BETWEEN_PINGS, "",
 +              String.valueOf(receiver.getMaximumTimeBetweenPings()));
 +        // socket-buffer-size
 +        if (generateDefaults()
 +            || receiver.getSocketBufferSize() != GatewayReceiver.DEFAULT_SOCKET_BUFFER_SIZE)
 +          atts.addAttribute("", "", SOCKET_BUFFER_SIZE, "",
 +              String.valueOf(receiver.getSocketBufferSize()));
 +
 +        if (this.version.compareTo(CacheXmlVersion.VERSION_8_0) < 0) {
 +          return;
 +        }
 +        // manual-start
 +        if (generateDefaults()
 +            || receiver.isManualStart() != GatewayReceiver.DEFAULT_MANUAL_START)
 +          atts.addAttribute("", "", MANUAL_START, "",
 +              String.valueOf(receiver.isManualStart()));
 +
 +      }
 +      finally {
 +        handler.startElement("", GATEWAY_RECEIVER, GATEWAY_RECEIVER, atts);
 +        for (GatewayTransportFilter gsf : receiver.getGatewayTransportFilters()) {
 +          generateGatewayTransportFilter(gsf);
 +        }
 +        handler.endElement("", GATEWAY_RECEIVER, GATEWAY_RECEIVER);
 +      }
 +    }
 +  }
 +  
 +  private void generateGatewayEventFilter(GatewayEventFilter gef)
 +      throws SAXException {
 +
 +    handler.startElement("", GATEWAY_EVENT_FILTER, GATEWAY_EVENT_FILTER, EMPTY);
 +    String className = gef.getClass().getName();
 +
 +    handler.startElement("", CLASS_NAME, CLASS_NAME, EMPTY);
 +    handler.characters(className.toCharArray(), 0, className.length());
 +    handler.endElement("", CLASS_NAME, CLASS_NAME);
 +    Properties props = null;
 +    if (gef instanceof Declarable2) {
 +      props = ((Declarable2)gef).getConfig();
 +      generate(props, null);
 +    }
 +    handler.endElement("", GATEWAY_EVENT_FILTER, GATEWAY_EVENT_FILTER);
 +  }
 +
 +  private void generateGatewayTransportFilter(GatewayTransportFilter gef)
 +      throws SAXException {
 +
 +    handler.startElement("", GATEWAY_TRANSPORT_FILTER, GATEWAY_TRANSPORT_FILTER,
 +        EMPTY);
 +    String className = gef.getClass().getName();
 +
 +    handler.startElement("", CLASS_NAME, CLASS_NAME, EMPTY);
 +    handler.characters(className.toCharArray(), 0, className.length());
 +    handler.endElement("", CLASS_NAME, CLASS_NAME);
 +    Properties props = null;
 +    if (gef instanceof Declarable2) {
 +      props = ((Declarable2)gef).getConfig();
 +      generate(props, null);
 +    }
 +    handler.endElement("", GATEWAY_TRANSPORT_FILTER, GATEWAY_TRANSPORT_FILTER);
 +  }
++
++  private void generateGatewayEventSubstitutionFilter(GatewayEventSubstitutionFilter filter)
++      throws SAXException {
++
++    handler.startElement("", GATEWAY_EVENT_SUBSTITUTION_FILTER, GATEWAY_EVENT_SUBSTITUTION_FILTER,
++        EMPTY);
++    String className = filter.getClass().getName();
++
++    handler.startElement("", CLASS_NAME, CLASS_NAME, EMPTY);
++    handler.characters(className.toCharArray(), 0, className.length());
++    handler.endElement("", CLASS_NAME, CLASS_NAME);
++    Properties props = null;
++    if (filter instanceof Declarable2) {
++      props = ((Declarable2)filter).getConfig();
++      generate(props, null);
++    }
++    handler.endElement("", GATEWAY_EVENT_SUBSTITUTION_FILTER, GATEWAY_EVENT_SUBSTITUTION_FILTER);
++  }
 +//
 +//  private void generateGatewayEventListener(GatewayEventListener gef)
 +//      throws SAXException {
 +//
 +//    handler.startElement("", GATEWAY_EVENT_LISTENER, GATEWAY_EVENT_LISTENER,
 +//        EMPTY);
 +//    String className = gef.getClass().getName();
 +//
 +//    handler.startElement("", CLASS_NAME, CLASS_NAME, EMPTY);
 +//    handler.characters(className.toCharArray(), 0, className.length());
 +//    handler.endElement("", CLASS_NAME, CLASS_NAME);
 +//    Properties props = null;
 +//    if (gef instanceof Declarable2) {
 +//      props = ((Declarable2)gef).getConfig();
 +//      generate(props, null);
 +//    }
 +//    handler.endElement("", GATEWAY_EVENT_LISTENER, GATEWAY_EVENT_LISTENER);
 +//  }
 +  
 +  /**
 +   * Generates XML for a given region
 +   */
 +  private void generate(Region region, String elementName) throws SAXException {
 +    if (region == null) {
 +      return;
 +    }
 +
 +    AttributesImpl atts = new AttributesImpl();
 +    atts.addAttribute("", "", NAME, "", region.getName());
 +    if (region instanceof RegionCreation) {
 +      RegionCreation rc = (RegionCreation)region;
 +      String refId = rc.getRefid();
 +      if (refId != null) {
 +        atts.addAttribute("", "", REFID, "", refId);
 +      }
 +    }
 +    handler.startElement("", elementName, elementName, atts);
 +
 +    if (region instanceof RegionCreation) {
 +      RegionCreation rc = (RegionCreation)region;
 +      if (rc.hasAttributes()) {
 +        generate(null /* unknown id */, region.getAttributes());
 +      }
 +    } else {
 +      generate(null /* unknown id */, region.getAttributes());
 +    }
 +    
 +    //generate index data here
 +    Collection indexesForRegion = this.cache.getQueryService().getIndexes(region);
 +    if (indexesForRegion != null) {
 +      for (Object index: indexesForRegion) {
 +        generate((Index)index);
 +      }
 +    }
 +
 +    if (region instanceof PartitionedRegion) {
 +      if (includeKeysValues) {
 +        if (!region.isEmpty()) {
 +          for (Iterator iter = region.entrySet(false).iterator(); iter.hasNext();) {
 +            Region.Entry entry = (Region.Entry)iter.next();
 +            generate(entry);
 +          }
 +        }
 +      }
 +    }
 +    else {
 +      if (includeKeysValues) {
 +        for (Iterator iter = region.entrySet(false).iterator(); iter.hasNext();) {
 +          Region.Entry entry = (Region.Entry)iter.next();
 +          generate(entry);
 +        }
 +      }
 +    }
 +    
 +    TreeSet rSet = new TreeSet(new RegionComparator());
 +    rSet.addAll(region.subregions(false));
 +    for (Iterator iter = rSet.iterator(); iter.hasNext(); ) {
 +      Region subregion = (Region) iter.next();
 +      generate(subregion, REGION);
 +    }
 +
 +    if (region instanceof Extensible) {
 +      @SuppressWarnings({ "unchecked" })
 +      Extensible<Region<?, ?>> extensible = (Extensible<Region<?, ?>>) region;
 +      generate(extensible);
 +    }
 +
 +    handler.endElement("", elementName, elementName);
 +  }
 +
 +  /**
 +   * Generates XML for a region entry
 +   */
 +  private void generate(Region.Entry entry) throws SAXException {
 +    if ((entry == null)) {
 +      return;
 +    }
 +
 +    handler.startElement("", ENTRY, ENTRY, EMPTY);
 +
 +    handler.startElement("", KEY, KEY, EMPTY);
 +    generate(entry.getKey());
 +    handler.endElement("", KEY, KEY);
 +
 +    handler.startElement("", VALUE, VALUE, EMPTY);
 +    generate(entry.getValue());
 +    handler.endElement("", VALUE, VALUE);
 +
 +    handler.endElement("", ENTRY, ENTRY);
 +  }
 +
 +  /**
 +   * Generates XML for an index
 +   */
 +  private void generate(Index index) throws SAXException {
 +    if (index == null) {
 +      return;
 +    }
 +    AttributesImpl atts = new AttributesImpl();
 +
 +    if (index instanceof IndexCreationData) {
 +      IndexCreationData indexData = (IndexCreationData) index;
 +      atts.addAttribute("", "", NAME, "", indexData.getIndexName());
 +      String indexType = indexData.getIndexType();
 +      if (indexType.equals("KEY")) {
 +        atts.addAttribute("","", KEY_INDEX, "", "true");
 +      }
 +      else {
 +        //convert the indexType to the xml indexType
 +        if (indexType.equals("HASH")) {
 +          indexType = HASH_INDEX_TYPE;
 +        }
 +        else {
 +          indexType = RANGE_INDEX_TYPE;
 +        }
 +        atts.addAttribute("","", KEY_INDEX, "", "false");
 +        atts.addAttribute("", "", INDEX_TYPE, "", "" + indexType);
 +      }
 +      atts.addAttribute("", "", FROM_CLAUSE, "", indexData.getIndexFromClause());
 +      atts.addAttribute("", "", EXPRESSION, "",indexData.getIndexExpression());
 +    }
 +    else {
 +      atts.addAttribute("", "", NAME, "", index.getName());
 +      if (index instanceof PrimaryKeyIndex) {
 +        atts.addAttribute("","", KEY_INDEX, "", "true");
 +      }
 +      else {
 +        atts.addAttribute("","", KEY_INDEX, "", "false");
 +        String indexType = "range";
 +        if (index instanceof HashIndex) {
 +          indexType = "hash";
 +        }
 +        atts.addAttribute("", "", INDEX_TYPE, "", "" + indexType);
 +      }
 +      atts.addAttribute("", "", FROM_CLAUSE, "", index.getFromClause());
 +      atts.addAttribute("", "", EXPRESSION, "",index.getIndexedExpression());
 +    }
 +    handler.startElement("", INDEX, INDEX, atts);
 +
 +    handler.endElement("", INDEX, INDEX);
 +  }
 +  
 +  /**
 +   * Generates XML for region attributes.
 +   *
 +   * @param id
 +   *        The id of the named region attributes (may be
 +   *        <code>null</code>)
 +   */
 +  private void generate(String id, RegionAttributes attrs)
 +    throws SAXException {
 +    AttributesImpl atts = new AttributesImpl();
 +
 +    if (id != null) {
 +      atts.addAttribute("", "", ID, "", id);
 +    }
 +
 +    // Unless, the attrs is a "creation" instance, 
 +    // we have no way of generating a refid, because by this
 +    // point, the refid information is lost.  
 +    if (attrs instanceof RegionAttributesCreation) {
 +      String refId = ((RegionAttributesCreation) attrs).getRefid();
 +      if (refId != null) {
 +        atts.addAttribute("", "", REFID, "", refId);
 +      }
 +    }
 +
 +    if ((!(attrs instanceof RegionAttributesCreation) ||
 +        ((RegionAttributesCreation) attrs).hasScope())) {
 +      String scopeString;
 +      Scope scope = attrs.getScope();
 +      if (scope.equals(Scope.LOCAL)) {
 +        scopeString = LOCAL;
 +
 +      } else if (scope.equals(Scope.DISTRIBUTED_NO_ACK)) {
 +        scopeString = DISTRIBUTED_NO_ACK;
 +
 +      } else if (scope.equals(Scope.DISTRIBUTED_ACK)) {
 +        scopeString = DISTRIBUTED_ACK;
 +
 +      } else if (scope.equals(Scope.GLOBAL)) {
 +        scopeString = GLOBAL;
 +
 +      } else {
 +        throw new InternalGemFireException(LocalizedStrings.CacheXmlGenerator_UNKNOWN_SCOPE_0.toLocalizedString(scope));
 +      }
 +      
 +
 +      final boolean isPartitionedRegion;
 +      if (attrs instanceof RegionAttributesCreation) {
 +        RegionAttributesCreation rac = (RegionAttributesCreation) attrs;
 +        isPartitionedRegion = rac.getPartitionAttributes() != null || 
 +          (rac.hasDataPolicy() && rac.getDataPolicy().withPartitioning());
 +      } else {
 +        isPartitionedRegion = attrs.getPartitionAttributes() != null ||
 +          attrs.getDataPolicy().withPartitioning();
 +      }
 +
 +      if ( ! isPartitionedRegion) {
 +        // Partitioned Region don't support setting scope
 +        if (generateDefaults() || !scope.equals(AbstractRegion.DEFAULT_SCOPE))
 +        atts.addAttribute("", "", SCOPE, "", scopeString);
 +      } 
 +    } // hasScope
 +
 +    if ((!(attrs instanceof RegionAttributesCreation) ||
 +         ((RegionAttributesCreation) attrs).hasEarlyAck())) {
 +      if (generateDefaults() || attrs.getEarlyAck())
 +      atts.addAttribute("", "", EARLY_ACK, "",
 +                        String.valueOf(attrs.getEarlyAck()));
 +    }
 +
 +    if ((!(attrs instanceof RegionAttributesCreation) ||
 +         ((RegionAttributesCreation) attrs).hasMulticastEnabled())) {
 +      if (generateDefaults() || attrs.getMulticastEnabled())
 +      atts.addAttribute("", "", MULTICAST_ENABLED, "",
 +                        String.valueOf(attrs.getMulticastEnabled()));
 +    }
 +    if ((!(attrs instanceof RegionAttributesCreation) ||
 +         ((RegionAttributesCreation) attrs).hasPublisher())) {
 +      if (generateDefaults() || attrs.getPublisher())
 +      atts.addAttribute("", "", PUBLISHER, "",
 +                        String.valueOf(attrs.getPublisher()));
 +    }
 +
 +    if ((!(attrs instanceof RegionAttributesCreation) ||
 +         ((RegionAttributesCreation) attrs).hasEnableAsyncConflation())) {
 +      if (generateDefaults() || attrs.getEnableAsyncConflation())
 +      atts.addAttribute("", "", ENABLE_ASYNC_CONFLATION, "",
 +                        String.valueOf(attrs.getEnableAsyncConflation()));
 +    }
 +
 +    if (this.version.compareTo(CacheXmlVersion.VERSION_5_0) >= 0) {
 +      
 +      if ((!(attrs instanceof RegionAttributesCreation) ||
 +           ((RegionAttributesCreation) attrs).hasEnableSubscriptionConflation())) {
 +        if (this.version.compareTo(CacheXmlVersion.VERSION_5_7) >= 0) {
 +          // starting with 5.7 it is enable-subscription-conflation
 +          if (generateDefaults() || attrs.getEnableSubscriptionConflation())
 +          atts.addAttribute("", "", ENABLE_SUBSCRIPTION_CONFLATION, "",
 +                            String.valueOf(attrs.getEnableSubscriptionConflation()));
 +        } else {
 +          // before 5.7 it was enable-bridge-conflation
 +          if (generateDefaults() || attrs.getEnableSubscriptionConflation())
 +          atts.addAttribute("", "", ENABLE_BRIDGE_CONFLATION, "",
 +                            String.valueOf(attrs.getEnableSubscriptionConflation()));
 +        }
 +      }
 +      
 +      if ((!(attrs instanceof RegionAttributesCreation) ||
 +           ((RegionAttributesCreation) attrs).hasDataPolicy())) {
 +        String dpString;
 +        DataPolicy dp = attrs.getDataPolicy();
 +        if (dp.isEmpty()) {
 +          dpString = EMPTY_DP;
 +        } else if (dp.isNormal()) {
 +          dpString = NORMAL_DP;
 +        } else if (dp.isPreloaded()) {
 +          dpString = PRELOADED_DP;
 +        } else if (dp.isReplicate()) {
 +          dpString = REPLICATE_DP;
 +        } else if (dp == DataPolicy.PERSISTENT_REPLICATE) {
 +          dpString = PERSISTENT_REPLICATE_DP;
 +        } else if (dp == DataPolicy.PERSISTENT_PARTITION) {
 +          dpString = PERSISTENT_PARTITION_DP;
 +        } else if (dp.isPartition()) {
 +          if (this.version.compareTo(CacheXmlVersion.VERSION_5_1) >= 0) {
 +            dpString = PARTITION_DP;
 +          }
 +          else {
 +            // prior to 5.1 the data policy for partitioned regions was EMPTY
 +            dpString = EMPTY_DP;
 +          }
 +        } else {
 +          throw new InternalGemFireException(LocalizedStrings.CacheXmlGenerator_UNKNOWN_DATA_POLICY_0.toLocalizedString(dp));
 +        }
 +
 +        if (generateDefaults() || !dp.equals(DataPolicy.DEFAULT))
 +        atts.addAttribute("", "", DATA_POLICY, "", dpString);
 +      } // hasDataPolicy
 +    } // VERSION_5_0 >= 0
 +    else { // VERSION_5_0 < 0
 +      if ((!(attrs instanceof RegionAttributesCreation) ||
 +          ((RegionAttributesCreation) attrs).hasEnableSubscriptionConflation())) {
 +       if (generateDefaults() || attrs.getEnableSubscriptionConflation())
 +       atts.addAttribute("", "", "enable-conflation", "",
 +                         String.valueOf(attrs.getEnableSubscriptionConflation()));
 +     }
 +      
 +      if ((!(attrs instanceof RegionAttributesCreation) ||
 +           ((RegionAttributesCreation) attrs).hasMirrorType())) {
 +        String mirrorString;
 +        MirrorType mirror = attrs.getMirrorType();
 +        if (mirror.equals(MirrorType.NONE))
 +          mirrorString = NONE;
 +        else if (mirror.equals(MirrorType.KEYS))
 +          mirrorString = KEYS;
 +        else if (mirror.equals(MirrorType.KEYS_VALUES))
 +          mirrorString = KEYS_VALUES;
 +        else
 +          throw new InternalGemFireException(LocalizedStrings.CacheXmlGenerator_UNKNOWN_MIRROR_TYPE_0.toLocalizedString(mirror));
 +        atts.addAttribute("", "", MIRROR_TYPE, "", mirrorString);
 +      }
 +      if ((!(attrs instanceof RegionAttributesCreation) ||
 +           ((RegionAttributesCreation) attrs).hasPersistBackup())) {
 +        atts.addAttribute("", "", PERSIST_BACKUP, "",
 +                          String.valueOf(attrs.getDataPolicy() == DataPolicy.PERSISTENT_REPLICATE));
 +      }
 +    } // VERSION_5_0 < 0
 +
 +    if ((!(attrs instanceof RegionAttributesCreation) ||
 +         ((RegionAttributesCreation) attrs).hasInitialCapacity())) {
 +      if (generateDefaults() || attrs.getInitialCapacity() != 16)
 +      atts.addAttribute("", "", INITIAL_CAPACITY, "",
 +                        String.valueOf(attrs.getInitialCapacity()));
 +    }
 +
 +    if ((!(attrs instanceof RegionAttributesCreation) ||
 +         ((RegionAttributesCreation) attrs).hasLoadFactor())) {
 +      if (generateDefaults() || attrs.getLoadFactor() != 0.75f)
 +      atts.addAttribute("", "", LOAD_FACTOR, "",
 +                        String.valueOf(attrs.getLoadFactor()));
 +    }
 +
 +    if ((!(attrs instanceof RegionAttributesCreation) ||
 +         ((RegionAttributesCreation) attrs).hasConcurrencyLevel())) {
 +      if (generateDefaults() || attrs.getConcurrencyLevel() != 16)
 +      atts.addAttribute("", "", CONCURRENCY_LEVEL, "",
 +                        String.valueOf(attrs.getConcurrencyLevel()));
 +    }
 +    
 +    if (this.version.compareTo(CacheXmlVersion.VERSION_7_0) >= 0) {
 +      if ((!(attrs instanceof RegionAttributesCreation) ||
 +          ((RegionAttributesCreation) attrs).hasConcurrencyChecksEnabled())) {
 +       if (generateDefaults() || attrs.getConcurrencyChecksEnabled() != true/*fixes bug 46654*/)
 +       atts.addAttribute("", "", CONCURRENCY_CHECKS_ENABLED, "",
 +                         String.valueOf(attrs.getConcurrencyChecksEnabled()));
 +      }
 +    }
 +   
 +    
 +
 +    if ((!(attrs instanceof RegionAttributesCreation) ||
 +         ((RegionAttributesCreation) attrs).hasStatisticsEnabled())) {
 +      if (generateDefaults() || attrs.getStatisticsEnabled())
 +      atts.addAttribute("", "", STATISTICS_ENABLED, "",
 +                        String.valueOf(attrs.getStatisticsEnabled()));
 +    }
 +
 +    if ( !(attrs instanceof RegionAttributesCreation) ||
 +         ((RegionAttributesCreation)attrs).hasIgnoreJTA() ) {
 +      if (generateDefaults() || attrs.getIgnoreJTA())
 +      atts.addAttribute("", "", IGNORE_JTA, "",
 +                        String.valueOf(attrs.getIgnoreJTA()));
 +    }
 +
 +    if (this.version.compareTo(CacheXmlVersion.VERSION_4_0) >= 0) {
 +      if ((!(attrs instanceof RegionAttributesCreation) ||
 +           ((RegionAttributesCreation) attrs).hasIsLockGrantor())) {
 +        if (generateDefaults() || attrs.isLockGrantor())
 +        atts.addAttribute("", "

<TRUNCATED>


[43/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/distributed/ServerLauncher.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/distributed/ServerLauncher.java
index c991cc1,0000000..f13bb73
mode 100644,000000..100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/distributed/ServerLauncher.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/distributed/ServerLauncher.java
@@@ -1,2605 -1,0 +1,2613 @@@
 +/*
 + * 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.distributed;
 +
 +import java.io.File;
 +import java.io.FileNotFoundException;
 +import java.io.IOException;
 +import java.lang.management.ManagementFactory;
 +import java.net.InetAddress;
 +import java.net.UnknownHostException;
 +import java.util.Arrays;
 +import java.util.Collections;
 +import java.util.HashMap;
 +import java.util.List;
 +import java.util.Map;
 +import java.util.Properties;
 +import java.util.TreeMap;
 +import java.util.concurrent.TimeUnit;
 +import java.util.concurrent.TimeoutException;
 +import java.util.concurrent.atomic.AtomicBoolean;
 +import java.util.concurrent.atomic.AtomicReference;
 +
 +import javax.management.MalformedObjectNameException;
 +import javax.management.ObjectName;
 +
 +import com.gemstone.gemfire.SystemFailure;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheFactory;
 +import com.gemstone.gemfire.cache.partition.PartitionRegionHelper;
 +import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.distributed.AbstractLauncher.Status;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.internal.GemFireVersion;
 +import com.gemstone.gemfire.internal.SocketCreator;
 +import com.gemstone.gemfire.internal.cache.AbstractCacheServer;
 +import com.gemstone.gemfire.internal.cache.CacheConfig;
 +import com.gemstone.gemfire.internal.cache.CacheServerLauncher;
 +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
 +import com.gemstone.gemfire.internal.cache.PartitionedRegion;
++import com.gemstone.gemfire.internal.cache.tier.sockets.CacheServerHelper;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.lang.ObjectUtils;
 +import com.gemstone.gemfire.internal.lang.StringUtils;
 +import com.gemstone.gemfire.internal.lang.SystemUtils;
 +import com.gemstone.gemfire.internal.process.ClusterConfigurationNotAvailableException;
 +import com.gemstone.gemfire.internal.process.ConnectionFailedException;
 +import com.gemstone.gemfire.internal.process.ControlNotificationHandler;
 +import com.gemstone.gemfire.internal.process.ControllableProcess;
 +import com.gemstone.gemfire.internal.process.FileAlreadyExistsException;
 +import com.gemstone.gemfire.internal.process.MBeanInvocationFailedException;
 +import com.gemstone.gemfire.internal.process.PidUnavailableException;
 +import com.gemstone.gemfire.internal.process.ProcessController;
 +import com.gemstone.gemfire.internal.process.ProcessControllerFactory;
 +import com.gemstone.gemfire.internal.process.ProcessControllerParameters;
 +import com.gemstone.gemfire.internal.process.ProcessLauncherContext;
 +import com.gemstone.gemfire.internal.process.ProcessType;
 +import com.gemstone.gemfire.internal.process.StartupStatusListener;
 +import com.gemstone.gemfire.internal.process.UnableToControlProcessException;
 +import com.gemstone.gemfire.internal.util.CollectionUtils;
 +import com.gemstone.gemfire.internal.util.IOUtils;
 +import com.gemstone.gemfire.lang.AttachAPINotFoundException;
 +import com.gemstone.gemfire.management.internal.cli.i18n.CliStrings;
 +import com.gemstone.gemfire.management.internal.cli.json.GfJsonArray;
 +import com.gemstone.gemfire.management.internal.cli.json.GfJsonException;
 +import com.gemstone.gemfire.management.internal.cli.json.GfJsonObject;
 +import com.gemstone.gemfire.pdx.PdxSerializer;
 +
 +import joptsimple.OptionException;
 +import joptsimple.OptionParser;
 +import joptsimple.OptionSet;
 +
 +import org.springframework.data.gemfire.support.SpringContextBootstrappingInitializer;
 +
 +/**
 + * The ServerLauncher class is a launcher class with main method to start a GemFire Server (implying a GemFire Cache
 + * Server process).
 + * 
 + * @author John Blum
 + * @author Kirk Lund
 + * @see com.gemstone.gemfire.distributed.AbstractLauncher
 + * @see com.gemstone.gemfire.distributed.LocatorLauncher
 + * @since 7.0
 + */
 +@SuppressWarnings({ "unused" })
 +public final class ServerLauncher extends AbstractLauncher<String> {
 +
 +  /**
 +   * @deprecated This is specific to the internal implementation and may go away in a future release.
 +   */
 +  protected static final Integer DEFAULT_SERVER_PORT = getDefaultServerPort();
 +
 +  private static final Map<String, String> helpMap = new HashMap<>();
 +
 +  static {
 +    helpMap.put("launcher", LocalizedStrings.ServerLauncher_SERVER_LAUNCHER_HELP.toLocalizedString());
 +    helpMap.put(Command.START.getName(), LocalizedStrings.ServerLauncher_START_SERVER_HELP.toLocalizedString(String.valueOf(getDefaultServerPort())));
 +    helpMap.put(Command.STATUS.getName(), LocalizedStrings.ServerLauncher_STATUS_SERVER_HELP.toLocalizedString());
 +    helpMap.put(Command.STOP.getName(), LocalizedStrings.ServerLauncher_STOP_SERVER_HELP.toLocalizedString());
 +    helpMap.put(Command.VERSION.getName(), LocalizedStrings.ServerLauncher_VERSION_SERVER_HELP.toLocalizedString());
 +    helpMap.put("assign-buckets", LocalizedStrings.ServerLauncher_SERVER_ASSIGN_BUCKETS_HELP.toLocalizedString());
 +    helpMap.put("debug", LocalizedStrings.ServerLauncher_SERVER_DEBUG_HELP.toLocalizedString());
 +    helpMap.put("dir", LocalizedStrings.ServerLauncher_SERVER_DIR_HELP.toLocalizedString());
 +    helpMap.put("disable-default-server", LocalizedStrings.ServerLauncher_SERVER_DISABLE_DEFAULT_SERVER_HELP.toLocalizedString());
 +    helpMap.put("force", LocalizedStrings.ServerLauncher_SERVER_FORCE_HELP.toLocalizedString());
 +    helpMap.put("help", LocalizedStrings.SystemAdmin_CAUSES_GEMFIRE_TO_PRINT_OUT_INFORMATION_INSTEAD_OF_PERFORMING_THE_COMMAND_THIS_OPTION_IS_SUPPORTED_BY_ALL_COMMANDS.toLocalizedString());
 +    helpMap.put("member", LocalizedStrings.ServerLauncher_SERVER_MEMBER_HELP.toLocalizedString());
 +    helpMap.put("pid", LocalizedStrings.ServerLauncher_SERVER_PID_HELP.toLocalizedString());
 +    helpMap.put("rebalance", LocalizedStrings.ServerLauncher_SERVER_REBALANCE_HELP.toLocalizedString());
 +    helpMap.put("redirect-output", LocalizedStrings.ServerLauncher_SERVER_REDIRECT_OUTPUT_HELP.toLocalizedString());
 +    helpMap.put("server-bind-address", LocalizedStrings.ServerLauncher_SERVER_BIND_ADDRESS_HELP.toLocalizedString());
 +    helpMap.put("hostname-for-clients", LocalizedStrings.ServerLauncher_SERVER_HOSTNAME_FOR_CLIENT_HELP.toLocalizedString());
 +    helpMap.put("server-port", LocalizedStrings.ServerLauncher_SERVER_PORT_HELP.toLocalizedString(String.valueOf(getDefaultServerPort())));
 +  }
 +
 +  private static final Map<Command, String> usageMap = new TreeMap<>();
 +
 +  static {
 +    usageMap.put(Command.START, "start <member-name> [--assign-buckets] [--disable-default-server] [--rebalance] [--server-bind-address=<IP-address>] [--server-port=<port>] [--force] [--debug] [--help]");
 +    usageMap.put(Command.STATUS, "status [--member=<member-ID/Name>] [--pid=<process-ID>] [--dir=<Server-working-directory>] [--debug] [--help]");
 +    usageMap.put(Command.STOP, "stop [--member=<member-ID/Name>] [--pid=<process-ID>] [--dir=<Server-working-directory>] [--debug] [--help]");
 +    usageMap.put(Command.VERSION, "version");
 +  }
 +
 +  /**
 +   * @deprecated This is specific to the internal implementation and may go away in a future release.
 +   */
 +  public static final String DEFAULT_SERVER_PID_FILE = "vf.gf.server.pid";
 +
 +  private static final String DEFAULT_SERVER_LOG_EXT = ".log";
 +  private static final String DEFAULT_SERVER_LOG_NAME = "gemfire";
 +  private static final String SERVER_SERVICE_NAME = "Server";
 +
 +  private static final AtomicReference<ServerLauncher> INSTANCE = new AtomicReference<>();
 +
 +  private volatile transient boolean debug;
 +
 +  private final transient ControlNotificationHandler controlHandler;
 +  
 +  private final AtomicBoolean starting = new AtomicBoolean(false);
 +
 +  private final boolean assignBuckets;
 +  private final boolean disableDefaultServer;
 +  private final boolean force;
 +  private final boolean help;
 +  private final boolean rebalance;
 +  private final boolean redirectOutput;
 +
 +  private volatile transient Cache cache;
 +
 +  private final transient CacheConfig cacheConfig;
 +
 +  private final Command command;
 +
 +  private final InetAddress serverBindAddress;
 +
 +  private final Integer pid;
 +  private final Integer serverPort;
 +
 +  private final Properties distributedSystemProperties;
 +
 +  private final String memberName;
 +  private final String springXmlLocation;
 +  private final String workingDirectory;
 +
 +  // NOTE in addition to debug, the other shared, mutable state
 +  private volatile transient String statusMessage;
 +  
 +  private final Float criticalHeapPercentage;
 +  private final Float evictionHeapPercentage;
 +  
 +  private final Float criticalOffHeapPercentage;
 +  private final Float evictionOffHeapPercentage;
 +  
 +  private final String hostNameForClients; 
 +  private final Integer maxConnections;
 +  private final Integer maxMessageCount;
 +  private final Integer messageTimeToLive;
 +  private final Integer socketBufferSize;
 +  
 +  private final Integer maxThreads;
 +
 +  private volatile transient ControllableProcess process;
 +
 +  private final transient ServerControllerParameters controllerParameters;
 +  
 +  /**
 +   * Launches a GemFire Server from the command-line configured with the given arguments.
 +   * 
 +   * @param args the command-line arguments used to configure the GemFire Server at runtime.
 +   */
 +  public static void main(final String... args) {
 +    try {
 +      new Builder(args).build().run();
 +    }
 +    catch (AttachAPINotFoundException e) {
 +      System.err.println(e.getMessage());
 +    }
 +  }
 +
 +  private static Integer getDefaultServerPort() {
 +    return Integer.getInteger(AbstractCacheServer.TEST_OVERRIDE_DEFAULT_PORT_PROPERTY, CacheServer.DEFAULT_PORT);
 +  }
 +
 +  /**
 +   * Gets the instance of the ServerLauncher used to launch the GemFire Cache Server, or null if this VM does not
 +   * have an instance of ServerLauncher indicating no GemFire Cache Server is running.
 +   * 
 +   * @return the instance of ServerLauncher used to launcher a GemFire Cache Server in this VM.
 +   */
 +  public static ServerLauncher getInstance() {
 +    return INSTANCE.get();
 +  }
 +
 +  /**
 +   * Gets the ServerState for this process or null if this process was not launched using this VM's
 +   * ServerLauncher reference .
 +   * 
 +   * @return the ServerState for this process or null.
 +   */
 +  public static ServerState getServerState() {
 +    return (getInstance() != null ? getInstance().status() : null);
 +  }
 +
 +  /**
 +   * Private constructor used to properly construct an immutable instance of the ServerLauncher using a Builder.
 +   * The Builder is used to configure a ServerLauncher instance.  The Builder can process user input from the
 +   * command-line or be used programmatically to properly construct an instance of the ServerLauncher using the API.
 +   * 
 +   * @param builder an instance of ServerLauncher.Builder for configuring and constructing an instance of the
 +   * ServerLauncher.
 +   * @see com.gemstone.gemfire.distributed.ServerLauncher.Builder
 +   */
 +  private ServerLauncher(final Builder builder) {
 +    this.cache = builder.getCache(); // testing
 +    this.cacheConfig = builder.getCacheConfig();
 +    this.command = builder.getCommand();
 +    this.assignBuckets = Boolean.TRUE.equals(builder.getAssignBuckets());
 +    setDebug(Boolean.TRUE.equals(builder.getDebug()));
 +    this.disableDefaultServer = Boolean.TRUE.equals(builder.getDisableDefaultServer());
 +    CacheServerLauncher.disableDefaultServer.set(this.disableDefaultServer);
 +    this.distributedSystemProperties = builder.getDistributedSystemProperties();
 +    this.force = Boolean.TRUE.equals(builder.getForce());
 +    this.help = Boolean.TRUE.equals(builder.getHelp());
 +    this.hostNameForClients = builder.getHostNameForClients();
 +    this.memberName = builder.getMemberName();
 +    // TODO:KIRK: set ThreadLocal for LogService with getLogFile or getLogFileName
 +    this.pid = builder.getPid();
 +    this.rebalance = Boolean.TRUE.equals(builder.getRebalance());
 +    this.redirectOutput = Boolean.TRUE.equals(builder.getRedirectOutput());
 +    this.serverBindAddress = builder.getServerBindAddress();
 +    if (builder.isServerBindAddressSetByUser() && this.serverBindAddress != null) {
 +      CacheServerLauncher.serverBindAddress.set(this.serverBindAddress.getHostAddress());
 +    }
 +    this.serverPort = builder.getServerPort();
 +    if (builder.isServerPortSetByUser() && this.serverPort != null) {
 +      CacheServerLauncher.serverPort.set(this.serverPort);
 +    }
 +    this.springXmlLocation = builder.getSpringXmlLocation();
 +    this.workingDirectory = builder.getWorkingDirectory();
 +    this.criticalHeapPercentage = builder.getCriticalHeapPercentage();
 +    this.evictionHeapPercentage = builder.getEvictionHeapPercentage();
 +    this.criticalOffHeapPercentage = builder.getCriticalOffHeapPercentage();
 +    this.evictionOffHeapPercentage = builder.getEvictionOffHeapPercentage();
 +    this.maxConnections = builder.getMaxConnections();
 +    this.maxMessageCount = builder.getMaxMessageCount();
 +    this.maxThreads = builder.getMaxThreads();
 +    this.messageTimeToLive = builder.getMessageTimeToLive();
 +    this.socketBufferSize = builder.getSocketBufferSize();
 +    this.controllerParameters = new ServerControllerParameters();
 +    this.controlHandler = new ControlNotificationHandler() {
 +      @Override
 +      public void handleStop() {
 +        if (isStoppable()) {
 +          stopInProcess();
 +        }
 +      }
 +      @Override
 +      public ServiceState<?> handleStatus() {
 +        return statusInProcess();
 +      }
 +    };
 +  }
 +
 +  /**
 +   * Gets a reference to the Cache that was created when the GemFire Server was started.
 +   * 
 +   * @return a reference to the Cache created by the GemFire Server start operation.
 +   * @see com.gemstone.gemfire.cache.Cache
 +   */
 +  final Cache getCache() {
 +    return this.cache;
 +  }
 +
 +  /**
 +   * Gets the CacheConfig object used to configure additional GemFire Cache components and features (e.g. PDX).
 +   *
 +   * @return a CacheConfig object with additional GemFire Cache configuration meta-data used on startup to configure
 +   * the Cache.
 +   */
 +  final CacheConfig getCacheConfig() {
 +    final CacheConfig copy = new CacheConfig();
 +    copy.setDeclarativeConfig(this.cacheConfig);
 +    return copy;
 +  }
 +
 +  /**
 +   * Gets an identifier that uniquely identifies and represents the Server associated with this launcher.
 +   * 
 +   * @return a String value identifier to uniquely identify the Server and it's launcher.
 +   * @see #getServerBindAddressAsString()
 +   * @see #getServerPortAsString()
 +   */
 +  public final String getId() {
 +    final StringBuilder buffer = new StringBuilder(ServerState.getServerBindAddressAsString(this));
 +    final String serverPort = ServerState.getServerPortAsString(this);
 +
 +    if (!StringUtils.isBlank(serverPort)) {
 +      buffer.append("[").append(serverPort).append("]");
 +    }
 +
 +    return buffer.toString();
 +  }
 +
 +  /**
 +   * Get the Server launcher command used to invoke the Server.
 +   * 
 +   * @return the Server launcher command used to invoke the Server.
 +   * @see com.gemstone.gemfire.distributed.ServerLauncher.Command
 +   */
 +  public Command getCommand() {
 +    return this.command;
 +  }
 +
 +  /**
 +   * Determines whether buckets should be assigned to partitioned regions in the cache upon Server start.
 +   * 
 +   * @return a boolean indicating if buckets should be assigned upon Server start.
 +   */
 +  public boolean isAssignBuckets() {
 +    return this.assignBuckets;
 +  }
 +
 +  /**
 +   * Determines whether a default cache server will be added when the GemFire Server comes online.
 +   * 
 +   * @return a boolean value indicating whether to add a default cache server.
 +   */
 +  public boolean isDisableDefaultServer() {
 +    return this.disableDefaultServer;
 +  }
 +
 +  /**
 +   * Determines whether the PID file is allowed to be overwritten when the Server is started and a PID file
 +   * already exists in the Server's specified working directory.
 +   * 
 +   * @return boolean indicating if force has been enabled.
 +   */
 +  public boolean isForcing() {
 +    return this.force;
 +  }
 +
 +  /**
 +   * Determines whether this launcher will be used to display help information.  If so, then none of the standard
 +   * Server launcher commands will be used to affect the state of the Server.  A launcher is said to be 'helping'
 +   * if the user entered the "--help" option (switch) on the command-line.
 +   * 
 +   * @return a boolean value indicating if this launcher is used for displaying help information.
 +   * @see com.gemstone.gemfire.distributed.ServerLauncher.Command
 +   */
 +  public boolean isHelping() {
 +    return this.help;
 +  }
 +
 +  /**
 +   * Determines whether a rebalance operation on the cache will occur upon starting the GemFire server using this
 +   * launcher.
 +   * 
 +   * @return a boolean indicating if the cache will be rebalance when the GemFire server starts.
 +   */
 +  public boolean isRebalancing() {
 +    return this.rebalance;
 +  }
 +
 +  /**
 +   * Determines whether this launcher will redirect output to system logs when
 +   * starting a new Locator process.
 +   * 
 +   * @return a boolean value indicating if this launcher will redirect output 
 +   * to system logs when starting a new Locator process
 +   */
 +  public boolean isRedirectingOutput() {
 +    return this.redirectOutput;
 +  }
 +
 +  /**
 +   * Gets the name of the log file used to log information about this Server.
 +   * 
 +   * @return a String value indicating the name of this Server's log file.
 +   */
 +  public String getLogFileName() {
 +    return StringUtils.defaultIfBlank(getMemberName(), DEFAULT_SERVER_LOG_NAME).concat(DEFAULT_SERVER_LOG_EXT);
 +  }
 +
 +  /**
 +   * Gets the name of this member (this Server) in the GemFire distributed system as determined by the 'name' GemFire
 +   * property.
 +   * 
 +   * @return a String indicating the name of the member (this Server) in the GemFire distributed system.
 +   * @see AbstractLauncher#getMemberName()
 +   */
 +  public String getMemberName() {
 +    return StringUtils.defaultIfBlank(this.memberName, super.getMemberName());
 +  }
 +
 +  /**
 +   * Gets the user-specified process ID (PID) of the running Server that ServerLauncher uses to issue status and
 +   * stop commands to the Server.
 +   * 
 +   * @return an Integer value indicating the process ID (PID) of the running Server.
 +   */
 +  @Override
 +  public Integer getPid() {
 +    return this.pid;
 +  }
 +
 +  /**
 +   * Gets the GemFire Distributed System (cluster) Properties.
 +   *
 +   * @return a Properties object containing the configuration settings for the GemFire Distributed System (cluster).
 +   * @see java.util.Properties
 +   */
 +  public Properties getProperties() {
 +    return (Properties) this.distributedSystemProperties.clone();
 +  }
 +
 +  /**
 +   * Gets the IP address to which the Server is bound listening for and accepting cache client connections.  This
 +   * property should not be confused with 'bindAddress' ServerLauncher property, which is the port for binding the
 +   * Server's ServerSocket used in distribution and messaging between the peers of the GemFire distributed system.
 +   * 
 +   * @return an InetAddress indicating the IP address that the Server is bound to listening for and accepting cache
 +   * client connections in a client/server topology.
 +   */
 +  public InetAddress getServerBindAddress() {
 +    return this.serverBindAddress;
 +  }
 +
 +  /**
 +   * Gets the host, as either hostname or IP address, on which the Server was bound and running.  An attempt is made
 +   * to get the canonical hostname for IP address to which the Server was bound for accepting client requests.  If
 +   * the server bind address is null or localhost is unknown, then a default String value of "localhost/127.0.0.1"
 +   * is returned.
 +   * 
 +   * Note, this information is purely information and should not be used to re-construct state or for
 +   * other purposes.
 +   * 
 +   * @return the hostname or IP address of the host running the Server, based on the bind-address, or
 +   * 'localhost/127.0.0.1' if the bind address is null and localhost is unknown.
 +   * @see java.net.InetAddress
 +   * @see #getServerBindAddress()
 +   */
 +  public String getServerBindAddressAsString() {
 +    try {
 +      if (getServerBindAddress() != null) {
 +        return getServerBindAddress().getCanonicalHostName();
 +      }
 +
 +      final InetAddress localhost = SocketCreator.getLocalHost();
 +
 +      return localhost.getCanonicalHostName();
 +    }
 +    catch (UnknownHostException ignore) {
 +      // TODO determine a better value for the host on which the Server is running to return here...
 +      // NOTE returning localhost/127.0.0.1 implies the serverBindAddress was null and no IP address for localhost
 +      // could be found
 +      return "localhost/127.0.0.1";
 +    }
 +  }
 +
 +  /**
 +   * Gets the port on which the Server is listening for cache client connections.  This property should not be confused
 +   * with the 'port' ServerLauncher property, which is used by the Server to set the 'tcp-port' distribution config
 +   * property and is used by the ServerSocket for peer distribution and messaging.
 +   * 
 +   * @return an Integer value indicating the port the Server is listening on for cache client connections in the
 +   * client/server topology.
 +   */
 +  public Integer getServerPort() {
 +    return this.serverPort;
 +  }
 +
 +  /**
 +   * Gets the server port on which the Server is listening for client requests represented as a String value.
 +   * 
 +   * @return a String representing the server port on which the Server is listening for client requests.
 +   * @see #getServerPort()
 +   */
 +  public String getServerPortAsString() {
 +    return ObjectUtils.defaultIfNull(getServerPort(), getDefaultServerPort()).toString();
 +  }
 +
 +  /**
 +   * Gets the name for a GemFire Server.
 +   * 
 +   * @return a String indicating the name for a GemFire Server.
 +   */
 +  public String getServiceName() {
 +    return SERVER_SERVICE_NAME;
 +  }
 +
 +  /**
 +   * Gets the location of the Spring XML configuration meta-data file used to bootstrap, configure and initialize
 +   * the GemFire Server on start.
 +   * <p>
 +   * @return a String indicating the location of the Spring XML configuration file.
 +   * @see com.gemstone.gemfire.distributed.ServerLauncher.Builder#getSpringXmlLocation()
 +   */
 +  public String getSpringXmlLocation() {
 +    return this.springXmlLocation;
 +  }
 +
 +  /**
 +   * Determines whether this GemFire Server was configured and initialized with Spring configuration meta-data.
 +   * <p>
 +   * @return a boolean value indicating whether this GemFire Server was configured with Spring configuration meta-data.
 +   */
 +  public boolean isSpringXmlLocationSpecified() {
 +    return !StringUtils.isBlank(this.springXmlLocation);
 +  }
 +
 +  /**
 +   * Gets the working directory pathname in which the Server will be run.
 +   * 
 +   * @return a String value indicating the pathname of the Server's working directory.
 +   */
 +  @Override
 +  public String getWorkingDirectory() {
 +    return this.workingDirectory;
 +  }
 +  
 +  public Float getCriticalHeapPercentage() {
 +    return this.criticalHeapPercentage;
 +  }
 +  
 +  public Float getEvictionHeapPercentage() {
 +    return this.evictionHeapPercentage;
 +  }
 +  
 +  public Float getCriticalOffHeapPercentage() {
 +    return this.criticalOffHeapPercentage;
 +  }
 +  
 +  public Float getEvictionOffHeapPercentage() {
 +    return this.evictionOffHeapPercentage;
 +  }
 +  
 +  public String getHostNameForClients() {
 +    return this.hostNameForClients;
 +  }
 +
 +  public Integer getMaxConnections() {
 +    return this.maxConnections;
 +  }
 +
 +  public Integer getMaxMessageCount() {
 +    return this.maxMessageCount;
 +  }
 +
 +  public Integer getMessageTimeToLive() {
 +    return this.messageTimeToLive;
 +  }
 +
 +  public Integer getMaxThreads() {
 +    return this.maxThreads;
 +  }
 +
 +  public Integer getSocketBufferSize() {
 +    return this.socketBufferSize;
 +  }
 +  
 +  /**
 +   * Displays help for the specified Server launcher command to standard err.  If the Server launcher command
 +   * is unspecified, then usage information is displayed instead.
 +   * 
 +   * @param command the Server launcher command in which to display help information.
 +   * @see #usage()
 +   */
 +  public void help(final Command command) {
 +    if (Command.isUnspecified(command)) {
 +      usage();
 +    }
 +    else {
 +      info(StringUtils.wrap(helpMap.get(command.getName()), 80, ""));
 +      info("\n\nusage: \n\n");
 +      info(StringUtils.wrap("> java ... " + getClass().getName() + " " + usageMap.get(command), 80, "\t\t"));
 +      info("\n\noptions: \n\n");
 +
 +      for (final String option : command.getOptions()) {
 +        info(StringUtils.wrap("--" + option + ": " + helpMap.get(option) + "\n", 80, "\t"));
 +      }
 +
 +      info("\n\n");
 +    }
 +  }
 +
 +  /**
 +   * Displays usage information on the proper invocation of the ServerLauncher from the command-line to standard err.
 +   * 
 +   * @see #help(com.gemstone.gemfire.distributed.ServerLauncher.Command)
 +   */
 +  public void usage() {
 +    info(StringUtils.wrap(helpMap.get("launcher"), 80, "\t"));
 +    info("\n\nSTART\n\n");
 +    help(Command.START);
 +    info("STATUS\n\n");
 +    help(Command.STATUS);
 +    info("STOP\n\n");
 +    help(Command.STOP);
 +  }
 +
 +  /**
 +   * A Runnable method used to invoke the GemFire server (cache server) with the specified command.  From run, a user
 +   * can invoke 'start', 'status', 'stop' and 'version'.  Note, that 'version' is also a command-line option, but can
 +   * be treated as a "command" as well.
 +   * 
 +   * @see java.lang.Runnable
 +   */
 +  @Override
 +  public void run() {
 +    if (!isHelping()) {
 +      switch (getCommand()) {
 +        case START:
 +          info(start());
 +          waitOnServer();
 +          break;
 +        case STATUS:
 +          info(status());
 +          break;
 +        case STOP:
 +          info(stop());
 +          break;
 +        case VERSION:
 +          info(version());
 +          break;
 +        default:
 +          usage();
 +      }
 +    }
 +    else {
 +      help(getCommand());
 +    }
 +  }
 +
 +  /**
 +   * Gets a File reference with the path to the PID file for the Server.
 +   * 
 +   * @return a File reference to the path of the Server's PID file.
 +   */
 +  protected File getServerPidFile() {
 +    return new File(getWorkingDirectory(), ProcessType.SERVER.getPidFileName());
 +  }
 +  
 +  /**
 +   * Determines whether a GemFire Cache Server can be started with this instance of ServerLauncher.
 +   *
 +   * @return a boolean indicating whether a GemFire Cache Server can be started with this instance of ServerLauncher,
 +   * which is true if the ServerLauncher has not already started a Server or a Server is not already running.
 +   * @see #start()
 +   */
 +  private boolean isStartable() {
 +    return (!isRunning() && this.starting.compareAndSet(false, true));
 +  }
 +
 +  /**
 +   * Invokes the 'start' command and operation to startup a GemFire server (a cache server).  Note, this method will
 +   * cause the JVM to block upon server start, providing the calling Thread is a non-daemon Thread.
 +   *
 +   * @see #run()
 +   */
 +  public ServerState start() {
 +    if (isStartable()) {
 +      INSTANCE.compareAndSet(null, this);
 +
 +      try {
 +        process = new ControllableProcess(this.controlHandler, new File(getWorkingDirectory()), ProcessType.SERVER, isForcing());
 +
 +        if (!isDisableDefaultServer()) {
 +          assertPortAvailable(getServerBindAddress(), getServerPort());
 +        }
 +
 +        SystemFailure.setExitOK(true);
 +
 +        ProcessLauncherContext.set(isRedirectingOutput(), getOverriddenDefaults(), new StartupStatusListener() {
 +          @Override
 +          public void setStatus(final String statusMessage) {
 +            debug("Callback setStatus(String) called with message (%1$s)...", statusMessage);
 +            ServerLauncher.this.statusMessage = statusMessage;
 +          }
 +        });
 +
 +        try {
 +          final Properties gemfireProperties = getDistributedSystemProperties(getProperties());
 +          this.cache = (isSpringXmlLocationSpecified() ? startWithSpring() : startWithGemFireApi(gemfireProperties));
 +          
 +          //Set the resource manager options
 +          if (this.criticalHeapPercentage != null) {
 +            this.cache.getResourceManager().setCriticalHeapPercentage(getCriticalHeapPercentage());
 +          } 
 +          if (this.evictionHeapPercentage != null) {
 +            this.cache.getResourceManager().setEvictionHeapPercentage(getEvictionHeapPercentage());
 +          }
 +          if (this.criticalOffHeapPercentage != null) {
 +            this.cache.getResourceManager().setCriticalOffHeapPercentage(getCriticalOffHeapPercentage());
 +          } 
 +          if (this.evictionOffHeapPercentage != null) {
 +            this.cache.getResourceManager().setEvictionOffHeapPercentage(getEvictionOffHeapPercentage());
 +          }
 +          
 +          this.cache.setIsServer(true);
 +          startCacheServer(this.cache);
 +          assignBuckets(this.cache);
 +          rebalance(this.cache);
 +        }
 +        finally {
 +          ProcessLauncherContext.remove();
 +        }
 +        
 +        debug("Running Server on (%1$s) in (%2$s) as (%2$s)...", getId(), getWorkingDirectory(), getMember());
 +        this.running.set(true);
 +
 +        return new ServerState(this, Status.ONLINE);
 +      }
 +      catch (IOException e) {
 +        failOnStart(e);
 +        throw new RuntimeException(LocalizedStrings.Launcher_Command_START_IO_ERROR_MESSAGE.toLocalizedString(
 +          getServiceName(), getWorkingDirectory(), getId(), e.getMessage()), e);
 +      }
 +      catch (FileAlreadyExistsException e) {
 +        failOnStart(e);
 +        throw new RuntimeException(LocalizedStrings.Launcher_Command_START_PID_FILE_ALREADY_EXISTS_ERROR_MESSAGE.
 +           toLocalizedString(getServiceName(), getWorkingDirectory(), getId()), e);
 +      }
 +      catch (PidUnavailableException e) {
 +        failOnStart(e);
 +        throw new RuntimeException(LocalizedStrings.Launcher_Command_START_PID_UNAVAILABLE_ERROR_MESSAGE
 +          .toLocalizedString(getServiceName(), getId(), getWorkingDirectory(), e.getMessage()), e);
 +      }
 +      catch (ClusterConfigurationNotAvailableException e) {
 +        failOnStart(e);
 +        throw e;
 +      }
 +      catch (RuntimeException e) {
 +        failOnStart(e);
 +        throw e;
 +      }
 +      catch (Exception e) {
 +        failOnStart(e);
 +        throw new RuntimeException(e);
 +      }
 +      catch (Error e) {
 +        failOnStart(e);
 +        throw e;
 +      }
 +      finally {
 +        this.starting.set(false);
 +      }
 +    }
 +    else {
 +      throw new IllegalStateException(LocalizedStrings.Launcher_Command_START_SERVICE_ALREADY_RUNNING_ERROR_MESSAGE
 +        .toLocalizedString(getServiceName(), getWorkingDirectory(), getId()));
 +    }
 +  }
 +
 +  private Cache startWithSpring() {
 +    System.setProperty(DistributionConfig.GEMFIRE_PREFIX + DistributionConfig.NAME_NAME, getMemberName());
 +
 +    new SpringContextBootstrappingInitializer().init(CollectionUtils.createProperties(Collections.singletonMap(
 +      SpringContextBootstrappingInitializer.CONTEXT_CONFIG_LOCATIONS_PARAMETER, getSpringXmlLocation())));
 +
 +    return SpringContextBootstrappingInitializer.getApplicationContext().getBean(Cache.class);
 +  }
 +
 +  private Cache startWithGemFireApi(final Properties gemfireProperties ) {
 +    final CacheConfig cacheConfig = getCacheConfig();
 +    final CacheFactory cacheFactory = new CacheFactory(gemfireProperties);
 +
 +    if (cacheConfig.pdxPersistentUserSet) {
 +      cacheFactory.setPdxPersistent(cacheConfig.isPdxPersistent());
 +    }
 +
 +    if (cacheConfig.pdxDiskStoreUserSet) {
 +      cacheFactory.setPdxDiskStore(cacheConfig.getPdxDiskStore());
 +    }
 +
 +    if (cacheConfig.pdxIgnoreUnreadFieldsUserSet) {
 +      cacheFactory.setPdxIgnoreUnreadFields(cacheConfig.getPdxIgnoreUnreadFields());
 +    }
 +
 +    if (cacheConfig.pdxReadSerializedUserSet) {
 +      cacheFactory.setPdxReadSerialized(cacheConfig.isPdxReadSerialized());
 +    }
 +
 +    if (cacheConfig.pdxSerializerUserSet) {
 +      cacheFactory.setPdxSerializer(cacheConfig.getPdxSerializer());
 +    }
 +
 +    return cacheFactory.create();
 +  }
 +
 +  /**
 +   * A helper method to ensure the same sequence of actions are taken when the Server fails to start
 +   * caused by some exception.
 +   * 
 +   * @param cause the Throwable thrown during the startup operation on the Server.
 +   */
 +  private void failOnStart(final Throwable cause) {
 +    if (this.cache != null) {
 +      this.cache.close();
 +      this.cache = null;
 +    }
 +    if (this.process != null) {
 +      this.process.stop();
 +      this.process = null;
 +    }
 +
 +    INSTANCE.compareAndSet(this, null);
 +
 +    this.running.set(false);
 +  }
 +
 +  /**
 +   * Determines whether the specified Cache has any CacheServers.
 +   * 
 +   * @param cache the Cache to check for existing CacheServers.
 +   * @return a boolean value indicating if any CacheServers were added to the Cache.
 +   */
 +  protected boolean isServing(final Cache cache) {
 +    return !cache.getCacheServers().isEmpty();
 +  }
 +
 +  /**
 +   * Determines whether to continue waiting and keep the GemFire non-Server data member running.
 +   * 
 +   * @param cache the Cache associated with this GemFire (non-Server) data member.
 +   * @return a boolean value indicating whether the GemFire data member should continue running, as determined
 +   * by the running flag and a connection to the distributed system (GemFire cluster).
 +   */
 +  final boolean isWaiting(final Cache cache) {
 +    //return (isRunning() && !getCache().isClosed());
 +    return (isRunning() && cache.getDistributedSystem().isConnected());
 +  }
 +
 +  /**
 +   * Causes the calling Thread to block until the GemFire Cache Server/Data Member stops.
 +   */
 +  public void waitOnServer() {
 +    assert getCache() != null : "The Cache Server must first be started with a call to start!";
 +
 +    if (!isServing(getCache())) {
 +      Throwable cause = null;
 +      try {
 +        while (isWaiting(getCache())) {
 +          try {
 +            synchronized (this) {
 +              wait(500l);
 +            }
 +          }
 +          catch (InterruptedException ignore) {
 +          }
 +        }
 +      }
 +      catch (RuntimeException e) {
 +        cause = e;
 +        throw e;
 +      }
 +      finally {
 +        failOnStart(cause);
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Determines whether a default server (a cache server) should be created on startup as determined by the absence
 +   * of specifying the --disable-default-server command-line option (switch).  In addition, a default cache server
 +   * is started only if no cache servers have been added to the Cache by way of cache.xml.
 +   * 
 +   * @param cache the reference to the Cache to check for any existing cache servers.
 +   * @return a boolean indicating whether a default server should be added to the Cache.
 +   * @see #isDisableDefaultServer()
 +   */
 +  protected boolean isDefaultServerEnabled(final Cache cache) {
 +    return (cache.getCacheServers().isEmpty() && !isDisableDefaultServer());
 +  }
 +
 +  /**
 +   * If the default server (cache server) has not been disabled and no prior cache servers were added to the cache,
 +   * then this method will add a cache server to the Cache and start the server Thread on the specified bind address
 +   * and port.
 +   * 
 +   * @param cache the Cache to which the server will be added.
 +   * @throws IOException if the Cache server fails to start due to IO error.
 +   */
 +  final void startCacheServer(final Cache cache) throws IOException {
 +    if (isDefaultServerEnabled(cache)) {
 +      final String serverBindAddress = (getServerBindAddress() == null ? null : getServerBindAddress().getHostAddress());
 +      final Integer serverPort = getServerPort();
 +      CacheServerLauncher.serverBindAddress.set(serverBindAddress);
 +      CacheServerLauncher.serverPort.set(serverPort);
 +      final CacheServer cacheServer = cache.addCacheServer();
 +      cacheServer.setBindAddress(serverBindAddress);
 +      cacheServer.setPort(serverPort);
 +      
 +      if (getMaxThreads() != null) {
 +        cacheServer.setMaxThreads(getMaxThreads());
 +      }
 +      
 +      if (getMaxConnections() != null) {
 +        cacheServer.setMaxConnections(getMaxConnections());
 +      }
 +      
 +      if (getMaxMessageCount() != null) {
 +        cacheServer.setMaximumMessageCount(getMaxMessageCount());
 +      }
 +      
 +      if (getMessageTimeToLive() != null) {
 +        cacheServer.setMessageTimeToLive(getMessageTimeToLive());
 +      }
 +      
 +      if (getSocketBufferSize() != null) {
 +        cacheServer.setSocketBufferSize(getSocketBufferSize());
 +      }
 +
 +      if (getHostNameForClients() != null) {
 +        cacheServer.setHostnameForClients(getHostNameForClients());
 +      }
++      
++      CacheServerHelper.setIsDefaultServer(cacheServer);
 +
 +      cacheServer.start();
 +    }
 +  }
 +
 +  /**
 +   * Causes a rebalance operation to occur on the given Cache.
 +   * 
 +   * @param cache the reference to the Cache to rebalance.
 +   * @see com.gemstone.gemfire.cache.control.ResourceManager#createRebalanceFactory()
 +   */
 +  private void rebalance(final Cache cache) {
 +    if (isRebalancing()) {
 +      cache.getResourceManager().createRebalanceFactory().start();
 +    }
 +  }
 +
 +  /**
 +   * Determines whether the user indicated that buckets should be assigned on cache server start using the
 +   * --assign-buckets command-line option (switch) at the command-line as well as whether the option is technically
 +   * allowed.  The option is only allowed if the instance of the Cache is the internal GemFireCacheImpl at present.
 +   * @param cache the Cache reference to check for instance type.
 +   * @return a boolean indicating if bucket assignment is both enabled and allowed.
 +   * @see #isAssignBuckets()
 +   */
 +  protected boolean isAssignBucketsAllowed(final Cache cache) {
 +    return (isAssignBuckets() && (cache instanceof GemFireCacheImpl));
 +  }
 +
 +  /**
 +   * Assigns buckets to individual Partitioned Regions of the Cache.
 +   * 
 +   * @param cache the Cache who's Partitioned Regions are accessed to assign buckets to.
 +   * @see PartitionRegionHelper#assignBucketsToPartitions(com.gemstone.gemfire.cache.Region)
 +   */
 +  final void assignBuckets(final Cache cache) {
 +    if (isAssignBucketsAllowed(cache)) {
 +      for (PartitionedRegion region : ((GemFireCacheImpl) cache).getPartitionedRegions()) {
 +        PartitionRegionHelper.assignBucketsToPartitions(region);
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Determines whether the Server is the process of starting or is already running.
 +   * 
 +   * @return a boolean indicating if the Server is starting or is already running.
 +   */
 +  protected boolean isStartingOrRunning() {
 +    return (this.starting.get() || isRunning());
 +  }
 +
 +  /**
 +   * Invokes the 'status' command and operation to check the status of a GemFire server (a cache server).
 +   */
 +  public ServerState status() {
 +    final ServerLauncher launcher = getInstance();
 +    // if this instance is running then return local status
 +    if (isStartingOrRunning()) {
 +      debug("Getting status from the ServerLauncher instance that actually launched the GemFire Cache Server.%n");
 +      return new ServerState(this, (isRunning() ? Status.ONLINE : Status.STARTING));
 +    }
 +    else if (isPidInProcess() && launcher != null) {
 +      return launcher.statusInProcess();
 +    }
 +    else if (getPid() != null) {
 +      debug("Getting Server status using process ID (%1$s)%n", getPid());
 +      return statusWithPid();
 +    }
 +    // attempt to get status using workingDirectory
 +    else if (getWorkingDirectory() != null) {
 +      debug("Getting Server status using working directory (%1$s)%n", getWorkingDirectory());
 +      return statusWithWorkingDirectory();
 +    }
 +
 +    debug("This ServerLauncher was not the instance used to launch the GemFire Cache Server, and neither PID "
 +      .concat("nor working directory were specified; the Server's state is unknown.%n"));
 +
 +    return new ServerState(this, Status.NOT_RESPONDING);
 +  }
 +  
 +  private ServerState statusInProcess() {
 +    if (isStartingOrRunning()) {
 +      debug("Getting status from the ServerLauncher instance that actually launched the GemFire Cache Server.%n");
 +      return new ServerState(this, (isRunning() ? Status.ONLINE : Status.STARTING));
 +    } else {
 +      return new ServerState(this, Status.NOT_RESPONDING);
 +    }
 +  }
 +  
 +  private ServerState statusWithPid() {
 +    try {
 +      final ProcessController controller = new ProcessControllerFactory().createProcessController(this.controllerParameters, getPid());
 +      controller.checkPidSupport();
 +      final String statusJson = controller.status();
 +      return ServerState.fromJson(statusJson);
 +    }
 +//    catch (NoClassDefFoundError error) {
 +//      if (isAttachAPINotFound(error)) {
 +//        throw new AttachAPINotFoundException(LocalizedStrings.Launcher_ATTACH_API_NOT_FOUND_ERROR_MESSAGE
 +//          .toLocalizedString(), error);
 +//      }
 +//
 +//      throw error;
 +//    }
 +    catch (ConnectionFailedException e) {
 +      // failed to attach to server JVM
 +      return createNoResponseState(e, "Failed to connect to server with process id " + getPid());
 +    } 
 +    catch (IOException e) {
 +      // failed to open or read file or dir
 +      return createNoResponseState(e, "Failed to communicate with server with process id " + getPid());
 +    } 
 +//    catch (MalformedObjectNameException e) { // impossible
 +//      // JMX object name is bad
 +//      return createNoResponseState(e, "Failed to communicate with server with process id " + getPid());
 +//    } 
 +    catch (MBeanInvocationFailedException e) {
 +      // MBean either doesn't exist or method or attribute don't exist
 +      return createNoResponseState(e, "Failed to communicate with server with process id " + getPid());
 +    } 
 +//    catch (PidUnavailableException e) {
 +//      // couldn't determine pid from within server JVM
 +//      return createNoResponseState(e, "Failed to communicate with server with process id " + getPid());
 +//    } 
 +    catch (UnableToControlProcessException e) {
 +      // TODO comment me
 +      return createNoResponseState(e, "Failed to communicate with server with process id " + getPid());
 +    } 
 +    catch (InterruptedException e) {
 +      // TODO comment me
 +      return createNoResponseState(e, "Failed to communicate with server with process id " + getPid());
 +    } 
 +    catch (TimeoutException e) {
 +      // TODO comment me
 +      return createNoResponseState(e, "Failed to communicate with server with process id " + getPid());
 +    }
 +  }
 +
 +  private ServerState statusWithWorkingDirectory() {
 +    int parsedPid = 0;
 +    try {
 +      final ProcessController controller = new ProcessControllerFactory().createProcessController(this.controllerParameters, new File(getWorkingDirectory()), ProcessType.SERVER.getPidFileName(), READ_PID_FILE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
 +      parsedPid = controller.getProcessId();
 +      
 +      // note: in-process request will go infinite loop unless we do the following
 +      if (parsedPid == identifyPid()) {
 +        final ServerLauncher runningLauncher = getInstance();
 +        if (runningLauncher != null) {
 +          return runningLauncher.statusInProcess();
 +        }
 +      }
 +
 +      final String statusJson = controller.status();
 +      return ServerState.fromJson(statusJson);
 +    }
 +    catch (ConnectionFailedException e) {
 +      // failed to attach to server JVM
 +      return createNoResponseState(e, "Failed to connect to server with process id " + parsedPid);
 +    } 
 +    catch (FileNotFoundException e) {
 +      // could not find pid file
 +      return createNoResponseState(e, "Failed to find process file " + ProcessType.SERVER.getPidFileName() + " in " + getWorkingDirectory());
 +    } 
 +    catch (IOException e) {
 +      // failed to open or read file or dir
 +      return createNoResponseState(e, "Failed to communicate with server with process id " + parsedPid);
 +    } 
 +    catch (InterruptedException e) {
 +      Thread.currentThread().interrupt();
 +      return createNoResponseState(e, "Interrupted while trying to communicate with server with process id " + parsedPid);
 +    } 
 +    catch (MBeanInvocationFailedException e) {
 +      // MBean either doesn't exist or method or attribute don't exist
 +      return createNoResponseState(e, "Failed to communicate with server with process id " + parsedPid);
 +    } 
 +    catch (PidUnavailableException e) {
 +      // couldn't determine pid from within server JVM
 +      return createNoResponseState(e, "Failed to find usable process id within file " + ProcessType.SERVER.getPidFileName() + " in " + getWorkingDirectory());
 +    } 
 +    catch (UnableToControlProcessException e) {
 +      return createNoResponseState(e, "Failed to communicate with server with process id " + parsedPid);
 +    } 
 +    catch (TimeoutException e) {
 +      return createNoResponseState(e, "Failed to communicate with server with process id " + parsedPid);
 +    }
 +  }
 +
 +  /**
 +   * Determines whether the Server can be stopped in-process, such as when a Server is embedded in an application
 +   * and the ServerLauncher API is being used.
 +   * 
 +   * @return a boolean indicating whether the Server can be stopped in-process (the application's process with
 +   * an embedded Server).
 +   */
 +  private boolean isStoppable() {
 +    return (isRunning() && getCache() != null);
 +  }
 +
 +  /**
 +   * Invokes the 'stop' command and operation to stop a GemFire server (a cache server).
 +   */
 +  public ServerState stop() {
 +    final ServerLauncher launcher = getInstance();
 +    // if this instance is running then stop it
 +    if (isStoppable()) {
 +      return stopInProcess();
 +    }
 +    // if in-process but difference instance of ServerLauncher
 +    else if (isPidInProcess() && launcher != null) {
 +      return launcher.stopInProcess();
 +    }
 +    // attempt to stop using pid if provided
 +    else if (getPid() != null) {
 +      return stopWithPid();
 +    }
 +    // attempt to stop using workingDirectory
 +    else if (getWorkingDirectory() != null) {
 +      return stopWithWorkingDirectory();
 +    }
 +
 +    // TODO give user detailed error message?
 +    return new ServerState(this, Status.NOT_RESPONDING);
 +  }
 +  
 +  private ServerState stopInProcess() {
 +    if (isStoppable()) {
 +      this.cache.close();
 +      this.cache = null;
 +      this.process.stop();
 +      this.process = null;
 +      INSTANCE.compareAndSet(this, null); // note: other thread may return Status.NOT_RESPONDING now
 +      this.running.set(false);
 +      return new ServerState(this, Status.STOPPED);
 +    } else {
 +      return new ServerState(this, Status.NOT_RESPONDING);
 +    }
 +  }
 +
 +  private ServerState stopWithPid() {
 +    try {
 +      final ProcessController controller = new ProcessControllerFactory().createProcessController(this.controllerParameters, getPid());
 +      controller.checkPidSupport();
 +      controller.stop();
 +      return new ServerState(this, Status.STOPPED);
 +    }
 +//    catch (NoClassDefFoundError error) {
 +//      if (isAttachAPINotFound(error)) {
 +//        throw new AttachAPINotFoundException(LocalizedStrings.Launcher_ATTACH_API_NOT_FOUND_ERROR_MESSAGE
 +//          .toLocalizedString(), error);
 +//      }
 +//
 +//      throw error;
 +//    }
 +    catch (ConnectionFailedException e) {
 +      // failed to attach to server JVM
 +      return createNoResponseState(e, "Failed to connect to server with process id " + getPid());
 +    } 
 +    catch (IOException e) {
 +      // failed to open or read file or dir
 +      return createNoResponseState(e, "Failed to communicate with server with process id " + getPid());
 +    } 
 +//    catch (MalformedObjectNameException e) { // impossible
 +//      // JMX object name is bad
 +//      return createNoResponseState(e, "Failed to communicate with server with process id " + getPid());
 +//    } 
 +    catch (MBeanInvocationFailedException e) {
 +      // MBean either doesn't exist or method or attribute don't exist
 +      return createNoResponseState(e, "Failed to communicate with server with process id " + getPid());
 +    } 
 +//    catch (PidUnavailableException e) {
 +//      // couldn't determine pid from within server JVM
 +//      return createNoResponseState(e, "Failed to communicate with server with process id " + getPid());
 +//    } 
 +    catch (UnableToControlProcessException e) {
 +      // TODO comment me
 +      return createNoResponseState(e, "Failed to communicate with server with process id " + getPid());
 +    }
 +  }
 +
 +  private ServerState stopWithWorkingDirectory() {
 +    int parsedPid = 0;
 +    try {
 +      final ProcessController controller = new ProcessControllerFactory().createProcessController(this.controllerParameters, new File(getWorkingDirectory()), ProcessType.SERVER.getPidFileName(), READ_PID_FILE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
 +      parsedPid = controller.getProcessId();
 +      
 +      // NOTE in-process request will go infinite loop unless we do the following
 +      if (parsedPid == identifyPid()) {
 +        final ServerLauncher runningLauncher = getInstance();
 +        if (runningLauncher != null) {
 +          return runningLauncher.stopInProcess();
 +        }
 +      }
 +      
 +      controller.stop();
 +      return new ServerState(this, Status.STOPPED);
 +    }
 +    catch (ConnectionFailedException e) {
 +      // failed to attach to server JVM
 +      return createNoResponseState(e, "Failed to connect to server with process id " + parsedPid);
 +    } 
 +    catch (FileNotFoundException e) {
 +      // could not find pid file
 +      return createNoResponseState(e, "Failed to find process file " + ProcessType.SERVER.getPidFileName() + " in " + getWorkingDirectory());
 +    } 
 +    catch (IOException e) {
 +      // failed to open or read file or dir
 +      return createNoResponseState(e, "Failed to communicate with server with process id " + parsedPid);
 +    } 
 +    catch (InterruptedException e) {
 +      Thread.currentThread().interrupt();
 +      return createNoResponseState(e, "Interrupted while trying to communicate with server with process id " + parsedPid);
 +    } 
 +    catch (MBeanInvocationFailedException e) {
 +      // MBean either doesn't exist or method or attribute don't exist
 +      return createNoResponseState(e, "Failed to communicate with server with process id " + parsedPid);
 +    } 
 +    catch (PidUnavailableException e) {
 +      // couldn't determine pid from within server JVM
 +      return createNoResponseState(e, "Failed to find usable process id within file " + ProcessType.SERVER.getPidFileName() + " in " + getWorkingDirectory());
 +    } 
 +    catch (TimeoutException e) {
 +      return createNoResponseState(e, "Timed out trying to find usable process id within file " + ProcessType.SERVER.getPidFileName() + " in " + getWorkingDirectory());
 +    } 
 +    catch (UnableToControlProcessException e) {
 +      return createNoResponseState(e, "Failed to communicate with server with process id " + parsedPid);
 +    }
 +  }
 +
 +  private ServerState createNoResponseState(final Exception cause, final String errorMessage) {
 +    debug(cause);
 +    return new ServerState(this, Status.NOT_RESPONDING, errorMessage);
 +  }
 +
 +  private Properties getOverriddenDefaults() {
 +    final Properties overriddenDefaults = new Properties();
 +    
 +    overriddenDefaults.put(
 +      ProcessLauncherContext.OVERRIDDEN_DEFAULTS_PREFIX.concat(DistributionConfig.LOG_FILE_NAME), 
 +      getLogFileName());
 +
 +    for (String key : System.getProperties().stringPropertyNames()) {
 +      if (key.startsWith(ProcessLauncherContext.OVERRIDDEN_DEFAULTS_PREFIX)) {
 +        overriddenDefaults.put(key, System.getProperty(key));
 +      }
 +    }
 +
 +    return overriddenDefaults;
 +  }
 +
 +  private class ServerControllerParameters implements ProcessControllerParameters {
 +    @Override
 +    public File getPidFile() {
 +      return getServerPidFile();
 +    }
 +  
 +    @Override
 +    public File getWorkingDirectory() {
 +      return new File(ServerLauncher.this.getWorkingDirectory());
 +    }
 +  
 +    @Override
 +    public int getProcessId() {
 +      return getPid();
 +    }
 +  
 +    @Override
 +    public ProcessType getProcessType() {
 +      return ProcessType.SERVER;
 +    }
 +  
 +    @Override
 +    public ObjectName getNamePattern() {
 +      try {
 +        return ObjectName.getInstance("GemFire:type=Member,*");
 +      } catch (MalformedObjectNameException e) {
 +        return null;
 +      } catch (NullPointerException e) {
 +        return null;
 +      }
 +    }
 +  
 +    @Override
 +    public String getPidAttribute() {
 +      return "ProcessId";
 +    }
 +  
 +    @Override
 +    public String getStopMethod() {
 +      return "shutDownMember";
 +    }
 +    
 +    @Override
 +    public String getStatusMethod() {
 +      return "status";
 +    }
 +  
 +    @Override
 +    public String[] getAttributes() {
 +      return new String[] {"Server"};
 +    }
 +  
 +    @Override
 +    public Object[] getValues() {
 +      return new Object[] {Boolean.TRUE};
 +    }
 +  }
 +
 +  /**
 +   * The Builder class, modeled after the Builder creational design pattern, is used to construct a properly configured
 +   * and initialized instance of the ServerLauncher to control and run GemFire servers (in particular, cache servers).
 +   */
 +  public static class Builder {
 +
 +    protected static final Command DEFAULT_COMMAND = Command.UNSPECIFIED;
 +
 +    private boolean serverBindAddressSetByUser;
 +    private boolean serverPortSetByUser;
 +
 +    private Boolean assignBuckets;
 +    private Boolean debug;
 +    private Boolean disableDefaultServer;
 +    private Boolean force;
 +    private Boolean help;
 +    private Boolean rebalance;
 +    private Boolean redirectOutput;
 +
 +    private Cache cache;
 +
 +    private final CacheConfig cacheConfig = new CacheConfig();
 +
 +    private Command command;
 +
 +    private InetAddress serverBindAddress;
 +
 +    private Integer pid;
 +    private Integer serverPort;
 +
 +    private final Properties distributedSystemProperties = new Properties();
 +
 +    private String memberName;
 +    private String springXmlLocation;
 +    private String workingDirectory;
 +    
 +    private Float criticalHeapPercentage;
 +    private Float evictionHeapPercentage;
 +    
 +    private Float criticalOffHeapPercentage;
 +    private Float evictionOffHeapPercentage;
 +    
 +    private String hostNameForClients; 
 +    private Integer loadPollInterval;
 +    private Integer maxConnections;
 +    private Integer maxMessageCount;
 +    private Integer messageTimeToLive;
 +    private Integer socketBufferSize;
 +    private Integer maxThreads;
 +
 +    /**
 +     * Default constructor used to create an instance of the Builder class for programmatical access.
 +     */
 +    public Builder() {
 +    }
 +
 +    /**
 +     * Constructor used to create and configure an instance of the Builder class with the specified arguments, passed in
 +     * from the command-line when launching an instance of this class from the command-line using the Java launcher.
 +     * 
 +     * @param args the array of arguments used to configure the Builder.
 +     * @see #parseArguments(String...)
 +     */
 +    public Builder(final String... args) {
 +      parseArguments(args != null ? args : new String[0]);
 +    }
 +
 +    /**
 +     * Gets an instance of the JOptSimple OptionParser to parse the command-line arguments for Server.
 +     * 
 +     * @return an instance of the JOptSimple OptionParser configured with the command-line options used by the Server.
 +     */
 +    private OptionParser getParser() {
 +      OptionParser parser = new OptionParser(true);
 +
 +      parser.accepts("assign-buckets");
 +      parser.accepts("debug");
 +      parser.accepts("dir").withRequiredArg().ofType(String.class);
 +      parser.accepts("disable-default-server");
 +      parser.accepts("force");
 +      parser.accepts("help");
 +      parser.accepts("member").withRequiredArg().ofType(String.class);
 +      parser.accepts("pid").withRequiredArg().ofType(Integer.class);
 +      parser.accepts("rebalance");
 +      parser.accepts("redirect-output");
 +      parser.accepts("server-bind-address").withRequiredArg().ofType(String.class);
 +      parser.accepts("server-port").withRequiredArg().ofType(Integer.class);
 +      parser.accepts("spring-xml-location").withRequiredArg().ofType(String.class);
 +      parser.accepts("version");
 +      parser.accepts(CliStrings.START_SERVER__CRITICAL__HEAP__PERCENTAGE).withRequiredArg().ofType(Float.class);
 +      parser.accepts(CliStrings.START_SERVER__EVICTION__HEAP__PERCENTAGE).withRequiredArg().ofType(Float.class);
 +      parser.accepts(CliStrings.START_SERVER__CRITICAL_OFF_HEAP_PERCENTAGE).withRequiredArg().ofType(Float.class);
 +      parser.accepts(CliStrings.START_SERVER__EVICTION_OFF_HEAP_PERCENTAGE).withRequiredArg().ofType(Float.class);
 +      parser.accepts(CliStrings.START_SERVER__MAX__CONNECTIONS).withRequiredArg().ofType(Integer.class);
 +      parser.accepts(CliStrings.START_SERVER__MAX__MESSAGE__COUNT).withRequiredArg().ofType(Integer.class);
 +      parser.accepts(CliStrings.START_SERVER__MAX__THREADS).withRequiredArg().ofType(Integer.class);
 +      parser.accepts(CliStrings.START_SERVER__MESSAGE__TIME__TO__LIVE).withRequiredArg().ofType(Integer.class);
 +      parser.accepts(CliStrings.START_SERVER__SOCKET__BUFFER__SIZE).withRequiredArg().ofType(Integer.class);
 +      parser.accepts(CliStrings.START_SERVER__HOSTNAME__FOR__CLIENTS).withRequiredArg().ofType(String.class);
 +
 +      return parser;
 +    }
 +
 +    /**
 +     * Parses the list of arguments to configure this Builder with the intent of constructing a Server launcher to
 +     * invoke a Cache Server.  This method is called to parse the arguments specified by the user on the command-line.
 +     * 
 +     * @param args the array of arguments used to configure this Builder and create an instance of ServerLauncher.
 +     */
 +    protected void parseArguments(final String... args) {
 +      try {
 +        OptionSet options = getParser().parse(args);
 +
 +        parseCommand(args);
 +        parseMemberName(args); // TODO:KIRK: need to get the name to LogService for log file name
 +
 +        setAssignBuckets(options.has("assign-buckets"));
 +        setDebug(options.has("debug"));
 +        setDisableDefaultServer(options.has("disable-default-server"));
 +        setForce(options.has("force"));
 +        setHelp(options.has("help"));
 +        setRebalance(options.has("rebalance"));
 +        setRedirectOutput(options.has("redirect-output"));
 +        
 +        if (options.hasArgument(CliStrings.START_SERVER__CRITICAL__HEAP__PERCENTAGE)) {
 +          setCriticalHeapPercentage(Float.parseFloat(ObjectUtils.toString(options.valueOf(CliStrings.START_SERVER__CRITICAL__HEAP__PERCENTAGE))));
 +        }
 +        
 +        if (options.hasArgument(CliStrings.START_SERVER__EVICTION__HEAP__PERCENTAGE)) {
 +          setEvictionHeapPercentage(Float.parseFloat(ObjectUtils.toString(options.valueOf(CliStrings.START_SERVER__EVICTION__HEAP__PERCENTAGE))));
 +        }
 +        
 +        if (options.hasArgument(CliStrings.START_SERVER__CRITICAL_OFF_HEAP_PERCENTAGE)) {
 +          setCriticalOffHeapPercentage(Float.parseFloat(ObjectUtils.toString(options.valueOf(CliStrings.START_SERVER__CRITICAL_OFF_HEAP_PERCENTAGE))));
 +        }
 +        
 +        if (options.hasArgument(CliStrings.START_SERVER__EVICTION_OFF_HEAP_PERCENTAGE)) {
 +          setEvictionOffHeapPercentage(Float.parseFloat(ObjectUtils.toString(options.valueOf(CliStrings.START_SERVER__EVICTION_OFF_HEAP_PERCENTAGE))));
 +        }
 +        
 +        if (options.hasArgument(CliStrings.START_SERVER__MAX__CONNECTIONS)) {
 +          setMaxConnections(Integer.parseInt(ObjectUtils.toString(options.valueOf(CliStrings.START_SERVER__MAX__CONNECTIONS))));
 +        }
 +        
 +        if (options.hasArgument(CliStrings.START_SERVER__MAX__MESSAGE__COUNT)) {
 +          setMaxConnections(Integer.parseInt(ObjectUtils.toString(options.valueOf(CliStrings.START_SERVER__MAX__MESSAGE__COUNT))));
 +        }
 +        
 +        if (options.hasArgument(CliStrings.START_SERVER__MESSAGE__TIME__TO__LIVE)) {
 +          setMaxConnections(Integer.parseInt(ObjectUtils.toString(options.valueOf(CliStrings.START_SERVER__MESSAGE__TIME__TO__LIVE))));
 +        }
 +        
 +        if (options.hasArgument(CliStrings.START_SERVER__SOCKET__BUFFER__SIZE)) {
 +          setMaxConnections(Integer.parseInt(ObjectUtils.toString(options.valueOf(CliStrings.START_SERVER__SOCKET__BUFFER__SIZE))));
 +        } 
 +        
 +        if (options.hasArgument(CliStrings.START_SERVER__MAX__THREADS)) {
 +          setMaxThreads(Integer.parseInt(ObjectUtils.toString(options.valueOf(CliStrings.START_SERVER__MAX__THREADS))));
 +        }
 +        
 +        if (!isHelping()) {
 +          if (options.has("dir")) {
 +            setWorkingDirectory(ObjectUtils.toString(options.valueOf("dir")));
 +          }
 +
 +          if (options.has("pid")) {
 +            setPid((Integer) options.valueOf("pid"));
 +          }
 +
 +          if (options.has("server-bind-address")) {
 +            setServerBindAddress(ObjectUtils.toString(options.valueOf("server-bind-address")));
 +          }
 +
 +          if (options.has("server-port")) {
 +            setServerPort((Integer) options.valueOf("server-port"));
 +          }
 +
 +          if (options.has("spring-xml-location")) {
 +            setSpringXmlLocation(ObjectUtils.toString(options.valueOf("spring-xml-location")));
 +          }
 +
 +          if (options.has("version")) {
 +            setCommand(Command.VERSION);
 +          }
 +        }
 +
 +        // TODO why are these option not inside the 'if (!isHelping())' conditional block!?
 +
 +        if (options.hasArgument(CliStrings.START_SERVER__CRITICAL__HEAP__PERCENTAGE)) {
 +          setCriticalHeapPercentage(Float.parseFloat(ObjectUtils.toString(options.valueOf(CliStrings.START_SERVER__CRITICAL__HEAP__PERCENTAGE))));
 +        }
 +
 +        if (options.hasArgument(CliStrings.START_SERVER__EVICTION__HEAP__PERCENTAGE)) {
 +          setEvictionHeapPercentage(Float.parseFloat(ObjectUtils.toString(options.valueOf(
 +            CliStrings.START_SERVER__EVICTION__HEAP__PERCENTAGE))));
 +        }
 +
 +        if (options.hasArgument(CliStrings.START_SERVER__MAX__CONNECTIONS)) {
 +          setMaxConnections(Integer.parseInt(ObjectUtils.toString(options.valueOf(
 +            CliStrings.START_SERVER__MAX__CONNECTIONS))));
 +        }
 +
 +        if (options.hasArgument(CliStrings.START_SERVER__MAX__MESSAGE__COUNT)) {
 +          setMaxMessageCount(Integer.parseInt(ObjectUtils.toString(options.valueOf(
 +            CliStrings.START_SERVER__MAX__MESSAGE__COUNT))));
 +        }
 +
 +        if (options.hasArgument(CliStrings.START_SERVER__MAX__THREADS)) {
 +          setMaxThreads(Integer.parseInt(ObjectUtils.toString(options.valueOf(CliStrings.START_SERVER__MAX__THREADS))));
 +        }
 +
 +        if (options.hasArgument(CliStrings.START_SERVER__MESSAGE__TIME__TO__LIVE)) {
 +          setMessageTimeToLive(Integer.parseInt(ObjectUtils.toString(options.valueOf(
 +            CliStrings.START_SERVER__MESSAGE__TIME__TO__LIVE))));
 +        }
 +        
 +        if (options.hasArgument(CliStrings.START_SERVER__SOCKET__BUFFER__SIZE)) {
 +          setSocketBufferSize(Integer.parseInt(ObjectUtils.toString(options.valueOf(
 +            CliStrings.START_SERVER__SOCKET__BUFFER__SIZE))));
 +        }
 +
 +        if (options.hasArgument(CliStrings.START_SERVER__HOSTNAME__FOR__CLIENTS)) {
 +          setHostNameForClients(ObjectUtils.toString(options.valueOf(CliStrings.START_SERVER__HOSTNAME__FOR__CLIENTS)));
 +        }
 +
 +      }
 +      catch (OptionException e) {
 +        throw new IllegalArgumentException(LocalizedStrings.Launcher_Builder_PARSE_COMMAND_LINE_ARGUMENT_ERROR_MESSAGE
 +          .toLocalizedString("Server", e.getMessage()), e);
 +      }
 +      catch (Exception e) {
 +        throw new RuntimeException(e.getMessage(), e);
 +      }
 +    }
 +
 +    /**
 +     * Iterates the list of arguments in search of the target Server launcher command.
 +     * 
 +     * @param args an array of arguments from which to search for the Server launcher command.
 +     * @see com.gemstone.gemfire.distributed.ServerLauncher.Command#valueOfName(String)
 +     * @see #parseArguments(String...)
 +     */
 +    protected void parseCommand(final String... args) {
 +      if (args != null) {
 +        for (String arg : args) {
 +          Command command = Command.valueOfName(arg);
 +          if (command != null) {
 +            setCommand(command);
 +            break;
 +          }
 +        }
 +      }
 +    }
 +
 +    /**
 +     * Iterates the list of arguments in search of the Server's GemFire member name.  If the argument does not
 +     * start with '-' or is not the name of a Server launcher command, then the value is presumed to be the member name
 +     * for the Server in GemFire.
 +     * 
 +     * @param args the array of arguments from which to search for the Server's member name in GemFire.
 +     * @see com.gemstone.gemfire.distributed.ServerLauncher.Command#isCommand(String)
 +     * @see #parseArguments(String...)
 +     */
 +    protected void parseMemberName(final String... args) {
 +      if (args != null) {
 +        for (String arg : args) {
 +          if (!(arg.startsWith(OPTION_PREFIX) || Command.isCommand(arg))) {
 +            setMemberName(arg);
 +            break;
 +          }
 +        }
 +      }
 +    }
 +
 +    /**
 +     * Gets the CacheConfig object used to configure PDX on the GemFire Cache by the Builder.
 +     *
 +     * @return the CacheConfig object used to configure PDX on the GemFire Cache by the Builder.
 +     */
 +    CacheConfig getCacheConfig() {
 +      return this.cacheConfig;
 +    }
 +
 +    /**
 +     * Gets the Server launcher command used during the invocation of the ServerLauncher.
 +     * 
 +     * @return the Server launcher command used to invoke (run) the ServerLauncher class.
 +     * @see #setCommand(com.gemstone.gemfire.distributed.ServerLauncher.Command)
 +     * @see com.gemstone.gemfire.distributed.ServerLauncher.Command
 +     */
 +    public Command getCommand() {
 +      return ObjectUtils.defaultIfNull(this.command, DEFAULT_COMMAND);
 +    }
 +
 +    /**
 +     * Sets the Sever launcher command used during the invocation of the ServerLauncher
 +     * 
 +     * @param command the targeted Server launcher command used during the invocation (run) of ServerLauncher.
 +     * @return this Builder instance.
 +     * @see #getCommand()
 +     * @see com.gemstone.gemfire.distributed.ServerLauncher.Command
 +     */
 +    public Builder setCommand(final Command command) {
 +      this.command = command;
 +      return this;
 +    }
 +
 +    /**
 +     * Determines whether buckets should be assigned to partitioned regions in the cache upon Server start.
 +     * 
 +     * @return a boolean indicating if buckets should be assigned upon Server start.
 +     * @see #setAssignBuckets(Boolean)
 +     */
 +    public Boolean getAssignBuckets() {
 +      return this.assignBuckets;
 +    }
 +
 +    /**
 +     * Sets whether buckets should be assigned to partitioned regions in the cache upon Server start.
 +     * 
 +     * @param assignBuckets a boolean indicating if buckets should be assigned upon Server start.
 +     * @return this Builder instance.
 +     * @see #getAssignBuckets()
 +     */
 +    public Builder setAssignBuckets(final Boolean assignBuckets) {
 +      this.assignBuckets = assignBuckets;
 +      return this;
 +    }
 +
 +    // For testing purposes only!
 +    Cache getCache() {
 +      return this.cache;
 +    }
 +
 +    // For testing purposes only!
 +    Builder setCache(final Cache cache) {
 +      this.cache = cache;
 +      return this;
 +    }
 +
 +    /**
 +     * Determines whether the new instance of the ServerLauncher will be set to debug mode.
 +     * 
 +     * @return a boolean value indicating whether debug mode is enabled or disabled.
 +     * @see #setDebug(Boolean)
 +     */
 +    public Boolean getDebug() {
 +      return this.debug;
 +    }
 +
 +    /**
 +     * Sets whether the new instance of the ServerLauncher will be set to debug mode.
 +     * 
 +     * @param debug a boolean value indicating whether debug mode is to be enabled or disabled.
 +     * @return this Builder instance.
 +     * @see #getDebug()
 +     */
 +    public Builder setDebug(final Boolean debug) {
 +      this.debug = debug;
 +      return this;
 +    }
 +
 +    /**
 +     * Determines whether a default cache server will be added when the GemFire Server comes online.
 +     * 
 +     * @return a boolean value indicating whether to add a default cache server.
 +     * @see #setDisableDefaultServer(Boolean)
 +     */
 +    public Boolean getDisableDefaultServer() {
 +      return this.disableDefaultServer;
 +    }
 +
 +    /**
 +     * Sets a boolean value indicating whether to add a default cache when the GemFire Server comes online.
 +     * 
 +     * @param disableDefaultServer a boolean value indicating whether to add a default cache server.
 +     * @return this Builder instance.
 +     * @see #getDisableDefaultServer()
 +     */
 +    public Builder setDisableDefaultServer(final Boolean disableDefaultServer) {
 +      this.disableDefaultServer = disableDefaultServer;
 +      return this;
 +    }
 +
 +    /**
 +     * Gets the GemFire Distributed System (cluster) Properties configuration.
 +     *
 +     * @return a Properties object containing configuration settings for the GemFire Distributed System (cluster).
 +     * @see java.util.Properties
 +     */
 +    public Properties getDistributedSystemProperties() {
 +      return this.distributedSystemProperties;
 +    }
 +
 +    /**
 +     * Gets the boolean value used by the Server to determine if it should overwrite the PID file if it already exists.
 +     * 
 +     * @return the boolean value specifying whether or not to overwrite the PID file if it already exists.
 +     * @see com.gemstone.gemfire.internal.process.LocalProcessLauncher
 +     * @see #setForce(Boolean)
 +     */
 +    public Boolean getForce() {
 +      return ObjectUtils.defaultIfNull(this.force, DEFAULT_FORCE);
 +    }
 +
 +    /**
 +     * Sets the boolean value used by the Server to determine if it should overwrite the PID file if it already exists.
 +     * 
 +     * @param force a boolean value indicating whether to overwrite the PID file when it already exists.
 +     * @return this Builder instance.
 +     * @see com.gemstone.gemfire.internal.process.LocalProcessLauncher
 +     * @see #getForce()
 +     */
 +    public Builder setForce(final Boolean force) {
 +      this.force = force;
 +      return this;
 +    }
 +
 +    /**
 +     * Determines whether the new instance of the ServerLauncher will be used to output help information for either
 +     * a specific command, or for using ServerLauncher in general.
 +     * 
 +     * @return a boolean value indicating whether help will be output during the invocation of the ServerLauncher.
 +     * @see #setHelp(Boolean)
 +     */
 +    public Boolean getHelp() {
 +      return this.help;
 +    }
 +
 +    /**
 +     * Determines whether help has been enabled.
 +     * 
 +     * @return a boolean indicating if help was enabled.
 +     */
 +    protected final boolean isHelping() {
 +      return Boolean.TRUE.equals(getHelp());
 +    }
 +
 +    /**
 +     * Sets whether the new instance of ServerLauncher will be used to output help information for either a specific
 +     * command, or for using ServerLauncher in general.
 +     * 
 +     * @param help a boolean indicating whether help information is to be displayed during invocation of ServerLauncher.
 +     * @return this Builder instance.
 +     * @see #getHelp()
 +     */
 +    public Builder setHelp(final Boolean help) {
 +      this.help = help;
 +      return this;
 +    }
 +
 +    /**
 +     * Determines whether a rebalance operation on the cache will occur upon starting the GemFire server.
 +     * 
 +     * @return a boolean indicating if the cache will be rebalance when the GemFire server starts.
 +     * @see #setRebalance(Boolean)
 +     */
 +    public Boolean getRebalance() {
 +      return this.rebalance;
 +    }
 +
 +    /**
 +     * Set a boolean value indicating whether a rebalance operation on the cache should occur upon starting
 +     * the GemFire server.
 +     * 
 +     * @param rebalance a boolean indicating if the cache will be rebalanced when the GemFire server starts.
 +     * @return this Builder instance.
 +     * @see #getRebalance()
 +     */
 +    public Builder setRebalance(final Boolean rebalance) {
 +      this.rebalance = rebalance;
 +      return this;
 +    }
 +
 +    /**
 +     * Gets the member name of this Server in GemFire.
 +     * 
 +     * @return a String indicating the member name of this Server in GemFire.
 +     * @see #setMemberName(String)
 +     */
 +    public String getMemberName() {
 +      return this.memberName;
 +    }
 +
 +    /**
 +     * Sets the member name of the Server in GemFire.
 +     * 
 +     * @param memberName a String indicating the member name of this Server in GemFire.
 +     * @return this Builder instance.
 +     * @throws IllegalArgumentException if the member name is invalid.
 +     * @see #getMemberName()
 +     */
 +    public Builder setMemberName(final String memberName) {
 +      if (StringUtils.isEmpty(StringUtils.trim(memberName))) {
 +        throw new IllegalArgumentException(LocalizedStrings.Launcher_Builder_MEMBER_NAME_ERROR_MESSAGE
 +          .toLocalizedString("Server"));
 +      }
 +      this.memberName = memberName;
 +      return this;
 +    }
 +
 +    /**
 +     * Gets the process ID (PID) of the running Server indicated by the user as an argument to the ServerLauncher.
 +     * This PID is used by the Server launcher to determine the Server's status, or invoke shutdown on the Server.
 +     * 
 +     * @return a user specified Integer value indicating the process ID of the running Server.
 +     * @see #setPid(Integer)
 +     */
 +    public Integer getPid() {
 +      return this.pid;
 +    }
 +
 +    /**
 +     * Sets the process ID (PID) of the running Server indicated by the user as an argument to the ServerLauncher.
 +     * This PID will be used by the Server launcher to determine the Server's status, or invoke shutdown on the Server.
 +     * 
 +     * @param pid a user specified Integer value indicating the process ID of the running Server.
 +     * @return this Builder instance.
 +     * @throws IllegalArgumentException if the process ID (PID) is not valid (greater than zero if not null).
 +     * @see #getPid()
 +     */
 +    public Builder setPid(final Integer pid) {
 +      if (pid != null && pid < 0) {
 +        throw new IllegalArgumentException(LocalizedStrings.Launcher_Builder_PID_ERROR_MESSAGE.toLocalizedString());
 +      }
 +      this.pid = pid;
 +      return this;
 +    }
 +
 +    /**
 +     * Determines whether the new instance of LocatorLauncher will redirect
 +     * output to system logs when starting a Locator.
 +     * 
 +     * @return a boolean value indicating if output will be redirected to system 
 +     * logs when starting a Locator
 +     * 
 +     * @see #setRedirectOutput(Boolean)
 +     */
 +    public Boolean getRedirectOutput() {
 +      return this.redirectOutput;
 +    }
 +
 +    /**
 +     * Determines whether redirecting of output has been enabled.
 +     * 
 +     * @return a boolean indicating if redirecting of output was enabled.
 +     */
 +    private boolean isRedirectingOutput() {
 +      return Boolean.TRUE.equals(getRedirectOutput());
 +    }
 +
 +    /**
 +     * Sets whether the new instance of LocatorLauncher will redirect output to system logs when starting a Locator.
 +     * 
 +     * @param redirectOutput a boolean value indicating if output will be redirected to system logs when starting
 +     * a Locator.
 +     * @return this Builder instance.
 +     * @see #getRedirectOutput()
 +     */
 +    public Builder setRedirectOutput(final Boolean redirectOutput) {
 +      this.redirectOutput = redirectOutput;
 +      return this;
 +    }
 +
 +    /**
 +     * Gets the IP address to which the Server will be bound listening for and accepting cache client connections in
 +     * a client/server topology.
 +     * 
 +     * @return an InetAddress indicating the IP address that the Server is bound to listening for and accepting cache
 +     * client connections in a client/server topology.
 +     * @see #setServerBindAddress(String)
 +     */
 +    public InetAddress getServerBindAddress() {
 +      return this.serverBindAddress;
 +    }
 +    
 +    boolean isServerBindAddressSetByUser() {
 +      return this.serverBindAddressSetByUser;
 +    }
 +
 +    /**
 +     * Sets the IP address to which the Server will be bound listening for and accepting cache client connections in
 +     * a client/server topology.
 +     * 
 +     * @param serverBindAddress a String specifying the IP address or hostname that the Server will be bound to listen
 +     * for and accept cache client connections in a client/server topology.
 +     * @return this Builder instance.
 +     * @throws IllegalArgumentException wrapping the UnknownHostException if the IP address or hostname for the
 +     * server bind address is unknown.
 +     * @see #getServerBindAddress()
 +     */
 +    public Builder setServerBindAddress(final String serverBindAddress) {
 +      if (StringUtils.isBlank(serverBindAddress)) {
 +        this.serverBindAddress = null;
 +        return this;
 +      }
 +      // NOTE only set the 'bind address' if the user specified a value
 +      else {
 +        try {
 +          this.serverBindAddress = InetAddress.getByName(serverBindAddress);
-           this.serverBindAddressSetByUser = true;
-           return this;
++          if (SocketCreator.isLocalHost(this.serverBindAddress)) {
++            this.serverBindAddressSetByUser = true;
++            return this;
++          }
++          else {
++            throw new IllegalArgumentException(serverBindAddress + " is not an address for this machine.");
++          }
 +        }
 +        catch (UnknownHostException e) {
 +          throw new IllegalArgumentException(LocalizedStrings.Launcher_Builder_UNKNOWN_HOST_ERROR_MESSAGE
 +            .toLocalizedString("Server"), e);
 +        }
 +      }
 +    }
 +
 +    /**
 +     * Gets the port on which the Server will listen for and accept cache client connections in a client/server topology.
 +     * 
 +     * @return an Integer value specifying the port the Server will listen on and accept cache client connections in
 +     * a client/server topology.
 +     * @see #setServerPort(Integer)
 +     */
 +    public Integer getServerPort() {
 +      return ObjectUtils.defaultIfNull(this.serverPort, getDefaultServerPort());
 +    }
 +    
 +    boolean isServerPortSetByUser() {
 +      return this.serverPortSetByUser;
 +    }
 +
 +    /**
 +     * Sets the port on which the Server will listen for and accept cache client connections in a client/server topology.
 +     * 
 +     * @param serverPort an Integer value specifying the port the Server will listen on and accept cache client
 +     * connections in a client/server topology.
 +     * @return this Builder instance.
 +     * @throws IllegalArgumentException if the port number is not valid.
 +     * @see #getServerPort()
 +     */
 +    public Builder setServerPort(final Integer serverPort) {
 +      if (serverPort != null && (serverPort < 0 || serverPort > 65535)) {
 +        throw new IllegalArgumentException(LocalizedStrings.Launcher_Builder_INVALID_PORT_ERROR_MESSAGE
 +          .toLocalizedString("Server"));
 +      }
 +      this.serverPort = serverPort;
 +      this.serverPortSetByUser = true;
 +      return this;
 +    }
 +
 +    /**
 +     * Gets the location of the Spring XML configuration meta-data file used to bootstrap, configure and initialize
 +     * the GemFire Server on start.
 +     * <p>
 +     * @return a String indicating the location of the Spring XML configuration file.
 +     * @see #setSpringXmlLocation(String)
 +     */
 +    public String getSpringXmlLocation() {
 +      return this.springXmlLocation;
 +    }
 +
 +    /**
 +     * Sets the location of the Spring XML configuration meta-data file used to bootstrap, configure and initialize
 +     * the GemFire Server on start.
 +     * <p>
 +     * @param springXmlLocation a String indicating the location of the Spring XML configuration file.
 +     * @return this Builder instance.
 +     * @see #getSpringXmlLocation()
 +     */
 +    public Builder setSpringXmlLocation(final String springXmlLocation) {
 +      this.springXmlLocation = springXmlLocation;
 +      return this;
 +    }
 +
 +    /**
 +     * Gets the working directory pathname in which the Server will be ran.  If the directory is unspecified,
 +     * then working directory defaults to the current directory.
 +     * 
 +     * @return a String indicating the working directory pathname.
 +     * @see #setWorkingDirectory(String)
 +     */
 +    public String getWorkingDirectory() {
 +      return IOUtils.tryGetCanonicalPathElseGetAbsolutePath(
 +        new File(StringUtils.defaultIfBlank(this.workingDirectory, DEFAULT_WORKING_DIRECTORY)));
 +    }
 +
 +    /**
 +     * Sets the working directory in which the Server will be ran.  This also the directory in which all Server files
 +     * (such as log and license files) will be written.  If the directory is unspecified, then the working directory
 +     * defaults to the current directory.
 +     * 
 +     * @param workingDirectory a String indicating the pathname of the directory in which the Server will be ran.
 +     * @return this Builder instance.
 +     * @throws IllegalArgumentException wrapping a FileNotFoundException if the working directory pathname cannot be
 +     * found.
 +     * @see #getWorkingDirectory()
 +     * @see java.io.FileNotFoundException
 +     */
 +    public Builder setWorkingDirectory(final String workingDirectory) {
 +      if (!(new File(StringUtils.defaultIfBlank(workingDirectory, DEFAULT_WORKING_DIRECTORY)).isDirectory())) {
 +        throw new IllegalArgumentException(
 +          LocalizedStrings.Launcher_Builder_WORKING_DIRECTORY_NOT_FOUND_ERROR_MESSAGE.toLocalizedString("Server"),
 +            new FileNotFoundException(workingDirectory));
 +      }
 +      this.workingDirectory = workingDirectory;
 +      return this;
 +    }
 +    
 +    public Float getCriticalHeapPercentage() {
 +      return this.criticalHeapPercentage;
 +    }
 +
 +    public Builder setCriticalHeapPercentage(final Float criticalHeapPercentage) {
 +      if (criticalHeapPercentage != null) {
 +        if (criticalHeapPercentage < 0 || criticalHeapPercentage > 100.0f) {
 +          throw new IllegalArgumentException(String.format("Critical heap percentage (%1$s) must be between 0 and 100!",
 +            criticalHeapPercentage));
 +        }
 +      }
 +      this.criticalHeapPercentage = criticalHeapPercentage;
 +      return this;
 +    }
 +
 +    public Float getCriticalOffHeapPercentage() {
 +      return this.criticalOffHeapPercentage;
 +    }
 +    
 +    public Builder setCriticalOffHeapPercentage(final Float criticalOffHeapPercentage) {
 +      if (criticalOffHeapPercentage != null) {
 +        if (criticalOffHeapPercentage < 0 || criticalOffHeapPercentage > 100.0f) {
 +          throw new IllegalArgumentException(String.format("Critical off-heap percentage (%1$s) must be between 0 and 100!", criticalOffHeapPercentage));
 +        }
 +      }
 +     this.criticalOffHeapPercentage = criticalOffHeapPercentage;
 +     return this;
 +    }
 +    
 +    public Float getEvictionHeapPercentage() {
 +      return this.evictionHeapPercentage;
 +    }
 +
 +    public Builder setEvictionHeapPercentage(final Float evictionHeapPercentage) {
 +      if (evictionHeapPercentage != null) {
 +        if (evictionHeapPercentage < 0 || evictionHeapPercentage > 100.0f) {
 +          throw new IllegalArgumentException(String.format("Eviction heap percentage (%1$s) must be between 0 and 100!",
 +            evictionHeapPercentage));
 +        }
 +      }
 +      this.evictionHeapPercentage = evictionHeapPercentage;
 +      return this;
 +    }
 +    
 +    public Float getEvictionOffHeapPercentage() {
 +      return this.evictionOffHeapPercentage;
 +    }
 +    
 +    public Builder setEvictionOffHeapPercentage(final Float evictionOffHeapPercentage) {
 +      if (evictionOffHeapPercentage != null) {
 +        if (evictionOffHeapPercentage < 0 || evictionOffHeapPercentage > 100.0f) {
 +          throw new IllegalArgumentException(String.format("Eviction off-heap percentage (%1$s) must be between 0 and 100", evictionOffHeapPercentage));
 +        }
 +      }
 +      this.evictionOffHeapPercentage = evictionOffHeapPercentage;
 +      return this;
 +    }
 +    
 +    public String getHostNameForClients() {
 +      return this.hostNameForClients;
 +    }
 +
 +    public Builder setHostNameForClients(String hostNameForClients) {
 +      this.hostNameForClients = hostNameForClients;
 +      return this;
 +    }
 +
 +    public Integer getMaxConnections() {
 +      return this.maxConnections;
 +    }
 +
 +    public Builder setMaxConnections(Integer maxConnections) {
 +      if (maxConnections != null && maxConnections < 1) {
 +        throw new IllegalArgumentException(String.format("Max Connections (%1$s) must be greater than 0!",
 +          maxConnections));
 +      }
 +      this.maxConnections = maxConnections;
 +      return this;
 +    }
 +
 +    public Integer getMaxMessageCount() {
 +      return this.maxMessageCount;
 +    }
 +
 +    public Builder setMaxMessageCount(Integer maxMessageCount) {
 +      if (maxMessageCount != null && maxMessageCount < 1) {
 +        throw new IllegalArgumentException(String.format("Max Message Count (%1$s) must be greater than 0!",
 +          maxMessageCount));
 +      }
 +      this.maxMessageCount = maxMessageCount;
 +      return this;
 +    }
 +


<TRUNCATED>


[46/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/hdfs/internal/hoplog/SequenceFileHoplog.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/hdfs/internal/hoplog/SequenceFileHoplog.java
index 04cbb05,0000000..fbb8f14
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/hdfs/internal/hoplog/SequenceFileHoplog.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/hdfs/internal/hoplog/SequenceFileHoplog.java
@@@ -1,396 -1,0 +1,396 @@@
 +/*
 + * 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.cache.hdfs.internal.hoplog;
 +  
 +import java.io.Closeable;
 +import java.io.EOFException;
 +import java.io.IOException;
 +import java.nio.ByteBuffer;
 +import java.util.EnumMap;
 +
++import com.gemstone.gemfire.internal.hll.ICardinality;
 +import org.apache.hadoop.conf.Configuration;
 +import org.apache.hadoop.fs.FSDataOutputStream;
 +import org.apache.hadoop.fs.FileSystem;
 +import org.apache.hadoop.fs.Path;
 +import org.apache.hadoop.io.BytesWritable;
 +import org.apache.hadoop.io.IOUtils;
 +import org.apache.hadoop.io.Text;
 +
 +import com.gemstone.gemfire.cache.hdfs.HDFSIOException;
- import com.gemstone.gemfire.cache.hdfs.internal.cardinality.ICardinality;
 +import com.gemstone.gemfire.cache.hdfs.internal.hoplog.HoplogSetReader.HoplogIterator;
 +import com.gemstone.gemfire.cache.hdfs.internal.org.apache.hadoop.io.SequenceFile;
 +import com.gemstone.gemfire.cache.hdfs.internal.org.apache.hadoop.io.SequenceFile.Reader;
 +import com.gemstone.gemfire.internal.cache.persistence.soplog.SortedOplogStatistics;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.logging.log4j.LocalizedMessage;
 +import com.gemstone.gemfire.internal.Version;
 +
 +import org.apache.logging.log4j.Logger;
 +
 +/**
 + * Implements Sequence file based {@link Hoplog}
 + * 
 + * @author hemantb
 + *
 + */
 +public class SequenceFileHoplog extends AbstractHoplog{
 +  
 +   public SequenceFileHoplog(FileSystem inputFS, Path filePath,  
 +      SortedOplogStatistics stats)
 +  throws IOException
 +  {
 +     super(inputFS, filePath, stats);
 +  }
 +  @Override
 +  public void close() throws IOException {
 +    // Nothing to do 
 +  }
 +
 +  @Override
 +  public HoplogReader getReader() throws IOException {
 +    return new SequenceFileReader();
 +  }
 +
 +  @Override
 +  /**
 +   * gets the writer for sequence file. 
 +   * 
 +   * @param keys is not used for SequenceFileHoplog class 
 +   */
 +  public HoplogWriter createWriter(int keys) throws IOException {
 +    return new SequenceFileHoplogWriter();
 +  }
 +
 +  @Override
 +  public boolean isClosed() {
 +    return false;
 +  }
 +  
 +  @Override
 +  public void close(boolean clearCache) throws IOException {
 +    // Nothing to do 
 +  }
 +
 +  /**
 +   * Currently, hsync does not update the file size on namenode. So, if last time the 
 +   * process died after calling hsync but before calling file close, the file is 
 +   * left with an inconsistent file size. This is a workaround that - open the file stream in append 
 +   * mode and close it. This fixes the file size on the namenode.
 +   * 
 +   * @throws IOException
 +   * @return true if the file size was fixed 
 +   */
 +  public boolean fixFileSize() throws IOException {
 +    // Try to fix the file size
 +    // Loop so that the expected expceptions can be ignored 3
 +   // times
 +    if (logger.isDebugEnabled())
 +      logger.debug("{}Fixing size of hoplog " + path, logPrefix);
 +    Exception e = null;
 +    boolean exceptionThrown = false;
 +    for (int i =0; i < 3; i++) {
 +      try {
 +        FSDataOutputStream stream = fsProvider.getFS().append(path);
 +        stream.close();
 +        stream = null;
 +      } catch (IOException ie) {
 +        exceptionThrown = true;
 +        e = ie;
 +        if (logger.isDebugEnabled())
 +        logger.debug("{}Retry run " + (i + 1) + ": Hoplog " + path + " is still a temporary " +
 +            "hoplog because the node managing it wasn't shutdown properly last time. Failed to " +
 +            "fix the hoplog because an exception was thrown " + e, logPrefix );
 +      }
 +      // As either RecoveryInProgressException was thrown or 
 +      // Already being created exception was thrown, wait for 
 +      // sometime before next retry. 
 +      if (exceptionThrown) {
 +        try {
 +          Thread.sleep(5000);
 +        } catch (InterruptedException e1) {
 +        } 
 +        exceptionThrown = false;
 +      } else {
 +        // no exception was thrown, break;
 +        return true;
 +      }
 +    }
 +    logger.info (logPrefix, LocalizedMessage.create(LocalizedStrings.DEBUG, "Hoplog " + path + " is still a temporary " +
 +        "hoplog because the node managing it wasn't shutdown properly last time. Failed to " +
 +        "fix the hoplog because an exception was thrown " + e));
 +    
 +    return false;
 +  }
 +  
 +  @Override
 +  public String toString() {
 +    return "SequenceFileHplog[" + getFileName() + "]";
 +  }
 +  
 +  private class SequenceFileHoplogWriter implements HoplogWriter {
 +    
 +    private SequenceFile.Writer writer = null;
 +    
 +    public SequenceFileHoplogWriter() throws IOException{
 +      writer = AbstractHoplog.getSequenceFileWriter(path, conf, logger);
 +    }
 +   
 +    @Override
 +    public void close() throws IOException {
 +      writer.close();
 +      if (logger.isDebugEnabled())
 +        logger.debug("{}Completed creating hoplog " + path, logPrefix);
 +    }
 +    
 +    @Override
 +    public void hsync() throws IOException {
 +      writer.hsyncWithSizeUpdate();
 +      if (logger.isDebugEnabled())
 +        logger.debug("{}hsync'ed a batch of data to hoplog " + path, logPrefix);
 +    }
 +    
 +    @Override
 +    public void append(byte[] key, byte[] value) throws IOException {
 +      writer.append(new BytesWritable(key), new BytesWritable(value));
 +    }
 +
 +    @Override
 +    public void append(ByteBuffer key, ByteBuffer value) throws IOException {
 +      throw new UnsupportedOperationException("Not supported for Sequence files");
 +    }
 +
 +    @Override
 +    public void close(EnumMap<Meta, byte[]> metadata) throws IOException {
 +      throw new UnsupportedOperationException("Not supported for Sequence files");
 +    }
 +    @Override
 +    public long getCurrentSize() throws IOException {
 +      return writer.getLength();
 +    }
 +    
 +  }
 +  /**
 +   * Sequence file reader. This is currently to be used only by MapReduce jobs and 
 +   * test functions
 +   * 
 +   */
 +  public class SequenceFileReader implements HoplogReader, Closeable {
 +    @Override
 +    public byte[] read(byte[] key) throws IOException {
 +      throw new UnsupportedOperationException("Not supported for Sequence files");
 +    }
 +
 +    @Override
 +    public HoplogIterator<byte[], byte[]> scan()
 +        throws IOException {
 +      return  new SequenceFileIterator(fsProvider.getFS(), path, 0, Long.MAX_VALUE, conf, logger);
 +    }
 +
 +    @Override
 +    public HoplogIterator<byte[], byte[]> scan(
 +        byte[] from, byte[] to) throws IOException {
 +      throw new UnsupportedOperationException("Not supported for Sequence files");
 +    }
 +    
 +    @Override
 +    public HoplogIterator<byte[], byte[]> scan(
 +        long startOffset, long length) throws IOException {
 +      return  new SequenceFileIterator(fsProvider.getFS(), path, startOffset, length, conf, logger);
 +    }
 +    
 +    @Override
 +    public HoplogIterator<byte[], byte[]> scan(
 +        byte[] from, boolean fromInclusive, byte[] to, boolean toInclusive)
 +        throws IOException {
 +      throw new UnsupportedOperationException("Not supported for Sequence files");
 +    }
 +
 +    @Override
 +    public boolean isClosed() {
 +      throw new UnsupportedOperationException("Not supported for Sequence files.");
 +    }
 +    
 +    @Override
 +    public void close() throws IOException {
 +      throw new UnsupportedOperationException("Not supported for Sequence files. Close the iterator instead.");
 +    }
 +
 +    @Override
 +    public ByteBuffer get(byte[] key) throws IOException {
 +      throw new UnsupportedOperationException("Not supported for Sequence files");
 +    }
 +
 +    @Override
 +    public BloomFilter getBloomFilter() throws IOException {
 +      throw new UnsupportedOperationException("Not supported for Sequence files");
 +    }
 +
 +    @Override
 +    public long getEntryCount() {
 +      throw new UnsupportedOperationException("Not supported for Sequence files");
 +    }
 +
 +    @Override
 +    public ICardinality getCardinalityEstimator() {
 +      throw new UnsupportedOperationException("Not supported for Sequence files");
 +    }
 +
 +    @Override
 +    public long sizeEstimate() {
 +      throw new UnsupportedOperationException("Not supported for Sequence files");
 +    }
 +
 +
 +  }
 +  
 +  /**
 +   * Sequence file iterator. This is currently to be used only by MapReduce jobs and 
 +   * test functions
 +   * 
 +   */
 +  public static class SequenceFileIterator implements HoplogIterator<byte[], byte[]> {
 +    
 +    SequenceFile.Reader reader = null;
 +    private BytesWritable prefetchedKey = null;
 +    private BytesWritable prefetchedValue = null;
 +    private byte[] currentKey;
 +    private byte[] currentValue;
 +    boolean hasNext = false;
 +    Logger logger; 
 +    Path path;
 +    private long start;
 +    private long end;
 +    
 +    public SequenceFileIterator(FileSystem fs, Path path, long startOffset, 
 +        long length, Configuration conf, Logger logger) 
 +        throws IOException {
 +      Reader.Option optPath = SequenceFile.Reader.file(path);
 +      
 +      // Hadoop has a configuration parameter io.serializations that is a list of serialization 
 +      // classes which can be used for obtaining serializers and deserializers. This parameter 
 +      // by default contains avro classes. When a sequence file is created, it calls 
 +      // SerializationFactory.getSerializer(keyclass). This internally creates objects using 
 +      // reflection of all the classes that were part of io.serializations. But since, there is 
 +      // no avro class available it throws an exception. 
 +      // Before creating a sequenceFile, override the io.serializations parameter and pass only the classes 
 +      // that are important to us. 
 +      String serializations[] = conf.getStrings("io.serializations",
 +          new String[]{"org.apache.hadoop.io.serializer.WritableSerialization"});
 +      conf.setStrings("io.serializations",
 +          new String[]{"org.apache.hadoop.io.serializer.WritableSerialization"});
 +      // create reader
 +      boolean emptyFile = false;
 +      try {
 +        reader = new SequenceFile.Reader(conf, optPath);
 +      }catch (EOFException e) {
 +        // this is ok as the file has ended. just return false that no more records available
 +        emptyFile = true;
 +      }
 +      // reset the configuration to its original value 
 +      conf.setStrings("io.serializations", serializations);
 +      this.logger = logger;
 +      this.path = path;
 +      
 +      if (emptyFile) {
 +        hasNext = false;
 +      } else {
 +        // The file should be read from the first sync marker after the start position and 
 +        // until the first sync marker after the end position is seen. 
 +        this.end = startOffset + length;
 +        if (startOffset > reader.getPosition()) {
 +          reader.sync(startOffset);                  // sync to start
 +        }
 +        this.start = reader.getPosition();
 +        this.hasNext = this.start < this.end;
 +        if (hasNext)
 +          readNext();
 +      } 
 +    }
 +  
 +
 +    public Version getVersion(){
 +      String version = reader.getMetadata().get(new Text(Meta.GEMFIRE_VERSION.name())).toString();
 +      return Version.fromOrdinalOrCurrent(Short.parseShort(version)); 
 +    }
 +    @Override
 +    public boolean hasNext() {
 +      return hasNext;
 +    }
 +
 +    @Override
 +    public byte[] next() {
 +      currentKey = prefetchedKey.getBytes();
 +      currentValue = prefetchedValue.getBytes();
 +      
 +      readNext();
 +
 +      return currentKey;
 +    }
 +    
 +    private void readNext() {
 +      try {
 +        long pos = reader.getPosition();
 +        prefetchedKey = new BytesWritable();
 +        prefetchedValue = new BytesWritable();
 +        hasNext = reader.next(prefetchedKey, prefetchedValue);
 +        // The file should be read from the first sync marker after the start position and 
 +        // until the first sync marker after the end position is seen. 
 +        if (pos >= end && reader.syncSeen()) {
 +          hasNext = false;
 +        }
 +      } catch (EOFException e) {
 +        // this is ok as the file has ended. just return false that no more records available
 +        hasNext = false;
 +      } 
 +      catch (IOException e) {
 +        hasNext = false;
 +        logger.error(LocalizedMessage.create(LocalizedStrings.HOPLOG_FAILED_TO_READ_HDFS_FILE, path), e);
 +        throw new HDFSIOException(
 +            LocalizedStrings.HOPLOG_FAILED_TO_READ_HDFS_FILE.toLocalizedString(path), e);
 +      }
 +    }
 +    @Override
 +    public void remove() {
 +      throw new UnsupportedOperationException("Not supported for Sequence files");
 +    }
 +
 +    @Override
 +    public void close() {
 +      IOUtils.closeStream(reader);
 +    }
 +
 +    @Override
 +    public byte[] getKey() {
 +      return currentKey;
 +    }
 +
 +    @Override
 +    public byte[] getValue() {
 +      return currentValue;
 +    }
 +    
 +    /** Returns true iff the previous call to next passed a sync mark.*/
 +    public boolean syncSeen() { return reader.syncSeen(); }
 +
 +    /** Return the current byte position in the input file. */
 +    public synchronized long getPosition() throws IOException {
 +      return reader.getPosition();
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/IndexElemArray.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/IndexElemArray.java
index b94f975,0000000..de694a4
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/IndexElemArray.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/IndexElemArray.java
@@@ -1,333 -1,0 +1,361 @@@
 +/*
 + * 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.cache.query.internal.index;
 +
 +import java.util.Arrays;
 +import java.util.Collection;
 +import java.util.Iterator;
 +import java.util.NoSuchElementException;
 +
 +/**
 + * A wrapper around an object array for storing values in index data structure
 + * with minimal set of operations supported and the maximum size of 128 elements  
 + * 
 + * @author Tejas Nomulwar
 + * @since 7.0
 + */
 +public class IndexElemArray implements Iterable, Collection {
 +
 +  private Object[] elementData;
 +  private volatile byte size;
 +
++  /* lock for making size and data changes atomically. */
++  private Object lock = new Object();
++
 +  public IndexElemArray(int initialCapacity) {
 +    if (initialCapacity < 0) {
 +      throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity);
 +    }
 +    this.elementData = new Object[initialCapacity];
 +  }
 +
 +  /**
 +   * Constructs an empty list with an initial capacity of ten.
 +   */
 +  public IndexElemArray() {
 +    this(IndexManager.INDEX_ELEMARRAY_SIZE);
 +  }
 +
 +  /**
 +   * Increases the capacity of this <tt>ArrayList</tt> instance, if necessary,
 +   * to ensure that it can hold at least the number of elements specified by the
 +   * minimum capacity argument.
 +   * 
 +   * @param minCapacity
 +   *          the desired minimum capacity
 +   */
 +  private void ensureCapacity(int minCapacity) {
 +    int oldCapacity = elementData.length;
 +    if (minCapacity > oldCapacity) {
 +      int newCapacity = oldCapacity + 5;
 +      if (newCapacity < minCapacity) {
 +        newCapacity = minCapacity;
 +      }
 +      // minCapacity is usually close to size, so this is a win:
 +      Object[] newElementData = new Object[newCapacity];
 +      System.arraycopy(this.elementData, 0, newElementData, 0,
 +          this.elementData.length);
 +      elementData = newElementData;
 +    }
 +  }
 +
 +  /**
 +   * Returns the number of elements in this list. (Warning: May not return
 +   * correct size always, as remove operation is not atomic)
 +   * 
 +   * @return the number of elements in this list
 +   */
 +  public int size() {
 +    return size;
 +  }
 +
 +  /**
 +   * Returns <tt>true</tt> if this list contains no elements.
 +   * 
 +   * @return <tt>true</tt> if this list contains no elements
 +   */
 +  public boolean isEmpty() {
 +    return size == 0;
 +  }
 +
 +  /**
 +   * Returns <tt>true</tt> if this list contains the specified element. More
 +   * formally, returns <tt>true</tt> if and only if this list contains at least
 +   * one element <tt>e</tt> such that
 +   * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
 +   * 
 +   * @param o
 +   *          element whose presence in this list is to be tested
 +   * @return <tt>true</tt> if this list contains the specified element
 +   */
 +  public boolean contains(Object o) {
 +    return indexOf(o) >= 0;
 +  }
 +
 +  /**
 +   * Returns the index of the first occurrence of the specified element in this
 +   * list, or -1 if this list does not contain the element. More formally,
 +   * returns the lowest index <tt>i</tt> such that
 +   * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
 +   * or -1 if there is no such index.
 +   */
 +  public int indexOf(Object o) {
-     if (o == null) {
-       for (int i = 0; i < size; i++)
-         if (elementData[i] == null)
-           return i;
-     } else {
-       for (int i = 0; i < size; i++)
-         if (o.equals(elementData[i]))
-           return i;
++    synchronized (lock) {
++      if (o == null) {
++        for (int i = 0; i < size; i++)
++          if (elementData[i] == null)
++            return i;
++      } else {
++        for (int i = 0; i < size; i++)
++          if (o.equals(elementData[i]))
++            return i;
++      }
 +    }
 +    return -1;
 +  }
 +
 +  /**
 +   * Returns the element at the specified position in this list.
 +   * 
 +   * @param index
 +   *          index of the element to return
 +   * @return the element at the specified position in this list
 +   * @throws IndexOutOfBoundsException
 +   *          
 +   */
 +  public Object get(int index) {
-     RangeCheck(index);
-     return elementData[index];
++    synchronized (lock) {
++      RangeCheck(index);
++      return elementData[index];
++    }
 +  }
 +
 +  /**
 +   * Replaces the element at the specified position in this list with the
 +   * specified element.
 +   * 
 +   * @param index
 +   *          index of the element to replace
 +   * @param element
 +   *          element to be stored at the specified position
 +   * @return the element previously at the specified position
 +   * @throws IndexOutOfBoundsException
 +   *           
 +   */
 +  public Object set(int index, Object element) {
-     RangeCheck(index);
++    synchronized (lock) {
++      RangeCheck(index);
 +
-     Object oldValue = (Object) elementData[index];
-     elementData[index] = element;
-     return oldValue;
++      Object oldValue = (Object) elementData[index];
++      elementData[index] = element;
++      return oldValue;
++    }
 +  }
 +
 +  /**
 +   * Appends the specified element to the end of this array.
 +   * If the array is full, creates a new array with 
 +   * new capacity = old capacity + 5
 +   * 
 +   * @param e
 +   *          element to be appended to this list
 +   * @return <tt>true</tt> (as specified by {@link Collection#add})
 +   * @throws ArrayIndexOutOfBoundsException
 +   */
-   public synchronized boolean add(Object e) {
-     ensureCapacity(size + 1);
-     elementData[size] = e;
-     ++size;
++  public boolean add(Object e) {
++    synchronized (lock) {
++      ensureCapacity(size + 1);
++      elementData[size] = e;
++      ++size;
++    }
 +    return true;
 +  }
 +
 +  /**
 +   * Removes the first occurrence of the specified element from this list, if it
 +   * is present. If the list does not contain the element, it is unchanged. More
 +   * formally, removes the element with the lowest index <tt>i</tt> such that
 +   * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
 +   * (if such an element exists). Returns <tt>true</tt> if this list contained
 +   * the specified element (or equivalently, if this list changed as a result of
 +   * the call).
 +   * 
 +   * @param o
 +   *          element to be removed from this list, if present
 +   * @return <tt>true</tt> if this list contained the specified element
 +   */
-   public synchronized boolean remove(Object o) {
++  public boolean remove(Object o) {
 +    if (o == null) {
 +      for (int index = 0; index < size; index++)
 +        if (elementData[index] == null) {
 +          fastRemove(index);
 +          return true;
 +        }
 +    } else {
 +      for (int index = 0; index < size; index++)
 +        if (o.equals(elementData[index])) {
 +          fastRemove(index);
 +          return true;
 +        }
 +    }
 +    return false;
 +  }
 +
 +  /*
 +   * Private remove method that skips bounds checking and does not return the
 +   * value removed.
 +   */
 +  private void fastRemove(int index) {
 +    int len = elementData.length;
 +    Object[] newArray = new Object[len - 1];
 +    System.arraycopy(elementData, 0, newArray, 0, index);
 +    int numMoved = len - index - 1;
 +    if (numMoved > 0)
 +      System.arraycopy(elementData, index + 1, newArray, index, numMoved);
-     elementData = newArray;
-     --size;
++
++    synchronized (lock) {
++      elementData = newArray;
++      --size;
++    }
 +  }
 +
 +  /**
 +   * Removes all of the elements from this list. The list will be empty after
 +   * this call returns.
 +   */
 +  public void clear() {
 +    // Let gc do its work
-     for (int i = 0; i < size; i++) {
-       elementData[i] = null;
++    synchronized (lock) {
++      for (int i = 0; i < size; i++) {
++        elementData[i] = null;
++      }
++      size = 0;
 +    }
-     size = 0;
 +  }
 +
 +  /**
 +   * Checks if the given index is in range. If not, throws an appropriate
 +   * runtime exception. This method does *not* check if the index is negative:
 +   * It is always used immediately prior to an array access, which throws an
 +   * ArrayIndexOutOfBoundsException if index is negative.
 +   */
 +  private void RangeCheck(int index) {
 +    if (index >= size) {
 +      throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
 +    }
 +  }
 +
 +  @Override
-   public synchronized boolean addAll(Collection c) {
++  public boolean addAll(Collection c) {
 +    Object[] a = c.toArray();
 +    int numNew = a.length;
-     ensureCapacity(size + numNew);
-     System.arraycopy(a, 0, elementData, size, numNew);
-     size += numNew;
++    synchronized (lock) {
++      ensureCapacity(size + numNew);
++      System.arraycopy(a, 0, elementData, size, numNew);
++      size += numNew;
++    }
 +    return numNew != 0;
 +  }
 +
 +  @Override
 +  public Object[] toArray() {
 +    return Arrays.copyOf(elementData, size);
 +  }
 +
 +  @Override
 +  public Iterator iterator() {
 +    return new IndexArrayListIterator();
 +  }
 +
 +  private class IndexArrayListIterator implements Iterator {
 +    private byte current;
 +    private Object currentEntry;
++    private Object[] elements;
++    private int len;
++
++    IndexArrayListIterator() {
++      synchronized (lock) {
++        elements = elementData;
++        len = size;
++      }
++    }
 +    
 +    /**
 +     * Checks if the array has next element, stores reference to the current
 +     * element and increments cursor. This is required since an element may be
 +     * removed between hasNext() and next() method calls
 +     * 
 +     */
 +    @Override
 +    public boolean hasNext() {
-       return current < size;
++      return current < len;
 +    }
 +
 +    /**
 +     * Returns next element. But does not increment the cursor.
 +     * Always use hasNext() before this method call
 +     */
 +    @Override
 +    public Object next() {
 +      try {
-         currentEntry = elementData[current++];
++        currentEntry = elements[current++];
 +      } catch (IndexOutOfBoundsException e) {
-         // Following exception must never be thrown.
-         //throw new NoSuchElementException();
-         return null;
++        // We should not be coming here as element-data and
++        // size are updated atomically.
++        throw new NoSuchElementException();
++        //return null;
 +      }
 +      return currentEntry;
 +    }
 +
 +    @Override
 +    public void remove() {
 +      throw new UnsupportedOperationException(
 +          "remove() method is not supported");
 +    }
 +
 +  }
 +
 +  @Override
 +  public Object[] toArray(Object[] a) {
 +    throw new UnsupportedOperationException(
 +        "toArray(Object[] a) method is not supported");
 +  }
 +
 +  @Override
 +  public boolean containsAll(Collection c) {
 +    throw new UnsupportedOperationException(
 +        "containsAll() method is not supported");
 +  }
 +
 +  @Override
 +  public boolean removeAll(Collection c) {
 +    throw new UnsupportedOperationException(
 +        "removeAll() method is not supported");
 +  }
 +
 +  @Override
 +  public boolean retainAll(Collection c) {
 +    throw new UnsupportedOperationException(
 +        "retainAll() method is not supported");
 +  }
 +
 +  //for internal testing only
 +  public Object[] getElementData() {
 +    return elementData;
 +  }
 +}


[18/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache/management/MemoryThresholdsDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache/management/MemoryThresholdsDUnitTest.java
index 4e4b10f,0000000..c93848b
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache/management/MemoryThresholdsDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache/management/MemoryThresholdsDUnitTest.java
@@@ -1,2333 -1,0 +1,2333 @@@
 +/*
 + * 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.cache.management;
 +
 +import java.io.Serializable;
 +import java.util.ArrayList;
 +import java.util.HashMap;
 +import java.util.HashSet;
 +import java.util.Iterator;
 +import java.util.List;
 +import java.util.Map;
 +import java.util.Properties;
 +import java.util.Set;
 +import java.util.concurrent.CountDownLatch;
 +import java.util.concurrent.TimeUnit;
 +import java.util.concurrent.atomic.AtomicInteger;
 +
 +import util.TestException;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.AttributesMutator;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheLoader;
 +import com.gemstone.gemfire.cache.CacheLoaderException;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.LoaderHelper;
 +import com.gemstone.gemfire.cache.LowMemoryException;
 +import com.gemstone.gemfire.cache.PartitionAttributesFactory;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionShortcut;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.client.Pool;
 +import com.gemstone.gemfire.cache.client.PoolFactory;
 +import com.gemstone.gemfire.cache.client.PoolManager;
 +import com.gemstone.gemfire.cache.client.ServerOperationException;
 +import com.gemstone.gemfire.cache.control.ResourceManager;
 +import com.gemstone.gemfire.cache.execute.Execution;
 +import com.gemstone.gemfire.cache.execute.FunctionAdapter;
 +import com.gemstone.gemfire.cache.execute.FunctionContext;
 +import com.gemstone.gemfire.cache.execute.FunctionException;
 +import com.gemstone.gemfire.cache.execute.FunctionService;
 +import com.gemstone.gemfire.cache.execute.RegionFunctionContext;
 +import com.gemstone.gemfire.cache.server.CacheServer;
 +import com.gemstone.gemfire.cache30.ClientServerTestCase;
 +import com.gemstone.gemfire.cache30.CacheSerializableRunnable;
 +import com.gemstone.gemfire.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
 +import com.gemstone.gemfire.internal.AvailablePortHelper;
 +import com.gemstone.gemfire.internal.GemFireStatSampler;
 +import com.gemstone.gemfire.internal.LocalStatListener;
 +import com.gemstone.gemfire.internal.StatisticsImpl;
 +import com.gemstone.gemfire.internal.cache.DistributedRegion;
 +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
 +import com.gemstone.gemfire.internal.cache.PartitionedRegion;
 +import com.gemstone.gemfire.internal.cache.PartitionedRegionHelper;
 +import com.gemstone.gemfire.internal.cache.control.HeapMemoryMonitor;
 +import com.gemstone.gemfire.internal.cache.control.InternalResourceManager;
 +import com.gemstone.gemfire.internal.cache.control.InternalResourceManager.ResourceType;
 +import com.gemstone.gemfire.internal.cache.control.MemoryEvent;
 +import com.gemstone.gemfire.internal.cache.control.MemoryThresholds.MemoryState;
 +import com.gemstone.gemfire.internal.cache.control.ResourceAdvisor;
 +import com.gemstone.gemfire.internal.cache.control.ResourceListener;
 +import com.gemstone.gemfire.internal.cache.control.TestMemoryThresholdListener;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.test.dunit.Assert;
 +import com.gemstone.gemfire.test.dunit.AsyncInvocation;
 +import com.gemstone.gemfire.test.dunit.DistributedTestCase;
 +import com.gemstone.gemfire.test.dunit.DistributedTestUtils;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.IgnoredException;
 +import com.gemstone.gemfire.test.dunit.Invoke;
 +import com.gemstone.gemfire.test.dunit.LogWriterUtils;
 +import com.gemstone.gemfire.test.dunit.NetworkUtils;
 +import com.gemstone.gemfire.test.dunit.SerializableCallable;
 +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;
 +
 +/**
 + * Tests the Heap Memory thresholds of {@link ResourceManager}
 + * @author sbawaska
 + * @author Mitch Thomas
 + * @since 6.0
 + */
 +public class MemoryThresholdsDUnitTest extends ClientServerTestCase {
 +  
 +  public static class Range implements Serializable {
 +    public final static Range DEFAULT = new Range(0, 20);
 +    public final int start;
 +    public final int end;
 +    public Range(int s, int e) {this.start = s; this.end = e;}
 +    public Range(Range r, int shift) {this.start = r.start+shift; this.end=r.end+shift;}
 +    public int width() {return end-start;}
 +  }
 +
 +  final String expectedEx = LocalizedStrings.MemoryMonitor_MEMBER_ABOVE_CRITICAL_THRESHOLD
 +                            .getRawText().replaceAll("\\{[0-9]+\\}", ".*?");
 +  final String addExpectedExString =
 +    "<ExpectedException action=add>" + expectedEx + "</ExpectedException>";
 +  final String removeExpectedExString =
 +    "<ExpectedException action=remove>" + expectedEx + "</ExpectedException>";
 +  final String expectedBelow = LocalizedStrings.MemoryMonitor_MEMBER_BELOW_CRITICAL_THRESHOLD
 +                              .getRawText().replaceAll("\\{[0-9]+\\}", ".*?");
 +  final String addExpectedBelow =
 +  "<ExpectedException action=add>" + expectedBelow + "</ExpectedException>";
 +  final String removeExpectedBelow =
 +"<ExpectedException action=remove>" + expectedBelow + "</ExpectedException>";
 +
 +  final String expectedFunctionEx = " cannot be executed";
 +  final String addExpectedFunctionExString =
 +    "<ExpectedException action=add>" + expectedFunctionEx + "</ExpectedException>";
 +  final String removeExpectedFunctionExString =
 +    "<ExpectedException action=remove>" + expectedFunctionEx + "</ExpectedException>";
 +  
 +  public MemoryThresholdsDUnitTest(String name) {
 +    super(name);
 +  }
 +  
 +  @Override
 +  public void setUp() throws Exception {
 +    super.setUp();
 +    Invoke.invokeInEveryVM(this.setHeapMemoryMonitorTestMode);
 +    IgnoredException.addIgnoredException(expectedEx);
 +    IgnoredException.addIgnoredException(expectedBelow);
 +  }
 +
 +  @Override
 +  protected void preTearDownClientServerTestCase() throws Exception {
 +    Invoke.invokeInEveryVM(resetResourceManager);
 +  }
 +
 +  public void testPRClientPutRejection() throws Exception {
 +    doClientServerTest("parRegReject", true/*createPR*/);
 +  }
 +
 +  public void testDistributedRegionClientPutRejection() throws Exception {
 +    doClientServerTest("distrReject", false/*createPR*/);
 +  }
 +
 +  private void doClientServerTest(final String regionName, boolean createPR)
 +      throws Exception {
 +    //create region on the server
 +    final Host host = Host.getHost(0);
 +    final VM server = host.getVM(0);
 +    final VM client = host.getVM(1);
 +
 +    ServerPorts ports = startCacheServer(server, 0f, 90f, regionName, createPR, false, 0);
 +    startClient(client, server, ports.getPort(), regionName);
 +    doPuts(client, regionName, false/*catchServerException*/,
 +        false/*catchLowMemoryException*/);
 +    doPutAlls(client, regionName, false/*catchServerException*/,
 +        false/*catchLowMemoryException*/, Range.DEFAULT);
 +
 +    //make the region sick in the server
 +    server.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        InternalResourceManager irm = (InternalResourceManager)getCache().
 +                                    getResourceManager();
 +        irm.setCriticalHeapPercentage(90f);
 +        
 +        getCache().getLoggerI18n().fine(addExpectedExString);
 +        irm.getHeapMonitor().updateStateAndSendEvent(950);
 +        getCache().getLoggerI18n().fine(removeExpectedExString);
 +        return null;
 +      }
 +    });
 +
 +    //make sure client puts are rejected
 +    doPuts(client, regionName, true/*catchServerException*/,
 +        false/*catchLowMemoryException*/);
 +    doPutAlls(client, regionName, true/*catchServerException*/,
 +        false/*catchLowMemoryException*/, new Range(Range.DEFAULT, Range.DEFAULT.width()+1));
 +  }
 +
 +  public void testDistributedRegionRemotePutRejectionLocalDestroy() throws Exception {
 +    doDistributedRegionRemotePutRejection(true, false);
 +  }
 +  public void testDistributedRegionRemotePutRejectionCacheClose() throws Exception {
 +    doDistributedRegionRemotePutRejection(false, true);
 +  }
 +  public void testDistributedRegionRemotePutRejectionBelowThreshold() throws Exception {
 +    doDistributedRegionRemotePutRejection(false, false);
 +  }
 +  /**
 +   * test that puts in a server are rejected when a remote VM crosses
 +   * critical threshold
 +   * @throws Exception
 +   */
 +  private void doDistributedRegionRemotePutRejection(boolean localDestroy, boolean cacheClose) throws Exception {
 +    final Host host = Host.getHost(0);
 +    final VM server1 = host.getVM(0);
 +    final VM server2 = host.getVM(1);
 +
 +    final String regionName = "rejectRemoteOp";
 +
 +    ServerPorts ports1 = startCacheServer(server1, 0f, 0f,
 +        regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +    ServerPorts ports2 = startCacheServer(server2, 0f, 90f,
 +        regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +
 +    registerTestMemoryThresholdListener(server1);
 +    registerTestMemoryThresholdListener(server2);
 +
 +    doPuts(server1, regionName, false/*catchRejectedException*/,
 +        false/*catchLowMemoryException*/);
 +    doPutAlls(server1, regionName, false/*catchRejectedException*/,
 +        false/*catchLowMemoryException*/, Range.DEFAULT);
 +
 +    //make server2 critical
 +    setUsageAboveCriticalThreshold(server2);
 +
 +    verifyListenerValue(server1, MemoryState.CRITICAL, 1, true);
 +    verifyListenerValue(server2, MemoryState.CRITICAL, 1, false);
 +
 +    //make sure that local server1 puts are rejected
 +    doPuts(server1, regionName, false/*catchRejectedException*/,
 +        true/*catchLowMemoryException*/);
 +    Range r1 = new Range(Range.DEFAULT, Range.DEFAULT.width()+1);
 +    doPutAlls(server1, regionName, false/*catchRejectedException*/,
 +        true/*catchLowMemoryException*/, r1);
 +
 +    if (localDestroy) {
 +    //local destroy the region on sick member
 +    server2.invoke(new SerializableCallable("local destroy") {
 +      public Object call() throws Exception {
 +        Region r = getRootRegion().getSubregion(regionName);
 +        r.localDestroyRegion();
 +        return null;
 +      }
 +    });
 +    } else if (cacheClose) {
 +      server2.invoke(new SerializableCallable() {
 +        public Object call() throws Exception {
 +          getCache().close();
 +          return null;
 +        }
 +      });
 +    } else {
 +      setUsageBelowEviction(server2);
 +    }
 +    
 +    //wait for remote region destroyed message to be processed
 +    server1.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        WaitCriterion wc = new WaitCriterion() {
 +          public String description() {
 +            return "remote localRegionDestroyed message not received";
 +          }
 +          public boolean done() {
 +            DistributedRegion dr = (DistributedRegion)getRootRegion().
 +                                                getSubregion(regionName);
 +            return dr.getMemoryThresholdReachedMembers().size() == 0;
 +          }
 +        };
 +        Wait.waitForCriterion(wc, 30000, 10, true);
 +        return null;
 +      }
 +    });
 +
 +    //make sure puts succeed
 +    doPuts(server1, regionName, false/*catchRejectedException*/,
 +        false/*catchLowMemoryException*/);
 +    Range r2 = new Range(r1, r1.width()+1);
 +    doPutAlls(server1, regionName, false/*catchRejectedException*/,
 +        false/*catchLowMemoryException*/, r2);
 +  }
 +
 +  public void testBug45513() {
 +    ResourceManager rm = getCache().getResourceManager();
 +    assertEquals(0.0f, rm.getCriticalHeapPercentage());
 +    assertEquals(0.0f, rm.getEvictionHeapPercentage());
 +    
 +    rm.setEvictionHeapPercentage(50);
 +    rm.setCriticalHeapPercentage(90);
 +    
 +    // verify
 +    assertEquals(50.0f, rm.getEvictionHeapPercentage());
 +    assertEquals(90.0f, rm.getCriticalHeapPercentage());
 +    
 +    getCache().createRegionFactory(RegionShortcut.REPLICATE_HEAP_LRU).create(getName());
 +    
 +    assertEquals(50.0f, rm.getEvictionHeapPercentage());
 +    assertEquals(90.0f, rm.getCriticalHeapPercentage());
 +  }
 +
 +  /**
 +   * test that puts in a client are rejected when a remote VM crosses
 +   * critical threshold
 +   * @throws Exception
 +   */
 +  public void testDistributedRegionRemoteClientPutRejection() throws Exception {
 +    final Host host = Host.getHost(0);
 +    final VM server1 = host.getVM(0);
 +    final VM server2 = host.getVM(1);
 +    final VM client = host.getVM(2);
 +
 +    final String regionName = "rejectRemoteClientOp";
 +
 +    ServerPorts ports1 = startCacheServer(server1, 0f, 0f, regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +    ServerPorts ports2 = startCacheServer(server2, 0f, 90f,
 +        regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +
 +    startClient(client, server1, ports1.getPort(), regionName);
 +
 +    registerTestMemoryThresholdListener(server1);
 +    registerTestMemoryThresholdListener(server2);
 +
 +    doPuts(client, regionName, false/*catchRejectedException*/,
 +        false/*catchLowMemoryException*/);
 +    doPutAlls(client, regionName, false/*catchRejectedException*/,
 +        false/*catchLowMemoryException*/, Range.DEFAULT);
 +
 +    //make server2 critical
 +    setUsageAboveCriticalThreshold(server2);
 +
 +    verifyListenerValue(server1, MemoryState.CRITICAL, 1, true);
 +    verifyListenerValue(server2, MemoryState.CRITICAL, 1, false);
 +
 +    //make sure that client puts are rejected
 +    doPuts(client, regionName, true/*catchRejectedException*/,
 +        false/*catchLowMemoryException*/);
 +    doPutAlls(client, regionName, true/*catchRejectedException*/,
 +        false/*catchLowMemoryException*/, new Range(Range.DEFAULT, Range.DEFAULT.width()+1));
 +  }
 +
 +
 +  /**
 +   * test that disabling threshold does not cause remote event and
 +   * remote DISABLED events are delivered
 +   * @throws Exception
 +   */
 +  public void testDisabledThresholds() throws Exception {
 +    final Host host = Host.getHost(0);
 +    final VM server1 = host.getVM(0);
 +    final VM server2 = host.getVM(1);
 +
 +    final String regionName = "disableThresholdPr";
 +
 +    ServerPorts ports1 = startCacheServer(server1, 0f, 0f, regionName, true/*createPR*/, false/*notifyBySubscription*/, 0);
 +    ServerPorts ports2 = startCacheServer(server2, 0f, 0f,
 +        regionName, true/*createPR*/, false/*notifyBySubscription*/, 0);
 +
 +    registerTestMemoryThresholdListener(server1);
 +    registerTestMemoryThresholdListener(server2);
 +
 +    setUsageAboveEvictionThreshold(server1);
 +    verifyListenerValue(server1, MemoryState.EVICTION, 0, false);
 +    verifyListenerValue(server2, MemoryState.EVICTION, 0, true);
 +
 +    setThresholds(server1, 80f, 0f);
 +    verifyListenerValue(server1, MemoryState.EVICTION, 1, false);
 +    verifyListenerValue(server2, MemoryState.EVICTION, 1, true);
 +
 +    setUsageAboveCriticalThreshold(server1);
 +    verifyListenerValue(server1, MemoryState.CRITICAL, 0, false);
 +    verifyListenerValue(server2, MemoryState.CRITICAL, 0, true);
 +
 +    setThresholds(server1, 0f, 0f);
 +    verifyListenerValue(server1, MemoryState.EVICTION_DISABLED, 1, false);
 +    verifyListenerValue(server2, MemoryState.EVICTION_DISABLED, 1, true);
 +
 +    setThresholds(server1, 0f, 90f);
 +    verifyListenerValue(server1, MemoryState.CRITICAL, 1, false);
 +    verifyListenerValue(server2, MemoryState.CRITICAL, 1, true);
 +
 +    //verify that stats on server2 are not changed by events on server1
 +    server2.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        InternalResourceManager irm = ((GemFireCacheImpl)getCache()).getResourceManager();
 +        assertEquals(0, irm.getStats().getEvictionStartEvents());
 +        assertEquals(0, irm.getStats().getHeapCriticalEvents());
 +        assertEquals(0, irm.getStats().getCriticalThreshold());
 +        assertEquals(0, irm.getStats().getEvictionThreshold());
 +        return null;
 +      }
 +    });
 +  }
 +
 +  /**
 +   * Make sure appropriate events are delivered when moving
 +   * between states.
 +   * 
 +   * @throws Exception
 +   */
 +  public void testEventDelivery() throws Exception {
 +    final Host host = Host.getHost(0);
 +    final VM server1 = host.getVM(0);
 +    final VM server2 = host.getVM(1);
 +
 +    final String regionName = "testEventDelivery";
 +
 +    ServerPorts ports1 = startCacheServer(server1, 0f, 0f, regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +    ServerPorts ports2 = startCacheServer(server2, 80f, 90f,
 +        regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +
 +    registerLoggingTestMemoryThresholdListener(server1);
 +    registerTestMemoryThresholdListener(server2);
 +
 +    //NORMAL -> CRITICAL
 +    server2.invoke(new SerializableCallable("NORMAL->CRITICAL") {
 +      public Object call() throws Exception {
 +        GemFireCacheImpl gfCache = (GemFireCacheImpl)getCache();
 +        getCache().getLoggerI18n().fine(addExpectedExString);
 +        gfCache.getResourceManager().getHeapMonitor().updateStateAndSendEvent(950);
 +        getCache().getLoggerI18n().fine(removeExpectedExString);
 +        return null;
 +      }
 +    });
 +    verifyListenerValue(server2, MemoryState.CRITICAL, 1, true);
 +    verifyListenerValue(server2, MemoryState.EVICTION, 1, true);
 +    verifyListenerValue(server2, MemoryState.NORMAL, 0, true);
 +
 +    //make sure we get two events on remote server
 +    verifyListenerValue(server1, MemoryState.CRITICAL, 1, true);
 +    verifyListenerValue(server1, MemoryState.EVICTION, 1, true);
 +    verifyListenerValue(server1, MemoryState.NORMAL, 0, true);;
 +
 +    //CRITICAL -> EVICTION
 +    server2.invoke(new SerializableCallable("CRITICAL->EVICTION") {
 +      public Object call() throws Exception {
 +        GemFireCacheImpl gfCache = (GemFireCacheImpl)getCache();
 +        getCache().getLoggerI18n().fine(addExpectedBelow);
 +        gfCache.getResourceManager().getHeapMonitor().updateStateAndSendEvent(850);
 +        getCache().getLoggerI18n().fine(removeExpectedBelow);
 +        return null;
 +      }
 +    });
 +    verifyListenerValue(server2, MemoryState.CRITICAL, 1, true);
 +    verifyListenerValue(server2, MemoryState.EVICTION, 2, true);
 +    verifyListenerValue(server2, MemoryState.NORMAL, 0, true);
 +    verifyListenerValue(server1, MemoryState.CRITICAL, 1, true);
 +    verifyListenerValue(server1, MemoryState.EVICTION, 2, true);
 +    verifyListenerValue(server1, MemoryState.NORMAL, 0, true);;
 +    
 +    //EVICTION -> EVICTION
 +    server2.invoke(new SerializableCallable("EVICTION->EVICTION") {
 +      public Object call() throws Exception {
 +        GemFireCacheImpl gfCache = (GemFireCacheImpl)getCache();
 +        gfCache.getResourceManager().getHeapMonitor().updateStateAndSendEvent(840);
 +        return null;
 +      }
 +    });
 +    verifyListenerValue(server2, MemoryState.CRITICAL, 1, true);
 +    verifyListenerValue(server2, MemoryState.EVICTION, 2, true);
 +    verifyListenerValue(server2, MemoryState.NORMAL, 0, true);
 +    verifyListenerValue(server1, MemoryState.CRITICAL, 1, true);
 +    verifyListenerValue(server1, MemoryState.EVICTION, 2, true);
 +    verifyListenerValue(server1, MemoryState.NORMAL, 0, true);
 +
 +    //EVICTION -> NORMAL
 +    server2.invoke(new SerializableCallable("EVICTION->NORMAL") {
 +      public Object call() throws Exception {
 +        GemFireCacheImpl gfCache = (GemFireCacheImpl)getCache();
 +        gfCache.getResourceManager().getHeapMonitor().updateStateAndSendEvent(750);
 +        return null;
 +      }
 +    });
 +
 +    verifyListenerValue(server2, MemoryState.CRITICAL, 1, true);
 +    verifyListenerValue(server2, MemoryState.EVICTION, 2, true);
 +    verifyListenerValue(server2, MemoryState.NORMAL, 1, true);
 +    verifyListenerValue(server1, MemoryState.CRITICAL, 1, true);
 +    verifyListenerValue(server1, MemoryState.EVICTION, 2, true);
 +    verifyListenerValue(server1, MemoryState.NORMAL, 1, true);
 +    
 +    LogWriterUtils.getLogWriter().info("before NORMAL->CRITICAL->NORMAL");
 +    //NORMAL -> EVICTION -> NORMAL
 +    server2.invoke(new SerializableCallable("NORMAL->CRITICAL->NORMAL") {
 +      public Object call() throws Exception {
 +        GemFireCacheImpl gfCache = (GemFireCacheImpl)getCache();
 +        gfCache.getResourceManager().getHeapMonitor().updateStateAndSendEvent(950);
 +        gfCache.getResourceManager().getHeapMonitor().updateStateAndSendEvent(750);
 +        return null;
 +      }
 +    });
 +    LogWriterUtils.getLogWriter().info("after NORMAL->CRITICAL->NORMAL");
 +
 +    verifyListenerValue(server2, MemoryState.CRITICAL, 2, true);
 +    verifyListenerValue(server2, MemoryState.EVICTION, 3, true);
 +    verifyListenerValue(server2, MemoryState.NORMAL, 2, true);
 +    verifyListenerValue(server1, MemoryState.CRITICAL, 2, true);
 +    verifyListenerValue(server1, MemoryState.EVICTION, 3, true);
 +    verifyListenerValue(server1, MemoryState.NORMAL, 2, true);
 +    
 +    //NORMAL -> EVICTION
 +    server2.invoke(new SerializableCallable("NORMAL->EVICTION") {
 +      public Object call() throws Exception {
 +        GemFireCacheImpl gfCache = (GemFireCacheImpl)getCache();
 +        gfCache.getResourceManager().getHeapMonitor().updateStateAndSendEvent(850);
 +        return null;
 +      }
 +    });
 +
 +    verifyListenerValue(server2, MemoryState.CRITICAL, 2, true);
 +    verifyListenerValue(server2, MemoryState.EVICTION, 4, true);
 +    verifyListenerValue(server2, MemoryState.NORMAL, 2, true);
 +    verifyListenerValue(server1, MemoryState.CRITICAL, 2, true);
 +    verifyListenerValue(server1, MemoryState.EVICTION, 4, true);
 +    verifyListenerValue(server1, MemoryState.NORMAL, 2, true);
 +  }
 +
 +  public void testCleanAdvisorClose() throws Exception {
 +    final Host host = Host.getHost(0);
 +    final VM server1 = host.getVM(0);
 +    final VM server2 = host.getVM(1);
 +    final VM server3 = host.getVM(2);
 +
 +    final String regionName = "testEventOrger";
 +
 +    ServerPorts ports1 = startCacheServer(server1, 0f, 0f, regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +    ServerPorts ports2 = startCacheServer(server2, 0f, 0f,
 +        regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +
 +    verifyProfiles(server1, 2);
 +    verifyProfiles(server2, 2);
 +
 +    server2.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        closeCache();
 +        return null;
 +      }
 +    });
 +
 +    verifyProfiles(server1, 1);
 +
 +    startCacheServer(server3, 0f, 0f,
 +        regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +
 +    verifyProfiles(server1, 2);
 +    verifyProfiles(server3, 2);
 +  }
 +
 +  public void testPR_RemotePutRejectionLocalDestroy() throws Exception {
 +    prRemotePutRejection(false, true, false);
 +  }
 +
 +  public void testPR_RemotePutRejectionCacheClose() throws Exception {
 +    prRemotePutRejection(true, false, false);
 +  }
 +
 +  public void testPR_RemotePutRejection() throws Exception {
 +    prRemotePutRejection(false, false, false);
 +  }
 +
 +  public void testPR_RemotePutRejectionLocalDestroyWithTx() throws Exception {
 +    prRemotePutRejection(false, true, true);
 +  }
 +
 +  public void testPR_RemotePutRejectionCacheCloseWithTx() throws Exception {
 +    prRemotePutRejection(true, false, true);
 +  }
 +
 +  public void testPR_RemotePutRejectionWithTx() throws Exception {
 +    prRemotePutRejection(false, false, true);
 +  }
 +
 +  private void prRemotePutRejection(boolean cacheClose, boolean localDestroy, final boolean useTx) throws Exception {
 +    final Host host = Host.getHost(0);
 +    final VM accessor = host.getVM(0);
 +    final VM server1 = host.getVM(1);
 +    final VM server2 = host.getVM(2);
 +    final VM server3 = host.getVM(3);
 +
 +    final String regionName = "testPrRejection";
 +    final int redundancy = 1;
 +
 +    final ServerPorts ports1 = startCacheServer(server1, 80f, 90f, regionName, true/*createPR*/, false/*notifyBySubscription*/, redundancy);
 +    ServerPorts ports2 = startCacheServer(server2, 80f, 90f,
 +        regionName, true/*createPR*/, false/*notifyBySubscription*/, redundancy);
 +    ServerPorts ports3 = startCacheServer(server3, 80f, 90f,
 +        regionName, true/*createPR*/, false/*notifyBySubscription*/, redundancy);
 +    accessor.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        getSystem(getServerProperties());
 +        getCache();
 +        AttributesFactory factory = new AttributesFactory();        
 +        PartitionAttributesFactory paf = new PartitionAttributesFactory();
 +        paf.setRedundantCopies(redundancy);
 +        paf.setLocalMaxMemory(0);
 +        paf.setTotalNumBuckets(11);
 +        factory.setPartitionAttributes(paf.create());
 +        createRegion(regionName, factory.create());
 +        return null;
 +      }
 +    });
 +    
 +    doPuts(accessor, regionName, false, false);
 +    final Range r1 = Range.DEFAULT;
 +    doPutAlls(accessor, regionName, false, false, r1);
 +    
 +    SerializableCallable getMyId = new SerializableCallable() {
 +      public Object call() throws Exception {        
 +        return ((GemFireCacheImpl)getCache()).getMyId();
 +      }
 +    };
 +    
 +    final DistributedMember server1Id = 
 +      (DistributedMember)server1.invoke(getMyId); 
 +    
 +    setUsageAboveCriticalThreshold(server1);
 +     
 +    accessor.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        final PartitionedRegion pr = (PartitionedRegion)getRootRegion().
 +                                getSubregion(regionName);
 +        final String regionPath = getRootRegion().getSubregion(regionName).getFullPath();
 +        //server1 is sick, look for a key on server1, and attempt put again
 +        WaitCriterion wc = new WaitCriterion() {
 +          public String description() {
 +            return "remote bucket not marked sick";
 +          }
 +          public boolean done() {
 +            boolean keyFoundOnSickMember = false;
 +            boolean caughtException = false;
 +            for (int i=0; i<20; i++) {
 +              Integer key = Integer.valueOf(i);
 +              int hKey = PartitionedRegionHelper.getHashKey(pr, null, key, null, null);
 +              Set<InternalDistributedMember> owners = pr.getRegionAdvisor().getBucketOwners(hKey);
 +              if (owners.contains(server1Id)) {
 +                keyFoundOnSickMember = true;
 +                try {
 +                  if (useTx) getCache().getCacheTransactionManager().begin();
 +                  pr.getCache().getLogger().fine("SWAP:putting in tx:"+useTx);
 +                  pr.put(key, "value");
 +                  if (useTx) getCache().getCacheTransactionManager().commit();
 +                } catch (LowMemoryException ex) {
 +                  caughtException = true;
 +                  if (useTx) getCache().getCacheTransactionManager().rollback();
 +                }
 +              } else {
 +                //puts on healthy member should continue
 +                pr.put(key, "value");
 +              }
 +            }
 +            return keyFoundOnSickMember && caughtException;
 +          }
 +        };
 +        Wait.waitForCriterion(wc, 30000, 10, true);
 +        return null;
 +      }
 +    });
 +
 +    {
 +      Range r2 = new Range(r1, r1.width()+1);
 +      doPutAlls(accessor, regionName, false, true, r2);
 +    }
 +
 +    if (localDestroy) {
 +    //local destroy the region on sick member
 +      server1.invoke(new SerializableCallable("local destroy sick member") {
 +        public Object call() throws Exception {
 +          Region r = getRootRegion().getSubregion(regionName);
 +          LogWriterUtils.getLogWriter().info("PRLocalDestroy");
 +          r.localDestroyRegion();
 +          return null;
 +        }
 +      });
 +    } else if (cacheClose) {
 +      //close cache on sick member
 +      server1.invoke(new SerializableCallable("close cache sick member") {
 +        public Object call() throws Exception {
 +          getCache().close();
 +          return null;
 +        }
 +      });
 +    } else {
 +      setUsageBelowEviction(server1);
 +    }
 +    
 +    //do put all in a loop to allow distribution of message
 +    accessor.invoke(new SerializableCallable("Put in a loop") {
 +      public Object call() throws Exception {
 +        final Region r = getRootRegion().getSubregion(regionName);
 +        WaitCriterion wc = new WaitCriterion() {
 +          public String description() {            
 +            return "pr should have gone un-critical";
 +          }
 +          public boolean done() {
 +            boolean done = true;
 +            for (int i=0; i<20; i++) {
 +              try {
 +                r.put(i,"value");
 +              } catch (LowMemoryException e) {
 +                //expected
 +                done = false;
 +              }
 +            }            
 +            return done;
 +          }
 +        };
 +        Wait.waitForCriterion(wc, 30000, 10, true);
 +        return null;
 +      }
 +    });
 +    doPutAlls(accessor, regionName, false, false, r1);
 +  }
 +
 +  // this test is DISABLED due to test issues.  It sometimes
 +  // fails with a TransactionDataNotColocatedException.  See bug #52222
 +  public void disabledtestTxCommitInCritical() throws Exception {
 +    final Host host = Host.getHost(0);
 +    final VM accessor = host.getVM(0);
 +    final VM server1 = host.getVM(1);
 +    final VM server2 = host.getVM(2);
 +    final VM server3 = host.getVM(3);
 +
 +    final String regionName = "testPrRejection";
 +    final int redundancy = 1;
 +
 +    final ServerPorts ports1 = startCacheServer(server1, 80f, 90f,
 +        regionName, true/*createPR*/, false/*notifyBySubscription*/, redundancy);
 +    ServerPorts ports2 = startCacheServer(server2, 80f, 90f,
 +        regionName, true/*createPR*/, false/*notifyBySubscription*/, redundancy);
 +    ServerPorts ports3 = startCacheServer(server3, 80f, 90f,
 +        regionName, true/*createPR*/, false/*notifyBySubscription*/, redundancy);
 +    
 +    registerTestMemoryThresholdListener(server1);
 +    registerTestMemoryThresholdListener(server2);
 +    registerTestMemoryThresholdListener(server3);
 +    
 +    accessor.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        getSystem(getServerProperties());
 +        getCache();
 +        AttributesFactory factory = new AttributesFactory();        
 +        PartitionAttributesFactory paf = new PartitionAttributesFactory();
 +        paf.setRedundantCopies(redundancy);
 +        paf.setLocalMaxMemory(0);
 +        paf.setTotalNumBuckets(11);
 +        factory.setPartitionAttributes(paf.create());
 +        createRegion(regionName, factory.create());
 +        return null;
 +      }
 +    });
 +    
 +    doPuts(accessor, regionName, false, false);
 +    final Range r1 = Range.DEFAULT;
 +    doPutAlls(accessor, regionName, false, false, r1);
 +    
 +    SerializableCallable getMyId = new SerializableCallable() {
 +      public Object call() throws Exception {        
 +        return ((GemFireCacheImpl)getCache()).getMyId();
 +      }
 +    };
 +    
 +    final DistributedMember server1Id = 
 +      (DistributedMember)server1.invoke(getMyId); 
 +    
 +    final Integer lastKey = (Integer)accessor.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        final PartitionedRegion pr = (PartitionedRegion)getRootRegion().getSubregion(regionName);
 +        getCache().getCacheTransactionManager().begin();
 +        for (int i=0; i< 20; i++) {
 +          Integer key = Integer.valueOf(i);
 +          int hKey = PartitionedRegionHelper.getHashKey(pr, key);
 +          Set<InternalDistributedMember> owners = pr.getRegionAdvisor().getBucketOwners(hKey);
 +          if (owners.contains(server1Id)) {
 +            pr.put(key, "txTest");
 +            return i;
 +          }
 +        }
 +        return null;
 +      }
 +    });
 +    assertNotNull(lastKey);
 +    
 +    setUsageAboveCriticalThreshold(server1);
 +
 +    verifyListenerValue(server1, MemoryState.CRITICAL, 1, true);
 +
 +    accessor.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        final PartitionedRegion pr = (PartitionedRegion)getRootRegion().getSubregion(regionName);
 +        assertTrue(getCache().getCacheTransactionManager().exists());
 +        boolean exceptionThrown = false;
 +        for (int i= lastKey; i< 20; i++) {
 +          Integer key = Integer.valueOf(i);
 +          int hKey = PartitionedRegionHelper.getHashKey(pr, key);
 +          Set<InternalDistributedMember> owners = pr.getRegionAdvisor().getBucketOwners(hKey);
 +          if (owners.contains(server1Id)) {
 +            try {
 +              pr.put(key, "txTest");
 +            } catch (LowMemoryException e) {
 +              exceptionThrown = true;
 +              break;
 +            }
 +          }
 +        }
 +        if (!exceptionThrown) {
 +          fail("expected exception not thrown");
 +        }
 +        getCache().getCacheTransactionManager().commit();
 +        int seenCount = 0;
 +        for (int i=0; i<20; i++) {
 +          if ("txTest".equals(pr.get(i))) {
 +            seenCount++;
 +          }
 +        }
 +        assertEquals(1, seenCount);
 +        return null;
 +      }
 +    });
 +  }
 +
 +  public void testDRFunctionExecutionRejection() throws Exception {
 +    IgnoredException.addIgnoredException("LowMemoryException");
 +    final Host host = Host.getHost(0);
 +    final VM server1 = host.getVM(0);
 +    final VM server2 = host.getVM(1);
 +    final VM client = host.getVM(2);
 +    final String regionName = "drFuncRej";
 +
 +    ServerPorts ports1 = startCacheServer(server1, 80f, 90f, regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +    ServerPorts ports2 = startCacheServer(server2, 80f, 90f,
 +        regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +
 +    startClient(client, server1, ports1.getPort(), regionName);
 +
 +    registerTestMemoryThresholdListener(server1);
 +    registerTestMemoryThresholdListener(server2);
 +    
 +    final RejectFunction function = new RejectFunction();
 +    final RejectFunction function2 = new RejectFunction("noRejFunc", false);
 +    Invoke.invokeInEveryVM(new SerializableCallable("register function") {
 +      public Object call() throws Exception {
 +        FunctionService.registerFunction(function);
 +        FunctionService.registerFunction(function2);
 +        return null;
 +      }
 +    });
 +
 +    doPutAlls(server1, regionName, false, false, Range.DEFAULT);
 +    doPuts(server1, regionName, false, false);
 +
 +    server1.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        FunctionService.onRegion(getRootRegion().getSubregion(regionName))
 +          .execute(function);
 +        FunctionService.onRegion(getRootRegion().getSubregion(regionName))
 +        .execute(function2);
 +        return null;
 +      }
 +    });
 +    //should not fail
 +    server1.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        FunctionService.onMembers(getSystem()).execute(function);
 +        FunctionService.onMembers(getSystem()).execute(function2);
 +        return null;
 +      }
 +    });    
 +    client.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        FunctionService.onRegion(getRootRegion().getSubregion(regionName))
 +          .execute(function).getResult(30, TimeUnit.SECONDS);
 +        FunctionService.onRegion(getRootRegion().getSubregion(regionName))
 +          .execute(function2).getResult(30, TimeUnit.SECONDS);
 +        return null;
 +      }
 +    });
 +    
 +    setUsageAboveCriticalThreshold(server2);
 +
 +    verifyListenerValue(server1, MemoryState.CRITICAL, 1, true);
 +
 +    server1.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        try {
 +          getCache().getLoggerI18n().fine(addExpectedFunctionExString);
 +          FunctionService.onRegion(getRootRegion().getSubregion(regionName))
 +            .execute(function);
 +          getCache().getLoggerI18n().fine(removeExpectedFunctionExString);
 +          fail("expected low memory exception was not thrown");
 +        } catch (LowMemoryException e) {
 +          //expected
 +        }
 +        FunctionService.onRegion(getRootRegion().getSubregion(regionName))
 +        .execute(function2);
 +        return null;
 +      }
 +    });
 +
 +    server1.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        try {
 +          getCache().getLoggerI18n().fine(addExpectedFunctionExString);
 +          FunctionService.onMembers(getSystem()).execute(function);
 +          getCache().getLoggerI18n().fine(removeExpectedFunctionExString);
 +          fail("expected low memory exception was not thrown");
 +        } catch (LowMemoryException e) {
 +          //expected
 +        }
 +        FunctionService.onMembers(getSystem()).execute(function2);
 +        return null;
 +      }
 +    });
 +    client.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        try {
 +          getCache().getLoggerI18n().fine(addExpectedFunctionExString);
 +          FunctionService.onRegion(getRootRegion().getSubregion(regionName))
 +            .execute(function);
 +          getCache().getLoggerI18n().fine(removeExpectedFunctionExString);
 +          fail("expected low memory exception was not thrown");
 +        } catch (FunctionException e) {
 +          if (!(e.getCause().getCause() instanceof LowMemoryException)) {
 +            Assert.fail("unexpected exception ", e);
 +          }
 +          //expected
 +        }
 +        FunctionService.onRegion(getRootRegion().getSubregion(regionName))
 +          .execute(function2);
 +        return null;
 +      }
 +    });
 +  }
 +
 +  // this test is DISABLED due to intermittent failures.  See bug #52222
 +  public void disabledtestPRFunctionExecutionRejection() throws Exception {
 +    IgnoredException.addIgnoredException("LowMemoryException");
 +    final Host host = Host.getHost(0);
 +    final VM accessor = host.getVM(0);
 +    final VM server1 = host.getVM(1);
 +    final VM server2 = host.getVM(2);
 +    final VM client = host.getVM(3);
 +    final String regionName = "prFuncRej";
 +
 +    final ServerPorts ports1 = startCacheServer(server1, 80f, 90f, regionName, true/*createPR*/, false/*notifyBySubscription*/, 0);
 +    ServerPorts ports2 = startCacheServer(server2, 80f, 90f,
 +        regionName, true/*createPR*/, false/*notifyBySubscription*/, 0);
 +    accessor.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        getSystem(getServerProperties());
 +        getCache();
 +        AttributesFactory factory = new AttributesFactory();
 +        PartitionAttributesFactory paf = new PartitionAttributesFactory();
 +        paf.setRedundantCopies(0);
 +        paf.setLocalMaxMemory(0);
 +        paf.setTotalNumBuckets(11);
 +        factory.setPartitionAttributes(paf.create());
 +        createRegion(regionName, factory.create());
 +        return null;
 +      }
 +    });
 +    
 +    startClient(client, server1, ports1.getPort(), regionName);
 +
 +    registerTestMemoryThresholdListener(server1);
 +    registerTestMemoryThresholdListener(server2);
 +    
 +    final RejectFunction function = new RejectFunction();
 +    final RejectFunction function2 = new RejectFunction("noRejFunc", false);
 +    Invoke.invokeInEveryVM(new SerializableCallable("register function") {
 +      public Object call() throws Exception {
 +        FunctionService.registerFunction(function);
 +        FunctionService.registerFunction(function2);
 +        return null;
 +      }
 +    });
 +
 +    doPutAlls(accessor, regionName, false, false, Range.DEFAULT);
 +    doPuts(accessor, regionName, false, false);
 +
 +    accessor.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        FunctionService.onRegion(getRootRegion().getSubregion(regionName))
 +          .execute(function);
 +        FunctionService.onRegion(getRootRegion().getSubregion(regionName))
 +        .execute(function2);
 +        return null;
 +      }
 +    });
 +    //should not fail
 +    accessor.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        FunctionService.onMembers(getSystem()).execute(function);
 +        FunctionService.onMembers(getSystem()).execute(function2);
 +        return null;
 +      }
 +    });    
 +    client.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        FunctionService.onRegion(getRootRegion().getSubregion(regionName))
 +          .execute(function);
 +        FunctionService.onRegion(getRootRegion().getSubregion(regionName))
 +          .execute(function2);
 +        return null;
 +      }
 +    });
 +    
 +    setUsageAboveCriticalThreshold(server2);
 +
 +    verifyListenerValue(server1, MemoryState.CRITICAL, 1, true);
 +
 +    accessor.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        try {
 +          getCache().getLoggerI18n().fine(addExpectedFunctionExString);
 +          FunctionService.onRegion(getRootRegion().getSubregion(regionName))
 +            .execute(function);
 +          getCache().getLoggerI18n().fine(removeExpectedFunctionExString);
 +          fail("expected low memory exception was not thrown");
 +        } catch (LowMemoryException e) {
 +          //expected
 +        }
 +        FunctionService.onRegion(getRootRegion().getSubregion(regionName))
 +        .execute(function2);
 +        return null;
 +      }
 +    });
 +
 +    accessor.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        try {
 +          getCache().getLoggerI18n().fine(addExpectedFunctionExString);
 +          FunctionService.onMembers(getSystem()).execute(function);
 +          getCache().getLoggerI18n().fine(removeExpectedFunctionExString);
 +          fail("expected low memory exception was not thrown");
 +        } catch (LowMemoryException e) {
 +          //expected
 +        }
 +        FunctionService.onMembers(getSystem()).execute(function2);
 +        return null;
 +      }
 +    });
 +
 +    server1.invoke(addExpectedFunctionException);
 +    server2.invoke(addExpectedFunctionException);
 +
 +    client.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        try {
 +          FunctionService.onRegion(getRootRegion().getSubregion(regionName))
 +            .execute(function);
 +          fail("expected low memory exception was not thrown");
 +        } catch (FunctionException e) {
 +          if (!(e.getCause().getCause() instanceof LowMemoryException)) {
 +            Assert.fail("unexpected exception", e);
 +          }
 +          //expected
 +        }
 +        FunctionService.onRegion(getRootRegion().getSubregion(regionName))
 +          .execute(function2);
 +        return null;
 +      }
 +    });
 +
 +    server1.invoke(removeExpectedFunctionException);
 +    server2.invoke(removeExpectedFunctionException);
 +
 +    final DistributedMember server2Id = (DistributedMember)server2.invoke(
 +        new SerializableCallable() {
 +          public Object call() throws Exception {
 +            return ((GemFireCacheImpl)getCache()).getMyId();
 +          }
 +        });
 +
 +    //test function execution on healthy & sick members
 +    accessor.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        PartitionedRegion pr = (PartitionedRegion)getRootRegion().
 +                          getSubregion(regionName);
 +        Object sickKey1 = null;
 +        Object sickKey2 = null;
 +        Object healthyKey = null;
 +        for (int i=0; i < 20; i++) {
 +          Object key = i;
 +          DistributedMember m = pr.getMemberOwning(key);
 +          if (m.equals(server2Id)) {
 +            if (sickKey1 == null) {
 +              sickKey1 = key;
 +              //execute
 +              Set s = new HashSet();
 +              s.add(sickKey1);
 +              Execution e = FunctionService.onRegion(pr);
 +              try {
 +                getCache().getLoggerI18n().fine(addExpectedFunctionExString);
 +                e.withFilter(s).withArgs((Serializable)s).execute(function);
 +                getCache().getLoggerI18n().fine(removeExpectedFunctionExString);
 +                fail("expected LowMemoryExcception was not thrown");
 +              } catch (LowMemoryException ex) {
 +                //expected
 +              }
 +            } else if (sickKey2 == null) {
 +              sickKey2 = key;
 +              //execute
 +              Set s = new HashSet();
 +              s.add(sickKey1);
 +              s.add(sickKey2);
 +              Execution e = FunctionService.onRegion(pr);
 +              try {
 +                e.withFilter(s).withArgs((Serializable)s).execute(function);
 +                fail("expected LowMemoryExcception was not thrown");
 +              } catch (LowMemoryException ex) {
 +                //expected
 +              }
 +            }
 +          } else {
 +            healthyKey = key;
 +            //execute
 +            Set s = new HashSet();
 +            s.add(healthyKey);
 +            Execution e = FunctionService.onRegion(pr);
 +            e.withFilter(s).withArgs((Serializable)s).execute(function);
 +          }
 +          if (sickKey1 != null && sickKey2 != null && healthyKey != null) {
 +            break;
 +          }
 +        }
 +        return null;
 +      }
 +    });
 +  }
 +  
 +  public void testFunctionExecutionRejection() throws Exception {
 +    final Host host = Host.getHost(0);
 +    final VM server1 = host.getVM(0);
 +    final VM server2 = host.getVM(1);
 +    final VM client = host.getVM(2);
 +    final String regionName = "FuncRej";
 +
 +    ServerPorts ports1 = startCacheServer(server1, 80f, 90f, regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +    ServerPorts ports2 = startCacheServer(server2, 80f, 90f,
 +        regionName, false/*createPR*/, false/*notifyBySubscription*/, 0);
 +
 +    startClient(client, server1, ports1.getPort(), regionName);
 +
 +    registerTestMemoryThresholdListener(server1);
 +    registerTestMemoryThresholdListener(server2);
 +    
 +    final RejectFunction function = new RejectFunction();
 +    final RejectFunction function2 = new RejectFunction("noRejFunc", false);
 +    Invoke.invokeInEveryVM(new SerializableCallable("register function") {
 +      public Object call() throws Exception {
 +        FunctionService.registerFunction(function);
 +        FunctionService.registerFunction(function2);
 +        return null;
 +      }
 +    });
 +
 +    client.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        Pool p = PoolManager.find("pool1");
 +        assertTrue(p != null);
 +        FunctionService.onServers(p).execute(function);
 +        FunctionService.onServers(p).execute(function2);
 +        return null;
 +      }
 +    });
 +    
 +    final DistributedMember s1 = (DistributedMember)server1.invoke(getDistributedMember);
 +
 +    server2.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        FunctionService.onMembers(getSystem()).execute(function);
 +        FunctionService.onMember(getSystem(), s1).execute(function);
 +        FunctionService.onMembers(getSystem()).execute(function2);
 +        FunctionService.onMember(getSystem(), s1).execute(function2);
 +        return null;
 +      }
 +    });
 +
 +    setUsageAboveCriticalThreshold(server1);
 +
 +    verifyListenerValue(server2, MemoryState.CRITICAL, 1, true);
 +
 +    server1.invoke(addExpectedFunctionException);
 +    server2.invoke(addExpectedFunctionException);
 +
 +    client.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        Pool p = PoolManager.find("pool1");
 +        assertTrue(p != null);
 +        try {
 +          FunctionService.onServers(p).execute(function);
 +          fail("expected LowMemoryExcception was not thrown");
 +        } catch (ServerOperationException e) {
 +          if (!(e.getCause().getMessage().matches(".*low.*memory.*"))) {
 +            Assert.fail("unexpected exception", e);
 +          }
 +          //expected
 +        }
 +        FunctionService.onServers(p).execute(function2);
 +        return null;
 +      }
 +    });
 +
 +    server2.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        try {
 +          FunctionService.onMembers(getSystem()).execute(function);
 +          fail("expected LowMemoryExcception was not thrown");
 +        } catch (LowMemoryException e) {          
 +          //expected
 +        }
 +        try {
 +          FunctionService.onMember(getSystem(), s1).execute(function);
 +          fail("expected LowMemoryExcception was not thrown");
 +        } catch (LowMemoryException e) {
 +          //expected
 +        }
 +        FunctionService.onMembers(getSystem()).execute(function2);
 +        FunctionService.onMember(getSystem(), s1).execute(function2);
 +        return null;
 +      }
 +    });
 +    server1.invoke(removeExpectedFunctionException);
 +    server2.invoke(removeExpectedFunctionException);
 +  }
 +
 +  SerializableCallable getDistributedMember = new SerializableCallable() {
 +    public Object call() throws Exception {
 +      return ((GemFireCacheImpl)getCache()).getMyId();
 +    };
 +  };
 +
 +  /**
 +   * Starts up a CacheServer.
 +   * @return a {@link ServerPorts} containing the CacheServer ports.
 +   */
 +  private ServerPorts startCacheServer(VM server, final float evictionThreshold, final float criticalThreshold, final String regionName,
 +      final boolean createPR, final boolean notifyBySubscription, final int prRedundancy) throws Exception {
 +
 +    return (ServerPorts) server.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        getSystem(getServerProperties());
 +        GemFireCacheImpl cache = (GemFireCacheImpl)getCache();
 +
 +        InternalResourceManager irm = cache.getResourceManager();
 +        HeapMemoryMonitor hmm = irm.getHeapMonitor();
 +        hmm.setTestMaxMemoryBytes(1000);
 +        HeapMemoryMonitor.setTestBytesUsedForThresholdSet(500);
 +        irm.setEvictionHeapPercentage(evictionThreshold);
 +        irm.setCriticalHeapPercentage(criticalThreshold);
 +
 +        AttributesFactory factory = new AttributesFactory();
 +        if (createPR) {
 +          PartitionAttributesFactory paf = new PartitionAttributesFactory();
 +          paf.setRedundantCopies(prRedundancy);
 +          paf.setTotalNumBuckets(11);
 +          factory.setPartitionAttributes(paf.create());
 +        } else {
 +          factory.setScope(Scope.DISTRIBUTED_ACK);
 +          factory.setDataPolicy(DataPolicy.REPLICATE);
 +        }
 +        Region region = createRegion(regionName, factory.create());
 +        if (createPR) {
 +          assertTrue(region instanceof PartitionedRegion);
 +        } else {
 +          assertTrue(region instanceof DistributedRegion);
 +        }
 +        CacheServer cacheServer = getCache().addCacheServer();
 +        int port = AvailablePortHelper.getRandomAvailableTCPPorts(1)[0];
 +        cacheServer.setPort(port);
 +        cacheServer.setNotifyBySubscription(notifyBySubscription);
 +        cacheServer.start();
 +        
 +        return new ServerPorts(port);
 +      }
 +    });
 +  }
 +
 +  private void startClient(VM client, final VM server, final int serverPort,
 +      final String regionName) {
 +
 +    client.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        getSystem(getClientProps());
 +        getCache();
 +
 +        PoolFactory pf = PoolManager.createFactory();
 +        pf.addServer(NetworkUtils.getServerHostName(server.getHost()), serverPort);
 +        pf.create("pool1");
 +        
 +        AttributesFactory af = new AttributesFactory();
 +        af.setScope(Scope.LOCAL);
 +        af.setPoolName("pool1");        
 +        createRegion(regionName, af.create());
 +        return null;
 +      }
 +    });
 +  }
 +
 +  private void doPuts(VM vm, final String regionName,
 +      final boolean catchServerException, final boolean catchLowMemoryException) {
 +
 +    vm.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        Region r = getRootRegion().getSubregion(regionName);
 +        try {
 +          r.put(Integer.valueOf(0), "value-1");
 +          if (catchServerException || catchLowMemoryException) {
 +            fail("An expected ResourceException was not thrown");
 +          }
 +        } catch (ServerOperationException ex) {
 +          if (!catchServerException) {
 +            Assert.fail("Unexpected exception: ", ex);
 +          }
 +          if (!(ex.getCause() instanceof LowMemoryException)) {
 +            Assert.fail("Unexpected exception: ", ex);
 +          }
 +        } catch (LowMemoryException low) {
 +          if (!catchLowMemoryException) {
 +            Assert.fail("Unexpected exception: ", low);
 +          }
 +        }
 +        return null;
 +      }
 +    });
 +  }
 +
 +  private void doPutAlls(VM vm, final String regionName,
 +      final boolean catchServerException, final boolean catchLowMemoryException, 
 +      final Range rng) {
 +
 +    vm.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        Region r = getRootRegion().getSubregion(regionName);
 +        Map<Integer, String> temp = new HashMap<Integer, String>();
 +        for (int i=rng.start; i<rng.end; i++) {
 +          Integer k = Integer.valueOf(i);
 +          temp.put(k, "value-"+i);
 +        }
 +        try {
 +          r.putAll(temp);
 +          if (catchServerException || catchLowMemoryException) {
 +            fail("An expected ResourceException was not thrown");
 +          }
 +          for (Map.Entry<Integer, String> me: temp.entrySet()) {
 +            assertEquals(me.getValue(), r.get(me.getKey()));
 +          }
 +        } catch (ServerOperationException ex) {
 +          if (!catchServerException) {
 +            Assert.fail("Unexpected exception: ", ex);
 +          }
 +          if (!(ex.getCause() instanceof LowMemoryException)) {
 +            Assert.fail("Unexpected exception: ", ex);
 +          }
 +          for(Integer me: temp.keySet()) {
 +            assertFalse("Key " + me + " should not exist", r.containsKey(me));
 +          }
 +        } catch (LowMemoryException low) {
 +          LogWriterUtils.getLogWriter().info("Caught LowMemoryException", low);
 +          if (!catchLowMemoryException) {
 +            Assert.fail("Unexpected exception: ", low);
 +          }
 +          for(Integer me: temp.keySet()) {
 +            assertFalse("Key " + me + " should not exist", r.containsKey(me));
 +          }
 +        }
 +        return null;
 +      }
 +    });
 +  }
 +
 +  private void setUsageAboveCriticalThreshold(VM vm) {
 +    vm.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        getCache().getLoggerI18n().fine(addExpectedExString);
 +        ((GemFireCacheImpl)getCache()).getResourceManager().getHeapMonitor().updateStateAndSendEvent(950);
 +        HeapMemoryMonitor.setTestBytesUsedForThresholdSet(950);
 +        getCache().getLoggerI18n().fine(removeExpectedExString);
 +        return null;
 +      }
 +    });
 +  }
 +
 +  private void setUsageAboveEvictionThreshold(VM vm) {
 +    vm.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        getCache().getLoggerI18n().fine(addExpectedBelow);
 +        HeapMemoryMonitor.setTestBytesUsedForThresholdSet(850);
 +        ((GemFireCacheImpl)getCache()).getResourceManager().getHeapMonitor().updateStateAndSendEvent(850);
 +        getCache().getLoggerI18n().fine(removeExpectedBelow);
 +        return null;
 +      }
 +    });
 +  }
 +  
 +  private void setUsageBelowEviction(VM vm) {
 +    vm.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        getCache().getLoggerI18n().fine(addExpectedBelow);
 +        ((GemFireCacheImpl)getCache()).getResourceManager().getHeapMonitor().updateStateAndSendEvent(750);
 +        getCache().getLoggerI18n().fine(removeExpectedBelow);
 +        return null;
 +      }
 +    });
 +  }
 +
 +  private void setThresholds(VM server, final float evictionThreshold,
 +      final float criticalThreshold) {
 +    
 +    server.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        ResourceManager irm = getCache().getResourceManager();
 +        irm.setCriticalHeapPercentage(criticalThreshold);
 +        irm.setEvictionHeapPercentage(evictionThreshold);
 +        return null;
 +      }
 +    });
 +  }
 +  
 +  private void registerTestMemoryThresholdListener(VM vm) {
 +    vm.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        TestMemoryThresholdListener listener = new TestMemoryThresholdListener();
 +        InternalResourceManager irm = ((GemFireCacheImpl)getCache()).getResourceManager();
 +        irm.addResourceListener(ResourceType.HEAP_MEMORY, listener);
 +        assertTrue(irm.getResourceListeners(ResourceType.HEAP_MEMORY).contains(listener));
 +        return null;
 +      }
 +    });
 +  }
 +
 +  private void registerLoggingTestMemoryThresholdListener(VM vm) {
 +    vm.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        TestMemoryThresholdListener listener = new TestMemoryThresholdListener(true);
 +        InternalResourceManager irm = ((GemFireCacheImpl)getCache()).getResourceManager();
 +        irm.addResourceListener(ResourceType.HEAP_MEMORY, listener);
 +        assertTrue(irm.getResourceListeners(ResourceType.HEAP_MEMORY).contains(listener));
 +        return null;
 +      }
 +    });
 +  }
 +
 +  /**
 +   * Verifies that the test listener value on the given vm is what is expected
 +   * Note that for remote events useWaitCriterion must be true
 +   * @param vm the vm where verification should take place
 +   * @param state
 +   * @param value the expected value
 +   * @param useWaitCriterion must be true for remote events
 +   */
 +  private void verifyListenerValue(VM vm, final MemoryState state,
 +      final int value, final boolean useWaitCriterion) {
 +    vm.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        WaitCriterion wc = null;
 +        Set<ResourceListener> listeners = getGemfireCache().getResourceManager().getResourceListeners(ResourceType.HEAP_MEMORY);
 +        TestMemoryThresholdListener tmp_listener = null;
 +        Iterator<ResourceListener> it = listeners.iterator();
 +        while (it.hasNext()) {
 +          ResourceListener<MemoryEvent> l = it.next();
 +          if (l instanceof TestMemoryThresholdListener) {
 +            tmp_listener = (TestMemoryThresholdListener) l;
 +            break;
 +          }
 +        }
 +        final TestMemoryThresholdListener listener = tmp_listener == null ? null : tmp_listener;
 +        switch(state) {
 +        case CRITICAL:
 +          if (useWaitCriterion) {
 +            wc = new WaitCriterion() {
 +              public String description() {
 +                return "Remote CRITICAL assert failed "+listener.toString();
 +              }
 +              public boolean done() {
 +                return value == listener.getCriticalThresholdCalls();
 +              }
 +            };
 +          } else {
 +            assertEquals(value, listener.getCriticalThresholdCalls());
 +          }
 +          break;
 +        case CRITICAL_DISABLED:
 +          if (useWaitCriterion) {
 +            wc = new WaitCriterion() {
 +              public String description() {
 +                return "Remote CRITICAL_DISABLED assert failed "+listener.toString();
 +              }
 +              public boolean done() {
 +                return value == listener.getCriticalDisabledCalls();
 +              }
 +            };
 +          } else {
 +            assertEquals(value, listener.getCriticalDisabledCalls());
 +          }
 +          break;
 +        case EVICTION:
 +          if (useWaitCriterion) {
 +            wc = new WaitCriterion() {
 +              public String description() {
 +                return "Remote EVICTION assert failed "+listener.toString();
 +              }
 +              public boolean done() {
 +                return value == listener.getEvictionThresholdCalls();
 +              }
 +            };
 +          } else {
 +            assertEquals(value, listener.getEvictionThresholdCalls());
 +          }
 +          break;
 +        case EVICTION_DISABLED:
 +          if (useWaitCriterion) {
 +            wc = new WaitCriterion() {
 +              public String description() {
 +                return "Remote EVICTION_DISABLED assert failed "+listener.toString();
 +              }
 +              public boolean done() {
 +                return value == listener.getEvictionDisabledCalls();
 +              }
 +            };
 +          } else {
 +            assertEquals(value, listener.getEvictionDisabledCalls());
 +          }
 +          break;
 +        case NORMAL:
 +          if (useWaitCriterion) {
 +            wc = new WaitCriterion() {
 +              public String description() {
 +                return "Remote NORMAL assert failed "+listener.toString();
 +              }
 +              public boolean done() {
 +                return value == listener.getNormalCalls();
 +              }
 +            };
 +          } else {
 +            assertEquals(value, listener.getNormalCalls());
 +          }
 +          break;
 +          default:
 +            throw new IllegalStateException("Unknown memory state");
 +        }
 +        if (useWaitCriterion) {
 +          Wait.waitForCriterion(wc, 30000, 10, true);
 +        }
 +        return null;
 +      }
 +    });
 +  }
 +
 +  private void verifyProfiles(VM vm, final int numberOfProfiles) {
 +    vm.invoke(new SerializableCallable() {
 +      public Object call() throws Exception {
 +        InternalResourceManager irm = ((GemFireCacheImpl)getCache()).getResourceManager();
 +        final ResourceAdvisor ra = irm.getResourceAdvisor();
 +        WaitCriterion wc = new WaitCriterion() {
 +          public String description() {
 +            return "verify profiles failed. Current profiles: " + ra.adviseGeneric();
 +          }
 +          public boolean done() {
 +            return numberOfProfiles == ra.adviseGeneric().size();
 +          }
 +        };
 +        Wait.waitForCriterion(wc, 30000, 10, true);
 +        return null;
 +      }
 +    });
 +  }
 +
 +  protected Properties getClientProps() {
 +    Properties p = new Properties();
 +    p.setProperty(DistributionConfig.MCAST_PORT_NAME, "0");
 +    p.setProperty(DistributionConfig.LOCATORS_NAME, "");
 +    return p;
 +  }
 +
 +  protected Properties getServerProperties() {
 +    Properties p = new Properties();
 +    p.setProperty(DistributionConfig.LOCATORS_NAME, "localhost["+DistributedTestUtils.getDUnitLocatorPort()+"]");
 +    return p;
 +  }
 +
 +  private SerializableCallable setHeapMemoryMonitorTestMode = new SerializableCallable() {
 +    public Object call() throws Exception {
 +      HeapMemoryMonitor.setTestDisableMemoryUpdates(true);
 +      return null;
 +    }
 +  };
 +  
 +  private SerializableCallable resetResourceManager = new SerializableCallable() {
 +    public Object call() throws Exception {
 +      InternalResourceManager irm = ((GemFireCacheImpl)getCache()).getResourceManager();
 +      // Reset CRITICAL_UP by informing all that heap usage is now 1 byte (0 would disable).
 +      irm.getHeapMonitor().updateStateAndSendEvent(1);
 +      Set<ResourceListener> listeners = irm.getResourceListeners(ResourceType.HEAP_MEMORY);
 +      Iterator<ResourceListener> it = listeners.iterator();
 +      while (it.hasNext()) {
 +        ResourceListener<MemoryEvent> l = it.next();
 +        if (l instanceof TestMemoryThresholdListener) {
 +          ((TestMemoryThresholdListener)l).resetThresholdCalls();
 +        }
 +      }
 +      irm.getHeapMonitor().setTestMaxMemoryBytes(0);
 +      HeapMemoryMonitor.setTestDisableMemoryUpdates(false);
 +      return null;
 +    }
 +  };
 +
 +  private class RejectFunction extends FunctionAdapter {
 +    private boolean optimizeForWrite = true;
 +    private String id = "RejectFunction";
 +    public RejectFunction() {
 +    }
 +    public RejectFunction(String id, boolean optimizeForWrite) {
 +      this.id = id;
 +      this.optimizeForWrite = optimizeForWrite;
 +    }
 +    @Override
 +    public void execute(FunctionContext context) {
 +      if (context instanceof RegionFunctionContext) {
 +        RegionFunctionContext regionContext = (RegionFunctionContext)context;
 +        Region dataSet = regionContext.getDataSet();
 +        dataSet.get(1);
 +        regionContext.getResultSender().lastResult("executed");
 +      } else {
 +        context.getResultSender().lastResult("executed");
 +      }
 +    }
 +    @Override
 +    public String getId() {
 +      return id;
 +    }
 +
 +    @Override
 +    public boolean hasResult() {
 +      return true;
 +    }
 +    @Override
 +    public boolean optimizeForWrite() {
 +      return optimizeForWrite;
 +    }
 +    public void setOptimizeForWrite(boolean optimizeForWrite) {
 +      this.optimizeForWrite = optimizeForWrite;
 +    }
 +    public boolean isHA() {
 +      return false;
 +    }
 +  }
 +
 +  /**
 +   * putting this test here because junit does not have host stat sampler enabled
 +   */
 +  public void testLocalStatListenerRegistration() throws Exception{
 +    final CountDownLatch latch = new CountDownLatch(1);
 +    Cache cache = getCache();
 +    InternalDistributedSystem internalSystem = (InternalDistributedSystem)cache.
 +                                                getDistributedSystem();
 +    final GemFireStatSampler sampler = internalSystem.getStatSampler();
 +    sampler.waitForInitialization(10000); // fix: remove infinite wait 
 +    final LocalStatListener l = new LocalStatListener(){
 +      public void statValueChanged(double value) {
 +        latch.countDown();
 +      }
 +    };
 +    final String tenuredPoolName = HeapMemoryMonitor.getTenuredMemoryPoolMXBean().getName();
 +    LogWriterUtils.getLogWriter().info("TenuredPoolName:"+tenuredPoolName);
 +    final List list = internalSystem.getStatsList();
 +    assertFalse(list.isEmpty());
 +    
 +    // fix: found race condition here...
 +    WaitCriterion wc = new WaitCriterion() {
 +      public boolean done() {
 +        int i=0;
 +        synchronized (list) {
 +          for (Object o : list) {
 +            LogWriterUtils.getLogWriter().info("List:"+(++i)+":"+o);
 +            if (o instanceof StatisticsImpl) {
 +              StatisticsImpl si = (StatisticsImpl)o;
 +              LogWriterUtils.getLogWriter().info("stat:"+si.getTextId());
 +              if (si.getTextId().contains(tenuredPoolName)) {
 +                sampler.addLocalStatListener(l, si, "currentUsedMemory");
 +                return true;
 +              }
 +            }
 +          }
 +        }
 +        return false;
 +      }
 +      public String description() {
 +        return "Waiting for " + tenuredPoolName + " statistics to be added to create listener for";
 +      }
 +    };
 +    Wait.waitForCriterion(wc, 5000, 10, true);
 +    
 +    assertTrue("expected at least one stat listener, found " +
 +        sampler.getLocalListeners().size(),
 +        sampler.getLocalListeners().size() > 0);            
 +    long maxTenuredMemory = HeapMemoryMonitor.getTenuredPoolMaxMemory();
 +    AttributesFactory factory = new AttributesFactory();
 +    factory.setScope(Scope.LOCAL);
 +    Region r = createRegion(getUniqueName()+"region", factory.create());
 +    //keep putting objects (of size 1% of maxTenuredMemory) and wait for stat callback
 +    //if we don't get a callback after 75 attempts, throw exception
 +    int count = 0;
 +    while (true) {
 +      count++;
 +      if (count > 75) {
 +        throw new TestException("Did not receive a stat listener callback");
 +      }
 +      byte[] value = new byte[(int)(maxTenuredMemory*0.01)];
 +      r.put("key-"+count, value);
 +      if (latch.await(50, TimeUnit.MILLISECONDS)) {
 +        break;
 +      } else {
 +        continue;
 +      }
 +    }
 +    r.close();
 +  }
 +  
 +  /**
 +   * Test that LocalRegion cache Loads are not stored in the Region
 +   * if the VM is in a critical state, then test that they are allowed
 +   * once the VM is no longer critical
 +   * @throws Exception
 +   */
 +  public void testLRLoadRejection() throws Exception {
 +    final Host host = Host.getHost(0);
 +    final VM vm = host.getVM(2);
 +    final String rName = getUniqueName();
 +    final float criticalHeapThresh = 0.90f;
 +    final int fakeHeapMaxSize = 1000;
 +
-     vm.invoke(DistributedTestCase.class, "disconnectFromDS");
++    vm.invoke(() -> DistributedTestCase.disconnectFromDS());
 +    
 +    vm.invoke(new CacheSerializableRunnable("test LocalRegion load passthrough when critical") {
 +      @Override
 +      public void run2() throws CacheException {
 +        InternalResourceManager irm = (InternalResourceManager)getCache().getResourceManager();
 +        HeapMemoryMonitor hmm = irm.getHeapMonitor();
 +        long fakeHeapUsage = Math.round(fakeHeapMaxSize * (criticalHeapThresh - 0.5f));  // below critical by 50%
 +        assertTrue(fakeHeapMaxSize > 0);
 +        irm.getHeapMonitor().setTestMaxMemoryBytes(fakeHeapMaxSize);
 +        HeapMemoryMonitor.setTestBytesUsedForThresholdSet(fakeHeapUsage);
 +        irm.setCriticalHeapPercentage((criticalHeapThresh * 100.0f));
 +        AttributesFactory<Integer, String> af = new AttributesFactory<Integer, String>();
 +        af.setScope(Scope.LOCAL);
 +        final AtomicInteger numLoaderInvocations = new AtomicInteger();
 +        af.setCacheLoader(new CacheLoader<Integer, String>() {
 +          public String load(LoaderHelper<Integer, String> helper)
 +          throws CacheLoaderException {
 +            numLoaderInvocations.incrementAndGet();
 +            return helper.getKey().toString();
 +          }
 +          public void close() {}
 +        });
 +        Region<Integer, String> r = getCache().createRegion(rName, af.create());
 +        
 +        assertFalse(hmm.getState().isCritical());
 +        int expectedInvocations = 0;
 +        assertEquals(expectedInvocations++, numLoaderInvocations.get());
 +        {
 +          Integer k = new Integer(1);
 +          assertEquals(k.toString(), r.get(k));
 +        }
 +        assertEquals(expectedInvocations++, numLoaderInvocations.get());
 +        expectedInvocations++; expectedInvocations++;
 +        r.getAll(createRanges(10, 12));
 +        assertEquals(expectedInvocations++, numLoaderInvocations.get());
 +        
 +        getCache().getLoggerI18n().fine(addExpectedExString);
 +        fakeHeapUsage = Math.round(fakeHeapMaxSize * (criticalHeapThresh + 0.1f));  // usage above critical by 10%
 +        assertTrue(fakeHeapUsage > 0);
 +        assertTrue(fakeHeapUsage <= fakeHeapMaxSize);
 +        hmm.updateStateAndSendEvent(fakeHeapUsage);
 +        getCache().getLoggerI18n().fine(removeExpectedExString);
 +        
 +        assertTrue(hmm.getState().isCritical());
 +        { 
 +          Integer k = new Integer(2);
 +          assertEquals(k.toString(), r.get(k));
 +        }
 +        assertEquals(expectedInvocations++, numLoaderInvocations.get());
 +        expectedInvocations++; expectedInvocations++;
 +        r.getAll(createRanges(13, 15));
 +        assertEquals(expectedInvocations++, numLoaderInvocations.get());
 +
 +        fakeHeapUsage = Math.round(fakeHeapMaxSize * (criticalHeapThresh - 0.3f));  // below critical by 30%
 +        assertTrue(fakeHeapMaxSize > 0);
 +        getCache().getLoggerI18n().fine(addExpectedBelow);
 +        hmm.updateStateAndSendEvent(fakeHeapUsage);
 +        getCache().getLoggerI18n().fine(removeExpectedBelow);
 +        assertFalse(hmm.getState().isCritical());
 +        {
 +          Integer k = new Integer(3);
 +          assertEquals(k.toString(), r.get(k));
 +        }
 +        assertEquals(expectedInvocations++, numLoaderInvocations.get());
 +        expectedInvocations++; expectedInvocations++;
 +        r.getAll(createRanges(16, 18));
 +        assertEquals(expectedInvocations, numLoaderInvocations.get());
 +
 +        // Do extra validation that the entry doesn't exist in the local region
 +        for (Integer i: createRanges(2, 2, 13, 15)) {
 +          if (r.containsKey(i)) {
 +            fail("Expected containsKey return false for key" + i);
 +          }
 +          if (r.getEntry(i) != null) {
 +            fail("Expected getEntry to return null for key" + i);
 +          }
 +        }
 +      }
 +    });
 +  }
 +
 +  /** Create a list of integers consisting of the ranges defined by the provided
 +  * argument e.g..  createRanges(1, 4, 10, 12) means create ranges 1 through 4 and
 +  * 10 through 12 and should yield the list:
 +  * 1, 2, 3, 4, 10, 11, 12
 +  */ 
 +  public static List<Integer> createRanges(int... startEnds) {
 +    assert startEnds.length % 2 == 0;
 +    ArrayList<Integer> ret = new ArrayList<Integer>();
 +    for (int si=0; si<startEnds.length; si++) { 
 +      final int start = startEnds[si++];
 +      final int end = startEnds[si];
 +      assert end >= start;
 +      ret.ensureCapacity(ret.size() + ((end-start)+1));
 +      for (int i=start; i<=end; i++) {
 +        ret.add(new Integer(i));
 +      }
 +    }
 +    return ret;
 +  }
 +
 +  
 +  /**
 +   * Test that DistributedRegion cacheLoade and netLoad are passed through to the 
 +   * calling thread if the local VM is in a critical state.  Once the VM has moved
 +   * to a safe state then test that they are allowed.
 +   * @throws Exception
 +   */
 +  public void testDRLoadRejection() throws Exception {
 +    final Host host = Host.getHost(0);
 +    final VM replicate1 = host.getVM(2);
 +    final VM replicate2 = host.getVM(3);
 +    final String rName = getUniqueName();
 +    final float criticalHeapThresh = 0.90f;
 +    final int fakeHeapMaxSize = 1000;
 +
 +    // Make sure the desired VMs will have a fresh DS.
-     AsyncInvocation d1 = replicate1.invokeAsync(DistributedTestCase.class, "disconnectFromDS");
-     AsyncInvocation d2 = replicate2.invokeAsync(DistributedTestCase.class, "disconnectFromDS");
++    AsyncInvocation d1 = replicate1.invokeAsync(() -> DistributedTestCase.disconnectFromDS());
++    AsyncInvocation d2 = replicate2.invokeAsync(() -> DistributedTestCase.disconnectFromDS());
 +    d1.join();
 +    assertFalse(d1.exceptionOccurred());
 +    d2.join();
 +    assertFalse(d2.exceptionOccurred());
 +    CacheSerializableRunnable establishConnectivity = new CacheSerializableRunnable("establishcConnectivity") {
 +      @Override
 +      public void run2() throws CacheException {
 +        getSystem();
 +      }
 +    };
 +    replicate1.invoke(establishConnectivity);
 +    replicate2.invoke(establishConnectivity);
 +    
 +    CacheSerializableRunnable createRegion = new CacheSerializableRunnable("create DistributedRegion") {
 +      @Override
 +      public void run2() throws CacheException {
 +        // Assert some level of connectivity
 +        InternalDistributedSystem ds = getSystem();
 +        assertTrue(ds.getDistributionManager().getNormalDistributionManagerIds().size() >= 1);
 +        
 +        final long fakeHeapUsage = Math.round(fakeHeapMaxSize * (criticalHeapThresh - 0.5f));  // below critical by 50%
 +        InternalResourceManager irm = (InternalResourceManager)getCache().getResourceManager();
 +        HeapMemoryMonitor hmm = irm.getHeapMonitor();
 +        assertTrue(fakeHeapMaxSize > 0);
 +        hmm.setTestMaxMemoryBytes(fakeHeapMaxSize);
 +        HeapMemoryMonitor.setTestBytesUsedForThresholdSet(fakeHeapUsage);
 +        irm.setCriticalHeapPercentage((criticalHeapThresh * 100.0f));
 +        AttributesFactory<Integer, String> af = new AttributesFactory<Integer, String>();
 +        af.setScope(Scope.DISTRIBUTED_ACK);
 +        af.setDataPolicy(DataPolicy.REPLICATE);
 +        getCache().createRegion(rName, af.create());
 +      }
 +    };
 +    replicate1.invoke(createRegion);
 +    replicate2.invoke(createRegion);
 +    
 +    replicate1.invoke(addExpectedException);
 +    replicate2.invoke(addExpectedException);
 +    
 +    final Integer expected = (Integer)replicate1.invoke(new SerializableCallable("test Local DistributedRegion Load") {
 +      public Object call() throws Exception {
 +        Region<Integer, String> r = getCache().getRegion(rName);
 +        AttributesMutator<Integer, String> am = r.getAttributesMutator();
 +        am.setCacheLoader(new CacheLoader<Integer, String>() {
 +          final AtomicInteger numLoaderInvocations = new AtomicInteger();
 +          public String load(LoaderHelper<Integer, String> helper) throws CacheLoaderException {
 +            Integer expectedInvocations = (Integer)helper.getArgument();
 +            final int actualInvocations = this.numLoaderInvocations.getAndIncrement();
 +            if (expectedInvocations.intValue() != actualInvocations) {
 +              throw new CacheLoaderException("Expected " + expectedInvocations 
 +                  + " invocations, actual is " + actualInvocations);
 +            }
 +            return helper.getKey().toString();
 +          }
 +          public void close() {}
 +        });
 +
 +        int expectedInvocations = 0;
 +        HeapMemoryMonitor hmm = ((InternalResourceManager)getCache().getResourceManager()).getHeapMonitor();
 +        assertFalse(hmm.getState().isCritical());
 +        {
 +          Integer k = new Integer(1);
 +          assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
 +        }
 +
 +        long newfakeHeapUsage = Math.round(fakeHeapMaxSize * (criticalHeapThresh + 0.1f));  // usage above critical by 10%
 +        assertTrue(newfakeHeapUsage > 0);
 +        assertTrue(newfakeHeapUsage <= fakeHeapMaxSize);
 +        hmm.updateStateAndSendEvent(newfakeHeapUsage);
 +        assertTrue(hmm.getState().isCritical());
 +        {
 +          Integer k = new Integer(2); 
 +          assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
 +        }
 +
 +        newfakeHeapUsage = Math.round(fakeHeapMaxSize * (criticalHeapThresh - 0.3f));  // below critical by 30%
 +        assertTrue(fakeHeapMaxSize > 0);
 +        getCache().getLoggerI18n().fine(addExpectedBelow);
 +        hmm.updateStateAndSendEvent(newfakeHeapUsage);
 +        getCache().getLoggerI18n().fine(removeExpectedBelow);
 +        assertFalse(hmm.getState().isCritical());
 +        {
 +          Integer k = new Integer(3);
 +          assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
 +        }
 +        return new Integer(expectedInvocations);
 +      }
 +    });
 +
 +    final CacheSerializableRunnable validateData1 = new CacheSerializableRunnable("Validate data 1") {
 +      @Override
 +      public void run2() throws CacheException {
 +        Region<Integer, String> r = getCache().getRegion(rName);
 +        Integer i1 = new Integer(1);
 +        assertTrue(r.containsKey(i1));
 +        assertNotNull(r.getEntry(i1));
 +        Integer i2 = new Integer(2);
 +        assertFalse(r.containsKey(i2));
 +        assertNull(r.getEntry(i2));
 +        Integer i3 = new Integer(3);
 +        assertTrue(r.containsKey(i3));
 +        assertNotNull(r.getEntry(i3));
 +      }
 +    };
 +    replicate1.invoke(validateData1);
 +    replicate2.invoke(validateData1);
 +    
 +    replicate2.invoke(new SerializableCallable("test DistributedRegion netLoad") {
 +      public Object call() throws Exception {
 +        Region<Integer, String> r = getCache().getRegion(rName);
 +        HeapMemoryMonitor hmm = ((InternalResourceManager)getCache().getResourceManager()).getHeapMonitor();
 +        assertFalse(hmm.getState().isCritical());
 +        
 +        int expectedInvocations = expected.intValue();
 +        {
 +          Integer k = new Integer(4);
 +          assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
 +        }
 +        
 +        // Place in a critical state for the next test
 +        long newfakeHeapUsage = Math.round(fakeHeapMaxSize * (criticalHeapThresh + 0.1f));  // usage above critical by 10%
 +        assertTrue(newfakeHeapUsage > 0);
 +        assertTrue(newfakeHeapUsage <= fakeHeapMaxSize);
 +        hmm.updateStateAndSendEvent(newfakeHeapUsage);
 +        assertTrue(hmm.getState().isCritical());
 +        {
 +          Integer k = new Integer(5);
 +          assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
 +        }
 +
 +        newfakeHeapUsage = Math.round(fakeHeapMaxSize * (criticalHeapThresh - 0.3f));  // below critical by 30%
 +        assertTrue(fakeHeapMaxSize > 0);
 +        getCache().getLoggerI18n().fine(addExpectedBelow);
 +        hmm.updateStateAndSendEvent(newfakeHeapUsage);
 +        getCache().getLoggerI18n().fine(removeExpectedBelow);
 +        assertFalse(hmm.getState().isCritical());
 +        {
 +          Integer k = new Integer(6);
 +          assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
 +        }
 +        return new Integer(expectedInvocations);
 +      }
 +    });
 +    
 +    replicate1.invoke(removeExpectedException);
 +    replicate2.invoke(removeExpectedException);
 +    
 +    final CacheSerializableRunnable validateData2 = new CacheSerializableRunnable("Validate data 2") {
 +      @Override
 +      public void run2() throws CacheException {
 +        Region<Integer, String> r = getCache().getRegion(rName);
 +        Integer i4 = new Integer(4);
 +        assertTrue(r.containsKey(i4));
 +        assertNotNull(r.getEntry(i4));
 +        Integer i5 = new Integer(5);
 +        assertFalse(r.containsKey(i5));
 +        assertNull(r.getEntry(i5));
 +        Integer i6 = new Integer(6);
 +        assertTrue(r.containsKey(i6));
 +        assertNotNull(r.getEntry(i6));
 +      }
 +    };
 +    replicate1.invoke(validateData2);
 +    replicate2.invoke(validateData2);
 +  }
 +  
 +  /**
 +   * Test that a Partitioned Region loader invocation is rejected
 +   * if the VM with the bucket is in a critical state.
 +   * @throws Exception
 +   */
 +  public void testPRLoadRejection() throws Exception {
 +    final Host host = Host.getHost(0);
 +    final VM accessor = host.getVM(1);
 +    final VM ds1 = host.getVM(2);
 +    final String rName = getUniqueName();
 +    final float criticalHeapThresh = 0.90f;
 +    final int fakeHeapMaxSize = 1000;
 +
 +    // Make sure the desired VMs will have a fresh DS.
-     AsyncInvocation d0 = accessor.invokeAsync(DistributedTestCase.class, "disconnectFromDS");
-     AsyncInvocation d1 = ds1.invokeAsync(DistributedTestCase.class, "disconnectFromDS");
++    AsyncInvocation d0 = accessor.invokeAsync(() -> DistributedTestCase.disconnectFromDS());
++    AsyncInvocation d1 = ds1.invokeAsync(() -> DistributedTestCase.disconnectFromDS());
 +    d0.join();
 +    assertFalse(d0.exceptionOccurred());
 +    d1.join();
 +    assertFalse(d1.exceptionOccurred());
 +    CacheSerializableRunnable establishConnectivity = new CacheSerializableRunnable("establishcConnectivity") {
 +      @Override
 +      public void run2() throws CacheException { getSystem();  }
 +    };
 +    ds1.invoke(establishConnectivity);
 +    accessor.invoke(establishConnectivity);
 +
 +    ds1.invoke(createPR(rName, false, fakeHeapMaxSize, criticalHeapThresh));
 +    accessor.invoke(createPR(rName, true, fakeHeapMaxSize, criticalHeapThresh));
 +    
 +    final AtomicInteger expectedInvocations = new AtomicInteger(0);
 +
 +    Integer ex = (Integer) accessor.invoke(new SerializableCallable("Invoke loader from accessor, non-critical") {
 +      public Object call() throws Exception {
 +        Region<Integer, String> r = getCache().getRegion(rName);
 +        Integer k = new Integer(1);
 +        Integer expectedInvocations0 = new Integer(expectedInvocations.getAndIncrement());
 +        assertEquals(k.toString(), r.get(k, expectedInvocations0)); // should load for new key
 +        assertTrue(r.containsKey(k));
 +        Integer expectedInvocations1 = new Integer(expectedInvocations.get());
 +        assertEquals(k.toString(), r.get(k, expectedInvocations1)); // no load
 +        assertEquals(k.toString(), r.get(k, expectedInvocations1)); // no load
 +        return expectedInvocations1;
 +      }
 +    });
 +    expectedInvocations.set(ex.intValue());
 +
 +    ex = (Integer)ds1.invoke(new SerializableCallable("Invoke loader from datastore, non-critical") {
 +      public Object call() throws Exception {
 +        Region<Integer, String> r = getCache().getRegion(rName);
 +        Integer k = new Integer(2);
 +        Integer expectedInvocations1 = new Integer(expectedInvocations.getAndIncrement());
 +        assertEquals(k.toString(), r.get(k, expectedInvocations1)); // should load for new key
 +        assertTrue(r.containsKey(k));
 +        Integer expectedInvocations2 = new Integer(expectedInvocations.get());
 +        assertEquals(k.toString(), r.get(k, expectedInvocations2)); // no load
 +        assertEquals(k.toString(), r.get(k, expectedInvocations2)); // no load
 +        String oldVal = r.remove(k);
 +        assertFalse(r.containsKey(k));
 +        assertEquals(k.toString(), oldVal);
 +        return expectedInvocations2;
 +      }
 +    });
 +    expectedInvocations.set(ex.intValue());
 +
 +    accessor.invoke(addExpectedException);
 +    ds1.invoke(addExpectedException);
 +
 +    ex = (Integer)ds1.invoke(new SerializableCallable("Set critical state, assert local load behavior") {
 +      public Object call() throws Exception {
 +        long newfakeHeapUsage = Math.round(fakeHeapMaxSize * (criticalHeapThresh + 0.1f));  // usage above critical by 10%
 +        assertTrue(newfakeHeapUsage > 0);
 +        assertTrue(newfakeHeapUsage <= fakeHeapMaxSize);
 +        HeapMemoryMonitor hmm = ((InternalResourceManager)getCache().getResourceManager()).getHeapMonitor();
 +        hmm.updateStateAndSendEvent(newfakeHeapUsage);
 +        assertTrue(hmm.getState().isCritical());
 +        final Integer k = new Integer(2); // reload with same key again and again
 +        final Integer expectedInvocations3 = new 

<TRUNCATED>


[04/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/RegionReliabilityTestCase.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/RegionReliabilityTestCase.java
index 2ae8293,0000000..8cc65d3
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/RegionReliabilityTestCase.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/RegionReliabilityTestCase.java
@@@ -1,1450 -1,0 +1,1463 @@@
 +/*
 + * 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.cache30;
 +
 +import java.io.ByteArrayInputStream;
 +import java.io.ByteArrayOutputStream;
 +import java.util.HashMap;
 +import java.util.HashSet;
 +import java.util.Map;
 +import java.util.Properties;
 +import java.util.Set;
 +import java.util.concurrent.locks.Lock;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.AttributesMutator;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheLoader;
 +import com.gemstone.gemfire.cache.CacheLoaderException;
 +import com.gemstone.gemfire.cache.CacheTransactionManager;
 +import com.gemstone.gemfire.cache.CommitDistributionException;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.ExpirationAction;
 +import com.gemstone.gemfire.cache.ExpirationAttributes;
 +import com.gemstone.gemfire.cache.LoaderHelper;
 +import com.gemstone.gemfire.cache.LossAction;
 +import com.gemstone.gemfire.cache.MembershipAttributes;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionAccessException;
 +import com.gemstone.gemfire.cache.RegionAttributes;
 +import com.gemstone.gemfire.cache.RegionDistributionException;
 +import com.gemstone.gemfire.cache.RegionEvent;
 +import com.gemstone.gemfire.cache.RegionMembershipListener;
 +import com.gemstone.gemfire.cache.RegionReinitializedException;
 +import com.gemstone.gemfire.cache.RequiredRoles;
 +import com.gemstone.gemfire.cache.ResumptionAction;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.cache.query.Query;
 +import com.gemstone.gemfire.cache.query.QueryService;
 +import com.gemstone.gemfire.cache.util.RegionMembershipListenerAdapter;
 +import com.gemstone.gemfire.distributed.Role;
 +import com.gemstone.gemfire.distributed.internal.DM;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
 +import com.gemstone.gemfire.distributed.internal.membership.InternalRole;
 +import com.gemstone.gemfire.internal.cache.AbstractRegion;
 +import com.gemstone.gemfire.internal.cache.DistributedCacheOperation;
 +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
 +import com.gemstone.gemfire.internal.cache.LocalRegion;
 +import com.gemstone.gemfire.internal.cache.TXManagerImpl;
 +import com.gemstone.gemfire.internal.cache.TXState;
 +import com.gemstone.gemfire.internal.cache.TXStateInterface;
 +import com.gemstone.gemfire.internal.cache.TXStateProxyImpl;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.SerializableRunnable;
++import com.gemstone.gemfire.test.dunit.SerializableRunnableIF;
 +import com.gemstone.gemfire.test.dunit.ThreadUtils;
 +import com.gemstone.gemfire.test.dunit.Wait;
 +import com.gemstone.gemfire.test.dunit.WaitCriterion;
 +
 +/**
 + * Tests region reliability defined by MembershipAttributes.
 + *
 + * @author Kirk Lund
 + * @since 5.0
 + */
 +public abstract class RegionReliabilityTestCase extends ReliabilityTestCase {
 +
 +  public RegionReliabilityTestCase(String name) {
 +    super(name);
 +  }
 +
 +  @Override
 +  protected final void preTearDownCacheTestCase() throws Exception {
 +    DistributedCacheOperation.setBeforePutOutgoing(null);
 +  }
 +
 +  // -------------------------------------------------------------------------
 +  // Configuration and setup methods
 +  // -------------------------------------------------------------------------
 +  
 +  /** Returns scope to execute tests under. */
 +  protected abstract Scope getRegionScope();
 +  
 +  protected InternalDistributedSystem createConnection(String[] roles) {
 +    StringBuffer rolesValue = new StringBuffer();
 +    if (roles != null) {
 +      for (int i = 0; i < roles.length; i++) {
 +        if (i > 0) {
 +          rolesValue.append(",");
 +        }
 +        rolesValue.append(roles[i]);
 +      }
 +    }
 +    Properties config = new Properties();
 +    config.setProperty(DistributionConfig.ROLES_NAME, rolesValue.toString());
 +    return getSystem(config);
 +  }
 +
 +  protected void assertLimitedAccessThrows(Region region) throws Exception {
 +    try {
 +      region.clear();
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      region.create("KEY", "VAL");
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      region.destroy(new Object());
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      region.destroyRegion();
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    if (region.getAttributes().getScope().isGlobal()) {
 +      try {
 +        region.becomeLockGrantor();
 +        fail("Should have thrown an RegionAccessException");
 +      } catch (RegionAccessException ex) {
 +        // pass...
 +      }
 +      try {
 +        region.getDistributedLock(new Object());
 +        fail("Should have thrown an RegionAccessException");
 +      } catch (RegionAccessException ex) {
 +        // pass...
 +      }
 +      try {
 +        region.getRegionDistributedLock();
 +        fail("Should have thrown an RegionAccessException");
 +      } catch (RegionAccessException ex) {
 +        // pass...
 +      }
 +    }
 +    try {
 +      region.invalidate(new Object());
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      region.invalidateRegion();
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      region.loadSnapshot(new ByteArrayInputStream(new byte[] {}));
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try { // netload TODO: configure CacheLoader in region
 +      region.get("netload");
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try { // netsearch TODO: add 2nd VM that has the object
 +      region.get("netsearch");
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      region.put(new Object(), new Object());
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      Map map = new HashMap();
 +      map.put(new Object(), new Object());
 +      region.putAll(map);
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      Map map = new HashMap();
 +      map.put(new Object(), new Object());
 +      region.putAll(map, "callbackArg");
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      region.remove(new Object());
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    if (!region.getAttributes().getScope().isGlobal()) {
 +      CacheTransactionManager tx = region.getCache().getCacheTransactionManager();
 +      tx.begin();
 +      try {
 +        region.put("KEY-tx", "VAL-tx");
 +        fail("Should have thrown an RegionAccessException");
 +      } catch (RegionAccessException ex) {
 +        // pass...
 +      }
 +      tx.rollback();
 +    }
 +  }
 +  
 +  protected void assertNoAccessThrows(Region region) throws Exception {
 +    assertLimitedAccessThrows(region);
 +    try {
 +      region.containsKey(new Object());
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      region.containsValue(new Object());
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      region.containsValueForKey(new Object());
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      region.entries(false);
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      region.entrySet();
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      region.get(new Object());
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      region.getEntry(new Object());
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      region.isEmpty();
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      region.keys();
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      region.keySet();
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      region.localDestroy(new Object());
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      region.localInvalidate(new Object());
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      region.localInvalidateRegion();
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      region.saveSnapshot(new ByteArrayOutputStream());
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      region.size();
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    try {
 +      region.values();
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +    
 +    try {
 +      QueryService qs = region.getCache().getQueryService();
 +      Query query = qs.newQuery(
 +        "(select distinct * from " + region.getFullPath() + ").size");
 +      query.execute();
 +      fail("Should have thrown an RegionAccessException");
 +    } catch (RegionAccessException ex) {
 +      // pass...
 +    }
 +  }
 +  
 +  protected void assertLimitedAccessDoesNotThrow(Region region) throws Exception {
 +    // insert some values for test
 +    Object[] keys = new Object[] {"hip", "hop"};
 +    Object[] values = new Object[] {"clip", "clop"};
 +    for (int i = 0; i < keys.length; i++) {
 +      region.put(keys[i], values[i]);
 +    }
 +
 +    // test the ops that can throw RegionAccessException for LIMITED_ACCESS
 +    region.create("jack", "jill");
 +    region.destroy("jack");
 +    
 +    if (region.getAttributes().getScope().isGlobal()) {
 +      region.becomeLockGrantor();
 +
 +      Lock dlock = region.getDistributedLock(keys[0]);
 +      dlock.lock();
 +      dlock.unlock();
 +      
 +      Lock rlock = region.getRegionDistributedLock();
 +      rlock.lock();
 +      rlock.unlock();
 +    }
 +    
 +    // netload (configured in vm0)
 +    assertEquals("netload", region.get("netload"));
 +    // netsearch (entry exists in vm0)
 +    assertEquals("netsearch", region.get("netsearch"));
 +    
 +    region.invalidate(keys[0]);
 +    region.invalidateRegion();
 +    
 +    ByteArrayOutputStream baos = new ByteArrayOutputStream();
 +    region.saveSnapshot(baos);
 +    region.loadSnapshot(new ByteArrayInputStream(baos.toByteArray()));
 +    
 +    // need to get a new handle to the region...
 +    region = getRootRegion(region.getFullPath());
 +    
 +    region.put(keys[0], values[0]);
 +
 +    Map map = new HashMap();
 +    map.put("mom", "pop");
 +    region.putAll(map);
 +    region.putAll(map, "callbackArg");
 +
 +    QueryService qs = region.getCache().getQueryService();
 +    Query query = qs.newQuery(
 +      "(select distinct * from " + region.getFullPath() + ").size");
 +    query.execute();
 +    
 +    region.remove(keys[0]);
 +
 +    if (!region.getAttributes().getScope().isGlobal()) {
 +      CacheTransactionManager tx = region.getCache().getCacheTransactionManager();
 +      tx.begin();
 +      region.put("KEY-tx", "VAL-tx");
 +      tx.commit();
 +    }
 +    
 +    region.clear();
 +    region.destroyRegion();
 +  }
 +  
 +  protected void assertNoAccessDoesNotThrow(Region region) throws Exception {
 +    // insert some values for test
 +    Object[] keys = new Object[] {"bip", "bam"};
 +    Object[] values = new Object[] {"foo", "bar"};
 +    for (int i = 0; i < keys.length; i++) {
 +      region.put(keys[i], values[i]);
 +    }
 +    
 +    // test the ops that can throw RegionAccessException for NO_ACCESS
 +    region.containsKey(new Object());
 +    region.containsValue(new Object());
 +    region.containsValueForKey(new Object());
 +    region.entries(false);
 +    region.entrySet();
 +    region.get(keys[0]);
 +    region.getEntry(keys[0]);
 +    region.isEmpty();
 +    region.keys();
 +    region.keySet();
 +    region.localDestroy(keys[0]);
 +    region.localInvalidate(keys[1]);
 +    region.localInvalidateRegion();
 +    region.saveSnapshot(new ByteArrayOutputStream());
 +    region.size();
 +    region.values();
 +
 +    QueryService qs = region.getCache().getQueryService();
 +    Query query = qs.newQuery(
 +      "(select distinct * from " + region.getFullPath() + ").size");
 +    query.execute();
 +    
 +    assertLimitedAccessDoesNotThrow(region);
 +  }
 +  
 +  protected void sleep(long millis) {
 +    try {
 +      Thread.sleep(millis);
 +    }
 +    catch (InterruptedException e) {
 +      fail("interrupted");
 +    }
 +  }
 +  
 +  // -------------------------------------------------------------------------
 +  // Tests to be run under every permutation of config options
 +  //   Valid configurations include scope D-ACK, D-NOACK, GLOBAL
 +  // -------------------------------------------------------------------------
 +  
 +  /**
 +   * Tests affect of NO_ACCESS on region operations.
 +   */
 +  public void testNoAccess() throws Exception {
 +    final String name = this.getUniqueName();
 +    
 +    final String roleA = name+"-A";
 +    
 +    // assign names to 4 vms...
 +    final String[] requiredRoles = {roleA};
 +    Set requiredRolesSet = new HashSet();
 +    for (int i = 0; i < requiredRoles.length; i++) {
 +      requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
 +    }
 +    assertEquals(requiredRoles.length, requiredRolesSet.size());
 +
 +    // connect controller to system...
 +    Properties config = new Properties();
 +    config.setProperty(DistributionConfig.ROLES_NAME, "");
 +    getSystem(config);
 +    
 +    getCache();
 +    
 +    // create region in controller...
 +    MembershipAttributes ra = new MembershipAttributes(
 +        requiredRoles, LossAction.NO_ACCESS, ResumptionAction.NONE);
 +    AttributesFactory fac = new AttributesFactory();
 +    fac.setMembershipAttributes(ra);
 +    fac.setScope(getRegionScope());
 +    fac.setStatisticsEnabled(true);
 +    RegionAttributes attr = fac.create();
 +    Region region = createRootRegion(name, attr);
 +    
 +    // wait for memberTimeout to expire
 +    waitForMemberTimeout();
 +
 +    // use vm0 for netsearch and netload
 +    Host.getHost(0).getVM(0).invoke(new CacheSerializableRunnable("Create Region") {
 +      public void run2() throws CacheException {
 +        createConnection(null);
 +        AttributesFactory fac = new AttributesFactory();
 +        fac.setScope(getRegionScope());
 +        fac.setCacheLoader(new CacheLoader() {
 +          public Object load(LoaderHelper helper) throws CacheLoaderException {
 +            if ("netload".equals(helper.getKey())) {
 +              return "netload";
 +            } else {
 +              return null;
 +            }
 +          }
 +          public void close() {}
 +        });
 +        RegionAttributes attr = fac.create();
 +        Region region = createRootRegion(name, attr);
 +        Object netsearch = "netsearch";
 +        region.put(netsearch, netsearch);
 +      }
 +    });
 +    
 +    // test ops on Region that should throw
 +    assertNoAccessThrows(region);
 +    
 +    // use vm1 to create role
 +    Host.getHost(0).getVM(1).invoke(new CacheSerializableRunnable("Create Region") {
 +      public void run2() throws CacheException {
 +        createConnection(new String[] {roleA});
 +        AttributesFactory fac = new AttributesFactory();
 +        fac.setScope(getRegionScope());
 +        RegionAttributes attr = fac.create();
 +        createRootRegion(name, attr);
 +      }
 +    });
 +    
 +    Role role = (Role) requiredRolesSet.iterator().next();
 +    assertTrue(RequiredRoles.isRoleInRegionMembership(region, role));
 +    
 +    // retest ops on Region to assert no longer throw    
 +    assertNoAccessDoesNotThrow(region);
 +  }
 +  
 +  private static InternalDistributedMember findDistributedMember() {
 +    DM dm = (
 +      InternalDistributedSystem.getAnyInstance()).getDistributionManager();
 +    return dm.getDistributionManagerId();
 +  }
 +  
 +  /**
 +   * Tests affect of NO_ACCESS on local entry expiration actions.
 +   */
 +  public void testNoAccessWithLocalEntryExpiration() throws Exception {
 +    final String name = this.getUniqueName();
 +    
 +    final String roleA = name+"-A";
 +    
 +    // assign names to 4 vms...
 +    final String[] requiredRoles = {roleA};
 +    Set requiredRolesSet = new HashSet();
 +    for (int i = 0; i < requiredRoles.length; i++) {
 +      requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
 +    }
 +    assertEquals(requiredRoles.length, requiredRolesSet.size());
 +
 +    // connect controller to system...
 +    Properties config = new Properties();
 +    config.setProperty(DistributionConfig.ROLES_NAME, "");
 +    getSystem(config);
 +    
 +    getCache();
 +    
 +    // create region in controller...
 +    MembershipAttributes ra = new MembershipAttributes(
 +        requiredRoles, LossAction.NO_ACCESS, ResumptionAction.NONE);
 +    AttributesFactory fac = new AttributesFactory();
 +    fac.setMembershipAttributes(ra);
 +    fac.setScope(getRegionScope());
 +    fac.setStatisticsEnabled(true);
 +    RegionAttributes attr = fac.create();
 +    final Region region = createExpiryRootRegion(name, attr);
 +    
 +    // wait for memberTimeout to expire
 +    waitForMemberTimeout();
 +
 +    // use vm1 to create role
 +    Host.getHost(0).getVM(1).invoke(new CacheSerializableRunnable("Create Region") {
 +      public void run2() throws CacheException {
 +        createConnection(new String[] {roleA});
 +        AttributesFactory fac = new AttributesFactory();
 +        fac.setScope(getRegionScope());
 +        RegionAttributes attr = fac.create();
 +        createRootRegion(name, attr);
 +      }
 +    });
 +    
 +    // test to make sure expiration is not suspended
 +    region.put("expireMe", "expireMe");
 +    assertTrue(region.size() == 1);
 +    Host.getHost(0).getVM(1).invoke(new CacheSerializableRunnable("Close Region") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion(name);
 +        region.close();
 +      }
 +    });
 +    //TODO: waitForMemberTimeout(); ?
 +
 +    // set expiration and sleep
 +    AttributesMutator mutator = region.getAttributesMutator();
 +    mutator.setEntryTimeToLive(
 +      new ExpirationAttributes(1, ExpirationAction.LOCAL_DESTROY));
 +    sleep(200);
 +
 +    // make sure no values were expired
 +    Set entries = ((LocalRegion) region).basicEntries(false);
 +    assertTrue(entries.size() == 1);
 +    
 +    // create region again in vm1
 +    Host.getHost(0).getVM(1).invoke(new CacheSerializableRunnable("Create Region") {
 +      public void run2() throws CacheException {
 +        AttributesFactory fac = new AttributesFactory();
 +        fac.setScope(getRegionScope());
 +        RegionAttributes attr = fac.create();
 +        createRootRegion(name, attr);
 +      }
 +    });
 +    
 +    waitForEntryDestroy(region, "expireMe");
 +  }
 +  
 +  /**
 +   * Tests affect of NO_ACCESS on local region expiration actions.
 +   */
 +  public void testNoAccessWithLocalRegionExpiration() throws Exception {
 +    final String name = this.getUniqueName();
 +    
 +    final String roleA = name+"-A";
 +    
 +    // assign names to 4 vms...
 +    final String[] requiredRoles = {roleA};
 +    Set requiredRolesSet = new HashSet();
 +    for (int i = 0; i < requiredRoles.length; i++) {
 +      requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
 +    }
 +    assertEquals(requiredRoles.length, requiredRolesSet.size());
 +
 +    // connect controller to system...
 +    Properties config = new Properties();
 +    config.setProperty(DistributionConfig.ROLES_NAME, "");
 +    getSystem(config);
 +    
 +    getCache();
 +    
 +    // create region in controller...
 +    MembershipAttributes ra = new MembershipAttributes(
 +        requiredRoles, LossAction.NO_ACCESS, ResumptionAction.NONE);
 +    AttributesFactory fac = new AttributesFactory();
 +    fac.setMembershipAttributes(ra);
 +    fac.setScope(getRegionScope());
 +    fac.setStatisticsEnabled(true);
 +    RegionAttributes attr = fac.create();
 +    final Region region = createExpiryRootRegion(name, attr);
 +    
 +    // wait for memberTimeout to expire
 +    waitForMemberTimeout();
 +
 +    AttributesMutator mutator = region.getAttributesMutator();
 +    mutator.setRegionTimeToLive(
 +      new ExpirationAttributes(1, ExpirationAction.LOCAL_DESTROY));
 +      
 +    // sleep and make sure region does not expire
 +    sleep(200);
 +    assertFalse(region.isDestroyed());
 +    
 +    // create region in vm1
 +    Host.getHost(0).getVM(1).invoke(new CacheSerializableRunnable("Create Region") {
 +      public void run2() throws CacheException {
 +        createConnection(new String[] {roleA});
 +        AttributesFactory fac = new AttributesFactory();
 +        fac.setScope(getRegionScope());
 +        RegionAttributes attr = fac.create();
 +        createRootRegion(name, attr);
 +      }
 +    });
 +    
 +    waitForRegionDestroy(region);
 +  }
 +  
 +  /**
 +   * Tests affect of LIMITED_ACCESS on region operations.
 +   */
 +  public void testLimitedAccess() throws Exception {
 +    final String name = this.getUniqueName();
 +    
 +    final String roleA = name+"-A";
 +    
 +    // assign names to 4 vms...
 +    final String[] requiredRoles = {roleA};
 +    Set requiredRolesSet = new HashSet();
 +    for (int i = 0; i < requiredRoles.length; i++) {
 +      requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
 +    }
 +    assertEquals(requiredRoles.length, requiredRolesSet.size());
 +
 +    // connect controller to system...
 +    Properties config = new Properties();
 +    config.setProperty(DistributionConfig.ROLES_NAME, "");
 +    getSystem(config);
 +    
 +    getCache();
 +    
 +    // create region in controller...
 +    MembershipAttributes ra = new MembershipAttributes(
 +        requiredRoles, LossAction.LIMITED_ACCESS, ResumptionAction.NONE);
 +    AttributesFactory fac = new AttributesFactory();
 +    fac.setMembershipAttributes(ra);
 +    fac.setScope(getRegionScope());
 +    fac.setStatisticsEnabled(true);
 +    RegionAttributes attr = fac.create();
 +    Region region = createRootRegion(name, attr);
 +    
 +    // wait for memberTimeout to expire
 +    waitForMemberTimeout();
 +
 +    // use vm0 for netsearch and netload
 +    Host.getHost(0).getVM(0).invoke(new CacheSerializableRunnable("Create Region") {
 +      public void run2() throws CacheException {
 +        createConnection(null);
 +        AttributesFactory fac = new AttributesFactory();
 +        fac.setScope(getRegionScope());
 +        fac.setCacheLoader(new CacheLoader() {
 +          public Object load(LoaderHelper helper) throws CacheLoaderException {
 +            if ("netload".equals(helper.getKey())) {
 +              return "netload";
 +            } else {
 +              return null;
 +            }
 +          }
 +          public void close() {}
 +        });
 +        RegionAttributes attr = fac.create();
 +        Region region = createRootRegion(name, attr);
 +        Object netsearch = "netsearch";
 +        region.put(netsearch, netsearch);
 +      }
 +    });
 +    
 +    // test ops on Region that should throw
 +    assertLimitedAccessThrows(region);
 +    
 +    // this query should not throw
 +    QueryService qs = region.getCache().getQueryService();
 +    Query query = qs.newQuery(
 +      "(select distinct * from " + region.getFullPath() + ").size");
 +    query.execute();
 +    
 +    // use vm1 to create role
 +    Host.getHost(0).getVM(1).invoke(new CacheSerializableRunnable("Create Region") {
 +      public void run2() throws CacheException {
 +        createConnection(new String[] {roleA});
 +        AttributesFactory fac = new AttributesFactory();
 +        fac.setScope(getRegionScope());
 +        RegionAttributes attr = fac.create();
 +        createRootRegion(name, attr);
 +      }
 +    });
 +    
 +    // retest ops on Region to assert no longer throw    
 +    assertLimitedAccessDoesNotThrow(region);
 +  }
 +  
 +  /**
 +   * Tests affect of LIMITED_ACCESS on local entry expiration actions.
 +   */
 +  public void testLimitedAccessWithLocalEntryExpiration() throws Exception {
 +    final String name = this.getUniqueName();
 +    
 +    final String roleA = name+"-A";
 +    
 +    // assign names to 4 vms...
 +    final String[] requiredRoles = {roleA};
 +    Set requiredRolesSet = new HashSet();
 +    for (int i = 0; i < requiredRoles.length; i++) {
 +      requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
 +    }
 +    assertEquals(requiredRoles.length, requiredRolesSet.size());
 +
 +    // connect controller to system...
 +    Properties config = new Properties();
 +    config.setProperty(DistributionConfig.ROLES_NAME, "");
 +    getSystem(config);
 +    
 +    getCache();
 +    
 +    // create region in controller...
 +    MembershipAttributes ra = new MembershipAttributes(
 +        requiredRoles, LossAction.LIMITED_ACCESS, ResumptionAction.NONE);
 +    AttributesFactory fac = new AttributesFactory();
 +    fac.setMembershipAttributes(ra);
 +    fac.setScope(getRegionScope());
 +    fac.setStatisticsEnabled(true);
 +    RegionAttributes attr = fac.create();
 +    final Region region = createExpiryRootRegion(name, attr);
 +     
 +    // wait for memberTimeout to expire
 +    waitForMemberTimeout();
 +
 +    // use vm1 to create role
 +    Host.getHost(0).getVM(1).invoke(new CacheSerializableRunnable("Create Region") {
 +      public void run2() throws CacheException {
 +        createConnection(new String[] {roleA});
 +        AttributesFactory fac = new AttributesFactory();
 +        fac.setScope(getRegionScope());
 +        RegionAttributes attr = fac.create();
 +        createRootRegion(name, attr);
 +      }
 +    });
 +    
 +    // test to make sure expiration is suspended
 +    region.put("expireMe", "expireMe");
 +    assertTrue(region.size() == 1);
 +    Host.getHost(0).getVM(1).invoke(new CacheSerializableRunnable("Close Region") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion(name);
 +        region.close();
 +      }
 +    });
 +    //TODO: waitForMemberTimeout(); ?
 +
 +    // set expiration and sleep
 +    AttributesMutator mutator = region.getAttributesMutator();
 +    mutator.setEntryTimeToLive(
 +      new ExpirationAttributes(1, ExpirationAction.LOCAL_DESTROY));
 +    WaitCriterion wc1 = new WaitCriterion() {
 +      public boolean done() {
 +        return ((LocalRegion) region).basicEntries(false).size() == 0;
 +      }
 +      public String description() {
 +        return "expected zero entries but have " + ((LocalRegion) region).basicEntries(false).size();
 +      }
 +    };
 +    Wait.waitForCriterion(wc1, 30*1000, 10, true);
 +
 +    // create region again
 +    Host.getHost(0).getVM(1).invoke(new CacheSerializableRunnable("Create Region") {
 +      public void run2() throws CacheException {
 +        AttributesFactory fac = new AttributesFactory();
 +        fac.setScope(getRegionScope());
 +        RegionAttributes attr = fac.create();
 +        createRootRegion(name, attr);
 +      }
 +    });
 +    
 +    region.put("expireMe", "expireMe");
 +    
 +    waitForEntryDestroy(region, "expireMe");
 +    assertTrue(region.size() == 0);
 +  }
 +  
 +  /**
 +   * Tests affect of LIMITED_ACCESS on local region expiration actions.
 +   */
 +  public void testLimitedAccessWithLocalRegionExpiration() throws Exception {
 +    final String name = this.getUniqueName();
 +    
 +    final String roleA = name+"-A";
 +    
 +    // assign names to 4 vms...
 +    final String[] requiredRoles = {roleA};
 +    Set requiredRolesSet = new HashSet();
 +    for (int i = 0; i < requiredRoles.length; i++) {
 +      requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
 +    }
 +    assertEquals(requiredRoles.length, requiredRolesSet.size());
 +
 +    // connect controller to system...
 +    Properties config = new Properties();
 +    config.setProperty(DistributionConfig.ROLES_NAME, "");
 +    getSystem(config);
 +    
 +    getCache();
 +    
 +    // create region in controller...
 +    MembershipAttributes ra = new MembershipAttributes(
 +        requiredRoles, LossAction.LIMITED_ACCESS, ResumptionAction.NONE);
 +    AttributesFactory fac = new AttributesFactory();
 +    fac.setMembershipAttributes(ra);
 +    fac.setScope(getRegionScope());
 +    fac.setStatisticsEnabled(true);
 +    RegionAttributes attr = fac.create();
 +    final Region region = createExpiryRootRegion(name, attr);
 +    
 +    // wait for memberTimeout to expire
 +    waitForMemberTimeout();
 +
 +    AttributesMutator mutator = region.getAttributesMutator();
 +    mutator.setRegionTimeToLive(
 +      new ExpirationAttributes(1, ExpirationAction.LOCAL_DESTROY));
 +      
 +    waitForRegionDestroy(region);
 +  }
 +  
 +  /**
 +   * Tests affect of FULL_ACCESS on region operations.
 +   */
 +  public void testFullAccess() throws Exception {
 +    final String name = this.getUniqueName();
 +    
 +    final String roleA = name+"-A";
 +    
 +    // assign names to 4 vms...
 +    final String[] requiredRoles = {roleA};
 +    Set requiredRolesSet = new HashSet();
 +    for (int i = 0; i < requiredRoles.length; i++) {
 +      requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
 +    }
 +    assertEquals(requiredRoles.length, requiredRolesSet.size());
 +
 +    // connect controller to system...
 +    Properties config = new Properties();
 +    config.setProperty(DistributionConfig.ROLES_NAME, "");
 +    getSystem(config);
 +    
 +    getCache();
 +    
 +    // create region in controller...
 +    MembershipAttributes ra = new MembershipAttributes(
 +        requiredRoles, LossAction.FULL_ACCESS, ResumptionAction.NONE);
 +    AttributesFactory fac = new AttributesFactory();
 +    fac.setMembershipAttributes(ra);
 +    fac.setScope(getRegionScope());
 +    fac.setStatisticsEnabled(true);
 +    RegionAttributes attr = fac.create();
 +    Region region = createRootRegion(name, attr);
 +    
 +    // wait for memberTimeout to expire
 +    waitForMemberTimeout();
 +
 +    // use vm0 for netsearch and netload
 +    Host.getHost(0).getVM(0).invoke(new CacheSerializableRunnable("Create Region") {
 +      public void run2() throws CacheException {
 +        createConnection(null);
 +        AttributesFactory fac = new AttributesFactory();
 +        fac.setScope(getRegionScope());
 +        fac.setCacheLoader(new CacheLoader() {
 +          public Object load(LoaderHelper helper) throws CacheLoaderException {
 +            if ("netload".equals(helper.getKey())) {
 +              return "netload";
 +            } else {
 +              return null;
 +            }
 +          }
 +          public void close() {}
 +        });
 +        RegionAttributes attr = fac.create();
 +        Region region = createRootRegion(name, attr);
 +        Object netsearch = "netsearch";
 +        region.put(netsearch, netsearch);
 +      }
 +    });
 +    
 +    // test ops on Region that should not throw
 +    assertNoAccessDoesNotThrow(region);
 +  }
 +  
 +  /**
 +   * Tests affect of FULL_ACCESS on local entry expiration actions.
 +   */
 +  public void testFullAccessWithLocalEntryExpiration() throws Exception {
 +    final String name = this.getUniqueName();
 +    
 +    final String roleA = name+"-A";
 +    
 +    // assign names to 4 vms...
 +    final String[] requiredRoles = {roleA};
 +    Set requiredRolesSet = new HashSet();
 +    for (int i = 0; i < requiredRoles.length; i++) {
 +      requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
 +    }
 +    assertEquals(requiredRoles.length, requiredRolesSet.size());
 +
 +    // connect controller to system...
 +    Properties config = new Properties();
 +    config.setProperty(DistributionConfig.ROLES_NAME, "");
 +    getSystem(config);
 +    
 +    getCache();
 +    
 +    // create region in controller...
 +    MembershipAttributes ra = new MembershipAttributes(
 +        requiredRoles, LossAction.FULL_ACCESS, ResumptionAction.NONE);
 +    AttributesFactory fac = new AttributesFactory();
 +    fac.setMembershipAttributes(ra);
 +    fac.setScope(getRegionScope());
 +    fac.setStatisticsEnabled(true);
 +    RegionAttributes attr = fac.create();
 +    final Region region = createExpiryRootRegion(name, attr);
 +    
 +    // wait for memberTimeout to expire
 +    waitForMemberTimeout();
 +
 +    // test to make sure expiration is not suspended
 +    region.put("expireMe", "expireMe");
 +    assertTrue(region.size() == 1);
 +
 +    // set expiration and sleep
 +    AttributesMutator mutator = region.getAttributesMutator();
 +    mutator.setEntryTimeToLive(
 +      new ExpirationAttributes(1, ExpirationAction.LOCAL_DESTROY));
 +
 +    waitForEntryDestroy(region, "expireMe");
 +    assertTrue(region.size() == 0);
 +  }
 +  
 +  public static void waitForRegionDestroy(final Region region) {
 +    WaitCriterion wc = new WaitCriterion() {
 +      public boolean done() {
 +        return region.isDestroyed();
 +      }
 +      public String description() {
 +        return "expected region " + region + " to be destroyed";
 +      }
 +    };
 +    Wait.waitForCriterion(wc, 30*1000, 10, true);
 +  }
 +  
 +  public static void waitForEntryDestroy(final Region region, final Object key) {
 +    WaitCriterion wc = new WaitCriterion() {
 +      public boolean done() {
 +        return region.get(key) == null;
 +      }
 +      public String description() {
 +        return "expected entry " + key + " to not exist but it has the value " + region.get(key);
 +      }
 +    };
 +    Wait.waitForCriterion(wc, 30*1000, 10, true);
 +  }
 +  
 +  /**
 +   * Tests affect of FULL_ACCESS on local region expiration actions.
 +   */
 +  public void testFullAccessWithLocalRegionExpiration() throws Exception {
 +    final String name = this.getUniqueName();
 +    
 +    final String roleA = name+"-A";
 +    
 +    final String[] requiredRoles = {roleA};
 +    Set requiredRolesSet = new HashSet();
 +    for (int i = 0; i < requiredRoles.length; i++) {
 +      requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
 +    }
 +    assertEquals(requiredRoles.length, requiredRolesSet.size());
 +
 +    // connect controller to system...
 +    Properties config = new Properties();
 +    config.setProperty(DistributionConfig.ROLES_NAME, "");
 +    getSystem(config);
 +    
 +    getCache();
 +    
 +    // create region in controller...
 +    MembershipAttributes ra = new MembershipAttributes(
 +        requiredRoles, LossAction.FULL_ACCESS, ResumptionAction.NONE);
 +    AttributesFactory fac = new AttributesFactory();
 +    fac.setMembershipAttributes(ra);
 +    fac.setScope(getRegionScope());
 +    fac.setStatisticsEnabled(true);
 +    RegionAttributes attr = fac.create();
 +    final Region region = createExpiryRootRegion(name, attr);
 +    
 +    // wait for memberTimeout to expire
 +    waitForMemberTimeout();
 +
 +    AttributesMutator mutator = region.getAttributesMutator();
 +    mutator.setRegionTimeToLive(
 +      new ExpirationAttributes(1, ExpirationAction.LOCAL_DESTROY));
 +      
 +    waitForRegionDestroy(region);
 +  }
 +  
 +  protected static Boolean[] detectedDeparture_testCommitDistributionException = 
 +    { Boolean.FALSE };
 +  public void testCommitDistributionException() throws Exception {
 +    if (getRegionScope().isGlobal()) return; // skip test under Global
 +    if (getRegionScope().isDistributedNoAck()) return; // skip test under DistributedNoAck
 +    
 +    final String name = this.getUniqueName();
 +    final String roleA = name+"-A";
 +    final String[] requiredRoles = {roleA};
 +    Set requiredRolesSet = new HashSet();
 +    for (int i = 0; i < requiredRoles.length; i++) {
 +      requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
 +    }
 +    assertEquals(requiredRoles.length, requiredRolesSet.size());
 +
 +    // connect controller to system...
 +    Properties config = new Properties();
 +    config.setProperty(DistributionConfig.ROLES_NAME, "");
 +    getSystem(config);
 +    
 +    GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
 +    
 +    RegionMembershipListener listener = new RegionMembershipListenerAdapter() {
 +      public void afterRemoteRegionDeparture(RegionEvent event) {
 +        synchronized (detectedDeparture_testCommitDistributionException) {
 +          detectedDeparture_testCommitDistributionException[0] = Boolean.TRUE;
 +          detectedDeparture_testCommitDistributionException.notify();
 +        }
 +      }
 +    };
 +    
 +    // create region in controller...
 +    MembershipAttributes ra = new MembershipAttributes(
 +        requiredRoles, LossAction.NO_ACCESS, ResumptionAction.NONE);
 +    AttributesFactory fac = new AttributesFactory();
 +    fac.setMembershipAttributes(ra);
 +    fac.setScope(getRegionScope());
 +    fac.addCacheListener(listener);
 +    RegionAttributes attr = fac.create();
 +    Region region = createRootRegion(name, attr);
 +    
 +    // use vm1 to create role
 +    Host.getHost(0).getVM(1).invoke(new CacheSerializableRunnable("Create Region") {
 +      public void run2() throws CacheException {
 +        createConnection(new String[] {roleA});
 +        AttributesFactory fac = new AttributesFactory();
 +        fac.setScope(getRegionScope());
 +        RegionAttributes attr = fac.create();
 +        createRootRegion(name, attr);
 +      }
 +    });
 +    
 +    // define the afterReleaseLocalLocks callback
-     Runnable removeRequiredRole = new SerializableRunnable() {
++    SerializableRunnableIF removeRequiredRole = new SerializableRunnableIF() {
 +      public void run() {
 +        Host.getHost(0).getVM(1).invoke(new SerializableRunnable("Close Region") {
 +          public void run() {
 +            getRootRegion(name).close();
 +          }
 +        });
 +        try {
 +          synchronized (detectedDeparture_testCommitDistributionException) {
 +            while (detectedDeparture_testCommitDistributionException[0] == Boolean.FALSE) {
 +              detectedDeparture_testCommitDistributionException.wait();
 +            }
 +          }
 +        }
 +        catch (InterruptedException e) {fail("interrupted");}
 +      }
 +    };
 +    
 +    // define the add and remove expected exceptions
 +    final String expectedExceptions = 
 +      "com.gemstone.gemfire.internal.cache.CommitReplyException";
 +    SerializableRunnable addExpectedExceptions = 
 +      new CacheSerializableRunnable("addExpectedExceptions") {
 +        public void run2() throws CacheException {
 +          getCache().getLogger().info("<ExpectedException action=add>" + 
 +              expectedExceptions + "</ExpectedException>");
 +        }
 +      };
 +    SerializableRunnable removeExpectedExceptions = 
 +      new CacheSerializableRunnable("removeExpectedExceptions") {
 +        public void run2() throws CacheException {
 +          getCache().getLogger().info("<ExpectedException action=remove>" + 
 +              expectedExceptions + "</ExpectedException>");
 +        }
 +      };
 +
 +    // perform the actual test...
 +      
 +    CacheTransactionManager ctm = cache.getCacheTransactionManager();
 +    ctm.begin();
 +    TXStateInterface txStateProxy = ((TXManagerImpl)ctm).getTXState();
 +    ((TXStateProxyImpl)txStateProxy).forceLocalBootstrap();
 +    TXState txState = (TXState)((TXStateProxyImpl)txStateProxy).getRealDeal(null,null);
-     txState.setBeforeSend(removeRequiredRole);
++    txState.setBeforeSend(() -> {
++      try { 
++        removeRequiredRole.run(); 
++      } catch(Exception e) {
++        throw new RuntimeException(e);
++      }
++    });
 +    
 +    // now start a transaction and commit it
 +    region.put("KEY", "VAL");
 +      
 +    addExpectedExceptions.run();
 +    Host.getHost(0).getVM(1).invoke(addExpectedExceptions);
 +    
 +    try {
 +      ctm.commit();
 +      fail("Should have thrown CommitDistributionException");
 +    }
 +    catch (CommitDistributionException e) {
 +      // pass
 +    }
 +    finally {
 +      removeExpectedExceptions.run();
 +      Host.getHost(0).getVM(1).invoke(removeExpectedExceptions);
 +    }
 +  }
 +  
 +  protected static Boolean[] detectedDeparture_testRegionDistributionException = 
 +    { Boolean.FALSE };
 +  public void testRegionDistributionException() throws Exception {
 +    if (getRegionScope().isDistributedNoAck()) return; // skip test under DistributedNoAck
 +    
 +    final String name = this.getUniqueName();
 +    final String roleA = name+"-A";
 +    final String[] requiredRoles = {roleA};
 +    Set requiredRolesSet = new HashSet();
 +    for (int i = 0; i < requiredRoles.length; i++) {
 +      requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
 +    }
 +    assertEquals(requiredRoles.length, requiredRolesSet.size());
 +
 +    // connect controller to system...
 +    Properties config = new Properties();
 +    config.setProperty(DistributionConfig.ROLES_NAME, "");
 +    getSystem(config);
 +    
 +    getCache();
 +    
 +    RegionMembershipListener listener = new RegionMembershipListenerAdapter() {
 +      public void afterRemoteRegionDeparture(RegionEvent event) {
 +        synchronized (detectedDeparture_testRegionDistributionException) {
 +          detectedDeparture_testRegionDistributionException[0] = Boolean.TRUE;
 +          detectedDeparture_testRegionDistributionException.notify();
 +        }
 +      }
 +    };
 +
 +    // create region in controller...
 +    MembershipAttributes ra = new MembershipAttributes(
 +        requiredRoles, LossAction.NO_ACCESS, ResumptionAction.NONE);
 +    AttributesFactory fac = new AttributesFactory();
 +    fac.setMembershipAttributes(ra);
 +    fac.setScope(getRegionScope());
 +//    fac.addCacheListener(listener);
 +    RegionAttributes attr = fac.create();
 +    Region region = createRootRegion(name, attr);
 +    
 +    assertTrue(((AbstractRegion)region).requiresReliabilityCheck());
 +    
 +    // use vm1 to create role
 +    CacheSerializableRunnable createRegion = new CacheSerializableRunnable("Create Region") {
 +      public void run2() throws CacheException {
 +        createConnection(new String[] {roleA});
 +        AttributesFactory fac = new AttributesFactory();
 +        fac.setScope(getRegionScope());
 +        RegionAttributes attr = fac.create();
 +        createRootRegion(name, attr);
 +      }
 +    };
 +    
 +    Host.getHost(0).getVM(1).invoke(createRegion);
 +    region.put("DESTROY_ME", "VAL");
 +    region.put("INVALIDATE_ME", "VAL");
 +      
 +    // define the afterReleaseLocalLocks callback
-     Runnable removeRequiredRole = new SerializableRunnable() {
++    SerializableRunnable removeRequiredRole = new SerializableRunnable() {
 +      public void run() {
 +        Host.getHost(0).getVM(1).invoke(new SerializableRunnable("Close Region") {
 +          public void run() {
 +            getRootRegion(name).close();
 +          }
 +        });
 +//        try {
 +//          synchronized (detectedDeparture_testRegionDistributionException) {
 +//            while (detectedDeparture_testRegionDistributionException[0] == Boolean.FALSE) {
 +//              detectedDeparture_testRegionDistributionException.wait();
 +//            }
 +//          }
 +//        }
 +//        catch (InterruptedException e) {}
 +      }
 +    };
-     DistributedCacheOperation.setBeforePutOutgoing(removeRequiredRole);
++    DistributedCacheOperation.setBeforePutOutgoing(() -> {
++     try {
++       removeRequiredRole.run();
++     } catch(Exception e) {
++       throw new RuntimeException(e);
++     }
++    });
 +
 +    Runnable reset = new Runnable() {
 +      public void run() {
 +//        synchronized (detectedDeparture_testRegionDistributionException) {
 +//          detectedDeparture_testRegionDistributionException[0] = Boolean.FALSE;
 +//        }
 +      }
 +    };
 +    
 +    // PUT    
 +    try {
 +      region.put("KEY", "VAL");
 +      fail("Should have thrown RegionDistributionException");
 +    }
 +    catch (RegionDistributionException e) {
 +      // pass
 +    }
 +    
 +    // INVALIDATE
 +    reset.run();
 +    Host.getHost(0).getVM(1).invoke(createRegion);
 +    try {
 +      region.invalidate("INVALIDATE_ME");
 +      fail("Should have thrown RegionDistributionException");
 +    }
 +    catch (RegionDistributionException e) {
 +      // pass
 +    }
 +    
 +    // DESTROY
 +    reset.run();
 +    Host.getHost(0).getVM(1).invoke(createRegion);
 +    try {
 +      region.destroy("DESTROY_ME");
 +      fail("Should have thrown RegionDistributionException");
 +    }
 +    catch (RegionDistributionException e) {
 +      // pass
 +    }
 +
 +    // CLEAR
 +    reset.run();
 +    Host.getHost(0).getVM(1).invoke(createRegion);
 +    try {
 +      region.clear();
 +      fail("Should have thrown RegionDistributionException");
 +    }
 +    catch (RegionDistributionException e) {
 +      // pass
 +    }
 +    
 +    // PUTALL
 +    reset.run();
 +    Host.getHost(0).getVM(1).invoke(createRegion);
 +    try {
 +      Map putAll = new HashMap();
 +      putAll.put("PUTALL_ME", "VAL");
 +      region.putAll(putAll);
 +      fail("Should have thrown RegionDistributionException");
 +    }
 +    catch (RegionDistributionException e) {
 +      // pass
 +    }
 +    
 +    // INVALIDATE REGION
 +    reset.run();
 +    Host.getHost(0).getVM(1).invoke(createRegion);
 +    try {
 +      region.invalidateRegion();
 +      fail("Should have thrown RegionDistributionException");
 +    }
 +    catch (RegionDistributionException e) {
 +      // pass
 +    }
 +    
 +    // DESTROY REGION
 +    reset.run();
 +    Host.getHost(0).getVM(1).invoke(createRegion);
 +    try {
 +      region.destroyRegion();
 +      fail("Should have thrown RegionDistributionException");
 +    }
 +    catch (RegionDistributionException e) {
 +      // pass
 +    }
 +  }
 +
 +  public void testReinitialization() throws Exception {
 +    final String name = this.getUniqueName();
 +    final String roleA = name+"-A";
 +    final String[] requiredRoles = {roleA};
 +    Set requiredRolesSet = new HashSet();
 +    for (int i = 0; i < requiredRoles.length; i++) {
 +      requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
 +    }
 +    assertEquals(requiredRoles.length, requiredRolesSet.size());
 +
 +    // connect controller to system...
 +    Properties config = new Properties();
 +    config.setProperty(DistributionConfig.ROLES_NAME, "");
 +    getSystem(config);
 +    
 +    getCache();
 +    
 +    // create region in controller...
 +    MembershipAttributes ra = new MembershipAttributes(
 +        requiredRoles, LossAction.NO_ACCESS, ResumptionAction.REINITIALIZE);
 +    AttributesFactory fac = new AttributesFactory();
 +    fac.setMembershipAttributes(ra);
 +    fac.setScope(getRegionScope());
 +    fac.setDataPolicy(DataPolicy.REPLICATE);
 +    RegionAttributes attr = fac.create();
 +    Region region = createRootRegion(name, attr);
 +    
 +    assertTrue(((AbstractRegion)region).requiresReliabilityCheck());
 +    assertFalse(RequiredRoles.checkForRequiredRoles(region).isEmpty());
 +
 +    final String key = "KEY-testReinitialization";
 +    final String val = "VALUE-testReinitialization";
 +    
 +    Host.getHost(0).getVM(0).invoke(new CacheSerializableRunnable("Create Data") {
 +      public void run2() throws CacheException {
 +        createConnection(new String[] {});
 +        AttributesFactory fac = new AttributesFactory();
 +        fac.setScope(getRegionScope());
 +        fac.setDataPolicy(DataPolicy.REPLICATE);
 +        RegionAttributes attr = fac.create();
 +        Region region = createRootRegion(name, attr);
 +        region.put(key, val);
 +      }
 +    });
 +
 +    final Region finalRegion = region;
 +    Thread thread = new Thread(new Runnable() {
 +      public void run() {
 +        try {
 +          RequiredRoles.waitForRequiredRoles(finalRegion, -1);
 +        } 
 +        catch (InterruptedException e) {fail("interrupted");}
 +        catch (RegionReinitializedException e) {}
 +      }
 +    });
 +    thread.start();
 +    
 +    // create role and verify reinitialization took place
 +    Host.getHost(0).getVM(1).invokeAsync(new CacheSerializableRunnable("Create Role") {
 +      public void run2() throws CacheException {
 +        createConnection(new String[] {roleA});
 +        AttributesFactory fac = new AttributesFactory();
 +        fac.setScope(getRegionScope());
 +        RegionAttributes attr = fac.create();
 +        createRootRegion(name, attr);
 +      }
 +    });
 +    
 +    ThreadUtils.join(thread, 30 * 1000);
 +    assertTrue(region.isDestroyed());
 +    try {
 +      region.put("fee", "fi");
 +      fail("Should have thrown RegionReinitializedException");
 +    }
 +    catch(RegionReinitializedException e) {
 +      // pass
 +    }
 +    try {
 +      RequiredRoles.checkForRequiredRoles(region);
 +      fail("Should have thrown RegionReinitializedException");
 +    }
 +    catch(RegionReinitializedException e) {
 +      // pass
 +    }
 +    try {
 +      Role role = (Role) requiredRolesSet.iterator().next();
 +      RequiredRoles.isRoleInRegionMembership(region, role);
 +      fail("Should have thrown RegionReinitializedException");
 +    }
 +    catch(RegionReinitializedException e) {
 +      // pass
 +    }
 +    
 +    region = getRootRegion(name);
 +    assertNotNull(region);
 +    assertTrue(((AbstractRegion)region).requiresReliabilityCheck());
 +    assertTrue(RequiredRoles.checkForRequiredRoles(region).isEmpty());
 +    assertNotNull(region.getEntry(key));
 +    assertEquals(val, region.getEntry(key).getValue());
 +  }
 +  
 +}
 +

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/RemoveAllMultiVmDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/RemoveAllMultiVmDUnitTest.java
index f45c403,0000000..cfd1ce8
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/RemoveAllMultiVmDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/RemoveAllMultiVmDUnitTest.java
@@@ -1,300 -1,0 +1,300 @@@
 +/*
 + * 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.
 + */
 +
 +/*
 + * RemoveAllMultiVmDUnitTest.java
 + *
 + * Adapted from PutAllMultiVmDUnitTest
 + */
 +package com.gemstone.gemfire.cache30;
 +
 +import java.util.Arrays;
 +import java.util.Collections;
 +import java.util.Properties;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.CacheFactory;
 +import com.gemstone.gemfire.cache.CacheTransactionManager;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionAttributes;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.distributed.DistributedSystem;
 +import com.gemstone.gemfire.test.dunit.DistributedTestCase;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.Invoke;
 +import com.gemstone.gemfire.test.dunit.SerializableRunnable;
 +import com.gemstone.gemfire.test.dunit.VM;
 +
 +/**
 + *
 + * @author  darrel
 + */
 +public class RemoveAllMultiVmDUnitTest extends DistributedTestCase {
 +    
 +    /** Creates a new instance of RemoveAllMultiVmDUnitTest */
 +    public RemoveAllMultiVmDUnitTest(String name) {
 +        super(name);
 +    }
 +    
 +    static Cache cache;
 +    static Properties props = new Properties();
 +    static Properties propsWork = new Properties();
 +    static DistributedSystem ds = null;
 +    static Region region;
 +    static Region mirroredRegion;
 +    static CacheTransactionManager cacheTxnMgr;
 +    
 +    @Override
 +    public void setUp() throws Exception {
 +      super.setUp();
 +      Host host = Host.getHost(0);
 +      VM vm0 = host.getVM(0);
 +      VM vm1 = host.getVM(1);
-       vm0.invoke(RemoveAllMultiVmDUnitTest.class, "createCache");
-       vm1.invoke(RemoveAllMultiVmDUnitTest.class, "createCache");
++      vm0.invoke(() -> RemoveAllMultiVmDUnitTest.createCache());
++      vm1.invoke(() -> RemoveAllMultiVmDUnitTest.createCache());
 +    }
 +    
 +    @Override
 +    protected final void preTearDown() throws Exception {
 +      Host host = Host.getHost(0);
 +      VM vm0 = host.getVM(0);
 +      VM vm1 = host.getVM(1);
-       vm0.invoke(RemoveAllMultiVmDUnitTest.class, "closeCache");
-       vm1.invoke(RemoveAllMultiVmDUnitTest.class, "closeCache");
++      vm0.invoke(() -> RemoveAllMultiVmDUnitTest.closeCache());
++      vm1.invoke(() -> RemoveAllMultiVmDUnitTest.closeCache());
 +      cache = null;
 +      Invoke.invokeInEveryVM(new SerializableRunnable() { public void run() { cache = null; } });
 +    }
 +    
 +    public static void createCache(){
 +        try{
 +            ds = (new RemoveAllMultiVmDUnitTest("temp")).getSystem(props);
 +            cache = CacheFactory.create(ds);
 +            AttributesFactory factory  = new AttributesFactory();
 +            factory.setScope(Scope.DISTRIBUTED_ACK);
 +            RegionAttributes attr = factory.create();
 +            region = cache.createRegion("map", attr);
 +            
 +        } catch (Exception ex){
 +            ex.printStackTrace();
 +        }
 +    }//end of createCache
 +    
 +    public static void createMirroredRegion(){
 +        try{
 +            AttributesFactory factory  = new AttributesFactory();
 +            factory.setDataPolicy(DataPolicy.REPLICATE);
 +            factory.setScope(Scope.DISTRIBUTED_ACK);
 +            RegionAttributes attr = factory.create();
 +            mirroredRegion = cache.createRegion("mirrored", attr);
 +        } catch (Exception ex){
 +            ex.printStackTrace();
 +        }
 +    }//end of createCache
 +    
 +    public static void closeCache(){
 +        try{
 +            //System.out.println("closing cache cache cache cache cache 33333333");
 +            cache.close();
 +            ds.disconnect();
 +            //System.out.println("closed cache cache cache cache cache 44444444");
 +        } catch (Exception ex){
 +            ex.printStackTrace();
 +        }
 +    }//end of closeCache
 +    
 +    
 +    //tests methods
 +    
 +    public void testLocalRemoveAll(){
 +      Host host = Host.getHost(0);
 +      VM vm0 = host.getVM(0);
 +    
 +      vm0.invoke(new CacheSerializableRunnable("testLocalRemoveAll"){
 +          public void run2() throws CacheException {
 +              int cntr = 0, cntr1 = 0;
 +              for(int i=1; i<6; i++) {
 +                  region.put(Integer.valueOf(i), new String("testLocalRemoveAll"+i));
 +                  cntr++;
 +              }
 +              
 +              int size1 = region.size();
 +              assertEquals(5, size1);
 +              
 +              region.removeAll(Collections.EMPTY_SET);
 +              assertEquals(size1, region.size());
 +              region.removeAll(Collections.singleton(Integer.valueOf(666)));
 +              assertEquals(size1, region.size());
 +              assertEquals(true, region.containsKey(Integer.valueOf(1)));
 +              region.removeAll(Collections.singleton(Integer.valueOf(1)));
 +              assertEquals(false, region.containsKey(Integer.valueOf(1)));
 +              assertEquals(size1-1, region.size());
 +              size1--;
 +              region.removeAll(Arrays.asList(Integer.valueOf(2), Integer.valueOf(3)));
 +              assertEquals(size1-2, region.size());
 +              size1 -= 2;
 +         }
 +      } );
 +    }
 +    
 +    public void testLocalTxRemoveAll(){
 +      Host host = Host.getHost(0);
 +      VM vm0 = host.getVM(0);
 +    
 +      vm0.invoke(new CacheSerializableRunnable("testSimpleRemoveAllTx"){
 +        public void run2() throws CacheException {
 +            cacheTxnMgr = cache.getCacheTransactionManager();
 +            int cntr = 0;
 +            for(int i=1; i<6; i++) {
 +                region.put(Integer.valueOf(i), new String("testLocalTxRemoveAll"+i));
 +                cntr++;
 +            }
 +            
 +            int size1 = region.size();
 +            assertEquals(5, size1);
 +            
 +            cacheTxnMgr.begin();
 +            region.removeAll(Arrays.asList(Integer.valueOf(1), Integer.valueOf(2)));
 +            cacheTxnMgr.rollback();
 +            
 +            assertEquals(size1, region.size());
 +            
 +            cacheTxnMgr.begin();
 +            region.removeAll(Arrays.asList(Integer.valueOf(666), Integer.valueOf(1), Integer.valueOf(2)));
 +            cacheTxnMgr.commit();
 +            
 +            int size2 = region.size();
 +            
 +            assertEquals(size1-2, size2);
 +            assertEquals(true, region.containsKey(Integer.valueOf(3)));
 +            assertEquals(false, region.containsKey(Integer.valueOf(2)));
 +            assertEquals(false, region.containsKey(Integer.valueOf(1)));
 +        }
 +    } );
 +    }
 +
 +    public void testDistributedRemoveAll(){
 +        Host host = Host.getHost(0);
 +        VM vm0 = host.getVM(0);
 +        VM vm1 = host.getVM(1);
 +        
 +        vm1.invoke(new CacheSerializableRunnable("create mirrored region"){
 +          public void run2() throws CacheException {
 +              createMirroredRegion();
 +          }
 +        });
 +      
 +        vm0.invoke(new CacheSerializableRunnable("testDistributedRemoveAll1"){
 +            public void run2() throws CacheException {
 +                createMirroredRegion();
 +                int cntr = 0, cntr1 = 0;
 +                for(int i=1; i<6; i++) {
 +                  mirroredRegion.put(Integer.valueOf(i), new String("testDistributedRemoveAll"+i));
 +                    cntr++;
 +                }
 +                
 +                int size1 = mirroredRegion.size();
 +                assertEquals(5, size1);
 +                
 +                mirroredRegion.removeAll(Collections.EMPTY_SET);
 +                assertEquals(size1, mirroredRegion.size());
 +                mirroredRegion.removeAll(Collections.singleton(Integer.valueOf(666)));
 +                assertEquals(size1, mirroredRegion.size());
 +                assertEquals(true, mirroredRegion.containsKey(Integer.valueOf(1)));
 +                mirroredRegion.removeAll(Collections.singleton(Integer.valueOf(1)));
 +                assertEquals(false, mirroredRegion.containsKey(Integer.valueOf(1)));
 +                assertEquals(size1-1, mirroredRegion.size());
 +                size1--;
 +                mirroredRegion.removeAll(Arrays.asList(Integer.valueOf(2), Integer.valueOf(3)));
 +                assertEquals(size1-2, mirroredRegion.size());
 +                size1 -= 2;
 +           }
 +        } );
 +        
 +        vm1.invoke(new CacheSerializableRunnable("testDistributedRemoveAllVerifyRemote"){
 +          public void run2() throws CacheException {
 +            assertEquals(true, mirroredRegion.containsKey(Integer.valueOf(5)));
 +            assertEquals(true, mirroredRegion.containsKey(Integer.valueOf(4)));
 +            assertEquals(false, mirroredRegion.containsKey(Integer.valueOf(3)));
 +            assertEquals(false, mirroredRegion.containsKey(Integer.valueOf(2)));
 +            assertEquals(false, mirroredRegion.containsKey(Integer.valueOf(1)));
 +            assertEquals(2, mirroredRegion.size());
 +          }
 +        } );
 +    }
 +    public void testDistributedTxRemoveAll(){
 +      Host host = Host.getHost(0);
 +      VM vm0 = host.getVM(0);
 +      VM vm1 = host.getVM(1);
 +      
 +      vm1.invoke(new CacheSerializableRunnable("create mirrored region"){
 +        public void run2() throws CacheException {
 +            createMirroredRegion();
 +        }
 +      });
 +    
 +      vm0.invoke(new CacheSerializableRunnable("testDistributedTxRemoveAll1"){
 +          public void run2() throws CacheException {
 +              createMirroredRegion();
 +              int cntr = 0, cntr1 = 0;
 +              for(int i=1; i<6; i++) {
 +                mirroredRegion.put(Integer.valueOf(i), new String("testDistributedTxRemoveAll"+i));
 +                  cntr++;
 +              }
 +              
 +              int size1 = mirroredRegion.size();
 +              assertEquals(5, size1);
 +              cacheTxnMgr = cache.getCacheTransactionManager();
 +              
 +              cacheTxnMgr.begin();
 +             mirroredRegion.removeAll(Collections.EMPTY_SET);
 +             cacheTxnMgr.commit();
 +              assertEquals(size1, mirroredRegion.size());
 +              cacheTxnMgr.begin();
 +              mirroredRegion.removeAll(Collections.singleton(Integer.valueOf(666)));
 +              cacheTxnMgr.commit();
 +              assertEquals(size1, mirroredRegion.size());
 +              assertEquals(true, mirroredRegion.containsKey(Integer.valueOf(1)));
 +              cacheTxnMgr.begin();
 +              mirroredRegion.removeAll(Collections.singleton(Integer.valueOf(1)));
 +              cacheTxnMgr.commit();
 +              assertEquals(false, mirroredRegion.containsKey(Integer.valueOf(1)));
 +              assertEquals(size1-1, mirroredRegion.size());
 +              size1--;
 +              cacheTxnMgr.begin();
 +              mirroredRegion.removeAll(Arrays.asList(Integer.valueOf(2), Integer.valueOf(3)));
 +              cacheTxnMgr.commit();
 +              assertEquals(size1-2, mirroredRegion.size());
 +              size1 -= 2;
 +         }
 +      } );
 +      
 +      vm1.invoke(new CacheSerializableRunnable("testDistributedTxRemoveAllVerifyRemote"){
 +        public void run2() throws CacheException {
 +          assertEquals(true, mirroredRegion.containsKey(Integer.valueOf(5)));
 +          assertEquals(true, mirroredRegion.containsKey(Integer.valueOf(4)));
 +          assertEquals(false, mirroredRegion.containsKey(Integer.valueOf(3)));
 +          assertEquals(false, mirroredRegion.containsKey(Integer.valueOf(2)));
 +          assertEquals(false, mirroredRegion.containsKey(Integer.valueOf(1)));
 +          assertEquals(2, mirroredRegion.size());
 +        }
 +      } );
 +  }
 +     
 +}//end of RemoveAllMultiVmDUnitTest

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/test/java/com/gemstone/gemfire/cache30/RolePerformanceDUnitTest.java
----------------------------------------------------------------------
diff --cc geode-core/src/test/java/com/gemstone/gemfire/cache30/RolePerformanceDUnitTest.java
index e7c0ba7,0000000..babbf12
mode 100644,000000..100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/RolePerformanceDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/RolePerformanceDUnitTest.java
@@@ -1,189 -1,0 +1,188 @@@
 +/*
 + * 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.cache30;
 +
 +import java.util.Properties;
 +
 +import com.gemstone.gemfire.cache.AttributesFactory;
 +import com.gemstone.gemfire.cache.CacheException;
 +import com.gemstone.gemfire.cache.DataPolicy;
 +import com.gemstone.gemfire.cache.Region;
 +import com.gemstone.gemfire.cache.RegionAttributes;
 +import com.gemstone.gemfire.cache.Scope;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 +import com.gemstone.gemfire.test.dunit.Host;
 +import com.gemstone.gemfire.test.dunit.LogWriterUtils;
 +import com.gemstone.gemfire.test.dunit.SerializableRunnable;
 +
 +/**
 + * Tests the performance of Regions when Roles are assigned.
 + *
 + * @author Kirk Lund
 + * @since 5.0
 + */
 +public class RolePerformanceDUnitTest extends CacheTestCase {
 +
 +  public RolePerformanceDUnitTest(String name) {
 +    super(name);
 +  }
 +
 +  /**
 +   * Compares times required for series of puts with Roles assigned to
 +   * series of puts with no Roles assigned. Scope is D_ACK.
 +   * <p>
 +   * Up to 10 attempts will be made before failing.
 +   */
 +  public void testRolePerformance() {
 +    int maxAttempts = 10;
 +    for (int i = 1; i <= maxAttempts; i++) {
 +      try {
 +        if (i > 1) {
 +          // clean up from previous run
 +          closeCaches();
 +        }
 +        doTestRolePerformance();
 +        break;
 +      }
 +      // only catch assertion failures...
 +      catch (junit.framework.AssertionFailedError e) {
 +        if (i == maxAttempts) {
 +          throw e;
 +        }
 +        else {
 +          LogWriterUtils.getLogWriter().info("testRolePerformance attempt #" + i + 
 +            " failed -- reattempting up to 10x", e);
 +        }
 +      }
 +    }
 +  }
 +  
 +  /**
 +   * Implementation of testRolePerformance.
 +   */
 +  private void doTestRolePerformance() {
 +    final String name = this.getUniqueName();
 +
 +    // throw away this run
 +    createConnections(name, false);
 +    createRegions(name);
 +    executeOperations(name);
 +    
 +    closeCaches();
 +    
 +    // first time with no roles
 +    createConnections(name, false);
 +    createRegions(name);
 +    long millisNoRoles = executeOperations(name);
 +    
 +    closeCaches();
 +    
 +    // second time with roles
 +    createConnections(name, true);
 +    createRegions(name);
 +    long millisWithRoles = executeOperations(name);
 +    
 +    long deviation = (long)(millisNoRoles * 0.05); // 5% increase is allowed
 +    long ceiling = millisNoRoles + deviation;
 +
 +    String data = name + " results: millisNoRoles=" + millisNoRoles +
 +      ", millisWithRoles=" + millisWithRoles + ", deviation=" + deviation + 
 +      ", ceiling=" + ceiling;
 +    LogWriterUtils.getLogWriter().info(data);
 +    
 +    assertTrue("millisWithRoles is greater than allowable deviation: " + data,
 +               millisWithRoles <= ceiling);
 +  }
 +  
 +  /**
 +   * Create connection to distributed system in all vms and assign one role
 +   * to each if assignRoles is true.
 +   */
 +  private void createConnections(final String name, final boolean assignRoles) {
 +    final String[][] vmRoles = new String[][] 
 +      {{name+"-A"},{name+"-B"},{name+"-C"},{name+"-D"}};
 +    for (int i = 0; i < vmRoles.length; i++) {
 +      final int vm = i;
 +      Host.getHost(0).getVM(vm).invoke(new SerializableRunnable("Connect") {
 +        public void run() {
 +          Properties config = new Properties();
 +          if (assignRoles) {
 +            config.setProperty(DistributionConfig.ROLES_NAME, vmRoles[vm][0]);
 +          }
 +          getSystem(config);
 +        }
 +      });
 +    }
 +  }
 +  
 +  /**
 +   * Close the cache in all vms.
 +   */
 +  private void closeCaches() { 
 +    for (int i = 0; i < Host.getHost(0).getVMCount(); i++) {
 +      final int vm = i;
 +      Host.getHost(0).getVM(vm).invoke(new CacheSerializableRunnable("Close Cache") {
 +        public void run2() throws CacheException {
 +          closeCache();
 +        }
 +      });
 +    }
 +  }
 +  
 +  /**
 +   * Create the named root region in all vms.
 +   */
 +  private void createRegions(final String name) {
 +    for (int i = 0; i < Host.getHost(0).getVMCount(); i++) {
 +      final int vm = i;
 +      Host.getHost(0).getVM(vm).invoke(new CacheSerializableRunnable("Create Region") {
 +        public void run2() throws CacheException {
 +          AttributesFactory fac = new AttributesFactory();
 +          fac.setScope(Scope.DISTRIBUTED_ACK);
 +          fac.setDataPolicy(DataPolicy.REPLICATE);
 +          RegionAttributes attr = fac.create();
 +          createRootRegion(name, attr);
 +        }
 +      });
 +    }
 +  }
 +  
 +  /**
 +   * Execute operations on the named region in one vm.
 +   */
 +  private long executeOperations(final String name) {
 +    Host.getHost(0).getVM(0).invoke(new CacheSerializableRunnable("Operations") {
 +      public void run2() throws CacheException {
 +        Region region = getRootRegion(name);
 +        long begin = System.currentTimeMillis();
 +        for (int i = 0; i < 1000; i++) {
 +          region.put("KEY-"+i, "VAL-"+i);
 +        }
 +        long finish = System.currentTimeMillis();
 +        timing = finish - begin;
 +      }
 +    });
-     Long timing = (Long) Host.getHost(0).getVM(0).invoke(
-       RolePerformanceDUnitTest.class, "getTiming", new Object[] {});
++    Long timing = (Long) Host.getHost(0).getVM(0).invoke(() -> RolePerformanceDUnitTest.getTiming());
 +    return timing.longValue();
 +  }
 +  protected static transient long timing = -1;
 +  private static Long getTiming() {
 +    return new Long(timing);
 +  }
 +  
 +}
 +


[47/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/hdfs/internal/hoplog/HdfsSortedOplogOrganizer.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/hdfs/internal/hoplog/HdfsSortedOplogOrganizer.java
index 9890b3b,0000000..61b925b
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/hdfs/internal/hoplog/HdfsSortedOplogOrganizer.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/hdfs/internal/hoplog/HdfsSortedOplogOrganizer.java
@@@ -1,2007 -1,0 +1,2007 @@@
 +/*
 + * 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.cache.hdfs.internal.hoplog;
 +
 +import java.io.IOException;
 +import java.nio.ByteBuffer;
 +import java.util.ArrayList;
 +import java.util.Arrays;
 +import java.util.Collections;
 +import java.util.EnumMap;
 +import java.util.Iterator;
 +import java.util.LinkedList;
 +import java.util.List;
 +import java.util.NoSuchElementException;
 +import java.util.concurrent.Callable;
 +import java.util.concurrent.ConcurrentHashMap;
 +import java.util.concurrent.ConcurrentMap;
 +import java.util.concurrent.ConcurrentSkipListSet;
 +import java.util.concurrent.Future;
 +import java.util.concurrent.TimeUnit;
 +import java.util.concurrent.atomic.AtomicBoolean;
 +import java.util.concurrent.atomic.AtomicInteger;
 +import java.util.concurrent.atomic.AtomicLong;
 +import java.util.concurrent.locks.ReadWriteLock;
 +import java.util.concurrent.locks.ReentrantReadWriteLock;
 +import java.util.regex.Matcher;
 +import java.util.regex.Pattern;
 +
++import com.gemstone.gemfire.internal.hll.CardinalityMergeException;
++import com.gemstone.gemfire.internal.hll.HyperLogLog;
++import com.gemstone.gemfire.internal.hll.ICardinality;
++import com.gemstone.gemfire.internal.hll.MurmurHash;
 +import org.apache.commons.lang.NotImplementedException;
 +import org.apache.hadoop.fs.FileStatus;
 +import org.apache.hadoop.fs.FileSystem;
 +import org.apache.hadoop.fs.Path;
 +import org.apache.hadoop.fs.PathFilter;
 +import org.apache.hadoop.ipc.RemoteException;
 +import org.apache.hadoop.util.ShutdownHookManager;
 +
 +import com.gemstone.gemfire.InternalGemFireException;
 +import com.gemstone.gemfire.cache.CacheClosedException;
 +import com.gemstone.gemfire.cache.hdfs.HDFSIOException;
 +import com.gemstone.gemfire.cache.hdfs.HDFSStore;
 +import com.gemstone.gemfire.cache.hdfs.internal.QueuedPersistentEvent;
 +import com.gemstone.gemfire.cache.hdfs.internal.SortedHoplogPersistedEvent;
- import com.gemstone.gemfire.cache.hdfs.internal.cardinality.CardinalityMergeException;
- import com.gemstone.gemfire.cache.hdfs.internal.cardinality.HyperLogLog;
- import com.gemstone.gemfire.cache.hdfs.internal.cardinality.ICardinality;
- import com.gemstone.gemfire.cache.hdfs.internal.cardinality.MurmurHash;
 +import com.gemstone.gemfire.cache.hdfs.internal.hoplog.HDFSCompactionManager.CompactionRequest;
 +import com.gemstone.gemfire.cache.hdfs.internal.hoplog.HDFSRegionDirector.HdfsRegionManager;
 +import com.gemstone.gemfire.cache.hdfs.internal.hoplog.Hoplog.HoplogReader;
 +import com.gemstone.gemfire.cache.hdfs.internal.hoplog.Hoplog.HoplogReaderActivityListener;
 +import com.gemstone.gemfire.cache.hdfs.internal.hoplog.Hoplog.HoplogWriter;
 +import com.gemstone.gemfire.cache.hdfs.internal.hoplog.Hoplog.Meta;
 +import com.gemstone.gemfire.cache.hdfs.internal.hoplog.mapreduce.HoplogUtil;
 +import com.gemstone.gemfire.internal.HeapDataOutputStream;
 +import com.gemstone.gemfire.internal.cache.ForceReattemptException;
 +import com.gemstone.gemfire.internal.cache.PrimaryBucketException;
 +import com.gemstone.gemfire.internal.cache.execute.BucketMovedException;
 +import com.gemstone.gemfire.internal.cache.persistence.soplog.SortedOplogStatistics.IOOperation;
 +import com.gemstone.gemfire.internal.cache.persistence.soplog.TrackedReference;
 +import com.gemstone.gemfire.internal.concurrent.ConcurrentHashSet;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.logging.log4j.LocalizedMessage;
 +import org.apache.hadoop.hbase.util.FSUtils;
 +
 +/**
 + * Manages sorted oplog files for a bucket. An instance per bucket will exist in
 + * each PR
 + * 
 + * @author ashvina
 + */
 +public class HdfsSortedOplogOrganizer extends AbstractHoplogOrganizer<SortedHoplogPersistedEvent> {
 +  public static final int AVG_NUM_KEYS_PER_INDEX_BLOCK = 200;
 +  
 +  // all valid sorted hoplogs will follow the following name pattern
 +  public static final String SORTED_HOPLOG_REGEX = HOPLOG_NAME_REGEX + "("
 +      + FLUSH_HOPLOG_EXTENSION + "|" + MINOR_HOPLOG_EXTENSION + "|"
 +      + MAJOR_HOPLOG_EXTENSION + ")";
 +  public static final Pattern SORTED_HOPLOG_PATTERN = Pattern.compile(SORTED_HOPLOG_REGEX);
 +  
 +  //Amount of time before deleting old temporary files
 +  final long TMP_FILE_EXPIRATION_TIME_MS = Long.getLong(HoplogConfig.TMP_FILE_EXPIRATION, HoplogConfig.TMP_FILE_EXPIRATION_DEFAULT);
 +  
 +  static float RATIO = HoplogConfig.COMPACTION_FILE_RATIO_DEFAULT;
 +
 +  // Compacter for this bucket
 +  private Compactor compacter;
 +    
 +  private final HoplogReadersController hoplogReadersController;
 +  private AtomicLong previousCleanupTimestamp = new AtomicLong(Long.MIN_VALUE);
 +
 +  /**
 +   * The default HLL constant. gives an accuracy of about 3.25%
 +   * public only for testing upgrade from 1.3 to 1.4
 +   */
 +  public static double HLL_CONSTANT = 0.03;
 +  /**
 +   * This estimator keeps track of this buckets entry count. This value is
 +   * affected by flush and compaction cycles
 +   */
 +  private volatile ICardinality bucketSize = new HyperLogLog(HLL_CONSTANT);
 +  //A set of tmp files that existed when this bucket organizer was originally
 +  //created. These may still be open by the old primary, or they may be
 +  //abandoned files.
 +  private LinkedList<FileStatus> oldTmpFiles;
 +
 +  private ConcurrentMap<Hoplog, Boolean> tmpFiles = new ConcurrentHashMap<Hoplog, Boolean>();
 +
 +  protected volatile boolean organizerClosed = false;
 +
 +  /**
 +   * For the 1.4 release we are changing the HLL_CONSTANT which will make the
 +   * old persisted HLLs incompatible with the new HLLs. To fix this we will
 +   * force a major compaction when the system starts up so that we will only
 +   * have new HLLs in the system (see bug 51403)
 +   */
 +  private boolean startCompactionOnStartup = false;
 +
 +  /**
 +   * @param region
 +   *          Region manager instance. Instances of hdfs listener instance,
 +   *          stats collector, file system, etc are shared by all buckets of a
 +   *          region and provided by region manager instance
 +   * @param bucketId bucket id to be managed by this organizer
 +   * @throws IOException
 +   */
 +  public HdfsSortedOplogOrganizer(HdfsRegionManager region, int bucketId) throws IOException{
 +    super(region, bucketId);
 +    
 +    String val = System.getProperty(HoplogConfig.COMPACTION_FILE_RATIO);
 +    try {
 +      RATIO = Float.parseFloat(val);
 +    } catch (Exception e) {
 +    }
 +
 +    hoplogReadersController = new HoplogReadersController();
 +    
 +    // initialize with all the files in the directory
 +    List<Hoplog> hoplogs = identifyAndLoadSortedOplogs(true);
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("{}Initializing bucket with existing hoplogs, count = " + hoplogs.size(), logPrefix);
 +    }
 +    for (Hoplog hoplog : hoplogs) {
 +      addSortedOplog(hoplog, false, true);
 +    }
 +
 +    // initialize sequence to the current maximum
 +    sequence = new AtomicInteger(findMaxSequenceNumber(hoplogs));
 +    
 +    initOldTmpFiles();
 +    
 +    FileSystem fs = store.getFileSystem();
 +    Path cleanUpIntervalPath = new Path(store.getHomeDir(), HoplogConfig.CLEAN_UP_INTERVAL_FILE_NAME); 
 +    if (!fs.exists(cleanUpIntervalPath)) {
 +      long intervalDurationMillis = store.getPurgeInterval() * 60 * 1000;
 +      HoplogUtil.exposeCleanupIntervalMillis(fs, cleanUpIntervalPath, intervalDurationMillis);
 +    }
 +
 +    if (startCompactionOnStartup) {
 +      forceCompactionOnVersionUpgrade();
 +      if (logger.isInfoEnabled()) {
 +        logger.info(LocalizedStrings.HOPLOG_MAJOR_COMPACTION_SCHEDULED_FOR_BETTER_ESTIMATE);
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Iterates on the input buffer and persists it in a new sorted oplog. This operation is
 +   * synchronous and blocks the thread.
 +   */
 +  @Override
 +  public void flush(Iterator<? extends QueuedPersistentEvent> iterator, final int count)
 +      throws IOException, ForceReattemptException {
 +    assert iterator != null;
 +
 +    if (logger.isDebugEnabled())
 +      logger.debug("{}Initializing flush operation", logPrefix);
 +    
 +    final Hoplog so = getTmpSortedOplog(null, FLUSH_HOPLOG_EXTENSION);
 +    HoplogWriter writer = null;
 +    ICardinality localHLL = new HyperLogLog(HLL_CONSTANT);
 +    
 +    // variables for updating stats
 +    long start = stats.getFlush().begin();
 +    int byteCount = 0;
 +    
 +    try {
 +      /**MergeGemXDHDFSToGFE changed the following statement as the code of HeapDataOutputStream is not merged */
 +      //HeapDataOutputStream out = new HeapDataOutputStream();
 +      
 +      try {
 +        writer = this.store.getSingletonWriter().runSerially(new Callable<Hoplog.HoplogWriter>() {
 +          @Override
 +          public HoplogWriter call() throws Exception {
 +            return so.createWriter(count);
 +          }
 +        });
 +      } catch (Exception e) {
 +        if (e instanceof IOException) {
 +          throw (IOException)e;
 +        }
 +        throw new IOException(e);
 +      }
 +
 +      while (iterator.hasNext() && !this.organizerClosed) {
 +        HeapDataOutputStream out = new HeapDataOutputStream(1024, null);
 +        
 +        QueuedPersistentEvent item = iterator.next();
 +        item.toHoplogEventBytes(out);
 +        byte[] valueBytes = out.toByteArray();
 +        writer.append(item.getRawKey(), valueBytes);
 +        
 +        // add key length and value length to stats byte counter
 +        byteCount += (item.getRawKey().length + valueBytes.length);
 +
 +        // increment size only if entry is not deleted
 +        if (!isDeletedEntry(valueBytes, 0)) {
 +          int hash = MurmurHash.hash(item.getRawKey());
 +          localHLL.offerHashed(hash);
 +        }
 +        /**MergeGemXDHDFSToGFE how to clear for reuse. Leaving it for Darrel to merge this change*/
 +        //out.clearForReuse();
 +      }
 +      if (organizerClosed)
 +        throw new BucketMovedException("The current bucket is moved BucketID: "+  
 +            this.bucketId + " Region name: " +  this.regionManager.getRegion().getName());
 +      
 +      // append completed. provide cardinality and close writer
 +      writer.close(buildMetaData(localHLL));
 +      writer = null;
 +    } catch (IOException e) {
 +      stats.getFlush().error(start);
 +      try {
 +        e = handleWriteHdfsIOError(writer, so, e);
 +      } finally {
 +        //Set the writer to null because handleWriteHDFSIOError has
 +        //already closed the writer.
 +        writer = null;
 +      }
 +      throw e;
 +    } catch (BucketMovedException e) {
 +      stats.getFlush().error(start);
 +      deleteTmpFile(writer, so);
 +      writer = null;
 +      throw e;
 +    } finally {
 +      if (writer != null) {
 +        writer.close();
 +      }
 +    }
 +
 +    try{
 +      
 +      // ping secondaries before making the file a legitimate file to ensure 
 +      // that in case of split brain, no other vm has taken up as primary. #50110.  
 +      pingSecondaries();
 +      
 +      // rename file and check if renaming was successful
 +      synchronized (changePrimarylockObject) {
 +        if (!organizerClosed)
 +          makeLegitimate(so);
 +        else 
 +          throw new BucketMovedException("The current bucket is moved BucketID: "+  
 +              this.bucketId + " Region name: " +  this.regionManager.getRegion().getName());
 +      }
 +      try {
 +        so.getSize();
 +      } catch (IllegalStateException e) {
 +        throw new IOException("Failed to rename hoplog file:" + so.getFileName());
 +      }
 +      
 +      //Disabling this assertion due to bug 49740
 +      // check to make sure the sequence number is correct
 +//      if (ENABLE_INTEGRITY_CHECKS) {
 +//        Assert.assertTrue(getSequenceNumber(so) == findMaxSequenceNumber(identifyAndLoadSortedOplogs(false)), 
 +//            "Invalid sequence number detected for " + so.getFileName());
 +//      }
 +      
 +      // record the file for future maintenance and reads
 +      addSortedOplog(so, false, true);
 +      stats.getFlush().end(byteCount, start);
 +      incrementDiskUsage(so.getSize());
 +    } catch (BucketMovedException e) {
 +      stats.getFlush().error(start);
 +      deleteTmpFile(writer, so);
 +      writer = null;
 +      throw e;
 +    } catch (IOException e) {
 +      stats.getFlush().error(start);
 +      logger.warn(LocalizedStrings.HOPLOG_FLUSH_OPERATION_FAILED, e);
 +      throw e;
 +    }
 +
 +    submitCompactionRequests();
 +  }
 +
 +
 +  /**
 +   * store cardinality information in metadata
 +   * @param localHLL the hll estimate for this hoplog only
 +   */
 +  private EnumMap<Meta, byte[]> buildMetaData(ICardinality localHLL) throws IOException {
 +    EnumMap<Meta, byte[]> map = new EnumMap<Hoplog.Meta, byte[]>(Meta.class);
 +    map.put(Meta.LOCAL_CARDINALITY_ESTIMATE_V2, localHLL.getBytes());
 +    return map;
 +  }
 +
 +  private void submitCompactionRequests() throws IOException {
 +    CompactionRequest req;
 +    
 +    // determine if a major compaction is needed and create a compaction request
 +    // with compaction manager
 +    if (store.getMajorCompaction()) {
 +      if (isMajorCompactionNeeded()) {
 +        req = new CompactionRequest(regionFolder, bucketId, getCompactor(), true);
 +        HDFSCompactionManager.getInstance(store).submitRequest(req);
 +      }
 +    }
 +    
 +    // submit a minor compaction task. It will be ignored if there is no work to
 +    // be done.
 +    if (store.getMinorCompaction()) {
 +      req = new CompactionRequest(regionFolder, bucketId, getCompactor(), false);
 +      HDFSCompactionManager.getInstance(store).submitRequest(req);
 +    }
 +  }
 +
 +  /**
 +   * @return true if the oldest hoplog was created 1 major compaction interval ago
 +   */
 +  private boolean isMajorCompactionNeeded() throws IOException {
 +    // major compaction interval in milliseconds
 +    
 +    long majorCInterval = ((long)store.getMajorCompactionInterval()) * 60 * 1000;
 +
 +    Hoplog oplog = hoplogReadersController.getOldestHoplog();
 +    if (oplog == null) {
 +      return false;
 +    }
 +    
 +    long oldestFileTime = oplog.getModificationTimeStamp();
 +    long now = System.currentTimeMillis();
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("{}Checking oldest hop " + oplog.getFileName()
 +          + " for majorCompactionInterval=" + majorCInterval
 +          + " + now=" + now, logPrefix);
 +    }
 +    if (oldestFileTime > 0l && oldestFileTime < (now - majorCInterval)) {
 +      return true;
 +    }
 +    return false;
 +  }
 +
 +  @Override
 +  public SortedHoplogPersistedEvent read(byte[] key) throws IOException {
 +    long startTime = stats.getRead().begin();
 +    String user = logger.isDebugEnabled() ? "Read" : null;
 +    
 +    // collect snapshot of hoplogs
 +    List<TrackedReference<Hoplog>> hoplogs = null;
 +    hoplogs = hoplogReadersController.getTrackedSortedOplogList(user);
 +    try {
 +      // search for the key in order starting with the youngest oplog
 +      for (TrackedReference<Hoplog> hoplog : hoplogs) {
 +        HoplogReader reader = hoplog.get().getReader();
 +        byte[] val = reader.read(key);
 +        if (val != null) {
 +          // value found in a younger hoplog. stop iteration
 +          SortedHoplogPersistedEvent eventObj = deserializeValue(val);
 +          stats.getRead().end(val.length, startTime);
 +          return eventObj;
 +        }
 +      }
 +    } catch (IllegalArgumentException e) {
 +      if (IOException.class.isAssignableFrom(e.getCause().getClass())) {
 +        throw handleIOError((IOException) e.getCause());
 +      } else {
 +        throw e;
 +      }
 +    } catch (IOException e) {
 +      throw handleIOError(e);
 +    } catch (HDFSIOException e) {
 +        throw handleIOError(e);
 +    } finally {
 +      hoplogReadersController.releaseHoplogs(hoplogs, user);
 +    }
 +    
 +    stats.getRead().end(0, startTime);
 +    return null;
 +  }
 +
 +  protected IOException handleIOError(IOException e) {
 +    // expose the error wrapped inside remote exception
 +    if (e instanceof RemoteException) {
 +      return ((RemoteException) e).unwrapRemoteException();
 +    } 
 +    
 +    checkForSafeError(e);
 +    
 +    // it is not a safe error. let the caller handle it
 +    return e;
 +  }
 +  
 +  protected HDFSIOException handleIOError(HDFSIOException e) {
 +    checkForSafeError(e);
 +    return e;
 +  }
 +
 +  protected void checkForSafeError(Exception e) {
 +    boolean safeError = ShutdownHookManager.get().isShutdownInProgress();
 +    if (safeError) {
 +      // IOException because of closed file system. This happens when member is
 +      // shutting down
 +      if (logger.isDebugEnabled())
 +        logger.debug("IO error caused by filesystem shutdown", e);
 +      throw new CacheClosedException("IO error caused by filesystem shutdown", e);
 +    } 
 +
 +    if(isClosed()) {
 +      //If the hoplog organizer is closed, throw an exception to indicate the 
 +      //caller should retry on the new primary.
 +      throw new PrimaryBucketException(e);
 +    }
 +  }
 +  
 +  protected IOException handleWriteHdfsIOError(HoplogWriter writer, Hoplog so, IOException e)
 +      throws IOException {
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("{}Handle write error:" + so, logPrefix);
 +    }
 +    
 +    closeWriter(writer);
 +    // add to the janitor queue
 +    tmpFiles.put(so, Boolean.TRUE);
 +
 +    return handleIOError(e);
 +  }
 +
 +  private void deleteTmpFile(HoplogWriter writer, Hoplog so) {
 +    closeWriter(writer);
 +    
 +    // delete the temporary hoplog
 +    try {
 +      if (so != null) {
 +        so.delete();
 +      }
 +    } catch (IOException e1) {
 +      logger.info(e1);
 +    }
 +  }
 +
 +  private void closeWriter(HoplogWriter writer) {
 +    if (writer != null) {
 +      // close writer before deleting it
 +      try {
 +        writer.close();
 +      } catch (Throwable e1) {
 +        // error to close hoplog will happen if no connections to datanode are
 +        // available. Try to delete the file on namenode
 +        if(!isClosed()) {
 +          logger.info(e1);
 +        }
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Closes hoplog and suppresses IO during reader close. Suppressing IO errors
 +   * when the organizer is closing or an hoplog becomes inactive lets the system
 +   * continue freeing other resources. It could potentially lead to socket
 +   * leaks though.
 +   */
 +  private void closeReaderAndSuppressError(Hoplog hoplog, boolean clearCache) {
 +    try {
 +      hoplog.close();
 +    } catch (IOException e) {
 +      // expose the error wrapped inside remote exception
 +      if (e instanceof RemoteException) {
 +        e = ((RemoteException) e).unwrapRemoteException();
 +      } 
 +      logger.info(e);
 +    }
 +  }
 +
 +  @Override
 +  public BucketIterator scan() throws IOException {
 +    String user = logger.isDebugEnabled() ? "Scan" : null;
 +    List<TrackedReference<Hoplog>> hoplogs = null;
 +    BucketIterator iter = null;
 +    try {
 +      hoplogs = hoplogReadersController.getTrackedSortedOplogList(user);
 +      iter = new BucketIterator(hoplogs);
 +      return iter;
 +    }  finally {
 +      // Normally the hoplogs will be released when the iterator is closed. The
 +      // hoplogs must be released only if creating the iterator has failed.
 +      if (iter == null) {
 +        hoplogReadersController.releaseHoplogs(hoplogs, user);
 +      }
 +    }
 +  }
 +
 +  @Override
 +  public BucketIterator scan(byte[] from, byte[] to) throws IOException {
 +    throw new NotImplementedException();
 +  }
 +
 +  @Override
 +  public BucketIterator scan(byte[] from, boolean fromInclusive, byte[] to, boolean toInclusive) throws IOException {
 +    throw new NotImplementedException();
 +  }
 +
 +  @Override
 +  public HoplogIterator<byte[], SortedHoplogPersistedEvent> scan(
 +      long startOffset, long length) throws IOException {
 +    throw new UnsupportedOperationException("Not supported for " + this.getClass().getSimpleName());
 +  }
 +
 +  @Override
 +  public void close() throws IOException {
 +    super.close();
 +    
 +    synchronized (changePrimarylockObject) {
 +      organizerClosed = true;
 +    }
 +    //Suspend compaction
 +    getCompactor().suspend();
 +    
 +    //Close the readers controller.
 +    hoplogReadersController.close();
 +    
 +    previousCleanupTimestamp.set(Long.MIN_VALUE);
 +    
 +  }
 +
 +  /**
 +   * This method call will happen on secondary node. The secondary node needs to update its data
 +   * structures
 +   */
 +  @Override
 +  public void hoplogCreated(String region, int bucketId, Hoplog... oplogs)
 +      throws IOException {
 +    for (Hoplog oplog : oplogs) {
 +      addSortedOplog(oplog, false, true);
 +    }
 +  }
 +
 +  @Override
 +  public long sizeEstimate() {
 +    return this.bucketSize.cardinality();
 +  }
 +
 +  private void addSortedOplog(Hoplog so, boolean notify, boolean addsToBucketSize)
 +  throws IOException {
 +    if (!hoplogReadersController.addSortedOplog(so)) {
 +      so.close();
 +      throw new InternalGemFireException("Failed to add " + so);
 +    }
 +
 +    String user = logger.isDebugEnabled() ? "Add" : null;
 +    if (addsToBucketSize) {
 +      TrackedReference<Hoplog> ref = null;
 +      try {
 +        ref = hoplogReadersController.trackHoplog(so, user);
 +        synchronized (bucketSize) {
 +          ICardinality localHLL = ref.get().getEntryCountEstimate();
 +          if (localHLL != null) {
 +            bucketSize = mergeHLL(bucketSize, localHLL);
 +          }
 +        }
 +      } finally {
 +        if (ref != null) {
 +          hoplogReadersController.releaseHoplog(ref, user);
 +        }
 +      }
 +    }
 +
 +    if (notify && listener != null) {
 +      listener.hoplogCreated(regionFolder, bucketId, so);
 +    }
 +  }
 +
 +  private void reEstimateBucketSize() throws IOException {
 +    ICardinality global = null;
 +    String user = logger.isDebugEnabled() ? "HLL" : null;
 +    List<TrackedReference<Hoplog>> hoplogs = null;
 +    try {
 +      hoplogs = hoplogReadersController.getTrackedSortedOplogList(user);
 +      global = new HyperLogLog(HLL_CONSTANT);
 +      for (TrackedReference<Hoplog> hop : hoplogs) {
 +        global = mergeHLL(global, hop.get().getEntryCountEstimate());
 +      }
 +    } finally {
 +      hoplogReadersController.releaseHoplogs(hoplogs, user);
 +    }
 +    bucketSize = global;
 +  }
 +
 +  protected ICardinality mergeHLL(ICardinality global, ICardinality local)
 +  /*throws IOException*/ {
 +    try {
 +      return global.merge(local);
 +    } catch (CardinalityMergeException e) {
 +      // uncomment this after the 1.4 release
 +      //throw new InternalGemFireException(e.getLocalizedMessage(), e);
 +      startCompactionOnStartup = true;
 +      return global;
 +    }
 +  }
 +
 +  private void removeSortedOplog(TrackedReference<Hoplog> so, boolean notify) throws IOException {
 +    hoplogReadersController.removeSortedOplog(so);
 +    
 +    // release lock before notifying listeners
 +    if (notify && listener != null) {
 +      listener.hoplogDeleted(regionFolder, bucketId, so.get());
 +    }
 +  }
 +  
 +  private void notifyCompactionListeners(boolean isMajor) {
 +    listener.compactionCompleted(regionFolder, bucketId, isMajor);
 +  }
 +  
 +  /**
 +   * This method call will happen on secondary node. The secondary node needs to update its data
 +   * structures
 +   * @throws IOException 
 +   */
 +  @Override
 +  public void hoplogDeleted(String region, int bucketId, Hoplog... oplogs) throws IOException {
 +    throw new NotImplementedException();
 +  }
 +
 +  @Override
 +  public synchronized Compactor getCompactor() {
 +    if (compacter == null) {
 +      compacter = new HoplogCompactor();
 +    }
 +    return compacter;
 +  }
 +
 +  @Override
 +  protected Hoplog getHoplog(Path hoplogPath) throws IOException {
 +    Hoplog so = new HFileSortedOplog(store, hoplogPath, store.getBlockCache(), stats, store.getStats());
 +    return so;
 +  }
 +
 +  /**
 +   * locks sorted oplogs collection, removes oplog and renames for deletion later
 +   * @throws IOException 
 +   */
 +  void markSortedOplogForDeletion(List<TrackedReference<Hoplog>> targets, boolean notify) throws IOException {
 +    for (int i = targets.size(); i > 0; i--) {
 +      TrackedReference<Hoplog> so = targets.get(i - 1);
 +      removeSortedOplog(so, true);
 +      if (!store.getFileSystem().exists(new Path(bucketPath, so.get().getFileName()))) {
 +        // the hoplog does not even exist on file system. Skip remaining steps
 +        continue;
 +      }
 +      addExpiryMarkerForAFile(so.get());
 +    }
 +  }
 +  
 +  /**
 +   * Deletes expired hoplogs and expiry markers from the file system. Calculates
 +   * a target timestamp based on cleanup interval. Then gets list of target
 +   * hoplogs. It also updates the disk usage state
 +   * 
 +   * @return number of files deleted
 +   */
 +   synchronized int initiateCleanup() throws IOException {
 +    int conf = store.getPurgeInterval();
 +    // minutes to milliseconds
 +    long intervalDurationMillis = conf * 60 * 1000;
 +    // Any expired hoplog with timestamp less than targetTS is a delete
 +    // candidate.
 +    long targetTS = System.currentTimeMillis() - intervalDurationMillis;
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("Target timestamp for expired hoplog deletion " + targetTS, logPrefix);
 +    }
 +    // avoid too frequent cleanup invocations. Exit cleanup invocation if the
 +    // previous cleanup was executed within 10% range of cleanup interval
 +    if (previousCleanupTimestamp.get() > targetTS
 +        && (previousCleanupTimestamp.get() - targetTS) < (intervalDurationMillis / 10)) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("Skip cleanup, previous " + previousCleanupTimestamp.get(), logPrefix);
 +      }
 +      return 0;
 +    }
 +
 +    List<FileStatus> targets = getOptimizationTargets(targetTS);
 +    return deleteExpiredFiles(targets);
 +  }
 +
 +  protected int deleteExpiredFiles(List<FileStatus> targets) throws IOException {
 +    if (targets == null) {
 +      return 0;
 +    }
 +
 +    for (FileStatus file : targets) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}Deleting file: " + file.getPath(), logPrefix);
 +      }
 +      store.getFileSystem().delete(file.getPath(), false);
 +      
 +      if (isClosed()) {
 +        if (logger.isDebugEnabled())
 +          logger.debug("{}Expiry file cleanup interupted by bucket close", logPrefix);
 +        return 0;
 +      }
 +      incrementDiskUsage(-1 * file.getLen());
 +    }
 +
 +    previousCleanupTimestamp.set(System.currentTimeMillis());
 +    return targets.size();
 +  }
 +
 +  /**
 +   * @param ts
 +   *          target timestamp
 +   * @return list of hoplogs, whose expiry markers were created before target
 +   *         timestamp, and the expiry marker itself.
 +   * @throws IOException
 +   */
 +  protected List<FileStatus> getOptimizationTargets(long ts) throws IOException {
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("{}Identifying optimization targets " + ts, logPrefix);
 +    }
 +
 +    List<FileStatus> deleteTargets = new ArrayList<FileStatus>();
 +    FileStatus[] markers = getExpiryMarkers();
 +    if (markers != null) {
 +      for (FileStatus marker : markers) {
 +        String name = truncateExpiryExtension(marker.getPath().getName());
 +        long timestamp = marker.getModificationTime();
 +
 +        // expired minor compacted files are not being used anywhere. These can
 +        // be removed immediately. All the other expired files should be removed
 +        // when the files have aged
 +        boolean isTarget = false;
 +        
 +        if (name.endsWith(MINOR_HOPLOG_EXTENSION)) {
 +          isTarget = true;
 +        } else if (timestamp < ts && name.endsWith(FLUSH_HOPLOG_EXTENSION)) {
 +          isTarget = true;
 +        } else if (timestamp < ts && name.endsWith(MAJOR_HOPLOG_EXTENSION)) {
 +          long majorCInterval = ((long)store.getMajorCompactionInterval()) * 60 * 1000;
 +          if (timestamp < (System.currentTimeMillis() - majorCInterval)) {
 +            isTarget = true;
 +          }
 +        }
 +        if (!isTarget) {
 +          continue;
 +        }
 +        
 +        // if the file is still being read, do not delete or rename it
 +        TrackedReference<Hoplog> used = hoplogReadersController.getInactiveHoplog(name);
 +        if (used != null) {
 +          if (used.inUse() && logger.isDebugEnabled()) {
 +            logger.debug("{}Optimizer: found active expired hoplog:" + name, logPrefix);
 +          } else if (logger.isDebugEnabled()) {
 +            logger.debug("{}Optimizer: found open expired hoplog:" + name, logPrefix);
 +          }
 +          continue;
 +        }
 +        
 +        if (logger.isDebugEnabled()) {
 +          logger.debug("{}Delete target identified " + marker.getPath(), logPrefix);
 +        }
 +        
 +        deleteTargets.add(marker);
 +        Path hoplogPath = new Path(bucketPath, name);
 +        if (store.getFileSystem().exists(hoplogPath)) {
 +          FileStatus hoplog = store.getFileSystem().getFileStatus(hoplogPath);
 +          deleteTargets.add(hoplog);
 +        }
 +      }
 +    }
 +    return deleteTargets;
 +  }
 +
 +  /**
 +   * Returns a list of of hoplogs present in the bucket's directory, expected to be called during
 +   * hoplog set initialization
 +   */
 +  List<Hoplog> identifyAndLoadSortedOplogs(boolean countSize) throws IOException {
 +    FileSystem fs = store.getFileSystem();
 +    if (! fs.exists(bucketPath)) {
 +      return new ArrayList<Hoplog>();
 +    }
 +    
 +    FileStatus allFiles[] = fs.listStatus(bucketPath);
 +    ArrayList<FileStatus> validFiles = new ArrayList<FileStatus>();
 +    for (FileStatus file : allFiles) {
 +      // All hoplog files contribute to disk usage
 +      Matcher matcher = HOPLOG_NAME_PATTERN.matcher(file.getPath().getName());
 +      if (! matcher.matches()) {
 +        // not a hoplog
 +        continue;
 +      }
 +      
 +      // account for the disk used by this file
 +      if (countSize) {
 +        incrementDiskUsage(file.getLen());
 +      }
 +      
 +      // All valid hoplog files must match the regex
 +      matcher = SORTED_HOPLOG_PATTERN.matcher(file.getPath().getName());
 +      if (matcher.matches()) {
 +        validFiles.add(file);
 +      }
 +    }
 +    
 +    FileStatus[] markers = getExpiryMarkers();
 +    FileStatus[] validHoplogs = filterValidHoplogs(
 +        validFiles.toArray(new FileStatus[validFiles.size()]), markers);
 +
 +    ArrayList<Hoplog> results = new ArrayList<Hoplog>();
 +    if (validHoplogs == null || validHoplogs.length == 0) {
 +      return results;
 +    }
 +
 +    for (int i = 0; i < validHoplogs.length; i++) {
 +      // Skip directories
 +      if (validHoplogs[i].isDirectory()) {
 +        continue;
 +      }
 +
 +      final Path p = validHoplogs[i].getPath();
 +      // skip empty file
 +      if (fs.getFileStatus(p).getLen() <= 0) {
 +        continue;
 +      }
 +
 +      Hoplog hoplog = new HFileSortedOplog(store, p, store.getBlockCache(), stats, store.getStats());
 +      results.add(hoplog);
 +    }
 +
 +    return results;
 +  }
 +
 +  private static int findMaxSequenceNumber(List<Hoplog> hoplogs) throws IOException {
 +    int maxSeq = 0;
 +    for (Hoplog hoplog : hoplogs) {
 +      maxSeq = Math.max(maxSeq, getSequenceNumber(hoplog));
 +    }
 +    return maxSeq;
 +  }
 +
 +  /**
 +   * @return the sequence number associate with a hoplog file
 +   */
 +  static int getSequenceNumber(Hoplog hoplog) {
 +    Matcher matcher = SORTED_HOPLOG_PATTERN.matcher(hoplog.getFileName());
 +    boolean matched = matcher.find();
 +    assert matched;
 +    return Integer.valueOf(matcher.group(3));
 +  }
 +
 +  protected FileStatus[] getExpiryMarkers() throws IOException {
 +    FileSystem fs = store.getFileSystem();
 +    if (hoplogReadersController.hoplogs == null
 +        || hoplogReadersController.hoplogs.size() == 0) {
 +      // there are no hoplogs in the system. May be the bucket is not existing
 +      // at all.
 +      if (!fs.exists(bucketPath)) {
 +        if (logger.isDebugEnabled())
 +          logger.debug("{}This bucket is unused, skipping expired hoplog check", logPrefix);
 +        return null;
 +      }
 +    }
 +    
 +    FileStatus files[] = FSUtils.listStatus(fs, bucketPath, new PathFilter() {
 +      @Override
 +      public boolean accept(Path file) {
 +        // All expired hoplog end with expire extension and must match the valid file regex
 +        String fileName = file.getName();
 +        if (! fileName.endsWith(EXPIRED_HOPLOG_EXTENSION)) {
 +          return false;
 +        }
 +        fileName = truncateExpiryExtension(fileName);
 +        Matcher matcher = SORTED_HOPLOG_PATTERN.matcher(fileName);
 +        return matcher.find();
 +      }
 +
 +    });
 +    return files;
 +  }
 +  
 +  @Override
 +  public void clear() throws IOException {
 +    //Suspend compaction while we are doing the clear. This
 +    //aborts the currently in progress compaction.
 +    getCompactor().suspend();
 +    
 +    // while compaction is suspended, clear method marks hoplogs for deletion
 +    // only. Files will be removed by cleanup thread after active gets and
 +    // iterations are completed
 +    String user = logger.isDebugEnabled() ? "clear" : null;
 +    List<TrackedReference<Hoplog>> oplogs = null;
 +    try {
 +      oplogs = hoplogReadersController.getTrackedSortedOplogList(user);
 +      markSortedOplogForDeletion(oplogs, true);
 +    } finally {
 +      if (oplogs != null) {
 +        hoplogReadersController.releaseHoplogs(oplogs, user);
 +      }
 +      //Resume compaction
 +      getCompactor().resume();
 +    }
 +  }
 +
 +  /**
 +   * Performs the following activities
 +   * <UL>
 +   * <LI>Submits compaction requests as needed
 +   * <LI>Deletes tmp files which the system failed to removed earlier
 +   */
 +  @Override
 +  public void performMaintenance() throws IOException {
 +    long startTime = System.currentTimeMillis();
 +    
 +    if (logger.isDebugEnabled())
 +      logger.debug("{}Executing bucket maintenance", logPrefix);
 +
 +    submitCompactionRequests();
 +    hoplogReadersController.closeInactiveHoplogs();
 +    initiateCleanup();
 +    
 +    cleanupTmpFiles();
 +    
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("{}Time spent in bucket maintenance (in ms): "
 +          + (System.currentTimeMillis() - startTime), logPrefix);
 +    }
 +  }
 +
 +  @Override
 +  public Future<CompactionStatus> forceCompaction(boolean isMajor) {
 +    CompactionRequest request = new CompactionRequest(regionFolder, bucketId,
 +        getCompactor(), isMajor, true/*force*/);
 +    return HDFSCompactionManager.getInstance(store).submitRequest(request);
 +  }
 +
 +  private Future<CompactionStatus> forceCompactionOnVersionUpgrade() {
 +    CompactionRequest request = new CompactionRequest(regionFolder, bucketId, getCompactor(), true, true, true);
 +    return HDFSCompactionManager.getInstance(store).submitRequest(request);
 +  }
 +
 +  @Override
 +  public long getLastMajorCompactionTimestamp() {
 +    long ts = 0;
 +    String user = logger.isDebugEnabled() ? "StoredProc" : null;
 +    List<TrackedReference<Hoplog>> hoplogs = hoplogReadersController.getTrackedSortedOplogList(user);
 +    try {
 +      for (TrackedReference<Hoplog> hoplog : hoplogs) {
 +        String fileName = hoplog.get().getFileName();
 +        Matcher file = HOPLOG_NAME_PATTERN.matcher(fileName);
 +        if (file.matches() && fileName.endsWith(MAJOR_HOPLOG_EXTENSION)) {
 +          ts = getHoplogTimestamp(file);
 +          break;
 +        }
 +      }
 +    } finally {
 +      hoplogReadersController.releaseHoplogs(hoplogs, user);
 +    }
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("{}HDFS: for bucket:"+getRegionBucketStr()+" returning last major compaction timestamp "+ts, logPrefix);
 +    }
 +    return ts;
 +  }
 +
 +  private void initOldTmpFiles() throws IOException {
 +    FileSystem fs = store.getFileSystem();
 +    if (! fs.exists(bucketPath)) {
 +      return;
 +    }
 +    
 +    oldTmpFiles = new LinkedList<FileStatus>(Arrays.asList(fs.listStatus(bucketPath, new TmpFilePathFilter())));
 +  }
 +  
 +  private void cleanupTmpFiles() throws IOException {
 +    if(oldTmpFiles == null && tmpFiles == null) {
 +      return;
 +    }
 +    
 +    if (oldTmpFiles != null) {
 +      FileSystem fs = store.getFileSystem();
 +      long now = System.currentTimeMillis();
 +      for (Iterator<FileStatus> itr = oldTmpFiles.iterator(); itr.hasNext();) {
 +        FileStatus file = itr.next();
 +        if(file.getModificationTime() + TMP_FILE_EXPIRATION_TIME_MS > now) {
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("{}Deleting temporary file:" + file.getPath(), logPrefix);
 +          }
 +          fs.delete(file.getPath(), false);
 +          itr.remove();
 +        }
 +      }
 +    }
 +    if (tmpFiles != null) {
 +      for (Hoplog so : tmpFiles.keySet()) {
 +        if (logger.isDebugEnabled()) {
 +          logger.debug("{}Deleting temporary file:" + so.getFileName(), logPrefix);
 +        }
 +        deleteTmpFile(null, so);
 +      }
 +    }
 +  }
 +  
 +  /**
 +   * Executes tiered compaction of hoplog files. One instance of compacter per bucket will exist
 +   */
 +  protected class HoplogCompactor implements Compactor {
 +    private volatile boolean suspend = false;
 +    
 +    // the following boolean will be used to synchronize minor compaction
 +    private AtomicBoolean isMinorCompactionActive = new AtomicBoolean(false);
 +    // the following boolean will be used to synchronize major compaction
 +    private AtomicBoolean isMajorCompactionActive = new AtomicBoolean(false);
 +    // the following integer tracks the max sequence number amongst the
 +    // target files being major compacted. This value will be used to prevent
 +    // concurrent MajorC and minorC. MinorC is preempted in case of an
 +    // overlap. This object is also used as a lock. The lock is acquired before
 +    // identifying compaction targets and before marking targets for expiry
 +    final AtomicInteger maxMajorCSeqNum = new AtomicInteger(-1);
 +
 +    @Override
 +    public void suspend() {
 +      long wait = Long.getLong(HoplogConfig.SUSPEND_MAX_WAIT_MS, HoplogConfig.SUSPEND_MAX_WAIT_MS_DEFAULT);
 +      this.suspend=true;
 +      //this forces the compact method to finish.
 +      while (isMajorCompactionActive.get() || isMinorCompactionActive.get()) {
 +        if (wait < 0) {
 +          wait = Long.getLong(HoplogConfig.SUSPEND_MAX_WAIT_MS, HoplogConfig.SUSPEND_MAX_WAIT_MS_DEFAULT);
 +          String act = isMajorCompactionActive.get() ? "MajorC" : "MinorC";
 +          logger.warn(LocalizedMessage.create(LocalizedStrings.HOPLOG_SUSPEND_OF_0_FAILED_IN_1, new Object[] {act, wait}));
 +          break;
 +        }
 +        try {
 +          TimeUnit.MILLISECONDS.sleep(50);
 +          wait -= 50;
 +        } catch (InterruptedException e) {
 +          break;
 +        }
 +      }
 +    }
 +    
 +    @Override
 +    public void resume() {
 +      this.suspend = false;
 +    }
 +    
 +    @Override
 +    public boolean isBusy(boolean isMajor) {
 +      if (isMajor) {
 +        return isMajorCompactionActive.get();
 +      } else {
 +        return isMinorCompactionActive.get();
 +      }
 +    }
 +    
 +    /**
 +     * compacts hoplogs. The method takes a minor or major compaction "lock" to
 +     * prevent concurrent execution of compaction cycles. A possible improvement
 +     * is to allow parallel execution of minor compaction if the sets of
 +     * hoplogs being compacted are disjoint.
 +     */
 +    @Override
 +    public boolean compact(boolean isMajor, boolean isForced) throws IOException {
 +      if(suspend) {
 +        return false;
 +      }
 +
 +      String extension = null;
 +      IOOperation compactionStats = null;
 +      long startTime = 0; 
 +      final AtomicBoolean lock;
 +      Hoplog compactedHoplog = null;
 +      List<TrackedReference<Hoplog>> targets = null;
 +      String user = logger.isDebugEnabled() ? (isMajor ? "MajorC" : "MinorC") : null;
 +      
 +      if (isMajor) {
 +        lock = isMajorCompactionActive;
 +        extension = MAJOR_HOPLOG_EXTENSION;
 +        compactionStats = stats.getMajorCompaction();
 +      } else {
 +        lock = isMinorCompactionActive;
 +        extension = MINOR_HOPLOG_EXTENSION;
 +        compactionStats = stats.getMinorCompaction();
 +      }
 +
 +      // final check before beginning compaction. Return if compaction is active
 +      if (! lock.compareAndSet(false, true)) {
 +        if (isMajor) {
 +          if (logger.isDebugEnabled())
 +            logger.debug("{}Major compaction already active. Ignoring new request", logPrefix);
 +        } else {
 +          if (logger.isDebugEnabled())
 +            logger.debug("Minor compaction already active. Ignoring new request", logPrefix);
 +        }
 +        return false;
 +      }
 +      
 +      try {
 +        if(suspend) {
 +          return false;
 +        }
 +        
 +        // variables for updating stats
 +        startTime = compactionStats.begin();
 +        
 +        int seqNum = -1;
 +        int lastKnownMajorCSeqNum;
 +        synchronized (maxMajorCSeqNum) {
 +          lastKnownMajorCSeqNum = maxMajorCSeqNum.get();
 +          targets = hoplogReadersController.getTrackedSortedOplogList(user);
 +          getCompactionTargets(isMajor, targets, lastKnownMajorCSeqNum);
 +          if (targets != null && targets.size() > 0) {
 +            targets = Collections.unmodifiableList(targets);
 +            seqNum = getSequenceNumber(targets.get(0).get());
 +            if (isMajor) {
 +              maxMajorCSeqNum.set(seqNum);
 +            }
 +          }
 +        }
 +        
 +        if (targets == null || targets.isEmpty() || (!isMajor && targets.size() == 1 && !isForced)) {
 +          if (logger.isDebugEnabled()){
 +            logger.debug("{}Skipping compaction, too few hoplops to compact. Major?" + isMajor, logPrefix);
 +          }
 +            
 +          compactionStats.end(0, startTime);
 +          return true;
 +        }
 +        
 +        //In case that we only have one major compacted file, we don't need to run major compaction to
 +        //generate a copy of the same content
 +        if (targets.size() == 1 && !isForced) {
 +        String hoplogName = targets.get(0).get().getFileName();
 +          if (hoplogName.endsWith(MAJOR_HOPLOG_EXTENSION)){
 +            if (logger.isDebugEnabled()){
 +              logger.debug("{}Skipping compaction, no need to compact a major compacted file. Major?" + isMajor, logPrefix);
 +            }
 +            compactionStats.end(0, startTime);
 +            return true;
 +          }
 +        }
 +        
 +        if (logger.isDebugEnabled()) {
 +          for (TrackedReference<Hoplog> target : targets) {
 +            if (logger.isDebugEnabled()) {
 +              fineLog("Target:", target, " size:", target.get().getSize());
 +            }
 +          }
 +        }
 +        
 +        // Create a temporary hoplog for compacted hoplog. The compacted hoplog
 +        // will have the seq number same as that of youngest target file. Any
 +        // hoplog younger than target hoplogs will have a higher sequence number
 +        compactedHoplog = getTmpSortedOplog(seqNum, extension);
 +        
 +        long byteCount;
 +        try {
 +          byteCount = fillCompactionHoplog(isMajor, targets, compactedHoplog, lastKnownMajorCSeqNum);
 +          compactionStats.end(byteCount, startTime);
 +        } catch (InterruptedException e) {
 +          if (logger.isDebugEnabled())
 +            logger.debug("{}Compaction execution suspended", logPrefix);
 +          compactionStats.error(startTime);
 +          return false;
 +        } catch (ForceReattemptException e) {
 +          if (logger.isDebugEnabled())
 +            logger.debug("{}Compaction execution suspended", logPrefix);
 +          compactionStats.error(startTime);
 +          return false;
 +        }
 +        
 +        // creation of compacted hoplog completed, its time to use it for
 +        // reading. Before using it, make sure minorC and mojorC were not
 +        // executing on overlapping sets of files. All targets can be marked for
 +        // expiration. Notify listener if configured. Update bucket size
 +        synchronized (maxMajorCSeqNum) {
 +          if (!isMajor && isMinorMajorOverlap(targets, maxMajorCSeqNum.get())) {
 +            // MajorC is higher priority. In case of any overlap kill minorC
 +            if (logger.isDebugEnabled())
 +              logger.debug("{}Interrupting MinorC for a concurrent MajorC", logPrefix);
 +            compactionStats.error(startTime);
 +            return false;
 +          }
 +          addSortedOplog(compactedHoplog, true, false);
 +          markSortedOplogForDeletion(targets, true);
 +        }
 +      } catch (IOException e) {
 +        compactionStats.error(startTime);
 +        throw e;
 +      } finally {
 +        if (isMajor) {
 +          maxMajorCSeqNum.set(-1);
 +        }
 +        lock.set(false);
 +        hoplogReadersController.releaseHoplogs(targets, user);
 +      }
 +      
 +      incrementDiskUsage(compactedHoplog.getSize());
 +      reEstimateBucketSize();
 +      
 +      notifyCompactionListeners(isMajor);
 +      return true;
 +    }
 +
 +    /**
 +     * Major compaction compacts all files. Seq number of the youngest file
 +     * being MajorCed is known. If MinorC is operating on any file with a seq
 +     * number less than this number, there is a overlap
 +     * @param num 
 +     */
 +    boolean isMinorMajorOverlap(List<TrackedReference<Hoplog>> targets, int num) {
 +      if (num < 0 || targets == null || targets.isEmpty()) {
 +        return false;
 +      }
 +
 +      for (TrackedReference<Hoplog> hop : targets) {
 +        if (getSequenceNumber(hop.get()) <= num) {
 +          return true;
 +        }
 +      }
 +      
 +      return false;
 +    }
 +
 +    /**
 +     * Iterates over targets and writes eligible targets to the output hoplog.
 +     * Handles creation of iterators and writer and closing it in case of
 +     * errors.
 +     */
 +    public long fillCompactionHoplog(boolean isMajor,
 +        List<TrackedReference<Hoplog>> targets, Hoplog output, int majorCSeqNum)
 +        throws IOException, InterruptedException, ForceReattemptException {
 +
 +      HoplogWriter writer = null;
 +      ICardinality localHLL = new HyperLogLog(HLL_CONSTANT);
 +      HoplogSetIterator mergedIter = null;
 +      int byteCount = 0;
 +      
 +      try {
 +        // create a merged iterator over the targets and write entries into
 +        // output hoplog
 +        mergedIter = new HoplogSetIterator(targets);
 +        writer = output.createWriter(mergedIter.getRemainingEntryCount());
 +
 +        boolean interrupted = false;
 +        for (; mergedIter.hasNext(); ) {
 +          if (suspend) {
 +            interrupted = true;
 +            break;
 +          } else if (!isMajor &&  maxMajorCSeqNum.get() > majorCSeqNum) {
 +            // A new major compaction cycle is starting, quit minorC to avoid
 +            // duplicate work and missing deletes
 +            if (logger.isDebugEnabled())
 +              logger.debug("{}Preempting MinorC, new MajorC cycle detected ", logPrefix);
 +            interrupted = true;
 +            break;
 +          }
 +
 +          mergedIter.nextBB();
 +          
 +          ByteBuffer k = mergedIter.getKeyBB();
 +          ByteBuffer v = mergedIter.getValueBB();
 +          
 +          boolean isDeletedEntry = isDeletedEntry(v.array(), v.arrayOffset());
 +          if (isMajor && isDeletedEntry) {
 +            // its major compaction, time to ignore deleted entries
 +            continue;
 +          }
 +
 +          if (!isDeletedEntry) {
 +            int hash = MurmurHash.hash(k.array(), k.arrayOffset(), k.remaining(), -1);
 +            localHLL.offerHashed(hash);
 +          }
 +
 +          writer.append(k, v);
 +          byteCount += (k.remaining() + v.remaining());
 +        }
 +
 +        mergedIter.close();
 +        mergedIter = null;
 +
 +        writer.close(buildMetaData(localHLL));
 +        writer = null;
 +
 +        if (interrupted) {
 +          // If we suspended compaction operations, delete the partially written
 +          // file and return.
 +          output.delete();
 +          throw new InterruptedException();
 +        }
 +        
 +        // ping secondaries before making the file a legitimate file to ensure 
 +        // that in case of split brain, no other vm has taken up as primary. #50110. 
 +        pingSecondaries();
 +        
 +        makeLegitimate(output);
 +        return byteCount;
 +      } catch (IOException e) {
 +        e = handleWriteHdfsIOError(writer, output, e);
 +        writer = null;
 +        throw e;
 +      } catch (ForceReattemptException e) {
 +        output.delete();
 +        throw e;
 +      }finally {
 +        if (mergedIter != null) {
 +          mergedIter.close();
 +        }
 +
 +        if (writer != null) {
 +          writer.close();
 +        }
 +      }
 +    }
 +
 +    /**
 +     * identifies targets. For major compaction all sorted oplogs will be
 +     * iterated on. For minor compaction, policy driven fewer targets will take
 +     * place.
 +     */
 +    protected void getCompactionTargets(boolean major,
 +        List<TrackedReference<Hoplog>> targets, int majorCSeqNum) {
 +      if (!major) {
 +        getMinorCompactionTargets(targets, majorCSeqNum);
 +      }
 +    }
 +
 +    /**
 +     * list of oplogs most suitable for compaction. The alogrithm selects m
 +     * smallest oplogs which are not bigger than X in size. Null if valid
 +     * candidates are not found
 +     */
 +    void getMinorCompactionTargets(List<TrackedReference<Hoplog>> targets, int majorCSeqNum) 
 +    {
 +      List<TrackedReference<Hoplog>> omittedHoplogs = new ArrayList<TrackedReference<Hoplog>>();
 +
 +      // reverse the order of hoplogs in list. the oldest file becomes the first file.
 +      Collections.reverse(targets);
 +
 +      // hoplog greater than this size will not be minor-compacted
 +      final long MAX_COMPACTION_FILE_SIZE;
 +      // maximum number of files to be included in any compaction cycle
 +      final int MAX_FILE_COUNT_COMPACTION;
 +      // minimum number of files that must be present for compaction to be worth
 +      final int MIN_FILE_COUNT_COMPACTION;
 +      
 +      MAX_COMPACTION_FILE_SIZE = ((long)store.getInputFileSizeMax()) * 1024 *1024;
 +      MAX_FILE_COUNT_COMPACTION = store.getInputFileCountMax();
 +      MIN_FILE_COUNT_COMPACTION = store.getInputFileCountMin();
 +
 +      try {
 +        // skip till first file smaller than the max compaction file size is
 +        // found. And if MajorC is active, move to a file which is also outside
 +        // scope of MajorC
 +        for (Iterator<TrackedReference<Hoplog>> iterator = targets.iterator(); iterator.hasNext();) {
 +          TrackedReference<Hoplog> oplog = iterator.next();
 +          if (majorCSeqNum >= getSequenceNumber(oplog.get())) {
 +            iterator.remove();
 +            omittedHoplogs.add(oplog);
 +            if (logger.isDebugEnabled()){
 +              fineLog("Overlap with MajorC, excluding hoplog " + oplog.get());
 +            }
 +            continue;
 +          }
 +          
 +          if (oplog.get().getSize() > MAX_COMPACTION_FILE_SIZE || oplog.get().getFileName().endsWith(MAJOR_HOPLOG_EXTENSION)) {
 +          // big file will not be included for minor compaction
 +          // major compacted file will not be converted to minor compacted file
 +            iterator.remove();
 +            omittedHoplogs.add(oplog);
 +            if (logger.isDebugEnabled()) {
 +              fineLog("Excluding big hoplog from minor cycle:",
 +                  oplog.get(), " size:", oplog.get().getSize(), " limit:",
 +                  MAX_COMPACTION_FILE_SIZE);
 +            }
 +          } else {
 +            // first small hoplog found, skip the loop
 +            break;
 +          }
 +        }
 +
 +        // If there are too few files no need to perform compaction
 +        if (targets.size() < MIN_FILE_COUNT_COMPACTION) {
 +          if (logger.isDebugEnabled()){
 +            logger.debug("{}Too few hoplogs for minor cycle:" + targets.size(), logPrefix);
 +          }
 +          omittedHoplogs.addAll(targets);
 +          targets.clear();
 +          return;
 +        }
 +        
 +        float maxGain = Float.MIN_VALUE;
 +        int bestFrom = -1; 
 +        int bestTo = -1; 
 +        
 +        // for listSize=5 list, minFile=3; maxIndex=5-3. 
 +        // so from takes values 0,1,2
 +        int maxIndexForFrom = targets.size() - MIN_FILE_COUNT_COMPACTION;
 +        for (int from = 0; from <= maxIndexForFrom ; from++) {
 +          // for listSize=6 list, minFile=3, maxFile=5; minTo=0+3-1, maxTo=0+5-1
 +          // so to takes values 2,3,4
 +          int minIndexForTo = from + MIN_FILE_COUNT_COMPACTION - 1;
 +          int maxIndexForTo = Math.min(from + MAX_FILE_COUNT_COMPACTION, targets.size());
 +          
 +          for (int i = minIndexForTo; i < maxIndexForTo; i++) {
 +            Float gain = computeGain(from, i, targets);
 +            if (gain == null) {
 +              continue;
 +            }
 +            
 +            if (gain > maxGain) {
 +              maxGain = gain;
 +              bestFrom = from;
 +              bestTo = i;
 +            }
 +          }
 +        }
 +        
 +        if (bestFrom == -1) {
 +          if (logger.isDebugEnabled())
 +            logger.debug("{}Failed to find optimal target set for MinorC", logPrefix);
 +          omittedHoplogs.addAll(targets);
 +          targets.clear();
 +          return;
 +        }
 +
 +        if (logger.isDebugEnabled()) {
 +          fineLog("MinorCTarget optimal result from:", bestFrom, " to:", bestTo);
 +        }
 +
 +        // remove hoplogs they do not fall in the bestFrom-bestTo range
 +        int i = 0;
 +        for (Iterator<TrackedReference<Hoplog>> iter = targets.iterator(); iter.hasNext();) {
 +          TrackedReference<Hoplog> hop = iter.next();
 +          if (i < bestFrom || i > bestTo) {
 +            iter.remove();
 +            omittedHoplogs.add(hop);
 +          }
 +          i++;
 +        }
 +      } finally {
 +        // release readers of targets not included in the compaction cycle 
 +        String user = logger.isDebugEnabled() ? "MinorC" : null;
 +        hoplogReadersController.releaseHoplogs(omittedHoplogs, user);
 +      }
 +      
 +      // restore the order, youngest file is the first file again
 +      Collections.reverse(targets);
 +    }
 +
 +    @Override
 +    public HDFSStore getHdfsStore() {
 +      return store;
 +    }
 +  }
 +  
 +  Float computeGain(int from, int to, List<TrackedReference<Hoplog>> targets) {
 +    double SIZE_64K = 64.0 * 1024;
 +    // TODO the base for log should depend on the average number of keys a index block will contain
 +    double LOG_BASE = Math.log(AVG_NUM_KEYS_PER_INDEX_BLOCK);
 +    
 +    long totalSize = 0;
 +    double costBefore = 0f;
 +    for (int i = from; i <= to; i++) {
 +      long size = targets.get(i).get().getSize();
 +      if (size == 0) {
 +        continue;
 +      }
 +      totalSize += size;
 +      
 +      // For each hoplog file, read cost is number of index block reads and 1
 +      // data block read. Index blocks on an average contain N keys and are
 +      // organized in a N-ary tree structure. Hence the number of index block
 +      // reads will be logBaseN(number of data blocks)
 +      costBefore += Math.ceil(Math.max(1.0, Math.log(size / SIZE_64K) / LOG_BASE)) + 1;
 +    }
 +    
 +    // if the first file is relatively too large this set is bad for compaction
 +    long firstFileSize = targets.get(from).get().getSize();
 +    if (firstFileSize > (totalSize - firstFileSize) * RATIO) {
 +      if (logger.isDebugEnabled()){
 +        fineLog("First file too big:", firstFileSize, " totalSize:", totalSize);
 +      }
 +      return null;
 +    }
 +        
 +    // compute size in mb so that the value of gain is in few decimals
 +    long totalSizeInMb = totalSize / 1024 / 1024;
 +    if (totalSizeInMb == 0) {
 +      // the files are tooooo small, just return the count. The more we compact
 +      // the better it is
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}Total size too small:" +totalSize, logPrefix);
 +      }
 +      return (float) costBefore;
 +    }
 +    
 +    double costAfter = Math.ceil(Math.log(totalSize / SIZE_64K) / LOG_BASE) + 1;
 +    return (float) ((costBefore - costAfter) / totalSizeInMb);
 +  }
 +  
 +  /**
 +   * Hoplog readers are accessed asynchronously. There could be a window in
 +   * which, while a hoplog is being iterated on, it gets compacted and becomes
 +   * expired or inactive. The reader of the hoplog must not be closed till the
 +   * iterator completes. All such scenarios will be managed by this class. It
 +   * will keep all the reader, active and inactive, and reference counter to the
 +   * readers. An inactive reader will be closed if the reference count goes down
 +   * to 0.
 +   * 
 +   * One important point, only compaction process makes a hoplog inactive.
 +   * Compaction process in a bucket is single threaded. So compaction itself
 +   * will not face race condition. Read and scan operations on the bucket will
 +   * be affected. So reference counter is incremented for each read and scan.
 +   * 
 +   * @author ashvina
 +   */
 +  private class HoplogReadersController implements HoplogReaderActivityListener {
 +    private Integer maxOpenFilesLimit;
 +
 +    // sorted collection of all the active oplog files associated with this bucket. Instead of a
 +    // queue, a set is used. New files created as part of compaction may be inserted after a few
 +    // hoplogs were created. The compacted file is such a case but should not be treated newest.
 +    private final ConcurrentSkipListSet<TrackedReference<Hoplog>> hoplogs;
 +    
 +    // list of all the hoplogs that have been compacted and need to be closed
 +    // once the reference count reduces to 0
 +    private final ConcurrentHashSet<TrackedReference<Hoplog>> inactiveHoplogs;
 +    
 +    // ReadWriteLock on list of oplogs to allow for consistent reads and scans
 +    // while hoplog set changes. A write lock is needed on completion of
 +    // compaction, addition of a new hoplog or on receiving updates message from
 +    // other GF nodes
 +    private final ReadWriteLock hoplogRWLock = new ReentrantReadWriteLock(true);
 +
 +    // tracks the number of active readers for hoplogs of this bucket
 +    private AtomicInteger activeReaderCount = new AtomicInteger(0);
 +    
 +    public HoplogReadersController() {
 +      HoplogComparator comp = new HoplogComparator();
 +      hoplogs = new ConcurrentSkipListSet<TrackedReference<Hoplog>>(comp) {
 +        private static final long serialVersionUID = 1L;
 +
 +        @Override
 +        public boolean add(TrackedReference<Hoplog> e) {
 +          // increment number of hoplogs active for this bucket
 +          boolean result =  super.add(e);
 +          if (result)
 +            stats.incActiveFiles(1);
 +          return result;
 +        }
 +        
 +        @Override
 +        public boolean remove(Object o) {
 +          // decrement the number of hoplogs active for this bucket
 +          boolean result =  super.remove(o);
 +          if (result)
 +            stats.incActiveFiles(-1);
 +          return result;
 +        }
 +      };
 +      
 +      inactiveHoplogs = new ConcurrentHashSet<TrackedReference<Hoplog>>() {
 +        private static final long serialVersionUID = 1L;
 +        
 +        @Override
 +        public boolean add(TrackedReference<Hoplog> e) {
 +          boolean result =  super.add(e);
 +          if (result)
 +            stats.incInactiveFiles(1);
 +          return result;
 +        }
 +        
 +        @Override
 +        public boolean remove(Object o) {
 +          boolean result =  super.remove(o);
 +          if (result)
 +            stats.incInactiveFiles(-1);
 +          return result;
 +        }
 +      };
 +      
 +      maxOpenFilesLimit = Integer.getInteger(
 +          HoplogConfig.BUCKET_MAX_OPEN_HFILES_CONF,
 +          HoplogConfig.BUCKET_MAX_OPEN_HFILES_DEFAULT);
 +    }
 +    
 +    Hoplog getOldestHoplog() {
 +      if (hoplogs.isEmpty()) {
 +        return null;
 +      }
 +      return hoplogs.last().get();
 +    }
 +
 +    /**
 +     * locks sorted oplogs collection and performs add operation
 +     * @return if addition was successful
 +     */
 +    private boolean addSortedOplog(Hoplog so) throws IOException {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}Try add " + so, logPrefix);
 +      }
 +      hoplogRWLock.writeLock().lock();
 +      try {
 +        int size = hoplogs.size();
 +        boolean result = hoplogs.add(new TrackedReference<Hoplog>(so));
 +        so.setReaderActivityListener(this);
 +        if (logger.isDebugEnabled()){
 +          fineLog("Added: ", so, " Before:", size, " After:", hoplogs.size());
 +        }
 +        return result;
 +      } finally {
 +        hoplogRWLock.writeLock().unlock();
 +      }
 +    }
 +    
 +    /**
 +     * locks sorted oplogs collection and performs remove operation and updates readers also
 +     */
 +    private void removeSortedOplog(TrackedReference<Hoplog> so) throws IOException {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("Try remove " + so, logPrefix);
 +      }
 +      hoplogRWLock.writeLock().lock();
 +      try {
 +        int size = hoplogs.size();
 +        boolean result = hoplogs.remove(so);
 +        if (result) {
 +          inactiveHoplogs.add(so);
 +          if (logger.isDebugEnabled()) {
 +            fineLog("Removed: ", so, " Before:", size, " After:", hoplogs.size());
 +          }
 +        } else {
 +          if (inactiveHoplogs.contains(so)) {
 +            if (logger.isDebugEnabled()) {
 +              logger.debug("{}Found a missing active hoplog in inactive list." + so, logPrefix);
 +            }
 +          } else {
 +            so.get().close();
 +            logger.warn(LocalizedMessage.create(LocalizedStrings.HOPLOG_MISSING_IN_BUCKET_FORCED_CLOSED, so.get()));
 +          }
 +        }
 +      } finally {
 +        hoplogRWLock.writeLock().unlock();
 +      }
 +    }
 +    
 +    private  void closeInactiveHoplogs() throws IOException {
 +      hoplogRWLock.writeLock().lock();
 +      try {
 +        for (TrackedReference<Hoplog> hoplog : inactiveHoplogs) {
 +          if (logger.isDebugEnabled()){
 +            logger.debug("{}Try close inactive " + hoplog, logPrefix);
 +          }
 +
 +          if (!hoplog.inUse()) {
 +            int size = inactiveHoplogs.size();            
 +            inactiveHoplogs.remove(hoplog);
 +            closeReaderAndSuppressError(hoplog.get(), true);
 +            if (logger.isDebugEnabled()){
 +              fineLog("Closed inactive: ", hoplog.get(), " Before:", size,
 +                  " After:", inactiveHoplogs.size());
 +            }
 +          }
 +        }
 +      } finally {
 +        hoplogRWLock.writeLock().unlock();
 +      }
 +    }
 +    
 +    /**
 +     * @param target
 +     *          name of the hoplog file
 +     * @return trackedReference if target exists in inactive hoplog list.
 +     * @throws IOException
 +     */
 +    TrackedReference<Hoplog> getInactiveHoplog(String target) throws IOException {
 +      hoplogRWLock.writeLock().lock();
 +      try {
 +        for (TrackedReference<Hoplog> hoplog : inactiveHoplogs) {
 +          if (hoplog.get().getFileName().equals(target)) {
 +            if (logger.isDebugEnabled()) {
 +              logger.debug("{}Target found in inactive hoplogs list: " + hoplog, logPrefix);
 +            }
 +            return hoplog;
 +          }
 +        }
 +        if (logger.isDebugEnabled()){
 +          logger.debug("{}Target not found in inactive hoplogs list: " + target, logPrefix);
 +        }
 +        return null;
 +      } finally {
 +        hoplogRWLock.writeLock().unlock();
 +      }
 +    }
 +    
 +    /**
 +     * force closes all readers
 +     */
 +    public void close() throws IOException {
 +      hoplogRWLock.writeLock().lock();
 +      try {
 +        for (TrackedReference<Hoplog> hoplog : hoplogs) {
 +          closeReaderAndSuppressError(hoplog.get(), true);
 +        }
 +        
 +        for (TrackedReference<Hoplog> hoplog : inactiveHoplogs) {
 +          closeReaderAndSuppressError(hoplog.get(), true);
 +        }
 +      } finally {
 +        hoplogs.clear();
 +        inactiveHoplogs.clear();
 +        hoplogRWLock.writeLock().unlock();
 +      }
 +    }
 +    
 +    /**
 +     * locks hoplogs to create a snapshot of active hoplogs. reference of each
 +     * reader is incremented to keep it from getting closed
 +     * 
 +     * @return ordered list of sorted oplogs
 +     */
 +    private List<TrackedReference<Hoplog>> getTrackedSortedOplogList(String user) {
 +      List<TrackedReference<Hoplog>> oplogs = new ArrayList<TrackedReference<Hoplog>>();
 +      hoplogRWLock.readLock().lock();
 +      try {
 +        for (TrackedReference<Hoplog> oplog : hoplogs) {
 +          oplog.increment(user);
 +          oplogs.add(oplog);
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("{}Track ref " + oplog, logPrefix);
 +          }
 +        }
 +      } finally {
 +        hoplogRWLock.readLock().unlock();
 +      }
 +      return oplogs;
 +    }
 +
 +    private TrackedReference<Hoplog> trackHoplog(Hoplog hoplog, String user) {
 +      hoplogRWLock.readLock().lock();
 +      try {
 +        for (TrackedReference<Hoplog> oplog : hoplogs) {
 +          if (oplog.get().getFileName().equals(hoplog.getFileName())) {
 +            oplog.increment(user);
 +            if (logger.isDebugEnabled()) {
 +              logger.debug("{}Track " + oplog, logPrefix);
 +            }
 +            return oplog;
 +          }
 +        }
 +      } finally {
 +        hoplogRWLock.readLock().unlock();
 +      }
 +      throw new NoSuchElementException(hoplog.getFileName());
 +    }
 +    
 +    public void releaseHoplogs(List<TrackedReference<Hoplog>> targets, String user) {
 +      if (targets == null) {
 +        return;
 +      }
 +      
 +      for (int i = targets.size() - 1; i >= 0; i--) {
 +        TrackedReference<Hoplog> hoplog = targets.get(i);
 +        releaseHoplog(hoplog, user);
 +      }
 +    }
 +
 +    public void releaseHoplog(TrackedReference<Hoplog> target, String user) {
 +      if (target ==  null) {
 +        return;
 +      }
 +      
 +      target.decrement(user);
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}Try release " + target, logPrefix);
 +      }
 +      if (target.inUse()) {
 +        return;
 +      }
 +      
 +      // there are no users of this hoplog. if it is inactive close it.
 +      hoplogRWLock.writeLock().lock();
 +      try {
 +        if (!target.inUse()) {
 +          if (inactiveHoplogs.contains(target) ) {
 +            int sizeBefore = inactiveHoplogs.size();
 +            inactiveHoplogs.remove(target);
 +            closeReaderAndSuppressError(target.get(), true);
 +            if (logger.isDebugEnabled()) {
 +              fineLog("Closed inactive: ", target, " totalBefore:", sizeBefore,
 +                  " totalAfter:", inactiveHoplogs.size());
 +            }
 +          } else if (hoplogs.contains(target)) {
 +            closeExcessReaders();              
 +          }
 +        }
 +      } catch (IOException e) {
 +        logger.warn(LocalizedMessage.create(LocalizedStrings.HOPLOG_IO_ERROR, 
 +            "Close reader: " + target.get().getFileName()), e);
 +      } finally {
 +        hoplogRWLock.writeLock().unlock();
 +      }
 +    }
 +
 +    /*
 +     * detects if the total number of open hdfs readers is more than configured
 +     * max file limit. In case the limit is exceeded, some readers need to be
 +     * closed to avoid dadanode receiver overflow error.
 +     */
 +    private void closeExcessReaders() throws IOException {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{}Close excess readers. Size:" + hoplogs.size()
 +            + " activeReaders:" + activeReaderCount.get() + " limit:"
 +            + maxOpenFilesLimit, logPrefix);
 +      }
 +
 +      if (hoplogs.size() <= maxOpenFilesLimit) {
 +        return;
 +      }
 +      
 +      if (activeReaderCount.get() <= maxOpenFilesLimit) {
 +        return;
 +      }
 +      
 +      for (TrackedReference<Hoplog> hoplog : hoplogs.descendingSet()) {
 +        if (!hoplog.inUse() && !hoplog.get().isClosed()) {
 +          hoplog.get().close(false);
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("{}Excess reader closed " + hoplog, logPrefix);
 +          }
 +        }
 +        
 +        if (activeReaderCount.get() <= maxOpenFilesLimit) {
 +          return;
 +        }
 +      }
 +    }
 +
 +    @Override
 +    public void readerCreated() {
 +      activeReaderCount.incrementAndGet();
 +      stats.incActiveReaders(1);
 +      if (logger.isDebugEnabled())
 +        logger.debug("{}ActiveReader++", logPrefix);
 +    }
 +
 +    @Override
 +    public void readerClosed() {
 +      activeReaderCount.decrementAndGet(); 
 +      stats.incActiveReaders(-1);
 +      if (logger.isDebugEnabled())
 +        logger.debug("{}ActiveReader--", logPrefix);
 +    }
 +  }
 +
 +  /**
 +   * returns an ordered list of oplogs, FOR TESTING ONLY
 +   */
 +  public List<TrackedReference<Hoplog>> getSortedOplogs() throws IOException {
 +    List<TrackedReference<Hoplog>> oplogs = new ArrayList<TrackedReference<Hoplog>>();
 +    for (TrackedReference<Hoplog> oplog : hoplogReadersController.hoplogs) {
 +        oplogs.add(oplog);
 +    }
 +    return oplogs;
 +  }
 +
 +  /**
 +   * Merged iterator on a list of hoplogs. 
 +   */
 +  public class BucketIterator implements HoplogIterator<byte[], SortedHoplogPersistedEvent> {
 +    // list of hoplogs to be iterated on.
 +    final List<TrackedReference<Hoplog>> hoplogList;
 +    HoplogSetIterator mergedIter;
 +
 +    public BucketIterator(List<TrackedReference<Hoplog>> hoplogs) throws IOException {
 +      this.hoplogList = hoplogs;
 +      try {
 +        mergedIter = new HoplogSetIterator(this.hoplogList);
 +        if (logger.isDebugEnabled()) {
 +          for (TrackedReference<Hoplog> hoplog : hoplogs) {
 +            logger.debug("{}BucketIter target hop:" + hoplog.get().getFileName(), logPrefix);
 +          }
 +        }
 +      } catch (IllegalArgumentException e) {
 +        if (IOException.class.isAssignableFrom(e.getCause().getClass())) {
 +          throw handleIOError((IOException) e.getCause());
 +        } else {
 +          throw e;
 +        }
 +      } catch (IOException e) {
 +        throw handleIOError(e);
 +      } catch (HDFSIOException e) {
 +        throw handleIOError(e);
 +      } 
 +    }
 +
 +    @Override
 +    public boolean hasNext() {
 +      return mergedIter.hasNext();
 +    }
 +
 +    @Override
 +    public byte[] next() throws IOException {
 +      try {
 +        return HFileSortedOplog.byteBufferToArray(mergedIter.next());
 +      } catch (IllegalArgumentException e) {
 +        if (IOException.class.isAssignableFrom(e.getCause().getClass())) {
 +          throw handleIOError((IOException) e.getCause());
 +        } else {
 +          throw e;
 +        }
 +      } catch (IOException e) {
 +        throw handleIOError(e);
 +      }  
 +    }
 +
 +    @Override
 +    public byte[] getKey() {
 +      // merged iterator returns a byte[]. This needs to be deserialized to the object which was
 +      // provided during flush operation
 +      return HFileSortedOplog.byteBufferToArray(mergedIter.getKey());
 +    }
 +
 +    @Override
 +    public SortedHoplogPersistedEvent getValue() {
 +      // merged iterator returns a byte[]. This needs to be deserialized to the
 +      // object which was provided during flush operation
 +      try {
 +        return deserializeValue(HFileSortedOplog.byteBufferToArray(mergedIter.getValue()));
 +      } catch (IOException e) {
 +        throw new HDFSIOException("Failed to deserialize byte while iterating on partition", e);
 +      }
 +    }
 +
 +    @Override
 +    public void remove() {
 +      mergedIter.remove();
 +    }
 +
 +    @Override
 +    public void close() {
 +      // TODO release the closed iterators early
 +      String user = logger.isDebugEnabled() ? "Scan" : null;
 +      hoplogReadersController.releaseHoplogs(hoplogList, user);
 +    }
 +  }
 +  
 +  /**
 +   * This utility class is used to filter temporary hoplogs in a bucket
 +   * directory
 +   * 
 +   * @author ashvina
 +   */
 +  private static class TmpFilePathFilter implements PathFilter {
 +    @Override
 +    public boolean accept(Path path) {
 +      Matcher matcher = HOPLOG_NAME_PATTERN.matcher(path.getName());
 +      if (matcher.matches() && path.getName().endsWith(TEMP_HOPLOG_EXTENSION)) {
 +        return true;
 +      }
 +      return false;
 +    }
 +  }
 +
 +  private void fineLog(Object... strings) {
 +    if (logger.isDebugEnabled()) {
 +      StringBuffer sb = concatString(strings);
 +      logger.debug(logPrefix + sb.toString());
 +    }
 +  }
 +
 +  private StringBuffer concatString(Object... strings) {
 +    StringBuffer sb = new StringBuffer();
 +    for (Object str : strings) {
 +      sb.append(str.toString());
 +    }
 +    return sb;
 +  }
 +
 +  @Override
 +  public void compactionCompleted(String region, int bucket, boolean isMajor) {
 +    // do nothing for compaction events. Hoplog Organizer depends on addition
 +    // and deletion of hoplogs only
 +  }
 +}
 +

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/cache/hdfs/internal/hoplog/Hoplog.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/cache/hdfs/internal/hoplog/Hoplog.java
index 113e49b,0000000..a5030ae
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/cache/hdfs/internal/hoplog/Hoplog.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/hdfs/internal/hoplog/Hoplog.java
@@@ -1,263 -1,0 +1,264 @@@
 +/*
 + * 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.cache.hdfs.internal.hoplog;
 +
++import com.gemstone.gemfire.internal.hll.ICardinality;
++
 +import java.io.Closeable;
 +import java.io.IOException;
 +import java.nio.ByteBuffer;
 +import java.util.EnumMap;
 +
- import com.gemstone.gemfire.cache.hdfs.internal.cardinality.ICardinality;
 +
 +/**
 + * Ordered sequence file
 + */
 +public interface Hoplog extends Closeable, Comparable<Hoplog>  {
 +  public static final boolean NOP_WRITE = Boolean.getBoolean("Hoplog.NOP_WRITE");
 +  
 +  /** the gemfire magic number for sorted oplogs */
 +  public static final byte[] MAGIC = new byte[] { 0x47, 0x53, 0x4F, 0x50 };
 +
 +  /**
 +   * @return an instance of cached reader, creates one if does not exist
 +   * @throws IOException
 +   */
 +  HoplogReader getReader() throws IOException;
 +
 +  /**
 +   * Creates a new sorted writer.
 +   * 
 +   * @param keys
 +   *          an estimate of the number of keys to be written
 +   * @return the writer
 +   * @throws IOException
 +   *           error creating writer
 +   */
 +  HoplogWriter createWriter(int keys) throws IOException;
 +
 +  /**
 +   * @param listener listener of reader's activity
 +   */
 +  void setReaderActivityListener(HoplogReaderActivityListener listener);
 +  
 +  /**
 +   * @return file name
 +   */
 +  String getFileName();
 +
 +  /**
 +   * @return Entry count estimate for this hoplog
 +   */
 +  public ICardinality getEntryCountEstimate() throws IOException;
 +
 +  /**
 +   * renames the file to the input name
 +   * 
 +   * @throws IOException
 +   */
 +  void rename(String name) throws IOException;
 +
 +  /**
 +   * Deletes the sorted oplog file
 +   */
 +  void delete() throws IOException;
 +  
 +  /**
 +   * Returns true if the hoplog is closed for reads.
 +   * @return true if closed
 +   */
 +  boolean isClosed();
 +  
 +  /**
 +   * @param clearCache clear this sorted oplog's cache if true
 +   * @throws IOException 
 +   */
 +  void close(boolean clearCache) throws IOException;
 +  
 +  /**
 +   * @return the modification timestamp of the file
 +   */
 +  long getModificationTimeStamp();
 +  
 +  /**
 +   * @return the size of file
 +   */
 +  long getSize();
 +
 +  /**
 +   * Reads sorted oplog file.
 +   */
 +  public interface HoplogReader extends HoplogSetReader<byte[], byte[]> {
 +    /**
 +     * Returns a byte buffer based view of the value linked to the key
 +     */
 +    ByteBuffer get(byte[] key) throws IOException;
 +
 +    /**
 +     * @return Returns the bloom filter associated with this sorted oplog file.
 +     */
 +    BloomFilter getBloomFilter() throws IOException;
 +
 +    /**
 +     * @return number of KV pairs in the file, including tombstone entries
 +     */
 +    long getEntryCount();
 +
 +    /**
 +     * Returns the {@link ICardinality} implementation that is useful for
 +     * estimating the size of this Hoplog.
 +     * 
 +     * @return the cardinality estimator
 +     */
 +    ICardinality getCardinalityEstimator();
 +  }
 +
 +  /**
 +   * Provides hoplog's reader's activity related events to owners
 +   * 
 +   * @author ashvina
 +   */
 +  public interface HoplogReaderActivityListener {
 +    /**
 +     * Invoked when a reader is created and an active reader did not exist
 +     * earlier
 +     */
 +    public void readerCreated();
 +    
 +    /**
 +     * Invoked when an active reader is closed
 +     */
 +    public void readerClosed();
 +  }
 +
 +  /**
 +   * Writes key/value pairs in a sorted oplog file. Each entry that is appended must have a key that
 +   * is greater than or equal to the previous key.
 +   */
 +  public interface HoplogWriter extends Closeable {
 +    /**
 +     * Appends another key and value. The key is expected to be greater than or equal to the last
 +     * key that was appended.
 +     * @param key
 +     * @param value
 +     */
 +    void append(byte[] key, byte[] value) throws IOException;
 +
 +    /**
 +     * Appends another key and value. The key is expected to be greater than or equal to the last
 +     * key that was appended.
 +     */
 +    void append(ByteBuffer key, ByteBuffer value) throws IOException;
 +
 +    void close(EnumMap<Meta, byte[]> metadata) throws IOException;
 +    
 +    /**
 +     * flushes all outstanding data into the OS buffers on all DN replicas 
 +     * @throws IOException
 +     */
 +    void hsync() throws IOException;
 +    
 +    /**
 +     * Gets the size of the data that has already been written
 +     * to the writer.  
 +     * 
 +     * @return number of bytes already written to the writer
 +     */
 +    public long getCurrentSize() throws IOException; 
 +  }
 +
 +  /**
 +   * Identifies the gemfire sorted oplog versions.
 +   */
 +  public enum HoplogVersion {
 +    V1;
 +
 +    /**
 +     * Returns the version string as bytes.
 +     * 
 +     * @return the byte form
 +     */
 +    public byte[] toBytes() {
 +      return name().getBytes();
 +    }
 +
 +    /**
 +     * Constructs the version from a byte array.
 +     * 
 +     * @param version
 +     *          the byte form of the version
 +     * @return the version enum
 +     */
 +    public static HoplogVersion fromBytes(byte[] version) {
 +      return HoplogVersion.valueOf(new String(version));
 +    }
 +  }
 +
 +  /**
 +   * Names the available metadata keys that will be stored in the sorted oplog.
 +   */
 +  public enum Meta {
 +    /** identifies the soplog as a gemfire file, required */
 +    GEMFIRE_MAGIC,
 +
 +    /** identifies the soplog version, required */
 +    SORTED_OPLOG_VERSION,
 +    
 +    /** identifies the gemfire version the soplog was created with */
 +    GEMFIRE_VERSION,
 +
 +    /** identifies the statistics data */
 +    STATISTICS,
 +
 +    /** identifies the embedded comparator types */
 +    COMPARATORS,
 +    
 +    /** identifies the pdx type data, optional */
 +    PDX,
 +
 +    /**
 +     * identifies the hyperLogLog byte[] which estimates the cardinality for
 +     * only one hoplog
 +     */
 +    LOCAL_CARDINALITY_ESTIMATE,
 +
 +    /**
 +     * represents the hyperLogLog byte[] after upgrading the constant from
 +     * 0.1 to 0.03 (in gfxd 1.4)
 +     */
 +    LOCAL_CARDINALITY_ESTIMATE_V2
 +    ;
 +
 +    /**
 +     * Converts the metadata name to bytes.
 +     */
 +    public byte[] toBytes() {
 +      return ("gemfire." + name()).getBytes();
 +    }
 +
 +    /**
 +     * Converts the byte form of the name to an enum.
 +     * 
 +     * @param key
 +     *          the key as bytes
 +     * @return the enum form
 +     */
 +    public static Meta fromBytes(byte[] key) {
 +      return Meta.valueOf(new String(key).substring("gemfire.".length()));
 +    }
 +  }
 +}


[31/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/EventTracker.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/cache/EventTracker.java
index 83b00b4,0000000..3d6ce73
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/EventTracker.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/EventTracker.java
@@@ -1,811 -1,0 +1,812 @@@
 +/*
 + * 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;
 +
 +import java.io.DataInput;
 +import java.io.DataOutput;
 +import java.io.IOException;
 +import java.util.HashMap;
 +import java.util.Iterator;
 +import java.util.LinkedList;
 +import java.util.List;
 +import java.util.Map;
 +import java.util.concurrent.ConcurrentHashMap;
 +import java.util.concurrent.ConcurrentMap;
 +
 +import org.apache.logging.log4j.Logger;
 +
 +import com.gemstone.gemfire.DataSerializable;
 +import com.gemstone.gemfire.DataSerializer;
 +import com.gemstone.gemfire.cache.client.PoolFactory;
 +import com.gemstone.gemfire.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
 +import com.gemstone.gemfire.internal.Assert;
 +import com.gemstone.gemfire.internal.SystemTimer.SystemTimerTask;
 +import com.gemstone.gemfire.internal.cache.ha.ThreadIdentifier;
 +import com.gemstone.gemfire.internal.cache.versions.RegionVersionVector;
 +import com.gemstone.gemfire.internal.cache.versions.VersionTag;
 +import com.gemstone.gemfire.internal.logging.LogService;
 +import com.gemstone.gemfire.internal.logging.log4j.LogMarker;
 +import com.gemstone.gemfire.internal.util.concurrent.StoppableCountDownLatch;
 +
 +/**
 + * EventTracker tracks the last sequence number for a particular
 + * memberID:threadID.  It is used to avoid replaying events in
 + * client/server and partitioned-region configurations.
 + * 
 + * @author bruce
 + * @since 6.0
 + */
 +public class EventTracker
 +{
 +  private static final Logger logger = LogService.getLogger();
 +  
 +  /**
 +   * a mapping of originator to the last event applied to this cache 
 +   *
 +   * Keys are instances of {@link ThreadIdentifier}, values are instances
 +   * of {@link com.gemstone.gemfire.internal.cache.EventTracker.EventSeqnoHolder}.
 +   */
 +  protected final ConcurrentMap<ThreadIdentifier, EventSeqnoHolder> recordedEvents 
 +      = new ConcurrentHashMap<ThreadIdentifier, EventSeqnoHolder>(100);
 + 
 +  /** a mapping of originator to bulkOp's last status (true means
 +   * finished processing) applied to this cache. 
 +   *
 +   * Keys are instances of @link {@link ThreadIdentifier}, values are instances
 +   * of {@link BulkOpProcessed}.
 +   */
 +  private final ConcurrentMap<ThreadIdentifier, BulkOpProcessed> recordedBulkOps 
 +      = new ConcurrentHashMap<ThreadIdentifier, BulkOpProcessed>(100);
 +  
 +  /** a mapping of originator to bulkOperation's last version tags. This map
 +   * differs from {@link #recordedBulkOps} in that the thread identifier used
 +   * here is the base member id and thread id of the bulk op, as opposed to the fake
 +   * thread id which is assigned for each bucket.
 +   * 
 +   * recordedBulkOps are also only tracked on the secondary for partitioned regions
 +   * recordedBulkOpVersionTags are tracked on both the primary and secondary.
 +   *
 +   * Keys are instances of @link {@link ThreadIdentifier}, values are instances
 +   * of {@link BulkOpHolder}.
 +   */
 +  private final ConcurrentMap<ThreadIdentifier, BulkOpHolder> recordedBulkOpVersionTags 
 +      = new ConcurrentHashMap<ThreadIdentifier, BulkOpHolder>(100);
 +  
 +  /**
 +   * The member that the region corresponding to this tracker (if any)
 +   * received its initial image from (if a replicate)
 +   */
 +  private volatile InternalDistributedMember initialImageProvider;
 +
 + 
 +  /**
 +   * The cache associated with this tracker
 +   */
 +  GemFireCacheImpl cache;
 + 
 +  /**
 +   * The name of this tracker
 +   */
 +  String name;
 +  
 +  /**
 +   * whether or not this tracker has been initialized with state from
 +   * another process
 +   */
 +  volatile boolean initialized;
 + 
 +  /**
 +   * object used to wait for initialization 
 +   */
 +  final StoppableCountDownLatch initializationLatch;
 +  
 +  /**
 +  * Initialize the EventTracker's timer task.  This is stored in the cache
 +  * for tracking and shutdown purposes
 +  * @param cache the cache to schedule tasks with
 +  */
 +  public static ExpiryTask startTrackerServices(GemFireCacheImpl cache) {
 +    long expiryTime = Long.getLong("gemfire.messageTrackingTimeout",
 +        PoolFactory.DEFAULT_SUBSCRIPTION_MESSAGE_TRACKING_TIMEOUT / 3).longValue();
 +    ExpiryTask result = new ExpiryTask(cache, expiryTime);
 +    cache.getCCPTimer().scheduleAtFixedRate(result,
 +        expiryTime, expiryTime);
 +    //schedule(result, expiryTime);
 +    return result;
 +  }
 +  
 +  /**
 +   * Terminate the tracker's timer task
 +   * @param cache the cache holding the tracker task
 +   */
 +  public static void stopTrackerServices(GemFireCacheImpl cache) {
 +    cache.getEventTrackerTask().cancel();
 +  }
 + 
 + /**
 +  * Create an event tracker
 +  * @param region the cache region to associate with this tracker
 +  */
 +  public EventTracker(LocalRegion region) {
 +   this.cache = region.cache;
 +   this.name = "Event Tracker for " + region.getName();
 +   this.initializationLatch = new StoppableCountDownLatch(region.stopper, 1);
 + }
 +
 +  /** start this event tracker */
 +  public void start() {
 +    if (this.cache.getEventTrackerTask() != null) {
 +      this.cache.getEventTrackerTask().addTracker(this);
 +    }
 +  }
 +  
 +  /** stop this event tracker */
 +  public void stop() {
 +    if (this.cache.getEventTrackerTask() != null) {
 +      this.cache.getEventTrackerTask().removeTracker(this);
 +    }
 +  }
 +  
 +  /**
 +   * retrieve a deep copy of the state of the event tracker.  Synchronization
 +   * is not used while copying the tracker's state.
 +   */
 +  public Map<ThreadIdentifier, EventSeqnoHolder> getState() {
 +    Map<ThreadIdentifier, EventSeqnoHolder> result = new HashMap<ThreadIdentifier, EventSeqnoHolder>(recordedEvents.size());
 +    for (Iterator<Map.Entry<ThreadIdentifier, EventSeqnoHolder>> it=recordedEvents.entrySet().iterator(); it.hasNext(); ) {
 +      Map.Entry<ThreadIdentifier, EventSeqnoHolder> entry = it.next();
 +      EventSeqnoHolder holder = entry.getValue();
 +      result.put(entry.getKey(), new EventSeqnoHolder(
 +          holder.lastSeqno, null)); // don't transfer version tags - adds too much bulk just so we can do client tag recovery
 +    }
 +    return result;
 +  }
 +  
 +  /**
 +   * record the given state in the tracker.
 +   * @param provider the member that provided this state
 +   * @param state a Map obtained from getState();
 +   */
 +  public void recordState(InternalDistributedMember provider, Map<ThreadIdentifier, EventSeqnoHolder> state) {
 +    this.initialImageProvider = provider;
 +    StringBuffer sb = null;
 +    if (logger.isDebugEnabled()) {
 +      sb = new StringBuffer(200);
 +      sb.append("Recording initial state for ")
 +        .append(this.name)
 +        .append(": ");
 +    }
 +    for (Iterator<Map.Entry<ThreadIdentifier, EventSeqnoHolder>> it=state.entrySet().iterator(); it.hasNext(); ) {
 +      Map.Entry<ThreadIdentifier, EventSeqnoHolder> entry = it.next();
 +      if (sb != null) {
 +        sb.append("\n  ")
 +          .append(entry.getKey().expensiveToString())
 +          .append("; sequenceID=")
 +          .append(entry.getValue()); 
 +      }
 +      // record only if we haven't received an event that is newer
 +      recordSeqno(entry.getKey(), entry.getValue(), true);
 +    }
 +    if (sb != null) {
 +      logger.debug(sb);
 +    }
 +    // fix for bug 41622 - hang in GII.  This keeps ops from waiting for the
 +    // full GII to complete
 +    setInitialized();
 +  }
 +  
 +  /**
 +   * Use this method to ensure that the tracker is put in an initialized state
 +   */
 +  public void setInitialized() {
 +    this.initializationLatch.countDown();
 +    this.initialized = true;
 +  }
 +  
 +  /**
 +   * Wait for the tracker to finishe being initialized
 +   */
 +  public void waitOnInitialization() throws InterruptedException {
 +    this.initializationLatch.await();
 +  }
 +  
 +  /**
 +   * Record an event sequence id if it is higher than what we currently have.
 +   * This is intended for use during initial image transfer.
 +   * @param membershipID the key of an entry in the map obtained from getEventState()
 +   * @param evhObj the value of an entry in the map obtained from getEventState()
 +   */
 +  protected void recordSeqno(ThreadIdentifier membershipID, EventSeqnoHolder evhObj){
 +    recordSeqno(membershipID, evhObj, false);
 +  }
 +  
 +  /**
 +   * Record an event sequence id if it is higher than what we currently have.
 +   * This is intended for use during initial image transfer.
 +   * @param threadID the key of an entry in the map obtained from getEventState()
 +   * @param evh the value of an entry in the map obtained from getEventState()
 +   * @param ifAbsent only record this state if there's not already an entry for this memberID
 +   */
 +  private void recordSeqno(ThreadIdentifier threadID, EventSeqnoHolder evh, boolean ifAbsent) {
 +    boolean removed;
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("recording {} {}", threadID.expensiveToString(), evh.toString());
 +    }
 +    do {
 +      removed = false;
 +      EventSeqnoHolder oldEvh = recordedEvents.putIfAbsent(
 +          threadID, evh);
 +      if (oldEvh != null) {
 +        synchronized(oldEvh) {
 +          if (oldEvh.removed) {
 +            // need to wait for an entry being removed by the sweeper to go away
 +            removed = true;
 +            continue;
 +          }
 +          else {
 +            if (ifAbsent) {
 +              break;
 +            }
 +            oldEvh.endOfLifeTimer = 0;
 +            if (oldEvh.lastSeqno < evh.lastSeqno) {
 +              oldEvh.lastSeqno = evh.lastSeqno;
 +              oldEvh.versionTag = evh.versionTag;
 +//              Exception e = oldEvh.context;
 +//              oldEvh.context = new Exception("stack trace");
 +//              oldEvh.context.initCause(e);
 +            }
 +          }
 +        }
 +      }
 +      else {
 +        evh.endOfLifeTimer = 0;
 +//        evh.context = new Exception("stack trace");
 +      }
 +    } while (removed);
 +  }
 +  
 +  /** record the event's threadid/sequenceid to prevent replay */
 +  public void recordEvent(InternalCacheEvent event) {
 +    EventID eventID = event.getEventId();
 +    if (ignoreEvent(event, eventID)) {
 +      return; // not tracked
 +    }
 +
 +    LocalRegion lr = (LocalRegion)event.getRegion();
 +
 +    ThreadIdentifier membershipID = new ThreadIdentifier(eventID.getMembershipID(),
 +        eventID.getThreadID());
 +
 +    VersionTag tag = null;
 +    if (lr.getServerProxy() == null/* && event.hasClientOrigin()*/) { // clients do not need to store version tags for replayed events
 +      tag = event.getVersionTag();
 +      RegionVersionVector v = ((LocalRegion)event.getRegion()).getVersionVector();
 +      // bug #46453 - make sure ID references are canonical before storing
 +      if (v != null && tag != null) {
 +        tag.setMemberID(v.getCanonicalId(tag.getMemberID()));
 +        if (tag.getPreviousMemberID() != null) {
 +          tag.setPreviousMemberID(v.getCanonicalId(tag.getPreviousMemberID()));
 +        }
 +      }
 +    }
-     EventSeqnoHolder newEvh = new EventSeqnoHolder(eventID.getSequenceID(), tag);
-     if (logger.isTraceEnabled()){
-       logger.trace("region event tracker recording {}", event);
-     }
-     recordSeqno(membershipID, newEvh);
 +    
 +    //If this is a bulkOp, and concurrency checks are enabled, we need to
 +    //save the version tag in case we retry.
 +    if (lr.concurrencyChecksEnabled 
 +        && (event.getOperation().isPutAll() || event.getOperation().isRemoveAll()) && lr.getServerProxy() == null) {
 +      recordBulkOpEvent(event, membershipID);
 +    }
++
++    EventSeqnoHolder newEvh = new EventSeqnoHolder(eventID.getSequenceID(), tag);
++    if (logger.isTraceEnabled()){
++      logger.trace("region event tracker recording {}", event);
++    }
++    recordSeqno(membershipID, newEvh);
 +  }
 +
 +  /**
 +   * Record a version tag for a bulk operation
 +   */
 +  private void recordBulkOpEvent(InternalCacheEvent event, ThreadIdentifier tid) {
 +    EventID eventID = event.getEventId();
 +    
 +    VersionTag tag = event.getVersionTag();
 +    if (tag == null) {
 +      return;
 +    }
 +    
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("recording bulkOp event {} {} {} op={}", tid.expensiveToString(),
 +          eventID, tag, event.getOperation());
 +    }
 +
 +    RegionVersionVector v = ((LocalRegion)event.getRegion()).getVersionVector();
 +    // bug #46453 - make sure ID references are canonical before storing
 +    if (v != null) {
 +      tag.setMemberID(v.getCanonicalId(tag.getMemberID()));
 +      if (tag.getPreviousMemberID() != null) {
 +        tag.setPreviousMemberID(v.getCanonicalId(tag.getPreviousMemberID()));
 +      }
 +    }
 +
 +    //Loop until we can successfully update the recorded bulk operations
 +    //For this thread id.
 +    boolean retry = false;
 +    do {
 +      BulkOpHolder bulkOpTracker = recordedBulkOpVersionTags.get(tid);
 +      if(bulkOpTracker == null) {
 +        bulkOpTracker = new BulkOpHolder();
 +        BulkOpHolder old = recordedBulkOpVersionTags.putIfAbsent(tid, bulkOpTracker);
 +        if(old != null) {
 +          retry = true;
 +          continue;
 +        }
 +      }
 +      synchronized(bulkOpTracker) {
 +        if(bulkOpTracker.removed) {
 +          retry = true;
 +          continue;
 +        }
 +        
 +        //Add the version tag for bulkOp event.
 +        bulkOpTracker.putVersionTag(eventID, event.getVersionTag());
 +        retry = false;
 +      }
 +    } while(retry);
 +  }
 +  
 +  public boolean hasSeenEvent(InternalCacheEvent event) {
 +//  ClientProxyMembershipID membershipID = event.getContext();
 +    EventID eventID = event.getEventId();
 +    if (ignoreEvent(event, eventID)) {
 +      return false; // not tracked
 +    }
 +    return hasSeenEvent(eventID, event);
 +  }
 +  
 +  public boolean hasSeenEvent(EventID eventID) {
 +    return hasSeenEvent(eventID, null);
 +  }
 +
 +  public boolean hasSeenEvent(EventID eventID, InternalCacheEvent tagHolder) {
 +    ThreadIdentifier membershipID = new ThreadIdentifier(
 +        eventID.getMembershipID(), eventID.getThreadID());
 +//  if (membershipID == null || eventID == null) {
 +//    return false;
 +//  }
 +        
 +    EventSeqnoHolder evh = recordedEvents.get(membershipID);
 +    if (evh == null) {
 +      return false;
 +    }
 +    
 +    synchronized (evh) {
 +      if (evh.removed || evh.lastSeqno < eventID.getSequenceID()) {
 +        return false;
 +      }
 +      // log at fine because partitioned regions can send event multiple times
 +      // during normal operation during bucket region initialization
 +      if (logger.isTraceEnabled(LogMarker.DISTRIBUTION_BRIDGE_SERVER)) {
 +        logger.trace(LogMarker.DISTRIBUTION_BRIDGE_SERVER, "Cache encountered replay of event with ID {}.  Highest recorded for this source is {}",
 +            eventID, evh.lastSeqno);
 +      }
 +      // bug #44956 - recover version tag for duplicate event
 +      if (evh.lastSeqno == eventID.getSequenceID() && tagHolder != null && evh.versionTag != null) {
 +        ((EntryEventImpl)tagHolder).setVersionTag(evh.versionTag);
 +      }
 +      return true;
 +    } // synchronized
 +  }
 +
 +  public VersionTag findVersionTag(EventID eventID) {
 +    ThreadIdentifier threadID = new ThreadIdentifier(
 +        eventID.getMembershipID(), eventID.getThreadID());
 +        
 +    EventSeqnoHolder evh = recordedEvents.get(threadID);
 +    if (evh == null) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("search for version tag failed as no event is recorded for {}", threadID.expensiveToString());
 +      }
 +      return null;
 +    }
 +    
 +    synchronized (evh) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("search for version tag located last event for {}: {}",threadID.expensiveToString(), evh);
 +      }
 +      if (evh.lastSeqno != eventID.getSequenceID()) {
 +        return null;
 +      }
 +      // log at fine because partitioned regions can send event multiple times
 +      // during normal operation during bucket region initialization
 +      if (logger.isTraceEnabled(LogMarker.DISTRIBUTION_BRIDGE_SERVER) && evh.versionTag == null) {
 +        logger.trace(LogMarker.DISTRIBUTION_BRIDGE_SERVER, "Could not recover version tag.  Found event holder with no version tag for {}", eventID);
 +      }
 +      return evh.versionTag;
 +    } // synchronized
 +  }
 +  
 +  public VersionTag findVersionTagForGateway(EventID eventID) {
 +    ThreadIdentifier threadID = new ThreadIdentifier(
 +        eventID.getMembershipID(), eventID.getThreadID());
 +        
 +    EventSeqnoHolder evh = recordedEvents.get(threadID);
 +    if (evh == null) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("search for version tag failed as no event is recorded for {}", threadID.expensiveToString());
 +      }
 +      return null;
 +    }
 +    
 +    synchronized (evh) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("search for version tag located last event for {}: {} {}",threadID.expensiveToString(), evh, eventID.getSequenceID() );
 +      }
 +      
 +      if (evh.lastSeqno < eventID.getSequenceID()) {
 +        return null;
 +      }
 +      // log at fine because partitioned regions can send event multiple times
 +      // during normal operation during bucket region initialization
 +      if (logger.isTraceEnabled(LogMarker.DISTRIBUTION_BRIDGE_SERVER) && evh.versionTag == null) {
 +        logger.trace(LogMarker.DISTRIBUTION_BRIDGE_SERVER, "Could not recover version tag.  Found event holder with no version tag for {}", eventID);
 +      }
 +      return evh.versionTag;
 +    } // synchronized
 +  }
 +  
 +  
 +  public VersionTag findVersionTagForBulkOp(EventID eventID) {
 +    ThreadIdentifier threadID = new ThreadIdentifier(
 +        eventID.getMembershipID(), eventID.getThreadID());
 +        
 +    BulkOpHolder evh = recordedBulkOpVersionTags.get(threadID);
 +    if (evh == null) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("search for version tag failed as no events are recorded for {}", threadID.expensiveToString());
 +      }
 +      return null;
 +    }
 +    
 +    synchronized (evh) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("search for version tag located event holder for {}: {}", threadID.expensiveToString(), evh);
 +      }
 +      return evh.entryVersionTags.get(eventID);
 +    } // synchronized
 +  }
 +
 +  /**
 +   * @param event
 +   * @param eventID
 +   * @return true if the event should not be tracked, false otherwise
 +   */
 +  private boolean ignoreEvent(InternalCacheEvent event, EventID eventID) {
 +    if (eventID == null) {
 +      return true;
 +    } else {
 +      boolean isVersioned = (event.getVersionTag() != null);
 +      boolean isClient = event.hasClientOrigin();
 +      if (isVersioned && isClient) {
 +        return false; // version tags for client events are kept for retries by the client
 +      }
 +      boolean isEntry = event.getOperation().isEntry();
 +      boolean isPr = event.getRegion().getAttributes().getDataPolicy().withPartitioning()
 +                   || ((LocalRegion)event.getRegion()).isUsedForPartitionedRegionBucket();
 +      return (!isClient &&   // ignore if it originated on a server, and
 +           isEntry &&        // it affects an entry and
 +          !isPr);            // is not on a PR
 +    }
 +  }
 +
 +  /**
 +   * A routine to provide synchronization running based on <memberShipID, threadID> 
 +   * of the requesting client
 +   * @param r - a Runnable to wrap the processing of the bulk op
 +   * @param eventID - the base event ID of the bulk op
 +   *
 +   * @since 5.7
 +   */
 +  public void syncBulkOp(Runnable r, EventID eventID) {
 +    Assert.assertTrue(eventID != null);
 +    ThreadIdentifier membershipID = new ThreadIdentifier(
 +      eventID.getMembershipID(), eventID.getThreadID());
 +
 +    BulkOpProcessed opSyncObj = recordedBulkOps.putIfAbsent(membershipID, new BulkOpProcessed(false));
 +    if (opSyncObj == null) {
 +      opSyncObj = recordedBulkOps.get(membershipID);
 +    }
 +    synchronized (opSyncObj) {
 +      try {
 +        if (opSyncObj.getStatus() && logger.isDebugEnabled()) {
 +          logger.debug("SyncBulkOp: The operation was performed by another thread.");
 +        }
 +        else {
 +          recordBulkOpStart(membershipID);
 +          
 +          //Perform the bulk op
 +          r.run();
 +          // set to true in case another thread is waiting at sync 
 +          opSyncObj.setStatus(true);
 +          recordedBulkOps.remove(membershipID);
 +        }
 +      }
 +      finally {
 +        recordedBulkOps.remove(membershipID);
 +      }
 +    }
 +  }
 +  
 +  /**
 +   * Called when a bulkOp is started on the local region. Used to clear
 +   * event tracker state from the last bulkOp.
 +   */
 +  public void recordBulkOpStart(ThreadIdentifier tid) {
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("recording bulkOp start for {}", tid.expensiveToString());
 +    }
 +    this.recordedBulkOpVersionTags.remove(tid);
 +  }
 +  
 +  /**
 +   * @return the initialized
 +   */
 +  public boolean isInitialized() {
 +    return this.initialized;
 +  }
 +  
 +  /**
 +   * @param mbr the member in question
 +   * @return true if the given member provided the initial image event state for this tracker
 +   */
 +  public boolean isInitialImageProvider(DistributedMember mbr) {
 +    return (this.initialImageProvider != null)
 +      && (mbr != null)
 +      && this.initialImageProvider.equals(mbr);
 +  }
 +  
 +  /**
 +   * Test method for getting the set of recorded version tags.
 +   */
 +  protected ConcurrentMap<ThreadIdentifier, BulkOpHolder> getRecordedBulkOpVersionTags() {
 +    return recordedBulkOpVersionTags;
 +  }
 +  
 +  @Override
 +  public String toString() {
 +    return ""+this.name+"(initialized=" + this.initialized+")";
 +  }
 +  
 +  /**
 +   * A sequence number tracker to keep events from clients from being
 +   * re-applied to the cache if they've already been seen.
 +   * @author bruce
 +   * @since 5.5
 +   */
 +  static class EventSeqnoHolder implements DataSerializable {
 +    private static final long serialVersionUID = 8137262960763308046L;
 +
 +    /** event sequence number.  These  */
 +    long lastSeqno = -1;
 +    
 +    /** millisecond timestamp */
 +    transient long endOfLifeTimer;
 +    
 +    /** whether this entry is being removed */
 +    transient boolean removed;
 +    
 +    /**
 +     * version tag, if any, for the operation
 +     */
 +    VersionTag versionTag;
 +    
 +    // for debugging
 +//    transient Exception context;
 +    
 +    EventSeqnoHolder(long id, VersionTag versionTag) {
 +      this.lastSeqno = id;
 +      this.versionTag = versionTag;
 +    }
 +    
 +    public EventSeqnoHolder() {
 +    }
 +    
 +    @Override
 +    public String toString() {
 +      StringBuilder result = new StringBuilder();
 +      result.append("seqNo").append(this.lastSeqno);
 +      if (this.versionTag != null) {
 +        result.append(",").append(this.versionTag);
 +      }
 +      return result.toString();
 +    }
 +
 +    public void fromData(DataInput in) throws IOException,
 +        ClassNotFoundException {
 +      lastSeqno = in.readLong();
 +      versionTag = (VersionTag)DataSerializer.readObject(in);
 +    }
 +
 +    public void toData(DataOutput out) throws IOException {
 +      out.writeLong(lastSeqno);
 +      DataSerializer.writeObject(versionTag, out);
 +    }
 +  }
 +
 +  /**
 +   * A status tracker for each bulk operation (putAll or removeAll) from originators specified by
 +   * membershipID and threadID in the cache
 +   * processed is true means the bulk op is processed by one thread 
 +   * no need to redo it by other threads.
 +   * @author Gester
 +   * @since 5.7
 +   */
 +  static class BulkOpProcessed {
 +    /** whether the op is processed */
 +    private boolean processed;
 +  
 +    /**
 +     * creates a new instance to save status of a bulk op 
 +     * @param status true if the op has been processed 
 +     */
 +    BulkOpProcessed(boolean status) {
 +      this.processed = status;
 +    }
 +    
 +    /**
 +     * setter method to change the status
 +     * @param status true if the op has been processed 
 +     */
 +    void setStatus(boolean status) {
 +      this.processed = status;
 +    }
 +    
 +    /**
 +     * getter method to peek the current status
 +     * @return current status
 +     */
 +    boolean getStatus() {
 +      return this.processed;
 +    }
 +    
 +    @Override
 +    public String toString() {
 +      return "BULKOP("+this.processed+")";
 +    }
 +  }
 +  
 +  /**
 +   * A holder for the version tags generated for a bulk operation (putAll or removeAll). These
 +   * version tags are retrieved when a bulk op is retried.
 +   * @author Dan
 +   * @since 7.0
 +   * protected for test purposes only.
 +   */
 +  protected static class BulkOpHolder {
 +    /**
 +     * Whether this object was removed by the cleanup thread.
 +     */
 +    public boolean removed;
 +    /**
 +     * public for tests only
 +     */
 +    public Map<EventID, VersionTag> entryVersionTags = new HashMap<EventID, VersionTag>();
 +    /** millisecond timestamp */
 +    transient long endOfLifeTimer;
 +  
 +    /**
 +     * creates a new instance to save status of a putAllOperation 
 +     */
 +    BulkOpHolder() {
 +    }
 +    
 +    public void putVersionTag(EventID eventId, VersionTag versionTag) {
 +      entryVersionTags.put(eventId, versionTag);
 +      this.endOfLifeTimer = 0;
 +    }
 +
 +    
 +    @Override
 +    public String toString() {
 +      return "BulkOpHolder tags=" + this.entryVersionTags;
 +    }
 +  }
 +  
 +  static class ExpiryTask extends SystemTimerTask {
 +    
 +    GemFireCacheImpl cache;
 +    long expiryTime;
 +    List trackers = new LinkedList();
 +    
 +    public ExpiryTask(GemFireCacheImpl cache, long expiryTime) {
 +      this.cache = cache;
 +      this.expiryTime = expiryTime;
 +    }
 +    void addTracker(EventTracker tracker) {
 +      synchronized(trackers) {
 +        trackers.add(tracker);
 +      }
 +    }
 +    void removeTracker(EventTracker tracker) {
 +      synchronized(trackers) {
 +        trackers.remove(tracker);
 +      }
 +    }
 +    int getNumberOfTrackers() {
 +      return trackers.size();
 +    }
 +    @Override
 +    public void run2() {
 +      long now = System.currentTimeMillis();
 +      long timeout = now - expiryTime;
 +      final boolean traceEnabled = logger.isTraceEnabled();
 +      synchronized(trackers) {
 +        for (Iterator it=trackers.iterator(); it.hasNext(); ) {
 +          EventTracker tracker = (EventTracker)it.next();
 +          if (traceEnabled) {
 +            logger.trace("{} sweeper: starting", tracker.name);
 +          }
 +          for (Iterator it2 = tracker.recordedEvents.entrySet().iterator(); it2.hasNext();) {
 +            Map.Entry e = (Map.Entry)it2.next();
 +            EventSeqnoHolder evh = (EventSeqnoHolder)e.getValue();
 +            synchronized(evh) {
 +              if (evh.endOfLifeTimer == 0) {
 +                evh.endOfLifeTimer = now; // a new holder - start the timer 
 +              }
 +              if (evh.endOfLifeTimer <= timeout) {
 +                evh.removed = true;
 +                evh.lastSeqno = -1;
 +                if (traceEnabled) {
 +                  logger.trace("{} sweeper: removing {}", tracker.name, e.getKey());
 +                }
 +                it2.remove();
 +              }
 +            }
 +          }
 +          
 +          //Remove bulk operations we're tracking
 +          for (Iterator<Map.Entry<ThreadIdentifier, BulkOpHolder>> it2 = tracker.recordedBulkOpVersionTags.entrySet().iterator(); it2.hasNext();) {
 +            Map.Entry<ThreadIdentifier, BulkOpHolder> e = it2.next();
 +            BulkOpHolder evh = e.getValue();
 +            synchronized(evh) {
 +              if (evh.endOfLifeTimer == 0) {
 +                evh.endOfLifeTimer = now; // a new holder - start the timer 
 +              }
 +              //Remove the PutAll tracker only if the put all is complete
 +              //and it has expired.
 +              if (evh.endOfLifeTimer <= timeout) {
 +                evh.removed = true;
 +                if (logger.isTraceEnabled()) {
 +                  logger.trace("{} sweeper: removing bulkOp {}", tracker.name, e.getKey());
 +                }
 +                it2.remove();
 +              }
 +            }
 +          }
 +          if (traceEnabled) {
 +            logger.trace("{} sweeper: done", tracker.name);
 +          }
 +        }
 +      }
 +    }
 +    
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/GemFireCacheImpl.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/cache/GemFireCacheImpl.java
index 201acc0,0000000..201acc0
mode 100644,000000..100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/GemFireCacheImpl.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/GemFireCacheImpl.java

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/TXManagerImpl.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/cache/TXManagerImpl.java
index f4216ac,0000000..de49fea
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/TXManagerImpl.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/TXManagerImpl.java
@@@ -1,1511 -1,0 +1,1511 @@@
 +/*
 + * 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;
 +
 +import java.io.DataInput;
 +import java.io.DataOutput;
 +import java.io.IOException;
 +import java.util.ArrayList;
 +import java.util.Arrays;
 +import java.util.Collections;
 +import java.util.HashMap;
 +import java.util.HashSet;
 +import java.util.Iterator;
 +import java.util.LinkedHashMap;
 +import java.util.List;
 +import java.util.Map;
 +import java.util.Map.Entry;
 +import java.util.Queue;
 +import java.util.Set;
 +import java.util.concurrent.ConcurrentHashMap;
 +import java.util.concurrent.ConcurrentLinkedQueue;
 +import java.util.concurrent.ConcurrentMap;
 +import java.util.concurrent.TimeUnit;
 +import java.util.concurrent.atomic.AtomicInteger;
 +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
 +import java.util.concurrent.locks.LockSupport;
 +
 +import org.apache.logging.log4j.Logger;
 +
 +import com.gemstone.gemfire.DataSerializer;
 +import com.gemstone.gemfire.GemFireException;
 +import com.gemstone.gemfire.InternalGemFireError;
 +import com.gemstone.gemfire.SystemFailure;
 +import com.gemstone.gemfire.cache.Cache;
 +import com.gemstone.gemfire.cache.CacheTransactionManager;
 +import com.gemstone.gemfire.cache.CommitConflictException;
 +import com.gemstone.gemfire.cache.TransactionDataRebalancedException;
 +import com.gemstone.gemfire.cache.TransactionId;
 +import com.gemstone.gemfire.cache.TransactionInDoubtException;
 +import com.gemstone.gemfire.cache.TransactionListener;
 +import com.gemstone.gemfire.cache.TransactionWriter;
 +import com.gemstone.gemfire.cache.UnsupportedOperationInTransactionException;
 +import com.gemstone.gemfire.distributed.TXManagerCancelledException;
 +import com.gemstone.gemfire.distributed.internal.DM;
 +import com.gemstone.gemfire.distributed.internal.DistributionManager;
 +import com.gemstone.gemfire.distributed.internal.HighPriorityDistributionMessage;
 +import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
 +import com.gemstone.gemfire.distributed.internal.MembershipListener;
 +import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
 +import com.gemstone.gemfire.internal.SystemTimer.SystemTimerTask;
 +import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.logging.LogService;
 +import com.gemstone.gemfire.internal.logging.log4j.LocalizedMessage;
 +import com.gemstone.gemfire.internal.util.concurrent.CustomEntryConcurrentHashMap;
 +import com.gemstone.gemfire.internal.util.concurrent.CustomEntryConcurrentHashMap.HashEntry;
 +import com.gemstone.gemfire.internal.util.concurrent.CustomEntryConcurrentHashMap.MapCallback;
 +
 +/** <p>The internal implementation of the {@link CacheTransactionManager}
 + * interface returned by {@link GemFireCacheImpl#getCacheTransactionManager}.
 + * Internal operations 
 +
 + </code>TransactionListener</code> invocation, Region synchronization, transaction statistics and
 +
 + * transaction logging are handled here 
 + * 
 + * @author Mitch Thomas
 + *
 + * @since 4.0
 + * 
 + * @see CacheTransactionManager
 + */
 +public final class TXManagerImpl implements CacheTransactionManager,
 +    MembershipListener {
 +
 +  private static final Logger logger = LogService.getLogger();
 +  
 +  // Thread specific context container
 +  private final ThreadLocal<TXStateProxy> txContext;
 +  private static TXManagerImpl currentInstance = null;
 +  // The unique transaction ID for this Manager 
 +  private final AtomicInteger uniqId;
 +
 +  private final DM dm;
 +  private final Cache cache;
 +
 +  // The DistributionMemberID used to construct TXId's
 +  private final InternalDistributedMember distributionMgrId;
 +
 +  private final CachePerfStats cachePerfStats;
 +
 +  private static final TransactionListener[] EMPTY_LISTENERS =
 +    new TransactionListener[0];
 +
 +  /**
 +   * Default transaction id to indicate no transaction
 +   */
 +  public static final int NOTX = -1;
 +  
 +  private final ArrayList<TransactionListener> txListeners = new ArrayList<TransactionListener>(8);
 +  public TransactionWriter writer = null;
 +  private boolean closed = false;
 +
 +  private final Map<TXId, TXStateProxy> hostedTXStates;
 +
 +  /**
 +   * the number of client initiated transactions to store for client failover
 +   */
 +  public final static int FAILOVER_TX_MAP_SIZE = Integer.getInteger("gemfire.transactionFailoverMapSize", 1000);
 +  
 +  /**
 +   * used to store TXCommitMessages for client initiated transactions, so that when a client failsover,
 +   * (after the delegate dies) the commit message can be sent to client.
 +   * //TODO we really need to keep around only one msg for each thread on a client
 +   */
 +  @SuppressWarnings("unchecked")
 +  private Map<TXId ,TXCommitMessage> failoverMap = Collections.synchronizedMap(new LinkedHashMap<TXId, TXCommitMessage>() {
 +    private static final long serialVersionUID = -4156018226167594134L;
 +
 +    protected boolean removeEldestEntry(Entry eldest) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("TX: removing client initiated transaction from failover map:{} :{}", eldest.getKey(), (size()>FAILOVER_TX_MAP_SIZE));
 +      }
 +      return size() > FAILOVER_TX_MAP_SIZE;
 +    };
 +  });
 +
 +  /**
 +   * A flag to allow persistent transactions. public for testing.
 +   */
 +  public static boolean ALLOW_PERSISTENT_TRANSACTIONS = Boolean.getBoolean("gemfire.ALLOW_PERSISTENT_TRANSACTIONS");
 +
 +  /**
 +   * this keeps track of all the transactions that were initiated locally.
 +   */
 +  private ConcurrentMap<TXId, TXStateProxy> localTxMap = new ConcurrentHashMap<TXId, TXStateProxy>();
 +
 +  /**
 +   * the time in minutes after which any suspended transaction are rolled back. default is 30 minutes
 +   */
 +  private volatile long suspendedTXTimeout = Long.getLong("gemfire.suspendedTxTimeout", 30);
 +  
 +  /**
 +   * Thread-specific flag to indicate whether the transactions managed by this
 +   * CacheTransactionManager for this thread should be distributed
 +   */
 +  private final ThreadLocal<Boolean> isTXDistributed;
 +  
 +
 +  /** Constructor that implements the {@link CacheTransactionManager}
 +   * interface. Only only one instance per {@link com.gemstone.gemfire.cache.Cache}
 +   *
 +   * @param cachePerfStats 
 +   */
 +  public TXManagerImpl(
 +                       CachePerfStats cachePerfStats,
 +                       Cache cache) {
 +    this.cache = cache;
 +    this.dm = ((InternalDistributedSystem)cache.getDistributedSystem())
 +        .getDistributionManager();
 +    this.distributionMgrId = this.dm.getDistributionManagerId();
 +    this.uniqId = new AtomicInteger(0);
 +    this.cachePerfStats = cachePerfStats;
 +    this.hostedTXStates = new HashMap<TXId, TXStateProxy>();
 +    this.txContext = new ThreadLocal<TXStateProxy>();
 +    this.isTXDistributed = new ThreadLocal<Boolean>();
 +    currentInstance = this;
 +  }
 +
 +  final Cache getCache() {
 +    return this.cache;
 +  }
 +  
 +  
 +  /**
 +   * Get the TransactionWriter for the cache
 +   * 
 +   * @return the current TransactionWriter
 +   * @see TransactionWriter
 +   */
 +  public final TransactionWriter getWriter() {
 +    return writer;
 +  }
 +  
 +
 +  public final void setWriter(TransactionWriter writer) {
 +    if (((GemFireCacheImpl)this.cache).isClient()) {
 +      throw new IllegalStateException(LocalizedStrings.TXManager_NO_WRITER_ON_CLIENT.toLocalizedString());
 +    }
 +    this.writer = writer;
 +  }
 +  
 +
 +  public final TransactionListener getListener() {
 +    synchronized (this.txListeners) {
 +      if (this.txListeners.isEmpty()) {
 +      return null;
 +      } else if (this.txListeners.size() == 1) {
 +        return this.txListeners.get(0);
 +      } else {
 +        throw new IllegalStateException(LocalizedStrings.TXManagerImpl_MORE_THAN_ONE_TRANSACTION_LISTENER_EXISTS.toLocalizedString());
 +      }
 +    }
 +  }
 +  
 +  public TransactionListener[] getListeners() {
 +    synchronized (this.txListeners) {
 +      int size = this.txListeners.size();
 +      if (size == 0) {
 +        return EMPTY_LISTENERS;
 +      } else {
 +        TransactionListener[] result = new TransactionListener[size];
 +        this.txListeners.toArray(result);
 +        return result;
 +      }
 +    }
 +  }
 +  
 +  public TransactionListener setListener(TransactionListener newListener) {
 +    synchronized (this.txListeners) {
 +      TransactionListener result = getListener();
 +      this.txListeners.clear();
 +      if (newListener != null) {
 +        this.txListeners.add(newListener);
 +      }
 +      if (result != null) {
 +        closeListener(result);
 +      }
 +      return result;
 +    }
 +  }
 +  public void addListener(TransactionListener aListener) {
 +    if (aListener == null) {
 +      throw new IllegalArgumentException(LocalizedStrings.TXManagerImpl_ADDLISTENER_PARAMETER_WAS_NULL.toLocalizedString());
 +    }
 +    synchronized (this.txListeners) {
 +      if (!this.txListeners.contains(aListener)) {
 +        this.txListeners.add(aListener);
 +      }
 +    }
 +  }
 +  public void removeListener(TransactionListener aListener) {
 +    if (aListener == null) {
 +      throw new IllegalArgumentException(LocalizedStrings.TXManagerImpl_REMOVELISTENER_PARAMETER_WAS_NULL.toLocalizedString());
 +    }
 +    synchronized (this.txListeners) {
 +      if (this.txListeners.remove(aListener)) {
 +        closeListener(aListener);
 +      }
 +    }
 +  }
 +  public void initListeners(TransactionListener[] newListeners) {
 +    synchronized (this.txListeners) {
 +      if (!this.txListeners.isEmpty()) {
 +        Iterator<TransactionListener> it = this.txListeners.iterator();
 +        while (it.hasNext()) {
 +          closeListener(it.next());
 +        }
 +        this.txListeners.clear();
 +      }
 +      if (newListeners != null && newListeners.length > 0) {
 +        List<TransactionListener> nl = Arrays.asList(newListeners);
 +        if (nl.contains(null)) {
 +          throw new IllegalArgumentException(LocalizedStrings.TXManagerImpl_INITLISTENERS_PARAMETER_HAD_A_NULL_ELEMENT.toLocalizedString());
 +        }
 +        this.txListeners.addAll(nl);
 +      }
 +    }
 +  }
 +
 +  final CachePerfStats getCachePerfStats() {
 +    return this.cachePerfStats;
 +  }
 +
 +  /** Build a new {@link TXId}, use it as part of the transaction
 +   * state and associate with the current thread using a {@link
 +   * ThreadLocal}.
 +   */
 +  public void begin() {
 +    checkClosed();
 +    {
 +      TransactionId tid = getTransactionId();
 +      if (tid != null) {
 +        throw new java.lang.IllegalStateException(LocalizedStrings.TXManagerImpl_TRANSACTION_0_ALREADY_IN_PROGRESS.toLocalizedString(tid));
 +      }
 +    }
 +    TXId id = new TXId(this.distributionMgrId, this.uniqId.incrementAndGet());
 +    TXStateProxyImpl proxy = null;
 +    if (isDistributed()) {
 +      proxy = new DistTXStateProxyImplOnCoordinator(this, id, null);  
 +    } else {
 +      proxy = new TXStateProxyImpl(this, id, null);  
 +    }
 +    setTXState(proxy);
 +    this.localTxMap.put(id, proxy);
 +  }
 +
 +
 +  /** Build a new {@link TXId}, use it as part of the transaction
 +   * state and associate with the current thread using a {@link
 +   * ThreadLocal}. Flag the transaction to be enlisted with a JTA
 +   * Transaction.  Should only be called in a context where we know
 +   * there is no existing transaction.
 +   */
 +  public TXStateProxy beginJTA() {
 +    checkClosed();
 +    TXId id = new TXId(this.distributionMgrId, this.uniqId.incrementAndGet());
 +    TXStateProxy newState = null;
 +    
 +    if (isDistributed()) {
 +      newState = new DistTXStateProxyImplOnCoordinator(this, id, true);
 +    } else {
 +      newState = new TXStateProxyImpl(this, id, true);
 +    }
 +    setTXState(newState);
 +    return newState;
 +  }
 +
 +  /*
 +   * Only applicable for Distributed transaction.
 +   */
 +  public void precommit() throws CommitConflictException {
 +    checkClosed();
 +
 +    final TXStateProxy tx = getTXState();
 +    if (tx == null) {
 +      throw new IllegalStateException(LocalizedStrings.TXManagerImpl_THREAD_DOES_NOT_HAVE_AN_ACTIVE_TRANSACTION.toLocalizedString());
 +    }
 +    
 +    tx.checkJTA(LocalizedStrings.TXManagerImpl_CAN_NOT_COMMIT_THIS_TRANSACTION_BECAUSE_IT_IS_ENLISTED_WITH_A_JTA_TRANSACTION_USE_THE_JTA_MANAGER_TO_PERFORM_THE_COMMIT.toLocalizedString());
 +  
 +    tx.precommit();
 +  }
 +  
 +  /** Complete the transaction associated with the current
 +   *  thread. When this method completes, the thread is no longer
 +   *  associated with a transaction.
 +   *
 +   */
 +  public void commit() throws CommitConflictException {
 +    checkClosed();
 +
 +    final TXStateProxy tx = getTXState();
 +    if (tx == null) {
 +      throw new IllegalStateException(LocalizedStrings.TXManagerImpl_THREAD_DOES_NOT_HAVE_AN_ACTIVE_TRANSACTION.toLocalizedString());
 +    }
 +
 +    tx.checkJTA(LocalizedStrings.TXManagerImpl_CAN_NOT_COMMIT_THIS_TRANSACTION_BECAUSE_IT_IS_ENLISTED_WITH_A_JTA_TRANSACTION_USE_THE_JTA_MANAGER_TO_PERFORM_THE_COMMIT.toLocalizedString());
 +
 +    final long opStart = CachePerfStats.getStatTime();
 +    final long lifeTime = opStart - tx.getBeginTime();
 +    try {
 +      setTXState(null);
 +      tx.commit();
 +    } catch (CommitConflictException ex) {
 +      saveTXStateForClientFailover(tx, TXCommitMessage.CMT_CONFLICT_MSG); //fixes #43350
 +      noteCommitFailure(opStart, lifeTime, tx);
 +      cleanup(tx.getTransactionId()); // fixes #52086
 +      throw ex;
 +    } catch (TransactionDataRebalancedException reb) {
 +      saveTXStateForClientFailover(tx, TXCommitMessage.REBALANCE_MSG);
 +      cleanup(tx.getTransactionId()); // fixes #52086
 +      throw reb;
 +    } catch (UnsupportedOperationInTransactionException e) {
 +      // fix for #42490
 +      setTXState(tx);
 +      throw e;
 +    } catch (RuntimeException e) {
 +      saveTXStateForClientFailover(tx, TXCommitMessage.EXCEPTION_MSG);
 +      cleanup(tx.getTransactionId()); // fixes #52086
 +      throw e;
 +    }
 +    saveTXStateForClientFailover(tx);
 +    cleanup(tx.getTransactionId());
 +    noteCommitSuccess(opStart, lifeTime, tx);
 +  }
 +
 +  final void noteCommitFailure(long opStart, long lifeTime, TXStateInterface tx) {
 +    long opEnd = CachePerfStats.getStatTime();
 +    this.cachePerfStats.txFailure(opEnd - opStart,
 +                                  lifeTime, tx.getChanges());
 +    TransactionListener[] listeners = getListeners();
 +    if (tx.isFireCallbacks() && listeners.length > 0) {
 +      final TXEvent e = tx.getEvent();
 +      try {
 +      for (int i=0; i < listeners.length; i++) {
 +        try {
 +          listeners[i].afterFailedCommit(e);
 +        } 
 +        catch (VirtualMachineError err) {
 +          SystemFailure.initiateFailure(err);
 +          // If this ever returns, rethrow the error.  We're poisoned
 +          // now, so don't let this thread continue.
 +          throw err;
 +        }
 +        catch (Throwable t) {
 +          // Whenever you catch Error or Throwable, you must also
 +          // catch VirtualMachineError (see above).  However, there is
 +          // _still_ a possibility that you are dealing with a cascading
 +          // error condition, so you also need to check to see if the JVM
 +          // is still usable:
 +          SystemFailure.checkFailure();
 +          logger.error(LocalizedMessage.create(LocalizedStrings.TXManagerImpl_EXCEPTION_OCCURRED_IN_TRANSACTIONLISTENER), t);
 +        }
 +      }
 +      } finally {
 +        e.release();
 +      }
 +    }
 +  }
 +
 +  final void noteCommitSuccess(long opStart, long lifeTime, TXStateInterface tx) {
 +    long opEnd = CachePerfStats.getStatTime();
 +    this.cachePerfStats.txSuccess(opEnd - opStart,
 +                                  lifeTime, tx.getChanges());
 +    TransactionListener[] listeners = getListeners();
 +    if (tx.isFireCallbacks() && listeners.length > 0) {
 +      final TXEvent e = tx.getEvent();
 +      try {
 +      for (final TransactionListener listener : listeners) {
 +        try {
 +          listener.afterCommit(e);
 +        } 
 +        catch (VirtualMachineError err) {
 +          SystemFailure.initiateFailure(err);
 +          // If this ever returns, rethrow the error.  We're poisoned
 +          // now, so don't let this thread continue.
 +          throw err;
 +        }
 +        catch (Throwable t) {
 +          // Whenever you catch Error or Throwable, you must also
 +          // catch VirtualMachineError (see above).  However, there is
 +          // _still_ a possibility that you are dealing with a cascading
 +          // error condition, so you also need to check to see if the JVM
 +          // is still usable:
 +          SystemFailure.checkFailure();
 +          logger.error(LocalizedMessage.create(LocalizedStrings.TXManagerImpl_EXCEPTION_OCCURRED_IN_TRANSACTIONLISTENER), t);
 +        }
 +      }
 +      } finally {
 +        e.release();
 +      }
 +    }
 +  }
 +  
 +  /**
 +   * prepare for transaction replay by assigning a new tx id to the current proxy
 +   */
 +  private void _incrementTXUniqueIDForReplay() {
 +    TXStateProxyImpl tx = (TXStateProxyImpl)getTXState();
 +    assert tx != null : "expected a transaction to be in progress";
 +    TXId id = new TXId(this.distributionMgrId, this.uniqId.incrementAndGet());
 +    tx.setTXIDForReplay(id);
 +  }
 +
 +
 +  /** Roll back the transaction associated with the current
 +   *  thread. When this method completes, the thread is no longer
 +   *  associated with a transaction.
 +   */
 +  public void rollback() {
 +    checkClosed();
 +    TXStateProxy tx = getTXState();
 +    if (tx == null) {
 +      throw new IllegalStateException(LocalizedStrings.TXManagerImpl_THREAD_DOES_NOT_HAVE_AN_ACTIVE_TRANSACTION.toLocalizedString());
 +    }
 +
 +    tx.checkJTA(LocalizedStrings.TXManagerImpl_CAN_NOT_ROLLBACK_THIS_TRANSACTION_IS_ENLISTED_WITH_A_JTA_TRANSACTION_USE_THE_JTA_MANAGER_TO_PERFORM_THE_ROLLBACK.toLocalizedString());
 +
 +    final long opStart = CachePerfStats.getStatTime();
 +    final long lifeTime = opStart - tx.getBeginTime();
 +    setTXState(null);
 +    tx.rollback();
 +    saveTXStateForClientFailover(tx);
 +    cleanup(tx.getTransactionId());
 +    noteRollbackSuccess(opStart, lifeTime, tx);
 +  }
 +  
 +  final void noteRollbackSuccess(long opStart, long lifeTime, TXStateInterface tx) {
 +    long opEnd = CachePerfStats.getStatTime();
 +    this.cachePerfStats.txRollback(opEnd - opStart,
 +                                   lifeTime, tx.getChanges());
 +    TransactionListener[] listeners = getListeners();
 +    if (tx.isFireCallbacks() && listeners.length > 0) {
 +      final TXEvent e = tx.getEvent();
 +      try {
 +      for (int i = 0; i < listeners.length; i++) {
 +        try {
 +          listeners[i].afterRollback(e);
 +        } 
 +        catch (VirtualMachineError err) {
 +          SystemFailure.initiateFailure(err);
 +          // If this ever returns, rethrow the error.  We're poisoned
 +          // now, so don't let this thread continue.
 +          throw err;
 +        }
 +        catch (Throwable t) {
 +          // Whenever you catch Error or Throwable, you must also
 +          // catch VirtualMachineError (see above).  However, there is
 +          // _still_ a possibility that you are dealing with a cascading
 +          // error condition, so you also need to check to see if the JVM
 +          // is still usable:
 +          SystemFailure.checkFailure();
 +          logger.error(LocalizedMessage.create(LocalizedStrings.TXManagerImpl_EXCEPTION_OCCURRED_IN_TRANSACTIONLISTENER), t);
 +        }
 +      }
 +      } finally {
 +        e.release();
 +      }
 +    }
 +  }
 +
 +  /**
 +   * Called from Commit and Rollback to unblock waiting threads
 +   */
 +  private void cleanup(TransactionId txId) {
 +    TXStateProxy proxy = this.localTxMap.remove(txId);
 +    if (proxy != null) {
 +      proxy.close();
 +    }
 +    Queue<Thread> waitingThreads = this.waitMap.get(txId);
 +    if (waitingThreads != null && !waitingThreads.isEmpty()) {
 +      for (Thread waitingThread : waitingThreads) {
 +        LockSupport.unpark(waitingThread);
 +      }
 +      waitMap.remove(txId);
 +    }
 +  }
 +  
 +  /** Reports the existance of a Transaction for this thread
 +   *
 +   */
 +  public boolean exists() {
 +    return null != getTXState();
 +  }
 +
 +  /** Gets the current transaction identifier or null if no transaction exists
 +   *
 +   */
 +  public TransactionId getTransactionId() {
 +    TXStateProxy t = getTXState();
 +    TransactionId ret = null;
 +    if (t!=null) {
 +      ret = t.getTransactionId();
 +    } 
 +    return ret;
 +  }
 +
 +  /** 
 +   * Returns the TXStateProxyInterface of the current thread; null if no transaction.
 +   */
 +  public final TXStateProxy getTXState() {
 +    TXStateProxy tsp = txContext.get();
 +    if (tsp != null && !tsp.isInProgress()) {
 +      this.txContext.set(null);
 +      tsp = null;
 +    }
 +    return tsp;
 +  }
 +
 +  /**
 +   * sets {@link TXStateProxy#setInProgress(boolean)} when a txContext is present.
 +   * This method must only be used in fail-over scenarios.
 +   * @param progress value of the progress flag to be set
 +   * @return the previous value of inProgress flag
 +   * @see TXStateProxy#setInProgress(boolean)
 +   */
 +  public boolean setInProgress(boolean progress) {
 +    boolean retVal = false;
 +    TXStateProxy tsp = txContext.get();
 +    if (tsp != null) {
 +      retVal = tsp.isInProgress();
 +      tsp.setInProgress(progress);
 +    }
 +    return retVal;
 +  }
 +
 +  public final void setTXState(TXStateProxy val) {
 +    txContext.set(val);
 +  }
 +
 +
 +  public void close() {
 +    if (isClosed()) {
 +      return;
 +    }
 +    this.closed = true;
 +    for (TXStateProxy proxy: this.hostedTXStates.values()) {
 +      proxy.close();
 +    }
 +    for (TXStateProxy proxy: this.localTxMap.values()) {
 +      proxy.close();
 +    }
 +    {
 +      TransactionListener[] listeners = getListeners();
 +      for (int i=0; i < listeners.length; i++) {
 +        closeListener(listeners[i]);
 +      }
 +    }
 +  }
 +  private void closeListener(TransactionListener tl) {
 +    try {
 +      tl.close();
 +    } 
 +    catch (VirtualMachineError err) {
 +      SystemFailure.initiateFailure(err);
 +      // If this ever returns, rethrow the error.  We're poisoned
 +      // now, so don't let this thread continue.
 +      throw err;
 +    }
 +    catch (Throwable t) {
 +      // Whenever you catch Error or Throwable, you must also
 +      // catch VirtualMachineError (see above).  However, there is
 +      // _still_ a possibility that you are dealing with a cascading
 +      // error condition, so you also need to check to see if the JVM
 +      // is still usable:
 +      SystemFailure.checkFailure();
 +      logger.error(LocalizedMessage.create(LocalizedStrings.TXManagerImpl_EXCEPTION_OCCURRED_IN_TRANSACTIONLISTENER), t);
 +    }
 +  }
 +  /**
 +   * If the current thread is in a transaction then suspend will
 +   * cause it to no longer be in a transaction.
 +   * @return the state of the transaction or null. Pass this value
 +   *  to {@link TXManagerImpl#resume} to reactivate the suspended transaction.
 +   */
 +  public final TXStateProxy internalSuspend() {
 +    TXStateProxy result = getTXState();
 +    if (result != null) {
 +      result.suspend();
 +      setTXState(null);
 +    }
 +    return result;
 +  }
 +  /**
 +   * Activates the specified transaction on the calling thread.
 +   * @param tx the transaction to activate.
 +   * @throws IllegalStateException if this thread already has an active transaction
 +   */
 +  public final void resume(TXStateProxy tx) {
 +    if (tx != null) {
 +      TransactionId tid = getTransactionId();
 +      if (tid != null) {
 +        throw new java.lang.IllegalStateException(LocalizedStrings.TXManagerImpl_TRANSACTION_0_ALREADY_IN_PROGRESS.toLocalizedString(tid));
 +      }
 +      if (tx instanceof TXState) {
 +        throw new java.lang.IllegalStateException("Found instance of TXState: " + tx);
 +      }
 +      setTXState(tx);
 +      tx.resume();
 +      SystemTimerTask task = this.expiryTasks.remove(tx.getTransactionId());
 +      if (task != null) {
 +        task.cancel();
 +      }
 +    }
 +  }
 +
 +  private final boolean isClosed() {
 +    return this.closed;
 +  }
 +  private final void checkClosed() {
 +    cache.getCancelCriterion().checkCancelInProgress(null);
 +    if (this.closed) {
 +      throw new TXManagerCancelledException("This transaction manager is closed.");
 +    }
 +  }
 +
 +  final DM getDM() {
 +    return this.dm;
 +  }
 +
 +  
 +  public static int getCurrentTXUniqueId() {
 +    if(currentInstance==null) {
 +      return NOTX;
 +    }
 +    return currentInstance.getMyTXUniqueId();
 +  }
 +  
 +  
 +  
 +  
 +  public final static TXStateProxy getCurrentTXState() {
 +    if(currentInstance==null) {
 +      return null;
 +    }
 +    return currentInstance.getTXState();
 +  }
 +  
 +  public static void incrementTXUniqueIDForReplay() {
 +    if(currentInstance != null) {
 +      currentInstance._incrementTXUniqueIDForReplay();
 +    }
 +  }
 +  
 +  public int getMyTXUniqueId() {
 +    TXStateProxy t = txContext.get();
 +    if (t != null) {
 +      return t.getTxId().getUniqId();
 +    } else {
 +      return NOTX;
 +    }
 +  }
 +
 +  /**
 +   * Associate the remote txState with the thread processing this message. Also,
 +   * we acquire a lock on the txState, on which this thread operates.
 +   * Some messages like SizeMessage should not create a new txState.
 +   * @param msg
 +   * @return {@link TXStateProxy} the txProxy for the transactional message
 +   * @throws InterruptedException 
 +   */
 +  public TXStateProxy masqueradeAs(TransactionMessage msg) throws InterruptedException {
 +    if (msg.getTXUniqId() == NOTX || !msg.canParticipateInTransaction()) {
 +      return null;
 +    }
 +    TXId key = new TXId(msg.getMemberToMasqueradeAs(), msg.getTXUniqId());
 +    TXStateProxy val;
 +    val = this.hostedTXStates.get(key);
 +    if (val == null) {
 +      synchronized(this.hostedTXStates) {
 +        val = this.hostedTXStates.get(key);
 +        if (val == null && msg.canStartRemoteTransaction()) {
 +          if (msg.isTransactionDistributed()) {
 +            val = new DistTXStateProxyImplOnDatanode(this, key, msg.getTXOriginatorClient());
 +            val.setLocalTXState(new DistTXState(val,true));
 +          } else {
 +            val = new TXStateProxyImpl(this, key, msg.getTXOriginatorClient());
 +            val.setLocalTXState(new TXState(val,true));
 +          }
 +          this.hostedTXStates.put(key, val);
 +        }
 +      }
 +    }
 +    if (val != null) {
 +      if (!val.getLock().isHeldByCurrentThread()) {
 +        val.getLock().lock();
 +      }
 +    }
 +
 +    setTXState(val);
 +    return val;
 +  }
 +  
 +  /**
 +   * Associate the remote txState with the thread processing this message. Also,
 +   * we acquire a lock on the txState, on which this thread operates.
 +   * Some messages like SizeMessage should not create a new txState.
 +   * @param msg
 +   * @param memberId
 +   * @param probeOnly - do not masquerade; just look up the TX state
 +   * @return {@link TXStateProxy} the txProxy for the transactional message
 +   * @throws InterruptedException 
 +   */
 +  public TXStateProxy masqueradeAs(Message msg,InternalDistributedMember memberId, boolean probeOnly) throws InterruptedException {
 +    if (msg.getTransactionId() == NOTX) {
 +      return null;
 +    }
 +    TXId key = new TXId(memberId, msg.getTransactionId());
 +    TXStateProxy val;
 +    val = this.hostedTXStates.get(key);
 +    if (val == null) {
 +      synchronized(this.hostedTXStates) {
 +        val = this.hostedTXStates.get(key);
-         if (val == null && msg.canStartRemoteTransaction()) {
++        if (val == null) {
 +          // [sjigyasu] TODO: Conditionally create object based on distributed or non-distributed tx mode 
 +          if (msg instanceof TransactionMessage && ((TransactionMessage)msg).isTransactionDistributed()) {
 +            val = new DistTXStateProxyImplOnDatanode(this, key, memberId);
 +            //val.setLocalTXState(new DistTXState(val,true));
 +          } else {
 +            val = new TXStateProxyImpl(this, key, memberId);
 +            //val.setLocalTXState(new TXState(val,true));
 +          }
 +          this.hostedTXStates.put(key, val);
 +        }
 +      }
 +    }
 +    if (!probeOnly) {
 +      if (val != null) {
 +        if (!val.getLock().isHeldByCurrentThread()) {
 +          val.getLock().lock();
 +          // add the TXStateProxy back to the map
 +          // in-case another thread removed it while we were waiting to lock.
 +          // This can happen during client transaction failover.
 +          synchronized (this.hostedTXStates) {
 +            this.hostedTXStates.put(key, val);
 +          }
 +        }
 +      }
 +      setTXState(val);
 +    }
 +    return val;
 +  }
 +  
 +  
 +  
 +  
 +  /**
 +   * Associate the transactional state with this thread.
 +   * @param txState the transactional state.
 +   */
 +  public void masqueradeAs(TXStateProxy txState) {
 +    assert txState != null;
 +    if (!txState.getLock().isHeldByCurrentThread()) {
 +      txState.getLock().lock();
 +    }
 +    setTXState(txState);
 +  }
 +
 +  /**
 +   * Remove the association created by {@link #masqueradeAs(TransactionMessage)}
 +   * @param tx
 +   */
 +  public void unmasquerade(TXStateProxy tx) {
 +    if (tx != null) {
 +      setTXState(null);
 +      tx.getLock().unlock();
 +    }
 +  }
 +
 +  /**
 +   * Cleanup the remote txState after commit and rollback
 +   * @param txId  
 +   * @return the TXStateProxy
 +   */
 +  public TXStateProxy removeHostedTXState(TXId txId) {
 +    synchronized (this.hostedTXStates) {
 +      TXStateProxy result = this.hostedTXStates.remove(txId);
 +      if (result != null) {
 +        result.close();
 +      }
 +      return result;
 +    }
 +  }
 +  
 +  /**
 +   * Called when the CacheServer is shutdown.
 +   * Removes txStates hosted on client's behalf
 +   */
 +  protected void removeHostedTXStatesForClients() {
 +    synchronized (this.hostedTXStates) {
 +      Iterator<Entry<TXId, TXStateProxy>> iterator = this.hostedTXStates.entrySet().iterator();
 +      while (iterator.hasNext()) {
 +        Entry<TXId, TXStateProxy> entry = iterator.next();
 +        if (entry.getValue().isOnBehalfOfClient()) {
 +          entry.getValue().close();
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("Cleaning up TXStateProxy for {}", entry.getKey());
 +          }
 +          iterator.remove();
 +        }
 +      }
 +    }
 +  }
 +  
 +  /**
 +   * Used to verify if a transaction with a given id is hosted by this txManager.
 +   * @param txId
 +   * @return true if the transaction is in progress, false otherwise
 +   */
 +  public boolean isHostedTxInProgress(TXId txId) {
 +    synchronized (this.hostedTXStates) {
 +      TXStateProxy tx = this.hostedTXStates.get(txId);
 +      if (tx == null) {
 +        return false;
 +      }
 +      return tx.isRealDealLocal();
 +    }
 +  }
 +
 +  public TXStateProxy getHostedTXState(TXId txId) {
 +    synchronized (this.hostedTXStates) {
 +      return this.hostedTXStates.get(txId);
 +    }
 +  }
 +  
 +  /**
 +   * @return number of transaction in progress on behalf of remote nodes
 +   */
 +  public int hostedTransactionsInProgressForTest() {
 +    synchronized (this.hostedTXStates) {
 +      return this.hostedTXStates.size();
 +    }
 +  }
 +  public int localTransactionsInProgressForTest() {
 +    return this.localTxMap.size();
 +  }
 +
 +  public void memberDeparted(InternalDistributedMember id, boolean crashed) {
 +    synchronized (this.hostedTXStates) {
 +      Iterator<Map.Entry<TXId,TXStateProxy>> iterator = this.hostedTXStates.entrySet().iterator();
 +      while (iterator.hasNext()) {
 +        Map.Entry<TXId,TXStateProxy> me = iterator.next();
 +        TXId txId = me.getKey();
 +        if (txId.getMemberId().equals(id)) {
 +          me.getValue().close();
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("Received memberDeparted, cleaning up txState:{}", txId);
 +          }
 +          iterator.remove();
 +        }
 +      }
 +    }
 +  }
 +
 +  public void memberJoined(InternalDistributedMember id) {
 +  }
 +
 +  public void quorumLost(Set<InternalDistributedMember> failures, List<InternalDistributedMember> remaining) {
 +  }
 +
 +  public void memberSuspect(InternalDistributedMember id,
 +      InternalDistributedMember whoSuspected, String reason) {
 +  }
 +  
 +
 +  /**
 +   * retrieve the transaction states for the given client
 +   * @param id the client's membership ID
 +   * @return a set of the currently open transaction states
 +   */
 +  public Set<TXId> getTransactionsForClient(InternalDistributedMember id) {
 +    Set<TXId> result = new HashSet<TXId>();
 +    synchronized (this.hostedTXStates) {
 +      for (Map.Entry<TXId, TXStateProxy> entry: this.hostedTXStates.entrySet()) {
 +        if (entry.getKey().getMemberId().equals(id)) {
 +          result.add(entry.getKey());
 +        }
 +      }
 +    }
 +    return result;
 +  }
 +
 +  /** remove the given TXStates */
 +  public void removeTransactions(Set<TXId> txIds, boolean distribute) {
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("expiring the following transactions: {}", txIds);
 +    }
 +    synchronized (this.hostedTXStates) {
 +      Iterator<Map.Entry<TXId, TXStateProxy>> iterator = this.hostedTXStates.entrySet().iterator();
 +      while (iterator.hasNext()) {
 +        Map.Entry<TXId,TXStateProxy> entry = iterator.next();
 +        if (txIds.contains(entry.getKey())) {
 +          entry.getValue().close();
 +          iterator.remove();
 +        }
 +      }
 +    }
 +    if (distribute) {
 +      // tell other VMs to also remove the transactions
 +      TXRemovalMessage.send(this.dm, this.dm.getOtherDistributionManagerIds(), txIds);
 +    }
 +  }
 +
 +  private void saveTXStateForClientFailover(TXStateProxy tx) {
 +    if (tx.isOnBehalfOfClient() && tx.isRealDealLocal()) {
 +      failoverMap.put(tx.getTxId(), tx.getCommitMessage());
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("TX: storing client initiated transaction:{}; now there are {} entries in the failoverMap",
 +            tx.getTxId(), failoverMap.size());
 +      }
 +    }
 +  }
 +
 +  private void saveTXStateForClientFailover(TXStateProxy tx, TXCommitMessage msg) {
 +    if (tx.isOnBehalfOfClient() && tx.isRealDealLocal()) {
 +      failoverMap.put(tx.getTxId(), msg);
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("TX: storing client initiated transaction:{}; now there are {} entries in the failoverMap",
 +            tx.getTxId(), failoverMap.size());
 +      }
 +    }
 +  }
 +
 +  public void saveTXCommitMessageForClientFailover(TXId txId, TXCommitMessage msg) {
 +    failoverMap.put(txId, msg);
 +  }
 +  
 +  public boolean isHostedTxRecentlyCompleted(TXId txId) {
 +    // if someone is asking to see if we have the txId, they will come
 +    // back and ask for the commit message, this could take a long time
 +    // specially when called from TXFailoverCommand, so we move
 +    // the txId to the front of the queue
 +    TXCommitMessage msg = failoverMap.remove(txId);
 +    if (msg != null) {
 +      failoverMap.put(txId, msg);
 +      return true;
 +    }
 +    return false;
 +  }
 +  
 +  
 +  /**
 +   * If the given transaction is already being completed by another thread
 +   * this will wait for that completion to finish and will ensure that
 +   * the result is saved in the client failover map.
 +   * @param txId
 +   * @return true if a wait was performed
 +   */
 +  public boolean waitForCompletingTransaction(TXId txId) {
 +    TXStateProxy val;
 +    val = this.hostedTXStates.get(txId);
 +    if (val == null) {
 +      synchronized(this.hostedTXStates) {
 +        val = this.hostedTXStates.get(txId);
 +      }
 +    }
 +    if (val != null && val.isRealDealLocal()) {
 +      TXStateProxyImpl impl = (TXStateProxyImpl)val;
 +      TXState state = impl.getLocalRealDeal();
 +      if (state.waitForPreviousCompletion()) {
 +        // the thread we were waiting for would have put a TXCommitMessage
 +        // in the failover map, doing so here may replace an existing token
 +        // like TXCommitMessage.REBALANCE_MSG with null. fixes bug 42661
 +        //saveTXStateForClientFailover(impl);
 +        return true;
 +      }
 +    }
 +    return false;
 +  }
 +  
 +  /**
 +   * Returns the TXCommitMessage for a transaction that has been
 +   * successfully completed.
 +   * @param txId
 +   * @return the commit message or an exception token e.g 
 +   * {@link TXCommitMessage#CMT_CONFLICT_MSG} if the transaction
 +   * threw an exception
 +   * @see #isExceptionToken(TXCommitMessage)
 +   */
 +  public TXCommitMessage getRecentlyCompletedMessage(TXId txId) {
 +    return failoverMap.get(txId);
 +  }
 +
 +  /**
 +   * @param msg
 +   * @return true if msg is an exception token, false otherwise
 +   */
 +  public boolean isExceptionToken(TXCommitMessage msg) {
 +    if (msg == TXCommitMessage.CMT_CONFLICT_MSG
 +        || msg == TXCommitMessage.REBALANCE_MSG
 +        || msg == TXCommitMessage.EXCEPTION_MSG) {
 +      return true;
 +    }
 +    return false;
 +  }
 +  
 +  /**
 +   * Generates exception messages for the three TXCommitMessage tokens that represent
 +   * exceptions during transaction execution. 
 +   * @param msg the token that represents the exception
 +   * @param txId
 +   * @return the exception
 +   */
 +  public RuntimeException getExceptionForToken(TXCommitMessage msg, TXId txId) {
 +    if (msg == TXCommitMessage.CMT_CONFLICT_MSG) {
 +      return new CommitConflictException(LocalizedStrings.
 +            TXState_CONFLICT_DETECTED_IN_GEMFIRE_TRANSACTION_0.toLocalizedString(txId));
 +    }
 +    if (msg == TXCommitMessage.REBALANCE_MSG) {
 +      return new TransactionDataRebalancedException(LocalizedStrings.
 +          PartitionedRegion_TRANSACTIONAL_DATA_MOVED_DUE_TO_REBALANCING.toLocalizedString());
 +    }
 +    if (msg == TXCommitMessage.EXCEPTION_MSG) {
 +      return new TransactionInDoubtException(LocalizedStrings.
 +          ClientTXStateStub_COMMIT_FAILED_ON_SERVER.toLocalizedString());
 +    }
 +    throw new InternalGemFireError("the parameter TXCommitMessage is not an exception token");
 +  }
 +  
 +  public static class TXRemovalMessage extends HighPriorityDistributionMessage {
 +
 +    Set<TXId> txIds;
 +
 +    /** for deserialization */
 +    public TXRemovalMessage() {
 +    }
 +
 +    static void send(DM dm, Set recipients, Set<TXId> txIds) {
 +      TXRemovalMessage msg = new TXRemovalMessage();
 +      msg.txIds = txIds;
 +      msg.setRecipients(recipients);
 +      dm.putOutgoing(msg);
 +    }
 +    
 +    @Override
 +    public void toData(DataOutput out) throws IOException {
 +      DataSerializer.writeHashSet((HashSet<TXId>)this.txIds, out);
 +    }
 +    
 +    @Override
 +    public void fromData(DataInput in) throws IOException, ClassNotFoundException {
 +      this.txIds = DataSerializer.readHashSet(in);
 +    }
 +
 +    public int getDSFID() {
 +      return TX_MANAGER_REMOVE_TRANSACTIONS;
 +    }
 +
 +    @Override
 +    protected void process(DistributionManager dm) {
 +      GemFireCacheImpl cache = GemFireCacheImpl.getInstance();
 +      if (cache != null) {
 +        TXManagerImpl mgr = cache.getTXMgr();
 +        mgr.removeTransactions(this.txIds, false);
 +      }
 +    }
 +    
 +  }
 +
 +  private ConcurrentMap<TransactionId, TXStateProxy> suspendedTXs = new ConcurrentHashMap<TransactionId, TXStateProxy>();
 +  
 +  public TransactionId suspend() {
 +    return suspend(TimeUnit.MINUTES);
 +  }
 +  
 +  TransactionId suspend(TimeUnit expiryTimeUnit) {
 +    TXStateProxy result = getTXState();
 +    if (result != null) {
 +      TransactionId txId = result.getTransactionId();
 +      internalSuspend();
 +      this.suspendedTXs.put(txId, result);
 +      // wake up waiting threads
 +      Queue<Thread> waitingThreads = this.waitMap.get(txId);
 +      if (waitingThreads != null) {
 +        Thread waitingThread = null;
 +        while (true) {
 +          waitingThread = waitingThreads.poll();
 +          if (waitingThread == null
 +              || !Thread.currentThread().equals(waitingThread)) {
 +            break;
 +          }
 +        }
 +        if (waitingThread != null) {
 +          LockSupport.unpark(waitingThread);
 +        }
 +      }
 +      scheduleExpiry(txId, expiryTimeUnit);
 +      return txId;
 +    }
 +    return null;
 +  }
 +
 +  public void resume(TransactionId transactionId) {
 +    if (transactionId == null) {
 +      throw new IllegalStateException(
 +          LocalizedStrings.TXManagerImpl_UNKNOWN_TRANSACTION_OR_RESUMED
 +              .toLocalizedString());
 +    }
 +    if (getTXState() != null) {
 +      throw new IllegalStateException(
 +          LocalizedStrings.TXManagerImpl_TRANSACTION_ACTIVE_CANNOT_RESUME
 +              .toLocalizedString());
 +    }
 +    TXStateProxy txProxy = this.suspendedTXs.remove(transactionId);
 +    if (txProxy == null) {
 +      throw new IllegalStateException(
 +          LocalizedStrings.TXManagerImpl_UNKNOWN_TRANSACTION_OR_RESUMED
 +              .toLocalizedString());
 +    }
 +    resume(txProxy);
 +  }
 +
 +  public boolean isSuspended(TransactionId transactionId) {
 +    return this.suspendedTXs.containsKey(transactionId);
 +  }
 +  
 +  public boolean tryResume(TransactionId transactionId) {
 +    if (transactionId == null || getTXState() != null) {
 +      return false;
 +    }
 +    TXStateProxy txProxy = this.suspendedTXs.remove(transactionId);
 +    if (txProxy != null) {
 +      resume(txProxy);
 +      return true;
 +    }
 +    return false;
 +  }
 +
 +  /**
 +   * this map keeps track of all the threads that are waiting in
 +   * {@link #tryResume(TransactionId, long, TimeUnit)} for a particular
 +   * transactionId
 +   */
 +  private ConcurrentMap<TransactionId, Queue<Thread>> waitMap = new ConcurrentHashMap<TransactionId, Queue<Thread>>();
 +  
 +  public boolean tryResume(TransactionId transactionId, long time, TimeUnit unit) {
 +    if (transactionId == null || getTXState() != null || !exists(transactionId)) {
 +      return false;
 +    }
 +    Thread currentThread = Thread.currentThread();
 +    long timeout = unit.toNanos(time);
 +    long startTime = System.nanoTime();
 +    Queue<Thread> threadq = null;
 +
 +    try {
 +      while (true) {
 +        threadq = waitMap.get(transactionId);
 +        if (threadq == null) {
 +          threadq = new ConcurrentLinkedQueue<Thread>();
 +          Queue<Thread> oldq = waitMap.putIfAbsent(transactionId, threadq);
 +          if (oldq != null) {
 +            threadq = oldq;
 +          }
 +        }
 +        threadq.add(currentThread);
 +        // after putting this thread in waitMap, we should check for
 +        // an entry in suspendedTXs. if no entry is found in suspendedTXs
 +        // next invocation of suspend() will unblock this thread
 +        if (tryResume(transactionId)) {
 +          return true;
 +        } else if (!exists(transactionId)) {
 +          return false;
 +        }
 +        LockSupport.parkNanos(timeout);
 +        long nowTime = System.nanoTime();
 +        timeout -= nowTime - startTime;
 +        startTime = nowTime;
 +        if (timeout <= 0) {
 +          break;
 +        }
 +      }
 +    } finally {
 +      threadq = waitMap.get(transactionId);
 +      if (threadq != null) {
 +        threadq.remove(currentThread);
 +        // the queue itself will be removed at commit/rollback
 +      }
 +    }
 +    return false;
 +  }
 +
 +  public boolean exists(TransactionId transactionId) {
 +    return isHostedTxInProgress((TXId) transactionId)
 +        || isSuspended(transactionId)
 +        || this.localTxMap.containsKey(transactionId);
 +  }
 +
 +  /**
 +   * The timeout after which any suspended transactions are
 +   * rolled back if they are not resumed. If a negative
 +   * timeout is passed, suspended transactions will never expire.
 +   * @param timeout the timeout in minutes
 +   */
 +  public void setSuspendedTransactionTimeout(long timeout) {
 +    this.suspendedTXTimeout = timeout;
 +  }
 +
 +  /**
 +   * Return the timeout after which suspended transactions
 +   * are rolled back.
 +   * @return the timeout in minutes
 +   * @see #setSuspendedTransactionTimeout(long)
 +   */
 +  public long getSuspendedTransactionTimeout() {
 +    return this.suspendedTXTimeout;
 +  }
 +  
 +  /**
 +   * map to track the scheduled expiry tasks of suspended transactions.
 +   */
 +  private ConcurrentMap<TransactionId, SystemTimerTask> expiryTasks = new ConcurrentHashMap<TransactionId, SystemTimerTask>();
 +  
 +  /**
 +   * schedules the transaction to expire after {@link #suspendedTXTimeout}
 +   * @param txId
 +   * @param expiryTimeUnit the time unit to use when scheduling the expiration
 +   */
 +  private void scheduleExpiry(TransactionId txId, TimeUnit expiryTimeUnit) {
 +    final GemFireCacheImpl cache = (GemFireCacheImpl) this.cache;
 +    if (suspendedTXTimeout < 0) {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("TX: transaction: {} not scheduled to expire", txId);
 +      }
 +      return;
 +    }
 +    SystemTimerTask task = new TXExpiryTask(txId);
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("TX: scheduling transaction: {} to expire after:{}", txId, suspendedTXTimeout);
 +    }
 +    cache.getCCPTimer().schedule(task, TimeUnit.MILLISECONDS.convert(suspendedTXTimeout, expiryTimeUnit));
 +    this.expiryTasks.put(txId, task);
 +  }
 +
 +  /**
 +   * Task scheduled to expire a transaction when it is suspended.
 +   * This task gets canceled if the transaction is resumed.
 +   * @author sbawaska
 +   */
 +  public static class TXExpiryTask extends SystemTimerTask {
 +
 +    /**
 +     * The txId to expire
 +     */
 +    private final TransactionId txId;
 +    
 +    public TXExpiryTask(TransactionId txId) {
 +      this.txId = txId;
 +    }
 +    @Override
 +    public void run2() {
 +      TXManagerImpl mgr = TXManagerImpl.currentInstance;
 +      TXStateProxy tx = mgr.suspendedTXs.remove(txId);
 +      if (tx != null) {
 +        try {
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("TX: Expiry task rolling back transaction: {}", txId);
 +          }
 +          tx.rollback();
 +        } catch (GemFireException e) {
 +          logger.warn(LocalizedMessage.create(LocalizedStrings.TXManagerImpl_EXCEPTION_IN_TRANSACTION_TIMEOUT, txId), e);
 +        }
 +      }
 +    }
 +  }
 +  private static class RefCountMapEntryCreator implements CustomEntryConcurrentHashMap.HashEntryCreator<AbstractRegionEntry, RefCountMapEntry> {
 +    @Override
 +    public HashEntry<AbstractRegionEntry, RefCountMapEntry> newEntry(AbstractRegionEntry key, int hash,
 +        HashEntry<AbstractRegionEntry, RefCountMapEntry> next, RefCountMapEntry value) {
 +      value.setNextEntry(next);
 +      return value;
 +    }
 +
 +    @Override
 +    public int keyHashCode(Object key, boolean compareValues) {
 +      // key will always be an AbstractRegionEntry because our map is strongly typed.
 +      return ((AbstractRegionEntry) key).getEntryHash();
 +    }
 +  }
 +  private static class RefCountMapEntry implements HashEntry<AbstractRegionEntry, RefCountMapEntry> {
 +    private final AbstractRegionEntry key;
 +    private HashEntry<AbstractRegionEntry, RefCountMapEntry> next;
 +    private volatile int refCount;
 +    private static final AtomicIntegerFieldUpdater<RefCountMapEntry> refCountUpdater
 +      = AtomicIntegerFieldUpdater.newUpdater(RefCountMapEntry.class, "refCount");    
 +    public RefCountMapEntry(AbstractRegionEntry k) {
 +      this.key = k;
 +      this.refCount = 1;
 +    }
 +
 +    @Override
 +    public AbstractRegionEntry getKey() {
 +      return this.key;
 +    }
 +
 +    @Override
 +    public boolean isKeyEqual(Object k) {
 +      return this.key.equals(k);
 +    }
 +
 +    @Override
 +    public RefCountMapEntry getMapValue() {
 +      return this;
 +    }
 +
 +    @Override
 +    public void setMapValue(RefCountMapEntry newValue) {
 +      if (newValue != this) {
 +        throw new IllegalStateException("Expected newValue " + newValue + " to be this " + this);
 +      }
 +    }
 +
 +    @Override
 +    public int getEntryHash() {
 +      return this.key.getEntryHash();
 +    }
 +
 +    @Override
 +    public HashEntry<AbstractRegionEntry, RefCountMapEntry> getNextEntry() {
 +      return this.next;
 +    }
 +
 +    @Override
 +    public void setNextEntry(HashEntry<AbstractRegionEntry, RefCountMapEntry> n) {
 +      this.next = n;
 +    }
 +
 +    public void incRefCount() {
 +      refCountUpdater.addAndGet(this, 1);
 +    }
 +
 +    /**
 +     * Returns true if refCount goes to 0.
 +     */
 +    public boolean decRefCount() {
 +      int rc = refCountUpdater.decrementAndGet(this);
 +      if (rc < 0) {
 +        throw new IllegalStateException("rc=" + rc);
 +      }
 +      return rc == 0;
 +    }
 +  }
 +  
 +  private final CustomEntryConcurrentHashMap<AbstractRegionEntry, RefCountMapEntry> refCountMap
 +    = new CustomEntryConcurrentHashMap<AbstractRegionEntry, RefCountMapEntry>(
 +        CustomEntryConcurrentHashMap.DEFAULT_INITIAL_CAPACITY, 
 +        CustomEntryConcurrentHashMap.DEFAULT_LOAD_FACTOR,
 +        CustomEntryConcurrentHashMap.DEFAULT_CONCURRENCY_LEVEL,
 +        true,
 +        new RefCountMapEntryCreator());
 +  
 +  private static final MapCallback<AbstractRegionEntry, RefCountMapEntry, Object, Object> incCallback = new MapCallback<AbstractRegionEntry, RefCountMapEntry, Object, Object>() {
 +    @Override
 +    public RefCountMapEntry newValue(AbstractRegionEntry key, Object context,
 +        Object createParams) {
 +      return new RefCountMapEntry(key);
 +    }
 +    @Override
 +    public void oldValueRead(RefCountMapEntry value) {
 +      value.incRefCount();
 +    }
 +    @Override
 +    public boolean doRemoveValue(RefCountMapEntry value, Object context,
 +        Object removeParams) {
 +      throw new IllegalStateException("doRemoveValue should not be called from create");
 +    }
 +  };
 +  
 +  private static final MapCallback<AbstractRegionEntry, RefCountMapEntry, Object, Object> decCallback = new MapCallback<AbstractRegionEntry, RefCountMapEntry, Object, Object>() {
 +    @Override
 +    public RefCountMapEntry newValue(AbstractRegionEntry key, Object context,
 +        Object createParams) {
 +      throw new IllegalStateException("newValue should not be called from remove");
 +    }
 +    @Override
 +    public void oldValueRead(RefCountMapEntry value) {
 +      throw new IllegalStateException("oldValueRead should not be called from remove");
 +    }
 +    @Override
 +    public boolean doRemoveValue(RefCountMapEntry value, Object context,
 +        Object removeParams) {
 +      return value.decRefCount();
 +    }
 +  };
 +  
 +  public static final void incRefCount(AbstractRegionEntry re) {
 +    TXManagerImpl mgr = currentInstance;
 +    if (mgr != null) {
 +      mgr.refCountMap.create(re, incCallback, null, null, true);
 +    }
 +  }
 +  /**
 +   * Return true if refCount went to zero.
 +   */
 +  public static final boolean decRefCount(AbstractRegionEntry re) {
 +    TXManagerImpl mgr = currentInstance;
 +    if (mgr != null) {
 +      return mgr.refCountMap.removeConditionally(re, decCallback, null, null) != null;
 +    } else {
 +      return true;
 +    }
 +  }
 +
 +  // Used by tests
 +  public Set<TXId> getLocalTxIds() {
 +    return this.localTxMap.keySet();
 +  }
 +
 +  // Used by tests
 +  public ArrayList<TXId> getHostedTxIds() {
 +    synchronized (this.hostedTXStates) {
 +      return new ArrayList<TXId>(this.hostedTXStates.keySet());
 +    }
 +  }
 +  
 +  public void setDistributed(boolean flag) {
 +    checkClosed();
 +    TXStateProxy tx = getTXState();
 +    // Check whether given flag and current flag are different and whether a transaction is in progress
 +    if (tx != null && flag != isDistributed()) {
 +      // Cannot change mode in the middle of a transaction
 +      throw new java.lang.IllegalStateException(
 +          LocalizedStrings.TXManagerImpl_CANNOT_CHANGE_TRANSACTION_MODE_WHILE_TRANSACTIONS_ARE_IN_PROGRESS
 +              .toLocalizedString());
 +    } else {
 +      isTXDistributed.set(new Boolean(flag));
 +    }
 +  }
 +
 +  /*
 +   * If explicitly set using setDistributed, this returns that value.
 +   * If not, it returns the value of gemfire property "distributed-transactions" if set.
 +   * If this is also not set, it returns the default value of this property.
 +   */
 +  public boolean isDistributed() {
 +    
 +     Boolean value = isTXDistributed.get();
 +    // This can be null if not set in setDistributed().
 +    if (value == null) {
 +      return InternalDistributedSystem.getAnyInstance().getOriginalConfig().getDistributedTransactions();
 +    } else {
 +      return value.booleanValue();
 +    }
 +  }
 +  
 +}


[21/51] [abbrv] [partial] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-917

Posted by up...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/5beaaedc/geode-core/src/main/java/com/gemstone/gemfire/internal/tcp/Connection.java
----------------------------------------------------------------------
diff --cc geode-core/src/main/java/com/gemstone/gemfire/internal/tcp/Connection.java
index 74660da,0000000..988ca33
mode 100644,000000..100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/tcp/Connection.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/tcp/Connection.java
@@@ -1,4153 -1,0 +1,4154 @@@
 +/*
 + * 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.tcp;
 +
 +import java.io.BufferedInputStream;
 +import java.io.ByteArrayOutputStream;
 +import java.io.DataInputStream;
 +import java.io.DataOutputStream;
 +import java.io.IOException;
 +import java.io.InputStream;
 +import java.io.InterruptedIOException;
 +import java.io.OutputStream;
 +import java.net.ConnectException;
 +import java.net.Inet6Address;
 +import java.net.InetSocketAddress;
 +import java.net.Socket;
 +import java.net.SocketException;
 +import java.net.SocketTimeoutException;
 +import java.nio.ByteBuffer;
 +import java.nio.channels.CancelledKeyException;
 +import java.nio.channels.ClosedChannelException;
 +import java.nio.channels.ClosedSelectorException;
 +import java.nio.channels.SocketChannel;
 +import java.util.Collections;
 +import java.util.HashMap;
 +import java.util.Iterator;
 +import java.util.LinkedList;
 +import java.util.List;
 +import java.util.Map;
 +import java.util.concurrent.Semaphore;
 +import java.util.concurrent.atomic.AtomicBoolean;
 +import java.util.concurrent.atomic.AtomicLong;
 +
 +import org.apache.logging.log4j.Logger;
 +
 +import com.gemstone.gemfire.CancelException;
 +import com.gemstone.gemfire.SystemFailure;
 +import com.gemstone.gemfire.cache.CacheClosedException;
 +import com.gemstone.gemfire.distributed.DistributedMember;
 +import com.gemstone.gemfire.distributed.DistributedSystemDisconnectedException;
 +import com.gemstone.gemfire.distributed.internal.ConflationKey;
 +import com.gemstone.gemfire.distributed.internal.DM;
 +import com.gemstone.gemfire.distributed.internal.DMStats;
 +import com.gemstone.gemfire.distributed.internal.DirectReplyProcessor;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfig;
 +import com.gemstone.gemfire.distributed.internal.DistributionConfigImpl;
 +import com.gemstone.gemfire.distributed.internal.DistributionManager;
 +import com.gemstone.gemfire.distributed.internal.DistributionMessage;
 +import com.gemstone.gemfire.distributed.internal.DistributionStats;
 +import com.gemstone.gemfire.distributed.internal.ReplyException;
 +import com.gemstone.gemfire.distributed.internal.ReplyMessage;
 +import com.gemstone.gemfire.distributed.internal.ReplyProcessor21;
 +import com.gemstone.gemfire.distributed.internal.ReplySender;
 +import com.gemstone.gemfire.distributed.internal.direct.DirectChannel;
 +import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
 +import com.gemstone.gemfire.distributed.internal.membership.MembershipManager;
 +import com.gemstone.gemfire.i18n.StringId;
 +import com.gemstone.gemfire.internal.Assert;
 +import com.gemstone.gemfire.internal.ByteArrayDataInput;
 +import com.gemstone.gemfire.internal.DSFIDFactory;
 +import com.gemstone.gemfire.internal.InternalDataSerializer;
 +import com.gemstone.gemfire.internal.SocketCreator;
 +import com.gemstone.gemfire.internal.SocketUtils;
 +import com.gemstone.gemfire.internal.SystemTimer;
 +import com.gemstone.gemfire.internal.SystemTimer.SystemTimerTask;
 +import com.gemstone.gemfire.internal.Version;
 +import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 +import com.gemstone.gemfire.internal.logging.LogService;
 +import com.gemstone.gemfire.internal.logging.LoggingThreadGroup;
 +import com.gemstone.gemfire.internal.logging.log4j.AlertAppender;
 +import com.gemstone.gemfire.internal.logging.log4j.LocalizedMessage;
 +import com.gemstone.gemfire.internal.tcp.MsgReader.Header;
 +import com.gemstone.gemfire.internal.util.concurrent.ReentrantSemaphore;
 +
 +/** <p>Connection is a socket holder that sends and receives serialized
 +    message objects.  A Connection may be closed to preserve system
 +    resources and will automatically be reopened when it's needed.</p>
 +
 +    @author Bruce Schuchardt
 +    @since 2.0
 +
 +*/
 +
 +public class Connection implements Runnable {
 +  private static final Logger logger = LogService.getLogger();
 +
 +  private static final int INITIAL_CAPACITY = Integer.getInteger("p2p.readerBufferSize", 32768).intValue();
 +  private static int P2P_CONNECT_TIMEOUT;
 +  private static boolean IS_P2P_CONNECT_TIMEOUT_INITIALIZED = false; 
 +  
 +  public final static int NORMAL_MSG_TYPE = 0x4c;
 +  public final static int CHUNKED_MSG_TYPE = 0x4d; // a chunk of one logical msg
 +  public final static int END_CHUNKED_MSG_TYPE = 0x4e; // last in a series of chunks
 +  public final static int DIRECT_ACK_BIT = 0x20;
 +  //We no longer support early ack
 +  //public final static int EARLY_ACK_BIT = 0x10;
 +
 +  public static final int MSG_HEADER_SIZE_OFFSET = 0;
 +  public static final int MSG_HEADER_TYPE_OFFSET = 4;
 +  public static final int MSG_HEADER_ID_OFFSET = 5;
 +  public static final int MSG_HEADER_BYTES = 7;
 +
 +  /**
 +   * Small buffer used for send socket buffer on receiver connections
 +   * and receive buffer on sender connections.
 +   */
 +  public final static int SMALL_BUFFER_SIZE = Integer.getInteger("gemfire.SMALL_BUFFER_SIZE",4096).intValue();
 +
 +  /** counter to give connections a unique id */
 +  private static AtomicLong idCounter = new AtomicLong(1);
 +
 +  /** string used as the reason for initiating suspect processing */
 +  public static final String INITIATING_SUSPECT_PROCESSING = "member unexpectedly shut down shared, unordered connection";
 +
 +  /** the table holding this connection */
 +  final ConnectionTable owner;
 +  
 +  /** Set to false once run() is terminating. Using this instead of Thread.isAlive  
 +    * as the reader thread may be a pooled thread.
 +    */ 
 +  private volatile boolean isRunning = false; 
 +
 +  /** true if connection is a shared resource that can be used by more than one thread */
 +  private boolean sharedResource;
 +
 +  public final boolean isSharedResource() {
 +    return this.sharedResource;
 +  }
 +  
 +  /** The idle timeout timer task for this connection */
 +  private SystemTimerTask idleTask;
 +  
 +  /**
 +   * Returns the depth of unshared reader threads from this thread to
 +   * the original non-reader-thread.
 +   * E.g., ServerConnection -> reader(domino=1) -> reader(domino=2) -> reader(domino=3)
 +   */
 +  public static int getDominoCount() {
 +    return dominoCount.get().intValue();
 +  }
 +
 +  private final static ThreadLocal isReaderThread = new ThreadLocal();
 +  public final static void makeReaderThread() {
 +    // mark this thread as a reader thread
 +    makeReaderThread(true);
 +  }
 +  private final static void makeReaderThread(boolean v) {
 +    isReaderThread.set(v);
 +  }
 +  // return true if this thread is a reader thread
 +  public final static boolean isReaderThread() {
 +    Object o = isReaderThread.get();
 +    if (o == null) {
 +      return false;
 +    } else {
 +      return ((Boolean)o).booleanValue();
 +    }
 +  }
 +
 +  private int getP2PConnectTimeout() {
 +    if(IS_P2P_CONNECT_TIMEOUT_INITIALIZED)
 +      return P2P_CONNECT_TIMEOUT;
 +    String connectTimeoutStr = System.getProperty("p2p.connectTimeout");
 +    if(connectTimeoutStr != null) {
 +      P2P_CONNECT_TIMEOUT =  Integer.parseInt(connectTimeoutStr);
 +    }
 +    else {      
 +      P2P_CONNECT_TIMEOUT = 6*this.owner.owner.getDM().getConfig().getMemberTimeout();
 +    }
 +    IS_P2P_CONNECT_TIMEOUT_INITIALIZED = true;
 +    return P2P_CONNECT_TIMEOUT;     
 +  }
 +  /**
 +   * If true then readers for thread owned sockets will send all messages on thread owned senders.
 +   * Even normally unordered msgs get send on TO socks.
 +   */
 +  private static final boolean DOMINO_THREAD_OWNED_SOCKETS = Boolean.getBoolean("p2p.ENABLE_DOMINO_THREAD_OWNED_SOCKETS");
 +  private final static ThreadLocal isDominoThread = new ThreadLocal();
 +  // return true if this thread is a reader thread
 +  public final static boolean tipDomino() {
 +    if (DOMINO_THREAD_OWNED_SOCKETS) {
 +      // mark this thread as one who wants to send ALL on TO sockets
 +      ConnectionTable.threadWantsOwnResources();
 +      isDominoThread.set(Boolean.TRUE);
 +      return true;
 +    } else {
 +      return false;
 +    }
 +  }
 +  public final static boolean isDominoThread() {
 +    Object o = isDominoThread.get();
 +    if (o == null) {
 +      return false;
 +    } else {
 +      return ((Boolean)o).booleanValue();
 +    }
 +  }
 +
 +  /** the socket entrusted to this connection */
 +  private final Socket socket;
 +
 +  /** the non-NIO output stream */
 +  OutputStream output;
 +
 +  /** output stream/channel lock */
 +  private final Object outLock = new Object();
 +
 +  /** the ID string of the conduit (for logging) */
 +  String conduitIdStr;
 +
 +  /** Identifies the java group member on the other side of the connection. */
 +  InternalDistributedMember remoteAddr;
 +
 +  /**
 +   * Identifies the version of the member on the other side of the connection.
 +   */
 +  Version remoteVersion;
 +
 +  /**
 +   * True if this connection was accepted by a listening socket.
 +   * This makes it a receiver.
 +   * False if this connection was explicitly created by a connect call.
 +   * This makes it a sender.
 +   */
 +  private final boolean isReceiver;
 +  
 +  /**
 +   * count of how many unshared p2p-readers removed from the original action this
 +   * thread is.  For instance, server-connection -> owned p2p reader (count 0)
 +   * -> owned p2p reader (count 1) -> owned p2p reader (count 2).  This shows
 +   * up in thread names as "DOM #x" (domino #x)
 +   */
 +  private static ThreadLocal<Integer> dominoCount = new ThreadLocal<Integer>() {
 +    @Override
 +    protected Integer initialValue() {
 +      return 0;
 +    }};
 +
 +//  /**
 +//   * name of sender thread thread.  Useful in finding out why a reader
 +//   * thread was created.  Add sending of the name in handshakes and
 +//   * add it to the name of the reader thread (the code is there but commented out)
 +//   */
 +//  private String senderName = null;
 +  
 +  // If we are a sender then we want to know if the receiver on the
 +  // other end is willing to have its messages queued. The following
 +  // four "async" inst vars come from his handshake response.
 +  /**
 +   * How long to wait if receiver will not accept a message before we
 +   * go into queue mode.
 +   * @since 4.2.2
 +   */
 +  private int asyncDistributionTimeout = 0;
 +  /**
 +   * How long to wait,
 +   * with the receiver not accepting any messages,
 +   * before kicking the receiver out of the distributed system.
 +   * Ignored if asyncDistributionTimeout is zero.
 +   * @since 4.2.2
 +   */
 +  private int asyncQueueTimeout = 0;
 +  /**
 +   * How much queued data we can have,
 +   * with the receiver not accepting any messages,
 +   * before kicking the receiver out of the distributed system.
 +   * Ignored if asyncDistributionTimeout is zero.
 +   * Canonicalized to bytes (property file has it as megabytes
 +   * @since 4.2.2
 +   */
 +  private long asyncMaxQueueSize = 0;
 +  /**
 +   * True if an async queue is already being filled.
 +   */
 +  private volatile boolean asyncQueuingInProgress = false;
 +
 +  /**
 +   * Maps ConflatedKey instances to ConflatedKey instance.
 +   * Note that even though the key and value for an entry is the map
 +   * will always be "equal" they will not always be "==".
 +   */
 +  private final Map conflatedKeys = new HashMap();
 +
 +  //private final Queue outgoingQueue = new LinkedBlockingQueue();
 +
 +
 +  // NOTE: LinkedBlockingQueue has a bug in which removes from the queue
 +  // cause future offer to increase the size without adding anything to the queue.
 +  // So I've changed from this backport class to a java.util.LinkedList
 +  private final LinkedList outgoingQueue = new LinkedList();
 +
 +  /**
 +   * Number of bytes in the outgoingQueue.
 +   * Used to control capacity.
 +   */
 +  private long queuedBytes = 0;
 +
 +  /** used for async writes */
 +  Thread pusherThread;
 +
 +  /**
 +   * The maximum number of concurrent senders sending a message to a single recipient.
 +   */
 +  private final static int MAX_SENDERS = Integer.getInteger("p2p.maxConnectionSenders", DirectChannel.DEFAULT_CONCURRENCY_LEVEL).intValue();
 +  /**
 +   * This semaphore is used to throttle how many threads will try to do sends on
 +   * this connection concurrently. A thread must acquire this semaphore before it
 +   * is allowed to start serializing its message.
 +   */
 +  private final Semaphore senderSem = new ReentrantSemaphore(MAX_SENDERS);
 +
 +  /** Set to true once the handshake has been read */
 +  volatile boolean handshakeRead = false;
 +  volatile boolean handshakeCancelled = false;
 +
 +  private volatile int replyCode = 0;
 +
 +  private static final byte REPLY_CODE_OK = (byte)69;
 +  private static final byte REPLY_CODE_OK_WITH_ASYNC_INFO = (byte)70;
 +
 +  private final Object handshakeSync = new Object();
 +
 +  /** message reader thread */
 +  private volatile Thread readerThread;
 +
 +//  /**
 +//   * When a thread owns the outLock and is writing to the socket, it must
 +//   * be placed in this variable so that it can be interrupted should the
 +//   * socket need to be closed.
 +//   */
 +//  private volatile Thread writerThread;
 +
 +  /** whether the reader thread is, or should be, running */
 +  volatile boolean stopped = true;
 +
 +  /** set to true once a close begins */
 +  private final AtomicBoolean closing = new AtomicBoolean(false);
 +
 +  volatile boolean readerShuttingDown = false;
 +
 +  /** whether the socket is connected */
 +  volatile boolean connected = false;
 +
 +  /**
 +   * Set to true once a connection finishes its constructor
 +   */
 +  volatile boolean finishedConnecting = false;
 +
 +  volatile boolean accessed = true;
 +  volatile boolean socketInUse = false;
 +  volatile boolean timedOut = false;
 +  
 +  /**
 +   * task for detecting ack timeouts and issuing alerts
 +   */
 +  private SystemTimer.SystemTimerTask ackTimeoutTask;
 +
 +  // State for ackTimeoutTask: transmissionStartTime, ackWaitTimeout, ackSATimeout, ackConnectionGroup, ackThreadName
 +  
 +  /** millisecond clock at the time message transmission started, if doing
 +   *  forced-disconnect processing */
 +  long transmissionStartTime;
 +  
 +  /** ack wait timeout - if socketInUse, use this to trigger SUSPECT processing */
 +  private long ackWaitTimeout;
 +
 +  /** ack severe alert timeout - if socketInUse, use this to send alert */
 +  private long ackSATimeout;
 +  
 +  /**
 +   * other connections participating in the current transmission.  we notify
 +   * them if ackSATimeout expires to keep all members from generating alerts
 +   * when only one is slow 
 +   */
 +  List ackConnectionGroup;
 +  
 +  /** name of thread that we're currently performing an operation in (may be null) */
 +  String ackThreadName;
 +  
 +
 +  /** the buffer used for NIO message receipt */
 +  ByteBuffer nioInputBuffer;
 +
 +  /** the position of the next message's content */
 +//  int nioMessageStart;
 +
 +  /** the length of the next message to be dispatched */
 +  int nioMessageLength;
 +//  byte nioMessageVersion;
 +
 +  /** the type of message being received */
 +  byte nioMessageType;
 +
 +  /** used to lock access to destreamer data */
 +  private final Object destreamerLock = new Object();
 +  /** caches a msg destreamer that is currently not being used */
 +  MsgDestreamer idleMsgDestreamer;
 +  /** used to map a msgId to a MsgDestreamer which are
 +    * used for destreaming chunked messages using nio */
 +  HashMap destreamerMap;
 +
 +  boolean directAck;
 +
 +  short nioMsgId;
 +
 +  /** whether the length of the next message has been established */
 +  boolean nioLengthSet = false;
 +
 +  /** is this connection used for serial message delivery? */
 +  boolean preserveOrder = false;
 +
 +  /** number of messages sent on this connection */
 +  private long messagesSent;
 +
 +  /** number of messages received on this connection */
 +  private long messagesReceived;
 +
 +  /** unique ID of this connection (remote if isReceiver==true) */
 +  private volatile long uniqueId;
 +
 +  private int sendBufferSize = -1;
 +  private int recvBufferSize = -1;
 +  
 +  private ReplySender replySender;
 +
 +  private void setSendBufferSize(Socket sock) {
 +    setSendBufferSize(sock, this.owner.getConduit().tcpBufferSize);
 +  }
 +  private void setReceiveBufferSize(Socket sock) {
 +    setReceiveBufferSize(sock, this.owner.getConduit().tcpBufferSize);
 +  }
 +  private void setSendBufferSize(Socket sock, int requestedSize) {
 +    setSocketBufferSize(sock, true, requestedSize);
 +  }
 +  private void setReceiveBufferSize(Socket sock, int requestedSize) {
 +    setSocketBufferSize(sock, false, requestedSize);
 +  }
 +  public int getReceiveBufferSize() {
 +    return recvBufferSize;
 +  }
 +  private void setSocketBufferSize(Socket sock, boolean send, int requestedSize) {
 +    setSocketBufferSize(sock, send, requestedSize, false);
 +  }
 +  private void setSocketBufferSize(Socket sock, boolean send, int requestedSize, boolean alreadySetInSocket) {
 +    if (requestedSize > 0) {
 +      try {
 +        int currentSize = send
 +          ? sock.getSendBufferSize()
 +          : sock.getReceiveBufferSize();
 +        if (currentSize == requestedSize) {
 +          if (send) {
 +            this.sendBufferSize = currentSize;
 +          }
 +          return;
 +        }
 +        if (!alreadySetInSocket) {
 +          if (send) {
 +            sock.setSendBufferSize(requestedSize);
 +          } else {
 +            sock.setReceiveBufferSize(requestedSize);
 +          }
 +        } else {
 +        }
 +      } catch (SocketException ignore) {
 +      }
 +      try {
 +        int actualSize = send
 +          ? sock.getSendBufferSize()
 +          : sock.getReceiveBufferSize();
 +        if (send) {
 +          this.sendBufferSize = actualSize;
 +        } else {
 +          this.recvBufferSize = actualSize;
 +        }
 +        if (actualSize < requestedSize) {
 +          logger.info(LocalizedMessage.create(LocalizedStrings.Connection_SOCKET_0_IS_1_INSTEAD_OF_THE_REQUESTED_2,
 +              new Object[] {(send ? "send buffer size" : "receive buffer size"), Integer.valueOf(actualSize), Integer.valueOf(requestedSize)}));
 +        } else if (actualSize > requestedSize) {
 +          if (logger.isTraceEnabled()) {
 +            logger.trace("Socket {} buffer size is {} instead of the requested {}",
 +                (send ? "send" : "receive"), actualSize, requestedSize);
 +          }
 +          // Remember the request size which is smaller.
 +          // This remembered value is used for allocating direct mem buffers.
 +          if (send) {
 +            this.sendBufferSize = requestedSize;
 +          } else {
 +            this.recvBufferSize = requestedSize;
 +          }
 +        }
 +      } catch (SocketException ignore) {
 +        if (send) {
 +          this.sendBufferSize = requestedSize;
 +        } else {
 +          this.recvBufferSize = requestedSize;
 +        }
 +      }
 +    }
 +  }
 +  /**
 +   * Returns the size of the send buffer on this connection's socket.
 +   */
 +  public int getSendBufferSize() {
 +    int result = this.sendBufferSize;
 +    if (result != -1) {
 +      return result;
 +    }
 +    try {
 +      result = getSocket().getSendBufferSize();
 +    } catch (SocketException ignore) {
 +      // just return a default
 +      result = this.owner.getConduit().tcpBufferSize;
 +    }
 +    this.sendBufferSize = result;
 +    return result;
 +  }
 +
 +  /** creates a connection that we accepted (it was initiated by
 +   * an explicit connect being done on the other side).
 +   * We will only receive data on this socket; never send.
 +   */
 +  protected static Connection createReceiver(ConnectionTable t, Socket s)
 +    throws IOException, ConnectionException
 +  {
 +    Connection c = new Connection(t, s);
 +    boolean readerStarted = false;
 +    try {
 +      c.startReader(t);
 +      readerStarted = true;
 +    } finally {
 +      if (!readerStarted) {
 +        c.closeForReconnect(LocalizedStrings.Connection_COULD_NOT_START_READER_THREAD.toLocalizedString());
 +      }
 +    }
 +    c.waitForHandshake();
 +    //sendHandshakeReplyOK();
 +    c.finishedConnecting = true;
 +    return c;
 +  }
 +  
 +  /** creates a connection that we accepted (it was initiated by
 +   * an explicit connect being done on the other side).
 +   */
 +  protected Connection(ConnectionTable t, Socket s)
 +    throws IOException, ConnectionException
 +  {
 +    if (t == null) {
 +      throw new IllegalArgumentException(LocalizedStrings.Connection_NULL_CONNECTIONTABLE.toLocalizedString());
 +    }
 +    this.isReceiver = true;
 +    this.owner = t;
 +    this.socket = s;
 +    this.conduitIdStr = owner.getConduit().getId().toString();
 +    this.handshakeRead = false;
 +    this.handshakeCancelled = false;
 +    this.connected = true;
 +
 +    try {
 +      s.setTcpNoDelay(true);
 +      s.setKeepAlive(true);
 +//      s.setSoLinger(true, (Integer.valueOf(System.getProperty("p2p.lingerTime", "5000"))).intValue());
 +      setSendBufferSize(s, SMALL_BUFFER_SIZE);
 +      setReceiveBufferSize(s);
 +    }
 +    catch (SocketException e) {
 +      // unable to get the settings we want.  Don't log an error because it will
 +      // likely happen a lot
 +    }
 +    if (!useNIO()) {
 +      try {
 +        //this.output = new BufferedOutputStream(s.getOutputStream(), SMALL_BUFFER_SIZE);
 +        this.output = s.getOutputStream();
 +      }
 +      catch (IOException io) {
 +        logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_UNABLE_TO_GET_P2P_CONNECTION_STREAMS), io);
 +        t.getSocketCloser().asyncClose(s, this.remoteAddr.toString(), null);
 +        throw io;
 +      }
 +    }
 +  }
 +  
 +  void setIdleTimeoutTask(SystemTimerTask task) {
 +    this.idleTask = task;
 +  }
 +
 +
 +  /**
 +   * Returns true if an idle connection was detected.
 +   */
 +  public boolean checkForIdleTimeout() {
 +    if (isSocketClosed()) {
 +      return true;
 +    }
 +    if (isSocketInUse()) {
 +      return false;
 +    }
 +    boolean isIdle = !this.accessed;
 +    this.accessed = false;
 +    if (isIdle) {
 +      this.timedOut = true;
 +      this.owner.getConduit().stats.incLostLease();
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("Closing idle connection {} shared={} ordered={}", this, this.sharedResource, this.preserveOrder);
 +      }
 +      try {
 +        // Instead of calling requestClose
 +        // we call closeForReconnect.
 +        // We don't want this timeout close to close
 +        // any other connections. The problem with
 +        // requestClose has removeEndpoint set to true
 +        // which will close an receivers we have if this
 +        // connection is a shared one.
 +        closeForReconnect(LocalizedStrings.Connection_IDLE_CONNECTION_TIMED_OUT.toLocalizedString());
 +      } catch (Exception ignore) {}
 +    }
 +    return isIdle;
 +  }
 +
 +  static private byte[] okHandshakeBytes;
 +  static private ByteBuffer okHandshakeBuf;
 +  static {
 +    int msglen = 1; // one byte for reply code
 +    byte[] bytes = new byte[MSG_HEADER_BYTES + msglen];
 +    msglen = calcHdrSize(msglen);
 +    bytes[MSG_HEADER_SIZE_OFFSET] = (byte)((msglen/0x1000000) & 0xff);
 +    bytes[MSG_HEADER_SIZE_OFFSET+1] = (byte)((msglen/0x10000) & 0xff);
 +    bytes[MSG_HEADER_SIZE_OFFSET+2] = (byte)((msglen/0x100) & 0xff);
 +    bytes[MSG_HEADER_SIZE_OFFSET+3] = (byte)(msglen & 0xff);
 +    bytes[MSG_HEADER_TYPE_OFFSET] = (byte)NORMAL_MSG_TYPE; // message type
 +    bytes[MSG_HEADER_ID_OFFSET] = (byte)((MsgIdGenerator.NO_MSG_ID/0x100) & 0xff);
 +    bytes[MSG_HEADER_ID_OFFSET+1] = (byte)(MsgIdGenerator.NO_MSG_ID & 0xff);
 +    bytes[MSG_HEADER_BYTES] = REPLY_CODE_OK;
 +    int allocSize = bytes.length;
 +    ByteBuffer bb;
 +    if (TCPConduit.useDirectBuffers) {
 +      bb = ByteBuffer.allocateDirect(allocSize);
 +    } else {
 +      bb = ByteBuffer.allocate(allocSize);
 +    }
 +    bb.put(bytes);
 +    okHandshakeBuf = bb;
 +    okHandshakeBytes = bytes;
 +  }
 +
 +  /**
 +   * maximum message buffer size
 +   */
 +  public static final int MAX_MSG_SIZE = 0x00ffffff;
 +  public static int calcHdrSize(int byteSize) {
 +    if (byteSize > MAX_MSG_SIZE) {
 +      throw new IllegalStateException(LocalizedStrings.Connection_TCP_MESSAGE_EXCEEDED_MAX_SIZE_OF_0.toLocalizedString(Integer.valueOf(MAX_MSG_SIZE)));
 +    }
 +    int hdrSize = byteSize;
 +    hdrSize |= (HANDSHAKE_VERSION << 24);
 +    return hdrSize;
 +  }
 +  public static int calcMsgByteSize(int hdrSize) {
 +    return hdrSize & MAX_MSG_SIZE;
 +  }
 +  public static byte calcHdrVersion(int hdrSize) throws IOException {
 +    byte ver = (byte)(hdrSize >> 24);
 +    if (ver != HANDSHAKE_VERSION) {
 +      throw new IOException(LocalizedStrings.Connection_DETECTED_WRONG_VERSION_OF_GEMFIRE_PRODUCT_DURING_HANDSHAKE_EXPECTED_0_BUT_FOUND_1.toLocalizedString(new Object[] {new Byte(HANDSHAKE_VERSION), new Byte(ver)}));
 +    }
 +    return ver;
 +  }
 +  private void sendOKHandshakeReply() throws IOException, ConnectionException {
 +    byte[] my_okHandshakeBytes = null;
 +    ByteBuffer my_okHandshakeBuf = null;
 +    if (this.isReceiver) {
 +      DistributionConfig cfg = owner.getConduit().config;
 +      ByteBuffer bb;
 +      if (useNIO() && TCPConduit.useDirectBuffers) {
 +        bb = ByteBuffer.allocateDirect(128);
 +      } else {
 +        bb = ByteBuffer.allocate(128);
 +      }
 +      bb.putInt(0); // reserve first 4 bytes for packet length
 +      bb.put((byte)NORMAL_MSG_TYPE);
 +      bb.putShort(MsgIdGenerator.NO_MSG_ID);
 +      bb.put(REPLY_CODE_OK_WITH_ASYNC_INFO);
 +      bb.putInt(cfg.getAsyncDistributionTimeout());
 +      bb.putInt(cfg.getAsyncQueueTimeout());
 +      bb.putInt(cfg.getAsyncMaxQueueSize());
 +      // write own product version
 +      Version.writeOrdinal(bb, Version.CURRENT.ordinal(), true);
 +      // now set the msg length into position 0
 +      bb.putInt(0, calcHdrSize(bb.position()-MSG_HEADER_BYTES));
 +      if (useNIO()) {
 +        my_okHandshakeBuf = bb;
 +        bb.flip();
 +      } else {
 +        my_okHandshakeBytes = new byte[bb.position()];
 +        bb.flip();
 +        bb.get(my_okHandshakeBytes);
 +      }
 +    } else {
 +      my_okHandshakeBuf = okHandshakeBuf;
 +      my_okHandshakeBytes = okHandshakeBytes;
 +    }
 +    if (useNIO()) {
 +      synchronized (my_okHandshakeBuf) {
 +        my_okHandshakeBuf.position(0);
 +        nioWriteFully(getSocket().getChannel(), my_okHandshakeBuf, false, null);
 +      }
 +    } else {
 +      synchronized(outLock) {
 +        try {
 +//          this.writerThread = Thread.currentThread();
 +          this.output.write(my_okHandshakeBytes, 0, my_okHandshakeBytes.length);
 +          this.output.flush();
 +        }
 +        finally {
 +//          this.writerThread = null;
 +        }
 +      }
 +    }
 +  }
 +
 +  private static final int HANDSHAKE_TIMEOUT_MS = Integer.getInteger("p2p.handshakeTimeoutMs", 59000).intValue();
 +  //private static final byte HANDSHAKE_VERSION = 1; // 501
 +  //public static final byte HANDSHAKE_VERSION = 2; // cbb5x_PerfScale
 +  //public static final byte HANDSHAKE_VERSION = 3; // durable_client
 +  //public static final byte HANDSHAKE_VERSION = 4; // dataSerialMay19
 +//  public static final byte HANDSHAKE_VERSION = 5; // split-brain bits
 +  //public static final byte HANDSHAKE_VERSION = 6; // direct ack changes
 +  // NOTICE: handshake_version should not be changed anymore.  Use the gemfire
 +  //         version transmitted with the handshake bits and handle old handshakes
 +  //         based on that
 +  public static final byte HANDSHAKE_VERSION = 7; // product version exchange during handshake
 +
 +  /**
 +   * @throws ConnectionException if the conduit has stopped
 +   */
 +  private void waitForHandshake() throws ConnectionException {
 +    boolean needToClose = false;
 +    String reason = null;
 +    try {
 +    synchronized (this.handshakeSync) {
 +      if (!this.handshakeRead && !this.handshakeCancelled) {
 +        boolean success = false;
 +        reason = LocalizedStrings.Connection_UNKNOWN.toLocalizedString();
 +        boolean interrupted = Thread.interrupted();
 +        try {
 +          final long endTime = System.currentTimeMillis() + HANDSHAKE_TIMEOUT_MS;
 +          long msToWait = HANDSHAKE_TIMEOUT_MS;
 +          while (!this.handshakeRead && !this.handshakeCancelled && msToWait > 0) {
 +            this.handshakeSync.wait(msToWait); // spurious wakeup ok
 +            if (!this.handshakeRead && !this.handshakeCancelled) {
 +              msToWait = endTime - System.currentTimeMillis();
 +            }
 +          }
 +          if (!this.handshakeRead && !this.handshakeCancelled) {
 +            reason = LocalizedStrings.Connection_HANDSHAKE_TIMED_OUT.toLocalizedString();
 +            String peerName;
 +            if (this.remoteAddr != null) {
 +              peerName = this.remoteAddr.toString();
 +              // late in the life of jdk 1.7 we started seeing connections accepted
 +              // when accept() was not even being called.  This started causing timeouts
 +              // to occur in the handshake threads instead of causing failures in
 +              // connection-formation.  So, we need to initiate suspect processing here
 +              owner.getDM().getMembershipManager().suspectMember(this.remoteAddr,
 +                  LocalizedStrings.Connection_CONNECTION_HANDSHAKE_WITH_0_TIMED_OUT_AFTER_WAITING_1_MILLISECONDS.toLocalizedString(
 +                      new Object[] {peerName, Integer.valueOf(HANDSHAKE_TIMEOUT_MS)}));
 +            }
 +            else {
 +              peerName = "socket " + this.socket.getRemoteSocketAddress().toString()
 +                  + ":" + this.socket.getPort();
 +            }
 +            throw new ConnectionException(LocalizedStrings.Connection_CONNECTION_HANDSHAKE_WITH_0_TIMED_OUT_AFTER_WAITING_1_MILLISECONDS.toLocalizedString(
 +                  new Object[] {peerName, Integer.valueOf(HANDSHAKE_TIMEOUT_MS)}));
 +          } else {
 +            success = this.handshakeRead;
 +          }
 +        } catch (InterruptedException ex) {
 +          interrupted = true;
 +          this.owner.getConduit().getCancelCriterion().checkCancelInProgress(ex);
 +          reason = LocalizedStrings.Connection_INTERRUPTED.toLocalizedString();
 +        } finally {
 +          if (interrupted) {
 +            Thread.currentThread().interrupt();
 +          }
 +          if (success) {
 +            if (this.isReceiver) {
 +              needToClose = !owner.getConduit().getMembershipManager().addSurpriseMember(this.remoteAddr);
 +              if (needToClose) {
 +                reason = "this member is shunned";
 +              }
 +            }
 +          }
 +          else {
 +            needToClose = true; // for bug 42159
 +          }
 +        }
 +      } // !handshakeRead
 +    } // synchronized
 +
 +    } finally {
 +      if (needToClose) {
 +        // moved this call outside of the sync for bug 42159
 +        try {
 +          requestClose(reason); // fix for bug 31546
 +        } 
 +        catch (Exception ignore) {
 +        }
 +      }
 +    }
 +  }
 +
 +  private void notifyHandshakeWaiter(boolean success) {
 +    synchronized (this.handshakeSync) {
 +      if (success) {
 +        this.handshakeRead = true;
 +      } else {
 +        this.handshakeCancelled = true;
 +      }
 +      this.handshakeSync.notify();
 +    }
 +  }
 +  
 +  private final AtomicBoolean asyncCloseCalled = new AtomicBoolean();
 +  
 +  /**
 +   * asynchronously close this connection
 +   * 
 +   * @param beingSick
 +   */
 +  private void asyncClose(boolean beingSick) {
 +    // note: remoteAddr may be null if this is a receiver that hasn't finished its handshake
 +    
 +    // we do the close in a background thread because the operation may hang if 
 +    // there is a problem with the network.  See bug #46659
 +
 +    // if simulating sickness, sockets must be closed in-line so that tests know
 +    // that the vm is sick when the beSick operation completes
 +    if (beingSick) {
 +      prepareForAsyncClose();
 +    }
 +    else {
 +      if (this.asyncCloseCalled.compareAndSet(false, true)) {
 +        Socket s = this.socket;
 +        if (s != null && !s.isClosed()) {
 +          prepareForAsyncClose();
 +          this.owner.getSocketCloser().asyncClose(s, String.valueOf(this.remoteAddr), null);
 +        }
 +      }
 +    }
 +  }
 +  
 +  private void prepareForAsyncClose() {
 +    synchronized(stateLock) {
 +      if (readerThread != null && isRunning && !readerShuttingDown
 +          && (connectionState == STATE_READING || connectionState == STATE_READING_ACK)) {
 +        readerThread.interrupt();
 +      }
 +    }
 +  }
 +
 +  private static final int CONNECT_HANDSHAKE_SIZE = 4096;
 +
 +  /**
 +   * waits until we've joined the distributed system
 +   * before returning
 +   */
 +  private void waitForAddressCompletion() {
 +    InternalDistributedMember myAddr = this.owner.getConduit().getLocalAddress();
 +    synchronized (myAddr) {
 +      while ((owner.getConduit().getCancelCriterion().cancelInProgress() == null)
 +          && myAddr.getInetAddress() == null && myAddr.getVmViewId() < 0) {
 +        try {
 +          myAddr.wait(100); // spurious wakeup ok
 +        }
 +        catch (InterruptedException ie) {
 +          Thread.currentThread().interrupt();
 +          this.owner.getConduit().getCancelCriterion().checkCancelInProgress(ie);
 +        }
 +      }
 +      Assert.assertTrue(myAddr.getDirectChannelPort() == this.owner.getConduit().getPort());
 +    }
 +  }
 +
 +  private void handshakeNio() throws IOException {
 +    waitForAddressCompletion();
 +    
 +    InternalDistributedMember myAddr = this.owner.getConduit().getLocalAddress();
 +    final MsgOutputStream connectHandshake
 +      = new MsgOutputStream(CONNECT_HANDSHAKE_SIZE);
 +    //connectHandshake.reset();
 +    /**
 +     * Note a byte of zero is always written because old products
 +     * serialized a member id with always sends an ip address.
 +     * My reading of the ip-address specs indicated that the first byte
 +     * of a valid address would never be 0.
 +     */
 +    connectHandshake.writeByte(0);
 +    connectHandshake.writeByte(HANDSHAKE_VERSION);
 +    // NOTE: if you add or remove code in this section bump HANDSHAKE_VERSION
 +    InternalDataSerializer.invokeToData(myAddr, connectHandshake);
 +    connectHandshake.writeBoolean(this.sharedResource);
 +    connectHandshake.writeBoolean(this.preserveOrder);
 +    connectHandshake.writeLong(this.uniqueId);
 +    // write the product version ordinal
 +    Version.CURRENT.writeOrdinal(connectHandshake, true);
 +    connectHandshake.writeInt(dominoCount.get()+1);
 +// this writes the sending member + thread name that is stored in senderName
 +// on the receiver to show the cause of reader thread creation
 +//    if (dominoCount.get() > 0) {
 +//      connectHandshake.writeUTF(Thread.currentThread().getName());
 +//    } else {
 +//      String name = owner.getDM().getConfig().getName();
 +//      if (name == null) {
 +//        name = "pid="+OSProcess.getId();
 +//      }
 +//      connectHandshake.writeUTF("["+name+"] "+Thread.currentThread().getName());
 +//    }
 +    connectHandshake.setMessageHeader(NORMAL_MSG_TYPE, DistributionManager.STANDARD_EXECUTOR, MsgIdGenerator.NO_MSG_ID);
 +    nioWriteFully(getSocket().getChannel(), connectHandshake.getContentBuffer(), false, null);
 +  }
 +
 +  private void handshakeStream() throws IOException {
 +    waitForAddressCompletion();
 +
 +    //this.output = new BufferedOutputStream(getSocket().getOutputStream(), owner.getConduit().bufferSize);
 +    this.output = getSocket().getOutputStream();
 +    ByteArrayOutputStream baos = new ByteArrayOutputStream(CONNECT_HANDSHAKE_SIZE);
 +    DataOutputStream os = new DataOutputStream(baos);
 +    InternalDistributedMember myAddr = owner.getConduit().getLocalAddress();
 +    os.writeByte(0);
 +    os.writeByte(HANDSHAKE_VERSION);
 +    // NOTE: if you add or remove code in this section bump HANDSHAKE_VERSION
 +    InternalDataSerializer.invokeToData(myAddr, os);
 +    os.writeBoolean(this.sharedResource);
 +    os.writeBoolean(this.preserveOrder);
 +    os.writeLong(this.uniqueId);
 +    Version.CURRENT.writeOrdinal(os, true);
 +    os.writeInt(dominoCount.get()+1);
 + // this writes the sending member + thread name that is stored in senderName
 + // on the receiver to show the cause of reader thread creation
 +//    if (dominoCount.get() > 0) {
 +//      os.writeUTF(Thread.currentThread().getName());
 +//    } else {
 +//      String name = owner.getDM().getConfig().getName();
 +//      if (name == null) {
 +//        name = "pid="+OSProcess.getId();
 +//      }
 +//      os.writeUTF("["+name+"] "+Thread.currentThread().getName());
 +//    }
 +    os.flush();
 +
 +    byte[] msg = baos.toByteArray();
 +    int len = calcHdrSize(msg.length);
 +    byte[] lenbytes = new byte[MSG_HEADER_BYTES];
 +    lenbytes[MSG_HEADER_SIZE_OFFSET] = (byte)((len/0x1000000) & 0xff);
 +    lenbytes[MSG_HEADER_SIZE_OFFSET+1] = (byte)((len/0x10000) & 0xff);
 +    lenbytes[MSG_HEADER_SIZE_OFFSET+2] = (byte)((len/0x100) & 0xff);
 +    lenbytes[MSG_HEADER_SIZE_OFFSET+3] = (byte)(len & 0xff);
 +    lenbytes[MSG_HEADER_TYPE_OFFSET] = (byte)NORMAL_MSG_TYPE;
 +    lenbytes[MSG_HEADER_ID_OFFSET] = (byte)((MsgIdGenerator.NO_MSG_ID/0x100) & 0xff);
 +    lenbytes[MSG_HEADER_ID_OFFSET+1] = (byte)(MsgIdGenerator.NO_MSG_ID & 0xff);
 +    synchronized(outLock) {
 +      try {
 +//        this.writerThread = Thread.currentThread();
 +        this.output.write(lenbytes, 0, lenbytes.length);
 +        this.output.write(msg, 0, msg.length);
 +        this.output.flush();
 +      }
 +      finally {
 +//        this.writerThread = null;
 +      }
 +    }
 +  }
 +
 +  /**
 +   *
 +   * @throws IOException if handshake fails
 +   */
 +  private void attemptHandshake(ConnectionTable connTable) throws IOException {
 +    // send HANDSHAKE
 +    // send this server's port.  It's expected on the other side
 +    if (useNIO()) {
 +      handshakeNio();
 +    }
 +    else {
 +      handshakeStream();
 +    }
 +
 +    startReader(connTable); // this reader only reads the handshake and then exits
 +    waitForHandshake(); // waiting for reply
 +  }
 +
 +  /** time between connection attempts*/
 +  private static final int RECONNECT_WAIT_TIME
 +    = Integer.getInteger("gemfire.RECONNECT_WAIT_TIME", 2000).intValue();
 +
 +  /** creates a new connection to a remote server.
 +   *  We are initiating this connection; the other side must accept us
 +   *  We will almost always send messages; small acks are received.
 +   */
 +  protected static Connection createSender(final MembershipManager mgr,
 +                                           final ConnectionTable t,
 +                                           final boolean preserveOrder,
 +                                           final DistributedMember remoteAddr,
 +                                           final boolean sharedResource,
 +                                           final long startTime,
 +                                           final long ackTimeout,
 +                                           final long ackSATimeout)
 +    throws IOException, DistributedSystemDisconnectedException
 +  {
 +    boolean warningPrinted = false;
 +    boolean success = false;
 +    boolean firstTime = true;
 +    Connection conn = null;
 +    // keep trying.  Note that this may be executing during the shutdown window
 +    // where a cancel criterion has not been established, but threads are being
 +    // interrupted.  In this case we must allow the connection to succeed even
 +    // though subsequent messaging using the socket may fail
 +    boolean interrupted = Thread.interrupted();
 +    boolean severeAlertIssued = false;
 +    boolean suspected = false;
 +    long reconnectWaitTime = RECONNECT_WAIT_TIME;
 +    boolean connectionErrorLogged = false;
 +    try {
 +      while (!success) { // keep trying
 +        // Quit if DM has stopped distribution
 +        t.getConduit().getCancelCriterion().checkCancelInProgress(null);
 +        long now = System.currentTimeMillis();
 +        if (!severeAlertIssued && ackSATimeout > 0  &&  startTime + ackTimeout < now) {
 +          if (startTime + ackTimeout + ackSATimeout < now) {
 +            if (remoteAddr != null) {
 +              logger.fatal(LocalizedMessage.create(
 +                  LocalizedStrings.Connection_UNABLE_TO_FORM_A_TCPIP_CONNECTION_TO_0_IN_OVER_1_SECONDS, new Object[] { remoteAddr,
 +                  (ackSATimeout + ackTimeout) / 1000 }));
 +            }
 +            severeAlertIssued = true;
 +          }
 +          else if (!suspected) {
 +            if (remoteAddr != null) {
 +              logger.warn(LocalizedMessage.create(
 +                  LocalizedStrings.Connection_UNABLE_TO_FORM_A_TCPIP_CONNECTION_TO_0_IN_OVER_1_SECONDS,
 +                  new Object[] { remoteAddr, (ackTimeout)/1000 }));
 +            }
 +            mgr.suspectMember(remoteAddr, LocalizedStrings.Connection_UNABLE_TO_FORM_A_TCPIP_CONNECTION_IN_A_REASONABLE_AMOUNT_OF_TIME.toLocalizedString());
 +            suspected = true;
 +          }
 +          reconnectWaitTime = Math.min(RECONNECT_WAIT_TIME,
 +              ackSATimeout - (now - startTime - ackTimeout));
 +          if (reconnectWaitTime <= 0) {
 +            reconnectWaitTime = RECONNECT_WAIT_TIME;
 +          }
 +        } else if (!suspected && (startTime > 0) && (ackTimeout > 0)
 +            && (startTime + ackTimeout < now)) {
 +          mgr.suspectMember(remoteAddr, LocalizedStrings.Connection_UNABLE_TO_FORM_A_TCPIP_CONNECTION_IN_A_REASONABLE_AMOUNT_OF_TIME.toLocalizedString());
 +          suspected = true;
 +        }
 +        if (firstTime) {
 +          firstTime = false;
 +          if (!mgr.memberExists(remoteAddr) || mgr.isShunned(remoteAddr) || mgr.shutdownInProgress()) {
 +            throw new IOException("Member " + remoteAddr + " left the system");
 +          }
 +        }
 +        else {
 +          // if we're sending an alert and can't connect, bail out.  A sick
 +          // alert listener should not prevent cache operations from continuing
 +          if (AlertAppender.isThreadAlerting()) {
 +            // do not change the text of this exception - it is looked for in exception handlers
 +            throw new IOException("Cannot form connection to alert listener " + remoteAddr);
 +          }
 +            
 +          // Wait briefly...
 +          interrupted = Thread.interrupted() || interrupted;
 +          try {
 +            Thread.sleep(reconnectWaitTime);
 +          }
 +          catch (InterruptedException ie) {
 +            interrupted = true;
 +            t.getConduit().getCancelCriterion().checkCancelInProgress(ie);
 +          }
 +          t.getConduit().getCancelCriterion().checkCancelInProgress(null);
 +          if (giveUpOnMember(mgr, remoteAddr)) {
 +            throw new IOException(LocalizedStrings.Connection_MEMBER_LEFT_THE_GROUP.toLocalizedString(remoteAddr));
 +          }
 +          if (!warningPrinted) {
 +            warningPrinted = true;
 +            logger.warn(LocalizedMessage.create(LocalizedStrings.Connection_CONNECTION_ATTEMPTING_RECONNECT_TO_PEER__0, remoteAddr));
 +          }          
 +          t.getConduit().stats.incReconnectAttempts();
 +        }
 +        //create connection
 +        try {
 +          conn = null;
 +          conn = new Connection(mgr, t, preserveOrder, remoteAddr, sharedResource);
 +        }
 +        catch (javax.net.ssl.SSLHandshakeException se) {
 +          // no need to retry if certificates were rejected
 +          throw se;
 +        }
 +        catch (IOException ioe) {
 +          // Only give up if the member leaves the view.
 +          if (giveUpOnMember(mgr, remoteAddr)) {
 +            throw ioe;
 +          }
 +          t.getConduit().getCancelCriterion().checkCancelInProgress(null);
 +          if ("Too many open files".equals(ioe.getMessage())) {
 +            t.fileDescriptorsExhausted();
 +          }
 +          else if (!connectionErrorLogged) {
 +            connectionErrorLogged = true; // otherwise change to use 100ms intervals causes a lot of these
 +            logger.info(LocalizedMessage.create(
 +                LocalizedStrings.Connection_CONNECTION_FAILED_TO_CONNECT_TO_PEER_0_BECAUSE_1,
 +                new Object[] {sharedResource, preserveOrder, remoteAddr, ioe}));
 +          }
 +        } // IOException
 +        finally {
 +          if (conn == null) {
 +            t.getConduit().stats.incFailedConnect();
 +          }
 +        }
 +        if (conn != null) {
 +          // handshake
 +          try {
 +            conn.attemptHandshake(t);
 +            if (conn.isSocketClosed()) {
 +              // something went wrong while reading the handshake
 +              // and the socket was closed or this guy sent us a
 +              // ShutdownMessage
 +              if (giveUpOnMember(mgr, remoteAddr)) {
 +                throw new IOException(LocalizedStrings.Connection_MEMBER_LEFT_THE_GROUP.toLocalizedString(remoteAddr));
 +              }
 +              t.getConduit().getCancelCriterion().checkCancelInProgress(null);
 +              // no success but no need to log; just retry
 +            }
 +            else {
 +              success = true;
 +            }
 +          }
 +          catch (DistributedSystemDisconnectedException e) {
 +            throw e;
 +          }
 +          catch (ConnectionException e) {
 +            if (giveUpOnMember(mgr, remoteAddr)) {
 +              IOException ioe = new IOException(LocalizedStrings.Connection_HANDSHAKE_FAILED.toLocalizedString());
 +              ioe.initCause(e);
 +              throw ioe;
 +            }
 +            t.getConduit().getCancelCriterion().checkCancelInProgress(null);
 +            logger.info(LocalizedMessage.create(
 +                LocalizedStrings.Connection_CONNECTION_HANDSHAKE_FAILED_TO_CONNECT_TO_PEER_0_BECAUSE_1,
 +                new Object[] {sharedResource, preserveOrder, remoteAddr ,e}));
 +          }
 +          catch (IOException e) {
 +            if (giveUpOnMember(mgr, remoteAddr)) {
 +              throw e;
 +            }
 +            t.getConduit().getCancelCriterion().checkCancelInProgress(null);
 +            logger.info(LocalizedMessage.create(
 +                LocalizedStrings.Connection_CONNECTION_HANDSHAKE_FAILED_TO_CONNECT_TO_PEER_0_BECAUSE_1,
 +                new Object[] {sharedResource, preserveOrder, remoteAddr ,e}));
 +            if (!sharedResource && "Too many open files".equals(e.getMessage())) {
 +              t.fileDescriptorsExhausted();
 +            }
 +          }
 +          finally {
 +            if (!success) {
 +              try {
 +                conn.requestClose(LocalizedStrings.Connection_FAILED_HANDSHAKE.toLocalizedString()); 
 +              } catch (Exception ignore) {}
 +              conn = null;
 +            }
 +          }
 +        }
 +      } // while
 +      if (warningPrinted) {
 +        logger.info(LocalizedMessage.create(
 +            LocalizedStrings.Connection_0_SUCCESSFULLY_REESTABLISHED_CONNECTION_TO_PEER_1,
 +            new Object[] {mgr.getLocalMember(), remoteAddr}));
 +      }
 +    }
 +    finally {
 +      try {
 +        if (!success) {
 +          if (conn != null) {
 +            conn.requestClose(LocalizedStrings.Connection_FAILED_CONSTRUCTION.toLocalizedString());
 +            conn = null;
 +          }
 +        }
 +      }
 +      finally {
 +        if (interrupted) {
 +          Thread.currentThread().interrupt();
 +        }
 +      }
 +    }
 +    //Assert.assertTrue(conn != null);
 +    if (conn == null) {
 +      throw new ConnectionException(
 +        LocalizedStrings.Connection_CONNECTION_FAILED_CONSTRUCTION_FOR_PEER_0
 +          .toLocalizedString(remoteAddr));
 +    }
 +    if (preserveOrder && BATCH_SENDS) {
 +      conn.createBatchSendBuffer();
 +    }
 +    conn.finishedConnecting = true;
 +    return conn;
 +  }
 +  
 +  private static boolean giveUpOnMember(MembershipManager mgr, DistributedMember remoteAddr) {
 +    return !mgr.memberExists(remoteAddr) || mgr.isShunned(remoteAddr) || mgr.shutdownInProgress();
 +  }
 +
 +  private void setRemoteAddr(DistributedMember m) {
 +    this.remoteAddr = this.owner.getDM().getCanonicalId(m);
 +    MembershipManager mgr = this.owner.owner.getMembershipManager();
 +    mgr.addSurpriseMember(m);
 +  }
 +  
 +  /** creates a new connection to a remote server.
 +   *  We are initiating this connection; the other side must accept us
 +   *  We will almost always send messages; small acks are received.
 +   */
 +  private Connection(MembershipManager mgr,
 +                     ConnectionTable t,
 +                     boolean preserveOrder,
 +                     DistributedMember remoteID,
 +                     boolean sharedResource)
 +    throws IOException, DistributedSystemDisconnectedException
 +  {    
 +    InternalDistributedMember remoteAddr = (InternalDistributedMember)remoteID;
 +    if (t == null) {
 +      throw new IllegalArgumentException(LocalizedStrings.Connection_CONNECTIONTABLE_IS_NULL.toLocalizedString());
 +    }
 +    this.isReceiver = false;
 +    this.owner = t;
 +    this.sharedResource = sharedResource;
 +    this.preserveOrder = preserveOrder;
 +    setRemoteAddr(remoteAddr);
 +    this.conduitIdStr = this.owner.getConduit().getId().toString();
 +    this.handshakeRead = false;
 +    this.handshakeCancelled = false;
 +    this.connected = true;
 +
 +    this.uniqueId = idCounter.getAndIncrement();
 +
 +    // connect to listening socket
 +
 +    InetSocketAddress addr = new InetSocketAddress(remoteAddr.getInetAddress(), remoteAddr.getDirectChannelPort());
 +    if (useNIO()) {
 +      SocketChannel channel = SocketChannel.open();
 +      this.owner.addConnectingSocket(channel.socket(), addr.getAddress());
 +      try {
 +        channel.socket().setTcpNoDelay(true);
 +
 +        channel.socket().setKeepAlive(SocketCreator.ENABLE_TCP_KEEP_ALIVE);
 +  
 +        /** If conserve-sockets is false, the socket can be used for receiving responses,
 +         * so set the receive buffer accordingly.
 +         */
 +        if(!sharedResource) {
 +          setReceiveBufferSize(channel.socket(), this.owner.getConduit().tcpBufferSize);
 +        } 
 +        else {
 +          setReceiveBufferSize(channel.socket(), SMALL_BUFFER_SIZE); // make small since only receive ack messages
 +        }
 +        setSendBufferSize(channel.socket());
 +        channel.configureBlocking(true);
 +
 +        int connectTime = getP2PConnectTimeout();; 
 +
 +        try {
 +          SocketUtils.connect(channel.socket(), addr, connectTime);
 +        } catch (NullPointerException e) {
 +          // bug #45044 - jdk 1.7 sometimes throws an NPE here
 +          ConnectException c = new ConnectException("Encountered bug #45044 - retrying");
 +          c.initCause(e);
 +          // prevent a hot loop by sleeping a little bit
 +          try {
 +            Thread.sleep(1000);
 +          } catch (InterruptedException ie) {
 +            Thread.currentThread().interrupt();
 +          }
 +          throw c;
 +        } catch (CancelledKeyException e) {
 +          // bug #44469: for some reason NIO throws this runtime exception
 +          //             instead of an IOException on timeouts
 +          ConnectException c = new ConnectException(LocalizedStrings.Connection_ATTEMPT_TO_CONNECT_TIMED_OUT_AFTER_0_MILLISECONDS
 +              .toLocalizedString(new Object[]{connectTime}));
 +          c.initCause(e);
 +          throw c;
 +        } catch (ClosedSelectorException e) {
 +          // bug #44808: for some reason JRockit NIO thorws this runtime exception
 +          //             instead of an IOException on timeouts
 +          ConnectException c = new ConnectException(LocalizedStrings.Connection_ATTEMPT_TO_CONNECT_TIMED_OUT_AFTER_0_MILLISECONDS
 +              .toLocalizedString(new Object[]{connectTime}));
 +          c.initCause(e);
 +          throw c;
 +        }
 +      }
 +      finally {
 +        this.owner.removeConnectingSocket(channel.socket());
 +      }
 +      this.socket = channel.socket();
 +    }
 +    else {
 +      if (TCPConduit.useSSL) {
 +        // socket = javax.net.ssl.SSLSocketFactory.getDefault()
 +        //  .createSocket(remoteAddr.getInetAddress(), remoteAddr.getPort());
 +        int socketBufferSize = sharedResource ? SMALL_BUFFER_SIZE : this.owner.getConduit().tcpBufferSize;
 +        this.socket = SocketCreator.getDefaultInstance().connectForServer( remoteAddr.getInetAddress(), remoteAddr.getDirectChannelPort(), socketBufferSize );
 +        // Set the receive buffer size local fields. It has already been set in the socket.
 +        setSocketBufferSize(this.socket, false, socketBufferSize, true);
 +        setSendBufferSize(this.socket);
 +      }
 +      else {
 +        //socket = new Socket(remoteAddr.getInetAddress(), remoteAddr.getPort());
 +        Socket s = new Socket();
 +        this.socket = s;
 +        s.setTcpNoDelay(true);
 +        s.setKeepAlive(SocketCreator.ENABLE_TCP_KEEP_ALIVE);
 +        setReceiveBufferSize(s, SMALL_BUFFER_SIZE);
 +        setSendBufferSize(s);
 +        SocketUtils.connect(s, addr, 0);
 +      }
 +    }
 +    if (logger.isDebugEnabled()) {
 +      logger.debug("Connection: connected to {} with stub {}", remoteAddr, addr);
 +    }
 +    try { getSocket().setTcpNoDelay(true); } catch (SocketException e) {  }
 +  }
 +
 +  /**
 +   * Batch sends currently should not be turned on because:
 +   *  1. They will be used for all sends (instead of just no-ack)
 +   *     and thus will break messages that wait for a response (or kill perf).
 +   *  2. The buffer is not properly flushed and closed on shutdown.
 +   *     The code attempts to do this but must not be doing it  correctly.
 +   */
 +  private static final boolean BATCH_SENDS = Boolean.getBoolean("p2p.batchSends");
 +  protected static final int BATCH_BUFFER_SIZE = Integer.getInteger("p2p.batchBufferSize", 1024*1024).intValue();
 +  protected static final int BATCH_FLUSH_MS = Integer.getInteger("p2p.batchFlushTime", 50).intValue();
 +  protected Object batchLock;
 +  protected ByteBuffer fillBatchBuffer;
 +  protected ByteBuffer sendBatchBuffer;
 +  private BatchBufferFlusher batchFlusher;
 +
 +  private void createBatchSendBuffer() {
 +    // batch send buffer isn't needed if old-io is being used
 +    if (!this.useNIO) {
 +      return;
 +    }
 +    this.batchLock = new Object();
 +    if (TCPConduit.useDirectBuffers) {
 +      this.fillBatchBuffer = ByteBuffer.allocateDirect(BATCH_BUFFER_SIZE);
 +      this.sendBatchBuffer = ByteBuffer.allocateDirect(BATCH_BUFFER_SIZE);
 +    } else {
 +      this.fillBatchBuffer = ByteBuffer.allocate(BATCH_BUFFER_SIZE);
 +      this.sendBatchBuffer = ByteBuffer.allocate(BATCH_BUFFER_SIZE);
 +    }
 +    this.batchFlusher = new BatchBufferFlusher();
 +    this.batchFlusher.start();
 +  }
 +
 +  private class BatchBufferFlusher extends Thread  {
 +    private volatile boolean flushNeeded = false;
 +    private volatile boolean timeToStop = false;
 +    private DMStats stats;
 +
 +
 +    public BatchBufferFlusher() {
 +      setDaemon(true);
 +      this.stats = owner.getConduit().stats;
 +    }
 +    /**
 +     * Called when a message writer needs the current fillBatchBuffer flushed
 +     */
 +    public void flushBuffer(ByteBuffer bb) {
 +      final long start = DistributionStats.getStatTime();
 +      try {
 +        synchronized (this) {
 +          synchronized (batchLock) {
 +            if (bb != fillBatchBuffer) {
 +              // it must have already been flushed. So just return
 +              // and use the new fillBatchBuffer
 +              return;
 +            }
 +          }
 +          this.flushNeeded = true;
 +          this.notify();
 +        }
 +        synchronized (batchLock) {
 +          // Wait for the flusher thread
 +          while (bb == fillBatchBuffer) {
 +            Connection.this.owner.getConduit().getCancelCriterion().checkCancelInProgress(null);
 +            boolean interrupted = Thread.interrupted();
 +            try {
 +              batchLock.wait();  // spurious wakeup ok
 +            } 
 +            catch (InterruptedException ex) {
 +              interrupted = true;
 +            }
 +            finally {
 +              if (interrupted) {
 +                Thread.currentThread().interrupt();
 +              }
 +            }
 +          } // while
 +        }
 +      } finally {
 +        owner.getConduit().stats.incBatchWaitTime(start);
 +      }
 +    }
 +
 +    public void close() {
 +      synchronized (this) {
 +        this.timeToStop = true;
 +        this.flushNeeded = true;
 +        this.notify();
 +      }
 +    }
 +
 +    @Override
 +    public void run() {
 +      try {
 +        synchronized (this) {
 +          while (!timeToStop) {
 +            if (!this.flushNeeded && fillBatchBuffer.position() <= (BATCH_BUFFER_SIZE/2)) {
 +              wait(BATCH_FLUSH_MS); // spurious wakeup ok
 +            }
 +            if (this.flushNeeded || fillBatchBuffer.position() > (BATCH_BUFFER_SIZE/2)) {
 +              final long start = DistributionStats.getStatTime();
 +              synchronized (batchLock) {
 +                // This is the only block of code that will swap
 +                // the buffer references
 +                this.flushNeeded = false;
 +                ByteBuffer tmp = fillBatchBuffer;
 +                fillBatchBuffer = sendBatchBuffer;
 +                sendBatchBuffer = tmp;
 +                batchLock.notifyAll();
 +              }
 +              // We now own the sendBatchBuffer
 +              if (sendBatchBuffer.position() > 0) {
 +                final boolean origSocketInUse = socketInUse;
 +                socketInUse = true;
 +                try {
 +                  sendBatchBuffer.flip();
 +                  SocketChannel channel = getSocket().getChannel();
 +                  nioWriteFully(channel, sendBatchBuffer, false, null);
 +                  sendBatchBuffer.clear();
 +                } catch (IOException ex) {
 +                  logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_EXCEPTION_FLUSHING_BATCH_SEND_BUFFER_0,ex));
 +                  readerShuttingDown = true;
 +                  requestClose(LocalizedStrings.Connection_EXCEPTION_FLUSHING_BATCH_SEND_BUFFER_0.toLocalizedString(ex));
 +                } catch (ConnectionException ex) {
 +                  logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_EXCEPTION_FLUSHING_BATCH_SEND_BUFFER_0,ex));
 +                  readerShuttingDown = true;
 +                  requestClose(LocalizedStrings.Connection_EXCEPTION_FLUSHING_BATCH_SEND_BUFFER_0.toLocalizedString(ex));
 +                } finally {
 +                  accessed();
 +                  socketInUse = origSocketInUse;
 +                }
 +              }
 +              this.stats.incBatchFlushTime(start);
 +            }
 +          }
 +        }
 +      } catch (InterruptedException ex) {
 +        // time for this thread to shutdown
 +//        Thread.currentThread().interrupt();
 +      }
 +    }
 +  }
 +
 +  private void closeBatchBuffer() {
 +    if (this.batchFlusher != null) {
 +      this.batchFlusher.close();
 +    }
 +  }
 +
 +  /** use to test message prep overhead (no socket write).
 +   * WARNING: turning this on completely disables distribution of batched sends
 +   */
 +  private static final boolean SOCKET_WRITE_DISABLED = Boolean.getBoolean("p2p.disableSocketWrite");
 +
 +  private void batchSend(ByteBuffer src) throws IOException {
 +    if (SOCKET_WRITE_DISABLED) {
 +      return;
 +    }
 +    final long start = DistributionStats.getStatTime();
 +    try {
 +      ByteBuffer dst = null;
 +      Assert.assertTrue(src.remaining() <= BATCH_BUFFER_SIZE , "Message size(" + src.remaining() + ") exceeded BATCH_BUFFER_SIZE(" + BATCH_BUFFER_SIZE + ")");
 +      do {
 +        synchronized (this.batchLock) {
 +          dst = this.fillBatchBuffer;
 +          if (src.remaining() <= dst.remaining()) {
 +            final long copyStart = DistributionStats.getStatTime();
 +            dst.put(src);
 +            this.owner.getConduit().stats.incBatchCopyTime(copyStart);
 +            return;
 +          }
 +        }
 +        // If we got this far then we do not have room in the current
 +        // buffer and need the flusher thread to flush before we can fill it
 +        this.batchFlusher.flushBuffer(dst);
 +      } while (true);
 +    } finally {
 +      this.owner.getConduit().stats.incBatchSendTime(start);
 +    }
 +  }
 +
 +  /**
 +   * Request that the manager close this connection, or close it
 +   * forcibly if there is no manager.  Invoking this method ensures
 +   * that the proper synchronization is done.
 +   */
 +  void requestClose(String reason) {
 +    close(reason, true, true, false, false);
 +  }
 +
 +  boolean isClosing() {
 +    return this.closing.get();
 +  }
 +
 +  /**
 +   * Used to close a connection that has not yet been registered
 +   * with the distribution manager.
 +   */
 +  void closePartialConnect(String reason) {
 +    close(reason, false, false, false, false);
 +  }
 +  
 +  void closePartialConnect(String reason, boolean beingSick) {
 +    close(reason, false, false, beingSick, false);
 +  }
 +
 +  void closeForReconnect(String reason) {
 +    close(reason, true, false, false, false);
 +  }
 +
 +  void closeOldConnection(String reason) {
 +    close(reason, true, true, false, true);
 +  }
 +
 +  /**
 +   * Closes the connection.
 +   *
 +   * @see #requestClose
 +   */
 +  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="TLW_TWO_LOCK_WAIT")
 +  private void close(String reason, boolean cleanupEndpoint, 
 +      boolean p_removeEndpoint, boolean beingSick, boolean forceRemoval) {
 +    boolean removeEndpoint = p_removeEndpoint;
 +    // use getAndSet outside sync on this to fix 42330
 +    boolean onlyCleanup = this.closing.getAndSet(true);
 +    if (onlyCleanup && !forceRemoval) {
 +      return;
 +    }
 +    if (!onlyCleanup) {
 +    synchronized (this) {
 +      this.stopped = true;
 +      if (this.connected) {
 +        if (this.asyncQueuingInProgress
 +            && this.pusherThread != Thread.currentThread()) {
 +          // We don't need to do this if we are the pusher thread
 +          // and we have determined that we need to close the connection.
 +          // See bug 37601.
 +          synchronized (this.outgoingQueue) {
 +            // wait for the flusher to complete (it may timeout)
 +            while (this.asyncQueuingInProgress) {
 +              // Don't do this: causes closes to not get done in the event
 +              // of an orderly shutdown:
 +  //            this.owner.getConduit().getCancelCriterion().checkCancelInProgress(null);
 +              boolean interrupted = Thread.interrupted();
 +              try {
 +                this.outgoingQueue.wait(); // spurious wakeup ok
 +              } catch (InterruptedException ie) {
 +                interrupted = true;
 +  //                this.owner.getConduit().getCancelCriterion().checkCancelInProgress(ie);
 +              }
 +              finally {
 +                if (interrupted) Thread.currentThread().interrupt();
 +              }
 +            } // while
 +          } // synchronized
 +        }
 +        this.connected = false;
 +        closeSenderSem();
 +        {
 +          final DMStats stats = this.owner.getConduit().stats;
 +          if (this.finishedConnecting) {
 +            if (this.isReceiver) {
 +              stats.decReceivers();
 +            } else {
 +              stats.decSenders(this.sharedResource, this.preserveOrder);
 +            }
 +          }
 +        }
 +        if (logger.isDebugEnabled()) {
 +          logger.debug("Closing socket for {}", this);
 +        }
 +      }
 +        else if (!forceRemoval) {
 +        removeEndpoint = false;
 +      }
 +      // make sure our socket is closed
 +      asyncClose(false);
 +      nioLengthSet = false;
 +    } // synchronized
 +    
 +      // moved the call to notifyHandshakeWaiter out of the above
 +      // synchronized block to fix bug #42159
 +      // Make sure anyone waiting for a handshake stops waiting
 +      notifyHandshakeWaiter(false);
 +    // wait a bit for the our reader thread to exit
 +    // don't wait if we are the reader thread
 +    boolean isIBM = false;
 +    // if network partition detection is enabled or this is an admin vm
 +    // we can't wait for the reader thread when running in an IBM JRE.  See
 +    // bug 41889
 +    if (this.owner.owner.config.getEnableNetworkPartitionDetection() ||
 +        this.owner.owner.getLocalAddr().getVmKind() == DistributionManager.ADMIN_ONLY_DM_TYPE ||
 +        this.owner.owner.getLocalAddr().getVmKind() == DistributionManager.LOCATOR_DM_TYPE) {
 +      isIBM = "IBM Corporation".equals(System.getProperty("java.vm.vendor"));
 +    }
 +    {
 +      // Now that readerThread is returned to a pool after we close
 +      // we need to be more careful not to join on a thread that belongs
 +      // to someone else.
 +      Thread readerThreadSnapshot = this.readerThread;
 +      if (!beingSick && readerThreadSnapshot != null && !isIBM
 +          && this.isRunning && !this.readerShuttingDown
 +          && readerThreadSnapshot != Thread.currentThread()) {
 +        try {
 +          readerThreadSnapshot.join(500);
 +          readerThreadSnapshot = this.readerThread;
 +          if (this.isRunning && !this.readerShuttingDown
 +              && readerThreadSnapshot != null
 +              && owner.getDM().getRootCause() == null) { // don't wait twice if there's a system failure
 +            readerThreadSnapshot.join(1500);
 +            if (this.isRunning) {
 +              logger.info(LocalizedMessage.create(LocalizedStrings.Connection_TIMED_OUT_WAITING_FOR_READERTHREAD_ON_0_TO_FINISH, this));
 +            }
 +          }
 +        }
 +        catch (IllegalThreadStateException ignore) {
 +          // ignored - thread already stopped
 +        }
 +        catch (InterruptedException ignore) {
 +          Thread.currentThread().interrupt();
 +          // but keep going, we're trying to close.
 +        }
 +      }
 +    }
 +
 +    closeBatchBuffer();
 +    closeAllMsgDestreamers();
 +    }
 +    if (cleanupEndpoint) {
 +      if (this.isReceiver) {
 +        this.owner.removeReceiver(this);
 +      }
 +      if (removeEndpoint) {
 +        if (this.sharedResource) {
 +          if (!this.preserveOrder) {
 +            // only remove endpoint when shared unordered connection
 +            // is closed. This is part of the fix for bug 32980.
 +            if (!this.isReceiver) {
 +              // Only remove endpoint if sender.
 +              if (this.finishedConnecting) {
 +                // only remove endpoint if our constructor finished
 +                this.owner.removeEndpoint(this.remoteAddr, reason);
 +              }
 +            }
 +          }
 +          else {
 +            this.owner.removeSharedConnection(reason, this.remoteAddr, this.preserveOrder, this);
 +          }
 +        }
 +        else if (!this.isReceiver) {
 +          this.owner.removeThreadConnection(this.remoteAddr, this);
 +        }
 +      }
 +      else {
 +        // This code is ok to do even if the ConnectionTable
 +        // has never added this Connection to its maps since
 +        // the calls in this block use our identity to do the removes.
 +        if (this.sharedResource) {
 +          this.owner.removeSharedConnection(reason, this.remoteAddr, this.preserveOrder, this);
 +        }
 +        else if (!this.isReceiver) {
 +          this.owner.removeThreadConnection(this.remoteAddr, this);
 +        }
 +      }
 +    }
 +    
 +    //This cancels the idle timer task, but it also removes the tasks
 +    //reference to this connection, freeing up the connection (and it's buffers
 +    //for GC sooner.
 +    if(idleTask != null) {
 +      idleTask.cancel();
 +    }
 +    
 +    if(ackTimeoutTask!=null){
 +        ackTimeoutTask.cancel();
 +    }
 +
 +  }
 +
 +  /** starts a reader thread */
 +  private void startReader(ConnectionTable connTable) { 
 +    Assert.assertTrue(!this.isRunning); 
 +    stopped = false; 
 +    this.isRunning = true; 
 +    connTable.executeCommand(this);  
 +  } 
 +
 +
 +  /** in order to read non-NIO socket-based messages we need to have a thread
 +      actively trying to grab bytes out of the sockets input queue.
 +      This is that thread. */
 +  public void run() {
 +    this.readerThread = Thread.currentThread();
 +    this.readerThread.setName(p2pReaderName());
 +    ConnectionTable.threadWantsSharedResources();
 +    makeReaderThread(this.isReceiver);
 +    try {
 +      if (useNIO()) {
 +        runNioReader();
 +      } else {
 +        runOioReader();
 +      }
 +    } finally {
 +      // bug36060: do the socket close within a finally block
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("Stopping {} for {}", p2pReaderName(), remoteAddr);
 +      }
 +      initiateSuspicionIfSharedUnordered();
 +      if (this.isReceiver) {
 +        if (!this.sharedResource) {
 +          this.owner.owner.stats.incThreadOwnedReceivers(-1L, dominoCount.get());
 +        }
 +        asyncClose(false);
 +        this.owner.removeAndCloseThreadOwnedSockets();
 +      }
 +      ByteBuffer tmp = this.nioInputBuffer;
 +      if(tmp != null) {
 +        this.nioInputBuffer = null;
 +        final DMStats stats = this.owner.getConduit().stats;
 +        Buffers.releaseReceiveBuffer(tmp, stats);
 +      }
 +      // make sure that if the reader thread exits we notify a thread waiting
 +      // for the handshake.
 +      // see bug 37524 for an example of listeners hung in waitForHandshake
 +      notifyHandshakeWaiter(false);
 +      this.readerThread.setName("unused p2p reader");
 +      synchronized (this.stateLock) {
 +        this.isRunning = false;
 +        this.readerThread = null;
 +      }
 +    } // finally
 +  }
 +
 +  private String p2pReaderName() {
 +    StringBuffer sb = new StringBuffer(64);
 +    if (this.isReceiver) {
 +      sb.append("P2P message reader@");
 +    } else {
 +      sb.append("P2P handshake reader@");
 +    }
 +    sb.append(Integer.toHexString(System.identityHashCode(this)));
 +    if (!this.isReceiver) {
 +      sb.append('-')
 +        .append(getUniqueId());
 +    }
 +    return sb.toString();
 +  }
 +
 +  private void runNioReader() {
 +    // take a snapshot of uniqueId to detect reconnect attempts; see bug 37592
 +    SocketChannel channel = null;
 +    try {
 +      channel = getSocket().getChannel();
 +      channel.configureBlocking(true);
 +    } catch (ClosedChannelException e) {
 +      // bug 37693: the channel was asynchronously closed.  Our work
 +      // is done.
 +      try { 
 +        requestClose(LocalizedStrings.Connection_RUNNIOREADER_CAUGHT_CLOSED_CHANNEL.toLocalizedString()); 
 +      } catch (Exception ignore) {}      
 +      return; // exit loop and thread
 +    } catch (IOException ex) {
 +      if (stopped || owner.getConduit().getCancelCriterion().cancelInProgress() != null) {
 +        try { 
 +          requestClose(LocalizedStrings.Connection_RUNNIOREADER_CAUGHT_SHUTDOWN.toLocalizedString());
 +        } catch (Exception ignore) {}
 +        return; // bug37520: exit loop (and thread)
 +      }
 +      logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_FAILED_SETTING_CHANNEL_TO_BLOCKING_MODE_0, ex));
 +      this.readerShuttingDown = true;
 +      try { requestClose(LocalizedStrings.Connection_FAILED_SETTING_CHANNEL_TO_BLOCKING_MODE_0.toLocalizedString(ex)); } catch (Exception ignore) {}
 +      return;
 +    }
 +
 +    if (!stopped) {
 +//      Assert.assertTrue(owner != null, "How did owner become null");
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("Starting {}", p2pReaderName());
 +      }
 +    }
 +    // we should not change the state of the connection if we are a handshake reader thread
 +    // as there is a race between this thread and the application thread doing direct ack
 +    // fix for #40869
 +    boolean isHandShakeReader = false;
 +    try {
 +      for (;;) {
 +        if (stopped) {
 +          break;
 +        }
 +        if (SystemFailure.getFailure() != null) {
 +          // Allocate no objects here!
 +          Socket s = this.socket;
 +          if (s != null) {
 +            try {
 +              s.close();
 +            }
 +            catch (IOException e) {
 +              // don't care
 +            }
 +          }
 +          SystemFailure.checkFailure(); // throws
 +        }
 +        if (this.owner.getConduit().getCancelCriterion().cancelInProgress() != null) {
 +          break;
 +        }
 +
 +        try {
 +          ByteBuffer buff = getNIOBuffer();
 +          synchronized(stateLock) {
 +            connectionState = STATE_READING;
 +          }
 +          int amt = channel.read(buff);
 +          synchronized(stateLock) {
 +            connectionState = STATE_IDLE;
 +          }
 +          if (amt == 0) {
 +            continue;
 +          }
 +          if (amt < 0) {
 +            this.readerShuttingDown = true;
 +            try {
 +              requestClose("SocketChannel.read returned EOF");
 +              requestClose(LocalizedStrings.Connection_SOCKETCHANNEL_READ_RETURNED_EOF.toLocalizedString());
 +            } catch (Exception e) {
 +              // ignore - shutting down
 +            }
 +            return;
 +          }
 +
 +          processNIOBuffer();
 +          if (!this.isReceiver
 +              && (this.handshakeRead || this.handshakeCancelled)) {
 +            if (logger.isDebugEnabled()) {
 +              if (this.handshakeRead) {
 +                logger.debug("{} handshake has been read {}", p2pReaderName(), this);
 +              } else {
 +                logger.debug("{} handshake has been cancelled {}", p2pReaderName(), this);
 +              }
 +            }
 +            isHandShakeReader = true;
 +            // Once we have read the handshake the reader can go away
 +            break;
 +          }
 +        }
 +        catch (CancelException e) {
 +          if (logger.isDebugEnabled()) {
 +            logger.debug("{} Terminated <{}> due to cancellation", p2pReaderName(), this, e);
 +          }
 +          this.readerShuttingDown = true;
 +          try { 
 +            requestClose(LocalizedStrings.Connection_CACHECLOSED_IN_CHANNEL_READ_0.toLocalizedString(e));
 +          } catch (Exception ex) {}
 +          return;
 +        }
 +        catch (ClosedChannelException e) {
 +          this.readerShuttingDown = true;
 +          try { 
 +            requestClose(LocalizedStrings.Connection_CLOSEDCHANNELEXCEPTION_IN_CHANNEL_READ_0.toLocalizedString(e));
 +          } catch (Exception ex) {}
 +          return;
 +        }
 +        catch (IOException e) {
 +          if (! isSocketClosed()
 +                && !"Socket closed".equalsIgnoreCase(e.getMessage()) // needed for Solaris jdk 1.4.2_08
 +                ) {
 +            if (logger.isDebugEnabled() && !isIgnorableIOException(e)) {
 +              logger.debug("{} io exception for {}", p2pReaderName(), this, e);
 +            }
 +            if(e.getMessage().contains("interrupted by a call to WSACancelBlockingCall")) {
 +              if (logger.isDebugEnabled()) {
 +                logger.debug("{} received unexpected WSACancelBlockingCall exception, which may result in a hang", p2pReaderName()); 
 +              }
 +            }
 +          }
 +          this.readerShuttingDown = true;
 +          try { 
 +            requestClose(LocalizedStrings.Connection_IOEXCEPTION_IN_CHANNEL_READ_0.toLocalizedString(e));
 +          } catch (Exception ex) {}
 +          return;
 +
 +        } catch (Exception e) {
 +          this.owner.getConduit().getCancelCriterion().checkCancelInProgress(null); // bug 37101
 +          if (!stopped && ! isSocketClosed() ) {
 +            logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_0_EXCEPTION_IN_CHANNEL_READ, p2pReaderName()), e);
 +          }
 +          this.readerShuttingDown = true;
 +          try { 
 +            requestClose(LocalizedStrings.Connection_0_EXCEPTION_IN_CHANNEL_READ.toLocalizedString(e)); 
 +          } catch (Exception ex) {}
 +          return;
 +        }
 +      } // for
 +    }
 +    finally {
 +      if (!isHandShakeReader) {
 +        synchronized(stateLock) {
 +          connectionState = STATE_IDLE;
 +        }
 +      }
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("{} runNioReader terminated id={} from {}", p2pReaderName(), conduitIdStr, remoteAddr);
 +      }
 +    }
 +  }
 +
 +  /** initiate suspect processing if a shared/ordered connection is lost and we're not shutting down */
 +  private void initiateSuspicionIfSharedUnordered() {
 +    if (this.isReceiver && this.handshakeRead && !this.preserveOrder && this.sharedResource) {
 +      if (this.owner.getConduit().getCancelCriterion().cancelInProgress() == null) {
 +        this.owner.getDM().getMembershipManager().suspectMember(this.getRemoteAddress(),
 +            INITIATING_SUSPECT_PROCESSING);
 +      }
 +    }
 +  }
 +
 +  /**
 +   * checks to see if an exception should not be logged: i.e., "forcibly closed",
 +   * "reset by peer", or "connection reset"
 +   * */
 +  public static final boolean isIgnorableIOException(Exception e) {
 +    if (e instanceof ClosedChannelException) {
 +      return true;
 +    }
 +
 +    String msg = e.getMessage();
 +    if (msg == null) {
 +      msg = e.toString();
 +    }
 +
 +    msg = msg.toLowerCase();
 +    return (msg.indexOf("forcibly closed") >= 0)
 +      ||   (msg.indexOf("reset by peer")   >= 0)
 +      ||   (msg.indexOf("connection reset")   >= 0);
 +  }
 +
 +  private static boolean validMsgType(int msgType) {
 +    return msgType == NORMAL_MSG_TYPE
 +      || msgType == CHUNKED_MSG_TYPE
 +      || msgType == END_CHUNKED_MSG_TYPE;
 +  }
 +
 +  private void closeAllMsgDestreamers() {
 +    synchronized (this.destreamerLock) {
 +      if (this.idleMsgDestreamer != null) {
 +        this.idleMsgDestreamer.close();
 +        this.idleMsgDestreamer = null;
 +      }
 +      if (this.destreamerMap != null) {
 +        Iterator it = this.destreamerMap.values().iterator();
 +        while (it.hasNext()) {
 +          MsgDestreamer md = (MsgDestreamer)it.next();
 +          md.close();
 +        }
 +        this.destreamerMap = null;
 +      }
 +    }
 +  }
 +
 +  MsgDestreamer obtainMsgDestreamer(short msgId, final Version v) {
 +    synchronized (this.destreamerLock) {
 +      if (this.destreamerMap == null) {
 +        this.destreamerMap = new HashMap();
 +      }
 +      Short key = new Short(msgId);
 +      MsgDestreamer result = (MsgDestreamer)this.destreamerMap.get(key);
 +      if (result == null) {
 +        result = this.idleMsgDestreamer;
 +        if (result != null) {
 +          this.idleMsgDestreamer = null;
 +        } else {
 +          result = new MsgDestreamer(this.owner.getConduit().stats, 
 +              this.owner.owner.getCancelCriterion(), v);
 +        }
 +        result.setName(p2pReaderName() + " msgId=" + msgId);
 +        this.destreamerMap.put(key, result);
 +      }
 +      return result;
 +    }
 +  }
 +  void releaseMsgDestreamer(short msgId, MsgDestreamer md) {
 +    Short key = new Short(msgId);
 +    synchronized (this.destreamerLock) {
 +      this.destreamerMap.remove(key);
 +      if (this.idleMsgDestreamer == null) {
 +        md.reset();
 +        this.idleMsgDestreamer = md;
 +      } else {
 +        md.close();
 +      }
 +    }
 +  }
 +
 +  private void sendFailureReply(int rpId, String exMsg, Throwable ex, boolean directAck) {
 +    ReplySender dm = null;
 +    if(directAck) {
 +      dm = new DirectReplySender(this);
 +    } else if(rpId != 0) {
 +      dm = this.owner.getDM();
 +    }
 +    if (dm != null) {
 +      ReplyMessage.send(getRemoteAddress(), rpId, new ReplyException(exMsg, ex), dm);
 +    }
 +  }
 +  private void runOioReader() {
 +    InputStream input = null;
 +    try {
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("Socket is of type: {}", getSocket().getClass() );
 +      }
 +      input = new BufferedInputStream(getSocket().getInputStream(), INITIAL_CAPACITY);
 +    }
 +    catch (IOException io) {
 +      if (stopped || owner.getConduit().getCancelCriterion().cancelInProgress() != null) {
 +        return; // bug 37520: exit run loop (and thread)
 +      }
 +      logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_UNABLE_TO_GET_INPUT_STREAM), io);
 +      stopped = true;
 +    }
 +
 +    if (!stopped) {
 +      Assert.assertTrue(owner != null, LocalizedStrings.Connection_OWNER_SHOULD_NOT_BE_NULL.toLocalizedString());
 +      if (logger.isDebugEnabled()) {
 +        logger.debug("Starting {}", p2pReaderName());
 +      }
 +    }
 +
 +    byte[] lenbytes = new byte[MSG_HEADER_BYTES];
 +
 +    final ByteArrayDataInput dis = new ByteArrayDataInput();
 +    while (!stopped) {
 +      try {
 +        if (SystemFailure.getFailure() != null) {
 +          // Allocate no objects here!
 +          Socket s = this.socket;
 +          if (s != null) {
 +            try {
 +              s.close();
 +            }
 +            catch (IOException e) {
 +              // don't care
 +            }
 +          }
 +          SystemFailure.checkFailure(); // throws
 +        }
 +        if (this.owner.getConduit().getCancelCriterion().cancelInProgress() != null) {
 +          break;
 +        }
 +        int len = 0;
 +        if (readFully(input, lenbytes, lenbytes.length) < 0) {
 +          stopped = true;
 +          continue;
 +        }
 +//        long recvNanos = DistributionStats.getStatTime();
 +        len = ((lenbytes[MSG_HEADER_SIZE_OFFSET]&0xff) * 0x1000000) +
 +          ((lenbytes[MSG_HEADER_SIZE_OFFSET+1]&0xff) * 0x10000) +
 +          ((lenbytes[MSG_HEADER_SIZE_OFFSET+2]&0xff) * 0x100) +
 +          (lenbytes[MSG_HEADER_SIZE_OFFSET+3]&0xff);
 +        /*byte msgHdrVersion =*/ calcHdrVersion(len);
 +        len = calcMsgByteSize(len);
 +        int msgType = lenbytes[MSG_HEADER_TYPE_OFFSET];
 +        short msgId = (short)((lenbytes[MSG_HEADER_ID_OFFSET]&0xff * 0x100)
 +                              + (lenbytes[MSG_HEADER_ID_OFFSET+1]&0xff));
 +        boolean myDirectAck = (msgType & DIRECT_ACK_BIT) != 0;
 +        if (myDirectAck) {
 +          msgType &= ~DIRECT_ACK_BIT; // clear the bit
 +        }
 +        // Following validation fixes bug 31145
 +        if (!validMsgType(msgType)) {
 +          logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_UNKNOWN_P2P_MESSAGE_TYPE_0, Integer.valueOf(msgType)));
 +          this.readerShuttingDown = true;
 +          requestClose(LocalizedStrings.Connection_UNKNOWN_P2P_MESSAGE_TYPE_0.toLocalizedString(Integer.valueOf(msgType)));
 +          break;
 +        }
 +        if (logger.isTraceEnabled())
 +          logger.trace("{} reading {} bytes", conduitIdStr, len);
 +        byte[] bytes = new byte[len];
 +        if (readFully(input, bytes, len) < 0) {
 +          stopped = true;
 +          continue;
 +        }
 +        boolean interrupted = Thread.interrupted();
 +        try {
 +          if (this.handshakeRead) {
 +            if (msgType == NORMAL_MSG_TYPE) {
 +              //DMStats stats = this.owner.getConduit().stats;
 +              //long start = DistributionStats.getStatTime();
 +              this.owner.getConduit().stats.incMessagesBeingReceived(true, len);
 +              dis.initialize(bytes, this.remoteVersion);
 +              DistributionMessage msg = null;
 +              try {
 +                ReplyProcessor21.initMessageRPId();
 +                long startSer = this.owner.getConduit().stats.startMsgDeserialization();
 +                msg = (DistributionMessage)InternalDataSerializer.readDSFID(dis);
 +                this.owner.getConduit().stats.endMsgDeserialization(startSer);
 +                if (dis.available() != 0) {
 +                  logger.warn(LocalizedMessage.create(
 +                      LocalizedStrings.Connection_MESSAGE_DESERIALIZATION_OF_0_DID_NOT_READ_1_BYTES,
 +                      new Object[] { msg, Integer.valueOf(dis.available())}));
 +                }
 +                //stats.incBatchCopyTime(start);
 +                try {
 +                  //start = DistributionStats.getStatTime();
 +                  if (!dispatchMessage(msg, len, myDirectAck)) {
 +                    continue;
 +                  }
 +                  //stats.incBatchSendTime(start);
 +                }
 +                catch (MemberShunnedException e) {
 +                  continue;
 +                }
 +                catch (Exception de) {
 +                  this.owner.getConduit().getCancelCriterion().checkCancelInProgress(de); // bug 37101
 +                  logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_ERROR_DISPATCHING_MESSAGE), de);
 +                }
 +              }
 +              catch (VirtualMachineError err) {
 +                SystemFailure.initiateFailure(err);
 +                // If this ever returns, rethrow the error.  We're poisoned
 +                // now, so don't let this thread continue.
 +                throw err;
 +              }
 +              catch (Throwable e) {
 +                // Whenever you catch Error or Throwable, you must also
 +                // catch VirtualMachineError (see above).  However, there is
 +                // _still_ a possibility that you are dealing with a cascading
 +                // error condition, so you also need to check to see if the JVM
 +                // is still usable:
 +                SystemFailure.checkFailure();
 +                // In particular I want OutOfMem to be caught here
 +                if (!myDirectAck) {
 +                  String reason = LocalizedStrings.Connection_ERROR_DESERIALIZING_MESSAGE.toLocalizedString();
 +                  sendFailureReply(ReplyProcessor21.getMessageRPId(), reason, e, myDirectAck);
 +                }
 +                if(e instanceof CancelException) {
 +                  if (!(e instanceof CacheClosedException)) {
 +                    // Just log a message if we had trouble deserializing due to CacheClosedException; see bug 43543
 +                    throw (CancelException) e;
 +                  }
 +                }
 +                logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_ERROR_DESERIALIZING_MESSAGE), e);
 +                //requestClose();
 +                //return;
 +              } finally {
 +                ReplyProcessor21.clearMessageRPId();
 +              }
 +            } else if (msgType == CHUNKED_MSG_TYPE) {
 +              MsgDestreamer md = obtainMsgDestreamer(msgId, remoteVersion);
 +              this.owner.getConduit().stats.incMessagesBeingReceived(md.size() == 0, len);
 +              try {
 +                md.addChunk(bytes);
 +              } catch (IOException ex) {
 +                logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_FAILED_HANDLING_CHUNK_MESSAGE), ex);
 +              }
 +            } else /* (msgType == END_CHUNKED_MSG_TYPE) */ {
 +              MsgDestreamer md = obtainMsgDestreamer(msgId, remoteVersion);
 +              this.owner.getConduit().stats.incMessagesBeingReceived(md.size() == 0, len);
 +              try {
 +                md.addChunk(bytes);
 +     

<TRUNCATED>