/hazelcast/src/main/java/com/hazelcast/examples/LongRunningTest.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 248 lines · 209 code · 22 blank · 17 comment · 17 complexity · 416b0c21d3bbc4888aa383d7b0e02149 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.hazelcast.examples;
  17. import com.hazelcast.core.Hazelcast;
  18. import com.hazelcast.core.HazelcastInstance;
  19. import com.hazelcast.util.Clock;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.concurrent.CopyOnWriteArrayList;
  23. import java.util.concurrent.ExecutorService;
  24. import java.util.concurrent.Executors;
  25. import java.util.concurrent.TimeUnit;
  26. import java.util.concurrent.atomic.AtomicLong;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. public class LongRunningTest {
  30. private static final int STATS_SECONDS = 10;
  31. private List<TheNode> nodes = new CopyOnWriteArrayList<TheNode>();
  32. private int nodeIdGen = 0;
  33. private final Logger logger = Logger.getLogger(LongRunningTest.class.getName());
  34. private int starts, stops, restarts = 0;
  35. public static void main(String[] args) {
  36. LongRunningTest t = new LongRunningTest();
  37. t.run();
  38. }
  39. public void run() {
  40. Runtime.getRuntime().addShutdownHook(new Thread() {
  41. public void run() {
  42. log("Shutting down " + nodes.size());
  43. while (nodes.size() > 0) {
  44. removeNode();
  45. }
  46. }
  47. });
  48. while (true) {
  49. if (nodes.size() > 4) {
  50. removeNode();
  51. } else if (nodes.size() == 0) {
  52. addNode();
  53. addNode();
  54. addNode();
  55. } else if (nodes.size() < 2) {
  56. addNode();
  57. } else {
  58. int action = random(3);
  59. switch (action) {
  60. case 0:
  61. removeNode();
  62. break;
  63. case 1:
  64. addNode();
  65. break;
  66. case 2:
  67. restartNode();
  68. break;
  69. }
  70. }
  71. try {
  72. int nextSeconds = random(60, 260);
  73. log("Next Action after " + nextSeconds + " seconds.");
  74. log("members:" + nodes.size() + ", starts: " + starts + ", stops:" + stops + ", restart:" + restarts);
  75. //noinspection BusyWait
  76. Thread.sleep(nextSeconds * 1000);
  77. } catch (InterruptedException e) {
  78. }
  79. }
  80. }
  81. void log(Object obj) {
  82. logger.log(Level.INFO, "LRT-" + obj);
  83. }
  84. void addNode() {
  85. starts++;
  86. int entryCount = random(10000);
  87. int threadCount = random(10, 50);
  88. int valueSizeMax = (entryCount < 1000) ? 50000 : 1000;
  89. int valueSize = random(10, valueSizeMax);
  90. TheNode node = new TheNode(nodeIdGen++, entryCount, threadCount, valueSize);
  91. nodes.add(node);
  92. node.start();
  93. log("Started " + node);
  94. }
  95. void restartNode() {
  96. restarts++;
  97. log("Restarting...");
  98. removeNode();
  99. try {
  100. Thread.sleep(random(10) * 1000);
  101. } catch (InterruptedException e) {
  102. }
  103. addNode();
  104. }
  105. void removeNode() {
  106. stops++;
  107. TheNode node = nodes.remove(random(nodes.size()));
  108. node.stop();
  109. log("Stopped " + node);
  110. }
  111. int random(int length) {
  112. return ((int) (Math.random() * 10000000) % length);
  113. }
  114. int random(int from, int to) {
  115. double diff = (to - from);
  116. return (int) (diff * Math.random() + from);
  117. }
  118. class TheNode {
  119. final int entryCount;
  120. final int threadCount;
  121. final int valueSize;
  122. final int nodeId;
  123. final long createTime;
  124. final ExecutorService es;
  125. final ExecutorService esStats;
  126. final HazelcastInstance hazelcast;
  127. volatile boolean running = true;
  128. TheNode(int nodeId, int entryCount, int threadCount, int valueSize) {
  129. this.entryCount = entryCount;
  130. this.threadCount = threadCount;
  131. this.valueSize = valueSize;
  132. this.nodeId = nodeId;
  133. es = Executors.newFixedThreadPool(threadCount);
  134. hazelcast = Hazelcast.newHazelcastInstance(null);
  135. esStats = Executors.newSingleThreadExecutor();
  136. createTime = Clock.currentTimeMillis();
  137. }
  138. public void stop() {
  139. try {
  140. running = false;
  141. es.shutdown();
  142. es.awaitTermination(10, TimeUnit.SECONDS);
  143. esStats.shutdown();
  144. hazelcast.shutdown();
  145. } catch (Throwable t) {
  146. t.printStackTrace();
  147. }
  148. }
  149. public void start() {
  150. final Stats stats = new Stats();
  151. for (int i = 0; i < threadCount; i++) {
  152. es.submit(new Runnable() {
  153. public void run() {
  154. Map<String, byte[]> map = hazelcast.getMap("default");
  155. while (running) {
  156. try {
  157. int key = (int) (Math.random() * entryCount);
  158. int operation = ((int) (Math.random() * 100)) % 10;
  159. if (operation < 4) {
  160. map.put(String.valueOf(key), new byte[valueSize]);
  161. stats.mapPuts.incrementAndGet();
  162. } else if (operation < 8) {
  163. map.get(String.valueOf(key));
  164. stats.mapGets.incrementAndGet();
  165. } else {
  166. map.remove(String.valueOf(key));
  167. stats.mapRemoves.incrementAndGet();
  168. }
  169. } catch (Throwable ignored) {
  170. }
  171. }
  172. }
  173. });
  174. }
  175. esStats.submit(new Runnable() {
  176. public void run() {
  177. while (running) {
  178. try {
  179. //noinspection BusyWait
  180. Thread.sleep(STATS_SECONDS * 1000);
  181. int clusterSize = hazelcast.getCluster().getMembers().size();
  182. Stats currentStats = stats.getAndReset();
  183. log("Cluster size: " + clusterSize + ", Operations per Second: "
  184. + (currentStats.total() / STATS_SECONDS));
  185. } catch (Exception e) {
  186. e.printStackTrace();
  187. }
  188. }
  189. }
  190. });
  191. }
  192. @Override
  193. public String toString() {
  194. return "TheNode{" +
  195. "nodeId=" + nodeId +
  196. ", entryCount=" + entryCount +
  197. ", threadCount=" + threadCount +
  198. ", valueSize=" + valueSize +
  199. ", liveSeconds=" + ((Clock.currentTimeMillis() - createTime) / 1000) +
  200. ", running=" + running +
  201. '}';
  202. }
  203. }
  204. class Stats {
  205. public AtomicLong mapPuts = new AtomicLong();
  206. public AtomicLong mapGets = new AtomicLong();
  207. public AtomicLong mapRemoves = new AtomicLong();
  208. public Stats getAndReset() {
  209. long mapPutsNow = mapPuts.getAndSet(0);
  210. long mapGetsNow = mapGets.getAndSet(0);
  211. long mapRemovesNow = mapRemoves.getAndSet(0);
  212. Stats newOne = new Stats();
  213. newOne.mapPuts.set(mapPutsNow);
  214. newOne.mapGets.set(mapGetsNow);
  215. newOne.mapRemoves.set(mapRemovesNow);
  216. return newOne;
  217. }
  218. public long total() {
  219. return mapPuts.get() + mapGets.get() + mapRemoves.get();
  220. }
  221. public String toString() {
  222. return "total= " + total() + ", puts:" + mapPuts.get() + ", gets:" + mapGets.get()
  223. + ", remove:" + mapRemoves.get();
  224. }
  225. }
  226. }