diff --git a/CHANGES.txt b/CHANGES.txt index ac84102522..b7880b259a 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -9,6 +9,7 @@ Merged from 4.1: Merged from 4.0: Merged from 3.11: Merged from 3.0: + * Refactor validation logic in StorageService.rebuild (CASSANDRA-18803) * Make alternation of a user type validate the same way as creation of a user type does (CASSANDRA-18585) 5.0-alpha1 diff --git a/src/java/org/apache/cassandra/service/StorageService.java b/src/java/org/apache/cassandra/service/StorageService.java index a33270a77e..c119f2d82b 100644 --- a/src/java/org/apache/cassandra/service/StorageService.java +++ b/src/java/org/apache/cassandra/service/StorageService.java @@ -1419,45 +1419,36 @@ public class StorageService extends NotificationBroadcasterSupport implements IE public void rebuild(String sourceDc, String keyspace, String tokens, String specificSources, boolean excludeLocalDatacenterNodes) { - try + // fail if source DC is local and --exclude-local-dc is set + if (sourceDc != null && sourceDc.equals(DatabaseDescriptor.getLocalDataCenter()) && excludeLocalDatacenterNodes) { - // check ongoing rebuild - if (!isRebuilding.compareAndSet(false, true)) - { - throw new IllegalStateException("Node is still rebuilding. Check nodetool netstats."); - } + throw new IllegalArgumentException("Cannot set source data center to be local data center, when excludeLocalDataCenter flag is set"); + } - // fail if source DC is local and --exclude-local-dc is set - if (sourceDc != null && sourceDc.equals(DatabaseDescriptor.getLocalDataCenter()) && excludeLocalDatacenterNodes) + if (sourceDc != null) + { + TokenMetadata.Topology topology = getTokenMetadata().cloneOnlyTokenMap().getTopology(); + Set availableDCs = topology.getDatacenterEndpoints().keySet(); + if (!availableDCs.contains(sourceDc)) { - throw new IllegalArgumentException("Cannot set source data center to be local data center, when excludeLocalDataCenter flag is set"); - } - - if (sourceDc != null) - { - TokenMetadata.Topology topology = getTokenMetadata().cloneOnlyTokenMap().getTopology(); - Set availableDCs = topology.getDatacenterEndpoints().keySet(); - if (!availableDCs.contains(sourceDc)) - { - throw new IllegalArgumentException(String.format("Provided datacenter '%s' is not a valid datacenter, available datacenters are: %s", - sourceDc, String.join(",", availableDCs))); - } + throw new IllegalArgumentException(String.format("Provided datacenter '%s' is not a valid datacenter, available datacenters are: %s", + sourceDc, String.join(",", availableDCs))); } } - catch (Throwable ex) + + if (keyspace == null && tokens != null) { - isRebuilding.set(false); - throw ex; + throw new IllegalArgumentException("Cannot specify tokens without keyspace."); + } + + // check ongoing rebuild + if (!isRebuilding.compareAndSet(false, true)) + { + throw new IllegalStateException("Node is still rebuilding. Check nodetool netstats."); } try { - // check the arguments - if (keyspace == null && tokens != null) - { - throw new IllegalArgumentException("Cannot specify tokens without keyspace."); - } - logger.info("rebuild from dc: {}, {}, {}", sourceDc == null ? "(any dc)" : sourceDc, keyspace == null ? "(All keyspaces)" : keyspace, tokens == null ? "(All tokens)" : tokens);