Search in sources :

Example 1 with AbstractClientEvent

use of org.infinispan.client.hotrod.event.impl.AbstractClientEvent in project infinispan by infinispan.

the class Codec20 method readCacheEvent.

@Override
public AbstractClientEvent readCacheEvent(ByteBuf buf, Function<byte[], DataFormat> listenerDataFormat, short eventTypeId, ClassAllowList allowList, SocketAddress serverAddress) {
    short status = buf.readUnsignedByte();
    // ignore, no topology expected
    buf.readUnsignedByte();
    ClientEvent.Type eventType;
    switch(eventTypeId) {
        case CACHE_ENTRY_CREATED_EVENT_RESPONSE:
            eventType = ClientEvent.Type.CLIENT_CACHE_ENTRY_CREATED;
            break;
        case CACHE_ENTRY_MODIFIED_EVENT_RESPONSE:
            eventType = ClientEvent.Type.CLIENT_CACHE_ENTRY_MODIFIED;
            break;
        case CACHE_ENTRY_REMOVED_EVENT_RESPONSE:
            eventType = ClientEvent.Type.CLIENT_CACHE_ENTRY_REMOVED;
            break;
        case ERROR_RESPONSE:
            checkForErrorsInResponseStatus(buf, null, status, serverAddress);
        // Fall through if we didn't throw an exception already
        default:
            throw HOTROD.unknownEvent(eventTypeId);
    }
    byte[] listenerId = ByteBufUtil.readArray(buf);
    short isCustom = buf.readUnsignedByte();
    boolean isRetried = buf.readUnsignedByte() == 1;
    DataFormat dataFormat = listenerDataFormat.apply(listenerId);
    if (isCustom == 1) {
        final Object eventData = dataFormat.valueToObj(ByteBufUtil.readArray(buf), allowList);
        return createCustomEvent(listenerId, eventData, eventType, isRetried);
    } else {
        switch(eventType) {
            case CLIENT_CACHE_ENTRY_CREATED:
                long createdDataVersion = buf.readLong();
                return createCreatedEvent(listenerId, dataFormat.keyToObj(ByteBufUtil.readArray(buf), allowList), createdDataVersion, isRetried);
            case CLIENT_CACHE_ENTRY_MODIFIED:
                long modifiedDataVersion = buf.readLong();
                return createModifiedEvent(listenerId, dataFormat.keyToObj(ByteBufUtil.readArray(buf), allowList), modifiedDataVersion, isRetried);
            case CLIENT_CACHE_ENTRY_REMOVED:
                return createRemovedEvent(listenerId, dataFormat.keyToObj(ByteBufUtil.readArray(buf), allowList), isRetried);
            default:
                throw HOTROD.unknownEvent(eventTypeId);
        }
    }
}
Also used : DataFormat(org.infinispan.client.hotrod.DataFormat) ClientEvent(org.infinispan.client.hotrod.event.ClientEvent) AbstractClientEvent(org.infinispan.client.hotrod.event.impl.AbstractClientEvent)

Example 2 with AbstractClientEvent

use of org.infinispan.client.hotrod.event.impl.AbstractClientEvent in project infinispan by infinispan.

