Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. public class HZLauncher {
  2. private final static Logger logger = LoggerFactory.getLogger(HZLauncher.class);
  3. public static final int WAIT_NODES_TIME_IN_SECOND = 1;
  4. public static final int MAX_WAIT_NODES_TIME_IN_SECOND = 30;
  5.  
  6. public static void main(String args[]) throws Exception {
  7. int nodeCount = parseNodeCount(args, 1);
  8. HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(loadConfig(args));
  9. waitForNodes(nodeCount, hazelcastInstance);
  10. initStoredMaps(hazelcastInstance);
  11.  
  12. installAllocationSamplerIfNeeded(args);
  13. }
  14.  
  15. private static void initStoredMaps(HazelcastInstance hazelcastInstance) {
  16. final Map<String, MapConfig> mapConfigs = hazelcastInstance.getConfig().getMapConfigs();
  17. ArrayList<String> storesNames = new ArrayList<>();
  18. for (Map.Entry<String, MapConfig> ent : mapConfigs.entrySet()) {
  19. final String name = ent.getKey();
  20. final MapConfig config = ent.getValue();
  21. if (config.getMapStoreConfig() != null && config.getMapStoreConfig().isEnabled()) {
  22. storesNames.add(name);
  23. }
  24. }
  25.  
  26. logger.info("maps with store: {}", storesNames);
  27. for (Map.Entry<String, MapConfig> ent : mapConfigs.entrySet()) {
  28. final String name = ent.getKey();
  29. final MapConfig config = ent.getValue();
  30. if (config.getMapStoreConfig() != null && config.getMapStoreConfig().isEnabled()) {
  31. IMap imap = hazelcastInstance.getMap(name);
  32. logger.info("stored map {} size: {}", imap.getName(), imap.size());
  33. }
  34. }
  35. logger.info("maps with store are ready");
  36. }
  37.  
  38. private static void waitForNodes(int nodeCount, HazelcastInstance hazelcastInstance) throws InterruptedException {
  39. int i = 0;
  40. while (hazelcastInstance.getCluster().getMembers().size() < nodeCount &&
  41. i * WAIT_NODES_TIME_IN_SECOND < MAX_WAIT_NODES_TIME_IN_SECOND) {
  42. logger.info("wait other nodes. node count: {}, joined: {}, seconds: {}",
  43. nodeCount, hazelcastInstance.getCluster().getMembers().size(), i * WAIT_NODES_TIME_IN_SECOND);
  44. Thread.sleep(WAIT_NODES_TIME_IN_SECOND * 1000);
  45. i++;
  46. }
  47. Thread.sleep(2000);
  48. }
  49.  
  50. private static int parseNodeCount(String[] args, int defNodeCount) {
  51. String nodeCountStr = getArg(args, "nodeCount");
  52. if (nodeCountStr != null) {
  53. try {
  54. return Integer.parseInt(nodeCountStr);
  55. } catch (NumberFormatException e) {
  56. logger.error("error parse nodeCount", e);
  57. }
  58. }
  59. return defNodeCount;
  60. }
  61.  
  62. private static Config loadConfig(String[] args) throws FileNotFoundException {
  63. Config c1 = null;
  64. String argPath = getArg(args, "config");
  65. if (argPath != null) {
  66. String homePath = System.getProperty("user.home");
  67. File localConfigFile = new File(homePath + "/" + argPath);
  68.  
  69. if (!localConfigFile.exists()) {
  70. c1 = new ClasspathXmlConfig(argPath);
  71. } else {
  72. c1 = new FileSystemXmlConfig(localConfigFile);
  73. }
  74. }
  75. return c1;
  76. }
  77.  
  78. private static void installAllocationSamplerIfNeeded(String[] args) {
  79. final String threadOfInterest = getArg(args, "allo-sample-thread");
  80. final String sampleRateStr = getArg(args, "allo-sample-rate");
  81.  
  82. final boolean sampleEnabled = sampleRateStr != null;
  83.  
  84. if (sampleEnabled) {
  85. final String classFilterDesc = getArg(args, "allo-sample-class");
  86. final Boolean classFilterIsArray = Boolean.parseBoolean(getArg(args, "allo-sample-class-isarray"));
  87.  
  88. AllocationRecorder.addSampler(
  89. new AllocationRecorderSampler(threadOfInterest,
  90. new AllocationRecorderSampler.ClassFilter(classFilterDesc, classFilterIsArray),
  91. Integer.parseInt(sampleRateStr)));
  92. }
  93. }
  94.  
  95. private static String getArg(String[] args, String name) {
  96. int argPos = -1;
  97. for (int i = 0; i < args.length; i ++) {
  98. if (args[i].equals(name)) {
  99. argPos = i + 1;
  100. }
  101. }
  102.  
  103. if (argPos < 0 || argPos > args.length - 1) {
  104. return null;
  105. } else {
  106. return args[argPos];
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement