Search in sources :

Example 1 with IMessage

use of org.apache.cassandra.distributed.api.IMessage in project cassandra by apache.

the class Instance method registerOutboundFilter.

private void registerOutboundFilter(ICluster cluster) {
    MessagingService.instance().outboundSink.add((message, to) -> {
        if (isShutdown())
            return false;
        IMessage serialzied = serializeMessage(message.from(), to, message);
        // since this instance is sending the message, from will always be this instance
        int fromNum = config.num();
        IInstance toInstance = cluster.get(fromCassandraInetAddressAndPort(to));
        if (toInstance == null)
            return false;
        int toNum = toInstance.config().num();
        return cluster.filters().permitOutbound(fromNum, toNum, serialzied);
    });
}
Also used : IMessage(org.apache.cassandra.distributed.api.IMessage) IInstance(org.apache.cassandra.distributed.api.IInstance)

Example 2 with IMessage

use of org.apache.cassandra.distributed.api.IMessage in project cassandra by apache.

the class Instance method registerInboundFilter.

private void registerInboundFilter(ICluster<?> cluster) {
    MessagingService.instance().inboundSink.add(message -> {
        if (!cluster.filters().hasInbound())
            return true;
        if (isShutdown())
            return false;
        IMessage serialized = serializeMessage(message.from(), toCassandraInetAddressAndPort(broadcastAddress()), message);
        IInstance from = cluster.get(serialized.from());
        if (from == null)
            return false;
        int fromNum = from.config().num();
        // since this instance is reciving the message, to will always be this instance
        int toNum = config.num();
        return cluster.filters().permitInbound(fromNum, toNum, serialized);
    });
}
Also used : IMessage(org.apache.cassandra.distributed.api.IMessage) IInstance(org.apache.cassandra.distributed.api.IInstance)

Example 3 with IMessage

use of org.apache.cassandra.distributed.api.IMessage in project cassandra by apache.

the class SimulatedAction method applyToMessage.

