This is an automated email from the ASF dual-hosted git repository.

ifesdjeen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cassandra-in-jvm-dtest-api.git


The following commit(s) were added to refs/heads/master by this push:
     new 326045f  Cluster builder should be provided to the factory and expose 
state
326045f is described below

commit 326045f699791686efa1ecf43b7353397c956494
Author: David Capwell <[email protected]>
AuthorDate: Wed Apr 15 13:30:08 2020 -0700

    Cluster builder should be provided to the factory and expose state
    
    Patch by David Capwell, reviewed by Alex Petrov for CASSANDRA-15733.
---
 .../shared/{Builder.java => AbstractBuilder.java}  | 148 +++++++++++----------
 .../distributed/shared/DistributedTestBase.java    |   2 +-
 2 files changed, 80 insertions(+), 70 deletions(-)

diff --git a/src/main/java/org/apache/cassandra/distributed/shared/Builder.java 
b/src/main/java/org/apache/cassandra/distributed/shared/AbstractBuilder.java
similarity index 70%
rename from src/main/java/org/apache/cassandra/distributed/shared/Builder.java
rename to 
src/main/java/org/apache/cassandra/distributed/shared/AbstractBuilder.java
index b3b7db0..cc341a3 100644
--- a/src/main/java/org/apache/cassandra/distributed/shared/Builder.java
+++ b/src/main/java/org/apache/cassandra/distributed/shared/AbstractBuilder.java
@@ -18,35 +18,31 @@
 
 package org.apache.cassandra.distributed.shared;
 
+import org.apache.cassandra.distributed.api.ICluster;
+import org.apache.cassandra.distributed.api.IInstance;
+import org.apache.cassandra.distributed.api.IInstanceConfig;
+import org.apache.cassandra.distributed.api.TokenSupplier;
+
 import java.io.File;
 import java.io.IOException;
 import java.nio.file.Files;
-import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.function.Consumer;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
 
-import org.apache.cassandra.distributed.api.ICluster;
-import org.apache.cassandra.distributed.api.IInstance;
-import org.apache.cassandra.distributed.api.IInstanceConfig;
-import org.apache.cassandra.distributed.api.TokenSupplier;
-
 import static 
org.apache.cassandra.distributed.api.TokenSupplier.evenlyDistributedTokens;
 
