Search in sources :

Example 1 with AtomixConfig

use of io.atomix.core.AtomixConfig in project atomix by atomix.

the class AtomixAgent method createConfig.

/**
 * Creates an Atomix configuration from the given namespace.
 *
 * @param namespace the namespace from which to create the configuration
 * @return the Atomix configuration for the given namespace
 */
static AtomixConfig createConfig(Namespace namespace) {
    final List<File> configFiles = namespace.getList("config");
    final String memberId = namespace.getString("member");
    final Address address = namespace.get("address");
    final String host = namespace.getString("host");
    final String rack = namespace.getString("rack");
    final String zone = namespace.getString("zone");
    final List<NodeConfig> bootstrap = namespace.getList("bootstrap");
    final boolean multicastEnabled = namespace.getBoolean("multicast");
    final String multicastGroup = namespace.get("multicast_group");
    final Integer multicastPort = namespace.get("multicast_port");
    System.setProperty("atomix.data", namespace.getString("data_dir"));
    // If a configuration was provided, merge the configuration's member information with the provided command line arguments.
    AtomixConfig config;
    if (configFiles != null && !configFiles.isEmpty()) {
        System.setProperty("atomix.config.resources", "");
        config = Atomix.config(configFiles);
    } else {
        config = Atomix.config();
    }
    if (memberId != null) {
        config.getClusterConfig().getNodeConfig().setId(memberId);
    }
    if (address != null) {
        config.getClusterConfig().getNodeConfig().setAddress(address);
    }
    if (host != null) {
        config.getClusterConfig().getNodeConfig().setHostId(host);
    }
    if (rack != null) {
        config.getClusterConfig().getNodeConfig().setRackId(rack);
    }
    if (zone != null) {
        config.getClusterConfig().getNodeConfig().setZoneId(zone);
    }
    if (bootstrap != null && !bootstrap.isEmpty()) {
        config.getClusterConfig().setDiscoveryConfig(new BootstrapDiscoveryConfig().setNodes(bootstrap));
    }
    if (multicastEnabled) {
        config.getClusterConfig().getMulticastConfig().setEnabled(true);
        if (multicastGroup != null) {
            config.getClusterConfig().getMulticastConfig().setGroup(multicastGroup);
        }
        if (multicastPort != null) {
            config.getClusterConfig().getMulticastConfig().setPort(multicastPort);
        }
        if (bootstrap == null || bootstrap.isEmpty()) {
            config.getClusterConfig().setDiscoveryConfig(new MulticastDiscoveryConfig());
        }
    }
    return config;
}
Also used : AtomixConfig(io.atomix.core.AtomixConfig) Address(io.atomix.utils.net.Address) MulticastDiscoveryConfig(io.atomix.cluster.discovery.MulticastDiscoveryConfig) BootstrapDiscoveryConfig(io.atomix.cluster.discovery.BootstrapDiscoveryConfig) File(java.io.File) NodeConfig(io.atomix.cluster.NodeConfig)

Example 2 with AtomixConfig

use of io.atomix.core.AtomixConfig in project atomix by atomix.

the class AtomixAgentTest method testCreateConfig.

@Test
public void testCreateConfig() {
    final List<String> unknown = new ArrayList<>();
    final String path = getClass().getClassLoader().getResource("test.conf").getPath();
    final String[] args = new String[] { "-c", path, "--cluster.node.id", "member-1", "--cluster.node.address", "localhost:5000" };
    final Namespace namespace = AtomixAgent.parseArgs(args, unknown);
    final Namespace extraArgs = AtomixAgent.parseUnknown(unknown);
    extraArgs.getAttrs().forEach((key, value) -> System.setProperty(key, value.toString()));
    final AtomixConfig config = AtomixAgent.createConfig(namespace);
    assertEquals("member-1", config.getClusterConfig().getNodeConfig().getId().id());
    assertEquals("localhost:5000", config.getClusterConfig().getNodeConfig().getAddress().toString());
}
Also used : AtomixConfig(io.atomix.core.AtomixConfig) ArrayList(java.util.ArrayList) Namespace(net.sourceforge.argparse4j.inf.Namespace) Test(org.junit.Test)

Aggregations

AtomixConfig (io.atomix.core.AtomixConfig)2 NodeConfig (io.atomix.cluster.NodeConfig)1 BootstrapDiscoveryConfig (io.atomix.cluster.discovery.BootstrapDiscoveryConfig)1 MulticastDiscoveryConfig (io.atomix.cluster.discovery.MulticastDiscoveryConfig)1 Address (io.atomix.utils.net.Address)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Namespace (net.sourceforge.argparse4j.inf.Namespace)1 Test (org.junit.Test)1