use of org.corfudb.protocols.wireprotocol.CorfuMsg in project CorfuDB by CorfuDB.
the class TestChannelContext method simulateSerialization.
public ByteBuf simulateSerialization(Object message) {
if (message instanceof CorfuMsg) {
/* simulate serialization/deserialization */
ByteBuf oBuf = Unpooled.buffer();
((CorfuMsg) message).serialize(oBuf);
oBuf.resetReaderIndex();
return oBuf;
}
throw new UnsupportedOperationException("Test framework does not support serialization of object type " + message.getClass());
}
use of org.corfudb.protocols.wireprotocol.CorfuMsg in project CorfuDB by CorfuDB.
the class TestClientRouter method handleMessage.
private void handleMessage(Object o) {
if (o instanceof CorfuMsg) {
CorfuMsg m = (CorfuMsg) o;
if (validateEpochAndClientID(m, channelContext)) {
IClient handler = handlerMap.get(m.getMsgType());
handler.handleMessage(m, null);
}
}
}
use of org.corfudb.protocols.wireprotocol.CorfuMsg in project CorfuDB by CorfuDB.
the class TestClientRouter method validateEpochAndClientID.
/**
* Validate the epoch of a CorfuMsg, and send a WRONG_EPOCH response if
* the server is in the wrong epoch. Ignored if the message type is reset (which
* is valid in any epoch).
*
* @param msg The incoming message to validate.
* @param ctx The context of the channel handler.
* @return True, if the epoch is correct, but false otherwise.
*/
public boolean validateEpochAndClientID(CorfuMsg msg, ChannelHandlerContext ctx) {
// Check if the message is intended for us. If not, drop the message.
if (!msg.getClientID().equals(clientID)) {
log.warn("Incoming message intended for client {}, our id is {}, dropping!", msg.getClientID(), clientID);
return false;
}
// Check if the message is in the right epoch.
if (!msg.getMsgType().ignoreEpoch && msg.getEpoch() != getEpoch()) {
CorfuMsg m = new CorfuMsg();
log.trace("Incoming message with wrong epoch, got {}, expected {}, message was: {}", msg.getEpoch(), getEpoch(), msg);
/* If this message was pending a completion, complete it with an error. */
completeExceptionally(msg.getRequestID(), new WrongEpochException(getEpoch()));
return false;
}
return true;
}
use of org.corfudb.protocols.wireprotocol.CorfuMsg in project CorfuDB by CorfuDB.
the class TestClientRouter method simulateSerialization.
public CorfuMsg simulateSerialization(CorfuMsg message) {
/* simulate serialization/deserialization */
ByteBuf oBuf = Unpooled.buffer();
message.serialize(oBuf);
oBuf.resetReaderIndex();
CorfuMsg msg = CorfuMsg.deserialize(oBuf);
oBuf.release();
return msg;
}
use of org.corfudb.protocols.wireprotocol.CorfuMsg in project CorfuDB by CorfuDB.
the class CorfuMsgHandler method generateHandlers.
/** Generate handlers for a particular server.
*
* @param caller The context that is being used. Call MethodHandles.lookup() to obtain.
* @param o The object that implements the server.
* @return
*/
public CorfuMsgHandler generateHandlers(final MethodHandles.Lookup caller, final Object o) {
Arrays.stream(o.getClass().getDeclaredMethods()).filter(x -> x.isAnnotationPresent(ServerHandler.class)).forEach(x -> {
ServerHandler a = x.getAnnotation(ServerHandler.class);
if (!x.getParameterTypes()[0].isAssignableFrom(a.type().messageType.getRawType())) {
throw new RuntimeException("Incorrect message type, expected " + a.type().messageType.getRawType() + " but provided " + x.getParameterTypes()[0]);
}
if (handlerMap.containsKey(a.type())) {
throw new RuntimeException("Handler for " + a.type() + " already registered!");
}
try {
Handler h;
if (Modifier.isStatic(x.getModifiers())) {
MethodHandle mh = caller.unreflect(x);
MethodType mt = mh.type().changeParameterType(0, CorfuMsg.class);
h = (Handler) LambdaMetafactory.metafactory(caller, "handle", MethodType.methodType(Handler.class), mt, mh, mh.type()).getTarget().invokeExact();
} else {
MethodType mt = MethodType.methodType(x.getReturnType(), x.getParameterTypes());
MethodHandle mh = caller.findVirtual(o.getClass(), x.getName(), mt);
MethodType mtGeneric = mh.type().changeParameterType(1, CorfuMsg.class);
h = (Handler) LambdaMetafactory.metafactory(caller, "handle", MethodType.methodType(Handler.class, o.getClass()), mtGeneric.dropParameterTypes(0, 1), mh, mh.type().dropParameterTypes(0, 1)).getTarget().bindTo(o).invoke();
}
final Timer t;
if (!a.opTimer().equals("")) {
t = ServerContext.getMetrics().timer(a.opTimer());
} else {
t = null;
}
handlerMap.put(a.type(), (CorfuMsg msg, ChannelHandlerContext ctx, IServerRouter r, boolean isMetricsEnabled) -> {
try (Timer.Context timerCxt = MetricsUtils.getConditionalContext(t != null && isMetricsEnabled, t)) {
h.handle(msg, ctx, r, isMetricsEnabled);
}
});
} catch (Throwable e) {
throw new RuntimeException(e);
}
});
return this;
}
Aggregations