This is an automated email from the ASF dual-hosted git repository. maedhroz pushed a commit to branch cep-15-accord in repository https://gitbox.apache.org/repos/asf/cassandra.git
commit 85b4b460603164744f41b9a7d2ae757d88753934 Author: Caleb Rackliffe <calebrackli...@gmail.com> AuthorDate: Fri Sep 29 16:33:09 2023 -0500 ninja-fix: minor post-TCM-rebase cleanup --- modules/accord | 2 +- src/java/org/apache/cassandra/net/Verb.java | 20 +- .../service/accord/AccordTopologyUtils.java | 8 +- .../test/log/ClusterMetadataTestHelper.java | 1 + test/unit/accord/utils/DefaultRandom.java | 94 ---- test/unit/accord/utils/Gen.java | 179 ------- test/unit/accord/utils/Gens.java | 576 --------------------- test/unit/accord/utils/Invariants.java | 327 ------------ test/unit/accord/utils/Property.java | 382 -------------- test/unit/accord/utils/RandomSource.java | 361 ------------- test/unit/accord/utils/WrappedRandomSource.java | 97 ---- .../concurrent/SimulatedExecutorFactory.java | 2 +- test/unit/org/apache/cassandra/cql3/CQLTester.java | 1 - .../org/apache/cassandra/repair/FuzzTestBase.java | 18 +- .../service/accord/AccordSyncPropagatorTest.java | 2 +- .../service/accord/AccordTopologyTest.java | 3 +- 16 files changed, 29 insertions(+), 2044 deletions(-) diff --git a/modules/accord b/modules/accord index 79fc1ebf7d..b1befa3cc0 160000 --- a/modules/accord +++ b/modules/accord @@ -1 +1 @@ -Subproject commit 79fc1ebf7db6aa5e616dbef1bc61b616fea3c2c6 +Subproject commit b1befa3cc0a8496451bb48ec3bb1c0f56b8c7653 diff --git a/src/java/org/apache/cassandra/net/Verb.java b/src/java/org/apache/cassandra/net/Verb.java index c38ac68d1f..747f91ef40 100644 --- a/src/java/org/apache/cassandra/net/Verb.java +++ b/src/java/org/apache/cassandra/net/Verb.java @@ -130,7 +130,6 @@ import org.apache.cassandra.utils.TimeUUID; import org.apache.cassandra.utils.UUIDSerializer; import static java.util.concurrent.TimeUnit.NANOSECONDS; -import static org.apache.cassandra.net.ResponseHandlerSupplier.RESPONSE_HANDLER; import static org.apache.cassandra.concurrent.Stage.ANTI_ENTROPY; import static org.apache.cassandra.concurrent.Stage.COUNTER_MUTATION; import static org.apache.cassandra.concurrent.Stage.FETCH_LOG; @@ -145,6 +144,7 @@ import static org.apache.cassandra.concurrent.Stage.PAXOS_REPAIR; import static org.apache.cassandra.concurrent.Stage.READ; import static org.apache.cassandra.concurrent.Stage.REQUEST_RESPONSE; import static org.apache.cassandra.concurrent.Stage.TRACING; +import static org.apache.cassandra.net.ResponseHandlerSupplier.RESPONSE_HANDLER; import static org.apache.cassandra.net.Verb.Kind.CUSTOM; import static org.apache.cassandra.net.Verb.Kind.NORMAL; import static org.apache.cassandra.net.Verb.Priority.P0; @@ -229,7 +229,7 @@ public enum Verb // repair; mostly doesn't use callbacks and sends responses as their own request messages, with matching sessions by uuid; should eventually harmonize and make idiomatic // for the repair messages that implement retry logic, use rpcTimeout so the single request fails faster, then retries can be used to recover - REPAIR_RSP (100, P1, repairTimeout, REQUEST_RESPONSE, () -> NoPayload.serializer, () -> ResponseVerbHandler.instance ), + REPAIR_RSP (100, P1, repairTimeout, REQUEST_RESPONSE, () -> NoPayload.serializer, RESPONSE_HANDLER ), VALIDATION_RSP (102, P1, repairValidationRspTimeout, ANTI_ENTROPY, () -> ValidationResponse.serializer, () -> RepairMessageVerbHandler.instance(), REPAIR_RSP ), VALIDATION_REQ (101, P1, repairWithBackoffTimeout, ANTI_ENTROPY, () -> ValidationRequest.serializer, () -> RepairMessageVerbHandler.instance(), REPAIR_RSP ), SYNC_RSP (104, P1, repairWithBackoffTimeout, ANTI_ENTROPY, () -> SyncResponse.serializer, () -> RepairMessageVerbHandler.instance(), REPAIR_RSP ), @@ -274,25 +274,25 @@ public enum Verb PAXOS2_CLEANUP_COMPLETE_REQ (48, P2, repairTimeout, PAXOS_REPAIR, () -> PaxosCleanupComplete.serializer, () -> PaxosCleanupComplete.verbHandler, PAXOS2_CLEANUP_COMPLETE_RSP ), // transactional cluster metadata - TCM_COMMIT_RSP (801, P1, rpcTimeout, INTERNAL_METADATA, MessageSerializers::commitResultSerializer, () -> ResponseVerbHandler.instance ), + TCM_COMMIT_RSP (801, P1, rpcTimeout, INTERNAL_METADATA, MessageSerializers::commitResultSerializer, RESPONSE_HANDLER ), TCM_COMMIT_REQ (802, P1, rpcTimeout, INTERNAL_METADATA, MessageSerializers::commitSerializer, () -> commitRequestHandler(), TCM_COMMIT_RSP ), - TCM_FETCH_CMS_LOG_RSP (803, P1, rpcTimeout, FETCH_LOG, MessageSerializers::logStateSerializer, () -> ResponseVerbHandler.instance ), + TCM_FETCH_CMS_LOG_RSP (803, P1, rpcTimeout, FETCH_LOG, MessageSerializers::logStateSerializer, RESPONSE_HANDLER ), TCM_FETCH_CMS_LOG_REQ (804, P1, rpcTimeout, FETCH_LOG, () -> FetchCMSLog.serializer, () -> fetchLogRequestHandler(), TCM_FETCH_CMS_LOG_RSP ), TCM_REPLICATION (805, P1, rpcTimeout, INTERNAL_METADATA, MessageSerializers::replicationSerializer, () -> replicationHandler() ), - TCM_NOTIFY_RSP (806, P1, rpcTimeout, INTERNAL_METADATA, () -> Epoch.messageSerializer, () -> ResponseVerbHandler.instance ), + TCM_NOTIFY_RSP (806, P1, rpcTimeout, INTERNAL_METADATA, () -> Epoch.messageSerializer, RESPONSE_HANDLER ), TCM_NOTIFY_REQ (807, P1, rpcTimeout, INTERNAL_METADATA, MessageSerializers::logStateSerializer, () -> logNotifyHandler(), TCM_NOTIFY_RSP ), TCM_CURRENT_EPOCH_REQ (808, P1, rpcTimeout, INTERNAL_METADATA, () -> Epoch.messageSerializer, () -> currentEpochRequestHandler(), TCM_NOTIFY_RSP ), - TCM_INIT_MIG_RSP (809, P1, rpcTimeout, INTERNAL_METADATA, MessageSerializers::metadataHolderSerializer, () -> ResponseVerbHandler.instance ), + TCM_INIT_MIG_RSP (809, P1, rpcTimeout, INTERNAL_METADATA, MessageSerializers::metadataHolderSerializer, RESPONSE_HANDLER ), TCM_INIT_MIG_REQ (810, P1, rpcTimeout, INTERNAL_METADATA, () -> Election.Initiator.serializer, () -> Election.instance.prepareHandler, TCM_INIT_MIG_RSP ), TCM_ABORT_MIG (811, P1, rpcTimeout, INTERNAL_METADATA, () -> Election.Initiator.serializer, () -> Election.instance.abortHandler, TCM_INIT_MIG_RSP ), - TCM_DISCOVER_RSP (812, P1, rpcTimeout, INTERNAL_METADATA, () -> Discovery.serializer, () -> ResponseVerbHandler.instance ), + TCM_DISCOVER_RSP (812, P1, rpcTimeout, INTERNAL_METADATA, () -> Discovery.serializer, RESPONSE_HANDLER ), TCM_DISCOVER_REQ (813, P1, rpcTimeout, INTERNAL_METADATA, () -> NoPayload.serializer, () -> Discovery.instance.requestHandler, TCM_DISCOVER_RSP ), - TCM_FETCH_PEER_LOG_RSP (818, P1, rpcTimeout, FETCH_LOG, MessageSerializers::logStateSerializer, () -> ResponseVerbHandler.instance ), + TCM_FETCH_PEER_LOG_RSP (818, P1, rpcTimeout, FETCH_LOG, MessageSerializers::logStateSerializer, RESPONSE_HANDLER ), TCM_FETCH_PEER_LOG_REQ (819, P1, rpcTimeout, FETCH_LOG, () -> FetchPeerLog.serializer, () -> FetchPeerLog.Handler.instance, TCM_FETCH_PEER_LOG_RSP ), - INITIATE_DATA_MOVEMENTS_RSP (814, P1, rpcTimeout, MISC, () -> NoPayload.serializer, () -> ResponseVerbHandler.instance ), + INITIATE_DATA_MOVEMENTS_RSP (814, P1, rpcTimeout, MISC, () -> NoPayload.serializer, RESPONSE_HANDLER ), INITIATE_DATA_MOVEMENTS_REQ (815, P1, rpcTimeout, MISC, () -> DataMovement.serializer, () -> DataMovementVerbHandler.instance, INITIATE_DATA_MOVEMENTS_RSP ), - DATA_MOVEMENT_EXECUTED_RSP (816, P1, rpcTimeout, MISC, () -> NoPayload.serializer, () -> ResponseVerbHandler.instance ), + DATA_MOVEMENT_EXECUTED_RSP (816, P1, rpcTimeout, MISC, () -> NoPayload.serializer, RESPONSE_HANDLER ), DATA_MOVEMENT_EXECUTED_REQ (817, P1, rpcTimeout, MISC, () -> DataMovement.Status.serializer, () -> DataMovements.instance, DATA_MOVEMENT_EXECUTED_RSP ), // accord diff --git a/src/java/org/apache/cassandra/service/accord/AccordTopologyUtils.java b/src/java/org/apache/cassandra/service/accord/AccordTopologyUtils.java index 19f0e628ff..d385cf5d31 100644 --- a/src/java/org/apache/cassandra/service/accord/AccordTopologyUtils.java +++ b/src/java/org/apache/cassandra/service/accord/AccordTopologyUtils.java @@ -35,7 +35,6 @@ import accord.topology.Topology; import accord.utils.Invariants; import org.apache.cassandra.dht.Range; import org.apache.cassandra.dht.Token; -import org.apache.cassandra.locator.EndpointsForRange; import org.apache.cassandra.locator.InetAddressAndPort; import org.apache.cassandra.schema.DistributedSchema; import org.apache.cassandra.schema.KeyspaceMetadata; @@ -48,6 +47,7 @@ import org.apache.cassandra.tcm.membership.Directory; import org.apache.cassandra.tcm.membership.NodeId; import org.apache.cassandra.tcm.ownership.DataPlacement; import org.apache.cassandra.tcm.ownership.DataPlacements; +import org.apache.cassandra.tcm.ownership.VersionedEndpoints; public class AccordTopologyUtils { @@ -56,7 +56,7 @@ public class AccordTopologyUtils return new Node.Id(nodeId.id()); } - private static Shard createShard(TokenRange range, Directory directory, EndpointsForRange reads, EndpointsForRange writes) + private static Shard createShard(TokenRange range, Directory directory, VersionedEndpoints.ForRange reads, VersionedEndpoints.ForRange writes) { Function<InetAddressAndPort, Node.Id> endpointMapper = e -> { NodeId tcmId = directory.peerId(e); @@ -106,8 +106,8 @@ public class AccordTopologyUtils List<Shard> shards = new ArrayList<>(ranges.size()); for (Range<Token> range : ranges) { - EndpointsForRange reads = placement.reads.forRange(range); - EndpointsForRange writes = placement.reads.forRange(range); + VersionedEndpoints.ForRange reads = placement.reads.forRange(range); + VersionedEndpoints.ForRange writes = placement.reads.forRange(range); // TCM doesn't create wrap around ranges Invariants.checkArgument(!range.isWrapAround() || range.right.equals(range.right.minValue()), diff --git a/test/distributed/org/apache/cassandra/distributed/test/log/ClusterMetadataTestHelper.java b/test/distributed/org/apache/cassandra/distributed/test/log/ClusterMetadataTestHelper.java index 4ca3e16f03..40a5200f12 100644 --- a/test/distributed/org/apache/cassandra/distributed/test/log/ClusterMetadataTestHelper.java +++ b/test/distributed/org/apache/cassandra/distributed/test/log/ClusterMetadataTestHelper.java @@ -181,6 +181,7 @@ public class ClusterMetadataTestHelper null, null, DataPlacements.empty(), + AccordKeyspaces.EMPTY, null, null, ImmutableMap.of()); diff --git a/test/unit/accord/utils/DefaultRandom.java b/test/unit/accord/utils/DefaultRandom.java deleted file mode 100644 index b16f1f8bbf..0000000000 --- a/test/unit/accord/utils/DefaultRandom.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * 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 accord.utils; - -import java.util.Random; - -public class DefaultRandom implements RandomSource -{ - private final Random delegate; - public DefaultRandom() - { - this.delegate = new Random(); - } - - public DefaultRandom(long seed) - { - this.delegate = new Random(seed); - } - - @Override - public void nextBytes(byte[] bytes) - { - delegate.nextBytes(bytes); - } - - @Override - public boolean nextBoolean() - { - return delegate.nextBoolean(); - } - - @Override - public int nextInt() - { - return delegate.nextInt(); - } - - @Override - public long nextLong() - { - return delegate.nextLong(); - } - - @Override - public float nextFloat() - { - return delegate.nextFloat(); - } - - @Override - public double nextDouble() - { - return delegate.nextDouble(); - } - - @Override - public double nextGaussian() - { - return delegate.nextGaussian(); - } - - @Override - public void setSeed(long seed) - { - delegate.setSeed(seed); - } - - @Override - public DefaultRandom fork() { - return new DefaultRandom(nextLong()); - } - - @Override - public Random asJdkRandom() - { - return delegate; - } -} diff --git a/test/unit/accord/utils/Gen.java b/test/unit/accord/utils/Gen.java deleted file mode 100644 index 7563e8c29b..0000000000 --- a/test/unit/accord/utils/Gen.java +++ /dev/null @@ -1,179 +0,0 @@ -/* - * 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 accord.utils; - -import java.util.function.BiFunction; -import java.util.function.Function; -import java.util.function.IntPredicate; -import java.util.function.IntSupplier; -import java.util.function.LongPredicate; -import java.util.function.LongSupplier; -import java.util.function.Predicate; -import java.util.function.Supplier; -import java.util.function.ToIntFunction; -import java.util.function.ToLongFunction; -import java.util.stream.IntStream; -import java.util.stream.LongStream; -import java.util.stream.Stream; - -public interface Gen<A> { - /** - * For cases where method handles isn't able to detect the proper type, this method acts as a cast - * to inform the compiler of the desired type. - */ - static <A> Gen<A> of(Gen<A> fn) - { - return fn; - } - - A next(RandomSource random); - - default <B> Gen<B> map(Function<? super A, ? extends B> fn) - { - return r -> fn.apply(this.next(r)); - } - - default <B> Gen<B> map(BiFunction<RandomSource, ? super A, ? extends B> fn) - { - return r -> fn.apply(r, this.next(r)); - } - - default IntGen mapToInt(ToIntFunction<A> fn) - { - return r -> fn.applyAsInt(next(r)); - } - - default LongGen mapToLong(ToLongFunction<A> fn) - { - return r -> fn.applyAsLong(next(r)); - } - - default <B> Gen<B> flatMap(Function<? super A, Gen<? extends B>> mapper) - { - return rs -> mapper.apply(this.next(rs)).next(rs); - } - - default <B> Gen<B> flatMap(BiFunction<RandomSource, ? super A, Gen<? extends B>> mapper) - { - return rs -> mapper.apply(rs, this.next(rs)).next(rs); - } - - default Gen<A> filter(Predicate<A> fn) - { - Gen<A> self = this; - return r -> { - A value; - do { - value = self.next(r); - } - while (!fn.test(value)); - return value; - }; - } - - default Supplier<A> asSupplier(RandomSource rs) - { - return () -> next(rs); - } - - default Stream<A> asStream(RandomSource rs) - { - return Stream.generate(() -> next(rs)); - } - - interface IntGen extends Gen<Integer> - { - int nextInt(RandomSource random); - - @Override - default Integer next(RandomSource random) - { - return nextInt(random); - } - - default Gen.IntGen filterInt(IntPredicate fn) - { - return rs -> { - int value; - do - { - value = nextInt(rs); - } - while (!fn.test(value)); - return value; - }; - } - - @Override - default Gen.IntGen filter(Predicate<Integer> fn) - { - return filterInt(i -> fn.test(i)); - } - - default IntSupplier asIntSupplier(RandomSource rs) - { - return () -> nextInt(rs); - } - - default IntStream asIntStream(RandomSource rs) - { - return IntStream.generate(() -> nextInt(rs)); - } - } - - interface LongGen extends Gen<Long> - { - long nextLong(RandomSource random); - - @Override - default Long next(RandomSource random) - { - return nextLong(random); - } - - default Gen.LongGen filterLong(LongPredicate fn) - { - return rs -> { - long value; - do - { - value = nextLong(rs); - } - while (!fn.test(value)); - return value; - }; - } - - @Override - default Gen.LongGen filter(Predicate<Long> fn) - { - return filterLong(i -> fn.test(i)); - } - - default LongSupplier asLongSupplier(RandomSource rs) - { - return () -> nextLong(rs); - } - - default LongStream asLongStream(RandomSource rs) - { - return LongStream.generate(() -> nextLong(rs)); - } - } -} diff --git a/test/unit/accord/utils/Gens.java b/test/unit/accord/utils/Gens.java deleted file mode 100644 index 4a80d724af..0000000000 --- a/test/unit/accord/utils/Gens.java +++ /dev/null @@ -1,576 +0,0 @@ -/* - * 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 accord.utils; - -import java.lang.reflect.Array; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; -import java.util.EnumMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.NavigableSet; -import java.util.Objects; -import java.util.Set; -import java.util.function.Function; -import java.util.function.Supplier; -import java.util.stream.Collectors; - - -public class Gens { - private Gens() { - } - - public static <T> Gen<T> constant(T constant) - { - return ignore -> constant; - } - - public static <T> Gen<T> constant(Supplier<T> constant) - { - return ignore -> constant.get(); - } - - public static <T> Gen<T> pick(T... ts) - { - return pick(Arrays.asList(ts)); - } - - public static <T> Gen<T> pick(List<T> ts) - { - Gen.IntGen offset = ints().between(0, ts.size() - 1); - return rs -> ts.get(offset.nextInt(rs)); - } - - public static <T extends Comparable<T>> Gen<T> pick(Set<T> set) - { - List<T> list = new ArrayList<>(set); - // Non-ordered sets may have different iteration order on different environments, which would make a seed produce different histories! - // To avoid such a problem, make sure to apply a deterministic function (sort). - if (!(set instanceof NavigableSet)) - list.sort(Comparator.naturalOrder()); - return pick(list); - } - - public static <T> Gen<T> pick(Map<T, Integer> values) - { - if (values == null || values.isEmpty()) - throw new IllegalArgumentException("values is empty"); - double totalWeight = values.values().stream().mapToDouble(Integer::intValue).sum(); - List<Weight<T>> list = values.entrySet().stream().map(e -> new Weight<>(e.getKey(), e.getValue())).collect(Collectors.toList()); - Collections.sort(list); - return rs -> { - double value = rs.nextDouble() * totalWeight; - for (Weight<T> w : list) - { - value -= w.weight; - if (value <= 0) - return w.value; - } - return list.get(list.size() - 1).value; - }; - } - - public static Gen<char[]> charArray(Gen.IntGen sizes, char[] domain) - { - return charArray(sizes, domain, (a, b) -> true); - } - - public interface IntCharBiPredicate - { - boolean test(int a, char b); - } - - public static Gen<char[]> charArray(Gen.IntGen sizes, char[] domain, IntCharBiPredicate fn) - { - Gen.IntGen indexGen = ints().between(0, domain.length - 1); - return rs -> { - int size = sizes.nextInt(rs); - char[] is = new char[size]; - for (int i = 0; i != size; i++) - { - char c; - do - { - c = domain[indexGen.nextInt(rs)]; - } - while (!fn.test(i, c)); - is[i] = c; - } - return is; - }; - } - - public static Gen<RandomSource> random() { - return r -> r; - } - - public static BooleanDSL bools() - { - return new BooleanDSL(); - } - - public static IntDSL ints() - { - return new IntDSL(); - } - - public static LongDSL longs() { - return new LongDSL(); - } - - public static <T> ListDSL<T> lists(Gen<T> fn) { - return new ListDSL<>(fn); - } - - public static <T> ArrayDSL<T> arrays(Class<T> type, Gen<T> fn) { - return new ArrayDSL<>(type, fn); - } - - public static IntArrayDSL arrays(Gen.IntGen fn) { - return new IntArrayDSL(fn); - } - - public static LongArrayDSL arrays(Gen.LongGen fn) { - return new LongArrayDSL(fn); - } - - public static EnumDSL enums() - { - return new EnumDSL(); - } - - public static StringDSL strings() - { - return new StringDSL(); - } - - public static class BooleanDSL - { - public Gen<Boolean> all() - { - return RandomSource::nextBoolean; - } - - public Gen<Boolean> runs(double ratio, int maxRuns) - { - Invariants.checkArgument(ratio > 0 && ratio <= 1, "Expected %d to be larger than 0 and <= 1", ratio); - double lower = ratio * .8; - double upper = ratio * 1.2; - return new Gen<>() { - // run represents how many consecutaive true values should be returned; -1 implies no active "run" exists - private int run = -1; - private long falseCount = 0, trueCount = 0; - @Override - public Boolean next(RandomSource rs) - { - if (run != -1) - { - run--; - trueCount++; - return true; - } - double currentRatio = trueCount / (double) (falseCount + trueCount); - if (currentRatio < lower) - { - // not enough true - trueCount++; - return true; - } - if (currentRatio > upper) - { - // not enough false - falseCount++; - return false; - } - if (rs.decide(ratio)) - { - run = rs.nextInt(maxRuns); - run--; - trueCount++; - return true; - } - falseCount++; - return false; - } - }; - } - } - - public static class IntDSL - { - public Gen.IntGen of(int value) - { - return r -> value; - } - - public Gen.IntGen all() - { - return RandomSource::nextInt; - } - - public Gen.IntGen between(int min, int max) - { - Invariants.checkArgument(max >= min, "max (%d) < min (%d)", max, min); - if (min == max) - return of(min); - // since bounds is exclusive, if max == max_value unable to do +1 to include... so will return a gen - // that does not include - if (max == Integer.MAX_VALUE) - return r -> r.nextInt(min, max); - return r -> r.nextInt(min, max + 1); - } - } - - public static class LongDSL { - public Gen.LongGen of(long value) - { - return r -> value; - } - - public Gen.LongGen all() { - return RandomSource::nextLong; - } - - public Gen.LongGen between(long min, long max) { - Invariants.checkArgument(max >= min); - if (min == max) - return of(min); - // since bounds is exclusive, if max == max_value unable to do +1 to include... so will return a gen - // that does not include - if (max == Long.MAX_VALUE) - return r -> r.nextLong(min, max); - return r -> r.nextLong(min, max + 1); - } - } - - public static class EnumDSL - { - public <T extends Enum<T>> Gen<T> all(Class<T> klass) - { - return pick(klass.getEnumConstants()); - } - - public <T extends Enum<T>> Gen<T> allWithWeights(Class<T> klass, int... weights) - { - T[] constants = klass.getEnumConstants(); - if (constants.length != weights.length) - throw new IllegalArgumentException(String.format("Total number of weights (%s) does not match the enum (%s)", Arrays.toString(weights), Arrays.toString(constants))); - Map<T, Integer> values = new EnumMap<>(klass); - for (int i = 0; i < constants.length; i++) - values.put(constants[i], weights[i]); - return pick(values); - } - } - - public static class StringDSL - { - public Gen<String> of(Gen.IntGen sizes, char[] domain) - { - // note, map is overloaded so String::new is ambugious to javac, so need a lambda here - return charArray(sizes, domain).map(c -> new String(c)); - } - - public SizeBuilder<String> of(char[] domain) - { - return new SizeBuilder<>(sizes -> of(sizes, domain)); - } - - public Gen<String> of(Gen.IntGen sizes, char[] domain, IntCharBiPredicate fn) - { - // note, map is overloaded so String::new is ambugious to javac, so need a lambda here - return charArray(sizes, domain, fn).map(c -> new String(c)); - } - - public SizeBuilder<String> of(char[] domain, IntCharBiPredicate fn) - { - return new SizeBuilder<>(sizes -> of(sizes, domain, fn)); - } - - public Gen<String> all(Gen.IntGen sizes) - { - return betweenCodePoints(sizes, Character.MIN_CODE_POINT, Character.MAX_CODE_POINT); - } - - public SizeBuilder<String> all() - { - return new SizeBuilder<>(this::all); - } - - public Gen<String> ascii(Gen.IntGen sizes) - { - return betweenCodePoints(sizes, 0, 127); - } - - public SizeBuilder<String> ascii() - { - return new SizeBuilder<>(this::ascii); - } - - public Gen<String> betweenCodePoints(Gen.IntGen sizes, int min, int max) - { - Gen.IntGen codePointGen = ints().between(min, max).filter(Character::isDefined); - return rs -> { - int[] array = new int[sizes.nextInt(rs)]; - for (int i = 0; i < array.length; i++) - array[i] = codePointGen.nextInt(rs); - return new String(array, 0, array.length); - }; - } - - public SizeBuilder<String> betweenCodePoints(int min, int max) - { - return new SizeBuilder<>(sizes -> betweenCodePoints(sizes, min, max)); - } - } - - public static class SizeBuilder<T> - { - private final Function<Gen.IntGen, Gen<T>> fn; - - public SizeBuilder(Function<Gen.IntGen, Gen<T>> fn) - { - this.fn = fn; - } - - public Gen<T> ofLength(int fixed) - { - return ofLengthBetween(fixed, fixed); - } - - public Gen<T> ofLengthBetween(int min, int max) - { - return fn.apply(ints().between(min, max)); - } - } - - public static class ListDSL<T> implements BaseSequenceDSL<ListDSL<T>, List<T>> { - private final Gen<T> fn; - - public ListDSL(Gen<T> fn) { - this.fn = Objects.requireNonNull(fn); - } - - @Override - public ListDSL<T> unique() - { - return new ListDSL<>(new GenReset<>(fn)); - } - - @Override - public Gen<List<T>> ofSizeBetween(int minSize, int maxSize) { - Gen.IntGen sizeGen = ints().between(minSize, maxSize); - return r -> - { - Reset.tryReset(fn); - int size = sizeGen.nextInt(r); - List<T> list = new ArrayList<>(size); - for (int i = 0; i < size; i++) - list.add(fn.next(r)); - return list; - }; - } - } - - public static class ArrayDSL<T> implements BaseSequenceDSL<ArrayDSL<T>, T[]> { - private final Class<T> type; - private final Gen<T> fn; - - public ArrayDSL(Class<T> type, Gen<T> fn) { - this.type = Objects.requireNonNull(type); - this.fn = Objects.requireNonNull(fn); - } - - @Override - public ArrayDSL<T> unique() - { - return new ArrayDSL<>(type, new GenReset<>(fn)); - } - - @Override - public Gen<T[]> ofSizeBetween(int minSize, int maxSize) { - Gen.IntGen sizeGen = ints().between(minSize, maxSize); - return r -> - { - Reset.tryReset(fn); - int size = sizeGen.nextInt(r); - T[] list = (T[]) Array.newInstance(type, size); - for (int i = 0; i < size; i++) - list[i] = fn.next(r); - return list; - }; - } - } - - public static class IntArrayDSL implements BaseSequenceDSL<IntArrayDSL, int[]> { - private final Gen.IntGen fn; - - public IntArrayDSL(Gen.IntGen fn) { - this.fn = Objects.requireNonNull(fn); - } - - @Override - public IntArrayDSL unique() - { - return new IntArrayDSL(new IntGenReset(fn)); - } - - @Override - public Gen<int[]> ofSizeBetween(int minSize, int maxSize) { - Gen.IntGen sizeGen = ints().between(minSize, maxSize); - return r -> - { - int size = sizeGen.nextInt(r); - int[] list = new int[size]; - for (int i = 0; i < size; i++) - list[i] = fn.nextInt(r); - return list; - }; - } - } - - public static class LongArrayDSL implements BaseSequenceDSL<LongArrayDSL, long[]> { - private final Gen.LongGen fn; - - public LongArrayDSL(Gen.LongGen fn) { - this.fn = Objects.requireNonNull(fn); - } - - @Override - public LongArrayDSL unique() - { - return new LongArrayDSL(new LongGenReset(fn)); - } - - @Override - public Gen<long[]> ofSizeBetween(int minSize, int maxSize) { - Gen.IntGen sizeGen = ints().between(minSize, maxSize); - return r -> - { - int size = sizeGen.nextInt(r); - long[] list = new long[size]; - for (int i = 0; i < size; i++) - list[i] = fn.nextLong(r); - return list; - }; - } - } - - public interface BaseSequenceDSL<A extends BaseSequenceDSL<A, B>, B> - { - A unique(); - - Gen<B> ofSizeBetween(int min, int max); - - default Gen<B> ofSize(int size) { - return ofSizeBetween(size, size); - } - } - - private interface Reset { - static void tryReset(Object o) - { - if (o instanceof Reset) - ((Reset) o).reset(); - } - - void reset(); - } - - private static class GenReset<T> implements Gen<T>, Reset - { - private final Set<T> seen = new HashSet<>(); - private final Gen<T> fn; - - private GenReset(Gen<T> fn) - { - this.fn = fn; - } - - @Override - public T next(RandomSource random) - { - T value; - while (!seen.add((value = fn.next(random)))) {} - return value; - } - - @Override - public void reset() - { - seen.clear(); - } - } - - private static class IntGenReset implements Gen.IntGen, Reset - { - private final GenReset<Integer> base; - - private IntGenReset(Gen.IntGen fn) - { - this.base = new GenReset<>(fn); - } - @Override - public int nextInt(RandomSource random) { - return base.next(random); - } - - @Override - public void reset() { - base.reset(); - } - } - - private static class LongGenReset implements Gen.LongGen, Reset - { - private final GenReset<Long> base; - - private LongGenReset(Gen.LongGen fn) - { - this.base = new GenReset<>(fn); - } - @Override - public long nextLong(RandomSource random) { - return base.next(random); - } - - @Override - public void reset() { - base.reset(); - } - } - - private static class Weight<T> implements Comparable<Weight<T>> - { - private final T value; - private final double weight; - - private Weight(T value, double weight) { - this.value = value; - this.weight = weight; - } - - @Override - public int compareTo(Weight<T> o) { - return Double.compare(weight, o.weight); - } - } -} diff --git a/test/unit/accord/utils/Invariants.java b/test/unit/accord/utils/Invariants.java deleted file mode 100644 index b960d8b268..0000000000 --- a/test/unit/accord/utils/Invariants.java +++ /dev/null @@ -1,327 +0,0 @@ -/* - * 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 accord.utils; - -import net.nicoulaj.compilecommand.annotations.Inline; - -import javax.annotation.Nullable; -import java.util.function.Predicate; - -import static java.lang.String.format; - -public class Invariants -{ - private static final boolean PARANOID = true; - private static final boolean DEBUG = true; - - public static boolean isParanoid() - { - return PARANOID; - } - public static boolean debug() - { - return DEBUG; - } - - private static void illegalState(String msg) - { - throw new IllegalStateException(msg); - } - - private static void illegalState() - { - illegalState(null); - } - - private static void illegalArgument(String msg) - { - throw new IllegalArgumentException(msg); - } - - - private static void illegalArgument() - { - illegalArgument(null); - } - - public static <T1, T2 extends T1> T2 checkType(T1 cast) - { - return (T2)cast; - } - - public static <T1, T2 extends T1> T2 checkType(Class<T2> to, T1 cast) - { - if (cast != null && !to.isInstance(cast)) - illegalState(); - return (T2)cast; - } - - public static <T1, T2 extends T1> T2 checkType(Class<T2> to, T1 cast, String msg) - { - if (cast != null && !to.isInstance(cast)) - illegalState(msg); - return (T2)cast; - } - - public static void paranoid(boolean condition) - { - if (PARANOID && !condition) - illegalState(); - } - - public static void checkState(boolean condition) - { - if (!condition) - illegalState(); - } - - public static void checkState(boolean condition, String msg) - { - if (!condition) - illegalState(msg); - } - - public static void checkState(boolean condition, String fmt, int p1) - { - if (!condition) - illegalState(format(fmt, p1)); - } - - public static void checkState(boolean condition, String fmt, int p1, int p2) - { - if (!condition) - illegalState(format(fmt, p1, p2)); - } - - public static void checkState(boolean condition, String fmt, long p1) - { - if (!condition) - illegalState(format(fmt, p1)); - } - - public static void checkState(boolean condition, String fmt, long p1, long p2) - { - if (!condition) - illegalState(format(fmt, p1, p2)); - } - - public static void checkState(boolean condition, String fmt, @Nullable Object p1) - { - if (!condition) - illegalState(format(fmt, p1)); - } - - public static void checkState(boolean condition, String fmt, @Nullable Object p1, @Nullable Object p2) - { - if (!condition) - illegalState(format(fmt, p1, p2)); - } - - public static void checkState(boolean condition, String fmt, Object... args) - { - if (!condition) - illegalState(format(fmt, args)); - } - - public static <T> T nonNull(T param) - { - if (param == null) - throw new NullPointerException(); - return param; - } - - public static <T> T nonNull(T param, String fmt, Object... args) - { - if (param == null) - throw new NullPointerException(format(fmt, args)); - return param; - } - - public static int isNatural(int input) - { - if (input < 0) - illegalState(); - return input; - } - - public static long isNatural(long input) - { - if (input < 0) - illegalState(); - return input; - } - - public static void checkArgument(boolean condition) - { - if (!condition) - illegalArgument(); - } - - public static void checkArgument(boolean condition, String msg) - { - if (!condition) - illegalArgument(msg); - } - - public static void checkArgument(boolean condition, String fmt, int p1) - { - if (!condition) - illegalArgument(format(fmt, p1)); - } - - public static void checkArgument(boolean condition, String fmt, int p1, int p2) - { - if (!condition) - illegalArgument(format(fmt, p1, p2)); - } - - public static void checkArgument(boolean condition, String fmt, long p1) - { - if (!condition) - illegalArgument(format(fmt, p1)); - } - - public static void checkArgument(boolean condition, String fmt, long p1, long p2) - { - if (!condition) - illegalArgument(format(fmt, p1, p2)); - } - - public static void checkArgument(boolean condition, String fmt, @Nullable Object p1) - { - if (!condition) - illegalArgument(format(fmt, p1)); - } - - public static void checkArgument(boolean condition, String fmt, @Nullable Object p1, @Nullable Object p2) - { - if (!condition) - illegalArgument(format(fmt, p1, p2)); - } - - public static void checkArgument(boolean condition, String fmt, Object... args) - { - if (!condition) - illegalArgument(format(fmt, args)); - } - - public static <T> T checkArgument(T param, boolean condition) - { - if (!condition) - illegalArgument(); - return param; - } - - public static <T> T checkArgument(T param, boolean condition, String msg) - { - if (!condition) - illegalArgument(msg); - return param; - } - - public static <T> T checkArgument(T param, boolean condition, String fmt, int p1) - { - if (!condition) - illegalArgument(format(fmt, p1)); - return param; - } - - public static <T> T checkArgument(T param, boolean condition, String fmt, int p1, int p2) - { - if (!condition) - illegalArgument(format(fmt, p1, p2)); - return param; - } - - public static <T> T checkArgument(T param, boolean condition, String fmt, long p1) - { - if (!condition) - illegalArgument(format(fmt, p1)); - return param; - } - - public static <T> T checkArgument(T param, boolean condition, String fmt, long p1, long p2) - { - if (!condition) - illegalArgument(format(fmt, p1, p2)); - return param; - } - - public static <T> T checkArgument(T param, boolean condition, String fmt, @Nullable Object p1) - { - if (!condition) - illegalArgument(format(fmt, p1)); - return param; - } - - public static <T> T checkArgument(T param, boolean condition, String fmt, @Nullable Object p1, @Nullable Object p2) - { - if (!condition) - illegalArgument(format(fmt, p1, p2)); - return param; - } - - public static <T> T checkArgument(T param, boolean condition, String fmt, Object... args) - { - if (!condition) - illegalArgument(format(fmt, args)); - return param; - } - - @Inline - public static <T> T checkArgument(T param, Predicate<T> condition) - { - if (!condition.test(param)) - illegalArgument(); - return param; - } - - @Inline - public static <T> T checkArgument(T param, Predicate<T> condition, String msg) - { - if (!condition.test(param)) - illegalArgument(msg); - return param; - } - - public static <O> O cast(Object o, Class<O> klass) - { - try - { - return klass.cast(o); - } - catch (ClassCastException e) - { - throw new IllegalArgumentException(format("Unable to cast %s to %s", o, klass.getName())); - } - } - - public static void checkIndexInBounds(int realLength, int offset, int length) - { - if (realLength == 0 || length == 0) - throw new IndexOutOfBoundsException("Unable to access offset " + offset + "; empty"); - if (offset < 0) - throw new IndexOutOfBoundsException("Offset " + offset + " must not be negative"); - if (length < 0) - throw new IndexOutOfBoundsException("Length " + length + " must not be negative"); - int endOffset = offset + length; - if (endOffset > realLength) - throw new IndexOutOfBoundsException(String.format("Offset %d, length = %d; real length was %d", offset, length, realLength)); - } -} diff --git a/test/unit/accord/utils/Property.java b/test/unit/accord/utils/Property.java deleted file mode 100644 index b90472c862..0000000000 --- a/test/unit/accord/utils/Property.java +++ /dev/null @@ -1,382 +0,0 @@ -/* - * 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 accord.utils; - -import java.time.Duration; -import java.util.Arrays; -import java.util.Objects; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ThreadLocalRandom; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; -import javax.annotation.Nullable; - -import org.apache.cassandra.transport.ProtocolException; -import org.apache.cassandra.utils.concurrent.AsyncPromise; - -public class Property -{ - public static abstract class Common<T extends Common<T>> - { - protected long seed = ThreadLocalRandom.current().nextLong(); - protected int examples = 1000; - - protected boolean pure = true; - @Nullable - protected Duration timeout = null; - - protected Common() { - } - - protected Common(Common<?> other) { - this.seed = other.seed; - this.examples = other.examples; - this.pure = other.pure; - this.timeout = other.timeout; - } - - public T withSeed(long seed) - { - this.seed = seed; - return (T) this; - } - - public T withExamples(int examples) - { - if (examples <= 0) - throw new IllegalArgumentException("Examples must be positive"); - this.examples = examples; - return (T) this; - } - - public T withPure(boolean pure) - { - this.pure = pure; - return (T) this; - } - - public T withTimeout(Duration timeout) - { - this.timeout = timeout; - return (T) this; - } - - protected void checkWithTimeout(Runnable fn) - { - AsyncPromise<?> promise = new AsyncPromise<>(); - Thread t = new Thread(() -> { - try - { - fn.run(); - promise.setSuccess(null); - } - catch (Throwable e) - { - promise.setFailure(e); - } - }); - t.setName("property with timeout"); - t.setDaemon(true); - try - { - t.start(); - promise.get(timeout.toNanos(), TimeUnit.NANOSECONDS); - } - catch (ExecutionException e) - { - throw new ProtocolException(propertyError(this, e.getCause())); - } - catch (InterruptedException e) - { - t.interrupt(); - throw new PropertyError(propertyError(this, e)); - } - catch (TimeoutException e) - { - t.interrupt(); - TimeoutException override = new TimeoutException("property test did not complete within " + this.timeout); - override.setStackTrace(new StackTraceElement[0]); - throw new PropertyError(propertyError(this, override)); - } - } - } - - public static class ForBuilder extends Common<ForBuilder> - { - public void check(FailingConsumer<RandomSource> fn) - { - forAll(Gens.random()).check(fn); - } - - public <T> SingleBuilder<T> forAll(Gen<T> gen) - { - return new SingleBuilder<>(gen, this); - } - - public <A, B> DoubleBuilder<A, B> forAll(Gen<A> a, Gen<B> b) - { - return new DoubleBuilder<>(a, b, this); - } - - public <A, B, C> TrippleBuilder<A, B, C> forAll(Gen<A> a, Gen<B> b, Gen<C> c) - { - return new TrippleBuilder<>(a, b, c, this); - } - } - - private static Object normalizeValue(Object value) - { - if (value == null) - return null; - // one day java arrays will have a useful toString... one day... - if (value.getClass().isArray()) - { - Class<?> subType = value.getClass().getComponentType(); - if (!subType.isPrimitive()) - return Arrays.asList((Object[]) value); - if (Byte.TYPE == subType) - return Arrays.toString((byte[]) value); - if (Character.TYPE == subType) - return Arrays.toString((char[]) value); - if (Short.TYPE == subType) - return Arrays.toString((short[]) value); - if (Integer.TYPE == subType) - return Arrays.toString((int[]) value); - if (Long.TYPE == subType) - return Arrays.toString((long[]) value); - if (Float.TYPE == subType) - return Arrays.toString((float[]) value); - if (Double.TYPE == subType) - return Arrays.toString((double[]) value); - } - try - { - return value.toString(); - } - catch (Throwable t) - { - return "Object.toString failed: " + t.getClass().getCanonicalName() + ": " + t.getMessage(); - } - } - - private static String propertyError(Common<?> input, Throwable cause, Object... values) - { - StringBuilder sb = new StringBuilder(); - // return "Seed=" + seed + "\nExamples=" + examples; - sb.append("Property error detected:\nSeed = ").append(input.seed).append('\n'); - sb.append("Examples = ").append(input.examples).append('\n'); - sb.append("Pure = ").append(input.pure).append('\n'); - if (cause != null) - { - String msg = cause.getMessage(); - sb.append("Error: "); - // to improve readability, if a newline is detected move the error msg to the next line - if (msg != null && msg.contains("\n")) - msg = "\n\t" + msg.replace("\n", "\n\t"); - if (msg == null) - msg = cause.getClass().getCanonicalName(); - sb.append(msg).append('\n'); - } - if (values != null) - { - sb.append("Values:\n"); - for (int i = 0; i < values.length; i++) - sb.append('\t').append(i).append(" = ").append(normalizeValue(values[i])).append('\n'); - } - return sb.toString(); - } - - public interface FailingConsumer<A> - { - void accept(A value) throws Exception; - } - - public static class SingleBuilder<T> extends Common<SingleBuilder<T>> - { - private final Gen<T> gen; - - private SingleBuilder(Gen<T> gen, Common<?> other) { - super(other); - this.gen = Objects.requireNonNull(gen); - } - - public void check(FailingConsumer<T> fn) - { - if (timeout != null) - { - checkWithTimeout(() -> checkInternal(fn)); - return; - } - checkInternal(fn); - } - - private void checkInternal(FailingConsumer<T> fn) - { - RandomSource random = new DefaultRandom(seed); - for (int i = 0; i < examples; i++) - { - T value = null; - try - { - checkInterrupted(); - fn.accept(value = gen.next(random)); - } - catch (Throwable t) - { - throw new PropertyError(propertyError(this, t, value), t); - } - if (pure) - { - seed = random.nextLong(); - random.setSeed(seed); - } - } - } - } - - public interface FailingBiConsumer<A, B> - { - void accept(A a, B b) throws Exception; - } - - public static class DoubleBuilder<A, B> extends Common<DoubleBuilder<A, B>> - { - private final Gen<A> aGen; - private final Gen<B> bGen; - - private DoubleBuilder(Gen<A> aGen, Gen<B> bGen, Common<?> other) { - super(other); - this.aGen = Objects.requireNonNull(aGen); - this.bGen = Objects.requireNonNull(bGen); - } - - public void check(FailingBiConsumer<A, B> fn) - { - if (timeout != null) - { - checkWithTimeout(() -> checkInternal(fn)); - return; - } - checkInternal(fn); - } - - private void checkInternal(FailingBiConsumer<A, B> fn) - { - RandomSource random = new DefaultRandom(seed); - for (int i = 0; i < examples; i++) - { - A a = null; - B b = null; - try - { - checkInterrupted(); - fn.accept(a = aGen.next(random), b = bGen.next(random)); - } - catch (Throwable t) - { - throw new PropertyError(propertyError(this, t, a, b), t); - } - if (pure) - { - seed = random.nextLong(); - random.setSeed(seed); - } - } - } - } - - public interface FailingTriConsumer<A, B, C> - { - void accept(A a, B b, C c) throws Exception; - } - - public static class TrippleBuilder<A, B, C> extends Common<TrippleBuilder<A, B, C>> - { - private final Gen<A> as; - private final Gen<B> bs; - private final Gen<C> cs; - - public TrippleBuilder(Gen<A> as, Gen<B> bs, Gen<C> cs, Common<?> other) - { - super(other); - this.as = as; - this.bs = bs; - this.cs = cs; - } - - public void check(FailingTriConsumer<A, B, C> fn) - { - if (timeout != null) - { - checkWithTimeout(() -> checkInternal(fn)); - return; - } - checkInternal(fn); - } - - private void checkInternal(FailingTriConsumer<A, B, C> fn) - { - RandomSource random = new DefaultRandom(seed); - for (int i = 0; i < examples; i++) - { - A a = null; - B b = null; - C c = null; - try - { - checkInterrupted(); - fn.accept(a = as.next(random), b = bs.next(random), c = cs.next(random)); - } - catch (Throwable t) - { - throw new PropertyError(propertyError(this, t, a, b, c), t); - } - if (pure) - { - seed = random.nextLong(); - random.setSeed(seed); - } - } - } - } - - private static void checkInterrupted() throws InterruptedException - { - if (Thread.currentThread().isInterrupted()) - throw new InterruptedException(); - } - - public static class PropertyError extends AssertionError - { - public PropertyError(String message, Throwable cause) - { - super(message, cause); - } - - public PropertyError(String message) - { - super(message); - } - } - - public static ForBuilder qt() - { - return new ForBuilder(); - } -} diff --git a/test/unit/accord/utils/RandomSource.java b/test/unit/accord/utils/RandomSource.java deleted file mode 100644 index 3d4861e5e4..0000000000 --- a/test/unit/accord/utils/RandomSource.java +++ /dev/null @@ -1,361 +0,0 @@ -/* - * 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 accord.utils; - -import java.util.ArrayList; -import java.util.Comparator; -import java.util.List; -import java.util.NavigableSet; -import java.util.Random; -import java.util.Set; -import java.util.stream.DoubleStream; -import java.util.stream.IntStream; -import java.util.stream.LongStream; - -public interface RandomSource -{ - static RandomSource wrap(Random random) - { - return new WrappedRandomSource(random); - } - - void nextBytes(byte[] bytes); - - boolean nextBoolean(); - - int nextInt(); - - default int nextInt(int maxExclusive) - { - return nextInt(0, maxExclusive); - } - - default int nextInt(int minInclusive, int maxExclusive) - { - // this is diff behavior than ThreadLocalRandom, which returns nextInt - if (minInclusive >= maxExclusive) - throw new IllegalArgumentException(String.format("Min (%s) should be less than max (%d).", minInclusive, maxExclusive)); - - int result = nextInt(); - int delta = maxExclusive - minInclusive; - int mask = delta - 1; - if ((delta & mask) == 0) // power of two - result = (result & mask) + minInclusive; - else if (delta > 0) - { - // reject over-represented candidates - for (int u = result >>> 1; // ensure nonnegative - u + mask - (result = u % delta) < 0; // rejection check - u = nextInt() >>> 1) // retry - ; - result += minInclusive; - } - else - { - // range not representable as int - while (result < minInclusive || result >= maxExclusive) - result = nextInt(); - } - return result; - } - - default IntStream ints() - { - return IntStream.generate(this::nextInt); - } - - default IntStream ints(int maxExclusive) - { - return IntStream.generate(() -> nextInt(maxExclusive)); - } - - default IntStream ints(int minInclusive, int maxExclusive) - { - return IntStream.generate(() -> nextInt(minInclusive, maxExclusive)); - } - - long nextLong(); - - default long nextLong(long maxExclusive) - { - return nextLong(0, maxExclusive); - } - - default long nextLong(long minInclusive, long maxExclusive) - { - // this is diff behavior than ThreadLocalRandom, which returns nextLong - if (minInclusive >= maxExclusive) - throw new IllegalArgumentException(String.format("Min (%s) should be less than max (%d).", minInclusive, maxExclusive)); - - long result = nextLong(); - long delta = maxExclusive - minInclusive; - long mask = delta - 1; - if ((delta & mask) == 0L) // power of two - result = (result & mask) + minInclusive; - else if (delta > 0L) - { - // reject over-represented candidates - for (long u = result >>> 1; // ensure nonnegative - u + mask - (result = u % delta) < 0L; // rejection check - u = nextLong() >>> 1) // retry - ; - result += minInclusive; - } - else - { - // range not representable as long - while (result < minInclusive || result >= maxExclusive) - result = nextLong(); - } - return result; - } - - default LongStream longs() - { - return LongStream.generate(this::nextLong); - } - - default LongStream longs(long maxExclusive) - { - return LongStream.generate(() -> nextLong(maxExclusive)); - } - - default LongStream longs(long minInclusive, long maxExclusive) - { - return LongStream.generate(() -> nextLong(minInclusive, maxExclusive)); - } - - float nextFloat(); - - double nextDouble(); - - default double nextDouble(double maxExclusive) - { - return nextDouble(0, maxExclusive); - } - - default double nextDouble(double minInclusive, double maxExclusive) - { - if (minInclusive >= maxExclusive) - throw new IllegalArgumentException(String.format("Min (%s) should be less than max (%d).", minInclusive, maxExclusive)); - - double result = nextDouble(); - result = result * (maxExclusive - minInclusive) + minInclusive; - if (result >= maxExclusive) // correct for rounding - result = Double.longBitsToDouble(Double.doubleToLongBits(maxExclusive) - 1); - return result; - } - - default DoubleStream doubles() - { - return DoubleStream.generate(this::nextDouble); - } - - default DoubleStream doubles(double maxExclusive) - { - return DoubleStream.generate(() -> nextDouble(maxExclusive)); - } - - default DoubleStream doubles(double minInclusive, double maxExclusive) - { - return DoubleStream.generate(() -> nextDouble(minInclusive, maxExclusive)); - } - - double nextGaussian(); - - default int pickInt(int first, int second, int... rest) - { - int offset = nextInt(0, rest.length + 2); - switch (offset) - { - case 0: return first; - case 1: return second; - default: return rest[offset - 2]; - } - } - - default int pickInt(int[] array) - { - return pickInt(array, 0, array.length); - } - - default int pickInt(int[] array, int offset, int length) - { - Invariants.checkIndexInBounds(array.length, offset, length); - if (length == 1) - return array[offset]; - return array[nextInt(offset, offset + length)]; - } - - default long pickLong(long first, long second, long... rest) - { - int offset = nextInt(0, rest.length + 2); - switch (offset) - { - case 0: return first; - case 1: return second; - default: return rest[offset - 2]; - } - } - - default long pickLong(long[] array) - { - return pickLong(array, 0, array.length); - } - - default long pickLong(long[] array, int offset, int length) - { - Invariants.checkIndexInBounds(array.length, offset, length); - if (length == 1) - return array[offset]; - return array[nextInt(offset, offset + length)]; - } - - default <T extends Comparable<T>> T pick(Set<T> set) - { - List<T> values = new ArrayList<>(set); - // Non-ordered sets may have different iteration order on different environments, which would make a seed produce different histories! - // To avoid such a problem, make sure to apply a deterministic function (sort). - if (!(set instanceof NavigableSet)) - values.sort(Comparator.naturalOrder()); - return pick(values); - } - - default <T> T pick(T first, T second, T... rest) - { - int offset = nextInt(0, rest.length + 2); - switch (offset) - { - case 0: return first; - case 1: return second; - default: return rest[offset - 2]; - } - } - - default <T> T pick(T[] array) - { - return array[nextInt(array.length)]; - } - - default <T> T pick(List<T> values) - { - return pick(values, 0, values.size()); - } - - default <T> T pick(List<T> values, int offset, int length) - { - Invariants.checkIndexInBounds(values.size(), offset, length); - if (length == 1) - return values.get(offset); - return values.get(nextInt(offset, offset + length)); - } - - void setSeed(long seed); - - RandomSource fork(); - - /** - * Returns true with a probability of {@code chance}. This logic is logically the same as - * <pre>{@code nextFloat() < chance}</pre> - * - * @param chance cumulative probability in range [0..1] - */ - default boolean decide(float chance) - { - return nextFloat() < chance; - } - - /** - * Returns true with a probability of {@code chance}. This logic is logically the same as - * <pre>{@code nextDouble() < chance}</pre> - * - * @param chance cumulative probability in range [0..1] - */ - default boolean decide(double chance) - { - return nextDouble() < chance; - } - - default long reset() - { - long seed = nextLong(); - setSeed(seed); - return seed; - } - - default Random asJdkRandom() - { - return new Random() - { - @Override - public void setSeed(long seed) - { - RandomSource.this.setSeed(seed); - } - - @Override - public void nextBytes(byte[] bytes) - { - RandomSource.this.nextBytes(bytes); - } - - @Override - public int nextInt() - { - return RandomSource.this.nextInt(); - } - - @Override - public int nextInt(int bound) - { - return RandomSource.this.nextInt(bound); - } - - @Override - public long nextLong() - { - return RandomSource.this.nextLong(); - } - - @Override - public boolean nextBoolean() - { - return RandomSource.this.nextBoolean(); - } - - @Override - public float nextFloat() - { - return RandomSource.this.nextFloat(); - } - - @Override - public double nextDouble() - { - return RandomSource.this.nextDouble(); - } - - @Override - public double nextGaussian() - { - return RandomSource.this.nextGaussian(); - } - }; - } -} diff --git a/test/unit/accord/utils/WrappedRandomSource.java b/test/unit/accord/utils/WrappedRandomSource.java deleted file mode 100644 index 3d02c101fb..0000000000 --- a/test/unit/accord/utils/WrappedRandomSource.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * 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 accord.utils; - -import java.util.Random; - -class WrappedRandomSource implements RandomSource -{ - private final Random random; - - WrappedRandomSource(Random random) - { - this.random = random; - } - - @Override - public Random asJdkRandom() - { - return random; - } - - @Override - public void nextBytes(byte[] bytes) - { - random.nextBytes(bytes); - } - - @Override - public boolean nextBoolean() - { - return random.nextBoolean(); - } - - @Override - public int nextInt() - { - return random.nextInt(); - } - - @Override - public int nextInt(int maxExclusive) - { - return random.nextInt(maxExclusive); - } - - @Override - public long nextLong() - { - return random.nextLong(); - } - - @Override - public float nextFloat() - { - return random.nextFloat(); - } - - @Override - public double nextDouble() - { - return random.nextDouble(); - } - - @Override - public double nextGaussian() - { - return random.nextGaussian(); - } - - @Override - public void setSeed(long seed) - { - random.setSeed(seed); - } - - @Override - public RandomSource fork() - { - return new WrappedRandomSource(new Random(nextLong())); - } -} diff --git a/test/unit/org/apache/cassandra/concurrent/SimulatedExecutorFactory.java b/test/unit/org/apache/cassandra/concurrent/SimulatedExecutorFactory.java index ffb1a0fabd..42c277aeea 100644 --- a/test/unit/org/apache/cassandra/concurrent/SimulatedExecutorFactory.java +++ b/test/unit/org/apache/cassandra/concurrent/SimulatedExecutorFactory.java @@ -273,7 +273,7 @@ public class SimulatedExecutorFactory implements ExecutorFactory, Clock long max = TimeUnit.MILLISECONDS.toNanos(5); LongSupplier small = () -> rs.nextLong(0, maxSmall); LongSupplier large = () -> rs.nextLong(maxSmall, max); - this.jitterNanos = Gens.bools().runs(rs.nextInt(1, 11) / 100.0D, rs.nextInt(3, 15)).mapToLong(b -> b ? large.getAsLong() : small.getAsLong()).asLongSupplier(rs); + this.jitterNanos = Gens.bools().biasedRepeatingRuns(rs.nextInt(1, 11) / 100.0D, rs.nextInt(3, 15)).mapToLong(b -> b ? large.getAsLong() : small.getAsLong()).asLongSupplier(rs); } @Override diff --git a/test/unit/org/apache/cassandra/cql3/CQLTester.java b/test/unit/org/apache/cassandra/cql3/CQLTester.java index 01fc9c34d6..d1de392b68 100644 --- a/test/unit/org/apache/cassandra/cql3/CQLTester.java +++ b/test/unit/org/apache/cassandra/cql3/CQLTester.java @@ -144,7 +144,6 @@ import org.apache.cassandra.io.sstable.Descriptor; import org.apache.cassandra.io.util.File; import org.apache.cassandra.io.util.FileSystems; import org.apache.cassandra.io.util.FileUtils; -import org.apache.cassandra.locator.InetAddressAndPort; import org.apache.cassandra.metrics.CassandraMetricsRegistry; import org.apache.cassandra.metrics.ClientMetrics; import org.apache.cassandra.schema.IndexMetadata; diff --git a/test/unit/org/apache/cassandra/repair/FuzzTestBase.java b/test/unit/org/apache/cassandra/repair/FuzzTestBase.java index 12e54add4d..1162638ed1 100644 --- a/test/unit/org/apache/cassandra/repair/FuzzTestBase.java +++ b/test/unit/org/apache/cassandra/repair/FuzzTestBase.java @@ -81,7 +81,7 @@ import org.apache.cassandra.dht.Murmur3Partitioner; import org.apache.cassandra.dht.Range; import org.apache.cassandra.dht.Token; import org.apache.cassandra.distributed.test.log.ClusterMetadataTestHelper; -import org.apache.cassandra.exceptions.RequestFailureReason; +import org.apache.cassandra.exceptions.RequestFailure; import org.apache.cassandra.gms.ApplicationState; import org.apache.cassandra.gms.EndpointState; import org.apache.cassandra.gms.HeartBeatState; @@ -754,9 +754,9 @@ public abstract class FuzzTestBase extends CQLTester.InMemory callback.onResponse(msg); } - public void onFailure(InetAddressAndPort from, RequestFailureReason failureReason) + public void onFailure(InetAddressAndPort from, RequestFailure failure) { - if (callback.invokeOnFailure()) callback.onFailure(from, failureReason); + if (callback.invokeOnFailure()) callback.onFailure(from, failure); } } @@ -847,7 +847,7 @@ public abstract class FuzzTestBase extends CQLTester.InMemory if (ctx != null) { assert ctx == cb; - ctx.onFailure(to, RequestFailureReason.TIMEOUT); + ctx.onFailure(to, RequestFailure.TIMEOUT); } }, message.verb().expiresAfterNanos(), TimeUnit.NANOSECONDS); } @@ -862,13 +862,13 @@ public abstract class FuzzTestBase extends CQLTester.InMemory long max = TimeUnit.SECONDS.toNanos(5); LongSupplier small = () -> rs.nextLong(min, maxSmall); LongSupplier large = () -> rs.nextLong(maxSmall, max); - return Gens.bools().runs(rs.nextInt(1, 11) / 100.0D, rs.nextInt(3, 15)).mapToLong(b -> b ? large.getAsLong() : small.getAsLong()).asLongSupplier(rs); + return Gens.bools().biasedRepeatingRuns(rs.nextInt(1, 11) / 100.0D, rs.nextInt(3, 15)).mapToLong(b -> b ? large.getAsLong() : small.getAsLong()).asLongSupplier(rs); }).getAsLong(); } private boolean networkDrops(InetAddressAndPort to) { - return networkDrops.computeIfAbsent(new Connection(broadcastAddressAndPort, to), ignore -> Gens.bools().runs(rs.nextInt(1, 11) / 100.0D, rs.nextInt(3, 15)).asSupplier(rs)).get(); + return networkDrops.computeIfAbsent(new Connection(broadcastAddressAndPort, to), ignore -> Gens.bools().biasedRepeatingRuns(rs.nextInt(1, 11) / 100.0D, rs.nextInt(3, 15)).asSupplier(rs)).get(); } @Override @@ -884,9 +884,9 @@ public abstract class FuzzTestBase extends CQLTester.InMemory } @Override - public void onFailure(InetAddressAndPort from, RequestFailureReason failureReason) + public void onFailure(InetAddressAndPort from, RequestFailure failure) { - promise.tryFailure(new MessagingService.FailureResponseException(from, failureReason)); + promise.tryFailure(new MessagingService.FailureResponseException(from, failure.reason)); } @Override @@ -1018,7 +1018,7 @@ public abstract class FuzzTestBase extends CQLTester.InMemory try { if (msg.isFailureResponse()) - callback.onFailure(msg.from(), (RequestFailureReason) msg.payload); + callback.onFailure(msg.from(), (RequestFailure) msg.payload); else callback.onResponse(msg); } catch (Throwable t) diff --git a/test/unit/org/apache/cassandra/service/accord/AccordSyncPropagatorTest.java b/test/unit/org/apache/cassandra/service/accord/AccordSyncPropagatorTest.java index 73830bda44..f3bb71707d 100644 --- a/test/unit/org/apache/cassandra/service/accord/AccordSyncPropagatorTest.java +++ b/test/unit/org/apache/cassandra/service/accord/AccordSyncPropagatorTest.java @@ -361,7 +361,7 @@ public class AccordSyncPropagatorTest { if (self.equals(ep)) return true; - return !nodeRuns.computeIfAbsent(ep, ignore -> Gens.bools().biasedRepeatingRuns(.01)).next(rs); + return !nodeRuns.computeIfAbsent(ep, ignore -> Gens.bools().biasedRepeatingRuns(.01, rs.nextInt(3, 15))).next(rs); } @Override diff --git a/test/unit/org/apache/cassandra/service/accord/AccordTopologyTest.java b/test/unit/org/apache/cassandra/service/accord/AccordTopologyTest.java index 318da5658d..4c24ee0569 100644 --- a/test/unit/org/apache/cassandra/service/accord/AccordTopologyTest.java +++ b/test/unit/org/apache/cassandra/service/accord/AccordTopologyTest.java @@ -47,6 +47,7 @@ import org.apache.cassandra.schema.Keyspaces; import org.apache.cassandra.schema.ReplicationParams; import org.apache.cassandra.schema.Tables; import org.apache.cassandra.tcm.ClusterMetadata; +import org.apache.cassandra.tcm.Epoch; import org.apache.cassandra.tcm.membership.Location; import org.apache.cassandra.tcm.membership.NodeAddresses; import org.apache.cassandra.tcm.membership.NodeId; @@ -128,7 +129,7 @@ public class AccordTopologyTest ReplicationParams replication = keyspace.params.replication; AbstractReplicationStrategy strategy = AbstractReplicationStrategy.createReplicationStrategy(keyspace.name, replication); DataPlacements.Builder placements = metadata.placements.unbuild(); - DataPlacement placement = strategy.calculateDataPlacement(metadata.tokenMap.toRanges(), metadata); + DataPlacement placement = strategy.calculateDataPlacement(Epoch.EMPTY, metadata.tokenMap.toRanges(), metadata); placements.with(replication, placement); metadata = transformer.with(placements.build()).build().metadata; } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@cassandra.apache.org For additional commands, e-mail: commits-h...@cassandra.apache.org