From 94b6471b93cc55dd078158e7eb00416b79e5d034 Mon Sep 17 00:00:00 2001 From: Sylvain Lebresne Date: Fri, 9 Oct 2015 13:50:24 +0200 Subject: [PATCH 1/2] Add Branimir's test from CASSANDRA-10219 --- .../org/apache/cassandra/db/KeyCacheTest.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/test/unit/org/apache/cassandra/db/KeyCacheTest.java b/test/unit/org/apache/cassandra/db/KeyCacheTest.java index 4a4c7d51bf..d3328f1eeb 100644 --- a/test/unit/org/apache/cassandra/db/KeyCacheTest.java +++ b/test/unit/org/apache/cassandra/db/KeyCacheTest.java @@ -17,12 +17,14 @@ */ package org.apache.cassandra.db; +import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; +import com.google.common.collect.ImmutableList; import com.google.common.util.concurrent.Uninterruptibles; import org.junit.AfterClass; import org.junit.Test; @@ -31,6 +33,7 @@ import org.apache.cassandra.SchemaLoader; import org.apache.cassandra.Util; import org.apache.cassandra.cache.KeyCacheKey; import org.apache.cassandra.concurrent.ScheduledExecutors; +import org.apache.cassandra.db.compaction.OperationType; import org.apache.cassandra.db.composites.*; import org.apache.cassandra.db.compaction.CompactionManager; import org.apache.cassandra.db.filter.QueryFilter; @@ -46,6 +49,8 @@ public class KeyCacheTest extends SchemaLoader private static final String KEYSPACE1 = "KeyCacheSpace"; private static final String COLUMN_FAMILY1 = "Standard1"; private static final String COLUMN_FAMILY2 = "Standard2"; + private static final String COLUMN_FAMILY3 = "Standard3"; + @AfterClass public static void cleanup() @@ -103,6 +108,56 @@ public class KeyCacheTest extends SchemaLoader } } + @Test + public void testKeyCacheLoadWithLostTable() throws Exception + { + CompactionManager.instance.disableAutoCompaction(); + + ColumnFamilyStore store = Keyspace.open(KEYSPACE1).getColumnFamilyStore(COLUMN_FAMILY3); + + // empty the cache + CacheService.instance.invalidateKeyCache(); + assertKeyCacheSize(0, KEYSPACE1, COLUMN_FAMILY3); + + // insert data and force to disk + insertData(KEYSPACE1, COLUMN_FAMILY3, 0, 100); + store.forceBlockingFlush(); + + Collection firstFlushTables = ImmutableList.copyOf(store.getSSTables()); + + // populate the cache + readData(KEYSPACE1, COLUMN_FAMILY3, 0, 100); + assertKeyCacheSize(100, KEYSPACE1, COLUMN_FAMILY3); + + // insert some new data and force to disk + insertData(KEYSPACE1, COLUMN_FAMILY3, 100, 50); + store.forceBlockingFlush(); + + // check that it's fine + readData(KEYSPACE1, COLUMN_FAMILY3, 100, 50); + assertKeyCacheSize(150, KEYSPACE1, COLUMN_FAMILY3); + + // force the cache to disk + CacheService.instance.keyCache.submitWrite(Integer.MAX_VALUE).get(); + + CacheService.instance.invalidateKeyCache(); + assertKeyCacheSize(0, KEYSPACE1, COLUMN_FAMILY3); + + // check that the content is written correctly + CacheService.instance.keyCache.loadSaved(); + assertKeyCacheSize(150, KEYSPACE1, COLUMN_FAMILY3); + + CacheService.instance.invalidateKeyCache(); + assertKeyCacheSize(0, KEYSPACE1, COLUMN_FAMILY3); + + // now remove the first sstable from the store to simulate losing the file + store.markObsolete(firstFlushTables, OperationType.UNKNOWN); + + // check that reading now correctly skips over lost table and reads the rest (CASSANDRA-10219) + CacheService.instance.keyCache.loadSaved(); + assertKeyCacheSize(50, KEYSPACE1, COLUMN_FAMILY3); + } + @Test public void testKeyCache() throws ExecutionException, InterruptedException { From 3573faf49e356a0530cac9f0a8ec02297011fe15 Mon Sep 17 00:00:00 2001 From: Sylvain Lebresne Date: Fri, 9 Oct 2015 14:49:34 +0200 Subject: [PATCH 2/2] Merge fixup --- test/unit/org/apache/cassandra/db/KeyCacheTest.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/test/unit/org/apache/cassandra/db/KeyCacheTest.java b/test/unit/org/apache/cassandra/db/KeyCacheTest.java index 9a98bde733..c8caff91d6 100644 --- a/test/unit/org/apache/cassandra/db/KeyCacheTest.java +++ b/test/unit/org/apache/cassandra/db/KeyCacheTest.java @@ -66,7 +66,8 @@ public class KeyCacheTest SimpleStrategy.class, KSMetaData.optsWithRF(1), SchemaLoader.standardCFMD(KEYSPACE1, COLUMN_FAMILY1), - SchemaLoader.standardCFMD(KEYSPACE1, COLUMN_FAMILY2)); + SchemaLoader.standardCFMD(KEYSPACE1, COLUMN_FAMILY2), + SchemaLoader.standardCFMD(KEYSPACE1, COLUMN_FAMILY3)); } @AfterClass @@ -139,21 +140,21 @@ public class KeyCacheTest assertKeyCacheSize(0, KEYSPACE1, COLUMN_FAMILY3); // insert data and force to disk - insertData(KEYSPACE1, COLUMN_FAMILY3, 0, 100); + SchemaLoader.insertData(KEYSPACE1, COLUMN_FAMILY3, 0, 100); store.forceBlockingFlush(); Collection firstFlushTables = ImmutableList.copyOf(store.getSSTables()); // populate the cache - readData(KEYSPACE1, COLUMN_FAMILY3, 0, 100); + SchemaLoader.readData(KEYSPACE1, COLUMN_FAMILY3, 0, 100); assertKeyCacheSize(100, KEYSPACE1, COLUMN_FAMILY3); // insert some new data and force to disk - insertData(KEYSPACE1, COLUMN_FAMILY3, 100, 50); + SchemaLoader.insertData(KEYSPACE1, COLUMN_FAMILY3, 100, 50); store.forceBlockingFlush(); // check that it's fine - readData(KEYSPACE1, COLUMN_FAMILY3, 100, 50); + SchemaLoader.readData(KEYSPACE1, COLUMN_FAMILY3, 100, 50); assertKeyCacheSize(150, KEYSPACE1, COLUMN_FAMILY3); // force the cache to disk