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);
});
}
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);
});
}
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;
}
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;
}
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;
}
}
Aggregations