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

brandonwilliams pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/cassandra.git


The following commit(s) were added to refs/heads/trunk by this push:
     new dbb4319  Add the cause to the exception and allow to log when an 
invalid partitioner is detected.
dbb4319 is described below

commit dbb43197fe9c462b22c4e4c3ef8a0c55b5696fa7
Author: David Capwell <dcapw...@gmail.com>
AuthorDate: Tue Feb 18 13:35:38 2020 -0800

    Add the cause to the exception and allow to log when an invalid
    partitioner is detected.
    
    Patch by David Capwell, reviewed by brandonwilliams for CASSANDRA-15591
---
 .../cassandra/config/DatabaseDescriptor.java       | 11 +++-
 .../cassandra/config/DatabaseDescriptorTest.java   | 63 ++++++++++++++++++++++
 2 files changed, 72 insertions(+), 2 deletions(-)

diff --git a/src/java/org/apache/cassandra/config/DatabaseDescriptor.java 
b/src/java/org/apache/cassandra/config/DatabaseDescriptor.java
index d9c060b..14db023 100644
--- a/src/java/org/apache/cassandra/config/DatabaseDescriptor.java
+++ b/src/java/org/apache/cassandra/config/DatabaseDescriptor.java
@@ -1103,18 +1103,25 @@ public class DatabaseDescriptor
     // definitely not safe for tools + clients - implicitly instantiates schema
     public static void applyPartitioner()
     {
+        applyPartitioner(conf);
+    }
+
+    public static void applyPartitioner(Config conf)
+    {
         /* Hashing strategy */
         if (conf.partitioner == null)
         {
             throw new ConfigurationException("Missing directive: partitioner", 
false);
         }
+        String name = conf.partitioner;
         try
         {
-            partitioner = 
FBUtilities.newPartitioner(System.getProperty(Config.PROPERTY_PREFIX + 
"partitioner", conf.partitioner));
+            name = System.getProperty(Config.PROPERTY_PREFIX + "partitioner", 
conf.partitioner);
+            partitioner = FBUtilities.newPartitioner(name);
         }
         catch (Exception e)
         {
-            throw new ConfigurationException("Invalid partitioner class " + 
conf.partitioner, false);
+            throw new ConfigurationException("Invalid partitioner class " + 
name, e);
         }
 
         paritionerName = partitioner.getClass().getCanonicalName();
diff --git a/test/unit/org/apache/cassandra/config/DatabaseDescriptorTest.java 
b/test/unit/org/apache/cassandra/config/DatabaseDescriptorTest.java
index efb7480..dc380be 100644
--- a/test/unit/org/apache/cassandra/config/DatabaseDescriptorTest.java
+++ b/test/unit/org/apache/cassandra/config/DatabaseDescriptorTest.java
@@ -26,7 +26,9 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.Enumeration;
 
+import com.google.common.base.Throwables;
 import org.junit.Assert;
+import org.junit.Assume;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -203,6 +205,67 @@ public class DatabaseDescriptorTest
         DatabaseDescriptor.applyAddressConfig(testConfig);
 
     }
+
+    @Test
+    public void testInvalidPartition() throws Exception
+    {
+        Config testConfig = DatabaseDescriptor.loadConfig();
+        testConfig.partitioner = "ThisDoesNotExist";
+
+        try
+        {
+            DatabaseDescriptor.applyPartitioner(testConfig);
+            Assert.fail("Partition does not exist, so should fail");
+        }
+        catch (ConfigurationException e)
+        {
+            Assert.assertEquals("Invalid partitioner class ThisDoesNotExist", 
e.getMessage());
+            Throwable cause = Throwables.getRootCause(e);
+            Assert.assertNotNull("Unable to find root cause why partitioner 
was rejected", cause);
+            // this is a bit implementation specific, so free to change; 
mostly here to make sure reason isn't lost
+            Assert.assertEquals(ClassNotFoundException.class, 
cause.getClass());
+            Assert.assertEquals("org.apache.cassandra.dht.ThisDoesNotExist", 
cause.getMessage());
+        }
+    }
+
+    @Test
+    public void testInvalidPartitionPropertyOverride() throws Exception
+    {
+        String key = Config.PROPERTY_PREFIX + "partitioner";
+        String previous = System.getProperty(key);
+        try
+        {
+            System.setProperty(key, "ThisDoesNotExist");
+            Config testConfig = DatabaseDescriptor.loadConfig();
+            testConfig.partitioner = "Murmur3Partitioner";
+
+            try
+            {
+                DatabaseDescriptor.applyPartitioner(testConfig);
+                Assert.fail("Partition does not exist, so should fail");
+            }
+            catch (ConfigurationException e)
+            {
+                Assert.assertEquals("Invalid partitioner class 
ThisDoesNotExist", e.getMessage());
+                Throwable cause = Throwables.getRootCause(e);
+                Assert.assertNotNull("Unable to find root cause why 
partitioner was rejected", cause);
+                // this is a bit implementation specific, so free to change; 
mostly here to make sure reason isn't lost
+                Assert.assertEquals(ClassNotFoundException.class, 
cause.getClass());
+                
Assert.assertEquals("org.apache.cassandra.dht.ThisDoesNotExist", 
cause.getMessage());
+            }
+        }
+        finally
+        {
+            if (previous == null)
+            {
+                System.getProperties().remove(key);
+            }
+            else
+            {
+                System.setProperty(key, previous);
+            }
+        }
+    }
     
     @Test
     public void testTokensFromString()


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@cassandra.apache.org
For additional commands, e-mail: commits-h...@cassandra.apache.org

Reply via email to