4
votes

I am using Cassandra 1.2.4 on my machine (windows 7).

I have got 3 nodes in my DC1, which is on my machine. I am only using one DC (my machine). I did keep replication factor=2 and "HConsistencyLevel.ONE". However, when one of the nodes is down, and I attempt to read or write to DB, I get the error - "May not be enough replicas present to handle consistency level.". I am under the impression that when we keep the consistency level as "ONE" and even if one node is up, the write/read should happen without errors. But I get this error. Could someone correct me as what I was doing wrong here. I did search in google but didn't get much help about this error although this error is quite relevant. I want to make the read/write happen in the event of node failure. Below is my code.

String keySpaceName="kspace";
String clusterName="Test Cluster";
String columFamilyName="ktable";
String host="127.0.0.1:9160,127.0.0.2:9161,127.0.0.3:9162";
int replicationFactor=2;

CassandraHostConfigurator cassandraHostConfigurator = new   
CassandraHostConfigurator(host);
Cluster cluster = HFactory.getOrCreateCluster(clusterName,cassandraHostConfigurator);
KeyspaceDefinition keyspaceDef = cluster.describeKeyspace(keySpaceName);

ConfigurableConsistencyLevel configurableConsistencyLevel = new 
ConfigurableConsistencyLevel();
Map<String, HConsistencyLevel> clmap = new HashMap<String, HConsistencyLevel>();

// Define CL.ONE for ColumnFamily "ktable"
clmap.put(columFamilyName, HConsistencyLevel.ONE);
configurableConsistencyLevel.setReadCfConsistencyLevels(clmap);
configurableConsistencyLevel.setWriteCfConsistencyLevels(clmap);

if(keyspaceDef==null)   
{
KeyspaceDefinition newKeyspace = HFactory.createKeyspaceDefinition(
keySpaceName, ThriftKsDef.DEF_STRATEGY_CLASS,replicationFactor, null);
cluster.addKeyspace(newKeyspace, true);
}
Keyspace keyspace = HFactory.createKeyspace(keySpaceName, cluster,  
configurableConsistencyLevel);

StringSerializer ss = StringSerializer.get();
ColumnFamilyTemplate<String, String> cft = new ThriftColumnFamilyTemplate<String,  
String>(keyspace, columFamilyName, ss, ss);
ColumnFamilyUpdater<String, String> updater = cft.createUpdater("xkey");
UUID uid = new UUID();
updater.setValue("id",Long.toString(uid.getClockSeqAndNode()),ss);
updater.setValue("name", "Catherine", ss);
updater.setValue("state", "GA", ss);
cft.update(updater);
2
Can you paste the schema output from 'show schema;' in cassandra-cli? Is it possible the keyspace was created before your script ran with replication factor 1? - Richard
@Richard - Thank you for the reply. I created the keyspace with replication factor "2" before the code execution. create keyspace kspace with placement_strategy = 'SimpleStrategy' and strategy_options = {replication_factor : 2} and durable_writes = true; My question was - even in the event of node failure, how can I write/read data successfully to cassandra. Any help would be highly appreciated. Regards, Kiran. - Kiran
Any suggestions on my issue would be highly appreciated. - Kiran
@Kiran - Any luck on this? I'm running into this exact same issue. =/ - duskstriker

2 Answers

1
votes

Change your replication factor to 1, i.e. the number of nodes you had setup. I did get the same error exercising the examples.

0
votes

Try to change replication factor equals to no of nodes available in cluster ...it survives from such failures....in my example with RF=5, 5 node cluster can survive 2 node failure,with RF=4, 5 node cluster can survive 1 node failure,with RF=3, 5 node cluster can survive only 1 node failure ....

I am not sure whether this approach is efficient or not but this should work always (i.e RF = no of nodes)any suggestions from experts will be greatly appreciated...