PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/gemfire-core/src/test/java/com/gemstone/gemfire/internal/cache/ha/HASlowReceiverDUnitTest.java

https://gitlab.com/kidaa/incubator-geode
Java | 281 lines | 226 code | 42 blank | 13 comment | 10 complexity | 503acd6fef88d4fe6a548c642b2be6ce MD5 | raw file
  1. /*=========================================================================
  2. * Copyright (c) 2010-2014 Pivotal Software, Inc. All Rights Reserved.
  3. * This product is protected by U.S. and international copyright
  4. * and intellectual property laws. Pivotal products are covered by
  5. * one or more patents listed at http://www.pivotal.io/patents.
  6. *=========================================================================
  7. */
  8. package com.gemstone.gemfire.internal.cache.ha;
  9. import java.net.SocketException;
  10. import java.util.Properties;
  11. import com.gemstone.gemfire.LogWriter;
  12. import com.gemstone.gemfire.cache.AttributesFactory;
  13. import com.gemstone.gemfire.cache.Cache;
  14. import com.gemstone.gemfire.cache.CacheFactory;
  15. import com.gemstone.gemfire.cache.DataPolicy;
  16. import com.gemstone.gemfire.cache.EntryEvent;
  17. import com.gemstone.gemfire.cache.Region;
  18. import com.gemstone.gemfire.cache.RegionAttributes;
  19. import com.gemstone.gemfire.cache.Scope;
  20. import com.gemstone.gemfire.cache.client.PoolManager;
  21. import com.gemstone.gemfire.cache.client.internal.PoolImpl;
  22. import com.gemstone.gemfire.cache.util.BridgeServer;
  23. import com.gemstone.gemfire.cache.util.CacheListenerAdapter;
  24. import com.gemstone.gemfire.distributed.DistributedSystem;
  25. import com.gemstone.gemfire.distributed.internal.DistributionConfig;
  26. import com.gemstone.gemfire.internal.AvailablePort;
  27. import com.gemstone.gemfire.internal.cache.BridgeObserverAdapter;
  28. import com.gemstone.gemfire.internal.cache.BridgeObserverHolder;
  29. import com.gemstone.gemfire.internal.cache.tier.sockets.CacheServerTestUtil;
  30. import dunit.DistributedTestCase;
  31. import dunit.Host;
  32. import dunit.VM;
  33. public class HASlowReceiverDUnitTest extends DistributedTestCase {
  34. protected static Cache cache = null;
  35. private static VM serverVM1 = null;
  36. protected static VM serverVM2 = null;
  37. protected static VM clientVM = null;
  38. private static int PORT0;
  39. private static int PORT1;
  40. private static int PORT2;
  41. private static final String regionName = "HASlowReceiverDUnitTest";
  42. protected static LogWriter logger = null;
  43. static PoolImpl pool = null;
  44. private static boolean isUnresponsiveClientRemoved = false;
  45. public HASlowReceiverDUnitTest(String name) {
  46. super(name);
  47. }
  48. @Override
  49. public void setUp() throws Exception {
  50. super.setUp();
  51. final Host host = Host.getHost(0);
  52. serverVM1 = host.getVM(1);
  53. serverVM2 = host.getVM(2);
  54. clientVM = host.getVM(3);
  55. PORT0 = createServerCache().intValue();
  56. PORT1 = ((Integer)serverVM1.invoke(HASlowReceiverDUnitTest.class,
  57. "createServerCache")).intValue();
  58. PORT2 = ((Integer)serverVM2.invoke(HASlowReceiverDUnitTest.class,
  59. "createServerCache")).intValue();
  60. }
  61. @Override
  62. public void tearDown2() throws Exception {
  63. super.tearDown2();
  64. clientVM.invoke(HASlowReceiverDUnitTest.class, "closeCache");
  65. // then close the servers
  66. closeCache();
  67. serverVM1.invoke(HASlowReceiverDUnitTest.class, "closeCache");
  68. serverVM2.invoke(HASlowReceiverDUnitTest.class, "closeCache");
  69. disconnectAllFromDS();
  70. }
  71. private void createCache(Properties props) throws Exception {
  72. DistributedSystem ds = getSystem(props);
  73. ds.disconnect();
  74. ds = getSystem(props);
  75. assertNotNull(ds);
  76. cache = CacheFactory.create(ds);
  77. assertNotNull(cache);
  78. }
  79. public static Integer createServerCache() throws Exception {
  80. return createServerCache(null);
  81. }
  82. public static Integer createServerCache(String ePolicy) throws Exception {
  83. return createServerCache(ePolicy, new Integer(1));
  84. }
  85. public static Integer createServerCache(String ePolicy, Integer cap)
  86. throws Exception {
  87. Properties prop = new Properties();
  88. prop.setProperty(DistributionConfig.REMOVE_UNRESPONSIVE_CLIENT_PROP_NAME,
  89. "true");
  90. new HASlowReceiverDUnitTest("temp").createCache(prop);
  91. AttributesFactory factory = new AttributesFactory();
  92. factory.setScope(Scope.DISTRIBUTED_ACK);
  93. factory.setDataPolicy(DataPolicy.REPLICATE);
  94. RegionAttributes attrs = factory.create();
  95. cache.createRegion(regionName, attrs);
  96. logger = cache.getLogger();
  97. int port = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
  98. BridgeServer server1 = cache.addBridgeServer();
  99. server1.setPort(port);
  100. server1.setNotifyBySubscription(true);
  101. server1.setMaximumMessageCount(200);
  102. if (ePolicy != null) {
  103. server1.getClientSubscriptionConfig().setEvictionPolicy(ePolicy);
  104. server1.getClientSubscriptionConfig().setCapacity(cap.intValue());
  105. }
  106. server1.start();
  107. return new Integer(server1.getPort());
  108. }
  109. public static void createClientCache(String host, Integer port1,
  110. Integer port2, Integer port3, Integer rLevel, Boolean addListener)
  111. throws Exception {
  112. CacheServerTestUtil.disableShufflingOfEndpoints();
  113. Properties props = new Properties();
  114. props.setProperty(DistributionConfig.MCAST_PORT_NAME, "0");
  115. props.setProperty(DistributionConfig.LOCATORS_NAME, "");
  116. new HASlowReceiverDUnitTest("temp").createCache(props);
  117. AttributesFactory factory = new AttributesFactory();
  118. PoolImpl p = (PoolImpl)PoolManager.createFactory().addServer("localhost",
  119. port1).addServer("localhost", port2).addServer("localhost", port3)
  120. .setSubscriptionEnabled(true).setSubscriptionRedundancy(
  121. rLevel.intValue()).setThreadLocalConnections(true)
  122. .setMinConnections(6).setReadTimeout(20000).setPingInterval(1000)
  123. .setRetryAttempts(5).create("HASlowRecieverDUnitTestPool");
  124. factory.setScope(Scope.LOCAL);
  125. factory.setPoolName(p.getName());
  126. if (addListener.booleanValue()) {
  127. factory.addCacheListener(new CacheListenerAdapter() {
  128. @Override
  129. public void afterUpdate(EntryEvent event) {
  130. if (event.getNewValue().equals("v20")) {
  131. try {
  132. Thread.sleep(120000);
  133. }
  134. catch (InterruptedException e) {
  135. // TODO Auto-generated catch block
  136. e.printStackTrace();
  137. }
  138. }
  139. }
  140. });
  141. }
  142. RegionAttributes attrs = factory.create();
  143. cache.createRegion(regionName, attrs);
  144. pool = p;
  145. }
  146. public static void createClientCache(String host, Integer port1,
  147. Integer port2, Integer port3, Integer rLevel) throws Exception {
  148. createClientCache(host, port1, port2, port3, rLevel, Boolean.TRUE);
  149. }
  150. public static void registerInterest() {
  151. try {
  152. Region r = cache.getRegion("/" + regionName);
  153. assertNotNull(r);
  154. r.registerInterest("ALL_KEYS");
  155. }
  156. catch (Exception ex) {
  157. fail("failed in registerInterestListAll", ex);
  158. }
  159. }
  160. public static void putEntries() {
  161. try {
  162. Region r = cache.getRegion("/" + regionName);
  163. assertNotNull(r);
  164. for (long i = 0; i < 300; i++) {
  165. r.put("k" + (i % 10), "v" + i);
  166. r.put("k" + (i % 10), new byte[1000]);
  167. }
  168. }
  169. catch (Exception ex) {
  170. fail("failed in putEntries()", ex);
  171. }
  172. }
  173. public static void createEntries(Long num) {
  174. try {
  175. Region r = cache.getRegion("/" + regionName);
  176. assertNotNull(r);
  177. for (long i = 0; i < num.longValue(); i++) {
  178. r.create("k" + i, "v" + i);
  179. }
  180. }
  181. catch (Exception ex) {
  182. fail("failed in createEntries(Long)", ex);
  183. }
  184. }
  185. public static void checkRedundancyLevel(final Integer redundantServers) {
  186. WaitCriterion wc = new WaitCriterion() {
  187. public boolean done() {
  188. return pool.getRedundantNames().size() == redundantServers.intValue();
  189. }
  190. public String description() {
  191. return "Expected redundant count (" + pool.getRedundantNames().size()
  192. + ") to become " + redundantServers.intValue();
  193. }
  194. };
  195. DistributedTestCase.waitForCriterion(wc, 200 * 1000, 1000, true);
  196. }
  197. // Test slow client
  198. public void testSlowClient() throws Exception {
  199. setBridgeObeserverForAfterQueueDestroyMessage();
  200. clientVM.invoke(HASlowReceiverDUnitTest.class, "createClientCache",
  201. new Object[] { getServerHostName(Host.getHost(0)), new Integer(PORT0),
  202. new Integer(PORT1), new Integer(PORT2), new Integer(2) });
  203. clientVM.invoke(HASlowReceiverDUnitTest.class, "registerInterest");
  204. // add expected socket exception string
  205. final ExpectedException ex1 = addExpectedException(SocketException.class
  206. .getName());
  207. final ExpectedException ex2 = addExpectedException(InterruptedException.class
  208. .getName());
  209. putEntries();
  210. Thread.sleep(20000);// wait for put to block and allow server to remove
  211. // client queue
  212. clientVM.invoke(HASlowReceiverDUnitTest.class, "checkRedundancyLevel",
  213. new Object[] { new Integer(2) });
  214. // check for slow client queue is removed or not.
  215. assertTrue("isUnresponsiveClientRemoved is false, but should be true "
  216. + "after 20 seconds", isUnresponsiveClientRemoved);
  217. ex1.remove();
  218. ex2.remove();
  219. }
  220. public static void setBridgeObeserverForAfterQueueDestroyMessage()
  221. throws Exception {
  222. PoolImpl.AFTER_QUEUE_DESTROY_MESSAGE_FLAG = true;
  223. BridgeObserverHolder.setInstance(new BridgeObserverAdapter() {
  224. @Override
  225. public void afterQueueDestroyMessage() {
  226. clientVM.invoke(HASlowReceiverDUnitTest.class, "checkRedundancyLevel",
  227. new Object[] { new Integer(0) });
  228. isUnresponsiveClientRemoved = true;
  229. PoolImpl.AFTER_QUEUE_DESTROY_MESSAGE_FLAG = false;
  230. }
  231. });
  232. }
  233. public static void closeCache() {
  234. if (cache != null && !cache.isClosed()) {
  235. cache.close();
  236. cache.getDistributedSystem().disconnect();
  237. }
  238. }
  239. }