dcapwell commented on code in PR #2256: URL: https://github.com/apache/cassandra/pull/2256#discussion_r1163452061
########## src/java/org/apache/cassandra/service/accord/AccordJournal.java: ########## @@ -0,0 +1,407 @@ +/* + * 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.service.accord; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.*; +import java.util.concurrent.Executor; +import java.util.zip.Checksum; + +import com.google.common.primitives.Ints; + +import accord.local.Node.Id; +import accord.local.PreLoadContext; +import accord.messages.Accept; +import accord.messages.Apply; +import accord.messages.Commit; +import accord.messages.MessageType; +import accord.messages.PreAccept; +import accord.messages.TxnRequest; +import accord.primitives.*; +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.io.IVersionedSerializer; +import org.apache.cassandra.io.util.DataInputPlus; +import org.apache.cassandra.io.util.DataOutputPlus; +import org.apache.cassandra.io.util.File; +import org.apache.cassandra.journal.Journal; +import org.apache.cassandra.journal.KeySupport; +import org.apache.cassandra.journal.Params; +import org.apache.cassandra.journal.ValueSerializer; +import org.apache.cassandra.service.accord.serializers.AcceptSerializers; +import org.apache.cassandra.service.accord.serializers.ApplySerializers; +import org.apache.cassandra.service.accord.serializers.CommitSerializers; +import org.apache.cassandra.service.accord.serializers.EnumSerializer; +import org.apache.cassandra.service.accord.serializers.PreacceptSerializers; + +import static org.apache.cassandra.db.TypeSizes.BYTE_SIZE; +import static org.apache.cassandra.db.TypeSizes.INT_SIZE; +import static org.apache.cassandra.db.TypeSizes.LONG_SIZE; +import static org.apache.cassandra.utils.FBUtilities.updateChecksumInt; +import static org.apache.cassandra.utils.FBUtilities.updateChecksumLong; + +/* + * TODO: expose more journal params via Config + */ +class AccordJournal +{ + private static final Set<Integer> SENTINEL_HOSTS = Collections.singleton(0); + + final File directory; + final Journal<Key, TxnRequest<?>> journal; + + AccordJournal() + { + directory = new File(DatabaseDescriptor.getAccordJournalDirectory()); + journal = new Journal<>("AccordJournal", directory, Params.DEFAULT, Key.SUPPORT, MESSAGE_SERIALIZER); + } + + AccordJournal start() + { + journal.start(); + return this; + } + + void shutdown() + { + journal.shutdown(); + } + + boolean mustAppend(PreLoadContext context) + { + return context instanceof TxnRequest && Type.mustAppend((TxnRequest<?>) context); + } + + void append(int storeId, PreLoadContext context, Executor executor, Runnable onDurable) + { + append(storeId, (TxnRequest<?>) context, executor, onDurable); + } + + void append(int storeId, TxnRequest<?> message, Executor executor, Runnable onDurable) + { + Key key = new Key(message.txnId, Type.fromMsgType(message.type()), storeId); + journal.asyncWrite(key, message, SENTINEL_HOSTS, executor, onDurable); + } + + TxnRequest<?> read(int storeId, TxnId txnId, Type type) + { + Key key = new Key(txnId, type, storeId); + return journal.read(key); + } + + PreAccept readPreAccept(int storeId, TxnId txnId) + { + return (PreAccept) read(storeId, txnId, Type.PREACCEPT_REQ); + } + + Accept readAccept(int storeId, TxnId txnId) + { + return (Accept) read(storeId, txnId, Type.ACCEPT_REQ); + } + + Commit readCommit(int storeId, TxnId txnId) + { + return (Commit) read(storeId, txnId, Type.COMMIT_REQ); + } + + Apply readApply(int storeId, TxnId txnId) + { + return (Apply) read(storeId, txnId, Type.APPLY_REQ); + } + + private static class Key Review Comment: would be good to write tests for this, took a stab at that ``` diff --git a/src/java/org/apache/cassandra/service/accord/AccordJournal.java b/src/java/org/apache/cassandra/service/accord/AccordJournal.java index 5947b0a25e..2cb4fedbeb 100644 --- a/src/java/org/apache/cassandra/service/accord/AccordJournal.java +++ b/src/java/org/apache/cassandra/service/accord/AccordJournal.java @@ -124,7 +124,7 @@ class AccordJournal return (Apply) read(storeId, txnId, Type.APPLY_REQ); } - private static class Key + static class Key { final TxnId txnId; final Type type; @@ -137,6 +137,21 @@ class AccordJournal this.storeId = storeId; } + @Override + public boolean equals(Object o) + { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Key key = (Key) o; + return storeId == key.storeId && Objects.equals(txnId, key.txnId) && type == key.type; + } + + @Override + public int hashCode() + { + return Objects.hash(txnId, type, storeId); + } + /** * Support for (de)serializing and comparing record keys. * <p> @@ -321,7 +336,7 @@ class AccordJournal * 2. It's persisted in the record key, so has the additional constraint of being fixed size and * shouldn't be using varint encoding */ - private enum Type + enum Type { PREACCEPT_REQ (0, MessageType.PREACCEPT_REQ, PreacceptSerializers.request), ACCEPT_REQ (1, MessageType.ACCEPT_REQ, AcceptSerializers.request ), diff --git a/src/java/org/apache/cassandra/utils/FBUtilities.java b/src/java/org/apache/cassandra/utils/FBUtilities.java index faea7c5300..8017ac9000 100644 --- a/src/java/org/apache/cassandra/utils/FBUtilities.java +++ b/src/java/org/apache/cassandra/utils/FBUtilities.java @@ -1193,4 +1193,21 @@ public class FBUtilities } return builder.build(); } + + public enum Order { LT, EQ, GT } + public static <T> Order compare(T a, T b, Comparator<T> comparator) + { + int rc = comparator.compare(a, b); + if (rc < 0) return Order.LT; + if (rc == 0) return Order.EQ; + return Order.GT; + } + + public static <A, B> Order compare(A a, B b, AsymmetricOrdering<A, B> comparator) + { + int rc = comparator.compareAsymmetric(a, b); + if (rc < 0) return Order.LT; + if (rc == 0) return Order.EQ; + return Order.GT; + } } \ No newline at end of file diff --git a/test/unit/org/apache/cassandra/service/accord/AccordJournalTest.java b/test/unit/org/apache/cassandra/service/accord/AccordJournalTest.java index e36e5df516..72c7bf11ae 100644 --- a/test/unit/org/apache/cassandra/service/accord/AccordJournalTest.java +++ b/test/unit/org/apache/cassandra/service/accord/AccordJournalTest.java @@ -1,4 +1,122 @@ -import static org.junit.Assert.*; -public class AccordJournalTest { - +/* + * 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.service.accord; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.junit.Test; + +import accord.primitives.TxnId; +import accord.utils.AccordGens; +import accord.utils.Gen; +import accord.utils.Gens; +import org.apache.cassandra.io.util.DataInputBuffer; +import org.apache.cassandra.io.util.DataOutputBuffer; +import org.apache.cassandra.service.accord.AccordJournal.Key; +import org.apache.cassandra.utils.AsymmetricOrdering; +import org.apache.cassandra.utils.FBUtilities; +import org.apache.cassandra.utils.FBUtilities.Order; +import org.assertj.core.api.Assertions; +import org.checkerframework.checker.nullness.qual.Nullable; + +import static accord.utils.Property.qt; + +public class AccordJournalTest +{ + @Test + public void keySerde() + { + DataOutputBuffer buffer = new DataOutputBuffer(); + qt().forAll(keyGen()).check(key -> { + buffer.clear(); + int expectedSize = Key.SUPPORT.serializedSize(1); + Key.SUPPORT.serialize(key, buffer, 1); + Assertions.assertThat(buffer.getLength()).isEqualTo(expectedSize); + try (DataInputBuffer input = new DataInputBuffer(buffer.unsafeGetBufferAndFlip(), false)) + { + Key read = Key.SUPPORT.deserialize(input, 1); + Assertions.assertThat(read).isEqualTo(key); + } + }); + } + + @Test + public void compare() + { + qt().forAll(Gens.lists(keyGen()).ofSizeBetween(2, 100)).check(keys -> { + Collections.sort(keys, Key.SUPPORT::compare); + List<ByteBuffer> bbs = new ArrayList<>(keys.size()); + for (Key k : keys) + bbs.add(toBB(k)); + + for (int i = 0; i < keys.size(); i++) + { + Key outter = keys.get(i); + for (int j = 0; j < keys.size(); j++) + { + Key inner = keys.get(j); + ByteBuffer bb = bbs.get(j); + Order expected = FBUtilities.compare(outter, inner, Key.SUPPORT::compare); + Order actual = FBUtilities.compare(outter, bb, new AsymmetricOrdering<Key, ByteBuffer>() + { + @Override + public int compareAsymmetric(Key left, ByteBuffer right) + { + return Key.SUPPORT.compareWithKeyAt(left, right, 0, 1); + } + + @Override + public int compare(@Nullable Key key, @Nullable Key t1) + { + throw new UnsupportedOperationException(); + } + }); + Assertions.assertThat(actual).isEqualTo(expected); + } + } + }); + } + + private static ByteBuffer toBB(Key k) + { + try (DataOutputBuffer buffer = new DataOutputBuffer(Key.SUPPORT.serializedSize(1))) + { + Key.SUPPORT.serialize(k, buffer, 1); + return buffer.unsafeGetBufferAndFlip(); + } + catch (IOException e) + { + throw new UncheckedIOException(e); + } + } + + private Gen<Key> keyGen() + { + Gen<TxnId> txnIdGen = AccordGens.txnIds(); + Gen<AccordJournal.Type> typeGen = Gens.enums().all(AccordJournal.Type.class); + Gen.IntGen storeGen = Gens.ints().between(0, 42); + Gen<Key> gen = rs -> new Key(txnIdGen.next(rs), typeGen.next(rs), storeGen.nextInt(rs)); + return gen; + } } \ No newline at end of file ``` -- 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]