Action applyToMessage(IInvokableInstance from, IInvokableInstance to, IMessage message) {
    Executor executor = to.executorFor(message.verb());
    if (executor instanceof ImmediateExecutor)
        executor = to.executor();
    InterceptedExecution.InterceptedTaskExecution task = new InterceptedRunnableExecution((InterceptingExecutor) executor, () -> to.receiveMessageWithInvokingThread(message));
    Verb verb = Verb.fromId(message.verb());
    Modifiers self = verbModifiers.getOrDefault(verb, NONE);
    int fromNum = from.config().num();
    int toNum = to.config().num();
    Deliver deliver;
    if (is(Modifier.RELIABLE) || self.is(Modifier.RELIABLE))
        deliver = DELIVER;
    else
        deliver = simulated.futureScheduler.shouldDeliver(fromNum, toNum);
    Action action;
    switch(deliver) {
        default:
            throw new AssertionError();
        case DELIVER:
            {
                Object description = lazy(() -> String.format("%s(%d) from %s to %s", Verb.fromId(message.verb()), message.id(), message.from(), to.broadcastAddress()));
                OrderOn orderOn = task.executor.orderAppliesAfterScheduling();
                action = applyTo(description, MESSAGE, orderOn, self, verb, task);
                action.setDeadline(simulated.futureScheduler.messageDeadlineNanos(fromNum, toNum));
                break;
            }
        case FAILURE:
        case TIMEOUT:
            {
                task.cancel();
                self = DROP.with(self);
                InetSocketAddress failedOn;
                IInvokableInstance notify;
                if (verb.isResponse()) {
                    failedOn = from.broadcastAddress();
                    notify = to;
                } else {
                    failedOn = to.broadcastAddress();
                    notify = from;
                }
                InterceptedExecution.InterceptedTaskExecution failTask = new InterceptedRunnableExecution((InterceptingExecutor) notify.executorFor(verb.id), () -> notify.unsafeApplyOnThisThread((socketAddress, id, isTimeout) -> {
                    InetAddressAndPort address = InetAddressAndPort.getByAddress(socketAddress);
                    RequestCallbacks.CallbackInfo callback = instance().callbacks.remove(id, address);
                    if (callback != null) {
                        RequestCallback<?> invokeOn = (RequestCallback<?>) callback.callback;
                        RequestFailureReason reason = isTimeout ? RequestFailureReason.TIMEOUT : RequestFailureReason.UNKNOWN;
                        invokeOn.onFailure(address, reason);
                    }
                    return null;
                }, failedOn, message.id(), deliver == TIMEOUT));
                Object description = (lazy(() -> String.format("Report Timeout of %s(%d) from %s to %s", Verb.fromId(message.verb()), message.id(), failedOn, notify.broadcastAddress())));
                OrderOn orderOn = failTask.executor.orderAppliesAfterScheduling();
                action = applyTo(description, MESSAGE, orderOn, self, failTask);
                switch(deliver) {
                    default:
                        throw new AssertionError();
                    case TIMEOUT:
                        long expiresAfterNanos = from.unsafeApplyOnThisThread(id -> Verb.fromId(id).expiresAfterNanos(), (verb.isResponse() ? forVerb : verb).id);
                        action.setDeadline(simulated.futureScheduler.messageTimeoutNanos(expiresAfterNanos));
                        break;
                    case FAILURE:
                        action.setDeadline(simulated.futureScheduler.messageFailureNanos(toNum, fromNum));
                        break;
                }
                break;
            }
    }
    return action;
}
Also used : Actions(org.apache.cassandra.simulator.Actions) START_TASK(org.apache.cassandra.simulator.Action.Modifiers.START_TASK) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) OrderOn(org.apache.cassandra.simulator.OrderOn) DELIVER(org.apache.cassandra.simulator.FutureActionScheduler.Deliver.DELIVER) TASK(org.apache.cassandra.simulator.systems.SimulatedAction.Kind.TASK) LazyToString.lazy(org.apache.cassandra.utils.LazyToString.lazy) START_DAEMON_TASK(org.apache.cassandra.simulator.Action.Modifiers.START_DAEMON_TASK) InterceptedRunnableExecution(org.apache.cassandra.simulator.systems.InterceptedExecution.InterceptedRunnableExecution) ActionList(org.apache.cassandra.simulator.ActionList) RequestCallbacks(org.apache.cassandra.net.RequestCallbacks) Deliver(org.apache.cassandra.simulator.FutureActionScheduler.Deliver) IMessage(org.apache.cassandra.distributed.api.IMessage) ArrayList(java.util.ArrayList) MESSAGE(org.apache.cassandra.simulator.systems.SimulatedAction.Kind.MESSAGE) UNBOUNDED_WAIT(org.apache.cassandra.simulator.systems.InterceptedWait.Kind.UNBOUNDED_WAIT) SIMULATION(org.apache.cassandra.utils.Shared.Scope.SIMULATION) Map(java.util.Map) WAKE_UP_THREAD(org.apache.cassandra.simulator.Action.Modifiers.WAKE_UP_THREAD) TIMEOUT(org.apache.cassandra.simulator.FutureActionScheduler.Deliver.TIMEOUT) MessagingService.instance(org.apache.cassandra.net.MessagingService.instance) START_THREAD(org.apache.cassandra.simulator.Action.Modifiers.START_THREAD) Action(org.apache.cassandra.simulator.Action) Nullable(javax.annotation.Nullable) EnumMap(java.util.EnumMap) Executor(java.util.concurrent.Executor) NONE(org.apache.cassandra.simulator.Action.Modifiers.NONE) DROP(org.apache.cassandra.simulator.Action.Modifiers.DROP) Verb(org.apache.cassandra.net.Verb) RequestFailureReason(org.apache.cassandra.exceptions.RequestFailureReason) RequestCallback(org.apache.cassandra.net.RequestCallback) START_INFINITE_LOOP(org.apache.cassandra.simulator.Action.Modifiers.START_INFINITE_LOOP) InetSocketAddress(java.net.InetSocketAddress) START_SCHEDULED_TASK(org.apache.cassandra.simulator.Action.Modifiers.START_SCHEDULED_TASK) START_TIMEOUT_TASK(org.apache.cassandra.simulator.Action.Modifiers.START_TIMEOUT_TASK) List(java.util.List) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) TriggerListener(org.apache.cassandra.simulator.systems.InterceptedWait.TriggerListener) SCHEDULED_TIMEOUT(org.apache.cassandra.simulator.systems.SimulatedAction.Kind.SCHEDULED_TIMEOUT) Shared(org.apache.cassandra.utils.Shared) LOG(org.apache.cassandra.simulator.Debug.Info.LOG) ImmediateExecutor(org.apache.cassandra.concurrent.ImmediateExecutor) Collections(java.util.Collections) LazyToString(org.apache.cassandra.utils.LazyToString) Deliver(org.apache.cassandra.simulator.FutureActionScheduler.Deliver) Action(org.apache.cassandra.simulator.Action) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) InterceptedRunnableExecution(org.apache.cassandra.simulator.systems.InterceptedExecution.InterceptedRunnableExecution) InetSocketAddress(java.net.InetSocketAddress) OrderOn(org.apache.cassandra.simulator.OrderOn) Executor(java.util.concurrent.Executor) ImmediateExecutor(org.apache.cassandra.concurrent.ImmediateExecutor) RequestFailureReason(org.apache.cassandra.exceptions.RequestFailureReason) RequestCallback(org.apache.cassandra.net.RequestCallback) ImmediateExecutor(org.apache.cassandra.concurrent.ImmediateExecutor) Verb(org.apache.cassandra.net.Verb) RequestCallbacks(org.apache.cassandra.net.RequestCallbacks)

