dcapwell commented on code in PR #2256: URL: https://github.com/apache/cassandra/pull/2256#discussion_r1175807243
########## test/unit/org/apache/cassandra/journal/IndexTest.java: ########## @@ -0,0 +1,263 @@ +/* + * 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.journal; + +import java.io.IOException; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Maps; +import org.junit.Test; + +import org.agrona.collections.IntHashSet; +import org.apache.cassandra.io.util.File; +import org.apache.cassandra.utils.Generators; +import org.apache.cassandra.utils.TimeUUID; +import org.quicktheories.core.Gen; +import org.quicktheories.impl.Constraint; + +import static org.apache.cassandra.utils.TimeUUID.Generator.nextTimeUUID; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.quicktheories.QuickTheory.qt; + +public class IndexTest +{ + private static final int[] EMPTY = new int[0]; + + @Test + public void testInMemoryIndexBasics() + { + InMemoryIndex<TimeUUID> index = InMemoryIndex.create(TimeUUIDKeySupport.INSTANCE); + + TimeUUID key0 = nextTimeUUID(); + TimeUUID key1 = nextTimeUUID(); + TimeUUID key2 = nextTimeUUID(); + TimeUUID key3 = nextTimeUUID(); + TimeUUID key4 = nextTimeUUID(); + + assertArrayEquals(EMPTY, index.lookUp(key0)); + assertArrayEquals(EMPTY, index.lookUp(key1)); + assertArrayEquals(EMPTY, index.lookUp(key2)); + assertArrayEquals(EMPTY, index.lookUp(key3)); + assertArrayEquals(EMPTY, index.lookUp(key4)); + + int val11 = 1100; + int val21 = 2100; + int val22 = 2200; + int val31 = 3100; + int val32 = 3200; + int val33 = 3300; + + index.update(key1, val11); + index.update(key2, val21); + index.update(key2, val22); + index.update(key3, val31); + index.update(key3, val32); + index.update(key3, val33); + + assertArrayEquals(EMPTY, index.lookUp(key0)); + assertArrayEquals(new int[] { val11 }, index.lookUp(key1)); + assertArrayEquals(new int[] { val21, val22 }, index.lookUp(key2)); + assertArrayEquals(new int[] { val31, val32, val33 }, index.lookUp(key3)); + assertArrayEquals(EMPTY, index.lookUp(key4)); + + assertEquals(key1, index.firstId()); + assertEquals(key3, index.lastId()); + + assertFalse(index.mayContainId(key0)); + assertTrue(index.mayContainId(key1)); + assertTrue(index.mayContainId(key2)); + assertTrue(index.mayContainId(key3)); + assertFalse(index.mayContainId(key4)); + } + + @Test + public void testInMemoryIndexPersists() throws IOException + { + InMemoryIndex<TimeUUID> inMemory = InMemoryIndex.create(TimeUUIDKeySupport.INSTANCE); + + TimeUUID key0 = nextTimeUUID(); + TimeUUID key1 = nextTimeUUID(); + TimeUUID key2 = nextTimeUUID(); + TimeUUID key3 = nextTimeUUID(); + TimeUUID key4 = nextTimeUUID(); + + int val11 = 1100; + int val21 = 2100; + int val22 = 2200; + int val31 = 3100; + int val32 = 3200; + int val33 = 3300; + + inMemory.update(key1, val11); + inMemory.update(key2, val21); + inMemory.update(key2, val22); + inMemory.update(key3, val31); + inMemory.update(key3, val32); + inMemory.update(key3, val33); + + File directory = new File(Files.createTempDirectory(null)); + directory.deleteOnExit(); + Descriptor descriptor = Descriptor.create(directory, System.currentTimeMillis(), 1); + inMemory.persist(descriptor); + + try (OnDiskIndex<TimeUUID> onDisk = OnDiskIndex.open(descriptor, TimeUUIDKeySupport.INSTANCE)) + { + assertArrayEquals(EMPTY, onDisk.lookUp(key0)); + assertArrayEquals(new int[] { val11 }, onDisk.lookUp(key1)); + assertArrayEquals(new int[] { val21, val22 }, onDisk.lookUp(key2)); + assertArrayEquals(new int[] { val31, val32, val33 }, onDisk.lookUp(key3)); + assertArrayEquals(EMPTY, onDisk.lookUp(key4)); + + assertEquals(key1, onDisk.firstId()); + assertEquals(key3, onDisk.lastId()); + + assertFalse(onDisk.mayContainId(key0)); + assertTrue(onDisk.mayContainId(key1)); + assertTrue(onDisk.mayContainId(key2)); + assertTrue(onDisk.mayContainId(key3)); + assertFalse(onDisk.mayContainId(key4)); + } + } + + @Test + public void prop() throws IOException + { + Constraint sizeConstraint = Constraint.between(1, 10); + Constraint valueSizeConstraint = Constraint.between(0, 10); + Constraint positionConstraint = Constraint.between(0, Integer.MAX_VALUE); + Gen<TimeUUID> keyGen = Generators.timeUUID(); + Gen<int[]> valueGen = rs -> { + int[] array = new int[(int) rs.next(valueSizeConstraint)]; + IntHashSet uniq = new IntHashSet(); + for (int i = 0; i < array.length; i++) + { + int value = (int) rs.next(positionConstraint); + while (!uniq.add(value)) + value = (int) rs.next(positionConstraint); + array[i] = value; + } + return array; + }; + Gen<Map<TimeUUID, int[]>> gen = rs -> { + int size = (int) rs.next(sizeConstraint); + Map<TimeUUID, int[]> map = Maps.newHashMapWithExpectedSize(size); + for (int i = 0; i < size; i++) + { + TimeUUID key = keyGen.generate(rs); + while (map.containsKey(key)) + key = keyGen.generate(rs); + int[] value = valueGen.generate(rs); + map.put(key, value); + } + return map; + }; + gen = gen.describedAs(map -> { + StringBuilder sb = new StringBuilder(); + for (Map.Entry<TimeUUID, int[]> entry : map.entrySet()) + sb.append('\n').append(entry.getKey()).append('\t').append(Arrays.toString(entry.getValue())); + return sb.toString(); + }); + File directory = new File(Files.createTempDirectory(null)); + directory.deleteOnExit(); + qt().withFixedSeed(1735898489413583L).forAll(gen).checkAssert(map -> test(directory, map)); Review Comment: should remove `.withFixedSeed(1735898489413583L)` ########## test/unit/org/apache/cassandra/journal/IndexTest.java: ########## @@ -0,0 +1,263 @@ +/* + * 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.journal; + +import java.io.IOException; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Maps; +import org.junit.Test; + +import org.agrona.collections.IntHashSet; +import org.apache.cassandra.io.util.File; +import org.apache.cassandra.utils.Generators; +import org.apache.cassandra.utils.TimeUUID; +import org.quicktheories.core.Gen; +import org.quicktheories.impl.Constraint; + +import static org.apache.cassandra.utils.TimeUUID.Generator.nextTimeUUID; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.quicktheories.QuickTheory.qt; + +public class IndexTest +{ + private static final int[] EMPTY = new int[0]; + + @Test + public void testInMemoryIndexBasics() + { + InMemoryIndex<TimeUUID> index = InMemoryIndex.create(TimeUUIDKeySupport.INSTANCE); + + TimeUUID key0 = nextTimeUUID(); + TimeUUID key1 = nextTimeUUID(); + TimeUUID key2 = nextTimeUUID(); + TimeUUID key3 = nextTimeUUID(); + TimeUUID key4 = nextTimeUUID(); + + assertArrayEquals(EMPTY, index.lookUp(key0)); + assertArrayEquals(EMPTY, index.lookUp(key1)); + assertArrayEquals(EMPTY, index.lookUp(key2)); + assertArrayEquals(EMPTY, index.lookUp(key3)); + assertArrayEquals(EMPTY, index.lookUp(key4)); + + int val11 = 1100; + int val21 = 2100; + int val22 = 2200; + int val31 = 3100; + int val32 = 3200; + int val33 = 3300; + + index.update(key1, val11); + index.update(key2, val21); + index.update(key2, val22); + index.update(key3, val31); + index.update(key3, val32); + index.update(key3, val33); + + assertArrayEquals(EMPTY, index.lookUp(key0)); + assertArrayEquals(new int[] { val11 }, index.lookUp(key1)); + assertArrayEquals(new int[] { val21, val22 }, index.lookUp(key2)); + assertArrayEquals(new int[] { val31, val32, val33 }, index.lookUp(key3)); + assertArrayEquals(EMPTY, index.lookUp(key4)); + + assertEquals(key1, index.firstId()); + assertEquals(key3, index.lastId()); + + assertFalse(index.mayContainId(key0)); + assertTrue(index.mayContainId(key1)); + assertTrue(index.mayContainId(key2)); + assertTrue(index.mayContainId(key3)); + assertFalse(index.mayContainId(key4)); + } + + @Test + public void testInMemoryIndexPersists() throws IOException + { + InMemoryIndex<TimeUUID> inMemory = InMemoryIndex.create(TimeUUIDKeySupport.INSTANCE); + + TimeUUID key0 = nextTimeUUID(); + TimeUUID key1 = nextTimeUUID(); + TimeUUID key2 = nextTimeUUID(); + TimeUUID key3 = nextTimeUUID(); + TimeUUID key4 = nextTimeUUID(); + + int val11 = 1100; + int val21 = 2100; + int val22 = 2200; + int val31 = 3100; + int val32 = 3200; + int val33 = 3300; + + inMemory.update(key1, val11); + inMemory.update(key2, val21); + inMemory.update(key2, val22); + inMemory.update(key3, val31); + inMemory.update(key3, val32); + inMemory.update(key3, val33); + + File directory = new File(Files.createTempDirectory(null)); + directory.deleteOnExit(); + Descriptor descriptor = Descriptor.create(directory, System.currentTimeMillis(), 1); + inMemory.persist(descriptor); + + try (OnDiskIndex<TimeUUID> onDisk = OnDiskIndex.open(descriptor, TimeUUIDKeySupport.INSTANCE)) + { + assertArrayEquals(EMPTY, onDisk.lookUp(key0)); + assertArrayEquals(new int[] { val11 }, onDisk.lookUp(key1)); + assertArrayEquals(new int[] { val21, val22 }, onDisk.lookUp(key2)); + assertArrayEquals(new int[] { val31, val32, val33 }, onDisk.lookUp(key3)); + assertArrayEquals(EMPTY, onDisk.lookUp(key4)); + + assertEquals(key1, onDisk.firstId()); + assertEquals(key3, onDisk.lastId()); + + assertFalse(onDisk.mayContainId(key0)); + assertTrue(onDisk.mayContainId(key1)); + assertTrue(onDisk.mayContainId(key2)); + assertTrue(onDisk.mayContainId(key3)); + assertFalse(onDisk.mayContainId(key4)); + } + } + + @Test + public void prop() throws IOException + { + Constraint sizeConstraint = Constraint.between(1, 10); + Constraint valueSizeConstraint = Constraint.between(0, 10); + Constraint positionConstraint = Constraint.between(0, Integer.MAX_VALUE); + Gen<TimeUUID> keyGen = Generators.timeUUID(); + Gen<int[]> valueGen = rs -> { + int[] array = new int[(int) rs.next(valueSizeConstraint)]; + IntHashSet uniq = new IntHashSet(); + for (int i = 0; i < array.length; i++) + { + int value = (int) rs.next(positionConstraint); + while (!uniq.add(value)) + value = (int) rs.next(positionConstraint); + array[i] = value; + } + return array; + }; + Gen<Map<TimeUUID, int[]>> gen = rs -> { + int size = (int) rs.next(sizeConstraint); + Map<TimeUUID, int[]> map = Maps.newHashMapWithExpectedSize(size); + for (int i = 0; i < size; i++) + { + TimeUUID key = keyGen.generate(rs); + while (map.containsKey(key)) + key = keyGen.generate(rs); + int[] value = valueGen.generate(rs); + map.put(key, value); + } + return map; + }; + gen = gen.describedAs(map -> { + StringBuilder sb = new StringBuilder(); + for (Map.Entry<TimeUUID, int[]> entry : map.entrySet()) + sb.append('\n').append(entry.getKey()).append('\t').append(Arrays.toString(entry.getValue())); + return sb.toString(); + }); + File directory = new File(Files.createTempDirectory(null)); + directory.deleteOnExit(); + qt().withFixedSeed(1735898489413583L).forAll(gen).checkAssert(map -> test(directory, map)); + } + + @Test + public void sample() throws IOException + { + File directory = new File(Files.createTempDirectory(null)); + directory.deleteOnExit(); + + test(directory, ImmutableMap.of(TimeUUID.fromString("58ed800a-d357-11b2-0000-000000000000"), new int[]{ 0 }, + TimeUUID.fromString("8001c17e-d357-11b2-0000-000000000000"), new int[]{ 0 } + )); + } Review Comment: test isn't needed, it was there to repo a bug found by `prop` ########## test/unit/org/apache/cassandra/journal/IndexTest.java: ########## @@ -0,0 +1,263 @@ +/* + * 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.journal; + +import java.io.IOException; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Maps; +import org.junit.Test; + +import org.agrona.collections.IntHashSet; +import org.apache.cassandra.io.util.File; +import org.apache.cassandra.utils.Generators; +import org.apache.cassandra.utils.TimeUUID; +import org.quicktheories.core.Gen; +import org.quicktheories.impl.Constraint; + +import static org.apache.cassandra.utils.TimeUUID.Generator.nextTimeUUID; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.quicktheories.QuickTheory.qt; + +public class IndexTest +{ + private static final int[] EMPTY = new int[0]; + + @Test + public void testInMemoryIndexBasics() + { + InMemoryIndex<TimeUUID> index = InMemoryIndex.create(TimeUUIDKeySupport.INSTANCE); + + TimeUUID key0 = nextTimeUUID(); + TimeUUID key1 = nextTimeUUID(); + TimeUUID key2 = nextTimeUUID(); + TimeUUID key3 = nextTimeUUID(); + TimeUUID key4 = nextTimeUUID(); + + assertArrayEquals(EMPTY, index.lookUp(key0)); + assertArrayEquals(EMPTY, index.lookUp(key1)); + assertArrayEquals(EMPTY, index.lookUp(key2)); + assertArrayEquals(EMPTY, index.lookUp(key3)); + assertArrayEquals(EMPTY, index.lookUp(key4)); + + int val11 = 1100; + int val21 = 2100; + int val22 = 2200; + int val31 = 3100; + int val32 = 3200; + int val33 = 3300; + + index.update(key1, val11); + index.update(key2, val21); + index.update(key2, val22); + index.update(key3, val31); + index.update(key3, val32); + index.update(key3, val33); + + assertArrayEquals(EMPTY, index.lookUp(key0)); + assertArrayEquals(new int[] { val11 }, index.lookUp(key1)); + assertArrayEquals(new int[] { val21, val22 }, index.lookUp(key2)); + assertArrayEquals(new int[] { val31, val32, val33 }, index.lookUp(key3)); + assertArrayEquals(EMPTY, index.lookUp(key4)); + + assertEquals(key1, index.firstId()); + assertEquals(key3, index.lastId()); + + assertFalse(index.mayContainId(key0)); + assertTrue(index.mayContainId(key1)); + assertTrue(index.mayContainId(key2)); + assertTrue(index.mayContainId(key3)); + assertFalse(index.mayContainId(key4)); + } + + @Test + public void testInMemoryIndexPersists() throws IOException Review Comment: this test is a more specific example of `prop`, ok to leave but is a duplicate now ########## test/unit/org/apache/cassandra/journal/IndexTest.java: ########## @@ -0,0 +1,263 @@ +/* + * 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.journal; + +import java.io.IOException; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Maps; +import org.junit.Test; + +import org.agrona.collections.IntHashSet; +import org.apache.cassandra.io.util.File; +import org.apache.cassandra.utils.Generators; +import org.apache.cassandra.utils.TimeUUID; +import org.quicktheories.core.Gen; +import org.quicktheories.impl.Constraint; + +import static org.apache.cassandra.utils.TimeUUID.Generator.nextTimeUUID; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.quicktheories.QuickTheory.qt; + +public class IndexTest +{ + private static final int[] EMPTY = new int[0]; + + @Test + public void testInMemoryIndexBasics() Review Comment: this test is a more specific example of `prop`, ok to leave but is a duplicate now ########## src/java/org/apache/cassandra/journal/Flusher.java: ########## @@ -259,7 +259,7 @@ private void asyncFlushBatch(ActiveSegment<K>.Allocation alloc, Executor executo alloc.awaitFlush(journal.metrics.waitingOnFlush); pending.decrementAndGet(); written.incrementAndGet(); - executor.execute(onDurable); + executor.execute(callback); Review Comment: rather than `AsyncWriteCallback extends Runnable` it might be better to do `callback::onSuccess` ########## src/java/org/apache/cassandra/journal/Flusher.java: ########## @@ -270,15 +270,15 @@ private void waitForFlushGroup(ActiveSegment<K>.Allocation alloc) pending.decrementAndGet(); } - private void asyncFlushGroup(ActiveSegment<K>.Allocation alloc, Executor executor, Runnable onDurable) + private void asyncFlushGroup(ActiveSegment<K>.Allocation alloc, Executor executor, AsyncWriteCallback callback) { pending.incrementAndGet(); callbackExecutor.execute(() -> { alloc.awaitFlush(journal.metrics.waitingOnFlush); pending.decrementAndGet(); written.incrementAndGet(); - executor.execute(onDurable); + executor.execute(callback); Review Comment: rather than `AsyncWriteCallback extends Runnable` it might be better to do `callback::onSuccess` ########## src/java/org/apache/cassandra/service/accord/async/AsyncAppender.java: ########## @@ -69,12 +72,23 @@ public boolean append() return state == State.FINISHED; } - @Override - public void run() + private void onFinished(@Nullable Throwable error) { commandStore.checkInStoreThread(); Invariants.checkState(state == State.WAITING, "Expected WAITING state but was %s", state); state = State.FINISHED; - callback.accept(null, null); + callback.accept(error, null); Review Comment: shouldn't this be `null, error`? ########## src/java/org/apache/cassandra/journal/Flusher.java: ########## @@ -305,7 +305,7 @@ private void asyncFlushPeriodic(ActiveSegment<K>.Allocation ignore, Executor exe pending.decrementAndGet(); } written.incrementAndGet(); - executor.execute(onDurable); + executor.execute(callback); Review Comment: rather than `AsyncWriteCallback extends Runnable` it might be better to do `callback::onSuccess` -- 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]

