You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by jb...@apache.org on 2012/09/28 17:15:48 UTC

[3/3] git commit: add trace logging for key cache

add trace logging for key cache


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/c710edf0
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/c710edf0
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/c710edf0

Branch: refs/heads/cassandra-1.1
Commit: c710edf07b8bda7c2456fa615ce61a7d490f89a4
Parents: c7ce11f
Author: Jonathan Ellis <jb...@apache.org>
Authored: Fri Sep 28 09:53:05 2012 -0500
Committer: Jonathan Ellis <jb...@apache.org>
Committed: Fri Sep 28 10:12:20 2012 -0500

----------------------------------------------------------------------
 .../org/apache/cassandra/cache/KeyCacheKey.java    |    9 +----
 .../apache/cassandra/io/sstable/SSTableReader.java |   30 ++++++++++++--
 .../apache/cassandra/io/sstable/SSTableWriter.java |    1 +
 3 files changed, 27 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/c710edf0/src/java/org/apache/cassandra/cache/KeyCacheKey.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cache/KeyCacheKey.java b/src/java/org/apache/cassandra/cache/KeyCacheKey.java
index 0c75edb..e515f61 100644
--- a/src/java/org/apache/cassandra/cache/KeyCacheKey.java
+++ b/src/java/org/apache/cassandra/cache/KeyCacheKey.java
@@ -61,14 +61,7 @@ public class KeyCacheKey implements CacheKey
 
     public String toString()
     {
-        try
-        {
-            return String.format("KeyCacheKey(descriptor:%s, key:%s)", desc, ByteBufferUtil.string(ByteBuffer.wrap(key)));
-        }
-        catch (CharacterCodingException e)
-        {
-            throw new AssertionError(e);
-        }
+        return String.format("KeyCacheKey(%s, %s)", desc, ByteBufferUtil.bytesToHex(ByteBuffer.wrap(key)));
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/cassandra/blob/c710edf0/src/java/org/apache/cassandra/io/sstable/SSTableReader.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/io/sstable/SSTableReader.java b/src/java/org/apache/cassandra/io/sstable/SSTableReader.java
index 21dc71d..a4cca37 100644
--- a/src/java/org/apache/cassandra/io/sstable/SSTableReader.java
+++ b/src/java/org/apache/cassandra/io/sstable/SSTableReader.java
@@ -651,7 +651,9 @@ public class SSTableReader extends SSTable
             return;
 
         // avoid keeping a permanent reference to the original key buffer
-        keyCache.put(new KeyCacheKey(descriptor, ByteBufferUtil.clone(key.key)), info);
+        KeyCacheKey cacheKey = new KeyCacheKey(descriptor, ByteBufferUtil.clone(key.key));
+        logger.trace("Adding cache entry for {} -> {}", cacheKey, info);
+        keyCache.put(cacheKey, info);
     }
 
     public Long getCachedPosition(DecoratedKey key, boolean updateStats)
@@ -696,9 +698,13 @@ public class SSTableReader extends SSTable
         if ((op == Operator.EQ || op == Operator.GE) && (key instanceof DecoratedKey))
         {
             DecoratedKey decoratedKey = (DecoratedKey)key;
-            Long cachedPosition = getCachedPosition(new KeyCacheKey(descriptor, decoratedKey.key), updateCacheAndStats);
+            KeyCacheKey cacheKey = new KeyCacheKey(descriptor, decoratedKey.key);
+            Long cachedPosition = getCachedPosition(cacheKey, updateCacheAndStats);
             if (cachedPosition != null)
+            {
+                logger.trace("Cache hit for {} -> {}", cacheKey, cachedPosition);
                 return cachedPosition;
+            }
         }
 
         // next, see if the sampled index says it's impossible for the key to be present
@@ -728,12 +734,26 @@ public class SSTableReader extends SSTable
                     int v = op.apply(comparison);
                     if (v == 0)
                     {
-                        if (comparison == 0 && keyCache != null && keyCache.getCapacity() > 0 && updateCacheAndStats)
+                        if (comparison == 0)
                         {
                             assert key instanceof DecoratedKey; // key can be == to the index key only if it's a true row key
                             DecoratedKey decoratedKey = (DecoratedKey)key;
-                            // store exact match for the key
-                            cacheKey(decoratedKey, dataPosition);
+
+                            if (logger.isTraceEnabled())
+                            {
+                                // expensive sanity check!  see CASSANDRA-4687
+                                FileDataInput fdi = dfile.getSegment(dataPosition);
+                                DecoratedKey keyInDisk = SSTableReader.decodeKey(partitioner, descriptor, ByteBufferUtil.readWithShortLength(fdi));
+                                if (!keyInDisk.equals(key))
+                                    throw new AssertionError(String.format("%s != %s in %s", keyInDisk, key, fdi.getPath()));
+                                fdi.close();
+                            }
+
+                            if (keyCache != null && keyCache.getCapacity() > 0 && updateCacheAndStats)
+                            {
+                                // store exact match for the key
+                                cacheKey(decoratedKey, dataPosition);
+                            }
                         }
                         if (op == Operator.EQ && updateCacheAndStats)
                             bloomFilterTracker.addTruePositive();

http://git-wip-us.apache.org/repos/asf/cassandra/blob/c710edf0/src/java/org/apache/cassandra/io/sstable/SSTableWriter.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/io/sstable/SSTableWriter.java b/src/java/org/apache/cassandra/io/sstable/SSTableWriter.java
index 5619951..5a6ca38 100644
--- a/src/java/org/apache/cassandra/io/sstable/SSTableWriter.java
+++ b/src/java/org/apache/cassandra/io/sstable/SSTableWriter.java
@@ -130,6 +130,7 @@ public class SSTableWriter extends SSTable
     private long beforeAppend(DecoratedKey<?> decoratedKey) throws IOException
     {
         assert decoratedKey != null : "Keys must not be null";
+        assert decoratedKey.key.remaining() > 0 : "Keys must not be empty";
         if (lastWrittenKey != null && lastWrittenKey.compareTo(decoratedKey) >= 0)
             throw new RuntimeException("Last written key " + lastWrittenKey + " >= current key " + decoratedKey + " writing into " + getFilename());
         return (lastWrittenKey == null) ? 0 : dataFile.getFilePointer();