Example #1
0
  public void doJoin() {
    NodeEngineImpl nodeEngine = null;
    synchronized (joinerLock) {
      for (Address address : joinAddresses) {
        NodeEngineImpl ne = nodes.get(address);
        if (ne != null && ne.isRunning() && ne.getNode().joined()) {
          nodeEngine = ne;
          break;
        }
      }
      Address master = null;
      if (nodeEngine != null) {
        if (nodeEngine.getNode().isMaster()) {
          master = nodeEngine.getThisAddress();
        } else {
          master = nodeEngine.getMasterAddress();
        }
      }
      if (master == null) {
        master = node.getThisAddress();
      }
      node.setMasterAddress(master);
      if (node.getMasterAddress().equals(node.getThisAddress())) {
        node.setJoined();
        node.setAsMaster();
      } else {
        final ClusterJoinManager clusterJoinManager = node.clusterService.getClusterJoinManager();

        for (int i = 0; !node.joined() && node.isRunning() && i < 2000; i++) {
          try {
            clusterJoinManager.sendJoinRequest(node.getMasterAddress(), true);
            Thread.sleep(50);
          } catch (InterruptedException e) {
            e.printStackTrace();
            break;
          }
        }
        if (!node.joined()) {
          logger.severe(
              "Node["
                  + node.getThisAddress()
                  + "] should have been joined to "
                  + node.getMasterAddress());
          node.shutdown(true);
        }
      }
    }
  }