Example 4 with IMessage

use of org.apache.cassandra.distributed.api.IMessage in project cassandra by apache.

the class MessageFiltersTest method executeWithWriteFailure.

public Object[][] executeWithWriteFailure(Cluster cluster, String statement, ConsistencyLevel cl, int coordinator, Object... bindings) {
    IMessageFilters filters = cluster.filters();
    // Drop exactly one coordinated message
    filters.verbs(Verb.MUTATION_REQ.id).from(coordinator).messagesMatching(new IMessageFilters.Matcher() {

        private final AtomicBoolean issued = new AtomicBoolean();

        public boolean matches(int from, int to, IMessage message) {
            if (from != coordinator || message.verb() != Verb.MUTATION_REQ.id)
                return false;
            return !issued.getAndSet(true);
        }
    }).drop().on();
    Object[][] res = cluster.coordinator(coordinator).execute(statement, cl, bindings);
    filters.reset();
    return res;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IMessageFilters(org.apache.cassandra.distributed.api.IMessageFilters) IMessage(org.apache.cassandra.distributed.api.IMessage)

Example 5 with IMessage

use of org.apache.cassandra.distributed.api.IMessage in project cassandra by apache.

the class InJvmSutBase method executeWithWriteFailure.

// TODO: Ideally, we need to be able to induce a failure of a single specific message
public Object[][] executeWithWriteFailure(String statement, ConsistencyLevel cl, Object... bindings) {
    if (isShutdown.get())
        throw new RuntimeException("Instance is shut down");
    try {
        int coordinator = (int) (cnt.getAndIncrement() % cluster.size() + 1);
        IMessageFilters filters = cluster.filters();
        // Drop exactly one coordinated message
        int MUTATION_REQ = 0;
        // TODO: make dropping deterministic
        filters.verbs(MUTATION_REQ).from(coordinator).messagesMatching(new IMessageFilters.Matcher() {

            private final AtomicBoolean issued = new AtomicBoolean();

            public boolean matches(int from, int to, IMessage message) {
                if (from != coordinator || message.verb() != MUTATION_REQ)
                    return false;
                return !issued.getAndSet(true);
            }
        }).drop().on();
        Object[][] res = cluster.coordinator(coordinator).execute(statement, toApiCl(cl), bindings);
        filters.reset();
        return res;
    } catch (Throwable t) {
        logger.error(String.format("Caught error while trying execute statement %s", statement), t);
        throw t;
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IMessageFilters(org.apache.cassandra.distributed.api.IMessageFilters) IMessage(org.apache.cassandra.distributed.api.IMessage)

Aggregations

IMessage (org.apache.cassandra.distributed.api.IMessage)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 IInstance (org.apache.cassandra.distributed.api.IInstance)2 IMessageFilters (org.apache.cassandra.distributed.api.IMessageFilters)2 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 EnumMap (java.util.EnumMap)1 List (java.util.List)1 Map (java.util.Map)1 Executor (java.util.concurrent.Executor)1 Nullable (javax.annotation.Nullable)1 ImmediateExecutor (org.apache.cassandra.concurrent.ImmediateExecutor)1 IInvokableInstance (org.apache.cassandra.distributed.api.IInvokableInstance)1 RequestFailureReason (org.apache.cassandra.exceptions.RequestFailureReason)1 InetAddressAndPort (org.apache.cassandra.locator.InetAddressAndPort)1 MessagingService.instance (org.apache.cassandra.net.MessagingService.instance)1 RequestCallback (org.apache.cassandra.net.RequestCallback)1 RequestCallbacks (org.apache.cassandra.net.RequestCallbacks)1 Verb (org.apache.cassandra.net.Verb)1