Skip to content

Commit

Permalink
Move discovery configurations/builders into separate un-nested class.
Browse files Browse the repository at this point in the history
  • Loading branch information
kuujo committed Jul 5, 2018
1 parent 90c09b0 commit d0f1e47
Show file tree
Hide file tree
Showing 14 changed files with 472 additions and 356 deletions.
8 changes: 4 additions & 4 deletions agent/src/main/java/io/atomix/agent/AtomixAgent.java
Expand Up @@ -15,9 +15,9 @@
*/ */
package io.atomix.agent; package io.atomix.agent;


import io.atomix.cluster.BootstrapDiscoveryProvider; import io.atomix.cluster.BootstrapDiscoveryConfig;
import io.atomix.cluster.MemberId; import io.atomix.cluster.MemberId;
import io.atomix.cluster.MulticastDiscoveryProvider; import io.atomix.cluster.MulticastDiscoveryConfig;
import io.atomix.cluster.Node; import io.atomix.cluster.Node;
import io.atomix.cluster.NodeConfig; import io.atomix.cluster.NodeConfig;
import io.atomix.core.Atomix; import io.atomix.core.Atomix;
Expand Down Expand Up @@ -157,7 +157,7 @@ public static void main(String[] args) throws Exception {
} }


if (bootstrap != null && !bootstrap.isEmpty()) { if (bootstrap != null && !bootstrap.isEmpty()) {
config.getClusterConfig().setDiscoveryConfig(new BootstrapDiscoveryProvider.Config().setNodes(bootstrap)); config.getClusterConfig().setDiscoveryConfig(new BootstrapDiscoveryConfig().setNodes(bootstrap));
} }


if (multicastEnabled) { if (multicastEnabled) {
Expand All @@ -169,7 +169,7 @@ public static void main(String[] args) throws Exception {
config.getClusterConfig().getMulticastConfig().setPort(multicastPort); config.getClusterConfig().getMulticastConfig().setPort(multicastPort);
} }
if (bootstrap == null || bootstrap.isEmpty()) { if (bootstrap == null || bootstrap.isEmpty()) {
config.getClusterConfig().setDiscoveryConfig(new MulticastDiscoveryProvider.Config()); config.getClusterConfig().setDiscoveryConfig(new MulticastDiscoveryConfig());
} }
} }


