You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by gd...@apache.org on 2010/03/18 15:04:07 UTC
svn commit: r924785 - in /cassandra/trunk:
src/java/org/apache/cassandra/config/ src/java/org/apache/cassandra/db/
src/java/org/apache/cassandra/db/commitlog/
test/unit/org/apache/cassandra/db/
Author: gdusbabek
Date: Thu Mar 18 14:04:06 2010
New Revision: 924785
URL: http://svn.apache.org/viewvc?rev=924785&view=rev
Log:
rename keyspace. patch by Gary Dusbabek, reviewed by Jonathan Ellis. CASSANDRA-861.
Modified:
cassandra/trunk/src/java/org/apache/cassandra/config/CFMetaData.java
cassandra/trunk/src/java/org/apache/cassandra/config/KSMetaData.java
cassandra/trunk/src/java/org/apache/cassandra/db/DefsTable.java
cassandra/trunk/src/java/org/apache/cassandra/db/commitlog/CommitLog.java
cassandra/trunk/test/unit/org/apache/cassandra/db/DefsTest.java
Modified: cassandra/trunk/src/java/org/apache/cassandra/config/CFMetaData.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/config/CFMetaData.java?rev=924785&r1=924784&r2=924785&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/config/CFMetaData.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/config/CFMetaData.java Thu Mar 18 14:04:06 2010
@@ -103,6 +103,13 @@ public final class CFMetaData
return new CFMetaData(cfm.tableName, newName, cfm.columnType, cfm.comparator, cfm.subcolumnComparator, cfm.comment, cfm.rowCacheSize, cfm.keyCacheSize, cfm.cfId);
}
+ /** clones existing CFMetaData. keeps the id but changes the table name.*/
+ public static CFMetaData renameTable(CFMetaData cfm, String tableName)
+ {
+ purge(cfm);
+ return new CFMetaData(tableName, cfm.cfName, cfm.columnType, cfm.comparator, cfm.subcolumnComparator, cfm.comment, cfm.rowCacheSize, cfm.keyCacheSize, cfm.cfId);
+ }
+
/** used for evicting cf data out of static tracking collections. */
public static void purge(CFMetaData cfm)
{
Modified: cassandra/trunk/src/java/org/apache/cassandra/config/KSMetaData.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/config/KSMetaData.java?rev=924785&r1=924784&r2=924785&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/config/KSMetaData.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/config/KSMetaData.java Thu Mar 18 14:04:06 2010
@@ -54,6 +54,16 @@ public final class KSMetaData
this.cfMetaData = Collections.<String, CFMetaData>unmodifiableMap(cfmap);
}
+ public static KSMetaData rename(KSMetaData ksm, String newName)
+ {
+ // cfs will need to have their tablenames reset. CFMetaData are immutable, so new ones get created with the
+ // same ids.
+ List<CFMetaData> newCfs = new ArrayList<CFMetaData>(ksm.cfMetaData().size());
+ for (CFMetaData oldCf : ksm.cfMetaData().values())
+ newCfs.add(CFMetaData.renameTable(oldCf, newName));
+ return new KSMetaData(newName, ksm.strategyClass, ksm.replicationFactor, ksm.snitch, newCfs.toArray(new CFMetaData[newCfs.size()]));
+ }
+
public boolean equals(Object obj)
{
if (obj == null)
Modified: cassandra/trunk/src/java/org/apache/cassandra/db/DefsTable.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/db/DefsTable.java?rev=924785&r1=924784&r2=924785&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/db/DefsTable.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/db/DefsTable.java Thu Mar 18 14:04:06 2010
@@ -37,8 +37,10 @@ import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
import java.util.List;
+import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
@@ -71,7 +73,8 @@ public class DefsTable
ksm = new KSMetaData(ksm.name, ksm.strategyClass, ksm.replicationFactor, ksm.snitch, newCfs.toArray(new CFMetaData[newCfs.size()]));
// store it.
- UUID newVersion = saveKeyspaceDefinition(ksm);
+
+ UUID newVersion = saveKeyspaceDefs(ksm, null);
// reinitialize the table.
Table.open(ksm.name).addCf(cfm.cfName);
@@ -105,7 +108,7 @@ public class DefsTable
ksm = new KSMetaData(ksm.name, ksm.strategyClass, ksm.replicationFactor, ksm.snitch, newCfs.toArray(new CFMetaData[newCfs.size()]));
// store it.
- UUID newVersion = saveKeyspaceDefinition(ksm);
+ UUID newVersion = saveKeyspaceDefs(ksm, null);
// reinitialize the table.
CFMetaData.purge(cfm);
@@ -147,13 +150,13 @@ public class DefsTable
ksm = new KSMetaData(ksm.name, ksm.strategyClass, ksm.replicationFactor, ksm.snitch, newCfs.toArray(new CFMetaData[newCfs.size()]));
// store it
- UUID newVersion = saveKeyspaceDefinition(ksm);
+ UUID newVersion = saveKeyspaceDefs(ksm, null);
// leave it up to operators to ensure there are no writes going on durng the file rename. Just know that
// attempting row mutations on oldcfName right now would be really bad.
try
{
- renameStorageFiles(ksm.name, oldCfm.cfName, newCfm.cfName);
+ renameCfStorageFiles(ksm.name, oldCfm.cfName, newCfm.cfName);
}
catch (IOException e)
{
@@ -181,7 +184,7 @@ public class DefsTable
if (DatabaseDescriptor.getTableDefinition(ksm.name) != null)
throw new ConfigurationException("Keyspace already exists.");
- UUID versionId = saveKeyspaceDefinition(ksm);
+ UUID versionId = saveKeyspaceDefs(ksm, null);
DatabaseDescriptor.setTableDefinition(ksm, versionId);
Table.open(ksm.name);
CommitLog.instance().forceNewSegment();
@@ -213,10 +216,7 @@ public class DefsTable
}
// update internal table.
- UUID versionId = UUIDGen.makeType1UUIDFromHost(FBUtilities.getLocalAddress());
- RowMutation rm = new RowMutation(Table.DEFINITIONS, versionId.toString());
- rm.delete(new QueryPath(SCHEMA_CF, null, ksm.name.getBytes()), System.currentTimeMillis());
- rm.apply();
+ UUID versionId = saveKeyspaceDefs(null, ksm);
// reset defs.
DatabaseDescriptor.clearTableDefinition(ksm, versionId);
@@ -228,6 +228,42 @@ public class DefsTable
});
}
+ public static Future rename(final KSMetaData oldKsm, final String newName)
+ {
+ return executor.submit(new WrappedRunnable()
+ {
+ protected void runMayThrow() throws Exception
+ {
+ if (oldKsm == null || DatabaseDescriptor.getTableDefinition(oldKsm.name) != oldKsm)
+ throw new ConfigurationException("Keyspace either does not exist or does not match the one currently defined.");
+ if (DatabaseDescriptor.getTableDefinition(newName) != null)
+ throw new ConfigurationException("Keyspace already exists.");
+
+ // clone the ksm, replacing thename.
+ KSMetaData newKsm = KSMetaData.rename(oldKsm, newName);
+ // at this point, the static methods in CFMetaData will start returning references to the new table, so
+ // it helps if the node is reasonably quiescent with respect to this ks.
+ UUID newVersion = saveKeyspaceDefs(newKsm, oldKsm);
+ try
+ {
+ renameKsStorageFiles(oldKsm.name, newName);
+ }
+ catch (IOException e)
+ {
+ ConfigurationException cex = new ConfigurationException("Critical: encountered IOException while attempting to rename KS storage files from " + oldKsm.name + " to " + newName);
+ throw cex;
+ }
+
+ DatabaseDescriptor.clearTableDefinition(oldKsm, newVersion);
+ DatabaseDescriptor.setTableDefinition(newKsm, newVersion);
+ Table.clear(oldKsm.name);
+ Table.open(newName);
+ // this isn't strictly necessary since the set of all cfs was not modified.
+ CommitLog.instance().forceNewSegment();
+ }
+ });
+ }
+
/** dumps current keyspace definitions to storage */
public static synchronized void dumpToStorage(UUID version) throws IOException
{
@@ -252,7 +288,6 @@ public class DefsTable
Collection<KSMetaData> tables = new ArrayList<KSMetaData>();
for (IColumn col : cf.getSortedColumns())
{
- String ksName = new String(col.name());
KSMetaData ks = KSMetaData.deserialize(new ByteArrayInputStream(col.value()));
tables.add(ks);
}
@@ -272,8 +307,7 @@ public class DefsTable
return pathname.getName().startsWith(cf + "-") && pathname.getName().endsWith(".db") && pathname.exists();
}
});
- for (File f : dbFiles)
- found.add(f);
+ found.addAll(Arrays.asList(dbFiles));
}
return found;
}
@@ -298,8 +332,33 @@ public class DefsTable
}
}
}
+
+ private static void renameKsStorageFiles(String oldKs, String newKs) throws IOException
+ {
+ IOException mostRecentProblem = null;
+ Set<String> cfNames = DatabaseDescriptor.getTableDefinition(oldKs).cfMetaData().keySet();
+ for (String cfName : cfNames)
+ {
+ for (File existing : getFiles(oldKs, cfName))
+ {
+ try
+ {
+ File newParent = new File(existing.getParentFile().getParent(), newKs);
+ newParent.mkdirs();
+ FileUtils.renameWithConfirm(existing, new File(newParent, existing.getName()));
+ }
+ catch (IOException ex)
+ {
+ mostRecentProblem = ex;
+ }
+ }
+ }
+ if (mostRecentProblem != null)
+ throw new IOException("One or more IOExceptions encountered while renaming files. Most recent problem is included.", mostRecentProblem);
+ }
+
// if this errors out, we are in a world of hurt.
- private static void renameStorageFiles(String table, String oldCfName, String newCfName) throws IOException
+ private static void renameCfStorageFiles(String table, String oldCfName, String newCfName) throws IOException
{
// complete as much of the job as possible. Don't let errors long the way prevent as much renaming as possible
// from happening.
@@ -320,12 +379,15 @@ public class DefsTable
throw new IOException("One or more IOExceptions encountered while renaming files. Most recent problem is included.", mostRecentProblem);
}
- private static UUID saveKeyspaceDefinition(KSMetaData ksm) throws IOException
+ private static UUID saveKeyspaceDefs(KSMetaData add, KSMetaData remove) throws IOException
{
- UUID newVersion = UUIDGen.makeType1UUIDFromHost(FBUtilities.getLocalAddress());
- RowMutation rm = new RowMutation(Table.DEFINITIONS, newVersion.toString());
- rm.add(new QueryPath(SCHEMA_CF, null, ksm.name.getBytes()), KSMetaData.serialize(ksm), System.currentTimeMillis());
+ UUID versionId = UUIDGen.makeType1UUIDFromHost(FBUtilities.getLocalAddress());
+ RowMutation rm = new RowMutation(Table.DEFINITIONS, versionId.toString());
+ if (remove != null)
+ rm.delete(new QueryPath(SCHEMA_CF, null, remove.name.getBytes()), System.currentTimeMillis());
+ if (add != null)
+ rm.add(new QueryPath(SCHEMA_CF, null, add.name.getBytes()), KSMetaData.serialize(add), System.currentTimeMillis());
rm.apply();
- return newVersion;
+ return versionId;
}
}
Modified: cassandra/trunk/src/java/org/apache/cassandra/db/commitlog/CommitLog.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/db/commitlog/CommitLog.java?rev=924785&r1=924784&r2=924785&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/db/commitlog/CommitLog.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/db/commitlog/CommitLog.java Thu Mar 18 14:04:06 2010
@@ -238,7 +238,6 @@ public class CommitLog
{
public void runMayThrow() throws IOException
{
- KSMetaData ksm = DatabaseDescriptor.getTableDefinition(table.name);
RowMutation newRm = new RowMutation(rm.getTable(), rm.key());
// Rebuild the row mutation, omitting column families that a) have already been flushed,
Modified: cassandra/trunk/test/unit/org/apache/cassandra/db/DefsTest.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/db/DefsTest.java?rev=924785&r1=924784&r2=924785&view=diff
==============================================================================
--- cassandra/trunk/test/unit/org/apache/cassandra/db/DefsTest.java (original)
+++ cassandra/trunk/test/unit/org/apache/cassandra/db/DefsTest.java Thu Mar 18 14:04:06 2010
@@ -27,6 +27,7 @@ import org.apache.cassandra.db.commitlog
import org.apache.cassandra.db.filter.NamesQueryFilter;
import org.apache.cassandra.db.filter.QueryPath;
import org.apache.cassandra.db.filter.SliceQueryFilter;
+import org.apache.cassandra.db.marshal.BytesType;
import org.apache.cassandra.locator.EndPointSnitch;
import org.apache.cassandra.locator.RackAwareStrategy;
import org.apache.cassandra.utils.FBUtilities;
@@ -39,6 +40,8 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import java.util.SortedSet;
+import java.util.TreeSet;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
@@ -242,7 +245,6 @@ public class DefsTest extends CleanupHel
ColumnFamilyStore store = Table.open(cfm.tableName).getColumnFamilyStore(cfm.cfName);
assert store != null;
store.forceBlockingFlush();
- store.getFlushPath();
assert DefsTable.getFiles(cfm.tableName, cfm.cfName).size() > 0;
DefsTable.drop(ks, true).get();
@@ -255,7 +257,7 @@ public class DefsTest extends CleanupHel
{
rm.add(new QueryPath("Standard1", null, "col0".getBytes()), "value0".getBytes(), 1L);
rm.apply();
- assert false : "This mutation should have failed since the CF no longer exists.";
+ throw new AssertionError("This mutation should have failed since the CF no longer exists.");
}
catch (Throwable th)
{
@@ -273,4 +275,78 @@ public class DefsTest extends CleanupHel
assert th instanceof NullPointerException;
}
}
+
+ @Test
+ public void renameKs() throws IOException, ExecutionException, InterruptedException
+ {
+ final KSMetaData oldKs = DatabaseDescriptor.getTableDefinition("Keyspace2");
+ assert oldKs != null;
+ final String cfName = "Standard3";
+ assert oldKs.cfMetaData().containsKey(cfName);
+ assert oldKs.cfMetaData().get(cfName).tableName.equals(oldKs.name);
+
+ // write some data that we hope to read back later.
+ RowMutation rm = new RowMutation(oldKs.name, "renameKs");
+ for (int i = 0; i < 10; i++)
+ rm.add(new QueryPath(cfName, null, ("col" + i).getBytes()), "value".getBytes(), 1L);
+ rm.apply();
+ ColumnFamilyStore store = Table.open(oldKs.name).getColumnFamilyStore(cfName);
+ assert store != null;
+ store.forceBlockingFlush();
+ assert DefsTable.getFiles(oldKs.name, cfName).size() > 0;
+
+ final String newKsName = "RenamedKeyspace2";
+ DefsTable.rename(oldKs, newKsName).get();
+ KSMetaData newKs = DatabaseDescriptor.getTableDefinition(newKsName);
+
+ assert DatabaseDescriptor.getTableDefinition(oldKs.name) == null;
+ assert newKs != null;
+ assert newKs.name.equals(newKsName);
+ assert newKs.cfMetaData().containsKey(cfName);
+ assert newKs.cfMetaData().get(cfName).tableName.equals(newKsName);
+ assert DefsTable.getFiles(newKs.name, cfName).size() > 0;
+ assert DefsTable.getFiles(oldKs.name, cfName).size() == 0;
+
+ // read on old should fail.
+ try
+ {
+ Table.open(oldKs.name);
+ }
+ catch (Throwable th)
+ {
+ assert th instanceof NullPointerException;
+ }
+
+ // write on old should fail.
+ rm = new RowMutation(oldKs.name, "any key will do");
+ try
+ {
+ rm.add(new QueryPath(cfName, null, "col0".getBytes()), "value0".getBytes(), 1L);
+ rm.apply();
+ throw new AssertionError("This mutation should have failed since the CF/Table no longer exists.");
+ }
+ catch (Throwable th)
+ {
+ assert th instanceof IllegalArgumentException;
+ }
+
+ // write on new should work.
+ rm = new RowMutation(newKsName, "renameKs");
+ rm.add(new QueryPath(cfName, null, "col0".getBytes()), "newvalue".getBytes(), 2L);
+ rm.apply();
+ store = Table.open(newKs.name).getColumnFamilyStore(cfName);
+ assert store != null;
+ store.forceBlockingFlush();
+
+ // read on new should work.
+ SortedSet<byte[]> cols = new TreeSet<byte[]>(new BytesType());
+ cols.add("col0".getBytes());
+ cols.add("col1".getBytes());
+ ColumnFamily cfam = store.getColumnFamily(new NamesQueryFilter("renameKs", new QueryPath(cfName), cols));
+ assert cfam.getColumnCount() == cols.size();
+ // tests new write.
+ assert Arrays.equals(cfam.getColumn("col0".getBytes()).value(), "newvalue".getBytes());
+ // tests old write.
+ assert Arrays.equals(cfam.getColumn("col1".getBytes()).value(), "value".getBytes());
+ }
}