-public abstract class Builder<I extends IInstance, C extends ICluster>
+public abstract class AbstractBuilder<I extends IInstance, C extends ICluster, 
B extends AbstractBuilder<I, C, B>>
 {
-
-    private final int BROADCAST_PORT = 7012;
-
-    public interface Factory<I extends IInstance, C extends ICluster>
+    public interface Factory<I extends IInstance, C extends ICluster, B 
extends AbstractBuilder<I, C, B>>
     {
-        C newCluster(File root, Versions.Version version, 
List<IInstanceConfig> configs, ClassLoader sharedClassLoader);
+        C newCluster(B builder);
     }
 
-    private final Factory<I, C> factory;
+    private final Factory<I, C, B> factory;
     private int nodeCount;
     private int subnet;
     private Map<Integer, NetworkTopology.DcAndRack> nodeIdTopology;
@@ -54,12 +50,50 @@ public abstract class Builder<I extends IInstance, C 
extends ICluster>
     private File root;
     private Versions.Version version;
     private Consumer<IInstanceConfig> configUpdater;
+    private ClassLoader sharedClassLoader = 
Thread.currentThread().getContextClassLoader();
+    private int broadcastPort = 7012;
 
-    public Builder(Factory<I, C> factory)
+    public AbstractBuilder(Factory<I, C, B> factory)
     {
         this.factory = factory;
     }
 
+    public int getNodeCount() {
+        return nodeCount;
+    }
+
+    public int getSubnet() {
+        return subnet;
+    }
+
+    public Map<Integer, NetworkTopology.DcAndRack> getNodeIdTopology() {
+        return nodeIdTopology;
+    }
+
+    public TokenSupplier getTokenSupplier() {
+        return tokenSupplier;
+    }
+
+    public File getRoot() {
+        return root;
+    }
+
+    public Versions.Version getVersion() {
+        return version;
+    }
+
+    public Consumer<IInstanceConfig> getConfigUpdater() {
+        return configUpdater;
+    }
+
+    public ClassLoader getSharedClassLoader() {
+        return sharedClassLoader;
+    }
+
+    public int getBroadcastPort() {
+        return broadcastPort;
+    }
+
     public C start() throws IOException
     {
         C cluster = createWithoutStarting();
@@ -75,79 +109,55 @@ public abstract class Builder<I extends IInstance, C 
extends ICluster>
         if (nodeCount <= 0)
             throw new IllegalStateException("Cluster must have at least one 
node");
 
+        root.mkdirs();
+
         if (nodeIdTopology == null)
-        {
             nodeIdTopology = IntStream.rangeClosed(1, nodeCount).boxed()
                                       .collect(Collectors.toMap(nodeId -> 
nodeId,
                                                                 nodeId -> 
NetworkTopology.dcAndRack(dcName(0), rackName(0))));
-        }
-
-        root.mkdirs();
-
-        ClassLoader sharedClassLoader = 
Thread.currentThread().getContextClassLoader();
-
-        List<IInstanceConfig> configs = new ArrayList<>();
 
         // TODO: make token allocation strategy configurable
         if (tokenSupplier == null)
             tokenSupplier = evenlyDistributedTokens(nodeCount);
 
-        for (int i = 0; i < nodeCount; ++i)
-        {
-            int nodeNum = i + 1;
-            configs.add(createInstanceConfig(nodeNum));
-        }
-
-        return factory.newCluster(root, version, configs, sharedClassLoader);
+        return factory.newCluster((B) this);
     }
 
-    public IInstanceConfig newInstanceConfig(C cluster)
+    public B withSharedClassLoader(ClassLoader sharedClassLoader)
     {
-        return createInstanceConfig(cluster.size() + 1);
+        this.sharedClassLoader = Objects.requireNonNull(sharedClassLoader, 
"sharedClassLoader");
+        return (B) this;
     }
 
-    protected IInstanceConfig createInstanceConfig(int nodeNum)
-    {
-        String ipPrefix = "127.0." + subnet + ".";
-        String seedIp = ipPrefix + "1";
-        String ipAddress = ipPrefix + nodeNum;
-        long token = tokenSupplier.token(nodeNum);
-
-        NetworkTopology topology = NetworkTopology.build(ipPrefix, 
BROADCAST_PORT, nodeIdTopology);
-
-        IInstanceConfig config = generateConfig(nodeNum, ipAddress, topology, 
root, String.valueOf(token), seedIp);
-        if (configUpdater != null)
-            configUpdater.accept(config);
-
-        return config;
+    public B withBroadcastPort(int broadcastPort) {
+        this.broadcastPort = broadcastPort;
+        return (B) this;
     }
 
-    protected abstract IInstanceConfig generateConfig(int nodeNum, String 
ipAddress, NetworkTopology networkTopology, File root, String token, String 
seedIp);
-
-    public Builder<I, C> withTokenSupplier(TokenSupplier tokenSupplier)
+    public B withTokenSupplier(TokenSupplier tokenSupplier)
     {
         this.tokenSupplier = tokenSupplier;
-        return this;
+        return (B) this;
     }
 
-    public Builder<I, C> withSubnet(int subnet)
+    public B withSubnet(int subnet)
     {
         this.subnet = subnet;
-        return this;
+        return (B) this;
     }
 
-    public Builder<I, C> withNodes(int nodeCount)
+    public B withNodes(int nodeCount)
     {
         this.nodeCount = nodeCount;
-        return this;
+        return (B) this;
     }
 
-    public Builder<I, C> withDCs(int dcCount)
+    public B withDCs(int dcCount)
     {
         return withRacks(dcCount, 1);
     }
 
-    public Builder<I, C> withRacks(int dcCount, int racksPerDC)
+    public B withRacks(int dcCount, int racksPerDC)
     {
         if (nodeCount == 0)
             throw new IllegalStateException("Node count will be calculated. Do 
not supply total node count in the builder");
@@ -157,7 +167,7 @@ public abstract class Builder<I extends IInstance, C 
extends ICluster>
         return withRacks(dcCount, racksPerDC, nodesPerRack);
     }
 
-    public Builder<I, C> withRacks(int dcCount, int racksPerDC, int 
nodesPerRack)
+    public B withRacks(int dcCount, int racksPerDC, int nodesPerRack)
     {
         if (nodeIdTopology != null)
             throw new IllegalStateException("Network topology already created. 
Call withDCs/withRacks once or before withDC/withRack calls");
@@ -181,15 +191,15 @@ public abstract class Builder<I extends IInstance, C 
extends ICluster>
                                              dcCount, racksPerDC, 
nodesPerRack, adjustedNodeCount));
             nodeCount = adjustedNodeCount;
         }
-        return this;
+        return (B) this;
     }
 
-    public Builder<I, C> withDC(String dcName, int nodeCount)
+    public B withDC(String dcName, int nodeCount)
     {
         return withRack(dcName, rackName(1), nodeCount);
     }
 
-    public Builder<I, C> withRack(String dcName, String rackName, int 
nodesInRack)
+    public B withRack(String dcName, String rackName, int nodesInRack)
     {
         if (nodeIdTopology == null)
         {
@@ -202,11 +212,11 @@ public abstract class Builder<I extends IInstance, C 
extends ICluster>
             nodeIdTopology.put(nodeId, NetworkTopology.dcAndRack(dcName, 
rackName));
 
         nodeCount += nodesInRack;
-        return this;
+        return (B) this;
     }
 
     // Map of node ids to dc and rack - must be contiguous with an entry 
nodeId 1 to nodeCount
-    public Builder<I, C> withNodeIdTopology(Map<Integer, 
NetworkTopology.DcAndRack> nodeIdTopology)
+    public B withNodeIdTopology(Map<Integer, NetworkTopology.DcAndRack> 
nodeIdTopology)
     {
         if (nodeIdTopology.isEmpty())
             throw new IllegalStateException("Topology is empty. It must have 
an entry for every nodeId.");
@@ -224,25 +234,25 @@ public abstract class Builder<I extends IInstance, C 
extends ICluster>
 
         this.nodeIdTopology = new HashMap<>(nodeIdTopology);
 
-        return this;
+        return (B) this;
     }
 
-    public Builder<I, C> withRoot(File root)
+    public B withRoot(File root)
     {
         this.root = root;
-        return this;
+        return (B) this;
     }
 
-    public Builder<I, C> withVersion(Versions.Version version)
+    public B withVersion(Versions.Version version)
     {
         this.version = version;
-        return this;
+        return (B) this;
     }
 
-    public Builder<I, C> withConfig(Consumer<IInstanceConfig> updater)
+    public B withConfig(Consumer<IInstanceConfig> updater)
     {
         this.configUpdater = updater;
-        return this;
+        return (B) this;
     }
 
     static String dcName(int index)
diff --git 
a/src/main/java/org/apache/cassandra/distributed/shared/DistributedTestBase.java
 
b/src/main/java/org/apache/cassandra/distributed/shared/DistributedTestBase.java
index 3ec5426..adcc306 100644
--- 
a/src/main/java/org/apache/cassandra/distributed/shared/DistributedTestBase.java
+++ 
b/src/main/java/org/apache/cassandra/distributed/shared/DistributedTestBase.java
@@ -34,7 +34,7 @@ public abstract class DistributedTestBase
         ICluster.setup();
     }
 
-    public abstract <I extends IInstance, C extends ICluster> Builder<I, C> 
builder();
+    public abstract <I extends IInstance, C extends ICluster, B extends 
AbstractBuilder<I, C, B>> AbstractBuilder<I, C, B> builder();
 
     public static String KEYSPACE = "distributed_test_keyspace";
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to