Beispiel #1
0
  private void clusterByConfig() {
    String hosts = config.get(HaSettings.initial_hosts);

    if (hosts.equals("")) {
      logger.logMessage("Creating cluster " + config.get(ClusterSettings.cluster_name));
      cluster.create(config.get(ClusterSettings.cluster_name));
    } else {
      try {
        for (String host : hosts.split(",")) {
          if (serverId.toString().endsWith(host)) {
            continue; // Don't try to join myself
          }

          logger.info("Attempting to join " + host);
          Future<ClusterConfiguration> clusterConfig = cluster.join(new URI("cluster://" + host));
          try {
            logger.info("Joined cluster:" + clusterConfig.get());
            return;
          } catch (InterruptedException e) {
            e.printStackTrace();
          } catch (ExecutionException e) {
            logger.error("Could not join cluster member " + host);
          }
        }

        // Failed to join cluster, create new one
        cluster.create(config.get(ClusterSettings.cluster_name));
      } catch (URISyntaxException e) {
        // This
        e.printStackTrace();
      }
    }
  }
Beispiel #2
0
  private void clusterDiscovery()
      throws URISyntaxException, ParserConfigurationException, SAXException, IOException {
    determineUri();

    readClustersXml();

    // Now try to join or create cluster
    if (clusters != null) {
      Clusters.Cluster clusterConfig =
          clusters.getCluster(config.get(ClusterSettings.cluster_name));
      if (clusterConfig != null) {
        for (Clusters.Member member : clusterConfig.getMembers()) {
          URI joinUri = new URI("cluster://" + member.getHost());
          if (!joinUri.equals(serverId)) {
            Future<ClusterConfiguration> config = cluster.join(joinUri);
            try {
              logger.logMessage("Joined cluster:" + config.get());

              try {
                updateMyInfo();
              } catch (TransformerException e) {
                throw new RuntimeException(e);
              }

              return;
            } catch (InterruptedException e) {
              e.printStackTrace();
            } catch (ExecutionException e) {
              logger.logMessage("Could not join cluster member " + member.getHost());
            }
          }
        }
      }

      // Could not find cluster or not join nodes in cluster - create it!
      if (clusterConfig == null) {
        clusterConfig = new Clusters.Cluster(config.get(ClusterSettings.cluster_name));
        clusters.getClusters().add(clusterConfig);
      }

      cluster.create(clusterConfig.getName());

      if (clusterConfig.getByUri(serverId) == null) {
        clusterConfig.getMembers().add(new Clusters.Member(serverId.toString()));

        try {
          updateMyInfo();
        } catch (TransformerException e) {
          logger.logMessage("Could not update cluster discovery file:" + clustersUri, e);
        }
      }
    }
  }
Beispiel #3
0
 @Override
 public void init() throws Throwable {
   cluster = protocolServer.newClient(Cluster.class);
   cluster.addClusterListener(clusterListener);
   builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
   transformer = TransformerFactory.newInstance().newTransformer();
 }
Beispiel #4
0
  @Override
  public void stop() {
    final Semaphore semaphore = new Semaphore(0);

    cluster.addClusterListener(
        new ClusterListener.Adapter() {
          @Override
          public void leftCluster() {
            cluster.removeClusterListener(this);
            semaphore.release();
          }
        });

    cluster.leave();

    try {
      if (!semaphore.tryAcquire(5, TimeUnit.SECONDS)) {
        logger.logMessage("Unable to leave cluster, timeout");
      }
    } catch (InterruptedException e) {
      Thread.interrupted();
      logger.logMessage("Unable to leave cluster, interrupted", e);
    }
  }