Esempio n. 1
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);
        }
      }
    }
  }
Esempio n. 2
0
  private void updateMyInfo() throws TransformerException, IOException, SAXException {
    Clusters.Cluster cluster = clusters.getCluster(config.get(ClusterSettings.cluster_name));

    if (cluster == null) {
      clusters
          .getClusters()
          .add(cluster = new Clusters.Cluster(config.get(ClusterSettings.cluster_name)));
    }

    if (cluster.contains(serverId)) {
      // Do nothing
    } else {
      // Add myself to list
      cluster
          .getMembers()
          .add(
              new Clusters.Member(
                  serverId.getHost() + (serverId.getPort() == -1 ? "" : ":" + serverId.getPort())));

      Document document = new ClustersXMLSerializer(builder).write(clusters);

      // Save new version
      if (clustersUri.getScheme().equals("file")) {
        File clustersFile = new File(clustersUri);
        if (clustersFile.lastModified() != clusters.getTimestamp()) {
          readClustersXml(); // Re-read XML file
          updateMyInfo(); // Try again
          return;
        }

        // Save new version
        transformer.transform(new DOMSource(document), new StreamResult(clustersFile));
        clusters.setTimestamp(clustersFile.lastModified());
      } else {
        // TODO Implement HTTP version
      }
    }
  }