maedhroz commented on code in PR #2267: URL: https://github.com/apache/cassandra/pull/2267#discussion_r1185535689
########## test/unit/org/apache/cassandra/io/sstable/format/bti/PartitionIndexTest.java: ########## @@ -0,0 +1,934 @@ +/* + * 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.io.sstable.format.bti; + +import java.io.IOException; +import java.nio.channels.FileChannel; +import java.nio.file.Files; +import java.nio.file.StandardOpenOption; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.Random; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Supplier; + +import com.google.common.collect.HashMultiset; +import com.google.common.collect.Lists; +import com.google.common.collect.Multiset; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.cassandra.Util; +import org.apache.cassandra.cache.ChunkCache; +import org.apache.cassandra.config.Config; +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.db.DecoratedKey; +import org.apache.cassandra.dht.ByteOrderedPartitioner; +import org.apache.cassandra.dht.IPartitioner; +import org.apache.cassandra.dht.RandomPartitioner; +import org.apache.cassandra.io.tries.TrieNode; +import org.apache.cassandra.io.tries.Walker; +import org.apache.cassandra.io.util.File; +import org.apache.cassandra.io.util.FileHandle; +import org.apache.cassandra.io.util.FileUtils; +import org.apache.cassandra.io.util.PageAware; +import org.apache.cassandra.io.util.Rebufferer; +import org.apache.cassandra.io.util.SequentialWriter; +import org.apache.cassandra.io.util.SequentialWriterOption; +import org.apache.cassandra.io.util.WrappingRebufferer; +import org.apache.cassandra.utils.ByteBufferUtil; +import org.apache.cassandra.utils.Pair; +import org.apache.cassandra.utils.bytecomparable.ByteComparable; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +@RunWith(Parameterized.class) +public class PartitionIndexTest +{ + private final static Logger logger = LoggerFactory.getLogger(PartitionIndexTest.class); + + private final static long SEED = System.nanoTime(); + private final static Random random = new Random(SEED); + + static final ByteComparable.Version VERSION = Walker.BYTE_COMPARABLE_VERSION; + + static + { + DatabaseDescriptor.daemonInitialization(); + } + + IPartitioner partitioner = Util.testPartitioner(); + //Lower the size of the indexes when running without the chunk cache, otherwise the test times out on Jenkins + static final int COUNT = ChunkCache.instance != null ? 245256 : 24525; + + @Parameterized.Parameters() + public static Collection<Object[]> generateData() + { + return Arrays.asList(new Object[]{ Config.DiskAccessMode.standard }, + new Object[]{ Config.DiskAccessMode.mmap }); + } + + @Parameterized.Parameter(value = 0) + public static Config.DiskAccessMode accessMode = Config.DiskAccessMode.standard; + + public static void beforeClass() + { + logger.info("Using random seed: {}", SEED); + } + + /** + * Tests last-nodes-sizing failure uncovered during code review. + */ + @Test + public void testSizingBug() throws IOException, InterruptedException + { + for (int i = 1; i < COUNT; i *= 10) + { + testGetEq(generateRandomIndex(i)); + testGetEq(generateSequentialIndex(i)); + } + } + + @Test + public void testGetEq() throws IOException, InterruptedException + { + testGetEq(generateRandomIndex(COUNT)); + testGetEq(generateSequentialIndex(COUNT)); + } + + @Test + public void testBrokenFile() throws IOException, InterruptedException + { + // put some garbage in the file + final Pair<List<DecoratedKey>, PartitionIndex> data = generateRandomIndex(COUNT); + File f = new File(data.right.getFileHandle().path()); + try (FileChannel ch = FileChannel.open(f.toPath(), StandardOpenOption.WRITE)) + { + ch.write(generateRandomKey().getKey(), f.length() * 2 / 3); + } + + boolean thrown = false; + try + { + testGetEq(data); + } + catch (Throwable e) + { + thrown = true; + } + assertTrue(thrown); Review Comment: nit: Could replace all this w/ ``` assertThatThrownBy(() -> testGetEq(data)).isInstanceOfAny(AssertionError.class, IndexOutOfBoundsException.class); ``` -- 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]