Expand Down
4 changes: 2 additions & 2 deletions cluster/src/main/java/io/atomix/cluster/AtomixCluster.java
Expand Up @@ -331,12 +331,12 @@ protected static ManagedBroadcastService buildBroadcastService(ClusterConfig con
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected static NodeDiscoveryProvider buildLocationProvider(ClusterConfig config) { protected static NodeDiscoveryProvider buildLocationProvider(ClusterConfig config) {
NodeDiscoveryProvider.Config discoveryProviderConfig = config.getDiscoveryConfig(); NodeDiscoveryConfig discoveryProviderConfig = config.getDiscoveryConfig();
if (discoveryProviderConfig != null) { if (discoveryProviderConfig != null) {
return discoveryProviderConfig.getType().newProvider(discoveryProviderConfig); return discoveryProviderConfig.getType().newProvider(discoveryProviderConfig);
} }
if (config.getMulticastConfig().isEnabled()) { if (config.getMulticastConfig().isEnabled()) {
return new MulticastDiscoveryProvider(new MulticastDiscoveryProvider.Config()); return new MulticastDiscoveryProvider(new MulticastDiscoveryConfig());
} else { } else {
return new BootstrapDiscoveryProvider(Collections.emptyList()); return new BootstrapDiscoveryProvider(Collections.emptyList());
} }
Expand Down
104 changes: 104 additions & 0 deletions cluster/src/main/java/io/atomix/cluster/BootstrapDiscoveryBuilder.java
@@ -0,0 +1,104 @@
/*
* Copyright 2018-present Open Networking Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.atomix.cluster;

import io.atomix.utils.net.Address;

import java.time.Duration;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Bootstrap discovery builder.
*/
public class BootstrapDiscoveryBuilder extends NodeDiscoveryBuilder {
private final BootstrapDiscoveryConfig config = new BootstrapDiscoveryConfig();

/**
* Sets the bootstrap nodes.
*
* @param nodes the bootstrap nodes
* @return the location provider builder
*/
public BootstrapDiscoveryBuilder withNodes(Address... nodes) {
return withNodes(Stream.of(nodes)
.map(address -> Node.builder()
.withAddress(address)
.build())
.collect(Collectors.toSet()));
}

/**
* Sets the bootstrap nodes.
*
* @param nodes the bootstrap nodes
* @return the location provider builder
*/
public BootstrapDiscoveryBuilder withNodes(Node... nodes) {
return withNodes(Arrays.asList(nodes));
}

/**
* Sets the bootstrap nodes.
*
* @param locations the bootstrap member locations
* @return the location provider builder
*/
public BootstrapDiscoveryBuilder withNodes(Collection<Node> locations) {
config.setNodes(locations);
return this;
}

/**
* Sets the failure detection heartbeat interval.
*
* @param heartbeatInterval the failure detection heartbeat interval
* @return the location provider builder
*/
public BootstrapDiscoveryBuilder withHeartbeatInterval(Duration heartbeatInterval) {
config.setHeartbeatInterval(heartbeatInterval);
return this;
}

/**
* Sets the phi accrual failure threshold.
*
* @param failureThreshold the phi accrual failure threshold
* @return the location provider builder
*/
public BootstrapDiscoveryBuilder withFailureThreshold(int failureThreshold) {
config.setFailureThreshold(failureThreshold);
return this;
}

/**
* Sets the failure timeout to use prior to phi failure detectors being populated.
*
* @param failureTimeout the failure timeout
* @return the location provider builder
*/
public BootstrapDiscoveryBuilder withFailureTimeout(Duration failureTimeout) {
config.setFailureTimeout(failureTimeout);
return this;
}

@Override
public NodeDiscoveryProvider build() {
return new BootstrapDiscoveryProvider(config);
}
}
121 changes: 121 additions & 0 deletions cluster/src/main/java/io/atomix/cluster/BootstrapDiscoveryConfig.java
@@ -0,0 +1,121 @@
/*
* Copyright 2018-present Open Networking Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.atomix.cluster;

import java.time.Duration;
import java.util.Collection;
import java.util.Collections;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* Bootstrap discovery configuration.
*/
public class BootstrapDiscoveryConfig extends NodeDiscoveryConfig {
private static final int DEFAULT_HEARTBEAT_INTERVAL = 1000;
private static final int DEFAULT_FAILURE_TIMEOUT = 10000;
private static final int DEFAULT_PHI_FAILURE_THRESHOLD = 10;

private Duration heartbeatInterval = Duration.ofMillis(DEFAULT_HEARTBEAT_INTERVAL);
private int failureThreshold = DEFAULT_PHI_FAILURE_THRESHOLD;
private Duration failureTimeout = Duration.ofMillis(DEFAULT_FAILURE_TIMEOUT);
private Collection<Node> nodes = Collections.emptySet();

@Override
public NodeDiscoveryProvider.Type getType() {
return BootstrapDiscoveryProvider.TYPE;
}

/**
* Returns the configured bootstrap nodes.
*
* @return the configured bootstrap nodes
*/
public Collection<Node> getNodes() {
return nodes;
}

/**
* Sets the bootstrap nodes.
*
* @param nodes the bootstrap nodes
* @return the bootstrap provider configuration
*/
public BootstrapDiscoveryConfig setNodes(Collection<Node> nodes) {
this.nodes = nodes;
return this;
}

/**
* Returns the heartbeat interval.
*
* @return the heartbeat interval
*/
public Duration getHeartbeatInterval() {
return heartbeatInterval;
}

/**
* Sets the heartbeat interval.
*
* @param heartbeatInterval the heartbeat interval
* @return the group membership configuration
*/
public BootstrapDiscoveryConfig setHeartbeatInterval(Duration heartbeatInterval) {
this.heartbeatInterval = checkNotNull(heartbeatInterval);
return this;
}

/**
* Returns the failure detector threshold.
*
* @return the failure detector threshold
*/
public int getFailureThreshold() {
return failureThreshold;
}

/**
* Sets the failure detector threshold.
*
* @param failureThreshold the failure detector threshold
* @return the group membership configuration
*/
public BootstrapDiscoveryConfig setFailureThreshold(int failureThreshold) {
this.failureThreshold = failureThreshold;
return this;
}

/**
* Returns the base failure timeout.
*
* @return the base failure timeout
*/
public Duration getFailureTimeout() {
return failureTimeout;
}

/**
* Sets the base failure timeout.
*
* @param failureTimeout the base failure timeout
* @return the group membership configuration
*/
public BootstrapDiscoveryConfig setFailureTimeout(Duration failureTimeout) {
this.failureTimeout = checkNotNull(failureTimeout);
return this;
}
}

0 comments on commit d0f1e47

Please sign in to comment.