aweisberg commented on code in PR #4508: URL: https://github.com/apache/cassandra/pull/4508#discussion_r2590468890
########## test/unit/org/apache/cassandra/service/replication/migration/MutationTrackingMigrationStateTest.java: ########## @@ -0,0 +1,231 @@ +/* + * 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.service.replication.migration; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.junit.BeforeClass; +import org.junit.Test; + +import org.apache.cassandra.ServerTestUtils; +import org.apache.cassandra.dht.Murmur3Partitioner; +import org.apache.cassandra.dht.Range; +import org.apache.cassandra.dht.Token; +import org.apache.cassandra.io.util.DataInputBuffer; +import org.apache.cassandra.io.util.DataOutputBuffer; +import org.apache.cassandra.schema.TableId; +import org.apache.cassandra.tcm.Epoch; +import org.apache.cassandra.tcm.membership.NodeVersion; + +import static org.junit.Assert.*; + +public class MutationTrackingMigrationStateTest +{ + private static Murmur3Partitioner partitioner; + private static TableId testTableId; + + @BeforeClass + public static void setup() throws Exception + { + ServerTestUtils.prepareServerNoRegister(); + partitioner = Murmur3Partitioner.instance; + testTableId = TableId.generate(); + } + + @Test + public void testEmptyState() + { + MutationTrackingMigrationState state = MutationTrackingMigrationState.EMPTY; + assertNotNull(state); + assertEquals(Epoch.EMPTY, state.lastModified); + assertTrue(state.keyspaceInfo.isEmpty()); + assertFalse(state.hasMigratingKeyspaces()); + } + + @Test + public void testWithKeyspaceMigrating() + { + MutationTrackingMigrationState state = MutationTrackingMigrationState.EMPTY; + Epoch epoch = Epoch.create(1); + + MutationTrackingMigrationState updated = state.withKeyspaceMigrating( + "test_ks", + Collections.singletonList(testTableId), + epoch + ); + + assertNotSame(state, updated); + assertTrue(state.keyspaceInfo.isEmpty()); + + // Verify update + assertTrue(updated.hasMigratingKeyspaces()); + assertNotNull(updated.getKeyspaceInfo("test_ks")); + assertEquals("test_ks", updated.getKeyspaceInfo("test_ks").keyspace); + assertFalse(updated.getKeyspaceInfo("test_ks").getPendingRangesForTable(testTableId).isEmpty()); + } + + @Test + public void testStateTransitions() + { + MutationTrackingMigrationState state = MutationTrackingMigrationState.EMPTY; + List<Range<Token>> ranges = createTestRanges(); + Epoch epoch1 = Epoch.create(1); + Epoch epoch2 = Epoch.create(2); + + // Start migration (full ring is pending for testTableId) + state = state.withKeyspaceMigrating("test_ks", Collections.singletonList(testTableId), epoch1); + assertTrue(state.hasMigratingKeyspaces()); + assertNotNull(state.getKeyspaceInfo("test_ks")); + assertFalse(state.getKeyspaceInfo("test_ks").isComplete()); + + // Subtract migrated ranges for specific table + state = state.withRangesRepairedForTable("test_ks", testTableId, ranges, epoch2); + KeyspaceMigrationInfo info = state.getKeyspaceInfo("test_ks"); + // After subtracting ranges, pending should be smaller (not necessarily empty) + assertNotNull(info); + } + + @Test + public void testWithMigrationsCompleted() + { + MutationTrackingMigrationState state = MutationTrackingMigrationState.EMPTY; + Epoch epoch1 = Epoch.create(1); + Epoch epoch2 = Epoch.create(2); + + // Start migration + state = state.withKeyspaceMigrating("test_ks", Collections.singletonList(testTableId), epoch1); + assertTrue(state.hasMigratingKeyspaces()); + + // Complete migration by subtracting the full ring (all ranges completed for the table) + Range<Token> fullRing = new Range<>(partitioner.getMinimumToken(), partitioner.getMinimumToken()); + MutationTrackingMigrationState completed = state.withRangesRepairedForTable("test_ks", testTableId, Collections.singleton(fullRing), epoch2); + + assertNotSame(state, completed); + + // Verify completion (keyspace removed from migration state because pendingRanges is empty) + assertFalse(completed.hasMigratingKeyspaces()); + assertNull(completed.getKeyspaceInfo("test_ks")); + } + + @Test + public void testWithMigrationsRemoved() + { + MutationTrackingMigrationState state = MutationTrackingMigrationState.EMPTY; + Epoch epoch = Epoch.create(1); + + // Start migration + state = state.withKeyspaceMigrating("test_ks", Collections.singletonList(testTableId), epoch); + + // Remove migration + MutationTrackingMigrationState removed = state.dropKeyspaces(epoch, Collections.singleton("test_ks")); + + // Verify removal + assertFalse(removed.hasMigratingKeyspaces()); + assertNull(removed.getKeyspaceInfo("test_ks")); + } + + @Test + public void testSerializationRoundtrip() throws IOException + { + List<Range<Token>> ranges = createTestRanges(); + Epoch epoch1 = Epoch.create(1); + Epoch epoch2 = Epoch.create(2); + + MutationTrackingMigrationState original = MutationTrackingMigrationState.EMPTY + .withKeyspaceMigrating("test_ks", Collections.singletonList(testTableId), epoch1) + .withRangesRepairedForTable("test_ks", testTableId, ranges, epoch2); + + // Serialize + DataOutputBuffer out = new DataOutputBuffer(); + MutationTrackingMigrationState.serializer.serialize(original, out, NodeVersion.CURRENT_METADATA_VERSION); + + // Deserialize + DataInputBuffer in = new DataInputBuffer(out.unsafeGetBufferAndFlip(), false); + MutationTrackingMigrationState deserialized = MutationTrackingMigrationState.serializer.deserialize(in, NodeVersion.CURRENT_METADATA_VERSION); + + // Verify equality + assertEquals(original.lastModified, deserialized.lastModified); + assertEquals(original.keyspaceInfo.size(), deserialized.keyspaceInfo.size()); + for (String ks : original.keyspaceInfo.keySet()) + { + KeyspaceMigrationInfo origInfo = original.keyspaceInfo.get(ks); + KeyspaceMigrationInfo deserInfo = deserialized.keyspaceInfo.get(ks); + assertNotNull(deserInfo); + assertEquals(origInfo.keyspace, deserInfo.keyspace); + assertEquals(origInfo.pendingRangesPerTable.size(), deserInfo.pendingRangesPerTable.size()); Review Comment: Only compares size? -- 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]

