Search in sources :

Example 56 with VersionTag

use of org.apache.geode.internal.cache.versions.VersionTag in project geode by apache.

the class GatewayReceiverCommand method cmdExecute.

@Override
public void cmdExecute(Message clientMessage, ServerConnection serverConnection, long start) throws IOException, InterruptedException {
    Part regionNamePart = null, keyPart = null, valuePart = null, callbackArgPart = null;
    String regionName = null;
    Object callbackArg = null, key = null;
    int partNumber = 0;
    CachedRegionHelper crHelper = serverConnection.getCachedRegionHelper();
    GatewayReceiverStats stats = (GatewayReceiverStats) serverConnection.getCacheServerStats();
    EventID eventId = null;
    LocalRegion region = null;
    List<BatchException70> exceptions = new ArrayList<BatchException70>();
    Throwable fatalException = null;
    // requiresResponse = true;// let PROCESS_BATCH deal with this itself
    {
        long oldStart = start;
        start = DistributionStats.getStatTime();
        stats.incReadProcessBatchRequestTime(start - oldStart);
    }
    Part callbackArgExistsPart;
    // Get early ack flag. This test should eventually be moved up above this switch
    // statement so that all messages can take advantage of it.
    // msg.getEarlyAck();
    boolean earlyAck = false;
    stats.incBatchSize(clientMessage.getPayloadLength());
    // Retrieve the number of events
    Part numberOfEventsPart = clientMessage.getPart(0);
    int numberOfEvents = numberOfEventsPart.getInt();
    stats.incEventsReceived(numberOfEvents);
    // Retrieve the batch id
    Part batchIdPart = clientMessage.getPart(1);
    int batchId = batchIdPart.getInt();
    // Instead, drop the batch and continue.
    if (batchId <= serverConnection.getLatestBatchIdReplied()) {
        if (GatewayReceiver.APPLY_RETRIES) {
            // Do nothing!!!
            logger.warn(LocalizedMessage.create(LocalizedStrings.ProcessBatch_RECEIVED_PROCESS_BATCH_REQUEST_0_THAT_HAS_ALREADY_BEEN_OR_IS_BEING_PROCESSED_GEMFIRE_GATEWAY_APPLYRETRIES_IS_SET_SO_THIS_BATCH_WILL_BE_PROCESSED_ANYWAY, batchId));
        } else {
            logger.warn(LocalizedMessage.create(LocalizedStrings.ProcessBatch_RECEIVED_PROCESS_BATCH_REQUEST_0_THAT_HAS_ALREADY_BEEN_OR_IS_BEING_PROCESSED__THIS_PROCESS_BATCH_REQUEST_IS_BEING_IGNORED, batchId));
            writeReply(clientMessage, serverConnection, batchId, numberOfEvents);
            return;
        }
        stats.incDuplicateBatchesReceived();
    }
    // Verify the batches arrive in order
    if (batchId != serverConnection.getLatestBatchIdReplied() + 1) {
        logger.warn(LocalizedMessage.create(LocalizedStrings.ProcessBatch_RECEIVED_PROCESS_BATCH_REQUEST_0_OUT_OF_ORDER_THE_ID_OF_THE_LAST_BATCH_PROCESSED_WAS_1_THIS_BATCH_REQUEST_WILL_BE_PROCESSED_BUT_SOME_MESSAGES_MAY_HAVE_BEEN_LOST, new Object[] { batchId, serverConnection.getLatestBatchIdReplied() }));
        stats.incOutoforderBatchesReceived();
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Received process batch request {} that will be processed.", batchId);
    }
    // Not sure if earlyAck makes sense with sliding window
    if (earlyAck) {
        serverConnection.incrementLatestBatchIdReplied(batchId);
        // writeReply(msg, servConn);
        // servConn.setAsTrue(RESPONDED);
        {
            long oldStart = start;
            start = DistributionStats.getStatTime();
            stats.incWriteProcessBatchResponseTime(start - oldStart);
        }
        stats.incEarlyAcks();
    }
    if (logger.isDebugEnabled()) {
        logger.debug("{}: Received process batch request {} containing {} events ({} bytes) with {} acknowledgement on {}", serverConnection.getName(), batchId, numberOfEvents, clientMessage.getPayloadLength(), (earlyAck ? "early" : "normal"), serverConnection.getSocketString());
        if (earlyAck) {
            logger.debug("{}: Sent process batch early response for batch {} containing {} events ({} bytes) with {} acknowledgement on {}", serverConnection.getName(), batchId, numberOfEvents, clientMessage.getPayloadLength(), (earlyAck ? "early" : "normal"), serverConnection.getSocketString());
        }
    }
    // logger.warn("Received process batch request " + batchId + " containing
    // " + numberOfEvents + " events (" + msg.getPayloadLength() + " bytes) with
    // " + (earlyAck ? "early" : "normal") + " acknowledgement on " +
    // getSocketString());
    // if (earlyAck) {
    // logger.warn("Sent process batch early response for batch " + batchId +
    // " containing " + numberOfEvents + " events (" + msg.getPayloadLength() +
    // " bytes) with " + (earlyAck ? "early" : "normal") + " acknowledgement on
    // " + getSocketString());
    // }
    // Retrieve the events from the message parts. The '2' below
    // represents the number of events (part0) and the batchId (part1)
    partNumber = 2;
    int dsid = clientMessage.getPart(partNumber++).getInt();
    boolean removeOnException = clientMessage.getPart(partNumber++).getSerializedForm()[0] == 1 ? true : false;
    // Keep track of whether a response has been written for
    // exceptions
    boolean wroteResponse = earlyAck;
    // event received in batch also have PDX events at the start of the batch,to
    // represent correct index on which the exception occurred, number of PDX
    // events need to be subtratced.
    //
    int indexWithoutPDXEvent = -1;
    for (int i = 0; i < numberOfEvents; i++) {
        boolean isPdxEvent = false;
        indexWithoutPDXEvent++;
        // System.out.println("Processing event " + i + " in batch " + batchId + "
        // starting with part number " + partNumber);
        Part actionTypePart = clientMessage.getPart(partNumber);
        int actionType = actionTypePart.getInt();
        long versionTimeStamp = VersionTag.ILLEGAL_VERSION_TIMESTAMP;
        EventIDHolder clientEvent = null;
        boolean callbackArgExists = false;
        try {
            Part possibleDuplicatePart = clientMessage.getPart(partNumber + 1);
            byte[] possibleDuplicatePartBytes;
            try {
                possibleDuplicatePartBytes = (byte[]) possibleDuplicatePart.getObject();
            } catch (Exception e) {
                logger.warn(LocalizedMessage.create(LocalizedStrings.ProcessBatch_0_CAUGHT_EXCEPTION_PROCESSING_BATCH_REQUEST_1_CONTAINING_2_EVENTS, new Object[] { serverConnection.getName(), Integer.valueOf(batchId), Integer.valueOf(numberOfEvents) }), e);
                throw e;
            }
            boolean possibleDuplicate = possibleDuplicatePartBytes[0] == 0x01;
            // Make sure instance variables are null before each iteration
            regionName = null;
            key = null;
            callbackArg = null;
            // Retrieve the region name from the message parts
            regionNamePart = clientMessage.getPart(partNumber + 2);
            regionName = regionNamePart.getString();
            if (regionName.equals(PeerTypeRegistration.REGION_FULL_PATH)) {
                indexWithoutPDXEvent--;
                isPdxEvent = true;
            }
            // Retrieve the event id from the message parts
            // This was going to be used to determine possible
            // duplication of events, but it is unused now. In
            // fact the event id is overridden by the FROM_GATEWAY
            // token.
            Part eventIdPart = clientMessage.getPart(partNumber + 3);
            eventIdPart.setVersion(serverConnection.getClientVersion());
            // String eventId = eventIdPart.getString();
            try {
                eventId = (EventID) eventIdPart.getObject();
            } catch (Exception e) {
                logger.warn(LocalizedMessage.create(LocalizedStrings.ProcessBatch_0_CAUGHT_EXCEPTION_PROCESSING_BATCH_REQUEST_1_CONTAINING_2_EVENTS, new Object[] { serverConnection.getName(), Integer.valueOf(batchId), Integer.valueOf(numberOfEvents) }), e);
                throw e;
            }
            // Retrieve the key from the message parts
            keyPart = clientMessage.getPart(partNumber + 4);
            try {
                key = keyPart.getStringOrObject();
            } catch (Exception e) {
                logger.warn(LocalizedMessage.create(LocalizedStrings.ProcessBatch_0_CAUGHT_EXCEPTION_PROCESSING_BATCH_REQUEST_1_CONTAINING_2_EVENTS, new Object[] { serverConnection.getName(), Integer.valueOf(batchId), Integer.valueOf(numberOfEvents) }), e);
                throw e;
            }
            switch(actionType) {
                case // Create
                0:
                    /*
             * CLIENT EXCEPTION HANDLING TESTING CODE String keySt = (String) key;
             * System.out.println("Processing new key: " + key); if (keySt.startsWith("failure")) {
             * throw new Exception(LocalizedStrings
             * .ProcessBatch_THIS_EXCEPTION_REPRESENTS_A_FAILURE_ON_THE_SERVER
             * .toLocalizedString()); }
             */
                    // Retrieve the value from the message parts (do not deserialize it)
                    valuePart = clientMessage.getPart(partNumber + 5);
                    // try {
                    // logger.warn(getName() + ": Creating key " + key + " value " +
                    // valuePart.getObject());
                    // } catch (Exception e) {}
                    // Retrieve the callbackArg from the message parts if necessary
                    int index = partNumber + 6;
                    callbackArgExistsPart = clientMessage.getPart(index++);
                    {
                        byte[] partBytes = (byte[]) callbackArgExistsPart.getObject();
                        callbackArgExists = partBytes[0] == 0x01;
                    }
                    if (callbackArgExists) {
                        callbackArgPart = clientMessage.getPart(index++);
                        try {
                            callbackArg = callbackArgPart.getObject();
                        } catch (Exception e) {
                            logger.warn(LocalizedMessage.create(LocalizedStrings.ProcessBatch_0_CAUGHT_EXCEPTION_PROCESSING_BATCH_CREATE_REQUEST_1_FOR_2_EVENTS, new Object[] { serverConnection.getName(), Integer.valueOf(batchId), Integer.valueOf(numberOfEvents) }), e);
                            throw e;
                        }
                    }
                    if (logger.isDebugEnabled()) {
                        logger.debug("{}: Processing batch create request {} on {} for region {} key {} value {} callbackArg {}, eventId={}", serverConnection.getName(), batchId, serverConnection.getSocketString(), regionName, key, valuePart, callbackArg, eventId);
                    }
                    versionTimeStamp = clientMessage.getPart(index++).getLong();
                    // Process the create request
                    if (key == null || regionName == null) {
                        StringId message = null;
                        Object[] messageArgs = new Object[] { serverConnection.getName(), Integer.valueOf(batchId) };
                        if (key == null) {
                            message = LocalizedStrings.ProcessBatch_0_THE_INPUT_REGION_NAME_FOR_THE_BATCH_CREATE_REQUEST_1_IS_NULL;
                        }
                        if (regionName == null) {
                            message = LocalizedStrings.ProcessBatch_0_THE_INPUT_REGION_NAME_FOR_THE_BATCH_CREATE_REQUEST_1_IS_NULL;
                        }
                        String s = message.toLocalizedString(messageArgs);
                        logger.warn(s);
                        throw new Exception(s);
                    }
                    region = (LocalRegion) crHelper.getRegion(regionName);
                    if (region == null) {
                        handleRegionNull(serverConnection, regionName, batchId);
                    } else {
                        clientEvent = new EventIDHolder(eventId);
                        if (versionTimeStamp > 0) {
                            VersionTag tag = VersionTag.create(region.getVersionMember());
                            tag.setIsGatewayTag(true);
                            tag.setVersionTimeStamp(versionTimeStamp);
                            tag.setDistributedSystemId(dsid);
                            clientEvent.setVersionTag(tag);
                        }
                        clientEvent.setPossibleDuplicate(possibleDuplicate);
                        handleMessageRetry(region, clientEvent);
                        try {
                            byte[] value = valuePart.getSerializedForm();
                            boolean isObject = valuePart.isObject();
                            // [sumedh] This should be done on client while sending
                            // since that is the WAN gateway
                            AuthorizeRequest authzRequest = serverConnection.getAuthzRequest();
                            if (authzRequest != null) {
                                PutOperationContext putContext = authzRequest.putAuthorize(regionName, key, value, isObject, callbackArg);
                                value = putContext.getSerializedValue();
                                isObject = putContext.isObject();
                            }
                            // Attempt to create the entry
                            boolean result = false;
                            if (isPdxEvent) {
                                result = addPdxType(crHelper, key, value);
                            } else {
                                result = region.basicBridgeCreate(key, value, isObject, callbackArg, serverConnection.getProxyID(), false, clientEvent, false);
                                // attempt to update the entry
                                if (!result) {
                                    result = region.basicBridgePut(key, value, null, isObject, callbackArg, serverConnection.getProxyID(), false, clientEvent);
                                }
                            }
                            if (result || clientEvent.isConcurrencyConflict()) {
                                serverConnection.setModificationInfo(true, regionName, key);
                                stats.incCreateRequest();
                            } else {
                                // This exception will be logged in the catch block below
                                throw new Exception(LocalizedStrings.ProcessBatch_0_FAILED_TO_CREATE_OR_UPDATE_ENTRY_FOR_REGION_1_KEY_2_VALUE_3_CALLBACKARG_4.toLocalizedString(new Object[] { serverConnection.getName(), regionName, key, valuePart, callbackArg }));
                            }
                        } catch (Exception e) {
                            logger.warn(LocalizedMessage.create(LocalizedStrings.ProcessBatch_0_CAUGHT_EXCEPTION_PROCESSING_BATCH_CREATE_REQUEST_1_FOR_2_EVENTS, new Object[] { serverConnection.getName(), Integer.valueOf(batchId), Integer.valueOf(numberOfEvents) }), e);
                            throw e;
                        }
                    }
                    break;
                case // Update
                1:
                    /*
             * CLIENT EXCEPTION HANDLING TESTING CODE keySt = (String) key;
             * System.out.println("Processing updated key: " + key); if
             * (keySt.startsWith("failure")) { throw new Exception(LocalizedStrings
             * .ProcessBatch_THIS_EXCEPTION_REPRESENTS_A_FAILURE_ON_THE_SERVER
             * .toLocalizedString()); }
             */
                    // Retrieve the value from the message parts (do not deserialize it)
                    valuePart = clientMessage.getPart(partNumber + 5);
                    // try {
                    // logger.warn(getName() + ": Updating key " + key + " value " +
                    // valuePart.getObject());
                    // } catch (Exception e) {}
                    // Retrieve the callbackArg from the message parts if necessary
                    index = partNumber + 6;
                    callbackArgExistsPart = clientMessage.getPart(index++);
                    {
                        byte[] partBytes = (byte[]) callbackArgExistsPart.getObject();
                        callbackArgExists = partBytes[0] == 0x01;
                    }
                    if (callbackArgExists) {
                        callbackArgPart = clientMessage.getPart(index++);
                        try {
                            callbackArg = callbackArgPart.getObject();
                        } catch (Exception e) {
                            logger.warn(LocalizedMessage.create(LocalizedStrings.ProcessBatch_0_CAUGHT_EXCEPTION_PROCESSING_BATCH_UPDATE_REQUEST_1_CONTAINING_2_EVENTS, new Object[] { serverConnection.getName(), Integer.valueOf(batchId), Integer.valueOf(numberOfEvents) }), e);
                            throw e;
                        }
                    }
                    versionTimeStamp = clientMessage.getPart(index++).getLong();
                    if (logger.isDebugEnabled()) {
                        logger.debug("{}: Processing batch update request {} on {} for region {} key {} value {} callbackArg {}", serverConnection.getName(), batchId, serverConnection.getSocketString(), regionName, key, valuePart, callbackArg);
                    }
                    // Process the update request
                    if (key == null || regionName == null) {
                        StringId message = null;
                        Object[] messageArgs = new Object[] { serverConnection.getName(), Integer.valueOf(batchId) };
                        if (key == null) {
                            message = LocalizedStrings.ProcessBatch_0_THE_INPUT_KEY_FOR_THE_BATCH_UPDATE_REQUEST_1_IS_NULL;
                        }
                        if (regionName == null) {
                            message = LocalizedStrings.ProcessBatch_0_THE_INPUT_REGION_NAME_FOR_THE_BATCH_UPDATE_REQUEST_1_IS_NULL;
                        }
                        String s = message.toLocalizedString(messageArgs);
                        logger.warn(s);
                        throw new Exception(s);
                    }
                    region = (LocalRegion) crHelper.getRegion(regionName);
                    if (region == null) {
                        handleRegionNull(serverConnection, regionName, batchId);
                    } else {
                        clientEvent = new EventIDHolder(eventId);
                        if (versionTimeStamp > 0) {
                            VersionTag tag = VersionTag.create(region.getVersionMember());
                            tag.setIsGatewayTag(true);
                            tag.setVersionTimeStamp(versionTimeStamp);
                            tag.setDistributedSystemId(dsid);
                            clientEvent.setVersionTag(tag);
                        }
                        clientEvent.setPossibleDuplicate(possibleDuplicate);
                        handleMessageRetry(region, clientEvent);
                        try {
                            byte[] value = valuePart.getSerializedForm();
                            boolean isObject = valuePart.isObject();
                            AuthorizeRequest authzRequest = serverConnection.getAuthzRequest();
                            if (authzRequest != null) {
                                PutOperationContext putContext = authzRequest.putAuthorize(regionName, key, value, isObject, callbackArg, PutOperationContext.UPDATE);
                                value = putContext.getSerializedValue();
                                isObject = putContext.isObject();
                            }
                            boolean result = false;
                            if (isPdxEvent) {
                                result = addPdxType(crHelper, key, value);
                            } else {
                                result = region.basicBridgePut(key, value, null, isObject, callbackArg, serverConnection.getProxyID(), false, clientEvent);
                            }
                            if (result || clientEvent.isConcurrencyConflict()) {
                                serverConnection.setModificationInfo(true, regionName, key);
                                stats.incUpdateRequest();
                            } else {
                                final Object[] msgArgs = new Object[] { serverConnection.getName(), regionName, key, valuePart, callbackArg };
                                final StringId message = LocalizedStrings.ProcessBatch_0_FAILED_TO_UPDATE_ENTRY_FOR_REGION_1_KEY_2_VALUE_3_AND_CALLBACKARG_4;
                                String s = message.toLocalizedString(msgArgs);
                                logger.info(s);
                                throw new Exception(s);
                            }
                        } catch (CancelException e) {
                            // FIXME better exception hierarchy would avoid this check
                            if (serverConnection.getCachedRegionHelper().getCache().getCancelCriterion().isCancelInProgress()) {
                                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 {
                                throw e;
                            }
                            return;
                        } catch (Exception e) {
                            // Preserve the connection under all circumstances
                            logger.warn(LocalizedMessage.create(LocalizedStrings.ProcessBatch_0_CAUGHT_EXCEPTION_PROCESSING_BATCH_UPDATE_REQUEST_1_CONTAINING_2_EVENTS, new Object[] { serverConnection.getName(), Integer.valueOf(batchId), Integer.valueOf(numberOfEvents) }), e);
                            throw e;
                        }
                    }
                    break;
                case // Destroy
                2:
                    // Retrieve the callbackArg from the message parts if necessary
                    index = partNumber + 5;
                    callbackArgExistsPart = clientMessage.getPart(index++);
                    {
                        byte[] partBytes = (byte[]) callbackArgExistsPart.getObject();
                        callbackArgExists = partBytes[0] == 0x01;
                    }
                    if (callbackArgExists) {
                        callbackArgPart = clientMessage.getPart(index++);
                        try {
                            callbackArg = callbackArgPart.getObject();
                        } catch (Exception e) {
                            logger.warn(LocalizedMessage.create(LocalizedStrings.ProcessBatch_0_CAUGHT_EXCEPTION_PROCESSING_BATCH_DESTROY_REQUEST_1_CONTAINING_2_EVENTS, new Object[] { serverConnection.getName(), Integer.valueOf(batchId), Integer.valueOf(numberOfEvents) }), e);
                            throw e;
                        }
                    }
                    versionTimeStamp = clientMessage.getPart(index++).getLong();
                    if (logger.isDebugEnabled()) {
                        logger.debug("{}: Processing batch destroy request {} on {} for region {} key {}", serverConnection.getName(), batchId, serverConnection.getSocketString(), regionName, key);
                    }
                    // Process the destroy request
                    if (key == null || regionName == null) {
                        StringId message = null;
                        if (key == null) {
                            message = LocalizedStrings.ProcessBatch_0_THE_INPUT_KEY_FOR_THE_BATCH_DESTROY_REQUEST_1_IS_NULL;
                        }
                        if (regionName == null) {
                            message = LocalizedStrings.ProcessBatch_0_THE_INPUT_REGION_NAME_FOR_THE_BATCH_DESTROY_REQUEST_1_IS_NULL;
                        }
                        Object[] messageArgs = new Object[] { serverConnection.getName(), Integer.valueOf(batchId) };
                        String s = message.toLocalizedString(messageArgs);
                        logger.warn(s);
                        throw new Exception(s);
                    }
                    region = (LocalRegion) crHelper.getRegion(regionName);
                    if (region == null) {
                        handleRegionNull(serverConnection, regionName, batchId);
                    } else {
                        clientEvent = new EventIDHolder(eventId);
                        if (versionTimeStamp > 0) {
                            VersionTag tag = VersionTag.create(region.getVersionMember());
                            tag.setIsGatewayTag(true);
                            tag.setVersionTimeStamp(versionTimeStamp);
                            tag.setDistributedSystemId(dsid);
                            clientEvent.setVersionTag(tag);
                        }
                        handleMessageRetry(region, clientEvent);
                        // Destroy the entry
                        try {
                            AuthorizeRequest authzRequest = serverConnection.getAuthzRequest();
                            if (authzRequest != null) {
                                DestroyOperationContext destroyContext = authzRequest.destroyAuthorize(regionName, key, callbackArg);
                                callbackArg = destroyContext.getCallbackArg();
                            }
                            region.basicBridgeDestroy(key, callbackArg, serverConnection.getProxyID(), false, clientEvent);
                            serverConnection.setModificationInfo(true, regionName, key);
                            stats.incDestroyRequest();
                        } catch (EntryNotFoundException e) {
                            logger.info(LocalizedMessage.create(LocalizedStrings.ProcessBatch_0_DURING_BATCH_DESTROY_NO_ENTRY_WAS_FOUND_FOR_KEY_1, new Object[] { serverConnection.getName(), key }));
                        // throw new Exception(e);
                        }
                    }
                    break;
                case // Update Time-stamp for a RegionEntry
                3:
                    try {
                        // Region name
                        regionNamePart = clientMessage.getPart(partNumber + 2);
                        regionName = regionNamePart.getString();
                        // Retrieve the event id from the message parts
                        eventIdPart = clientMessage.getPart(partNumber + 3);
                        eventId = (EventID) eventIdPart.getObject();
                        // Retrieve the key from the message parts
                        keyPart = clientMessage.getPart(partNumber + 4);
                        key = keyPart.getStringOrObject();
                        // Retrieve the callbackArg from the message parts if necessary
                        index = partNumber + 5;
                        callbackArgExistsPart = clientMessage.getPart(index++);
                        byte[] partBytes = (byte[]) callbackArgExistsPart.getObject();
                        callbackArgExists = partBytes[0] == 0x01;
                        if (callbackArgExists) {
                            callbackArgPart = clientMessage.getPart(index++);
                            callbackArg = callbackArgPart.getObject();
                        }
                    } catch (Exception e) {
                        logger.warn(LocalizedMessage.create(LocalizedStrings.ProcessBatch_0_CAUGHT_EXCEPTION_PROCESSING_BATCH_UPDATE_VERSION_REQUEST_1_CONTAINING_2_EVENTS, new Object[] { serverConnection.getName(), Integer.valueOf(batchId), Integer.valueOf(numberOfEvents) }), e);
                        throw e;
                    }
                    versionTimeStamp = clientMessage.getPart(index++).getLong();
                    if (logger.isDebugEnabled()) {
                        logger.debug("{}: Processing batch update-version request {} on {} for region {} key {} value {} callbackArg {}", serverConnection.getName(), batchId, serverConnection.getSocketString(), regionName, key, valuePart, callbackArg);
                    }
                    // Process the update time-stamp request
                    if (key == null || regionName == null) {
                        StringId message = LocalizedStrings.ProcessBatch_0_CAUGHT_EXCEPTION_PROCESSING_BATCH_UPDATE_VERSION_REQUEST_1_CONTAINING_2_EVENTS;
                        Object[] messageArgs = new Object[] { serverConnection.getName(), Integer.valueOf(batchId), Integer.valueOf(numberOfEvents) };
                        String s = message.toLocalizedString(messageArgs);
                        logger.warn(s);
                        throw new Exception(s);
                    } else {
                        region = (LocalRegion) crHelper.getRegion(regionName);
                        if (region == null) {
                            handleRegionNull(serverConnection, regionName, batchId);
                        } else {
                            clientEvent = new EventIDHolder(eventId);
                            if (versionTimeStamp > 0) {
                                VersionTag tag = VersionTag.create(region.getVersionMember());
                                tag.setIsGatewayTag(true);
                                tag.setVersionTimeStamp(versionTimeStamp);
                                tag.setDistributedSystemId(dsid);
                                clientEvent.setVersionTag(tag);
                            }
                            // Update the version tag
                            try {
                                region.basicBridgeUpdateVersionStamp(key, callbackArg, serverConnection.getProxyID(), false, clientEvent);
                            } catch (EntryNotFoundException e) {
                                logger.info(LocalizedMessage.create(LocalizedStrings.ProcessBatch_0_DURING_BATCH_UPDATE_VERSION_NO_ENTRY_WAS_FOUND_FOR_KEY_1, new Object[] { serverConnection.getName(), key }));
                            // throw new Exception(e);
                            }
                        }
                    }
                    break;
                default:
                    logger.fatal(LocalizedMessage.create(LocalizedStrings.Processbatch_0_UNKNOWN_ACTION_TYPE_1_FOR_BATCH_FROM_2, new Object[] { serverConnection.getName(), Integer.valueOf(actionType), serverConnection.getSocketString() }));
                    stats.incUnknowsOperationsReceived();
            }
        } catch (CancelException e) {
            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);
            return;
        } catch (Exception e) {
            // If an interrupted exception is thrown , rethrow it
            checkForInterrupt(serverConnection, e);
            // If we have an issue with the PDX registry, stop processing more data
            if (e.getCause() instanceof PdxRegistryMismatchException) {
                fatalException = e.getCause();
                logger.fatal(LocalizedMessage.create(LocalizedStrings.GatewayReceiver_PDX_CONFIGURATION, new Object[] { serverConnection.getMembershipID() }), e.getCause());
                break;
            }
            // write the batch exception to the caller and break
            if (!wroteResponse) {
                // Increment the batch id unless the received batch id is -1 (a
                // failover batch)
                DistributedSystem ds = crHelper.getCache().getDistributedSystem();
                String exceptionMessage = LocalizedStrings.GatewayReceiver_EXCEPTION_WHILE_PROCESSING_BATCH.toLocalizedString(new Object[] { ((InternalDistributedSystem) ds).getDistributionManager().getDistributedSystemId(), ds.getDistributedMember() });
                BatchException70 be = new BatchException70(exceptionMessage, e, indexWithoutPDXEvent, batchId);
                exceptions.add(be);
                if (!removeOnException) {
                    break;
                }
            // servConn.setAsTrue(RESPONDED);
            // wroteResponse = true;
            // break;
            } else {
                // occurred.
                return;
            }
        } finally {
            // Increment the partNumber
            if (actionType == 0 || /* create */
            actionType == 1) /* update */
            {
                if (callbackArgExists) {
                    partNumber += 9;
                } else {
                    partNumber += 8;
                }
            } else if (actionType == 2) /* destroy */
            {
                if (callbackArgExists) {
                    partNumber += 8;
                } else {
                    partNumber += 7;
                }
            } else if (actionType == 3) /* update-version */
            {
                if (callbackArgExists) {
                    partNumber += 8;
                } else {
                    partNumber += 7;
                }
            }
        }
    }
    {
        long oldStart = start;
        start = DistributionStats.getStatTime();
        stats.incProcessBatchTime(start - oldStart);
    }
    if (fatalException != null) {
        serverConnection.incrementLatestBatchIdReplied(batchId);
        writeFatalException(clientMessage, fatalException, serverConnection, batchId);
        serverConnection.setAsTrue(RESPONDED);
    } else if (!exceptions.isEmpty()) {
        serverConnection.incrementLatestBatchIdReplied(batchId);
        writeBatchException(clientMessage, exceptions, serverConnection, batchId);
        serverConnection.setAsTrue(RESPONDED);
    } else if (!wroteResponse) {
        // Increment the batch id unless the received batch id is -1 (a failover
        // batch)
        serverConnection.incrementLatestBatchIdReplied(batchId);
        writeReply(clientMessage, serverConnection, batchId, numberOfEvents);
        serverConnection.setAsTrue(RESPONDED);
        stats.incWriteProcessBatchResponseTime(DistributionStats.getStatTime() - start);
        if (logger.isDebugEnabled()) {
            logger.debug("{}: Sent process batch normal response for batch {} containing {} events ({} bytes) with {} acknowledgement on {}", serverConnection.getName(), batchId, numberOfEvents, clientMessage.getPayloadLength(), (earlyAck ? "early" : "normal"), serverConnection.getSocketString());
        }
    // logger.warn("Sent process batch normal response for batch " +
    // batchId + " containing " + numberOfEvents + " events (" +
    // msg.getPayloadLength() + " bytes) with " + (earlyAck ? "early" :
    // "normal") + " acknowledgement on " + getSocketString());
    }
}
Also used : AuthorizeRequest(org.apache.geode.internal.security.AuthorizeRequest) ArrayList(java.util.ArrayList) LocalRegion(org.apache.geode.internal.cache.LocalRegion) DistributedSystem(org.apache.geode.distributed.DistributedSystem) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) CachedRegionHelper(org.apache.geode.internal.cache.tier.CachedRegionHelper) PdxRegistryMismatchException(org.apache.geode.pdx.PdxRegistryMismatchException) VersionTag(org.apache.geode.internal.cache.versions.VersionTag) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) EventIDHolder(org.apache.geode.internal.cache.EventIDHolder) PdxRegistryMismatchException(org.apache.geode.pdx.PdxRegistryMismatchException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) CacheClosedException(org.apache.geode.cache.CacheClosedException) PdxConfigurationException(org.apache.geode.pdx.PdxConfigurationException) CancelException(org.apache.geode.CancelException) IOException(java.io.IOException) DestroyOperationContext(org.apache.geode.cache.operations.DestroyOperationContext) StringId(org.apache.geode.i18n.StringId) BatchException70(org.apache.geode.internal.cache.wan.BatchException70) Part(org.apache.geode.internal.cache.tier.sockets.Part) EventID(org.apache.geode.internal.cache.EventID) PutOperationContext(org.apache.geode.cache.operations.PutOperationContext) CancelException(org.apache.geode.CancelException) GatewayReceiverStats(org.apache.geode.internal.cache.wan.GatewayReceiverStats)

