adelapena commented on code in PR #1723: URL: https://github.com/apache/cassandra/pull/1723#discussion_r969849393
########## test/unit/org/apache/cassandra/metrics/TrieMemtableMetricsTest.java: ########## @@ -0,0 +1,215 @@ +/* + * 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.metrics; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.function.Supplier; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Session; +import org.apache.cassandra.SchemaLoader; +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.config.OverrideConfigurationLoader; +import org.apache.cassandra.db.ColumnFamilyStore; +import org.apache.cassandra.dht.Murmur3Partitioner; +import org.apache.cassandra.dht.Range; +import org.apache.cassandra.dht.Splitter; +import org.apache.cassandra.exceptions.ConfigurationException; +import org.apache.cassandra.schema.Schema; +import org.apache.cassandra.service.EmbeddedCassandraService; +import org.apache.cassandra.service.StorageService; +import org.jboss.byteman.contrib.bmunit.BMRule; +import org.jboss.byteman.contrib.bmunit.BMRules; +import org.jboss.byteman.contrib.bmunit.BMUnitRunner; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +@RunWith(BMUnitRunner.class) +public class TrieMemtableMetricsTest extends SchemaLoader +{ + private static final int NUM_SHARDS = 13; + + private static Logger logger = LoggerFactory.getLogger(TrieMemtableMetricsTest.class); + private static Session session; + + private static final String KEYSPACE = "triememtable"; + private static final String TABLE = "metricstest"; + + @BeforeClass + public static void loadSchema() throws ConfigurationException + { + // shadow superclass method; we'll call it directly + // after tinkering with the Config + } + + @BeforeClass + public static void setup() throws ConfigurationException, IOException + { + OverrideConfigurationLoader.override((config) -> { + config.partitioner = "Murmur3Partitioner"; + }); + System.setProperty("cassandra.trie.memtable.shard.count", "" + NUM_SHARDS); + + SchemaLoader.loadSchema(); + + Schema.instance.clear(); + + EmbeddedCassandraService cassandra = new EmbeddedCassandraService(); + cassandra.start(); + + Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").withPort(DatabaseDescriptor.getNativeTransportPort()).build(); + session = cluster.connect(); + + session.execute(String.format("CREATE KEYSPACE IF NOT EXISTS %s WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };", KEYSPACE)); + } + + private ColumnFamilyStore recreateTable() + { + return recreateTable(TABLE); + } + + private ColumnFamilyStore recreateTable(String table) + { + session.execute(String.format("DROP TABLE IF EXISTS %s.%s", KEYSPACE, table)); + session.execute(String.format("CREATE TABLE IF NOT EXISTS %s.%s (id int, val1 text, val2 text, PRIMARY KEY(id, val1)) WITH MEMTABLE = {'class':'TrieMemtable'};", KEYSPACE, table)); + return ColumnFamilyStore.getIfExists(KEYSPACE, table); + } + + @Test + public void testRegularStatementsAreCounted() + { + ColumnFamilyStore cfs = recreateTable(); + TrieMemtableMetricsView metrics = getMemtableMetrics(cfs); + assertEquals(0, metrics.contendedPuts.getCount()); + assertEquals(0, metrics.uncontendedPuts.getCount()); + + for (int i = 0; i < 10; i++) + { + session.execute(String.format("INSERT INTO %s.%s (id, val1, val2) VALUES (%d, '%s', '%s')", KEYSPACE, TABLE, i, "val" + i, "val" + i)); + } + + long allPuts = metrics.contendedPuts.getCount() + metrics.uncontendedPuts.getCount(); + assertEquals(10, allPuts); + } + + @Test + public void testFlushRelatedMetrics() throws IOException, ExecutionException, InterruptedException + { + ColumnFamilyStore cfs = recreateTable(); + TrieMemtableMetricsView metrics = getMemtableMetrics(cfs); + + StorageService.instance.forceKeyspaceFlush(KEYSPACE, TABLE); + assertEquals(0, metrics.contendedPuts.getCount() + metrics.uncontendedPuts.getCount()); + + writeAndFlush(10); + assertEquals(10, metrics.contendedPuts.getCount() + metrics.uncontendedPuts.getCount()); + + // verify that metrics survive flush / memtable switching + writeAndFlush(10); + assertEquals(20, metrics.contendedPuts.getCount() + metrics.uncontendedPuts.getCount()); + assertEquals(metrics.lastFlushShardDataSizes.toString(), NUM_SHARDS, (int) metrics.lastFlushShardDataSizes.numSamplesGauge.getValue()); Review Comment: This assertion fails, `TrieMemtableMetricsView` doesn't override `toString()`. ########## test/unit/org/apache/cassandra/metrics/TrieMemtableMetricsTest.java: ########## @@ -0,0 +1,215 @@ +/* + * 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.metrics; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.function.Supplier; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Session; +import org.apache.cassandra.SchemaLoader; +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.config.OverrideConfigurationLoader; +import org.apache.cassandra.db.ColumnFamilyStore; +import org.apache.cassandra.dht.Murmur3Partitioner; +import org.apache.cassandra.dht.Range; +import org.apache.cassandra.dht.Splitter; +import org.apache.cassandra.exceptions.ConfigurationException; +import org.apache.cassandra.schema.Schema; +import org.apache.cassandra.service.EmbeddedCassandraService; +import org.apache.cassandra.service.StorageService; +import org.jboss.byteman.contrib.bmunit.BMRule; +import org.jboss.byteman.contrib.bmunit.BMRules; +import org.jboss.byteman.contrib.bmunit.BMUnitRunner; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +@RunWith(BMUnitRunner.class) +public class TrieMemtableMetricsTest extends SchemaLoader +{ + private static final int NUM_SHARDS = 13; + + private static Logger logger = LoggerFactory.getLogger(TrieMemtableMetricsTest.class); + private static Session session; + + private static final String KEYSPACE = "triememtable"; + private static final String TABLE = "metricstest"; + + @BeforeClass + public static void loadSchema() throws ConfigurationException + { + // shadow superclass method; we'll call it directly + // after tinkering with the Config + } + + @BeforeClass + public static void setup() throws ConfigurationException, IOException + { + OverrideConfigurationLoader.override((config) -> { + config.partitioner = "Murmur3Partitioner"; + }); + System.setProperty("cassandra.trie.memtable.shard.count", "" + NUM_SHARDS); Review Comment: ```suggestion System.setProperty(TrieMemtable.SHARD_COUNT_PROPERTY, "" + NUM_SHARDS); ``` ########## test/unit/org/apache/cassandra/io/sstable/SSTableMetadataTest.java: ########## @@ -240,9 +242,40 @@ public void trackMaxMinColNames() throws CharacterCodingException { assertEquals(ByteBufferUtil.string(sstable.getSSTableMetadata().minClusteringValues.get(0)), "0col100"); assertEquals(ByteBufferUtil.string(sstable.getSSTableMetadata().maxClusteringValues.get(0)), "9col298"); - // and make sure the clustering values are still minimised after compaction - assertTrue(sstable.getSSTableMetadata().minClusteringValues.get(0).capacity() < 50); - assertTrue(sstable.getSSTableMetadata().maxClusteringValues.get(0).capacity() < 50); + // make sure stats don't reference native or off-heap data + assertBuffersAreRetainable(sstable.getSSTableMetadata().minClusteringValues); + assertBuffersAreRetainable(sstable.getSSTableMetadata().maxClusteringValues); + } + + key = "row3"; + new RowUpdateBuilder(store.metadata(), System.currentTimeMillis(), key) + .addRangeTombstone("0", "7") + .build() + .apply(); + + store.forceBlockingFlush(ColumnFamilyStore.FlushReason.UNIT_TESTS); + store.forceMajorCompaction(); + assertEquals(1, store.getLiveSSTables().size()); + for (SSTableReader sstable : store.getLiveSSTables()) + { + assertEquals(ByteBufferUtil.string(sstable.getSSTableMetadata().minClusteringValues.get(0)), "0"); + assertEquals(ByteBufferUtil.string(sstable.getSSTableMetadata().maxClusteringValues.get(0)), "9col298"); + // make sure stats don't reference native or off-heap data + assertBuffersAreRetainable(sstable.getSSTableMetadata().minClusteringValues); + assertBuffersAreRetainable(sstable.getSSTableMetadata().maxClusteringValues); + } + } + + + public static void assertBuffersAreRetainable(List<ByteBuffer> buffers) + { + for (ByteBuffer b : buffers) + { + assertFalse(b.isDirect()); + assertTrue(b.hasArray()); + assertTrue(b.capacity() == b.remaining()); Review Comment: ```suggestion assertEquals(b.capacity(), b.remaining()); ``` ########## test/unit/org/apache/cassandra/db/tries/MemtableTrieThreadedTest.java: ########## @@ -0,0 +1,176 @@ +/* + * 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.tries; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.cassandra.io.compress.BufferType; +import org.apache.cassandra.utils.bytecomparable.ByteComparable; + +import static org.apache.cassandra.db.tries.MemtableTrieTestBase.VERSION; +import static org.apache.cassandra.db.tries.MemtableTrieTestBase.generateKeys; + +public class MemtableTrieThreadedTest +{ + private static final int COUNT = 300000; + private static final int OTHERS = COUNT / 10; + private static final int PROGRESS_UPDATE = COUNT / 15; + private static final int READERS = 8; + private static final int WALKERS = 2; + Random rand = new Random(); + + static String value(ByteComparable b) + { + return b.byteComparableAsString(VERSION); + } + + @Test + public void testThreaded() throws InterruptedException + { + ByteComparable[] src = generateKeys(rand, COUNT + OTHERS); + MemtableTrie<String> trie = new MemtableTrie<>(BufferType.ON_HEAP); + ConcurrentLinkedQueue<Throwable> errors = new ConcurrentLinkedQueue<>(); + List<Thread> threads = new ArrayList<Thread>(); + AtomicBoolean writeCompleted = new AtomicBoolean(false); + AtomicInteger writeProgress = new AtomicInteger(0); + + for (int i = 0; i < WALKERS; ++i) + threads.add(new Thread() + { + public void run() + { + try + { + Random r = ThreadLocalRandom.current(); + while (!writeCompleted.get()) + { + int min = writeProgress.get(); + int count = 0; + for (Map.Entry<ByteComparable, String> en : trie.entrySet()) + { + String v = value(en.getKey()); + Assert.assertEquals(en.getKey() + .byteComparableAsString( + VERSION), v, en.getValue()); + ++count; + } + Assert.assertTrue("Got only " + count + " while progress is at " + min, count >= min); + } + } + catch (Throwable t) + { + t.printStackTrace(); + errors.add(t); + } + } + }); + + for (int i = 0; i < READERS; ++i) + { + threads.add(new Thread() + { + public void run() + { Review Comment: Could use the more concise `threads.add(new Thread(() -> {` syntax ########## test/unit/org/apache/cassandra/db/tries/MemtableTrieThreadedTest.java: ########## @@ -0,0 +1,176 @@ +/* + * 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.tries; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.cassandra.io.compress.BufferType; +import org.apache.cassandra.utils.bytecomparable.ByteComparable; + +import static org.apache.cassandra.db.tries.MemtableTrieTestBase.VERSION; +import static org.apache.cassandra.db.tries.MemtableTrieTestBase.generateKeys; + +public class MemtableTrieThreadedTest +{ + private static final int COUNT = 300000; + private static final int OTHERS = COUNT / 10; + private static final int PROGRESS_UPDATE = COUNT / 15; + private static final int READERS = 8; + private static final int WALKERS = 2; + Random rand = new Random(); Review Comment: Can be `private final static` ########## test/unit/org/apache/cassandra/db/tries/MemtableTrieThreadedTest.java: ########## @@ -0,0 +1,176 @@ +/* + * 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.tries; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.cassandra.io.compress.BufferType; +import org.apache.cassandra.utils.bytecomparable.ByteComparable; + +import static org.apache.cassandra.db.tries.MemtableTrieTestBase.VERSION; +import static org.apache.cassandra.db.tries.MemtableTrieTestBase.generateKeys; + +public class MemtableTrieThreadedTest +{ + private static final int COUNT = 300000; + private static final int OTHERS = COUNT / 10; + private static final int PROGRESS_UPDATE = COUNT / 15; + private static final int READERS = 8; + private static final int WALKERS = 2; + Random rand = new Random(); + + static String value(ByteComparable b) + { + return b.byteComparableAsString(VERSION); + } + + @Test + public void testThreaded() throws InterruptedException + { + ByteComparable[] src = generateKeys(rand, COUNT + OTHERS); + MemtableTrie<String> trie = new MemtableTrie<>(BufferType.ON_HEAP); + ConcurrentLinkedQueue<Throwable> errors = new ConcurrentLinkedQueue<>(); + List<Thread> threads = new ArrayList<Thread>(); + AtomicBoolean writeCompleted = new AtomicBoolean(false); + AtomicInteger writeProgress = new AtomicInteger(0); + + for (int i = 0; i < WALKERS; ++i) + threads.add(new Thread() + { + public void run() + { + try + { + Random r = ThreadLocalRandom.current(); Review Comment: This random doesn't seem used ########## test/unit/org/apache/cassandra/db/tries/MemtableTrieThreadedTest.java: ########## @@ -0,0 +1,176 @@ +/* + * 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.tries; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.cassandra.io.compress.BufferType; +import org.apache.cassandra.utils.bytecomparable.ByteComparable; + +import static org.apache.cassandra.db.tries.MemtableTrieTestBase.VERSION; +import static org.apache.cassandra.db.tries.MemtableTrieTestBase.generateKeys; + +public class MemtableTrieThreadedTest +{ + private static final int COUNT = 300000; + private static final int OTHERS = COUNT / 10; + private static final int PROGRESS_UPDATE = COUNT / 15; + private static final int READERS = 8; + private static final int WALKERS = 2; + Random rand = new Random(); + + static String value(ByteComparable b) + { + return b.byteComparableAsString(VERSION); + } + + @Test + public void testThreaded() throws InterruptedException + { + ByteComparable[] src = generateKeys(rand, COUNT + OTHERS); + MemtableTrie<String> trie = new MemtableTrie<>(BufferType.ON_HEAP); + ConcurrentLinkedQueue<Throwable> errors = new ConcurrentLinkedQueue<>(); + List<Thread> threads = new ArrayList<Thread>(); + AtomicBoolean writeCompleted = new AtomicBoolean(false); + AtomicInteger writeProgress = new AtomicInteger(0); + + for (int i = 0; i < WALKERS; ++i) + threads.add(new Thread() + { + public void run() + { Review Comment: Could use the more concise `threads.add(new Thread(() -> {` syntax ########## test/unit/org/apache/cassandra/db/tries/MemtableTrieThreadedTest.java: ########## @@ -0,0 +1,176 @@ +/* + * 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.tries; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.cassandra.io.compress.BufferType; +import org.apache.cassandra.utils.bytecomparable.ByteComparable; + +import static org.apache.cassandra.db.tries.MemtableTrieTestBase.VERSION; +import static org.apache.cassandra.db.tries.MemtableTrieTestBase.generateKeys; + +public class MemtableTrieThreadedTest +{ + private static final int COUNT = 300000; + private static final int OTHERS = COUNT / 10; + private static final int PROGRESS_UPDATE = COUNT / 15; + private static final int READERS = 8; + private static final int WALKERS = 2; + Random rand = new Random(); + + static String value(ByteComparable b) + { + return b.byteComparableAsString(VERSION); + } + + @Test + public void testThreaded() throws InterruptedException + { + ByteComparable[] src = generateKeys(rand, COUNT + OTHERS); + MemtableTrie<String> trie = new MemtableTrie<>(BufferType.ON_HEAP); + ConcurrentLinkedQueue<Throwable> errors = new ConcurrentLinkedQueue<>(); + List<Thread> threads = new ArrayList<Thread>(); Review Comment: ```suggestion List<Thread> threads = new ArrayList<>(); ``` ########## test/unit/org/apache/cassandra/io/sstable/SSTableMetadataTest.java: ########## @@ -240,9 +242,40 @@ public void trackMaxMinColNames() throws CharacterCodingException { assertEquals(ByteBufferUtil.string(sstable.getSSTableMetadata().minClusteringValues.get(0)), "0col100"); assertEquals(ByteBufferUtil.string(sstable.getSSTableMetadata().maxClusteringValues.get(0)), "9col298"); - // and make sure the clustering values are still minimised after compaction - assertTrue(sstable.getSSTableMetadata().minClusteringValues.get(0).capacity() < 50); - assertTrue(sstable.getSSTableMetadata().maxClusteringValues.get(0).capacity() < 50); + // make sure stats don't reference native or off-heap data + assertBuffersAreRetainable(sstable.getSSTableMetadata().minClusteringValues); + assertBuffersAreRetainable(sstable.getSSTableMetadata().maxClusteringValues); + } + + key = "row3"; + new RowUpdateBuilder(store.metadata(), System.currentTimeMillis(), key) + .addRangeTombstone("0", "7") + .build() + .apply(); + + store.forceBlockingFlush(ColumnFamilyStore.FlushReason.UNIT_TESTS); + store.forceMajorCompaction(); + assertEquals(1, store.getLiveSSTables().size()); + for (SSTableReader sstable : store.getLiveSSTables()) + { + assertEquals(ByteBufferUtil.string(sstable.getSSTableMetadata().minClusteringValues.get(0)), "0"); + assertEquals(ByteBufferUtil.string(sstable.getSSTableMetadata().maxClusteringValues.get(0)), "9col298"); + // make sure stats don't reference native or off-heap data + assertBuffersAreRetainable(sstable.getSSTableMetadata().minClusteringValues); + assertBuffersAreRetainable(sstable.getSSTableMetadata().maxClusteringValues); + } + } + Review Comment: Double blank line ########## test/unit/org/apache/cassandra/metrics/TrieMemtableMetricsTest.java: ########## @@ -0,0 +1,215 @@ +/* + * 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.metrics; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.function.Supplier; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Session; +import org.apache.cassandra.SchemaLoader; +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.config.OverrideConfigurationLoader; +import org.apache.cassandra.db.ColumnFamilyStore; +import org.apache.cassandra.dht.Murmur3Partitioner; +import org.apache.cassandra.dht.Range; +import org.apache.cassandra.dht.Splitter; +import org.apache.cassandra.exceptions.ConfigurationException; +import org.apache.cassandra.schema.Schema; +import org.apache.cassandra.service.EmbeddedCassandraService; +import org.apache.cassandra.service.StorageService; +import org.jboss.byteman.contrib.bmunit.BMRule; +import org.jboss.byteman.contrib.bmunit.BMRules; +import org.jboss.byteman.contrib.bmunit.BMUnitRunner; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +@RunWith(BMUnitRunner.class) +public class TrieMemtableMetricsTest extends SchemaLoader +{ + private static final int NUM_SHARDS = 13; + + private static Logger logger = LoggerFactory.getLogger(TrieMemtableMetricsTest.class); Review Comment: Can be `final` ########## test/unit/org/apache/cassandra/db/tries/MemtableTrieThreadedTest.java: ########## @@ -0,0 +1,176 @@ +/* + * 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.tries; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.cassandra.io.compress.BufferType; +import org.apache.cassandra.utils.bytecomparable.ByteComparable; + +import static org.apache.cassandra.db.tries.MemtableTrieTestBase.VERSION; +import static org.apache.cassandra.db.tries.MemtableTrieTestBase.generateKeys; + +public class MemtableTrieThreadedTest +{ + private static final int COUNT = 300000; + private static final int OTHERS = COUNT / 10; + private static final int PROGRESS_UPDATE = COUNT / 15; + private static final int READERS = 8; + private static final int WALKERS = 2; + Random rand = new Random(); + + static String value(ByteComparable b) + { + return b.byteComparableAsString(VERSION); + } + + @Test + public void testThreaded() throws InterruptedException + { + ByteComparable[] src = generateKeys(rand, COUNT + OTHERS); + MemtableTrie<String> trie = new MemtableTrie<>(BufferType.ON_HEAP); + ConcurrentLinkedQueue<Throwable> errors = new ConcurrentLinkedQueue<>(); + List<Thread> threads = new ArrayList<Thread>(); + AtomicBoolean writeCompleted = new AtomicBoolean(false); + AtomicInteger writeProgress = new AtomicInteger(0); + + for (int i = 0; i < WALKERS; ++i) + threads.add(new Thread() + { + public void run() + { + try + { + Random r = ThreadLocalRandom.current(); + while (!writeCompleted.get()) + { + int min = writeProgress.get(); + int count = 0; + for (Map.Entry<ByteComparable, String> en : trie.entrySet()) + { + String v = value(en.getKey()); + Assert.assertEquals(en.getKey() + .byteComparableAsString( + VERSION), v, en.getValue()); + ++count; + } + Assert.assertTrue("Got only " + count + " while progress is at " + min, count >= min); + } + } + catch (Throwable t) + { + t.printStackTrace(); + errors.add(t); + } + } + }); + + for (int i = 0; i < READERS; ++i) + { + threads.add(new Thread() + { + public void run() + { + try + { + Random r = ThreadLocalRandom.current(); + while (!writeCompleted.get()) + { + int min = writeProgress.get(); + + for (int i = 0; i < PROGRESS_UPDATE; ++i) + { + int index = r.nextInt(COUNT + OTHERS); + ByteComparable b = src[index]; + String v = value(b); + String result = trie.get(b); + if (result != null) + { + Assert.assertTrue("Got not added " + index + " when COUNT is " + COUNT, + index < COUNT); + Assert.assertEquals("Failed " + index, v, result); + } + else if (index < min) + Assert.fail("Failed index " + index + " while progress is at " + min); + } + } + } + catch (Throwable t) + { + t.printStackTrace(); + errors.add(t); + } + } + }); + } + + threads.add(new Thread() + { + public void run() + { Review Comment: Could use the more concise `threads.add(new Thread(() -> {` syntax ########## test/unit/org/apache/cassandra/db/tries/MemtableTrieThreadedTest.java: ########## @@ -0,0 +1,176 @@ +/* + * 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.tries; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.cassandra.io.compress.BufferType; +import org.apache.cassandra.utils.bytecomparable.ByteComparable; + +import static org.apache.cassandra.db.tries.MemtableTrieTestBase.VERSION; +import static org.apache.cassandra.db.tries.MemtableTrieTestBase.generateKeys; + +public class MemtableTrieThreadedTest +{ + private static final int COUNT = 300000; + private static final int OTHERS = COUNT / 10; + private static final int PROGRESS_UPDATE = COUNT / 15; + private static final int READERS = 8; + private static final int WALKERS = 2; + Random rand = new Random(); + + static String value(ByteComparable b) + { + return b.byteComparableAsString(VERSION); + } + + @Test + public void testThreaded() throws InterruptedException + { + ByteComparable[] src = generateKeys(rand, COUNT + OTHERS); + MemtableTrie<String> trie = new MemtableTrie<>(BufferType.ON_HEAP); + ConcurrentLinkedQueue<Throwable> errors = new ConcurrentLinkedQueue<>(); + List<Thread> threads = new ArrayList<Thread>(); + AtomicBoolean writeCompleted = new AtomicBoolean(false); + AtomicInteger writeProgress = new AtomicInteger(0); + + for (int i = 0; i < WALKERS; ++i) + threads.add(new Thread() + { + public void run() + { + try + { + Random r = ThreadLocalRandom.current(); + while (!writeCompleted.get()) + { + int min = writeProgress.get(); + int count = 0; + for (Map.Entry<ByteComparable, String> en : trie.entrySet()) + { + String v = value(en.getKey()); + Assert.assertEquals(en.getKey() + .byteComparableAsString( + VERSION), v, en.getValue()); Review Comment: No need to break the line ########## test/unit/org/apache/cassandra/metrics/TrieMemtableMetricsTest.java: ########## @@ -0,0 +1,215 @@ +/* + * 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.metrics; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.function.Supplier; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Session; +import org.apache.cassandra.SchemaLoader; +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.config.OverrideConfigurationLoader; +import org.apache.cassandra.db.ColumnFamilyStore; +import org.apache.cassandra.dht.Murmur3Partitioner; +import org.apache.cassandra.dht.Range; +import org.apache.cassandra.dht.Splitter; +import org.apache.cassandra.exceptions.ConfigurationException; +import org.apache.cassandra.schema.Schema; +import org.apache.cassandra.service.EmbeddedCassandraService; +import org.apache.cassandra.service.StorageService; +import org.jboss.byteman.contrib.bmunit.BMRule; +import org.jboss.byteman.contrib.bmunit.BMRules; +import org.jboss.byteman.contrib.bmunit.BMUnitRunner; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +@RunWith(BMUnitRunner.class) +public class TrieMemtableMetricsTest extends SchemaLoader +{ + private static final int NUM_SHARDS = 13; + + private static Logger logger = LoggerFactory.getLogger(TrieMemtableMetricsTest.class); + private static Session session; + + private static final String KEYSPACE = "triememtable"; + private static final String TABLE = "metricstest"; + + @BeforeClass + public static void loadSchema() throws ConfigurationException + { + // shadow superclass method; we'll call it directly + // after tinkering with the Config + } + + @BeforeClass + public static void setup() throws ConfigurationException, IOException + { + OverrideConfigurationLoader.override((config) -> { + config.partitioner = "Murmur3Partitioner"; + }); + System.setProperty("cassandra.trie.memtable.shard.count", "" + NUM_SHARDS); + + SchemaLoader.loadSchema(); + + Schema.instance.clear(); + + EmbeddedCassandraService cassandra = new EmbeddedCassandraService(); + cassandra.start(); + + Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").withPort(DatabaseDescriptor.getNativeTransportPort()).build(); + session = cluster.connect(); + + session.execute(String.format("CREATE KEYSPACE IF NOT EXISTS %s WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };", KEYSPACE)); + } + + private ColumnFamilyStore recreateTable() + { + return recreateTable(TABLE); + } + + private ColumnFamilyStore recreateTable(String table) + { + session.execute(String.format("DROP TABLE IF EXISTS %s.%s", KEYSPACE, table)); + session.execute(String.format("CREATE TABLE IF NOT EXISTS %s.%s (id int, val1 text, val2 text, PRIMARY KEY(id, val1)) WITH MEMTABLE = {'class':'TrieMemtable'};", KEYSPACE, table)); Review Comment: It seems that there is a syntax error here: ```suggestion session.execute(String.format("CREATE TABLE IF NOT EXISTS %s.%s " + "(id int, val1 text, val2 text, PRIMARY KEY(id, val1)) " + "WITH MEMTABLE = 'trie'", KEYSPACE, table)); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

