Repository: cassandra
Updated Branches:
  refs/heads/trunk 834f2a6ec -> 0bc2164df


Use new token allocation for non bootstrap case as well.

> patch by Dikang Gu; reviewed by Branimir Lambov for CASSANSRA-13080
backported by Mick Semb Wever; reviewed by Jon Haddad for CASSANSRA-14212


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/8a5e88f6
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/8a5e88f6
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/8a5e88f6

Branch: refs/heads/trunk
Commit: 8a5e88f635fdb984505a99a553b5799cedccd06d
Parents: f6ec5c5
Author: Dikang Gu <dikan...@gmail.com>
Authored: Tue Dec 27 11:55:13 2016 -0800
Committer: Mick Semb Wever <m...@apache.org>
Committed: Tue Feb 13 08:02:02 2018 +1100

----------------------------------------------------------------------
 CHANGES.txt                                     |  1 +
 .../org/apache/cassandra/dht/BootStrapper.java  | 18 +++--
 .../dht/tokenallocator/TokenAllocation.java     |  3 -
 .../cassandra/service/StorageService.java       | 69 +++++++++-----------
 .../apache/cassandra/dht/BootStrapperTest.java  |  2 +-
 5 files changed, 48 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/8a5e88f6/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index bce7e1d..ba72406 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 3.11.2
+ * Backport CASSANDRA-13080: Use new token allocation for non bootstrap case 
as well (CASSANDRA-14212)
  * Remove dependencies on JVM internal classes from JMXServerUtils 
(CASSANDRA-14173) 
  * Add DEFAULT, UNSET, MBEAN and MBEANS to `ReservedKeywords` (CASSANDRA-14205)
  * Add Unittest for schema migration fix (CASSANDRA-14140)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/8a5e88f6/src/java/org/apache/cassandra/dht/BootStrapper.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/dht/BootStrapper.java 
b/src/java/org/apache/cassandra/dht/BootStrapper.java
index 392dbf2..1e00f48 100644
--- a/src/java/org/apache/cassandra/dht/BootStrapper.java
+++ b/src/java/org/apache/cassandra/dht/BootStrapper.java
@@ -33,12 +33,15 @@ import org.apache.cassandra.db.TypeSizes;
 import org.apache.cassandra.dht.tokenallocator.TokenAllocation;
 import org.apache.cassandra.exceptions.ConfigurationException;
 import org.apache.cassandra.gms.FailureDetector;
+import org.apache.cassandra.gms.Gossiper;
 import org.apache.cassandra.io.IVersionedSerializer;
 import org.apache.cassandra.io.util.DataInputPlus;
 import org.apache.cassandra.io.util.DataOutputPlus;
 import org.apache.cassandra.locator.AbstractReplicationStrategy;
 import org.apache.cassandra.locator.TokenMetadata;
+import org.apache.cassandra.service.StorageService;
 import org.apache.cassandra.streaming.*;
+import org.apache.cassandra.utils.FBUtilities;
 import org.apache.cassandra.utils.progress.ProgressEvent;
 import org.apache.cassandra.utils.progress.ProgressEventNotifierSupport;
 import org.apache.cassandra.utils.progress.ProgressEventType;
@@ -155,7 +158,7 @@ public class BootStrapper extends 
ProgressEventNotifierSupport
      * otherwise, if allocationKeyspace is specified use the token allocation 
algorithm to generate suitable tokens
      * else choose num_tokens tokens at random
      */
