Example #1
0
  /**
   * Starts this server.
   *
   * @throws IgniteCheckedException If failed.
   */
  public void start() throws IgniteCheckedException {
    srvEndpoint = createEndpoint(endpointCfg, mgmt);

    if (U.isWindows() && srvEndpoint instanceof IpcSharedMemoryServerEndpoint)
      throw new IgniteCheckedException(
          IpcSharedMemoryServerEndpoint.class.getSimpleName()
              + " should not be configured on Windows (configure "
              + IpcServerTcpEndpoint.class.getSimpleName()
              + ")");

    if (srvEndpoint instanceof IpcServerTcpEndpoint) {
      IpcServerTcpEndpoint srvEndpoint0 = (IpcServerTcpEndpoint) srvEndpoint;

      srvEndpoint0.setManagement(mgmt);

      if (srvEndpoint0.getHost() == null) {
        if (mgmt) {
          String locHostName = igfsCtx.kernalContext().config().getLocalHost();

          try {
            srvEndpoint0.setHost(U.resolveLocalHost(locHostName).getHostAddress());
          } catch (IOException e) {
            throw new IgniteCheckedException("Failed to resolve local host: " + locHostName, e);
          }
        } else
          // Bind non-management endpoint to 127.0.0.1 by default.
          srvEndpoint0.setHost("127.0.0.1");
      }
    }

    igfsCtx.kernalContext().resource().injectGeneric(srvEndpoint);

    srvEndpoint.start();

    // IpcServerEndpoint.getPort contract states return -1 if there is no port to be registered.
    if (srvEndpoint.getPort() >= 0)
      igfsCtx
          .kernalContext()
          .ports()
          .registerPort(srvEndpoint.getPort(), TCP, srvEndpoint.getClass());

    hnd = new IgfsIpcHandler(igfsCtx, endpointCfg, mgmt);

    // Start client accept worker.
    acceptWorker = new AcceptWorker();
  }
  /**
   * Checks if address can be reached using one argument InetAddress.isReachable() version or ping
   * command if failed.
   *
   * @param addr Address to check.
   * @param reachTimeout Timeout for the check.
   * @return {@code True} if address is reachable.
   */
  public static boolean reachableByPing(InetAddress addr, int reachTimeout) {
    try {
      if (addr.isReachable(reachTimeout)) return true;

      String cmd = String.format("ping -%s 1 %s", U.isWindows() ? "n" : "c", addr.getHostAddress());

      Process myProc = Runtime.getRuntime().exec(cmd);

      myProc.waitFor();

      return myProc.exitValue() == 0;
    } catch (IOException ignore) {
      return false;
    } catch (InterruptedException ignored) {
      Thread.currentThread().interrupt();

      return false;
    }
  }
  /**
   * Run command in separated console.
   *
   * @param workFolder Work folder for command.
   * @param args A string array containing the program and its arguments.
   * @return Started process.
   * @throws IOException If failed to start process.
   */
  public static Process openInConsole(@Nullable File workFolder, String... args)
      throws IOException {
    String[] commands = args;

    String cmd = F.concat(Arrays.asList(args), " ");

    if (U.isWindows()) commands = F.asArray("cmd", "/c", String.format("start %s", cmd));

    if (U.isMacOs())
      commands =
          F.asArray(
              "osascript",
              "-e",
              String.format("tell application \"Terminal\" to do script \"%s\"", cmd));

    if (U.isUnix()) commands = F.asArray("xterm", "-sl", "1024", "-geometry", "200x50", "-e", cmd);

    ProcessBuilder pb = new ProcessBuilder(commands);

    if (workFolder != null) pb.directory(workFolder);

    return pb.start();
  }
  /**
   * Creates new shared memory communication server.
   *
   * @return Server.
   * @throws IgniteCheckedException If failed.
   */
  @Nullable
  private IpcSharedMemoryServerEndpoint resetShmemServer() throws IgniteCheckedException {
    if (boundTcpShmemPort >= 0)
      throw new IgniteCheckedException(
          "Shared memory server was already created on port " + boundTcpShmemPort);

    if (shmemPort == -1 || U.isWindows()) return null;

    IgniteCheckedException lastEx = null;

    // If configured TCP port is busy, find first available in range.
    for (int port = shmemPort; port < shmemPort + locPortRange; port++) {
      try {
        IpcSharedMemoryServerEndpoint srv =
            new IpcSharedMemoryServerEndpoint(
                log.getLogger(IpcSharedMemoryServerEndpoint.class),
                locProcDesc.processId(),
                gridName);

        srv.setPort(port);

        srv.omitOutOfResourcesWarning(true);

        srv.start();

        boundTcpShmemPort = port;

        // Ack Port the TCP server was bound to.
        if (log.isInfoEnabled())
          log.info(
              "Successfully bound shared memory communication to TCP port [port="
                  + boundTcpShmemPort
                  + ", locHost="
                  + locHost
                  + ']');

        return srv;
      } catch (IgniteCheckedException e) {
        lastEx = e;

        if (log.isDebugEnabled())
          log.debug(
              "Failed to bind to local port (will try next port within range) [port="
                  + port
                  + ", locHost="
                  + locHost
                  + ']');
      }
    }

    // If free port wasn't found.
    throw new IgniteCheckedException(
        "Failed to bind shared memory communication to any port within range [startPort="
            + locPort
            + ", portRange="
            + locPortRange
            + ", locHost="
            + locHost
            + ']',
        lastEx);
  }