Created
January 15, 2018 06:23
-
-
Save pawel-kaminski-krk/d351c6f9450bce8421bfc47221da6307 to your computer and use it in GitHub Desktop.
adding node to cluster in paralel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.carousel.it; | |
import com.google.common.io.Files; | |
import com.google.common.util.concurrent.Uninterruptibles; | |
import io.atomix.cluster.Node; | |
import io.atomix.core.Atomix; | |
import io.atomix.messaging.Endpoint; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
import java.util.concurrent.CompletableFuture; | |
import java.util.concurrent.TimeUnit; | |
public class AddNewNodes { | |
private static final Logger logger = LogManager.getLogger(AddNewNodes.class); | |
public static void main(String[] args) { | |
Node[] bootstrapNodes = new Node[] { | |
Node.builder("server1") | |
.withType(Node.Type.DATA) | |
.withEndpoint(Endpoint.from("127.0.0.1", 5001)) | |
.build(), | |
Node.builder("server2") | |
.withType(Node.Type.DATA) | |
.withEndpoint(Endpoint.from("127.0.0.1", 5002)) | |
.build(), | |
Node.builder("server3") | |
.withType(Node.Type.DATA) | |
.withEndpoint(Endpoint.from("127.0.0.1", 5003)) | |
.build() | |
}; | |
Atomix atomix1 = createAtomicNode("server1", 5001, bootstrapNodes); | |
Atomix atomix2 = createAtomicNode("server2", 5002, bootstrapNodes); | |
Atomix atomix3 = createAtomicNode("server3", 5003, bootstrapNodes); | |
CompletableFuture | |
.allOf( | |
atomix1.start(), | |
atomix2.start(), | |
atomix3.start() | |
) | |
.whenCompleteAsync((aVoid, throwable) -> logger.info("completed ", throwable)) | |
.join(); | |
logger.info("server4/5 joining cluster"); | |
Atomix atomix4 = createAtomicNode("server4", 5004, bootstrapNodes); | |
Atomix atomix5 = createAtomicNode("server5", 5005, bootstrapNodes); | |
CompletableFuture | |
.allOf( | |
atomix4.start(), | |
atomix5.start() | |
) | |
.whenCompleteAsync((aVoid, throwable) -> logger.info("completed adding 4 and 5", throwable)) | |
.join(); | |
printNodesState(atomix4); | |
printNodesState(atomix5); | |
Uninterruptibles.sleepUninterruptibly(10, TimeUnit.SECONDS); | |
System.exit(0); | |
} | |
private static void printNodesState(Atomix givenNode) { | |
logger.info("it seems {} is running {}.", givenNode.clusterService().getLocalNode().id(), | |
givenNode.isRunning()); | |
for (Node availableNode : givenNode.clusterService().getNodes()) { | |
logger.info("node {} is in state {}.", availableNode, availableNode.getState()); | |
} | |
} | |
private static Atomix createAtomicNode(String nodeId, int port, Node[] bootstrapNodes) { | |
Atomix.Builder builder = Atomix.builder(); | |
return builder | |
.withLocalNode(Node.builder(nodeId) | |
.withType(Node.Type.DATA) | |
.withEndpoint(Endpoint.from("127.0.0.1", port)) | |
.build()) | |
.withDataDirectory(Files.createTempDir()) | |
.withBootstrapNodes(bootstrapNodes) | |
.build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am using 2.1.0-SNAPSHOT on java8