Search in sources :

Example 1 with CounterValueTestResponse

use of org.infinispan.server.hotrod.counter.response.CounterValueTestResponse in project infinispan by infinispan.

the class RecoveryOp method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) {
    // magic byte
    buf.readUnsignedByte();
    long id = ExtendedByteBuf.readUnsignedLong(buf);
    HotRodOperation opCode = HotRodOperation.fromResponseOpCode((byte) buf.readUnsignedByte());
    OperationStatus status = OperationStatus.fromCode((byte) buf.readUnsignedByte());
    short topologyChangeMarker = buf.readUnsignedByte();
    Op op = client.idToOp.get(id);
    log.tracef("Decoding response messageId=%d opCode=%s status=%s topologyChange=%d", id, opCode, status, topologyChangeMarker);
    AbstractTestTopologyAwareResponse topologyChangeResponse;
    if (topologyChangeMarker == 1) {
        int topologyId = readUnsignedInt(buf);
        if (op.clientIntel == Constants.INTELLIGENCE_TOPOLOGY_AWARE) {
            int numberClusterMembers = readUnsignedInt(buf);
            ServerAddress[] viewArray = new ServerAddress[numberClusterMembers];
            for (int i = 0; i < numberClusterMembers; i++) {
                String host = readString(buf);
                int port = readUnsignedShort(buf);
                viewArray[i] = ServerAddress.forAddress(host, port, true);
            }
            topologyChangeResponse = new TestTopologyAwareResponse(topologyId, Arrays.asList(viewArray));
        } else if (op.clientIntel == Constants.INTELLIGENCE_HASH_DISTRIBUTION_AWARE) {
            if (op.version < 20)
                topologyChangeResponse = read1xHashDistAwareHeader(buf, topologyId, op);
            else
                topologyChangeResponse = read2xHashDistAwareHeader(buf, topologyId, op);
        } else {
            throw new UnsupportedOperationException("Client intelligence " + op.clientIntel + " not supported");
        }
    } else {
        topologyChangeResponse = null;
    }
    TestResponse resp;
    switch(opCode) {
        case STATS:
            int size = readUnsignedInt(buf);
            Map<String, String> stats = new HashMap<>();
            for (int i = 0; i < size; ++i) {
                stats.put(readString(buf), readString(buf));
            }
            resp = new TestStatsResponse(op.version, id, op.cacheName, op.clientIntel, op.topologyId, topologyChangeResponse, stats);
            break;
        case PUT:
        case PUT_IF_ABSENT:
        case REPLACE:
        case REPLACE_IF_UNMODIFIED:
        case REMOVE:
        case REMOVE_IF_UNMODIFIED:
        case PUT_STREAM:
            boolean checkPrevious;
            if (op.version >= 10 && op.version <= 13) {
                checkPrevious = (op.flags & ProtocolFlag.ForceReturnPreviousValue.getValue()) == 1;
            } else {
                checkPrevious = status == SuccessWithPrevious || status == NotExecutedWithPrevious;
            }
            if (checkPrevious) {
                int length = readUnsignedInt(buf);
                if (length == 0) {
                    resp = new TestResponseWithPrevious(op.version, id, op.cacheName, op.clientIntel, opCode, status, op.topologyId, topologyChangeResponse, Optional.empty());
                } else {
                    byte[] previous = new byte[length];
                    buf.readBytes(previous);
                    resp = new TestResponseWithPrevious(op.version, id, op.cacheName, op.clientIntel, opCode, status, op.topologyId, topologyChangeResponse, Optional.of(previous));
                }
            } else {
                resp = new TestResponse(op.version, id, op.cacheName, op.clientIntel, opCode, status, op.topologyId, topologyChangeResponse);
            }
            break;
        case CONTAINS_KEY:
        case CLEAR:
        case PING:
        case ADD_CLIENT_LISTENER:
        case REMOVE_CLIENT_LISTENER:
        case ITERATION_END:
            resp = new TestResponse(op.version, id, op.cacheName, op.clientIntel, opCode, status, op.topologyId, topologyChangeResponse);
            break;
        case GET_WITH_VERSION:
            if (status == Success) {
                long version = buf.readLong();
                Optional<byte[]> data = Optional.of(ExtendedByteBuf.readRangedBytes(buf));
                resp = new TestGetWithVersionResponse(op.version, id, op.cacheName, op.clientIntel, opCode, status, op.topologyId, topologyChangeResponse, data, version);
            } else {
                resp = new TestGetWithVersionResponse(op.version, id, op.cacheName, op.clientIntel, opCode, status, op.topologyId, topologyChangeResponse, Optional.empty(), 0);
            }
            break;
        case GET_WITH_METADATA:
        case GET_STREAM:
            if (status == Success) {
                long created = -1;
                int lifespan = -1;
                long lastUsed = -1;
                int maxIdle = -1;
                byte flags = buf.readByte();
                if ((flags & 0x01) != 0x01) {
                    created = buf.readLong();
                    lifespan = readUnsignedInt(buf);
                }
                if ((flags & 0x02) != 0x02) {
                    lastUsed = buf.readLong();
                    maxIdle = readUnsignedInt(buf);
                }
                long version = buf.readLong();
                Optional<byte[]> data = Optional.of(ExtendedByteBuf.readRangedBytes(buf));
                resp = new TestGetWithMetadataResponse(op.version, id, op.cacheName, op.clientIntel, opCode, status, op.topologyId, topologyChangeResponse, data, version, created, lifespan, lastUsed, maxIdle);
            } else {
                resp = new TestGetWithMetadataResponse(op.version, id, op.cacheName, op.clientIntel, opCode, status, op.topologyId, topologyChangeResponse, Optional.empty(), 0, -1, -1, -1, -1);
            }
            break;
        case GET:
            if (status == Success) {
                Optional<byte[]> data = Optional.of(ExtendedByteBuf.readRangedBytes(buf));
                resp = new TestGetResponse(op.version, id, op.cacheName, op.clientIntel, opCode, status, op.topologyId, topologyChangeResponse, data);
            } else {
                resp = new TestGetResponse(op.version, id, op.cacheName, op.clientIntel, opCode, status, op.topologyId, topologyChangeResponse, Optional.empty());
            }
            break;
        case BULK_GET:
            byte done = buf.readByte();
            Map<byte[], byte[]> bulkBuffer = new HashMap<>();
            while (done == 1) {
                bulkBuffer.put(ExtendedByteBuf.readRangedBytes(buf), ExtendedByteBuf.readRangedBytes(buf));
                done = buf.readByte();
            }
            resp = new TestBulkGetResponse(op.version, id, op.cacheName, op.clientIntel, op.topologyId, topologyChangeResponse, bulkBuffer);
            break;
        case BULK_GET_KEYS:
            done = buf.readByte();
            Set<byte[]> bulkKeys = new HashSet<>();
            while (done == 1) {
                bulkKeys.add(ExtendedByteBuf.readRangedBytes(buf));
                done = buf.readByte();
            }
            resp = new TestBulkGetKeysResponse(op.version, id, op.cacheName, op.clientIntel, op.topologyId, topologyChangeResponse, bulkKeys);
            break;
        case QUERY:
            byte[] result = ExtendedByteBuf.readRangedBytes(buf);
            resp = new TestQueryResponse(op.version, id, op.cacheName, op.clientIntel, op.topologyId, topologyChangeResponse, result);
            break;
        case AUTH_MECH_LIST:
            size = readUnsignedInt(buf);
            Set<String> mechs = new HashSet<>();
            for (int i = 0; i < size; ++i) {
                mechs.add(readString(buf));
            }
            resp = new TestAuthMechListResponse(op.version, id, op.cacheName, op.clientIntel, op.topologyId, topologyChangeResponse, mechs);
            break;
        case AUTH:
            {
                boolean complete = buf.readBoolean();
                byte[] challenge = ExtendedByteBuf.readRangedBytes(buf);
                resp = new TestAuthResponse(op.version, id, op.cacheName, op.clientIntel, op.topologyId, topologyChangeResponse, complete, challenge);
                break;
            }
        case CACHE_ENTRY_CREATED_EVENT:
        case CACHE_ENTRY_MODIFIED_EVENT:
        case CACHE_ENTRY_REMOVED_EVENT:
            byte[] listenerId = ExtendedByteBuf.readRangedBytes(buf);
            byte isCustom = buf.readByte();
            boolean isRetried = buf.readByte() == 1;
            if (isCustom == 1 || isCustom == 2) {
                byte[] eventData = ExtendedByteBuf.readRangedBytes(buf);
                resp = new TestCustomEvent(client.protocolVersion, id, client.defaultCacheName, opCode, listenerId, isRetried, eventData);
            } else {
                byte[] key = ExtendedByteBuf.readRangedBytes(buf);
                if (opCode == HotRodOperation.CACHE_ENTRY_REMOVED_EVENT) {
                    resp = new TestKeyEvent(client.protocolVersion, id, client.defaultCacheName, listenerId, isRetried, key);
                } else {
                    long dataVersion = buf.readLong();
                    resp = new TestKeyWithVersionEvent(client.protocolVersion, id, client.defaultCacheName, opCode, listenerId, isRetried, key, dataVersion);
                }
            }
            break;
        case SIZE:
            long lsize = ExtendedByteBuf.readUnsignedLong(buf);
            resp = new TestSizeResponse(op.version, id, op.cacheName, op.clientIntel, op.topologyId, topologyChangeResponse, lsize);
            break;
        case ERROR:
            if (op == null)
                resp = new TestErrorResponse((byte) 10, id, "", (short) 0, status, 0, null, readString(buf));
            else
                resp = new TestErrorResponse(op.version, id, op.cacheName, op.clientIntel, status, op.topologyId, topologyChangeResponse, readString(buf));
            break;
        case ITERATION_START:
            String iterationId = readString(buf);
            resp = new TestIteratorStartResponse(op.version, id, op.cacheName, op.clientIntel, op.topologyId, topologyChangeResponse, iterationId);
            break;
        case ITERATION_NEXT:
            resp = new TestIteratorNextResponse(op.version, id, op.cacheName, op.clientIntel, op.topologyId, topologyChangeResponse);
            break;
        case PREPARE_TX:
        case ROLLBACK_TX:
        case COMMIT_TX:
            resp = new TxResponse(client.protocolVersion, id, client.defaultCacheName, op.clientIntel, opCode, status, op.topologyId, topologyChangeResponse, status == OperationStatus.Success ? buf.readInt() : 0);
            break;
        case FORGET_TX:
            resp = new TestResponse(op.version, id, op.cacheName, op.clientIntel, opCode, status, op.topologyId, topologyChangeResponse);
            break;
        case FETCH_TX_RECOVERY:
            resp = status == OperationStatus.Success ? new RecoveryTestResponse(op.version, id, op.cacheName, op.clientIntel, opCode, status, op.topologyId, topologyChangeResponse, buf) : new TestResponse(op.version, id, op.cacheName, op.clientIntel, opCode, status, op.topologyId, topologyChangeResponse);
            break;
        case COUNTER_REMOVE:
        case COUNTER_CREATE:
        case COUNTER_IS_DEFINED:
        case COUNTER_RESET:
        case COUNTER_ADD_LISTENER:
        case COUNTER_REMOVE_LISTENER:
            resp = new TestResponse(op.version, id, op.cacheName, op.clientIntel, opCode, status, op.topologyId, topologyChangeResponse);
            break;
        case COUNTER_GET_CONFIGURATION:
            resp = status == OperationStatus.Success ? new CounterConfigurationTestResponse(op.version, id, op.cacheName, op.clientIntel, opCode, status, op.topologyId, topologyChangeResponse, decodeConfiguration(buf::readByte, buf::readLong, () -> readUnsignedInt(buf))) : new TestResponse(op.version, id, op.cacheName, op.clientIntel, opCode, status, op.topologyId, topologyChangeResponse);
            break;
        case COUNTER_GET:
        case COUNTER_ADD_AND_GET:
        case COUNTER_CAS:
            resp = status == OperationStatus.Success ? new CounterValueTestResponse(op.version, id, op.cacheName, op.clientIntel, opCode, status, op.topologyId, topologyChangeResponse, buf.readLong()) : new TestResponse(op.version, id, op.cacheName, op.clientIntel, opCode, status, op.topologyId, topologyChangeResponse);
            break;
        case COUNTER_GET_NAMES:
            resp = status == OperationStatus.Success ? new CounterNamesTestResponse(op.version, id, op.cacheName, op.clientIntel, opCode, status, op.topologyId, topologyChangeResponse, buf) : new TestResponse(op.version, id, op.cacheName, op.clientIntel, opCode, status, op.topologyId, topologyChangeResponse);
            break;
        case COUNTER_EVENT:
            resp = new TestCounterEventResponse(client.protocolVersion, id, opCode, buf);
            break;
        default:
            resp = null;
            break;
    }
    // We cannot assert this since in concurrent case we could get two responses in single buffer
    if (log.isTraceEnabled() && buf.readerIndex() != buf.writerIndex()) {
        log.tracef("Left bytes in the buffer: %s", new String(ByteBufUtil.getBytes(buf, buf.readerIndex(), buf.writerIndex() - buf.readerIndex())));
    }
    if (resp != null) {
        log.tracef("Got response: %s", resp);
        out.add(resp);
    }
}
Also used : CounterOp(org.infinispan.server.hotrod.counter.op.CounterOp) CounterConfigurationTestResponse(org.infinispan.server.hotrod.counter.response.CounterConfigurationTestResponse) RecoveryTestResponse(org.infinispan.server.hotrod.counter.response.RecoveryTestResponse) CounterNamesTestResponse(org.infinispan.server.hotrod.counter.response.CounterNamesTestResponse) CounterValueTestResponse(org.infinispan.server.hotrod.counter.response.CounterValueTestResponse) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ServerAddress(org.infinispan.server.hotrod.ServerAddress) ExtendedByteBuf.writeString(org.infinispan.server.hotrod.transport.ExtendedByteBuf.writeString) ExtendedByteBuf.readString(org.infinispan.server.hotrod.transport.ExtendedByteBuf.readString) OperationStatus(org.infinispan.server.hotrod.OperationStatus) CounterValueTestResponse(org.infinispan.server.hotrod.counter.response.CounterValueTestResponse) HashSet(java.util.HashSet) HotRodOperation(org.infinispan.server.hotrod.HotRodOperation) TestCounterEventResponse(org.infinispan.server.hotrod.counter.impl.TestCounterEventResponse) CounterConfigurationTestResponse(org.infinispan.server.hotrod.counter.response.CounterConfigurationTestResponse) CounterNamesTestResponse(org.infinispan.server.hotrod.counter.response.CounterNamesTestResponse) RecoveryTestResponse(org.infinispan.server.hotrod.counter.response.RecoveryTestResponse)

