Search in sources :

Example 1 with ActionListener

use of org.apache.cassandra.simulator.ActionListener in project cassandra by apache.

the class PairOfSequencesPaxosSimulation method plan.

public ActionPlan plan() {
    ActionPlan plan = new KeyspaceActions(simulated, KEYSPACE, TABLE, CREATE_TABLE, cluster, clusterOptions, serialConsistency, this, primaryKeys, debug).plan();
    plan = plan.encapsulate(ActionPlan.setUpTearDown(ActionList.of(cluster.stream().map(i -> simulated.run("Insert Partitions", i, executeForPrimaryKeys(INSERT1, primaryKeys)))), ActionList.of(cluster.stream().map(i -> simulated.run("Delete Partitions", i, executeForPrimaryKeys(DELETE1, primaryKeys))))));
    final int nodes = cluster.size();
    for (int primaryKey : primaryKeys) historyCheckers.add(new HistoryChecker(primaryKey));
    List<Supplier<Action>> primaryKeyActions = new ArrayList<>();
    for (int pki = 0; pki < primaryKeys.length; ++pki) {
        int primaryKey = primaryKeys[pki];
        HistoryChecker historyChecker = historyCheckers.get(pki);
        Supplier<Action> supplier = new Supplier<Action>() {

            int i = 0;

            @Override
            public Action get() {
                int node = simulated.random.uniform(1, nodes + 1);
                IInvokableInstance instance = cluster.get(node);
                switch(serialConsistency) {
                    default:
                        throw new AssertionError();
                    case LOCAL_SERIAL:
                        if (simulated.snitch.dcOf(node) > 0) {
                            // perform some queries against these nodes but don't expect them to be linearizable
                            return new NonVerifyingOperation(i++, instance, serialConsistency, primaryKey, historyChecker);
                        }
                    case SERIAL:
                        return simulated.random.decide(readRatio) ? new VerifyingOperation(i++, instance, serialConsistency, primaryKey, historyChecker) : new ModifyingOperation(i++, instance, ANY, serialConsistency, primaryKey, historyChecker);
                }
            }

            @Override
            public String toString() {
                return Integer.toString(primaryKey);
            }
        };
        final ActionListener listener = debug.debug(PARTITION, simulated.time, cluster, KEYSPACE, primaryKey);
        if (listener != null) {
            Supplier<Action> wrap = supplier;
            supplier = new Supplier<Action>() {

                @Override
                public Action get() {
                    Action action = wrap.get();
                    action.register(listener);
                    return action;
                }

                @Override
                public String toString() {
                    return wrap.toString();
                }
            };
        }
        primaryKeyActions.add(supplier);
    }
    List<Integer> available = IntStream.range(0, primaryKeys.length).boxed().collect(Collectors.toList());
    Action stream = Actions.infiniteStream(concurrency, new Supplier<Action>() {

        @Override
        public Action get() {
            int i = simulated.random.uniform(0, available.size());
            int next = available.get(i);
            available.set(i, available.get(available.size() - 1));
            available.remove(available.size() - 1);
            long untilNanos = simulated.time.nanoTime() + SECONDS.toNanos(simulateKeyForSeconds.select(simulated.random));
            int concurrency = withinKeyConcurrency.select(simulated.random);
            Supplier<Action> supplier = primaryKeyActions.get(next);
            // while this stream is finite, it participates in an infinite stream via its parent, so we want to permit termination while it's running
            return Actions.infiniteStream(concurrency, new Supplier<Action>() {

                @Override
                public Action get() {
                    if (simulated.time.nanoTime() >= untilNanos) {
                        available.add(next);
                        return null;
                    }
                    return supplier.get();
                }

                @Override
                public String toString() {
                    return supplier.toString();
                }
            });
        }

        @Override
        public String toString() {
            return "Primary Key Actions";
        }
    });
    return simulated.execution.plan().encapsulate(plan).encapsulate(ActionPlan.interleave(singletonList(ActionList.of(stream))));
}
Also used : Action(org.apache.cassandra.simulator.Action) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) ArrayList(java.util.ArrayList) ActionPlan(org.apache.cassandra.simulator.ActionPlan) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ActionListener(org.apache.cassandra.simulator.ActionListener) KeyspaceActions(org.apache.cassandra.simulator.cluster.KeyspaceActions) LongSupplier(java.util.function.LongSupplier) Supplier(java.util.function.Supplier)

Aggregations

ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 LongSupplier (java.util.function.LongSupplier)1 Supplier (java.util.function.Supplier)1 IInvokableInstance (org.apache.cassandra.distributed.api.IInvokableInstance)1 Action (org.apache.cassandra.simulator.Action)1 ActionListener (org.apache.cassandra.simulator.ActionListener)1 ActionPlan (org.apache.cassandra.simulator.ActionPlan)1 KeyspaceActions (org.apache.cassandra.simulator.cluster.KeyspaceActions)1