Skip to content

Commit b5f8ac2

Browse files
committed
Update system schema tables with new distributed keyspace on upgrade
Patch by marcuse; reviewed by Sam Tunnicliffe for CASSANDRA-20872
1 parent 7802743 commit b5f8ac2

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
5.1
2+
* Update system schema tables with new distributed keyspace on upgrade (CASSANDRA-20872)
23
* Fix issue when running cms reconfiguration with paxos repair disabled (CASSANDRA-20869)
34
* Added additional parameter to JVM shutdown to allow for logs to be properly shutdown (CASSANDRA-20978)
45
* Improve isGossipOnlyMember and location lookup performance (CASSANDRA-21039)

src/java/org/apache/cassandra/schema/DistributedSchema.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.google.common.base.Preconditions;
2222
import com.google.common.collect.ImmutableMap;
23+
import com.google.common.collect.ImmutableList;
2324
import org.apache.cassandra.auth.AuthKeyspace;
2425
import org.apache.cassandra.config.DatabaseDescriptor;
2526
import org.apache.cassandra.cql3.functions.UserFunction;
@@ -35,6 +36,7 @@
3536
import org.apache.cassandra.tcm.serialization.Version;
3637
import org.apache.cassandra.tracing.TraceKeyspace;
3738
import org.apache.cassandra.utils.FBUtilities;
39+
import org.apache.cassandra.utils.Pair;
3840

3941
import java.io.IOException;
4042
import java.util.ArrayList;
@@ -152,14 +154,25 @@ public boolean hasAccordKeyspaces()
152154
return keyspaces.stream().anyMatch(ksm -> ksm.tables.stream().anyMatch(TableMetadata::requiresAccordSupport));
153155
}
154156

