Author: [email protected]
Date: Mon Dec 19 11:59:36 2011
New Revision: 1859

Log:
[AMDATUCASSANDRA-125] Improved error handling in case of invalid configuration. 
Reasonable default values are used when available (and an error is logged).

Modified:
   
trunk/amdatu-cassandra/cassandra-application/src/main/java/org/amdatu/cassandra/application/service/CassandraConfigurationServiceImpl.java

Modified: 
trunk/amdatu-cassandra/cassandra-application/src/main/java/org/amdatu/cassandra/application/service/CassandraConfigurationServiceImpl.java
==============================================================================
--- 
trunk/amdatu-cassandra/cassandra-application/src/main/java/org/amdatu/cassandra/application/service/CassandraConfigurationServiceImpl.java
  (original)
+++ 
trunk/amdatu-cassandra/cassandra-application/src/main/java/org/amdatu/cassandra/application/service/CassandraConfigurationServiceImpl.java
  Mon Dec 19 11:59:36 2011
@@ -40,8 +40,14 @@
     // Statics
     private static final String STORAGE_CONF_SOURCE = "conf/cassandra.yaml";
     private static final String LOG4J_CONF_SOURCE = "conf/log4j.properties";
+
+    // (Reasonable) defaults
+    private static final int DEFAULT_RPC_PORT = 9160;
+    private static final int DEFAULT_STORAGE_PORT = 9160;
     private static final int DEFAULT_SCHEMA_AGREEMENT_TIMEOUT = 30;
     private static final int DEFAULT_THRIFT_TIMEOUT = 10;
+    private static final String DEFAULT_LISTEN_ADDRESS = "127.0.0.1";
+    private static final String DEFAULT_CLUSTER_NAME = "Amdatu Cluster";
 
     // Reference to the LogService
     private volatile LogService m_logService;
@@ -122,36 +128,89 @@
             if (dictionary.get(CONFIG_WORKDIR) == null) {
                 throw new ConfigurationException("Missing configuration key", 
CONFIG_WORKDIR);
             }
-            m_workDir = relativeToAbsolute((String) 
dictionary.get(CONFIG_WORKDIR));
-            m_defaultReplicationFactor = 
toInt(dictionary.get(DEFAULT_REPLICATION_FACTOR));
-            m_readConsistencyLevel = 
ConsistencyLevel.valueOf(dictionary.get(READ_CONSISTENCY_LEVEL).toString());
-            m_writeConsistencyLevel = 
ConsistencyLevel.valueOf(dictionary.get(WRITE_CONSISTENCY_LEVEL).toString());
-            m_bootstrapMode = 
dictionary.get(AUTOBOOTSTRAP_MODE).toString().equalsIgnoreCase("true");
-            m_rpcAddress = dictionary.get(RPC_ADDRESS).toString();
+            m_workDir = relativeToAbsolute(getString(dictionary, 
CONFIG_WORKDIR, ""));
+            m_defaultReplicationFactor = getInt(dictionary, 
DEFAULT_REPLICATION_FACTOR, 1, true);
+
+            m_readConsistencyLevel = getCL(dictionary, READ_CONSISTENCY_LEVEL);
+            m_writeConsistencyLevel = getCL(dictionary, 
WRITE_CONSISTENCY_LEVEL);
+
+            m_bootstrapMode = getBoolean(dictionary, AUTOBOOTSTRAP_MODE, 
false);
+            m_rpcAddress = getString(dictionary, RPC_ADDRESS, "");
             if (m_rpcAddress.isEmpty()) {
-                m_rpcAddress = dictionary.get(LISTEN_ADDRESS).toString();
+                m_rpcAddress = getString(dictionary, LISTEN_ADDRESS, 
DEFAULT_LISTEN_ADDRESS);
             }
