cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbel...@apache.org
Subject svn commit: r804372 - in /incubator/cassandra/trunk: src/java/org/apache/cassandra/db/SystemTable.java src/java/org/apache/cassandra/service/StorageService.java test/unit/org/apache/cassandra/db/SystemTableTest.java
Date Fri, 14 Aug 2009 21:00:18 GMT
Author: jbellis
Date: Fri Aug 14 21:00:18 2009
New Revision: 804372

URL: http://svn.apache.org/viewvc?rev=804372&view=rev
Log:
make StorageTable.StorageMetadata a singleton, so multiple initMetadatas are safe.
patch by Bill de hOra and jbellis; reviewed by Michael Greene for CASSANDRA-358

Modified:
    incubator/cassandra/trunk/src/java/org/apache/cassandra/db/SystemTable.java
    incubator/cassandra/trunk/src/java/org/apache/cassandra/service/StorageService.java
    incubator/cassandra/trunk/test/unit/org/apache/cassandra/db/SystemTableTest.java

Modified: incubator/cassandra/trunk/src/java/org/apache/cassandra/db/SystemTable.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/db/SystemTable.java?rev=804372&r1=804371&r2=804372&view=diff
==============================================================================
--- incubator/cassandra/trunk/src/java/org/apache/cassandra/db/SystemTable.java (original)
+++ incubator/cassandra/trunk/src/java/org/apache/cassandra/db/SystemTable.java Fri Aug 14
21:00:18 2009
@@ -43,6 +43,7 @@
     private static final String LOCATION_KEY = "L"; // only one row in Location CF
     private static final byte[] TOKEN = utf8("Token");
     private static final byte[] GENERATION = utf8("Generation");
+    private static StorageMetadata metadata_;
 
     private static byte[] utf8(String str)
     {
@@ -57,10 +58,11 @@
     }
 
     /*
-     * This method is used to update the SystemTable with the new token.
+     * This method is used to update the SystemTable on disk with the new token.
     */
-    public static void updateToken(Token token) throws IOException
+    public static synchronized void updateToken(Token token) throws IOException
     {
+        assert metadata_ != null;
         IPartitioner p = StorageService.getPartitioner();
         Table table = Table.open(Table.SYSTEM_TABLE);
         /* Retrieve the "LocationInfo" column family */
@@ -76,6 +78,7 @@
         cf.addColumn(tokenColumn);
         rm.add(cf);
         rm.apply();
+        metadata_.setStorageId(token);
     }
 
     /*
@@ -85,8 +88,11 @@
      * columns namely "Token" and "Generation". This is the token that
      * gets gossiped around and the generation info is used for FD.
     */
-    public static StorageMetadata initMetadata() throws IOException
+    public static synchronized StorageMetadata initMetadata() throws IOException
     {
+        if (metadata_ != null)  // guard to protect against being called twice
+            return metadata_;
+
         /* Read the system table to retrieve the storage ID and the generation */
         Table table = Table.open(Table.SYSTEM_TABLE);
         QueryFilter filter = new IdentityQueryFilter(LOCATION_KEY, new QueryPath(LOCATION_CF));
@@ -104,7 +110,8 @@
             cf.addColumn(new Column(GENERATION, BasicUtilities.intToByteArray(generation))
);
             rm.add(cf);
             rm.apply();
-            return new StorageMetadata(token, generation);
+            metadata_ = new StorageMetadata(token, generation);
+            return metadata_;
         }
 
         /* we crashed and came back up need to bump generation # */
@@ -120,7 +127,8 @@
         cf.addColumn(generation2);
         rm.add(cf);
         rm.apply();
-        return new StorageMetadata(token, gen);
+        metadata_ = new StorageMetadata(token, gen);
+        return metadata_;
     }
 
     public static class StorageMetadata

Modified: incubator/cassandra/trunk/src/java/org/apache/cassandra/service/StorageService.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/service/StorageService.java?rev=804372&r1=804371&r2=804372&view=diff
==============================================================================
--- incubator/cassandra/trunk/src/java/org/apache/cassandra/service/StorageService.java (original)
+++ incubator/cassandra/trunk/src/java/org/apache/cassandra/service/StorageService.java Fri
Aug 14 21:00:18 2009
@@ -489,8 +489,6 @@
     {
         /* update the token on disk */
         SystemTable.updateToken(token);
-        /* Update the storageMetadata cache */
-        storageMetadata_.setStorageId(token);
         /* Update the token maps */
         /* Get the old token. This needs to be removed. */
         tokenMetadata_.update(token, StorageService.tcpAddr_);

Modified: incubator/cassandra/trunk/test/unit/org/apache/cassandra/db/SystemTableTest.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/trunk/test/unit/org/apache/cassandra/db/SystemTableTest.java?rev=804372&r1=804371&r2=804372&view=diff
==============================================================================
--- incubator/cassandra/trunk/test/unit/org/apache/cassandra/db/SystemTableTest.java (original)
+++ incubator/cassandra/trunk/test/unit/org/apache/cassandra/db/SystemTableTest.java Fri Aug
14 21:00:18 2009
@@ -21,16 +21,28 @@
 import java.io.IOException;
 
 import org.junit.Test;
+import org.junit.Assert;
 
 import org.apache.cassandra.CleanupHelper;
 import org.apache.cassandra.service.StorageService;
 
 public class SystemTableTest extends CleanupHelper
 {
+
+    @Test
+    public void testOnlyOnceCreationOfStorageMetadata() throws IOException
+    {
+        SystemTable.StorageMetadata storageMetadata1 = SystemTable.initMetadata();
+        SystemTable.StorageMetadata storageMetadata2 = SystemTable.initMetadata();
+        Assert.assertTrue("smd should not change after calling initMetadata twice", storageMetadata1
== storageMetadata2);
+    }
+
     @Test
-    public void testMain() throws IOException
+    public void testTokenGetsUpdated() throws IOException
     {
-        SystemTable.initMetadata();
+        SystemTable.StorageMetadata storageMetadata1 = SystemTable.initMetadata();
         SystemTable.updateToken(StorageService.getPartitioner().getInitialToken("503545744:0"));
+        SystemTable.StorageMetadata storageMetadata2 = SystemTable.initMetadata();
+        Assert.assertTrue("smd should still be a singleton after updateToken", storageMetadata1
== storageMetadata2);
     }
 }



Mime
View raw message