the class HeaderDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
    try {
        switch(state()) {
            case READ_MESSAGE_ID:
                long messageId = codec.readMessageId(in);
                receivedOpCode = codec.readOpCode(in);
                switch(receivedOpCode) {
                    case CACHE_ENTRY_CREATED_EVENT_RESPONSE:
                    case CACHE_ENTRY_MODIFIED_EVENT_RESPONSE:
                    case CACHE_ENTRY_REMOVED_EVENT_RESPONSE:
                    case CACHE_ENTRY_EXPIRED_EVENT_RESPONSE:
                        if (codec.allowOperationsAndEvents()) {
                            operation = messageId == 0 ? null : incomplete.get(messageId);
                        } else if (incomplete.size() == 1) {
                            operation = incomplete.values().iterator().next();
                            messageId = operation.header().messageId();
                        } else if (incomplete.size() > 1) {
                            throw new IllegalStateException("Too many incomplete operations: " + incomplete);
                        } else {
                            operation = null;
                            messageId = 0;
                        }
                        // complete earlier.
                        if (operation != null && !(operation instanceof AddClientListenerOperation)) {
                            throw HOTROD.operationIsNotAddClientListener(messageId, operation.toString());
                        } else if (log.isTraceEnabled()) {
                            log.tracef("Received event for request %d", messageId, operation);
                        }
                        checkpoint(State.READ_CACHE_EVENT);
                        // the loop in HintedReplayingDecoder will call decode again
                        return;
                    case COUNTER_EVENT_RESPONSE:
                        checkpoint(State.READ_COUNTER_EVENT);
                        // the loop in HintedReplayingDecoder will call decode again
                        return;
                }
                if (messageId == 0) {
                    // let's read the header even at this stage; it should throw an error and the other throw statement
                    // won't be reached
                    codec.readHeader(in, receivedOpCode, null, channelFactory, ctx.channel().remoteAddress());
                    throw new IllegalStateException("Should be never reached");
                }
                // we can remove the operation at this point since we'll read no more in this state
                operation = incomplete.remove(messageId);
                if (operation == null) {
                    throw HOTROD.unknownMessageId(messageId);
                }
                if (log.isTraceEnabled()) {
                    log.tracef("Received response for request %d, %s", messageId, operation);
                }
                checkpoint(State.READ_HEADER);
            // fall through
            case READ_HEADER:
                if (log.isTraceEnabled()) {
                    log.tracef("Decoding header for message %s", HotRodConstants.Names.of(receivedOpCode));
                }
                status = codec.readHeader(in, receivedOpCode, operation.header(), channelFactory, ctx.channel().remoteAddress());
                checkpoint(State.READ_PAYLOAD);
            // fall through
            case READ_PAYLOAD:
                if (log.isTraceEnabled()) {
                    log.tracef("Decoding payload for message %s", HotRodConstants.Names.of(receivedOpCode));
                }
                operation.acceptResponse(in, status, this);
                checkpoint(State.READ_MESSAGE_ID);
                break;
            case READ_CACHE_EVENT:
                if (log.isTraceEnabled()) {
                    log.tracef("Decoding cache event %s", HotRodConstants.Names.of(receivedOpCode));
                }
                AbstractClientEvent cacheEvent;
                try {
                    cacheEvent = codec.readCacheEvent(in, listenerNotifier::getCacheDataFormat, receivedOpCode, configuration.getClassAllowList(), ctx.channel().remoteAddress());
                } catch (Signal signal) {
                    throw signal;
                } catch (Throwable t) {
                    log.unableToReadEventFromServer(t, ctx.channel().remoteAddress());
                    throw t;
                }
                if (operation != null) {
                    ((AddClientListenerOperation) operation).postponeTimeout(ctx.channel());
                }
                invokeEvent(cacheEvent.getListenerId(), cacheEvent);
                checkpoint(State.READ_MESSAGE_ID);
                break;
            case READ_COUNTER_EVENT:
                if (log.isTraceEnabled()) {
                    log.tracef("Decoding counter event %s", HotRodConstants.Names.of(receivedOpCode));
                }
                HotRodCounterEvent counterEvent;
                try {
                    counterEvent = codec.readCounterEvent(in);
                } catch (Signal signal) {
                    throw signal;
                } catch (Throwable t) {
                    HOTROD.unableToReadEventFromServer(t, ctx.channel().remoteAddress());
                    throw t;
                }
                invokeEvent(counterEvent.getListenerId(), counterEvent);
                checkpoint(State.READ_MESSAGE_ID);
                break;
        }
    } catch (Signal signal) {
        throw signal;
    } catch (Exception e) {
        // If this is server error make sure to restart the state of decoder
        checkpoint(State.READ_MESSAGE_ID);
        throw e;
    }
}
Also used : AbstractClientEvent(org.infinispan.client.hotrod.event.impl.AbstractClientEvent) Signal(io.netty.util.Signal) HotRodCounterEvent(org.infinispan.client.hotrod.counter.impl.HotRodCounterEvent) AddClientListenerOperation(org.infinispan.client.hotrod.impl.operations.AddClientListenerOperation) TransportException(org.infinispan.client.hotrod.exceptions.TransportException)

Example 3 with AbstractClientEvent