-            m_rpcPort = toInt(dictionary.get(RPC_PORT));
-            m_storagePort = toInt(dictionary.get(STORAGE_PORT));
-            m_clusterName = dictionary.get(CLUSTER_NAME).toString();
-            if (dictionary.get(SCHEMA_AGREEMENT_TIMEOUT) != null) {
-                m_schemaAgreementTimeout = 
toInt(dictionary.get(SCHEMA_AGREEMENT_TIMEOUT));
-            } else {
-                // For backwards compatibility
-                m_schemaAgreementTimeout = DEFAULT_SCHEMA_AGREEMENT_TIMEOUT;
+            m_rpcPort = getInt(dictionary, RPC_PORT, DEFAULT_RPC_PORT, true);
+            m_storagePort = getInt(dictionary, STORAGE_PORT, 
DEFAULT_STORAGE_PORT, true);
+            m_clusterName = getString(dictionary, CLUSTER_NAME, 
DEFAULT_CLUSTER_NAME);
+            m_schemaAgreementTimeout = getInt(dictionary, 
SCHEMA_AGREEMENT_TIMEOUT, DEFAULT_SCHEMA_AGREEMENT_TIMEOUT, false);
+            m_thriftTimeout = getInt(dictionary, THRIFT_TIMEOUT, 
DEFAULT_THRIFT_TIMEOUT, false);
+        }
+    }
+
+    @SuppressWarnings("rawtypes")
+    private ConsistencyLevel getCL(Dictionary dictionary, String key) {
+        Object rcl = dictionary.get(key);
+        if (rcl != null) {
+            try {
+                return ConsistencyLevel.valueOf(rcl.toString());
+            }
+            catch (IllegalArgumentException e) {
+                if (m_logService != null) {
+                    m_logService.log(LogService.LOG_WARNING, "Value '" + 
rcl.toString()
+                        + "' is invalid for property '" + key + "', switching 
to default Consistency Level ONE", e);
+                }
             }
-            
-            if (dictionary.get(THRIFT_TIMEOUT) != null) {
-                m_thriftTimeout = toInt(dictionary.get(THRIFT_TIMEOUT));
-            } else {
-                // For backwards compatibility
-                m_thriftTimeout = DEFAULT_THRIFT_TIMEOUT;
+        }
+        else if (m_logService != null) {
+            m_logService.log(LogService.LOG_ERROR, "No property found for 
property '" + key
+                + "', switching to default Consistency Level ONE");
+        }
+        // Return the default consistency level
+        return ConsistencyLevel.ONE;
+    }
+
+    @SuppressWarnings("rawtypes")
+    private String getString(final Dictionary dictionary, final String key, 
final String defaultValue) {
+        Object value = dictionary.get(key);
+        if (value == null) {
+            String msg = "No value set for property '" + key + "', switching 
to default value '" + defaultValue + "'";
+            log(msg);
+            return defaultValue;
+        }
+        return value.toString();
+    }
+
+    @SuppressWarnings("rawtypes")
+    private int getInt(final Dictionary dictionary, final String key, final 
int defaultValue, final boolean log) {
+        Object value = dictionary.get(key);
+        if (value == null) {
+            if (log) {
+                String msg = "No value set for property '" + key + "', 
switching to default value '" + defaultValue + "'";
+                log(msg);
             }
+            return defaultValue;
         }
+        return Integer.parseInt(value.toString().trim());
+    }
+
+    @SuppressWarnings("rawtypes")
+    private boolean getBoolean(final Dictionary dictionary, final String key, 
final boolean defaultValue) {
+        Object value = dictionary.get(key);
+        if (value == null) {
+            String msg = "No value set for property '" + key + "', switching 
to default value '" + defaultValue + "'";
+            log(msg);
+            return defaultValue;
+        }
+        return "true".equalsIgnoreCase(value.toString().trim());
     }
 
-    private int toInt(final Object property) {
-        return Integer.parseInt(property.toString().trim());
+    private void log(final String msg) {
+        if (m_logService != null) {
+            m_logService.log(LogService.LOG_ERROR, msg);
+        }
+        else {
+            System.err.println(msg);
+        }
     }
 
     private File relativeToAbsolute(final String workDir) {
@@ -223,11 +282,11 @@
     public String getClustername() {
         return m_clusterName;
     }
-    
+
     public int getSchemaAgreementTimeout() {
         return m_schemaAgreementTimeout;
     }
-    
+
     public int getThriftTimeout() {
         return m_thriftTimeout;
     }
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to