Example 2 with CounterValueTestResponse

use of org.infinispan.server.hotrod.counter.response.CounterValueTestResponse in project infinispan by infinispan.

the class TestWeakCounter method getValue.

@Override
public long getValue() {
    CounterOp op = new CounterOp(client.protocolVersion(), COUNTER_GET, name);
    TestResponse response = client.execute(op);
    switch(response.getStatus()) {
        case Success:
            return ((CounterValueTestResponse) response).getValue();
        case ServerError:
            throw new CounterException(((TestErrorResponse) response).msg);
        default:
            throw new CounterException("unknown response " + response);
    }
}
Also used : CounterValueTestResponse(org.infinispan.server.hotrod.counter.response.CounterValueTestResponse) TestResponse(org.infinispan.server.hotrod.test.TestResponse) CounterOp(org.infinispan.server.hotrod.counter.op.CounterOp) CounterValueTestResponse(org.infinispan.server.hotrod.counter.response.CounterValueTestResponse) CounterException(org.infinispan.counter.exception.CounterException)

Aggregations

CounterOp (org.infinispan.server.hotrod.counter.op.CounterOp)2 CounterValueTestResponse (org.infinispan.server.hotrod.counter.response.CounterValueTestResponse)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CounterException (org.infinispan.counter.exception.CounterException)1 HotRodOperation (org.infinispan.server.hotrod.HotRodOperation)1 OperationStatus (org.infinispan.server.hotrod.OperationStatus)1 ServerAddress (org.infinispan.server.hotrod.ServerAddress)1 TestCounterEventResponse (org.infinispan.server.hotrod.counter.impl.TestCounterEventResponse)1 CounterConfigurationTestResponse (org.infinispan.server.hotrod.counter.response.CounterConfigurationTestResponse)1 CounterNamesTestResponse (org.infinispan.server.hotrod.counter.response.CounterNamesTestResponse)1 RecoveryTestResponse (org.infinispan.server.hotrod.counter.response.RecoveryTestResponse)1 TestResponse (org.infinispan.server.hotrod.test.TestResponse)1 ExtendedByteBuf.readString (org.infinispan.server.hotrod.transport.ExtendedByteBuf.readString)1 ExtendedByteBuf.writeString (org.infinispan.server.hotrod.transport.ExtendedByteBuf.writeString)1