PageRenderTime 165ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/water/deploy/Cloud.java

https://gitlab.com/alvinahmadov2/h2o-2
Java | 162 lines | 130 code | 14 blank | 18 comment | 16 complexity | cd0b69a3d5a7fb4eccddc9be844e3493 MD5 | raw file
  1. package water.deploy;
  2. import java.io.File;
  3. import java.io.Serializable;
  4. import java.util.*;
  5. import water.*;
  6. import water.H2O.FlatFileEntry;
  7. import water.deploy.VM.Params;
  8. import water.deploy.VM.Watchdog;
  9. import water.util.Log;
  10. import water.util.Utils;
  11. /**
  12. * Deploys and starts a remote cluster.
  13. * <br>
  14. * Note: This class is intended for debug and experimentation purposes only, please refer to the
  15. * documentation to run an H2O cluster.
  16. */
  17. public class Cloud {
  18. public final List<String> publicIPs = new ArrayList<String>();
  19. public final List<String> privateIPs = new ArrayList<String>();
  20. /** Includes for rsync to the master */
  21. public final Set<String> clientRSyncIncludes = new HashSet<String>();
  22. /** Excludes for rsync to the master */
  23. public final Set<String> clientRSyncExcludes = new HashSet<String>();
  24. /** Includes for rsync between the master and slaves */
  25. public final Set<String> fannedRSyncIncludes = new HashSet<String>();
  26. /** Excludes for rsync between the master and slaves */
  27. public final Set<String> fannedRSyncExcludes = new HashSet<String>();
  28. /** Port for all remote machines. */
  29. public static final int PORT = 54423;
  30. public static final int FORWARDED_LOCAL_PORT = 54321;
  31. /**
  32. * To avoid configuring remote machines, a JVM can be sent through rsync with H2O. By default,
  33. * decompress the Oracle Linux x64 JDK to a local folder and point this path to it.
  34. */
  35. static final String JRE = null; // System.getProperty("user.home") + "/libs/jdk/jre";
  36. /** Watch dogs are additional JVMs that shutdown the cluster when the client is killed */
  37. static final boolean WATCHDOGS = true;
  38. static final String FLATFILE = "flatfile";
  39. public void start(String[] java_args, String[] args) {
  40. // Take first box as cloud master
  41. Host master = new Host(publicIPs.get(0));
  42. Set<String> incls = new HashSet<String>(clientRSyncIncludes);
  43. if( JRE != null && !new File(JRE + "/bin/java").exists() )
  44. throw new IllegalArgumentException("Invalid JRE");
  45. if( JRE != null )
  46. incls.add(JRE);
  47. List<String> ips = privateIPs.size() > 0 ? privateIPs : publicIPs;
  48. String s = "";
  49. for( Object o : ips )
  50. s += (s.length() == 0 ? "" : '\n') + o.toString() + ":" + PORT;
  51. File flatfile = Utils.writeFile(new File(Utils.tmp(), FLATFILE), s);
  52. incls.add(flatfile.getAbsolutePath());
  53. master.rsync(incls, clientRSyncExcludes, false);
  54. ArrayList<String> list = new ArrayList<String>();
  55. list.add("-mainClass");
  56. list.add(Master.class.getName());
  57. CloudParams p = new CloudParams();
  58. p._incls = new HashSet<String>(fannedRSyncIncludes);
  59. p._excls = fannedRSyncExcludes;
  60. p._incls.add(FLATFILE);
  61. if( JRE != null )
  62. p._incls.add(new File(JRE).getName());
  63. list.add(VM.write(p));
  64. list.addAll(Arrays.asList(args));
  65. String[] java = Utils.append(java_args, NodeVM.class.getName());
  66. Params params = new Params(master, java, list.toArray(new String[0]));
  67. if( WATCHDOGS ) {
  68. SSHWatchdog r = new SSHWatchdog(params);
  69. r.inheritIO();
  70. r.start();
  71. } else {
  72. try {
  73. SSHWatchdog.run(params);
  74. } catch( Exception e ) {
  75. throw new RuntimeException(e);
  76. }
  77. }
  78. }
  79. static class CloudParams implements Serializable {
  80. Set<String> _incls, _excls;
  81. }
  82. static class SSHWatchdog extends Watchdog {
  83. public SSHWatchdog(Params p) {
  84. super(javaArgs(SSHWatchdog.class.getName()), new String[] { write(p) });
  85. }
  86. public static void main(String[] args) throws Exception {
  87. exitWithParent();
  88. Params p = read(args[0]);
  89. run(p);
  90. }
  91. static void run(Params p) throws Exception {
  92. Host host = new Host(p._host[0], p._host[1], p._host[2]);
  93. String key = host.key() != null ? host.key() : "";
  94. String s = "ssh-agent sh -c \"ssh-add " + key + "; ssh -l " + host.user() + " -A" + Host.SSH_OPTS;
  95. s += " -L " + FORWARDED_LOCAL_PORT + ":127.0.0.1:" + PORT; // Port forwarding
  96. s += " " + host.address() + " '" + SSH.command(p._java, p._node) + "'\"";
  97. s = s.replace("\\", "\\\\").replace("$", "\\$");
  98. ArrayList<String> list = new ArrayList<String>();
  99. // Have to copy to file for cygwin, but works also on -nix
  100. File sh = Utils.writeFile(s);
  101. File onWindows = new File("C:/cygwin/bin/bash.exe");
  102. if( onWindows.exists() ) {
  103. list.add(onWindows.getPath());
  104. list.add("--login");
  105. } else
  106. list.add("bash");
  107. list.add(sh.getAbsolutePath());
  108. exec(list);
  109. }
  110. }
  111. public static class Master {
  112. public static void main(String[] args) throws Exception {
  113. VM.exitWithParent();
  114. CloudParams params = VM.read(args[0]);
  115. args = Utils.remove(args, 0);
  116. String[] workerArgs = new String[] { "-flatfile", FLATFILE, "-port", "" + PORT };
  117. List<FlatFileEntry> flatfile = H2O.parseFlatFile(new File(FLATFILE));
  118. HashMap<String, Host> hosts = new HashMap<String, Host>();
  119. ArrayList<Node> workers = new ArrayList<Node>();
  120. for( int i = 1; i < flatfile.size(); i++ ) {
  121. Host host = new Host(flatfile.get(i).inet.getHostAddress());
  122. hosts.put(host.address(), host);
  123. workers.add(new NodeHost(host, workerArgs));
  124. }
  125. Host.rsync(hosts.values().toArray(new Host[0]), params._incls, params._excls, false);
  126. for( Node w : workers ) {
  127. w.inheritIO();
  128. w.start();
  129. }
  130. H2O.main(Utils.append(workerArgs, args));
  131. stall_till_cloudsize(1 + workers.size(), 10000); // stall for cloud 10seconds
  132. Log.unwrap(System.out, "");
  133. Log.unwrap(System.out, "Cloud is up, local port " + FORWARDED_LOCAL_PORT + " forwarded");
  134. Log.unwrap(System.out, "Go to http://127.0.0.1:" + FORWARDED_LOCAL_PORT);
  135. Log.unwrap(System.out, "");
  136. int index = Arrays.asList(args).indexOf("-mainClass");
  137. if( index >= 0 ) {
  138. String pack = args[index + 1].substring(0, args[index + 1].lastIndexOf('.'));
  139. LaunchJar.weavePackages(pack);
  140. Boot.run(args);
  141. }
  142. }
  143. public static void stall_till_cloudsize(int x, long ms) {
  144. H2O.waitForCloudSize(x, ms);
  145. UKV.put(Job.LIST, new Job.List()); // Jobs.LIST must be part of initial keys
  146. }
  147. }
  148. }