You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by mc...@apache.org on 2020/11/18 12:11:39 UTC

[cassandra] branch cassandra-3.0 updated: Check between num_tokens and initial_token only applies to vnodes usage

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

mck pushed a commit to branch cassandra-3.0
in repository https://gitbox.apache.org/repos/asf/cassandra.git


The following commit(s) were added to refs/heads/cassandra-3.0 by this push:
     new bfd5d20  Check between num_tokens and initial_token only applies to vnodes usage
bfd5d20 is described below

commit bfd5d20a13501d897d8d34acce9b0394fa1cf00b
Author: Stefan Miklosovic <st...@instaclustr.com>
AuthorDate: Wed Nov 18 10:21:11 2020 +0100

    Check between num_tokens and initial_token only applies to vnodes usage
    
     patch by Stefan Miklosovic; reviewed by Mick Semb Wever for CASSANDRA-14477
---
 NEWS.txt                                           |  2 +-
 .../cassandra/config/DatabaseDescriptor.java       |  5 +++-
 .../cassandra/config/DatabaseDescriptorTest.java   | 34 ++++++++++++++++++----
 3 files changed, 33 insertions(+), 8 deletions(-)

diff --git a/NEWS.txt b/NEWS.txt
index 42fbf63..7034c2c 100644
--- a/NEWS.txt
+++ b/NEWS.txt
@@ -47,7 +47,7 @@ using the provided 'sstableupgrade' tool.
 
 Upgrading
 ---------
-    - In cassandra.yaml, num_tokens must be defined if initial_token is defined.
+    - In cassandra.yaml, when using vnodes num_tokens must be defined if initial_token is defined.
       If it is not defined, or not equal to the numbers of tokens defined in initial_tokens,
       the node will not start. See CASSANDRA-14477 for details.
 
diff --git a/src/java/org/apache/cassandra/config/DatabaseDescriptor.java b/src/java/org/apache/cassandra/config/DatabaseDescriptor.java
index 04293fb..3f9aa96 100644
--- a/src/java/org/apache/cassandra/config/DatabaseDescriptor.java
+++ b/src/java/org/apache/cassandra/config/DatabaseDescriptor.java
@@ -300,7 +300,10 @@ public class DatabaseDescriptor
             Collection<String> tokens = tokensFromString(config.initial_token);
             if (config.num_tokens == null)
             {
-                throw new ConfigurationException("initial_token was set but num_tokens is not!", false);
+                if (tokens.size() == 1)
+                    config.num_tokens = 1;
+                else
+                    throw new ConfigurationException("initial_token was set but num_tokens is not!", false);
             }
 
             if (tokens.size() != config.num_tokens)
diff --git a/test/unit/org/apache/cassandra/config/DatabaseDescriptorTest.java b/test/unit/org/apache/cassandra/config/DatabaseDescriptorTest.java
index 7614e02..0dcc7f7 100644
--- a/test/unit/org/apache/cassandra/config/DatabaseDescriptorTest.java
+++ b/test/unit/org/apache/cassandra/config/DatabaseDescriptorTest.java
@@ -315,7 +315,7 @@ public class DatabaseDescriptorTest
     }
 
     @Test
-    public void testApplyInitialTokensInitialTokensSetNumTokensSetAndDoesMatch() throws Exception
+    public void testApplyTokensConfigInitialTokensSetNumTokensSetAndDoesMatch() throws Exception
     {
         Config config = DatabaseDescriptor.loadConfig();
         config.initial_token = "0,256,1024";
@@ -337,7 +337,7 @@ public class DatabaseDescriptorTest
     }
 
     @Test
-    public void testApplyInitialTokensInitialTokensSetNumTokensSetAndDoesntMatch() throws Exception
+    public void testApplyTokensConfigInitialTokensSetNumTokensSetAndDoesntMatch() throws Exception
     {
         Config config = DatabaseDescriptor.loadConfig();
         config.initial_token = "0,256,1024";
@@ -349,7 +349,7 @@ public class DatabaseDescriptorTest
         {
             DatabaseDescriptor.applyTokensConfig(config);
 
-            Assert.fail("initial_token = 0,256,1024 and num_tokens = 10 but applyInitialTokens() did not fail!");
+            Assert.fail("initial_token = 0,256,1024 and num_tokens = 10 but applyTokensConfig() did not fail!");
         }
         catch (ConfigurationException ex)
         {
@@ -363,7 +363,7 @@ public class DatabaseDescriptorTest
     }
 
     @Test
-    public void testApplyInitialTokensInitialTokensSetNumTokensNotSet() throws Exception
+    public void testApplyTokensConfigInitialTokensSetNumTokensNotSet() throws Exception
     {
         Config config = DatabaseDescriptor.loadConfig();
 
@@ -387,7 +387,7 @@ public class DatabaseDescriptorTest
     }
 
     @Test
-    public void testApplyInitialTokensInitialTokensNotSetNumTokensSet() throws Exception
+    public void testApplyTokensConfigInitialTokensNotSetNumTokensSet() throws Exception
     {
         Config config = DatabaseDescriptor.loadConfig();
         config.num_tokens = 3;
@@ -408,7 +408,7 @@ public class DatabaseDescriptorTest
     }
 
     @Test
-    public void testApplyInitialTokensInitialTokensNotSetNumTokensNotSet() throws Exception
+    public void testApplyTokensConfigInitialTokensNotSetNumTokensNotSet() throws Exception
     {
         Config config = DatabaseDescriptor.loadConfig();
 
@@ -427,6 +427,28 @@ public class DatabaseDescriptorTest
         Assert.assertTrue(DatabaseDescriptor.tokensFromString(config.initial_token).isEmpty());
     }
 
+    @Test
+    public void testApplyTokensConfigInitialTokensOneNumTokensNotSet() throws Exception
+    {
+        Config config = DatabaseDescriptor.loadConfig();
+        config.initial_token = "123";
+        config.num_tokens = null;
+
+        unregisterSnitchesForTokenConfigTest();
+
+        try
+        {
+            DatabaseDescriptor.applyTokensConfig(config);
+        }
+        finally
+        {
+            unregisterSnitchesForTokenConfigTest();
+        }
+
+        Assert.assertEquals(Integer.valueOf(1), config.num_tokens);
+        Assert.assertEquals(1, DatabaseDescriptor.tokensFromString(config.initial_token).size());
+    }
+
     private void unregisterSnitchesForTokenConfigTest() throws Exception
     {
         try


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