31
31
import java .net .InetSocketAddress ;
32
32
import java .util .*;
33
33
import java .util .concurrent .TimeUnit ;
34
+ import java .util .regex .Matcher ;
35
+ import java .util .regex .Pattern ;
34
36
35
37
import static com .datastax .driver .core .TestUtils .executeNoFail ;
36
38
import static com .datastax .driver .core .TestUtils .findAvailablePort ;
@@ -659,22 +661,25 @@ protected void finalize() throws Throwable {
659
661
* use {@link #builder()} to get an instance
660
662
*/
661
663
public static class Builder {
662
- private final String clusterName = TestUtils .generateIdentifier ("cluster_" );
664
+
665
+ public static final String RANDOM_PORT = "__RANDOM_PORT__" ;
666
+ private static final Pattern RANDOM_PORT_PATTERN = Pattern .compile (RANDOM_PORT );
667
+
663
668
int [] nodes = {1 };
664
669
private boolean start = true ;
665
670
private boolean isDSE = isDSE ();
666
671
private String version = getCassandraVersion ();
667
- private Set <String > createOptions = new HashSet <String >(getInstallArguments ());
672
+ private Set <String > createOptions = new LinkedHashSet <String >(getInstallArguments ());
668
673
private Set <String > jvmArgs = new LinkedHashSet <String >();
669
- private final Map <String , Object > cassandraConfiguration = Maps .newHashMap ();
670
- private final Map <String , Object > dseConfiguration = Maps .newHashMap ();
671
- // -1 means random
672
- private int thriftPort = -1 ;
673
- private int binaryPort = -1 ;
674
- private int storagePort = -1 ;
674
+ private final Map <String , Object > cassandraConfiguration = Maps .newLinkedHashMap ();
675
+ private final Map <String , Object > dseConfiguration = Maps .newLinkedHashMap ();
675
676
private Map <Integer , Workload > workloads = new HashMap <Integer , Workload >();
676
677
677
678
private Builder () {
679
+ cassandraConfiguration .put ("start_rpc" , false );
680
+ cassandraConfiguration .put ("storage_port" , RANDOM_PORT );
681
+ cassandraConfiguration .put ("rpc_port" , RANDOM_PORT );
682
+ cassandraConfiguration .put ("native_transport_port" , RANDOM_PORT );
678
683
}
679
684
680
685
/**
@@ -781,17 +786,17 @@ public Builder withJvmArgs(String... jvmArgs) {
781
786
}
782
787
783
788
public Builder withStoragePort (int port ) {
784
- storagePort = port ;
789
+ cassandraConfiguration . put ( "storage_port" , port ) ;
785
790
return this ;
786
791
}
787
792
788
793
public Builder withThriftPort (int port ) {
789
- thriftPort = port ;
794
+ cassandraConfiguration . put ( "rpc_port" , port ) ;
790
795
return this ;
791
796
}
792
797
793
798
public Builder withBinaryPort (int port ) {
794
- binaryPort = port ;
799
+ cassandraConfiguration . put ( "native_transport_port" , port ) ;
795
800
return this ;
796
801
}
797
802
@@ -809,25 +814,23 @@ public Builder withWorkload(int node, Workload workload) {
809
814
810
815
public CCMBridge build () {
811
816
// be careful NOT to alter internal state (hashCode/equals) during build!
812
- int storagePort = this . storagePort == - 1 ? findAvailablePort () : this . storagePort ;
813
- int thriftPort = this . thriftPort == - 1 ? findAvailablePort () : this .thriftPort ;
814
- int binaryPort = this . binaryPort == - 1 ? findAvailablePort () : this .binaryPort ;
817
+ String clusterName = TestUtils . generateIdentifier ( "ccm_" ) ;
818
+ Map < String , Object > cassandraConfiguration = randomizePorts ( this .cassandraConfiguration ) ;
819
+ Map < String , Object > dseConfiguration = randomizePorts ( this .dseConfiguration ) ;
815
820
VersionNumber version = VersionNumber .parse (this .version );
821
+ int storagePort = Integer .parseInt (cassandraConfiguration .get ("storage_port" ).toString ());
822
+ int thriftPort = Integer .parseInt (cassandraConfiguration .get ("rpc_port" ).toString ());
823
+ int binaryPort = Integer .parseInt (cassandraConfiguration .get ("native_transport_port" ).toString ());
816
824
final CCMBridge ccm = new CCMBridge (clusterName , isDSE , version , storagePort , thriftPort , binaryPort , joinJvmArgs ());
817
825
Runtime .getRuntime ().addShutdownHook (new Thread () {
818
826
@ Override
819
827
public void run () {
820
828
ccm .close ();
821
829
}
822
830
});
823
- ccm .execute (buildCreateCommand ());
831
+ ccm .execute (buildCreateCommand (clusterName ));
824
832
updateNodeConf (ccm );
825
- HashMap <String , Object > config = new HashMap <String , Object >(cassandraConfiguration );
826
- config .put ("start_rpc" , false );
827
- config .put ("storage_port" , storagePort );
828
- config .put ("rpc_port" , thriftPort );
829
- config .put ("native_transport_port" , binaryPort );
830
- ccm .updateConfig (config );
833
+ ccm .updateConfig (cassandraConfiguration );
831
834
if (!dseConfiguration .isEmpty ())
832
835
ccm .updateDSEConfig (dseConfiguration );
833
836
for (Map .Entry <Integer , Workload > entry : workloads .entrySet ()) {
@@ -851,12 +854,12 @@ private String joinJvmArgs() {
851
854
StringBuilder allJvmArgs = new StringBuilder ("" );
852
855
for (String jvmArg : jvmArgs ) {
853
856
allJvmArgs .append (" --jvm_arg=" );
854
- allJvmArgs .append (jvmArg );
857
+ allJvmArgs .append (randomizePorts ( jvmArg ) );
855
858
}
856
859
return allJvmArgs .toString ();
857
860
}
858
861
859
- private String buildCreateCommand () {
862
+ private String buildCreateCommand (String clusterName ) {
860
863
StringBuilder result = new StringBuilder (CCM_COMMAND + " create" );
861
864
result .append (" " ).append (clusterName );
862
865
result .append (" -i " ).append (TestUtils .IP_PREFIX );
@@ -870,7 +873,7 @@ private String buildCreateCommand() {
870
873
result .append (node );
871
874
}
872
875
}
873
- result .append (" " ).append (Joiner .on (" " ).join (createOptions ));
876
+ result .append (" " ).append (Joiner .on (" " ).join (randomizePorts ( createOptions ) ));
874
877
return result .toString ();
875
878
}
876
879
@@ -888,7 +891,7 @@ private void updateNodeConf(CCMBridge ccm) {
888
891
for (int i = 0 ; i < nodesInDc ; i ++) {
889
892
int jmxPort = findAvailablePort ();
890
893
int debugPort = findAvailablePort ();
891
- logger .trace ("Node {} in cluster {} using JMX port {} and debug port {}" , n , clusterName , jmxPort , debugPort );
894
+ logger .trace ("Node {} in cluster {} using JMX port {} and debug port {}" , n , ccm . getClusterName () , jmxPort , debugPort );
892
895
File nodeConf = new File (ccm .getNodeDir (n ), "node.conf" );
893
896
File nodeConf2 = new File (ccm .getNodeDir (n ), "node.conf.tmp" );
894
897
BufferedReader br = closer .register (new BufferedReader (new FileReader (nodeConf )));
@@ -923,6 +926,36 @@ private void updateNodeConf(CCMBridge ccm) {
923
926
}
924
927
}
925
928
929
+ private Set <String > randomizePorts (Set <String > set ) {
930
+ Set <String > randomized = new LinkedHashSet <String >();
931
+ for (String value : set ) {
932
+ randomized .add (randomizePorts (value ));
933
+ }
934
+ return randomized ;
935
+ }
936
+
937
+ private Map <String , Object > randomizePorts (Map <String , Object > map ) {
938
+ Map <String , Object > randomized = new HashMap <String , Object >();
939
+ for (Map .Entry <String , Object > entry : map .entrySet ()) {
940
+ Object value = entry .getValue ();
941
+ if (value instanceof CharSequence ) {
942
+ value = randomizePorts ((CharSequence ) value );
943
+ }
944
+ randomized .put (entry .getKey (), value );
945
+ }
946
+ return randomized ;
947
+ }
948
+
949
+ private String randomizePorts (CharSequence str ) {
950
+ Matcher matcher = RANDOM_PORT_PATTERN .matcher (str );
951
+ StringBuffer sb = new StringBuffer ();
952
+ while (matcher .find ()) {
953
+ matcher .appendReplacement (sb , Integer .toString (TestUtils .findAvailablePort ()));
954
+ }
955
+ matcher .appendTail (sb );
956
+ return sb .toString ();
957
+ }
958
+
926
959
@ Override
927
960
@ SuppressWarnings ("SimplifiableIfStatement" )
928
961
public boolean equals (Object o ) {
@@ -932,9 +965,6 @@ public boolean equals(Object o) {
932
965
if (o == null || getClass () != o .getClass ()) return false ;
933
966
Builder builder = (Builder ) o ;
934
967
if (isDSE != builder .isDSE ) return false ;
935
- if (thriftPort != builder .thriftPort ) return false ;
936
- if (binaryPort != builder .binaryPort ) return false ;
937
- if (storagePort != builder .storagePort ) return false ;
938
968
if (!Arrays .equals (nodes , builder .nodes )) return false ;
939
969
if (!createOptions .equals (builder .createOptions )) return false ;
940
970
if (!jvmArgs .equals (builder .jvmArgs )) return false ;
@@ -955,17 +985,10 @@ public int hashCode() {
955
985
result = 31 * result + cassandraConfiguration .hashCode ();
956
986
result = 31 * result + dseConfiguration .hashCode ();
957
987
result = 31 * result + workloads .hashCode ();
958
- result = 31 * result + thriftPort ;
959
- result = 31 * result + binaryPort ;
960
- result = 31 * result + storagePort ;
961
988
result = 31 * result + version .hashCode ();
962
989
return result ;
963
990
}
964
991
965
- @ Override
966
- public String toString () {
967
- return String .format ("%s (%s nodes)" , clusterName , weight ());
968
- }
969
992
}
970
993
971
994
}
0 commit comments