Restore snapshotting of system ks on version change

Patch by Sam Tunnicliffe; reviewed by Tommy Stendahl and Aleksey Yeschenko for CASSANDRA-14412
This commit is contained in:
Sam Tunnicliffe 2018-04-19 11:51:35 +01:00
parent 35a9b501b2
commit 372a6cfa7b
4 changed files with 26 additions and 14 deletions

View File

@ -1,4 +1,5 @@
4.0
* Restore snapshotting of system keyspaces on version change (CASSANDRA-14412)
* Fix AbstractBTreePartition locking in java 11 (CASSANDRA-14607)
* SimpleClient should pass connection properties as options (CASSANDRA-15056)
* Set repaired data tracking flag on range reads if enabled (CASSANDRA-15019)

View File

@ -1389,30 +1389,27 @@ public final class SystemKeyspace
/**
* Compare the release version in the system.local table with the one included in the distro.
* If they don't match, snapshot all tables in the system keyspace. This is intended to be
* called at startup to create a backup of the system tables during an upgrade
* If they don't match, snapshot all tables in the system and schema keyspaces. This is intended
* to be called at startup to create a backup of the system tables during an upgrade
*
* @throws IOException
*/
public static boolean snapshotOnVersionChange() throws IOException
public static void snapshotOnVersionChange() throws IOException
{
String previous = getPreviousVersionString();
String next = FBUtilities.getReleaseVersionString();
// if we're restarting after an upgrade, snapshot the system keyspace
// if we're restarting after an upgrade, snapshot the system and schema keyspaces
if (!previous.equals(NULL_VERSION.toString()) && !previous.equals(next))
{
logger.info("Detected version upgrade from {} to {}, snapshotting system keyspace", previous, next);
logger.info("Detected version upgrade from {} to {}, snapshotting system keyspaces", previous, next);
String snapshotName = Keyspace.getTimestampedSnapshotName(format("upgrade-%s-%s",
previous,
next));
Keyspace systemKs = Keyspace.open(SchemaConstants.SYSTEM_KEYSPACE_NAME);
systemKs.snapshot(snapshotName, null);
return true;
for (String keyspace : SchemaConstants.LOCAL_SYSTEM_KEYSPACE_NAMES)
Keyspace.open(keyspace).snapshot(snapshotName, null);
}
return false;
}
/**

View File

@ -204,6 +204,15 @@ public class CassandraDaemon
exitOrFail(e.returnCode, e.getMessage(), e.getCause());
}
try
{
SystemKeyspace.snapshotOnVersionChange();
}
catch (IOException e)
{
exitOrFail(3, e.getMessage(), e.getCause());
}
// We need to persist this as soon as possible after startup checks.
// This should be the first write to SystemKeyspace (CASSANDRA-11742)
SystemKeyspace.persistLocalMetadata();

View File

@ -31,6 +31,7 @@ import org.apache.cassandra.cql3.QueryProcessor;
import org.apache.cassandra.cql3.UntypedResultSet;
import org.apache.cassandra.dht.ByteOrderedPartitioner.BytesToken;
import org.apache.cassandra.dht.Token;
import org.apache.cassandra.schema.SchemaKeyspace;
import org.apache.cassandra.utils.ByteBufferUtil;
import org.apache.cassandra.utils.FBUtilities;
import org.apache.cassandra.utils.CassandraVersion;
@ -94,7 +95,7 @@ public class SystemKeyspaceTest
if (FBUtilities.isWindows)
assertEquals(expectedCount, getDeferredDeletionCount());
else
assertTrue(getSystemSnapshotFiles().isEmpty());
assertTrue(getSystemSnapshotFiles(SchemaConstants.SYSTEM_KEYSPACE_NAME).isEmpty());
}
private int getDeferredDeletionCount()
@ -131,7 +132,11 @@ public class SystemKeyspaceTest
// Compare versions again & verify that snapshots were created for all tables in the system ks
SystemKeyspace.snapshotOnVersionChange();
assertEquals(SystemKeyspace.metadata().tables.size(), getSystemSnapshotFiles().size());
Set<String> snapshottedSystemTables = getSystemSnapshotFiles(SchemaConstants.SYSTEM_KEYSPACE_NAME);
SystemKeyspace.metadata().tables.forEach(t -> assertTrue(snapshottedSystemTables.contains(t.name)));
Set<String> snapshottedSchemaTables = getSystemSnapshotFiles(SchemaConstants.SCHEMA_KEYSPACE_NAME);
SchemaKeyspace.metadata().tables.forEach(t -> assertTrue(snapshottedSchemaTables.contains(t.name)));
// clear out the snapshots & set the previous recorded version equal to the latest, we shouldn't
// see any new snapshots created this time.
@ -156,10 +161,10 @@ public class SystemKeyspaceTest
return (String.format("%s.%s.%s", semver.major - 1, semver.minor, semver.patch));
}
private Set<String> getSystemSnapshotFiles()
private Set<String> getSystemSnapshotFiles(String keyspace)
{
Set<String> snapshottedTableNames = new HashSet<>();
for (ColumnFamilyStore cfs : Keyspace.open(SchemaConstants.SYSTEM_KEYSPACE_NAME).getColumnFamilyStores())
for (ColumnFamilyStore cfs : Keyspace.open(keyspace).getColumnFamilyStores())
{
if (!cfs.getSnapshotDetails().isEmpty())
snapshottedTableNames.add(cfs.getTableName());