Example 57 with VersionTag

use of org.apache.geode.internal.cache.versions.VersionTag in project geode by apache.

the class Get70 method getEntryRetained.

/**
   * Same as getValueAndIsObject but the returned value can be a retained off-heap reference.
   */
@Retained
public Entry getEntryRetained(Region region, Object key, Object callbackArg, ServerConnection servConn) {
    // Region.Entry entry;
    String regionName = region.getFullPath();
    if (servConn != null) {
        servConn.setModificationInfo(true, regionName, key);
    }
    VersionTag versionTag = null;
    // LocalRegion lregion = (LocalRegion)region;
    // entry = lregion.getEntry(key, true);
    boolean isObject = true;
    @Retained Object data = null;
    ClientProxyMembershipID id = servConn == null ? null : servConn.getProxyID();
    VersionTagHolder versionHolder = new VersionTagHolder();
    data = ((LocalRegion) region).getRetained(key, callbackArg, true, true, id, versionHolder, true);
    versionTag = versionHolder.getVersionTag();
    // If it is Token.REMOVED, Token.DESTROYED,
    // Token.INVALID, or Token.LOCAL_INVALID
    // set it to null. If it is NOT_AVAILABLE, get the value from
    // disk. If it is already a byte[], set isObject to false.
    boolean wasInvalid = false;
    if (data == Token.REMOVED_PHASE1 || data == Token.REMOVED_PHASE2 || data == Token.DESTROYED) {
        data = null;
    } else if (data == Token.INVALID || data == Token.LOCAL_INVALID) {
        // fix for bug 35884
        data = null;
        wasInvalid = true;
    } else if (data instanceof byte[]) {
        isObject = false;
    } else if (data instanceof CachedDeserializable) {
        CachedDeserializable cd = (CachedDeserializable) data;
        isObject = cd.isSerialized();
        if (cd.usesHeapForStorage()) {
            data = cd.getValue();
        }
    }
    Entry result = new Entry();
    result.value = data;
    result.isObject = isObject;
    result.keyNotPresent = !wasInvalid && (data == null || data == Token.TOMBSTONE);
    result.versionTag = versionTag;
    return result;
}
Also used : ClientProxyMembershipID(org.apache.geode.internal.cache.tier.sockets.ClientProxyMembershipID) Retained(org.apache.geode.internal.offheap.annotations.Retained) VersionTagHolder(org.apache.geode.internal.cache.VersionTagHolder) CachedDeserializable(org.apache.geode.internal.cache.CachedDeserializable) VersionTag(org.apache.geode.internal.cache.versions.VersionTag) Retained(org.apache.geode.internal.offheap.annotations.Retained)

Example 58 with VersionTag

use of org.apache.geode.internal.cache.versions.VersionTag in project geode by apache.

the class Get70 method cmdExecute.

@Override
public void cmdExecute(Message clientMessage, ServerConnection serverConnection, long startparam) throws IOException {
    long start = startparam;
    Part regionNamePart = null, keyPart = null, valuePart = null;
    String regionName = null;
    Object callbackArg = null, key = null;
    CachedRegionHelper crHelper = serverConnection.getCachedRegionHelper();
    CacheServerStats stats = serverConnection.getCacheServerStats();
    StringId errMessage = null;
    serverConnection.setAsTrue(REQUIRES_RESPONSE);
    // requiresResponse = true;
    {
        long oldStart = start;
        start = DistributionStats.getStatTime();
        stats.incReadGetRequestTime(start - oldStart);
    }
    // Retrieve the data from the message parts
    int parts = clientMessage.getNumberOfParts();
    regionNamePart = clientMessage.getPart(0);
    keyPart = clientMessage.getPart(1);
    // valuePart = null; (redundant assignment)
    if (parts > 2) {
        valuePart = clientMessage.getPart(2);
        try {
            callbackArg = valuePart.getObject();
        } catch (Exception e) {
            writeException(clientMessage, e, false, serverConnection);
            // responded = true;
            serverConnection.setAsTrue(RESPONDED);
            return;
        }
    }
    regionName = regionNamePart.getString();
    try {
        key = keyPart.getStringOrObject();
    } catch (Exception e) {
        writeException(clientMessage, e, false, serverConnection);
        // responded = true;
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    if (logger.isDebugEnabled()) {
        logger.debug("{}: Received 7.0 get request ({} bytes) from {} for region {} key {} txId {}", serverConnection.getName(), clientMessage.getPayloadLength(), serverConnection.getSocketString(), regionName, key, clientMessage.getTransactionId());
    }
    // Process the get request
    if (key == null || regionName == null) {
        if ((key == null) && (regionName == null)) {
            errMessage = LocalizedStrings.Request_THE_INPUT_REGION_NAME_AND_KEY_FOR_THE_GET_REQUEST_ARE_NULL;
        } else if (key == null) {
            errMessage = LocalizedStrings.Request_THE_INPUT_KEY_FOR_THE_GET_REQUEST_IS_NULL;
        } else if (regionName == null) {
            errMessage = LocalizedStrings.Request_THE_INPUT_REGION_NAME_FOR_THE_GET_REQUEST_IS_NULL;
        }
        String s = errMessage.toLocalizedString();
        logger.warn("{}: {}", serverConnection.getName(), s);
        writeErrorResponse(clientMessage, MessageType.REQUESTDATAERROR, s, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    Region region = serverConnection.getCache().getRegion(regionName);
    if (region == null) {
        String reason = LocalizedStrings.Request__0_WAS_NOT_FOUND_DURING_GET_REQUEST.toLocalizedString(regionName);
        writeRegionDestroyedEx(clientMessage, regionName, reason, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    GetOperationContext getContext = null;
    try {
        // for integrated security
        this.securityService.authorizeRegionRead(regionName, key.toString());
        AuthorizeRequest authzRequest = serverConnection.getAuthzRequest();
        if (authzRequest != null) {
            getContext = authzRequest.getAuthorize(regionName, key, callbackArg);
            callbackArg = getContext.getCallbackArg();
        }
    } catch (NotAuthorizedException ex) {
        writeException(clientMessage, ex, false, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    // Get the value and update the statistics. Do not deserialize
    // the value if it is a byte[].
    Entry entry;
    try {
        entry = getEntry(region, key, callbackArg, serverConnection);
    } catch (Exception e) {
        writeException(clientMessage, e, false, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    @Retained final Object originalData = entry.value;
    Object data = originalData;
    try {
        boolean isObject = entry.isObject;
        VersionTag versionTag = entry.versionTag;
        boolean keyNotPresent = entry.keyNotPresent;
        try {
            AuthorizeRequestPP postAuthzRequest = serverConnection.getPostAuthzRequest();
            if (postAuthzRequest != null) {
                try {
                    getContext = postAuthzRequest.getAuthorize(regionName, key, data, isObject, getContext);
                    GetOperationContextImpl gci = (GetOperationContextImpl) getContext;
                    Object newData = gci.getRawValue();
                    if (newData != data) {
                        // user changed the value
                        isObject = getContext.isObject();
                        data = newData;
                    }
                } finally {
                    if (getContext != null) {
                        ((GetOperationContextImpl) getContext).release();
                    }
                }
            }
        } catch (NotAuthorizedException ex) {
            writeException(clientMessage, ex, false, serverConnection);
            serverConnection.setAsTrue(RESPONDED);
            return;
        }
        // post process
        data = this.securityService.postProcess(regionName, key, data, entry.isObject);
        long oldStart = start;
        start = DistributionStats.getStatTime();
        stats.incProcessGetTime(start - oldStart);
        if (region instanceof PartitionedRegion) {
            PartitionedRegion pr = (PartitionedRegion) region;
            if (pr.getNetworkHopType() != PartitionedRegion.NETWORK_HOP_NONE) {
                writeResponseWithRefreshMetadata(data, callbackArg, clientMessage, isObject, serverConnection, pr, pr.getNetworkHopType(), versionTag, keyNotPresent);
                pr.clearNetworkHopData();
            } else {
                writeResponse(data, callbackArg, clientMessage, isObject, versionTag, keyNotPresent, serverConnection);
            }
        } else {
            writeResponse(data, callbackArg, clientMessage, isObject, versionTag, keyNotPresent, serverConnection);
        }
    } finally {
        OffHeapHelper.release(originalData);
    }
    serverConnection.setAsTrue(RESPONDED);
    if (logger.isDebugEnabled()) {
        logger.debug("{}: Wrote get response back to {} for region {} {}", serverConnection.getName(), serverConnection.getSocketString(), regionName, entry);
    }
    stats.incWriteGetResponseTime(DistributionStats.getStatTime() - start);
}
Also used : AuthorizeRequest(org.apache.geode.internal.security.AuthorizeRequest) AuthorizeRequestPP(org.apache.geode.internal.security.AuthorizeRequestPP) NotAuthorizedException(org.apache.geode.security.NotAuthorizedException) GetOperationContextImpl(org.apache.geode.cache.operations.internal.GetOperationContextImpl) NotAuthorizedException(org.apache.geode.security.NotAuthorizedException) IOException(java.io.IOException) GetOperationContext(org.apache.geode.cache.operations.GetOperationContext) CachedRegionHelper(org.apache.geode.internal.cache.tier.CachedRegionHelper) StringId(org.apache.geode.i18n.StringId) Retained(org.apache.geode.internal.offheap.annotations.Retained) 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) VersionTag(org.apache.geode.internal.cache.versions.VersionTag) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion)

Example 59 with VersionTag

use of org.apache.geode.internal.cache.versions.VersionTag in project geode by apache.

the class GetAll70 method fillAndSendGetAllResponseChunks.

private void fillAndSendGetAllResponseChunks(Region region, String regionName, Object[] keys, ServerConnection servConn, boolean requestSerializedValues) throws IOException {
    // Interpret null keys object as a request to get all key,value entry pairs
    // of the region; otherwise iterate each key and perform the get behavior.
    Iterator allKeysIter;
    int numKeys;
    if (keys != null) {
        allKeysIter = null;
        numKeys = keys.length;
    } else {
        Set allKeys = region.keySet();
        allKeysIter = allKeys.iterator();
        numKeys = allKeys.size();
    }
    // Shouldn't it be 'keys != null' below?
    // The answer is no.
    // Note that the current implementation of client/server getAll the "keys" will always be
    // non-null.
    // The server callects and returns the values in the same order as the keys it received.
    // So the server does not need to send the keys back to the client.
    // When the client receives the server's "values" it calls setKeys using the key list the client
    // already has.
    // So the only reason we would tell the VersionedObjectList that it needs to track keys is if we
    // are running
    // in the old mode (which may be impossible since we only used that mode pre 7.0) in which the
    // client told us
    // to get and return all the keys and values. I think this was used for register interest.
    VersionedObjectList values = new VersionedObjectList(MAXIMUM_CHUNK_SIZE, keys == null, region.getAttributes().getConcurrencyChecksEnabled(), requestSerializedValues);
    try {
        AuthorizeRequest authzRequest = servConn.getAuthzRequest();
        AuthorizeRequestPP postAuthzRequest = servConn.getPostAuthzRequest();
        Get70 request = (Get70) Get70.getCommand();
        final boolean isDebugEnabled = logger.isDebugEnabled();
        for (int i = 0; i < numKeys; i++) {
            // Send the intermediate chunk if necessary
            if (values.size() == MAXIMUM_CHUNK_SIZE) {
                // Send the chunk and clear the list
                values.setKeys(null);
                sendGetAllResponseChunk(region, values, false, servConn);
                values.clear();
            }
            Object key;
            boolean keyNotPresent = false;
            if (keys != null) {
                key = keys[i];
            } else {
                key = allKeysIter.next();
            }
            if (isDebugEnabled) {
                logger.debug("{}: Getting value for key={}", servConn.getName(), key);
            }
            // Determine if the user authorized to get this key
            GetOperationContext getContext = null;
            if (authzRequest != null) {
                try {
                    getContext = authzRequest.getAuthorize(regionName, key, null);
                    if (isDebugEnabled) {
                        logger.debug("{}: Passed GET pre-authorization for key={}", servConn.getName(), key);
                    }
                } catch (NotAuthorizedException ex) {
                    logger.warn(LocalizedMessage.create(LocalizedStrings.GetAll_0_CAUGHT_THE_FOLLOWING_EXCEPTION_ATTEMPTING_TO_GET_VALUE_FOR_KEY_1, new Object[] { servConn.getName(), key }), ex);
                    values.addExceptionPart(key, ex);
                    continue;
                }
            }
            try {
                this.securityService.authorizeRegionRead(regionName, key.toString());
            } catch (NotAuthorizedException ex) {
                logger.warn(LocalizedMessage.create(LocalizedStrings.GetAll_0_CAUGHT_THE_FOLLOWING_EXCEPTION_ATTEMPTING_TO_GET_VALUE_FOR_KEY_1, new Object[] { servConn.getName(), key }), ex);
                values.addExceptionPart(key, ex);
                continue;
            }
            // Get the value and update the statistics. Do not deserialize
            // the value if it is a byte[].
            // Getting a value in serialized form is pretty nasty. I split this out
            // so the logic can be re-used by the CacheClientProxy.
            Get70.Entry entry = request.getEntry(region, key, null, servConn);
            @Retained final Object originalData = entry.value;
            Object data = originalData;
            if (logger.isDebugEnabled()) {
                logger.debug("retrieved key={} {}", key, entry);
            }
            boolean addedToValues = false;
            try {
                boolean isObject = entry.isObject;
                VersionTag versionTag = entry.versionTag;
                keyNotPresent = entry.keyNotPresent;
                if (postAuthzRequest != null) {
                    try {
                        getContext = postAuthzRequest.getAuthorize(regionName, key, data, isObject, getContext);
                        GetOperationContextImpl gci = (GetOperationContextImpl) getContext;
                        Object newData = gci.getRawValue();
                        if (newData != data) {
                            // user changed the value
                            isObject = getContext.isObject();
                            data = newData;
                        }
                    } catch (NotAuthorizedException ex) {
                        logger.warn(LocalizedMessage.create(LocalizedStrings.GetAll_0_CAUGHT_THE_FOLLOWING_EXCEPTION_ATTEMPTING_TO_GET_VALUE_FOR_KEY_1, new Object[] { servConn.getName(), key }), ex);
                        values.addExceptionPart(key, ex);
                        continue;
                    } finally {
                        if (getContext != null) {
                            ((GetOperationContextImpl) getContext).release();
                        }
                    }
                }
                data = this.securityService.postProcess(regionName, key, data, entry.isObject);
                // Add the entry to the list that will be returned to the client
                if (keyNotPresent) {
                    values.addObjectPartForAbsentKey(key, data, versionTag);
                    addedToValues = true;
                } else {
                    values.addObjectPart(key, data, isObject, versionTag);
                    addedToValues = true;
                }
            } finally {
                if (!addedToValues || data != originalData) {
                    OffHeapHelper.release(originalData);
                }
            }
        }
        // Send the last chunk even if the list is of zero size.
        if (Version.GFE_701.compareTo(servConn.getClientVersion()) <= 0) {
            // 7.0.1 and later clients do not expect the keys in the response
            values.setKeys(null);
        }
        sendGetAllResponseChunk(region, values, true, servConn);
        servConn.setAsTrue(RESPONDED);
    } finally {
        values.release();
    }
}
Also used : Set(java.util.Set) AuthorizeRequest(org.apache.geode.internal.security.AuthorizeRequest) AuthorizeRequestPP(org.apache.geode.internal.security.AuthorizeRequestPP) VersionedObjectList(org.apache.geode.internal.cache.tier.sockets.VersionedObjectList) NotAuthorizedException(org.apache.geode.security.NotAuthorizedException) GetOperationContextImpl(org.apache.geode.cache.operations.internal.GetOperationContextImpl) GetOperationContext(org.apache.geode.cache.operations.GetOperationContext) Retained(org.apache.geode.internal.offheap.annotations.Retained) Iterator(java.util.Iterator) VersionTag(org.apache.geode.internal.cache.versions.VersionTag)

Example 60 with VersionTag

use of org.apache.geode.internal.cache.versions.VersionTag in project geode by apache.

the class GetAllWithCallback method fillAndSendGetAllResponseChunks.

private void fillAndSendGetAllResponseChunks(Region region, String regionName, Object[] keys, ServerConnection servConn, Object callback) throws IOException {
    assert keys != null;
    int numKeys = keys.length;
    VersionedObjectList values = new VersionedObjectList(MAXIMUM_CHUNK_SIZE, false, region.getAttributes().getConcurrencyChecksEnabled(), false);
    try {
        AuthorizeRequest authzRequest = servConn.getAuthzRequest();
        AuthorizeRequestPP postAuthzRequest = servConn.getPostAuthzRequest();
        Get70 request = (Get70) Get70.getCommand();
        for (int i = 0; i < numKeys; i++) {
            // Send the intermediate chunk if necessary
            if (values.size() == MAXIMUM_CHUNK_SIZE) {
                // Send the chunk and clear the list
                sendGetAllResponseChunk(region, values, false, servConn);
                values.clear();
            }
            Object key;
            boolean keyNotPresent = false;
            key = keys[i];
            if (logger.isDebugEnabled()) {
                logger.debug("{}: Getting value for key={}", servConn.getName(), key);
            }
            // Determine if the user authorized to get this key
            GetOperationContext getContext = null;
            if (authzRequest != null) {
                try {
                    getContext = authzRequest.getAuthorize(regionName, key, callback);
                    if (logger.isDebugEnabled()) {
                        logger.debug("{}: Passed GET pre-authorization for key={}", servConn.getName(), key);
                    }
                } catch (NotAuthorizedException ex) {
                    logger.warn(LocalizedMessage.create(LocalizedStrings.GetAll_0_CAUGHT_THE_FOLLOWING_EXCEPTION_ATTEMPTING_TO_GET_VALUE_FOR_KEY_1, new Object[] { servConn.getName(), key }), ex);
                    values.addExceptionPart(key, ex);
                    continue;
                }
            }
            try {
                this.securityService.authorizeRegionRead(regionName, key.toString());
            } catch (NotAuthorizedException ex) {
                logger.warn(LocalizedMessage.create(LocalizedStrings.GetAll_0_CAUGHT_THE_FOLLOWING_EXCEPTION_ATTEMPTING_TO_GET_VALUE_FOR_KEY_1, new Object[] { servConn.getName(), key }), ex);
                values.addExceptionPart(key, ex);
                continue;
            }
            // Get the value and update the statistics. Do not deserialize
            // the value if it is a byte[].
            // Getting a value in serialized form is pretty nasty. I split this out
            // so the logic can be re-used by the CacheClientProxy.
            Get70.Entry entry = request.getEntry(region, key, callback, servConn);
            @Retained final Object originalData = entry.value;
            Object data = originalData;
            if (logger.isDebugEnabled()) {
                logger.debug("retrieved key={} {}", key, entry);
            }
            boolean addedToValues = false;
            try {
                boolean isObject = entry.isObject;
                VersionTag versionTag = entry.versionTag;
                keyNotPresent = entry.keyNotPresent;
                if (postAuthzRequest != null) {
                    try {
                        getContext = postAuthzRequest.getAuthorize(regionName, key, data, isObject, getContext);
                        GetOperationContextImpl gci = (GetOperationContextImpl) getContext;
                        Object newData = gci.getRawValue();
                        if (newData != data) {
                            // user changed the value
                            isObject = getContext.isObject();
                            data = newData;
                        }
                    } catch (NotAuthorizedException ex) {
                        logger.warn(LocalizedMessage.create(LocalizedStrings.GetAll_0_CAUGHT_THE_FOLLOWING_EXCEPTION_ATTEMPTING_TO_GET_VALUE_FOR_KEY_1, new Object[] { servConn.getName(), key }), ex);
                        values.addExceptionPart(key, ex);
                        continue;
                    } finally {
                        if (getContext != null) {
                            ((GetOperationContextImpl) getContext).release();
                        }
                    }
                }
                // Add the entry to the list that will be returned to the client
                if (keyNotPresent) {
                    values.addObjectPartForAbsentKey(key, data, versionTag);
                    addedToValues = true;
                } else {
                    values.addObjectPart(key, data, isObject, versionTag);
                    addedToValues = true;
                }
            } finally {
                if (!addedToValues || data != originalData) {
                    OffHeapHelper.release(originalData);
                }
            }
        }
        // Send the last chunk even if the list is of zero size.
        sendGetAllResponseChunk(region, values, true, servConn);
        servConn.setAsTrue(RESPONDED);
    } finally {
        values.release();
    }
}
Also used : AuthorizeRequest(org.apache.geode.internal.security.AuthorizeRequest) AuthorizeRequestPP(org.apache.geode.internal.security.AuthorizeRequestPP) VersionedObjectList(org.apache.geode.internal.cache.tier.sockets.VersionedObjectList) NotAuthorizedException(org.apache.geode.security.NotAuthorizedException) GetOperationContextImpl(org.apache.geode.cache.operations.internal.GetOperationContextImpl) GetOperationContext(org.apache.geode.cache.operations.GetOperationContext) Retained(org.apache.geode.internal.offheap.annotations.Retained) VersionTag(org.apache.geode.internal.cache.versions.VersionTag)

Aggregations

VersionTag (org.apache.geode.internal.cache.versions.VersionTag)225 Test (org.junit.Test)43 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)31 VM (org.apache.geode.test.dunit.VM)24 CacheException (org.apache.geode.cache.CacheException)22 LocalRegion (org.apache.geode.internal.cache.LocalRegion)22 Region (org.apache.geode.cache.Region)21 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)19 VersionStamp (org.apache.geode.internal.cache.versions.VersionStamp)19 Host (org.apache.geode.test.dunit.Host)19 IOException (java.io.IOException)17 NonTXEntry (org.apache.geode.internal.cache.LocalRegion.NonTXEntry)16 ConcurrentCacheModificationException (org.apache.geode.internal.cache.versions.ConcurrentCacheModificationException)16 VersionSource (org.apache.geode.internal.cache.versions.VersionSource)16 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)16 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)16 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)15 RegionVersionVector (org.apache.geode.internal.cache.versions.RegionVersionVector)15 ArrayList (java.util.ArrayList)14 CancelException (org.apache.geode.CancelException)14