use of org.infinispan.client.hotrod.event.impl.AbstractClientEvent in project infinispan by infinispan.

the class Codec21 method readCacheEvent.

@Override
public AbstractClientEvent readCacheEvent(ByteBuf buf, Function<byte[], DataFormat> listenerDataFormat, short eventTypeId, ClassAllowList allowList, SocketAddress serverAddress) {
    short status = buf.readUnsignedByte();
    // ignore, no topology expected
    buf.readUnsignedByte();
    ClientEvent.Type eventType;
    switch(eventTypeId) {
        case CACHE_ENTRY_CREATED_EVENT_RESPONSE:
            eventType = ClientEvent.Type.CLIENT_CACHE_ENTRY_CREATED;
            break;
        case CACHE_ENTRY_MODIFIED_EVENT_RESPONSE:
            eventType = ClientEvent.Type.CLIENT_CACHE_ENTRY_MODIFIED;
            break;
        case CACHE_ENTRY_REMOVED_EVENT_RESPONSE:
            eventType = ClientEvent.Type.CLIENT_CACHE_ENTRY_REMOVED;
            break;
        case CACHE_ENTRY_EXPIRED_EVENT_RESPONSE:
            eventType = ClientEvent.Type.CLIENT_CACHE_ENTRY_EXPIRED;
            break;
        case ERROR_RESPONSE:
            checkForErrorsInResponseStatus(buf, null, status, serverAddress);
        default:
            throw HOTROD.unknownEvent(eventTypeId);
    }
    byte[] listenerId = ByteBufUtil.readArray(buf);
    short isCustom = buf.readUnsignedByte();
    boolean isRetried = buf.readUnsignedByte() == 1;
    DataFormat dataFormat = listenerDataFormat.apply(listenerId);
    if (isCustom == 1) {
        final Object eventData = dataFormat.valueToObj(ByteBufUtil.readArray(buf), allowList);
        return createCustomEvent(listenerId, eventData, eventType, isRetried);
    } else if (isCustom == 2) {
        // Raw data
        return createCustomEvent(listenerId, ByteBufUtil.readArray(buf), eventType, isRetried);
    } else {
        switch(eventType) {
            case CLIENT_CACHE_ENTRY_CREATED:
                Object createdKey = dataFormat.keyToObj(ByteBufUtil.readArray(buf), allowList);
                long createdDataVersion = buf.readLong();
                return createCreatedEvent(listenerId, createdKey, createdDataVersion, isRetried);
            case CLIENT_CACHE_ENTRY_MODIFIED:
                Object modifiedKey = dataFormat.keyToObj(ByteBufUtil.readArray(buf), allowList);
                long modifiedDataVersion = buf.readLong();
                return createModifiedEvent(listenerId, modifiedKey, modifiedDataVersion, isRetried);
            case CLIENT_CACHE_ENTRY_REMOVED:
                Object removedKey = dataFormat.keyToObj(ByteBufUtil.readArray(buf), allowList);
                return createRemovedEvent(listenerId, removedKey, isRetried);
            case CLIENT_CACHE_ENTRY_EXPIRED:
                Object expiredKey = dataFormat.keyToObj(ByteBufUtil.readArray(buf), allowList);
                return createExpiredEvent(listenerId, expiredKey);
            default:
                throw HOTROD.unknownEvent(eventTypeId);
        }
    }
}
Also used : DataFormat(org.infinispan.client.hotrod.DataFormat) AbstractClientEvent(org.infinispan.client.hotrod.event.impl.AbstractClientEvent) ClientEvent(org.infinispan.client.hotrod.event.ClientEvent)

Aggregations

AbstractClientEvent (org.infinispan.client.hotrod.event.impl.AbstractClientEvent)3 DataFormat (org.infinispan.client.hotrod.DataFormat)2 ClientEvent (org.infinispan.client.hotrod.event.ClientEvent)2 Signal (io.netty.util.Signal)1 HotRodCounterEvent (org.infinispan.client.hotrod.counter.impl.HotRodCounterEvent)1 TransportException (org.infinispan.client.hotrod.exceptions.TransportException)1 AddClientListenerOperation (org.infinispan.client.hotrod.impl.operations.AddClientListenerOperation)1