-    public static Collection<Token> getBootstrapTokens(final TokenMetadata 
metadata, InetAddress address) throws ConfigurationException
+    public static Collection<Token> getBootstrapTokens(final TokenMetadata 
metadata, InetAddress address, int schemaWaitDelay) throws 
ConfigurationException
     {
         String allocationKeyspace = 
DatabaseDescriptor.getAllocateTokensForKeyspace();
         Collection<String> initialTokens = 
DatabaseDescriptor.getInitialTokens();
@@ -171,7 +174,7 @@ public class BootStrapper extends 
ProgressEventNotifierSupport
             throw new ConfigurationException("num_tokens must be >= 1");
 
         if (allocationKeyspace != null)
-            return allocateTokens(metadata, address, allocationKeyspace, 
numTokens);
+            return allocateTokens(metadata, address, allocationKeyspace, 
numTokens, schemaWaitDelay);
 
         if (numTokens == 1)
             logger.warn("Picking random token for a single vnode.  You should 
probably add more vnodes and/or use the automatic token allocation mechanism.");
@@ -182,7 +185,7 @@ public class BootStrapper extends 
ProgressEventNotifierSupport
     private static Collection<Token> getSpecifiedTokens(final TokenMetadata 
metadata,
                                                         Collection<String> 
initialTokens)
     {
-        logger.trace("tokens manually specified as {}",  initialTokens);
+        logger.info("tokens manually specified as {}",  initialTokens);
         List<Token> tokens = new ArrayList<>(initialTokens.size());
         for (String tokenString : initialTokens)
         {
@@ -197,8 +200,13 @@ public class BootStrapper extends 
ProgressEventNotifierSupport
     static Collection<Token> allocateTokens(final TokenMetadata metadata,
                                             InetAddress address,
                                             String allocationKeyspace,
-                                            int numTokens)
+                                            int numTokens,
+                                            int schemaWaitDelay)
     {
+        StorageService.instance.waitForSchema(schemaWaitDelay);
+        if 
(!FBUtilities.getBroadcastAddress().equals(InetAddress.getLoopbackAddress()))
+            Gossiper.waitToSettle();
+
         Keyspace ks = Keyspace.open(allocationKeyspace);
         if (ks == null)
             throw new ConfigurationException("Problem opening token allocation 
keyspace " + allocationKeyspace);
@@ -216,6 +224,8 @@ public class BootStrapper extends 
ProgressEventNotifierSupport
             if (metadata.getEndpoint(token) == null)
                 tokens.add(token);
         }
+
+        logger.info("Generated random tokens. tokens are {}", tokens);
         return tokens;
     }
 

http://git-wip-us.apache.org/repos/asf/cassandra/blob/8a5e88f6/src/java/org/apache/cassandra/dht/tokenallocator/TokenAllocation.java
----------------------------------------------------------------------
diff --git 
a/src/java/org/apache/cassandra/dht/tokenallocator/TokenAllocation.java 
b/src/java/org/apache/cassandra/dht/tokenallocator/TokenAllocation.java
index 15d7868..9c50613 100644
--- a/src/java/org/apache/cassandra/dht/tokenallocator/TokenAllocation.java
+++ b/src/java/org/apache/cassandra/dht/tokenallocator/TokenAllocation.java
@@ -53,9 +53,6 @@ public class TokenAllocation
                                                    final InetAddress endpoint,
                                                    int numTokens)
     {
-        if 
(!FBUtilities.getBroadcastAddress().equals(InetAddress.getLoopbackAddress()))
-            Gossiper.waitToSettle();
-
         TokenMetadata tokenMetadataCopy = tokenMetadata.cloneOnlyTokenMap();
         StrategyAdapter strategy = getStrategy(tokenMetadataCopy, rs, 
endpoint);
         Collection<Token> tokens = create(tokenMetadata, 
strategy).addUnit(endpoint, numTokens);

http://git-wip-us.apache.org/repos/asf/cassandra/blob/8a5e88f6/src/java/org/apache/cassandra/service/StorageService.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/service/StorageService.java 
b/src/java/org/apache/cassandra/service/StorageService.java
index 5106fad6..b743bf3 100644
--- a/src/java/org/apache/cassandra/service/StorageService.java
+++ b/src/java/org/apache/cassandra/service/StorageService.java
@@ -748,7 +748,12 @@ public class StorageService extends 
NotificationBroadcasterSupport implements IE
 
     private boolean shouldBootstrap()
     {
-        return DatabaseDescriptor.isAutoBootstrap() && 
!SystemKeyspace.bootstrapComplete() && 
!DatabaseDescriptor.getSeeds().contains(FBUtilities.getBroadcastAddress());
+        return DatabaseDescriptor.isAutoBootstrap() && 
!SystemKeyspace.bootstrapComplete() && !isSeed();
+    }
+
+    public static boolean isSeed()
+    {
+        return 
DatabaseDescriptor.getSeeds().contains(FBUtilities.getBroadcastAddress());
     }
 
     private void prepareToJoin() throws ConfigurationException
@@ -831,6 +836,29 @@ public class StorageService extends 
NotificationBroadcasterSupport implements IE
         }
     }
 
+    public void waitForSchema(int delay)
+    {
+        // first sleep the delay to make sure we see all our peers
+        for (int i = 0; i < delay; i += 1000)
+        {
+            // if we see schema, we can proceed to the next check directly
+            if 
(!Schema.instance.getVersion().equals(SchemaConstants.emptyVersion))
+            {
+                logger.debug("got schema: {}", Schema.instance.getVersion());
+                break;
+            }
+            Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
+        }
+        // if our schema hasn't matched yet, wait until it has
+        // we do this by waiting for all in-flight migration requests and 
responses to complete
+        // (post CASSANDRA-1391 we don't expect this to be necessary very 
often, but it doesn't hurt to be careful)
+        if (!MigrationManager.isReadyForBootstrap())
+        {
+            setMode(Mode.JOINING, "waiting for schema information to 
complete", true);
+            MigrationManager.waitUntilReadyForBootstrap();
+        }
+    }
+
     private void joinTokenRing(int delay) throws ConfigurationException
     {
         joined = true;
@@ -867,25 +895,7 @@ public class StorageService extends 
NotificationBroadcasterSupport implements IE
             else
                 
SystemKeyspace.setBootstrapState(SystemKeyspace.BootstrapState.IN_PROGRESS);
             setMode(Mode.JOINING, "waiting for ring information", true);
-            // first sleep the delay to make sure we see all our peers
-            for (int i = 0; i < delay; i += 1000)
-            {
-                // if we see schema, we can proceed to the next check directly
-                if 
(!Schema.instance.getVersion().equals(SchemaConstants.emptyVersion))
-                {
-                    logger.debug("got schema: {}", 
Schema.instance.getVersion());
-                    break;
-                }
-                Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
-            }
-            // if our schema hasn't matched yet, wait until it has
-            // we do this by waiting for all in-flight migration requests and 
responses to complete
-            // (post CASSANDRA-1391 we don't expect this to be necessary very 
often, but it doesn't hurt to be careful)
-            if (!MigrationManager.isReadyForBootstrap())
-            {
-                setMode(Mode.JOINING, "waiting for schema information to 
complete", true);
-                MigrationManager.waitUntilReadyForBootstrap();
-            }
+            waitForSchema(delay);
             setMode(Mode.JOINING, "schema complete, ready to bootstrap", true);
             setMode(Mode.JOINING, "waiting for pending range calculation", 
true);
             PendingRangeCalculatorService.instance.blockUntilFinished();
@@ -915,7 +925,7 @@ public class StorageService extends 
NotificationBroadcasterSupport implements IE
                     throw new UnsupportedOperationException(s);
                 }
                 setMode(Mode.JOINING, "getting bootstrap token", true);
-                bootstrapTokens = 
BootStrapper.getBootstrapTokens(tokenMetadata, 
FBUtilities.getBroadcastAddress());
+                bootstrapTokens = 
BootStrapper.getBootstrapTokens(tokenMetadata, 
FBUtilities.getBroadcastAddress(), delay);
             }
             else
             {
@@ -971,22 +981,7 @@ public class StorageService extends 
NotificationBroadcasterSupport implements IE
             bootstrapTokens = SystemKeyspace.getSavedTokens();
             if (bootstrapTokens.isEmpty())
             {
-                Collection<String> initialTokens = 
DatabaseDescriptor.getInitialTokens();
-                if (initialTokens.size() < 1)
-                {
-                    bootstrapTokens = 
BootStrapper.getRandomTokens(tokenMetadata, DatabaseDescriptor.getNumTokens());
-                    if (DatabaseDescriptor.getNumTokens() == 1)
-                        logger.warn("Generated random token {}. Random tokens 
will result in an unbalanced ring; see 
http://wiki.apache.org/cassandra/Operations";, bootstrapTokens);
-                    else
-                        logger.info("Generated random tokens. tokens are {}", 
bootstrapTokens);
-                }
-                else
-                {
-                    bootstrapTokens = new ArrayList<>(initialTokens.size());
-                    for (String token : initialTokens)
-                        
bootstrapTokens.add(getTokenFactory().fromString(token));
-                    logger.info("Saved tokens not found. Using configuration 
value: {}", bootstrapTokens);
-                }
+                bootstrapTokens = 
BootStrapper.getBootstrapTokens(tokenMetadata, 
FBUtilities.getBroadcastAddress(), delay);
             }
             else
             {

http://git-wip-us.apache.org/repos/asf/cassandra/blob/8a5e88f6/test/unit/org/apache/cassandra/dht/BootStrapperTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/dht/BootStrapperTest.java 
b/test/unit/org/apache/cassandra/dht/BootStrapperTest.java
index 3af52e5..ed15a70 100644
--- a/test/unit/org/apache/cassandra/dht/BootStrapperTest.java
+++ b/test/unit/org/apache/cassandra/dht/BootStrapperTest.java
@@ -224,7 +224,7 @@ public class BootStrapperTest
     private void allocateTokensForNode(int vn, String ks, TokenMetadata tm, 
InetAddress addr)
     {
         SummaryStatistics os = 
TokenAllocation.replicatedOwnershipStats(tm.cloneOnlyTokenMap(), 
Keyspace.open(ks).getReplicationStrategy(), addr);
-        Collection<Token> tokens = BootStrapper.allocateTokens(tm, addr, ks, 
vn);
+        Collection<Token> tokens = BootStrapper.allocateTokens(tm, addr, ks, 
vn, 0);
         assertEquals(vn, tokens.size());
         tm.updateNormalTokens(tokens, addr);
         SummaryStatistics ns = 
TokenAllocation.replicatedOwnershipStats(tm.cloneOnlyTokenMap(), 
Keyspace.open(ks).getReplicationStrategy(), addr);


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

Reply via email to