cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbel...@apache.org
Subject svn commit: r902868 - /incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/db/Table.java
Date Mon, 25 Jan 2010 16:44:45 GMT
Author: jbellis
Date: Mon Jan 25 16:44:45 2010
New Revision: 902868

URL: http://svn.apache.org/viewvc?rev=902868&view=rev
Log:
fix potential race in Table.open.  patch by jbellis; reviewed by Jeff Hodges for CASSANDRA-734

Modified:
    incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/db/Table.java

Modified: incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/db/Table.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/db/Table.java?rev=902868&r1=902867&r2=902868&view=diff
==============================================================================
--- incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/db/Table.java
(original)
+++ incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/db/Table.java
Mon Jan 25 16:44:45 2010
@@ -33,6 +33,7 @@
 import java.net.InetAddress;
 import org.apache.cassandra.utils.*;
 import org.apache.cassandra.db.filter.*;
+import org.cliffc.high_scale_lib.NonBlockingHashMap;
 
 import org.apache.log4j.Logger;
 
@@ -163,9 +164,8 @@
         }
     }
     
-    /* Used to lock the factory for creation of Table instance */
-    private static final Lock createLock_ = new ReentrantLock();
-    private static final Map<String, Table> instances_ = new HashMap<String, Table>();
+    /** Table objects, one per keyspace.  only one instance should ever exist for any given
keyspace. */
+    private static final Map<String, Table> instances_ = new NonBlockingHashMap<String,
Table>();
     /* Table name. */
     private final String table_;
     /* Handle to the Table Metadata */
@@ -178,26 +178,19 @@
     public static Table open(String table) throws IOException
     {
         Table tableInstance = instances_.get(table);
-        /*
-         * Read the config and figure the column families for this table.
-         * Set the isConfigured flag so that we do not read config all the
-         * time.
-        */
         if (tableInstance == null)
         {
-            Table.createLock_.lock();
-            try
+            // instantiate the Table.  we could use putIfAbsent but it's important to making
sure it is only done once
+            // per keyspace, so we synchronize and re-check before doing it.
+            synchronized (Table.class)
             {
+                tableInstance = instances_.get(table);
                 if (tableInstance == null)
                 {
                     tableInstance = new Table(table);
                     instances_.put(table, tableInstance);
                 }
             }
-            finally
-            {
-                createLock_.unlock();
-            }
         }
         return tableInstance;
     }



Mime
View raw message