aweisberg commented on code in PR #4508: URL: https://github.com/apache/cassandra/pull/4508#discussion_r2590523934
########## test/unit/org/apache/cassandra/tcm/transformations/AlterSchemaMutationTrackingTest.java: ########## @@ -0,0 +1,225 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.tcm.transformations; + +import com.google.common.collect.ImmutableList; +import org.junit.BeforeClass; +import org.junit.Test; + +import org.apache.cassandra.ServerTestUtils; +import org.apache.cassandra.db.marshal.Int32Type; +import org.apache.cassandra.dht.Murmur3Partitioner; +import org.apache.cassandra.schema.KeyspaceMetadata; +import org.apache.cassandra.schema.KeyspaceParams; +import org.apache.cassandra.schema.Keyspaces; +import org.apache.cassandra.schema.ReplicationType; +import org.apache.cassandra.schema.TableMetadata; +import org.apache.cassandra.schema.Tables; +import org.apache.cassandra.service.replication.migration.KeyspaceMigrationInfo; +import org.apache.cassandra.service.replication.migration.MutationTrackingMigrationState; +import org.apache.cassandra.tcm.ClusterMetadata; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +/** + * Tests for AlterSchema auto-starting mutation tracking migration when replication type changes. + */ +public class AlterSchemaMutationTrackingTest +{ + @BeforeClass + public static void setUpClass() throws Exception + { + ServerTestUtils.daemonInitialization(); + ServerTestUtils.prepareServer(); + } + + /** + * Helper to create a keyspace with a simple table. + * Required because migrations need tables to track - empty keyspaces result in empty pendingRanges. + */ + private static KeyspaceMetadata createKeyspaceWithTable(String keyspaceName, int replicationFactor, ReplicationType replicationType) + { + TableMetadata table = TableMetadata.builder(keyspaceName, "test_table") + .addPartitionKeyColumn("key", Int32Type.instance) + .addRegularColumn("val", Int32Type.instance) + .build(); + + return KeyspaceMetadata.create(keyspaceName, KeyspaceParams.simple(replicationFactor, replicationType), Tables.of(table)); + } + + @Test + public void testAutoStartToTrackedMigration() + { + // Create initial keyspace with untracked replication + KeyspaceMetadata before = createKeyspaceWithTable("test_ks", 3, ReplicationType.untracked); + + // Change replication type to tracked + KeyspaceMetadata after = createKeyspaceWithTable("test_ks", 3, ReplicationType.tracked); + + // Create keyspace diff using Keyspaces.diff() + Keyspaces beforeKs = Keyspaces.of(before); + Keyspaces afterKs = Keyspaces.of(after); + Keyspaces.KeyspacesDiff diff = Keyspaces.diff(beforeKs, afterKs); + ImmutableList<KeyspaceMetadata.KeyspaceDiff> altered = diff.altered; + + // Create initial state with no migrations + MutationTrackingMigrationState prev = MutationTrackingMigrationState.EMPTY; + assertFalse("Should have no migrations initially", prev.hasMigratingKeyspaces()); + + // Create minimal ClusterMetadata for transformer + ClusterMetadata metadata = new ClusterMetadata(Murmur3Partitioner.instance); + ClusterMetadata.Transformer next = metadata.transformer(); + + // Call the method under test + ClusterMetadata.Transformer updated = AlterSchema.maybeUpdateMutationTrackingMigrationState(next.epoch(), prev, next, altered, Keyspaces.none()); + ClusterMetadata result = updated.build().metadata; + + // Verify migration was auto-started + assertTrue("Migration should be started", result.mutationTrackingMigrationState.hasMigratingKeyspaces()); Review Comment: Check the actual contents of the state down to the table? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

