All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vlingo.xoom.cluster.model.ClusterConfiguration Maven / Gradle / Ivy

// Copyright © 2012-2022 VLINGO LABS. All rights reserved.
//
// This Source Code Form is subject to the terms of the
// Mozilla Public License, v. 2.0. If a copy of the MPL
// was not distributed with this file, You can obtain
// one at https://mozilla.org/MPL/2.0/.

package io.vlingo.xoom.cluster.model;

import java.util.*;

import io.vlingo.xoom.actors.Logger;
import io.vlingo.xoom.wire.node.Address;
import io.vlingo.xoom.wire.node.AddressType;
import io.vlingo.xoom.wire.node.Configuration;
import io.vlingo.xoom.wire.node.Host;
import io.vlingo.xoom.wire.node.Id;
import io.vlingo.xoom.wire.node.Name;
import io.vlingo.xoom.wire.node.Node;

public class ClusterConfiguration implements Configuration {
  private final Logger logger;
  private final Set nodes;
  private final Set seeds;

  public ClusterConfiguration(Properties properties, final Logger logger) {
    this.logger = logger;
    this.nodes = new TreeSet<>();
    this.seeds = new TreeSet<>();

    initializeConfiguredNodeEntries(properties);
  }

  @Override
  public Set allNodes() {
    return Collections.unmodifiableSet(nodes);
  }

  @Override
  public Set allNodesOf(final Collection ids) {
    // Currently not used

    return new TreeSet<>();
  }

  @Override
  public final Set allOtherNodes(Id nodeId) {
    final Set except = new TreeSet<>();

    for (final Node node : nodes) {
      if (!node.id().equals(nodeId)) {
        except.add(node);
      }
    }

    return except;
  }

  @Override
  public Set allOtherNodesId(final Id nodeId) {
    final Set ids = new TreeSet<>();

    for (final Node node : allOtherNodes(nodeId)) {
      ids.add(node.id());
    }

    return ids;
  }

  @Override
  public final Set allGreaterNodes(Id nodeId) {
    final Set greater = new TreeSet<>();

    for (final Node node : nodes) {
      if (node.id().greaterThan(nodeId)) {
        greater.add(node);
      }
    }

    return greater;
  }

  @Override
  public Set allNodeNames() {
    final Set names = new TreeSet<>();

    for (final Node node : nodes) {
      names.add(node.name().value());
    }

    return names;
  }

  @Override
  public final Node nodeMatching(Id nodeId) {
    for (final Node node : nodes) {
      if (node.id().equals(nodeId)) {
        return node;
      }
    }
    return Node.NO_NODE;
  }

  @Override
  public final Id greatestNodeId() {
    Id greatest = Id.NO_ID;

    for (final Node node : nodes) {
      if (node.id().greaterThan(greatest)) {
        greatest = node.id();
      }
    }

    return greatest;
  }

  @Override
  public boolean hasNode(Id nodeId) {
    for (final Node node : nodes) {
      if (node.id().equals(nodeId)) {
        return true;
      }
    }
    return false;
  }

  @Override
  public int totalNodes() {
    return nodes.size();
  }

  @Override
  public Logger logger() {
    return logger;
  }

  @Override
  public boolean equals(Object other) {
    if (other == null || other.getClass() != ClusterConfiguration.class) {
      return false;
    }

    return this.nodes.equals(((ClusterConfiguration) other).nodes);
  }

  @Override
  public int hashCode() {
    return 31 * nodes.hashCode();
  }

  @Override
  public String toString() {
    return "ConfiguredCluster[" + nodes + "]";
  }

  private void initializeConfiguredNodeEntries(final Properties properties) {
    for (String configuredNodeName : properties.nodes()) {
      final Id nodeId = Id.of(properties.nodeId(configuredNodeName));
      final Name nodeName = new Name(configuredNodeName);
      final Host host = Host.of(properties.host(configuredNodeName));
      final Address opNodeAddress = Address.from(host, properties.operationalPort(configuredNodeName), AddressType.OP);
      final boolean isSeed = properties.isSeed(configuredNodeName);
      final Address appNodeAddress = Address.from(host, properties.applicationPort(configuredNodeName), AddressType.APP);

      Node node = new Node(nodeId, nodeName, opNodeAddress, appNodeAddress, isSeed);
      nodes.add(node);
      if (isSeed) {
        seeds.add(node);
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy