Search in sources :

Example 26 with CacheServerStats

use of org.apache.geode.internal.cache.tier.sockets.CacheServerStats in project geode by apache.

the class Size method cmdExecute.

@Override
public void cmdExecute(Message clientMessage, ServerConnection serverConnection, long start) throws IOException, InterruptedException {
    StringBuilder errMessage = new StringBuilder();
    CachedRegionHelper crHelper = serverConnection.getCachedRegionHelper();
    CacheServerStats stats = serverConnection.getCacheServerStats();
    serverConnection.setAsTrue(REQUIRES_RESPONSE);
    long oldStart = start;
    start = DistributionStats.getStatTime();
    stats.incReadSizeRequestTime(start - oldStart);
    // Retrieve the data from the message parts
    Part regionNamePart = clientMessage.getPart(0);
    String regionName = regionNamePart.getString();
    if (regionName == null) {
        logger.warn(LocalizedMessage.create(LocalizedStrings.BaseCommand__THE_INPUT_REGION_NAME_FOR_THE_0_REQUEST_IS_NULL, "size"));
        errMessage.append(LocalizedStrings.BaseCommand__THE_INPUT_REGION_NAME_FOR_THE_0_REQUEST_IS_NULL.toLocalizedString("size"));
        writeErrorResponse(clientMessage, MessageType.SIZE_ERROR, errMessage.toString(), serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    LocalRegion region = (LocalRegion) crHelper.getRegion(regionName);
    if (region == null) {
        String reason = LocalizedStrings.BaseCommand__0_WAS_NOT_FOUND_DURING_1_REQUEST.toLocalizedString(regionName, "size");
        writeRegionDestroyedEx(clientMessage, regionName, reason, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    // Size the entry
    try {
        this.securityService.authorizeRegionRead(regionName);
        writeSizeResponse(region.size(), clientMessage, serverConnection);
    } catch (RegionDestroyedException rde) {
        writeException(clientMessage, rde, false, serverConnection);
    } catch (Exception e) {
        // If an interrupted exception is thrown , rethrow it
        checkForInterrupt(serverConnection, e);
        // If an exception occurs during the destroy, preserve the connection
        writeException(clientMessage, e, false, serverConnection);
        if (e instanceof GemFireSecurityException) {
            // logged by the security logger
            if (logger.isDebugEnabled()) {
                logger.debug("{}: Unexpected Security exception", serverConnection.getName(), e);
            }
        } else {
            logger.warn(LocalizedMessage.create(LocalizedStrings.BaseCommand_0_UNEXPECTED_EXCEPTION, serverConnection.getName()), e);
        }
    } finally {
        if (logger.isDebugEnabled()) {
            logger.debug("{}: Sent size response for region {}", serverConnection.getName(), regionName);
        }
        serverConnection.setAsTrue(RESPONDED);
        stats.incWriteSizeResponseTime(DistributionStats.getStatTime() - start);
    }
}
Also used : CachedRegionHelper(org.apache.geode.internal.cache.tier.CachedRegionHelper) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException) CacheServerStats(org.apache.geode.internal.cache.tier.sockets.CacheServerStats) Part(org.apache.geode.internal.cache.tier.sockets.Part) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) LocalRegion(org.apache.geode.internal.cache.LocalRegion) IOException(java.io.IOException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException)

Example 27 with CacheServerStats

use of org.apache.geode.internal.cache.tier.sockets.CacheServerStats in project geode by apache.

the class Put65 method cmdExecute.

@Override
public void cmdExecute(Message clientMessage, ServerConnection serverConnection, long p_start) throws IOException, InterruptedException {
    long start = p_start;
    Part regionNamePart = null, keyPart = null, valuePart = null, callbackArgPart = null;
    String regionName = null;
    Object callbackArg = null, key = null;
    Part eventPart = null;
    StringBuffer errMessage = new StringBuffer();
    boolean isDelta = false;
    CachedRegionHelper crHelper = serverConnection.getCachedRegionHelper();
    CacheServerStats stats = serverConnection.getCacheServerStats();
    // requiresResponse = true;
    serverConnection.setAsTrue(REQUIRES_RESPONSE);
    {
        long oldStart = start;
        start = DistributionStats.getStatTime();
        stats.incReadPutRequestTime(start - oldStart);
    }
    // Retrieve the data from the message parts
    int idx = 0;
    regionNamePart = clientMessage.getPart(idx++);
    Operation operation;
    try {
        operation = (Operation) clientMessage.getPart(idx++).getObject();
        if (operation == null) {
            // native clients send a null since the op is java-serialized
            operation = Operation.UPDATE;
        }
    } catch (ClassNotFoundException e) {
        writeException(clientMessage, e, false, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    int flags = clientMessage.getPart(idx++).getInt();
    boolean requireOldValue = ((flags & 0x01) == 0x01);
    boolean haveExpectedOldValue = ((flags & 0x02) == 0x02);
    Object expectedOldValue = null;
    if (haveExpectedOldValue) {
        try {
            expectedOldValue = clientMessage.getPart(idx++).getObject();
        } catch (ClassNotFoundException e) {
            writeException(clientMessage, e, false, serverConnection);
            serverConnection.setAsTrue(RESPONDED);
            return;
        }
    }
    keyPart = clientMessage.getPart(idx++);
    try {
        isDelta = ((Boolean) clientMessage.getPart(idx).getObject()).booleanValue();
        idx += 1;
    } catch (Exception e) {
        writeException(clientMessage, MessageType.PUT_DELTA_ERROR, e, false, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        // CachePerfStats not available here.
        return;
    }
    valuePart = clientMessage.getPart(idx++);
    eventPart = clientMessage.getPart(idx++);
    if (clientMessage.getNumberOfParts() > idx) {
        callbackArgPart = clientMessage.getPart(idx++);
        try {
            callbackArg = callbackArgPart.getObject();
        } catch (Exception e) {
            writeException(clientMessage, e, false, serverConnection);
            serverConnection.setAsTrue(RESPONDED);
            return;
        }
    }
    regionName = regionNamePart.getString();
    try {
        key = keyPart.getStringOrObject();
    } catch (Exception e) {
        writeException(clientMessage, e, false, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    final boolean isDebugEnabled = logger.isDebugEnabled();
    if (isDebugEnabled) {
        logger.debug("{}: Received {}put request ({} bytes) from {} for region {} key {} txId {} posdup: {}", serverConnection.getName(), (isDelta ? " delta " : " "), clientMessage.getPayloadLength(), serverConnection.getSocketString(), regionName, key, clientMessage.getTransactionId(), clientMessage.isRetry());
    }
    // Process the put request
    if (key == null || regionName == null) {
        if (key == null) {
            String putMsg = " The input key for the put request is null";
            if (isDebugEnabled) {
                logger.debug("{}:{}", serverConnection.getName(), putMsg);
            }
            errMessage.append(putMsg);
        }
        if (regionName == null) {
            String putMsg = " The input region name for the put request is null";
            if (isDebugEnabled) {
                logger.debug("{}:{}", serverConnection.getName(), putMsg);
            }
            errMessage.append(putMsg);
        }
        writeErrorResponse(clientMessage, MessageType.PUT_DATA_ERROR, errMessage.toString(), serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    LocalRegion region = (LocalRegion) serverConnection.getCache().getRegion(regionName);
    if (region == null) {
        String reason = " was not found during put request";
        writeRegionDestroyedEx(clientMessage, regionName, reason, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    if (valuePart.isNull() && operation != Operation.PUT_IF_ABSENT && region.containsKey(key)) {
        // Invalid to 'put' a null value in an existing key
        String putMsg = " Attempted to put a null value for existing key " + key;
        if (isDebugEnabled) {
            logger.debug("{}:{}", serverConnection.getName(), putMsg);
        }
        errMessage.append(putMsg);
        writeErrorResponse(clientMessage, MessageType.PUT_DATA_ERROR, errMessage.toString(), serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    ByteBuffer eventIdPartsBuffer = ByteBuffer.wrap(eventPart.getSerializedForm());
    long threadId = EventID.readEventIdPartsFromOptmizedByteArray(eventIdPartsBuffer);
    long sequenceId = EventID.readEventIdPartsFromOptmizedByteArray(eventIdPartsBuffer);
    EventIDHolder clientEvent = new EventIDHolder(new EventID(serverConnection.getEventMemberIDByteArray(), threadId, sequenceId));
    Breadcrumbs.setEventId(clientEvent.getEventId());
    // msg.isRetry might be set by v7.0 and later clients
    if (clientMessage.isRetry()) {
        // if (logger.isDebugEnabled()) {
        // logger.debug("DEBUG: encountered isRetry in Put65");
        // }
        clientEvent.setPossibleDuplicate(true);
        if (region.getAttributes().getConcurrencyChecksEnabled()) {
            // recover the version tag from other servers
            clientEvent.setRegion(region);
            if (!recoverVersionTagForRetriedOperation(clientEvent)) {
                // no-one has seen this event
                clientEvent.setPossibleDuplicate(false);
            }
        }
    }
    boolean result = false;
    boolean sendOldValue = false;
    boolean oldValueIsObject = true;
    Object oldValue = null;
    try {
        Object value = null;
        if (!isDelta) {
            value = valuePart.getSerializedForm();
        }
        boolean isObject = valuePart.isObject();
        boolean isMetaRegion = region.isUsedForMetaRegion();
        clientMessage.setMetaRegion(isMetaRegion);
        this.securityService.authorizeRegionWrite(regionName, key.toString());
        AuthorizeRequest authzRequest = null;
        if (!isMetaRegion) {
            authzRequest = serverConnection.getAuthzRequest();
        }
        if (authzRequest != null) {
            if (DynamicRegionFactory.regionIsDynamicRegionList(regionName)) {
                authzRequest.createRegionAuthorize((String) key);
            } else // Allow PUT operations on meta regions (bug #38961)
            {
                PutOperationContext putContext = authzRequest.putAuthorize(regionName, key, value, isObject, callbackArg);
                value = putContext.getValue();
                isObject = putContext.isObject();
                callbackArg = putContext.getCallbackArg();
            }
        }
        if (isDebugEnabled) {
            logger.debug("processing put65 with operation={}", operation);
        }
        // to be publicly accessible.
        if (operation == Operation.PUT_IF_ABSENT) {
            // try {
            if (clientMessage.isRetry() && clientEvent.getVersionTag() != null) {
                // version tag
                if (isDebugEnabled) {
                    logger.debug("putIfAbsent operation was successful last time with version {}", clientEvent.getVersionTag());
                }
                // invoke basicBridgePutIfAbsent anyway to ensure that the event is distributed to all
                // servers - bug #51664
                region.basicBridgePutIfAbsent(key, value, isObject, callbackArg, serverConnection.getProxyID(), true, clientEvent);
                oldValue = null;
            } else {
                oldValue = region.basicBridgePutIfAbsent(key, value, isObject, callbackArg, serverConnection.getProxyID(), true, clientEvent);
            }
            sendOldValue = true;
            oldValueIsObject = true;
            Version clientVersion = serverConnection.getClientVersion();
            if (oldValue instanceof CachedDeserializable) {
                oldValue = ((CachedDeserializable) oldValue).getSerializedValue();
            } else if (oldValue instanceof byte[]) {
                oldValueIsObject = false;
            } else if ((oldValue instanceof Token) && clientVersion.compareTo(Version.GFE_651) <= 0) {
                // older clients don't know that Token is now a DSFID class, so we
                // put the token in a serialized form they can consume
                HeapDataOutputStream str = new HeapDataOutputStream(Version.CURRENT);
                DataOutput dstr = new DataOutputStream(str);
                InternalDataSerializer.writeSerializableObject(oldValue, dstr);
                oldValue = str.toByteArray();
            }
            result = true;
        // } catch (Exception e) {
        // writeException(msg, e, false, servConn);
        // servConn.setAsTrue(RESPONDED);
        // return;
        // }
        } else if (operation == Operation.REPLACE) {
            // try {
            if (requireOldValue) {
                // <V> replace(<K>, <V>)
                if (clientMessage.isRetry() && clientEvent.isConcurrencyConflict() && clientEvent.getVersionTag() != null) {
                    if (isDebugEnabled) {
                        logger.debug("replace(k,v) operation was successful last time with version {}", clientEvent.getVersionTag());
                    }
                }
                oldValue = region.basicBridgeReplace(key, value, isObject, callbackArg, serverConnection.getProxyID(), true, clientEvent);
                sendOldValue = !clientEvent.isConcurrencyConflict();
                oldValueIsObject = true;
                Version clientVersion = serverConnection.getClientVersion();
                if (oldValue instanceof CachedDeserializable) {
                    oldValue = ((CachedDeserializable) oldValue).getSerializedValue();
                } else if (oldValue instanceof byte[]) {
                    oldValueIsObject = false;
                } else if ((oldValue instanceof Token) && clientVersion.compareTo(Version.GFE_651) <= 0) {
                    // older clients don't know that Token is now a DSFID class, so we
                    // put the token in a serialized form they can consume
                    HeapDataOutputStream str = new HeapDataOutputStream(Version.CURRENT);
                    DataOutput dstr = new DataOutputStream(str);
                    InternalDataSerializer.writeSerializableObject(oldValue, dstr);
                    oldValue = str.toByteArray();
                }
                if (isDebugEnabled) {
                    logger.debug("returning {} from replace(K,V)", oldValue);
                }
                result = true;
            } else {
                // boolean replace(<K>, <V>, <V>) {
                boolean didPut;
                didPut = region.basicBridgeReplace(key, expectedOldValue, value, isObject, callbackArg, serverConnection.getProxyID(), true, clientEvent);
                if (clientMessage.isRetry() && clientEvent.getVersionTag() != null) {
                    if (isDebugEnabled) {
                        logger.debug("replace(k,v,v) operation was successful last time with version {}", clientEvent.getVersionTag());
                    }
                    didPut = true;
                }
                sendOldValue = true;
                oldValueIsObject = true;
                oldValue = didPut ? Boolean.TRUE : Boolean.FALSE;
                if (isDebugEnabled) {
                    logger.debug("returning {} from replace(K,V,V)", oldValue);
                }
                result = true;
            }
        // } catch (Exception e) {
        // writeException(msg, e, false, servConn);
        // servConn.setAsTrue(RESPONDED);
        // return;
        // }
        } else if (value == null && !isDelta) {
            // Create the null entry. Since the value is null, the value of the
            // isObject
            // the true after null doesn't matter and is not used.
            result = region.basicBridgeCreate(key, null, true, callbackArg, serverConnection.getProxyID(), true, clientEvent, false);
            if (clientMessage.isRetry() && clientEvent.isConcurrencyConflict() && clientEvent.getVersionTag() != null) {
                result = true;
                if (isDebugEnabled) {
                    logger.debug("create(k,null) operation was successful last time with version {}", clientEvent.getVersionTag());
                }
            }
        } else {
            // Put the entry
            byte[] delta = null;
            if (isDelta) {
                delta = valuePart.getSerializedForm();
            }
            TXManagerImpl txMgr = (TXManagerImpl) serverConnection.getCache().getCacheTransactionManager();
            // bug 43068 - use create() if in a transaction and op is CREATE
            if (txMgr.getTXState() != null && operation.isCreate()) {
                result = region.basicBridgeCreate(key, (byte[]) value, isObject, callbackArg, serverConnection.getProxyID(), true, clientEvent, true);
            } else {
                result = region.basicBridgePut(key, value, delta, isObject, callbackArg, serverConnection.getProxyID(), true, clientEvent);
            }
            if (clientMessage.isRetry() && clientEvent.isConcurrencyConflict() && clientEvent.getVersionTag() != null) {
                if (isDebugEnabled) {
                    logger.debug("put(k,v) operation was successful last time with version {}", clientEvent.getVersionTag());
                }
                result = true;
            }
        }
        if (result) {
            serverConnection.setModificationInfo(true, regionName, key);
        } else {
            String message = serverConnection.getName() + ": Failed to put entry for region " + regionName + " key " + key + " value " + valuePart;
            if (isDebugEnabled) {
                logger.debug(message);
            }
            throw new Exception(message);
        }
    } catch (RegionDestroyedException rde) {
        writeException(clientMessage, rde, false, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    } catch (ResourceException re) {
        writeException(clientMessage, re, false, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    } catch (InvalidDeltaException ide) {
        logger.info(LocalizedMessage.create(LocalizedStrings.UpdateOperation_ERROR_APPLYING_DELTA_FOR_KEY_0_OF_REGION_1, new Object[] { key, regionName }));
        writeException(clientMessage, MessageType.PUT_DELTA_ERROR, ide, false, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        region.getCachePerfStats().incDeltaFullValuesRequested();
        return;
    } catch (Exception ce) {
        // If an interrupted exception is thrown , rethrow it
        checkForInterrupt(serverConnection, ce);
        // If an exception occurs during the put, preserve the connection
        writeException(clientMessage, ce, false, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        if (ce instanceof GemFireSecurityException) {
            // logged by the security logger
            if (isDebugEnabled) {
                logger.debug("{}: Unexpected Security exception", serverConnection.getName(), ce);
            }
        } else if (isDebugEnabled) {
            logger.debug("{}: Unexpected Exception", serverConnection.getName(), ce);
        }
        return;
    } finally {
        long oldStart = start;
        start = DistributionStats.getStatTime();
        stats.incProcessPutTime(start - oldStart);
    }
    // Increment statistics and write the reply
    if (region instanceof PartitionedRegion) {
        PartitionedRegion pr = (PartitionedRegion) region;
        if (pr.getNetworkHopType() != PartitionedRegion.NETWORK_HOP_NONE) {
            writeReplyWithRefreshMetadata(clientMessage, serverConnection, pr, sendOldValue, oldValueIsObject, oldValue, pr.getNetworkHopType(), clientEvent.getVersionTag());
            pr.clearNetworkHopData();
        } else {
            writeReply(clientMessage, serverConnection, sendOldValue, oldValueIsObject, oldValue, clientEvent.getVersionTag());
        }
    } else {
        writeReply(clientMessage, serverConnection, sendOldValue, oldValueIsObject, oldValue, clientEvent.getVersionTag());
    }
    serverConnection.setAsTrue(RESPONDED);
    if (isDebugEnabled) {
        logger.debug("{}: Sent put response back to {} for region {} key {} value {}", serverConnection.getName(), serverConnection.getSocketString(), regionName, key, valuePart);
    }
    stats.incWritePutResponseTime(DistributionStats.getStatTime() - start);
}
Also used : InvalidDeltaException(org.apache.geode.InvalidDeltaException) DataOutput(java.io.DataOutput) TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) AuthorizeRequest(org.apache.geode.internal.security.AuthorizeRequest) DataOutputStream(java.io.DataOutputStream) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) Token(org.apache.geode.internal.cache.Token) Operation(org.apache.geode.cache.Operation) LocalRegion(org.apache.geode.internal.cache.LocalRegion) CachedRegionHelper(org.apache.geode.internal.cache.tier.CachedRegionHelper) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException) Version(org.apache.geode.internal.Version) ResourceException(org.apache.geode.cache.ResourceException) CachedDeserializable(org.apache.geode.internal.cache.CachedDeserializable) EventIDHolder(org.apache.geode.internal.cache.EventIDHolder) ByteBuffer(java.nio.ByteBuffer) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException) InvalidDeltaException(org.apache.geode.InvalidDeltaException) IOException(java.io.IOException) ResourceException(org.apache.geode.cache.ResourceException) CacheServerStats(org.apache.geode.internal.cache.tier.sockets.CacheServerStats) Part(org.apache.geode.internal.cache.tier.sockets.Part) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) EventID(org.apache.geode.internal.cache.EventID) PutOperationContext(org.apache.geode.cache.operations.PutOperationContext)

Example 28 with CacheServerStats

use of org.apache.geode.internal.cache.tier.sockets.CacheServerStats in project geode by apache.

the class PutAll70 method cmdExecute.

@Override
public void cmdExecute(Message clientMessage, ServerConnection serverConnection, long startp) throws IOException, InterruptedException {
    // copy this since we need to modify it
    long start = startp;
    Part regionNamePart = null, numberOfKeysPart = null, keyPart = null, valuePart = null;
    String regionName = null;
    int numberOfKeys = 0;
    Object key = null;
    Part eventPart = null;
    boolean replyWithMetaData = false;
    VersionedObjectList response = null;
    StringBuffer errMessage = new StringBuffer();
    CachedRegionHelper crHelper = serverConnection.getCachedRegionHelper();
    CacheServerStats stats = serverConnection.getCacheServerStats();
    // requiresResponse = true;
    serverConnection.setAsTrue(REQUIRES_RESPONSE);
    {
        long oldStart = start;
        start = DistributionStats.getStatTime();
        stats.incReadPutAllRequestTime(start - oldStart);
    }
    try {
        // Retrieve the data from the message parts
        // part 0: region name
        regionNamePart = clientMessage.getPart(0);
        regionName = regionNamePart.getString();
        if (regionName == null) {
            String putAllMsg = LocalizedStrings.PutAll_THE_INPUT_REGION_NAME_FOR_THE_PUTALL_REQUEST_IS_NULL.toLocalizedString();
            logger.warn("{}: {}", serverConnection.getName(), putAllMsg);
            errMessage.append(putAllMsg);
            writeErrorResponse(clientMessage, MessageType.PUT_DATA_ERROR, errMessage.toString(), serverConnection);
            serverConnection.setAsTrue(RESPONDED);
            return;
        }
        LocalRegion region = (LocalRegion) crHelper.getRegion(regionName);
        if (region == null) {
            String reason = " was not found during put request";
            writeRegionDestroyedEx(clientMessage, regionName, reason, serverConnection);
            serverConnection.setAsTrue(RESPONDED);
            return;
        }
        // part 1: eventID
        eventPart = clientMessage.getPart(1);
        ByteBuffer eventIdPartsBuffer = ByteBuffer.wrap(eventPart.getSerializedForm());
        long threadId = EventID.readEventIdPartsFromOptmizedByteArray(eventIdPartsBuffer);
        long sequenceId = EventID.readEventIdPartsFromOptmizedByteArray(eventIdPartsBuffer);
        EventID eventId = new EventID(serverConnection.getEventMemberIDByteArray(), threadId, sequenceId);
        // part 2: invoke callbacks (used by import)
        Part callbacksPart = clientMessage.getPart(2);
        boolean skipCallbacks = callbacksPart.getInt() == 1 ? true : false;
        // part 3: number of keys
        numberOfKeysPart = clientMessage.getPart(3);
        numberOfKeys = numberOfKeysPart.getInt();
        // building the map
        Map map = new LinkedHashMap();
        Map<Object, VersionTag> retryVersions = new LinkedHashMap<Object, VersionTag>();
        // Map isObjectMap = new LinkedHashMap();
        for (int i = 0; i < numberOfKeys; i++) {
            keyPart = clientMessage.getPart(4 + i * 2);
            key = keyPart.getStringOrObject();
            if (key == null) {
                String putAllMsg = LocalizedStrings.PutAll_ONE_OF_THE_INPUT_KEYS_FOR_THE_PUTALL_REQUEST_IS_NULL.toLocalizedString();
                logger.warn("{}: {}", serverConnection.getName(), putAllMsg);
                errMessage.append(putAllMsg);
                writeErrorResponse(clientMessage, MessageType.PUT_DATA_ERROR, errMessage.toString(), serverConnection);
                serverConnection.setAsTrue(RESPONDED);
                return;
            }
            valuePart = clientMessage.getPart(4 + i * 2 + 1);
            if (valuePart.isNull()) {
                String putAllMsg = LocalizedStrings.PutAll_ONE_OF_THE_INPUT_VALUES_FOR_THE_PUTALL_REQUEST_IS_NULL.toLocalizedString();
                logger.warn("{}: {}", serverConnection.getName(), putAllMsg);
                errMessage.append(putAllMsg);
                writeErrorResponse(clientMessage, MessageType.PUT_DATA_ERROR, errMessage.toString(), serverConnection);
                serverConnection.setAsTrue(RESPONDED);
                return;
            }
            // byte[] value = valuePart.getSerializedForm();
            Object value;
            if (valuePart.isObject()) {
                // skipCallbacks configurable this code will need to be updated.
                if (skipCallbacks && Token.INVALID.isSerializedValue(valuePart.getSerializedForm())) {
                    value = Token.INVALID;
                } else {
                    value = CachedDeserializableFactory.create(valuePart.getSerializedForm());
                }
            } else {
                value = valuePart.getSerializedForm();
            }
            // put serializedform for auth. It will be modified with auth callback
            if (clientMessage.isRetry()) {
                // Constuct the thread id/sequence id information for this element in the
                // put all map
                // The sequence id is constructed from the base sequence id and the offset
                EventID entryEventId = new EventID(eventId, i);
                // For PRs, the thread id assigned as a fake thread id.
                if (region instanceof PartitionedRegion) {
                    PartitionedRegion pr = (PartitionedRegion) region;
                    int bucketId = pr.getKeyInfo(key).getBucketId();
                    long entryThreadId = ThreadIdentifier.createFakeThreadIDForBulkOp(bucketId, entryEventId.getThreadID());
                    entryEventId = new EventID(entryEventId.getMembershipID(), entryThreadId, entryEventId.getSequenceID());
                }
                VersionTag tag = findVersionTagsForRetriedBulkOp(region, entryEventId);
                if (tag != null) {
                    retryVersions.put(key, tag);
                }
            // FIND THE VERSION TAG FOR THIS KEY - but how? all we have is the
            // putAll eventId, not individual eventIds for entries, right?
            }
            map.put(key, value);
        // isObjectMap.put(key, new Boolean(isObject));
        }
        if (clientMessage.getNumberOfParts() == (4 + 2 * numberOfKeys + 1)) {
            // it means optional
            // timeout has
            // been added
            int timeout = clientMessage.getPart(4 + 2 * numberOfKeys).getInt();
            serverConnection.setRequestSpecificTimeout(timeout);
        }
        this.securityService.authorizeRegionWrite(regionName);
        AuthorizeRequest authzRequest = serverConnection.getAuthzRequest();
        if (authzRequest != null) {
            if (DynamicRegionFactory.regionIsDynamicRegionList(regionName)) {
                authzRequest.createRegionAuthorize(regionName);
            } else {
                PutAllOperationContext putAllContext = authzRequest.putAllAuthorize(regionName, map, null);
                map = putAllContext.getMap();
                if (map instanceof UpdateOnlyMap) {
                    map = ((UpdateOnlyMap) map).getInternalMap();
                }
            }
        } else {
        // no auth, so update the map based on isObjectMap here
        /*
         * Collection entries = map.entrySet(); Iterator iterator = entries.iterator(); Map.Entry
         * mapEntry = null; while (iterator.hasNext()) { mapEntry = (Map.Entry)iterator.next();
         * Object currkey = mapEntry.getKey(); byte[] serializedValue = (byte[])mapEntry.getValue();
         * boolean isObject = ((Boolean)isObjectMap.get(currkey)).booleanValue(); if (isObject) {
         * map.put(currkey, CachedDeserializableFactory.create(serializedValue)); } }
         */
        }
        if (logger.isDebugEnabled()) {
            logger.debug("{}: Received putAll request ({} bytes) from {} for region {}", serverConnection.getName(), clientMessage.getPayloadLength(), serverConnection.getSocketString(), regionName);
        }
        response = region.basicBridgePutAll(map, retryVersions, serverConnection.getProxyID(), eventId, skipCallbacks, null);
        if (!region.getConcurrencyChecksEnabled()) {
            // the client only needs this if versioning is being used
            response = null;
        }
        if (region instanceof PartitionedRegion) {
            PartitionedRegion pr = (PartitionedRegion) region;
            if (pr.getNetworkHopType() != PartitionedRegion.NETWORK_HOP_NONE) {
                writeReplyWithRefreshMetadata(clientMessage, response, serverConnection, pr, pr.getNetworkHopType());
                pr.clearNetworkHopData();
                replyWithMetaData = true;
            }
        }
    } catch (RegionDestroyedException rde) {
        writeException(clientMessage, rde, false, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    } catch (ResourceException re) {
        writeException(clientMessage, re, false, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    } catch (PutAllPartialResultException pre) {
        writeException(clientMessage, pre, false, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    } catch (Exception ce) {
        // If an interrupted exception is thrown , rethrow it
        checkForInterrupt(serverConnection, ce);
        // If an exception occurs during the put, preserve the connection
        writeException(clientMessage, ce, false, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        // if (logger.fineEnabled()) {
        logger.warn(LocalizedMessage.create(LocalizedStrings.Generic_0_UNEXPECTED_EXCEPTION, serverConnection.getName()), ce);
        // }
        return;
    } finally {
        long oldStart = start;
        start = DistributionStats.getStatTime();
        stats.incProcessPutAllTime(start - oldStart);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("{}: Sending putAll70 response back to {} for region {}: {}", serverConnection.getName(), serverConnection.getSocketString(), regionName, response);
    }
    // Starting in 7.0.1 we do not send the keys back
    if (response != null && Version.GFE_70.compareTo(serverConnection.getClientVersion()) < 0) {
        if (logger.isDebugEnabled()) {
            logger.debug("setting putAll keys to null");
        }
        response.setKeys(null);
    }
    // Increment statistics and write the reply
    if (!replyWithMetaData) {
        writeReply(clientMessage, response, serverConnection);
    }
    serverConnection.setAsTrue(RESPONDED);
    stats.incWritePutAllResponseTime(DistributionStats.getStatTime() - start);
}
Also used : UpdateOnlyMap(org.apache.geode.cache.operations.internal.UpdateOnlyMap) AuthorizeRequest(org.apache.geode.internal.security.AuthorizeRequest) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) VersionedObjectList(org.apache.geode.internal.cache.tier.sockets.VersionedObjectList) LocalRegion(org.apache.geode.internal.cache.LocalRegion) LinkedHashMap(java.util.LinkedHashMap) PutAllPartialResultException(org.apache.geode.internal.cache.PutAllPartialResultException) CachedRegionHelper(org.apache.geode.internal.cache.tier.CachedRegionHelper) PutAllOperationContext(org.apache.geode.cache.operations.PutAllOperationContext) VersionTag(org.apache.geode.internal.cache.versions.VersionTag) ResourceException(org.apache.geode.cache.ResourceException) ByteBuffer(java.nio.ByteBuffer) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) PutAllPartialResultException(org.apache.geode.internal.cache.PutAllPartialResultException) IOException(java.io.IOException) ResourceException(org.apache.geode.cache.ResourceException) CacheServerStats(org.apache.geode.internal.cache.tier.sockets.CacheServerStats) Part(org.apache.geode.internal.cache.tier.sockets.Part) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) EventID(org.apache.geode.internal.cache.EventID) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) UpdateOnlyMap(org.apache.geode.cache.operations.internal.UpdateOnlyMap)

Example 29 with CacheServerStats

use of org.apache.geode.internal.cache.tier.sockets.CacheServerStats in project geode by apache.

the class DestroyRegion method cmdExecute.

@Override
public void cmdExecute(Message clientMessage, ServerConnection serverConnection, long start) throws IOException, InterruptedException {
    Part regionNamePart = null, callbackArgPart = null;
    String regionName = null;
    Object callbackArg = null;
    Part eventPart = null;
    StringBuffer errMessage = new StringBuffer();
    CacheServerStats stats = serverConnection.getCacheServerStats();
    serverConnection.setAsTrue(REQUIRES_RESPONSE);
    {
        long oldStart = start;
        start = DistributionStats.getStatTime();
        stats.incReadDestroyRegionRequestTime(start - oldStart);
    }
    // Retrieve the data from the message parts
    regionNamePart = clientMessage.getPart(0);
    eventPart = clientMessage.getPart(1);
    // callbackArgPart = null; (redundant assignment)
    if (clientMessage.getNumberOfParts() > 2) {
        callbackArgPart = clientMessage.getPart(2);
        try {
            callbackArg = callbackArgPart.getObject();
        } catch (DistributedSystemDisconnectedException se) {
            // FIXME this can't happen
            if (logger.isDebugEnabled()) {
                logger.debug("{} ignoring message of type {} from client {} because shutdown occurred during message processing.", serverConnection.getName(), MessageType.getString(clientMessage.getMessageType()), serverConnection.getProxyID());
            }
            serverConnection.setFlagProcessMessagesAsFalse();
            serverConnection.setClientDisconnectedException(se);
            return;
        } catch (Exception e) {
            writeException(clientMessage, e, false, serverConnection);
            serverConnection.setAsTrue(RESPONDED);
            return;
        }
    }
    regionName = regionNamePart.getString();
    if (logger.isDebugEnabled()) {
        logger.debug("{}: Received destroy region request ({} bytes) from {} for region {}", serverConnection.getName(), clientMessage.getPayloadLength(), serverConnection.getSocketString(), regionName);
    }
    // Process the destroy region request
    if (regionName == null) {
        logger.warn(LocalizedMessage.create(LocalizedStrings.DestroyRegion_0_THE_INPUT_REGION_NAME_FOR_THE_DESTROY_REGION_REQUEST_IS_NULL, serverConnection.getName()));
        errMessage.append(LocalizedStrings.DestroyRegion__THE_INPUT_REGION_NAME_FOR_THE_DESTROY_REGION_REQUEST_IS_NULL.toLocalizedString());
        writeErrorResponse(clientMessage, MessageType.DESTROY_REGION_DATA_ERROR, errMessage.toString(), serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    LocalRegion region = (LocalRegion) serverConnection.getCache().getRegion(regionName);
    if (region == null) {
        String reason = LocalizedStrings.DestroyRegion_REGION_WAS_NOT_FOUND_DURING_DESTROY_REGION_REQUEST.toLocalizedString();
        writeRegionDestroyedEx(clientMessage, regionName, reason, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    // Destroy the region
    ByteBuffer eventIdPartsBuffer = ByteBuffer.wrap(eventPart.getSerializedForm());
    long threadId = EventID.readEventIdPartsFromOptmizedByteArray(eventIdPartsBuffer);
    long sequenceId = EventID.readEventIdPartsFromOptmizedByteArray(eventIdPartsBuffer);
    EventID eventId = new EventID(serverConnection.getEventMemberIDByteArray(), threadId, sequenceId);
    try {
        // user needs to have data:manage on all regions in order to destory a particular region
        this.securityService.authorizeDataManage();
        AuthorizeRequest authzRequest = serverConnection.getAuthzRequest();
        if (authzRequest != null) {
            RegionDestroyOperationContext destroyContext = authzRequest.destroyRegionAuthorize(regionName, callbackArg);
            callbackArg = destroyContext.getCallbackArg();
        }
        // region.destroyRegion(callbackArg);
        region.basicBridgeDestroyRegion(callbackArg, serverConnection.getProxyID(), true, /* boolean from cache Client */
        eventId);
    } catch (DistributedSystemDisconnectedException e) {
        // FIXME better exception hierarchy would avoid this check
        if (serverConnection.getCachedRegionHelper().getCache().getCancelCriterion().cancelInProgress() != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("{} ignoring message of type {} from client {} because shutdown occurred during message processing.", serverConnection.getName(), MessageType.getString(clientMessage.getMessageType()), serverConnection.getProxyID());
            }
            serverConnection.setFlagProcessMessagesAsFalse();
            serverConnection.setClientDisconnectedException(e);
        } else {
            writeException(clientMessage, e, false, serverConnection);
            serverConnection.setAsTrue(RESPONDED);
        }
        return;
    } catch (Exception e) {
        // If an interrupted exception is thrown , rethrow it
        checkForInterrupt(serverConnection, e);
        // Otherwise, write an exception message and continue
        writeException(clientMessage, e, false, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    // Update the statistics and write the reply
    {
        long oldStart = start;
        start = DistributionStats.getStatTime();
        stats.incProcessDestroyRegionTime(start - oldStart);
    }
    writeReply(clientMessage, serverConnection);
    serverConnection.setAsTrue(RESPONDED);
    if (logger.isDebugEnabled()) {
        logger.debug("{}: Sent destroy region response for region {}", serverConnection.getName(), regionName);
    }
    stats.incWriteDestroyRegionResponseTime(DistributionStats.getStatTime() - start);
}
Also used : DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException) AuthorizeRequest(org.apache.geode.internal.security.AuthorizeRequest) LocalRegion(org.apache.geode.internal.cache.LocalRegion) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException) RegionDestroyOperationContext(org.apache.geode.cache.operations.RegionDestroyOperationContext) CacheServerStats(org.apache.geode.internal.cache.tier.sockets.CacheServerStats) Part(org.apache.geode.internal.cache.tier.sockets.Part) EventID(org.apache.geode.internal.cache.EventID)

Example 30 with CacheServerStats

use of org.apache.geode.internal.cache.tier.sockets.CacheServerStats in project geode by apache.

the class ContainsKey method cmdExecute.

@Override
public void cmdExecute(Message clientMessage, ServerConnection serverConnection, long start) throws IOException {
    Part regionNamePart = null;
    Part keyPart = null;
    String regionName = null;
    Object key = null;
    CacheServerStats stats = serverConnection.getCacheServerStats();
    serverConnection.setAsTrue(REQUIRES_RESPONSE);
    {
        long oldStart = start;
        start = DistributionStats.getStatTime();
        stats.incReadContainsKeyRequestTime(start - oldStart);
    }
    // Retrieve the data from the message parts
    regionNamePart = clientMessage.getPart(0);
    keyPart = clientMessage.getPart(1);
    regionName = regionNamePart.getString();
    try {
        key = keyPart.getStringOrObject();
    } catch (Exception e) {
        writeException(clientMessage, e, false, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    if (logger.isDebugEnabled()) {
        logger.debug("{}: Received containsKey request ({} bytes) from {} for region {} key {}", serverConnection.getName(), clientMessage.getPayloadLength(), serverConnection.getSocketString(), regionName, key);
    }
    // Process the containsKey request
    if (key == null || regionName == null) {
        String errMessage = "";
        if (key == null) {
            logger.warn(LocalizedMessage.create(LocalizedStrings.ContainsKey_0_THE_INPUT_KEY_FOR_THE_CONTAINSKEY_REQUEST_IS_NULL, serverConnection.getName()));
            errMessage = LocalizedStrings.ContainsKey_THE_INPUT_KEY_FOR_THE_CONTAINSKEY_REQUEST_IS_NULL.toLocalizedString();
        }
        if (regionName == null) {
            logger.warn(LocalizedMessage.create(LocalizedStrings.ContainsKey_0_THE_INPUT_REGION_NAME_FOR_THE_CONTAINSKEY_REQUEST_IS_NULL, serverConnection.getName()));
            errMessage = LocalizedStrings.ContainsKey_THE_INPUT_REGION_NAME_FOR_THE_CONTAINSKEY_REQUEST_IS_NULL.toLocalizedString();
        }
        writeErrorResponse(clientMessage, MessageType.CONTAINS_KEY_DATA_ERROR, errMessage, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    LocalRegion region = (LocalRegion) serverConnection.getCache().getRegion(regionName);
    if (region == null) {
        String reason = LocalizedStrings.ContainsKey_WAS_NOT_FOUND_DURING_CONTAINSKEY_REQUEST.toLocalizedString();
        writeRegionDestroyedEx(clientMessage, regionName, reason, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    try {
        this.securityService.authorizeRegionRead(regionName, key.toString());
    } catch (NotAuthorizedException ex) {
        writeException(clientMessage, ex, false, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    AuthorizeRequest authzRequest = serverConnection.getAuthzRequest();
    if (authzRequest != null) {
        try {
            authzRequest.containsKeyAuthorize(regionName, key);
        } catch (NotAuthorizedException ex) {
            writeException(clientMessage, ex, false, serverConnection);
            serverConnection.setAsTrue(RESPONDED);
            return;
        }
    }
    // Execute the containsKey
    boolean containsKey = region.containsKey(key);
    // Update the statistics and write the reply
    {
        long oldStart = start;
        start = DistributionStats.getStatTime();
        stats.incProcessContainsKeyTime(start - oldStart);
    }
    writeContainsKeyResponse(containsKey, clientMessage, serverConnection);
    serverConnection.setAsTrue(RESPONDED);
    if (logger.isDebugEnabled()) {
        logger.debug("{}: Sent containsKey response for region {} key {}", serverConnection.getName(), regionName, key);
    }
    stats.incWriteContainsKeyResponseTime(DistributionStats.getStatTime() - start);
}
Also used : CacheServerStats(org.apache.geode.internal.cache.tier.sockets.CacheServerStats) AuthorizeRequest(org.apache.geode.internal.security.AuthorizeRequest) Part(org.apache.geode.internal.cache.tier.sockets.Part) LocalRegion(org.apache.geode.internal.cache.LocalRegion) NotAuthorizedException(org.apache.geode.security.NotAuthorizedException) IOException(java.io.IOException) NotAuthorizedException(org.apache.geode.security.NotAuthorizedException)

Aggregations

CacheServerStats (org.apache.geode.internal.cache.tier.sockets.CacheServerStats)31 IOException (java.io.IOException)23 Part (org.apache.geode.internal.cache.tier.sockets.Part)21 AuthorizeRequest (org.apache.geode.internal.security.AuthorizeRequest)21 LocalRegion (org.apache.geode.internal.cache.LocalRegion)18 CachedRegionHelper (org.apache.geode.internal.cache.tier.CachedRegionHelper)18 ByteBuffer (java.nio.ByteBuffer)12 EventID (org.apache.geode.internal.cache.EventID)12 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)11 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)11 ResourceException (org.apache.geode.cache.ResourceException)7 GemFireSecurityException (org.apache.geode.security.GemFireSecurityException)7 EventIDHolder (org.apache.geode.internal.cache.EventIDHolder)6 CqException (org.apache.geode.cache.query.CqException)5 CqService (org.apache.geode.cache.query.internal.cq.CqService)5 ClientProxyMembershipID (org.apache.geode.internal.cache.tier.sockets.ClientProxyMembershipID)5 VersionTag (org.apache.geode.internal.cache.versions.VersionTag)5 Set (java.util.Set)4 GatewayReceiver (org.apache.geode.cache.wan.GatewayReceiver)4 PutAllPartialResultException (org.apache.geode.internal.cache.PutAllPartialResultException)4