Skip to content
This repository has been archived by the owner on May 27, 2022. It is now read-only.

Commit

Permalink
Check for not null on cluster when running autodiscovery
Browse files Browse the repository at this point in the history
it fails during the initialization and it is never able
to set the cluster object properly remaining as null for
future runs.
  • Loading branch information
patricioe committed Jun 22, 2013
1 parent 0b27b2e commit 2adef79
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
Expand Up @@ -7,6 +7,7 @@
import me.prettyprint.cassandra.service.CassandraHost;
import me.prettyprint.cassandra.service.CassandraHostConfigurator;
import me.prettyprint.cassandra.service.ThriftCluster;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
import me.prettyprint.hector.api.factory.HFactory;
Expand All @@ -24,23 +25,13 @@ public class NodeDiscovery {
private CassandraHostConfigurator cassandraHostConfigurator;
private HConnectionManager connectionManager;
private DataCenterValidator dataCenterValidator;
private ThriftCluster cluster;


public NodeDiscovery(CassandraHostConfigurator cassandraHostConfigurator, HConnectionManager connectionManager) {
this((ThriftCluster) HFactory.getCluster(connectionManager.getClusterName()), cassandraHostConfigurator, connectionManager);
this.cassandraHostConfigurator = cassandraHostConfigurator;
this.connectionManager = connectionManager;
this.dataCenterValidator = new DataCenterValidator(cassandraHostConfigurator.getAutoDiscoveryDataCenters());
}

/**
* Package scoped constructor that allows cluster to be overriden for tests
*/
@VisibleForTesting
NodeDiscovery(ThriftCluster cluster, CassandraHostConfigurator cassandraHostConfigurator, HConnectionManager connectionManager) {
this.cassandraHostConfigurator = cassandraHostConfigurator;
this.connectionManager = connectionManager;
this.dataCenterValidator = new DataCenterValidator(cassandraHostConfigurator.getAutoDiscoveryDataCenters());
this.cluster = cluster;
}

/**
* Find any nodes that are not already in the connection manager but are in
* the cassandra ring and add them
Expand All @@ -67,6 +58,11 @@ public void doAddNodes() {
* Find any unknown nodes in the cassandra ring
*/
public Set<CassandraHost> discoverNodes() {
Cluster cluster = HFactory.getCluster(connectionManager.getClusterName());
if (cluster == null) {
return null;
}

Set<CassandraHost> existingHosts = connectionManager.getHosts();
Set<CassandraHost> foundHosts = new HashSet<CassandraHost>();

Expand Down
Expand Up @@ -202,6 +202,10 @@ public static Cluster createCluster(String clusterName,
return cluster;
}
}

public static void setClusterForTest(Cluster cluster) {
clusters.put(cluster.getName(), cluster);
}

/**
* Shutdown this cluster, removing it from the Map. This operation is
Expand Down
Expand Up @@ -9,6 +9,7 @@
import me.prettyprint.cassandra.service.ThriftCluster;
import me.prettyprint.hector.api.ddl.KeyspaceDefinition;

import me.prettyprint.hector.api.factory.HFactory;
import org.apache.cassandra.thrift.EndpointDetails;
import org.apache.cassandra.thrift.TokenRange;
import org.junit.Before;
Expand All @@ -17,8 +18,9 @@

import com.google.common.collect.Sets;

public class NodeDiscoveryTest {

public class NodeDiscoveryTest {

public static final String TEST_CLUSTER_NAME = "TestCluster";
CassandraHostConfigurator cassandraHostConfigurator;
HConnectionManager connectionManager;
ThriftCluster cluster;
Expand All @@ -41,21 +43,27 @@ public void setup() {
public void testDoAdd() {
List<TokenRange> tokens = createRange("localhost", "google.com");
Mockito.when(cluster.describeRing("TestKeyspace")).thenReturn(tokens);
Mockito.when(cluster.getName()).thenReturn(TEST_CLUSTER_NAME);
Mockito.when(connectionManager.getClusterName()).thenReturn(TEST_CLUSTER_NAME);
Mockito.when(connectionManager.getHosts()).thenReturn(Sets.newHashSet(new CassandraHost("localhost",9160)));

NodeDiscovery q = new NodeDiscovery(cluster, cassandraHostConfigurator, connectionManager);
HFactory.setClusterForTest(cluster);

NodeDiscovery q = new NodeDiscovery(cassandraHostConfigurator, connectionManager);
q.doAddNodes();

Mockito.verify(connectionManager).addCassandraHost(new CassandraHost("google.com",9160));
}

@Test
public void testMultipleAdded() {
List<TokenRange> tokens = createRange("localhost", "google.com", "yahoo.com", "datastax.com");
Mockito.when(cluster.describeRing("TestKeyspace")).thenReturn(tokens);
Mockito.when(cluster.getName()).thenReturn(TEST_CLUSTER_NAME);
Mockito.when(connectionManager.getClusterName()).thenReturn(TEST_CLUSTER_NAME);
Mockito.when(connectionManager.getHosts()).thenReturn(Sets.newHashSet(new CassandraHost("localhost",9160)));

NodeDiscovery q = new NodeDiscovery(cluster, cassandraHostConfigurator, connectionManager);
HFactory.setClusterForTest(cluster);

NodeDiscovery q = new NodeDiscovery(cassandraHostConfigurator, connectionManager);
q.doAddNodes();

Mockito.verify(connectionManager).addCassandraHost(new CassandraHost("google.com",9160));
Expand All @@ -67,9 +75,12 @@ public void testMultipleAdded() {
public void testNoneAdded() {
List<TokenRange> tokens = createRange("localhost");
Mockito.when(cluster.describeRing("TestKeyspace")).thenReturn(tokens);
Mockito.when(cluster.getName()).thenReturn(TEST_CLUSTER_NAME);
Mockito.when(connectionManager.getClusterName()).thenReturn(TEST_CLUSTER_NAME);
Mockito.when(connectionManager.getHosts()).thenReturn(Sets.newHashSet(new CassandraHost("localhost",9160)));
HFactory.setClusterForTest(cluster);

NodeDiscovery q = new NodeDiscovery(cluster, cassandraHostConfigurator, connectionManager);
NodeDiscovery q = new NodeDiscovery(cassandraHostConfigurator, connectionManager);
Assert.assertEquals(0, q.discoverNodes().size());
}

Expand Down

0 comments on commit 2adef79

Please sign in to comment.