add RowCacheTest

patch by Pavel Yaskevich; reviewed by jbellis for CASSANDRA-2173

git-svn-id: https://svn.apache.org/repos/asf/cassandra/branches/cassandra-0.7@1072164 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jonathan Ellis 2011-02-18 21:50:43 +00:00
parent 6c3bf55d02
commit c16a51827b
3 changed files with 138 additions and 1 deletions

View File

@ -190,7 +190,10 @@ public class RowMutation
Table.open(table_).apply(this, getSerializedBuffer(), true);
}
public void applyUnsafe() throws IOException
/**
* Apply without touching the commitlog. For testing.
*/
public void 2applyUnsafe() throws IOException
{
Table.open(table_).apply(this, getSerializedBuffer(), false);
}

View File

@ -185,3 +185,12 @@ keyspaces:
- name: Standard2
keys_cached: 1.0
- name: RowCacheSpace
replica_placement_strategy: org.apache.cassandra.locator.SimpleStrategy
replication_factor: 1
column_families:
- name: CachedCF
rows_cached: 100
- name: CFWithoutCache
rows_cached: 0

View File

@ -0,0 +1,125 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.cassandra.db;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Collection;
import org.apache.cassandra.CleanupHelper;
import org.apache.cassandra.Util;
import org.apache.cassandra.db.filter.QueryPath;
import org.apache.cassandra.utils.ByteBufferUtil;
import org.junit.Test;
public class RowCacheTest extends CleanupHelper
{
@Test
public void testRowCache() throws Exception
{
String KEYSPACE = "RowCacheSpace";
String COLUMN_FAMILY_WITH_CACHE = "CachedCF";
String COLUMN_FAMILY_WITHOUT_CACHE = "CFWithoutCache";
CompactionManager.instance.disableAutoCompaction();
Table table = Table.open(KEYSPACE);
ColumnFamilyStore cachedStore = table.getColumnFamilyStore(COLUMN_FAMILY_WITH_CACHE);
ColumnFamilyStore noCacheStore = table.getColumnFamilyStore(COLUMN_FAMILY_WITHOUT_CACHE);
// inserting 100 rows into both column families
insertData(KEYSPACE, COLUMN_FAMILY_WITH_CACHE, 0, 100);
insertData(KEYSPACE, COLUMN_FAMILY_WITHOUT_CACHE, 0, 100);
// now reading rows one by one and checking if row change grows
for (int i = 0; i < 100; i++)
{
DecoratedKey key = Util.dk("key" + i);
QueryPath path = new QueryPath(COLUMN_FAMILY_WITH_CACHE, null, ByteBufferUtil.bytes("col" + i));
cachedStore.getColumnFamily(key, path, ByteBufferUtil.EMPTY_BYTE_BUFFER, ByteBufferUtil.EMPTY_BYTE_BUFFER, false, 1);
assert cachedStore.getRowCacheSize() == i + 1;
assert cachedStore.getRawCachedRow(key) != null; // current key should be stored in the cache
// checking if column is read correctly after cache
ColumnFamily cf = cachedStore.getColumnFamily(key, path, ByteBufferUtil.EMPTY_BYTE_BUFFER, ByteBufferUtil.EMPTY_BYTE_BUFFER, false, 1);
Collection<IColumn> columns = cf.getSortedColumns();
IColumn column = columns.iterator().next();
assert columns.size() == 1;
assert column.name().equals(ByteBufferUtil.bytes("col" + i));
assert column.value().equals(ByteBufferUtil.bytes("val" + i));
path = new QueryPath(COLUMN_FAMILY_WITHOUT_CACHE, null, ByteBufferUtil.bytes("col" + i));
// row cache should not get populated for the second store
noCacheStore.getColumnFamily(key, path, ByteBufferUtil.EMPTY_BYTE_BUFFER, ByteBufferUtil.EMPTY_BYTE_BUFFER, false, 1);
assert noCacheStore.getRowCacheSize() == 0;
}
// insert 10 more keys and check that row cache is still at store.getRowCacheCapacity()
insertData(KEYSPACE, COLUMN_FAMILY_WITH_CACHE, 100, 10);
for (int i = 100; i < 110; i++)
{
DecoratedKey key = Util.dk("key" + i);
QueryPath path = new QueryPath(COLUMN_FAMILY_WITH_CACHE, null, ByteBufferUtil.bytes("col" + i));
cachedStore.getColumnFamily(key, path, ByteBufferUtil.EMPTY_BYTE_BUFFER, ByteBufferUtil.EMPTY_BYTE_BUFFER, false, 1);
assert cachedStore.getRowCacheSize() == cachedStore.getRowCacheCapacity();
assert cachedStore.getRawCachedRow(key) != null; // cache should be populated with the latest rows read (old ones should be popped)
// checking if column is read correctly after cache
ColumnFamily cf = cachedStore.getColumnFamily(key, path, ByteBufferUtil.EMPTY_BYTE_BUFFER, ByteBufferUtil.EMPTY_BYTE_BUFFER, false, 1);
Collection<IColumn> columns = cf.getSortedColumns();
IColumn column = columns.iterator().next();
assert columns.size() == 1;
assert column.name().equals(ByteBufferUtil.bytes("col" + i));
assert column.value().equals(ByteBufferUtil.bytes("val" + i));
}
// clear all 100 rows from the cache
int keysLeft = 99;
for (int i = 109; i >= 10; i--)
{
cachedStore.invalidateCachedRow(Util.dk("key" + i));
assert cachedStore.getRowCacheSize() == keysLeft;
keysLeft--;
}
}
private void insertData(String keyspace, String columnFamily, int offset, int numberOfRows) throws IOException
{
for (int i = offset; i < offset + numberOfRows; i++)
{
ByteBuffer key = ByteBufferUtil.bytes("key" + i);
RowMutation rowMutation = new RowMutation(keyspace, key);
QueryPath path = new QueryPath(columnFamily, null, ByteBufferUtil.bytes("col" + i));
rowMutation.add(path, ByteBufferUtil.bytes("val" + i), System.currentTimeMillis());
rowMutation.applyUnsafe();
}
}
}