Search in sources :

Example 11 with CorfuMsg

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());
}
Also used : CorfuMsg(org.corfudb.protocols.wireprotocol.CorfuMsg) ByteBuf(io.netty.buffer.ByteBuf)

Example 12 with CorfuMsg

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);
        }
    }
}
Also used : CorfuMsg(org.corfudb.protocols.wireprotocol.CorfuMsg)

Example 13 with CorfuMsg

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;
}
Also used : CorfuMsg(org.corfudb.protocols.wireprotocol.CorfuMsg) WrongEpochException(org.corfudb.runtime.exceptions.WrongEpochException)

Example 14 with CorfuMsg

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;
}
Also used : CorfuMsg(org.corfudb.protocols.wireprotocol.CorfuMsg) ByteBuf(io.netty.buffer.ByteBuf)

Example 15 with CorfuMsg

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;
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Arrays(java.util.Arrays) CorfuMsg(org.corfudb.protocols.wireprotocol.CorfuMsg) MetricsUtils(org.corfudb.util.MetricsUtils) Modifier(java.lang.reflect.Modifier) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Timer(com.codahale.metrics.Timer) Set(java.util.Set) java.lang.invoke(java.lang.invoke) CorfuMsgType(org.corfudb.protocols.wireprotocol.CorfuMsgType) CorfuMsg(org.corfudb.protocols.wireprotocol.CorfuMsg) Timer(com.codahale.metrics.Timer) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext)

Aggregations

CorfuMsg (org.corfudb.protocols.wireprotocol.CorfuMsg)15 ByteBuf (io.netty.buffer.ByteBuf)4 Test (org.junit.Test)2 Timer (com.codahale.metrics.Timer)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 java.lang.invoke (java.lang.invoke)1 Modifier (java.lang.reflect.Modifier)1 Arrays (java.util.Arrays)1 Map (java.util.Map)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CorfuMsgType (org.corfudb.protocols.wireprotocol.CorfuMsgType)1 WrongEpochException (org.corfudb.runtime.exceptions.WrongEpochException)1 MetricsUtils (org.corfudb.util.MetricsUtils)1