Search in sources :

Example 1 with AutoSavingCache

use of org.apache.cassandra.cache.AutoSavingCache in project cassandra by apache.

the class KeyCacheTest method testKeyCacheLoadCacheLoadTimeExceedingLimit.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testKeyCacheLoadCacheLoadTimeExceedingLimit() throws Exception {
    DatabaseDescriptor.setCacheLoadTimeout(2);
    int delayMillis = 1000;
    int numberOfRows = 100;
    String cf = COLUMN_FAMILY9;
    createAndInvalidateCache(Collections.singletonList(Pair.create(KEYSPACE1, cf)), numberOfRows);
    // Testing cache load. Here using custom built AutoSavingCache instance as simulating delay is not possible with
    // 'CacheService.instance.keyCache'. 'AutoSavingCache.loadSaved()' is returning no.of entries loaded so we don't need
    // to instantiate ICache.class.
    CacheService.KeyCacheSerializer keyCacheSerializer = new CacheService.KeyCacheSerializer();
    CacheService.KeyCacheSerializer keyCacheSerializerSpy = Mockito.spy(keyCacheSerializer);
    AutoSavingCache autoSavingCache = new AutoSavingCache(mock(ICache.class), CacheService.CacheType.KEY_CACHE, keyCacheSerializerSpy);
    doAnswer(new AnswersWithDelay(delayMillis, answer -> keyCacheSerializer.deserialize(answer.getArgument(0), answer.getArgument(1)))).when(keyCacheSerializerSpy).deserialize(any(DataInputPlus.class), any(ColumnFamilyStore.class));
    long maxExpectedKeyCache = Math.min(numberOfRows, 1 + TimeUnit.SECONDS.toMillis(DatabaseDescriptor.getCacheLoadTimeout()) / delayMillis);
    long keysLoaded = autoSavingCache.loadSaved();
    assertThat(keysLoaded, Matchers.lessThanOrEqualTo(maxExpectedKeyCache));
    assertNotEquals(0, keysLoaded);
    Mockito.verify(keyCacheSerializerSpy, Mockito.times(1)).cleanupAfterDeserialize();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CompactionManager(org.apache.cassandra.db.compaction.CompactionManager) BeforeClass(org.junit.BeforeClass) KeyCacheKey(org.apache.cassandra.cache.KeyCacheKey) HashMap(java.util.HashMap) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) ImmutableList(com.google.common.collect.ImmutableList) ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) OperationType(org.apache.cassandra.db.compaction.OperationType) Pair(org.apache.cassandra.utils.Pair) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ICache(org.apache.cassandra.cache.ICache) CacheService(org.apache.cassandra.service.CacheService) Refs(org.apache.cassandra.utils.concurrent.Refs) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) AfterClass(org.junit.AfterClass) Iterator(java.util.Iterator) Collection(java.util.Collection) Util(org.apache.cassandra.Util) KeyspaceParams(org.apache.cassandra.schema.KeyspaceParams) Set(java.util.Set) AutoSavingCache(org.apache.cassandra.cache.AutoSavingCache) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) SchemaLoader(org.apache.cassandra.SchemaLoader) Mockito(org.mockito.Mockito) List(java.util.List) LifecycleTransaction(org.apache.cassandra.db.lifecycle.LifecycleTransaction) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) AnswersWithDelay(org.mockito.internal.stubbing.answers.AnswersWithDelay) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) AnswersWithDelay(org.mockito.internal.stubbing.answers.AnswersWithDelay) ICache(org.apache.cassandra.cache.ICache) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) AutoSavingCache(org.apache.cassandra.cache.AutoSavingCache) CacheService(org.apache.cassandra.service.CacheService) Test(org.junit.Test)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 SchemaLoader (org.apache.cassandra.SchemaLoader)1 Util (org.apache.cassandra.Util)1 AutoSavingCache (org.apache.cassandra.cache.AutoSavingCache)1 ICache (org.apache.cassandra.cache.ICache)1 KeyCacheKey (org.apache.cassandra.cache.KeyCacheKey)1 DatabaseDescriptor (org.apache.cassandra.config.DatabaseDescriptor)1 CompactionManager (org.apache.cassandra.db.compaction.CompactionManager)1 OperationType (org.apache.cassandra.db.compaction.OperationType)1