157+
/**
158+
* @deprecated since TCM, used on upgrade from gossip to populate system schema tables with the correct generation
159+
*/
160+
@Deprecated(since = "TCM")
161+
public static List<Pair<KeyspaceMetadata, Long>> distributedKeyspacesWithGeneration(Set<String> knownDatacenters)
162+
{
163+
return ImmutableList.of(Pair.create(DistributedMetadataLogKeyspace.initialMetadata(knownDatacenters), DistributedMetadataLogKeyspace.GENERATION),
164+
Pair.create(TraceKeyspace.metadata(), TraceKeyspace.GENERATION),
165+
Pair.create(SystemDistributedKeyspace.metadata(), SystemDistributedKeyspace.GENERATION),
166+
Pair.create(AuthKeyspace.metadata(),AuthKeyspace.GENERATION));
167+
}
168+
155169
public static DistributedSchema fromSystemTables(Keyspaces keyspaces, Set<String> knownDatacenters)
156170
{
157171
if (!keyspaces.containsKeyspace(SchemaConstants.METADATA_KEYSPACE_NAME))
158172
{
159-
Keyspaces kss = Keyspaces.of(DistributedMetadataLogKeyspace.initialMetadata(knownDatacenters),
160-
TraceKeyspace.metadata(),
161-
SystemDistributedKeyspace.metadata(),
162-
AuthKeyspace.metadata());
173+
Keyspaces kss = Keyspaces.none();
174+
for (Pair<KeyspaceMetadata, Long> ksmGen : distributedKeyspacesWithGeneration(knownDatacenters))
175+
kss = kss.with(ksmGen.left);
163176
for (KeyspaceMetadata ksm : keyspaces) // on disk keyspaces
164177
kss = kss.withAddedOrUpdated(kss.get(ksm.name)
165178
.map(k -> merged(ksm, k))

src/java/org/apache/cassandra/tcm/Startup.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
package org.apache.cassandra.tcm;
1919

2020
import java.io.IOException;
21+
import java.util.ArrayList;
2122
import java.util.Collections;
2223
import java.util.HashSet;
24+
import java.util.List;
2325
import java.util.Map;
2426
import java.util.Objects;
2527
import java.util.Optional;
@@ -37,6 +39,7 @@
3739
import org.apache.cassandra.config.CassandraRelevantProperties;
3840
import org.apache.cassandra.config.DatabaseDescriptor;
3941
import org.apache.cassandra.db.ColumnFamilyStore;
42+
import org.apache.cassandra.db.Mutation;
4043
import org.apache.cassandra.db.SystemKeyspace;
4144
import org.apache.cassandra.db.commitlog.CommitLog;
4245
import org.apache.cassandra.dht.BootStrapper;
@@ -49,8 +52,11 @@
4952
import org.apache.cassandra.gms.VersionedValue;
5053
import org.apache.cassandra.locator.InetAddressAndPort;
5154
import org.apache.cassandra.net.MessagingService;
55+
import org.apache.cassandra.schema.DistributedSchema;
5256
import org.apache.cassandra.schema.KeyspaceMetadata;
57+
import org.apache.cassandra.schema.Keyspaces;
5358
import org.apache.cassandra.schema.SchemaConstants;
59+
import org.apache.cassandra.schema.SchemaKeyspace;
5460
import org.apache.cassandra.schema.TableMetadata;
5561
import org.apache.cassandra.service.StorageService;
5662
import org.apache.cassandra.tcm.log.LocalLog;
@@ -69,6 +75,7 @@
6975
import org.apache.cassandra.tcm.transformations.UnsafeJoin;
7076
import org.apache.cassandra.tcm.transformations.cms.Initialize;
7177
import org.apache.cassandra.utils.FBUtilities;
78+
import org.apache.cassandra.utils.Pair;
7279

7380
import static org.apache.cassandra.tcm.ClusterMetadataService.State.LOCAL;
7481
import static org.apache.cassandra.tcm.compatibility.GossipHelper.emptyWithSchemaFromSystemTables;
@@ -254,12 +261,26 @@ public static void initializeForDiscovery(Runnable initMessaging)
254261
Election.instance.migrated();
255262
}
256263

264+
private static void updateSystemSchemaTables(Set<String> knownDatacenters)
265+
{
266+
List<Pair<KeyspaceMetadata, Long>> kss = DistributedSchema.distributedKeyspacesWithGeneration(knownDatacenters);
267+
List<Mutation> mutations = new ArrayList<>();
268+
for (Pair<KeyspaceMetadata, Long> ksm : kss)
269+
{
270+
Keyspaces.KeyspacesDiff ksDiff = Keyspaces.diff(Keyspaces.none(), Keyspaces.of(ksm.left));
271+
mutations.addAll(SchemaKeyspace.convertSchemaDiffToMutations(ksDiff, ksm.right));
272+
}
273+
SchemaKeyspace.applyChanges(mutations);
274+
}
275+
257276
/**
258277
* This should only be called during startup.
259278
*/
260279
public static void initializeFromGossip(Function<Processor, Processor> wrapProcessor, Runnable initMessaging) throws StartupException
261280
{
262-
ClusterMetadata emptyFromSystemTables = emptyWithSchemaFromSystemTables(SystemKeyspace.allKnownDatacenters());
281+
Set<String> knownDcs = SystemKeyspace.allKnownDatacenters();
282+
updateSystemSchemaTables(knownDcs);
283+
ClusterMetadata emptyFromSystemTables = emptyWithSchemaFromSystemTables(knownDcs);
263284
LocalLog.LogSpec logSpec = LocalLog.logSpec()
264285
.withInitialState(emptyFromSystemTables)
265286
.afterReplay(Startup::scrubDataDirectories,

test/distributed/org/apache/cassandra/distributed/upgrade/ClusterMetadataUpgradeTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.cassandra.tcm.membership.NodeAddresses;
3636
import org.apache.cassandra.tcm.membership.NodeId;
3737

38+
import static org.junit.Assert.assertEquals;
3839
import static org.junit.Assert.assertFalse;
3940
import static org.psjava.util.AssertStatus.assertTrue;
4041

@@ -54,6 +55,11 @@ public void simpleUpgradeTest() throws Throwable
5455
cluster.schemaChange("CREATE TABLE " + KEYSPACE + ".tbl (pk int, ck int, v int, PRIMARY KEY (pk, ck))");
5556
})
5657
.runAfterClusterUpgrade((cluster) -> {
58+
Object [][] r = cluster.get(1).executeInternal("select keyspace_name from system_schema.keyspaces where keyspace_name='system_cluster_metadata'");
59+
assertEquals(1, r.length);
60+
r = cluster.get(1).executeInternal("select table_name from system_schema.tables where keyspace_name='system_cluster_metadata' and table_name='distributed_metadata_log'");
61+
assertEquals(1, r.length);
62+
5763
cluster.get(1).nodetoolResult("cms","initialize").asserts().success();
5864
cluster.forEach(i ->
5965
{

0 commit comments

Comments
 (0)