Search in sources :

Example 11 with CachedDeserializable

use of org.apache.geode.internal.cache.CachedDeserializable in project geode by apache.

the class Request method getValueAndIsObject.

// take the result 2 element "result" as argument instead of
// returning as the result to avoid creating the array repeatedly
// for large number of entries like in getAll
public void getValueAndIsObject(Region region, Object key, Object callbackArg, ServerConnection servConn, Object[] result) {
    String regionName = region.getFullPath();
    if (servConn != null) {
        servConn.setModificationInfo(true, regionName, key);
    }
    boolean isObject = true;
    ClientProxyMembershipID id = servConn == null ? null : servConn.getProxyID();
    Object data = ((LocalRegion) region).get(key, callbackArg, true, true, true, id, null, false);
    // disk. If it is already a byte[], set isObject to false.
    if (data instanceof CachedDeserializable) {
        CachedDeserializable cd = (CachedDeserializable) data;
        if (!cd.isSerialized()) {
            // it is a byte[]
            isObject = false;
            data = cd.getDeserializedForReading();
        } else {
            data = cd.getValue();
        }
    } else if (data == Token.REMOVED_PHASE1 || data == Token.REMOVED_PHASE2 || data == Token.TOMBSTONE || data == Token.DESTROYED) {
        data = null;
    } else if (data == Token.INVALID || data == Token.LOCAL_INVALID) {
        // fix for bug 35884
        data = null;
    } else if (data instanceof byte[]) {
        isObject = false;
    }
    result[0] = data;
    result[1] = Boolean.valueOf(isObject);
    result[2] = (data == null);
}
Also used : ClientProxyMembershipID(org.apache.geode.internal.cache.tier.sockets.ClientProxyMembershipID) CachedDeserializable(org.apache.geode.internal.cache.CachedDeserializable) LocalRegion(org.apache.geode.internal.cache.LocalRegion)

Example 12 with CachedDeserializable

use of org.apache.geode.internal.cache.CachedDeserializable in project geode by apache.

the class RequestEventValue method cmdExecute.

public void cmdExecute(Message clientMessage, ServerConnection serverConnection, long start) throws IOException {
    Part eventIDPart = null, valuePart = null;
    EventID event = null;
    Object callbackArg = null;
    CachedRegionHelper crHelper = serverConnection.getCachedRegionHelper();
    StringBuffer errMessage = new StringBuffer();
    serverConnection.setAsTrue(REQUIRES_RESPONSE);
    // Retrieve the data from the message parts
    int parts = clientMessage.getNumberOfParts();
    eventIDPart = clientMessage.getPart(0);
    if (eventIDPart == null) {
        logger.warn(LocalizedMessage.create(LocalizedStrings.RequestEventValue_0_THE_EVENT_ID_FOR_THE_GET_EVENT_VALUE_REQUEST_IS_NULL, serverConnection.getName()));
        errMessage.append(" The event id for the get event value request is null.");
        writeErrorResponse(clientMessage, MessageType.REQUESTDATAERROR, errMessage.toString(), serverConnection);
        serverConnection.setAsTrue(RESPONDED);
    } else {
        try {
            event = (EventID) eventIDPart.getObject();
        } catch (Exception e) {
            writeException(clientMessage, e, false, serverConnection);
            serverConnection.setAsTrue(RESPONDED);
            return;
        }
        if (parts > 1) {
            valuePart = clientMessage.getPart(1);
            try {
                if (valuePart != null) {
                    callbackArg = valuePart.getObject();
                }
            } catch (Exception e) {
                writeException(clientMessage, e, false, serverConnection);
                serverConnection.setAsTrue(RESPONDED);
                return;
            }
        }
        if (logger.isTraceEnabled()) {
            logger.trace("{}: Received get event value request ({} bytes) from {}", serverConnection.getName(), clientMessage.getPayloadLength(), serverConnection.getSocketString());
        }
        CacheClientNotifier ccn = serverConnection.getAcceptor().getCacheClientNotifier();
        // Get the ha container.
        HAContainerWrapper haContainer = (HAContainerWrapper) ccn.getHaContainer();
        if (haContainer == null) {
            String reason = " was not found during get event value request";
            writeRegionDestroyedEx(clientMessage, "ha container", reason, serverConnection);
            serverConnection.setAsTrue(RESPONDED);
        } else {
            Object[] valueAndIsObject = new Object[2];
            try {
                Object data = haContainer.get(new HAEventWrapper(event));
                if (data == null) {
                    logger.warn(LocalizedMessage.create(LocalizedStrings.RequestEventValue_UNABLE_TO_FIND_A_CLIENT_UPDATE_MESSAGE_FOR_0, event));
                    String msgStr = "No value found for " + event + " in " + haContainer.getName();
                    writeErrorResponse(clientMessage, MessageType.REQUEST_EVENT_VALUE_ERROR, msgStr, serverConnection);
                    serverConnection.setAsTrue(RESPONDED);
                    return;
                } else {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Value retrieved for event {}", event);
                    }
                    Object val = ((ClientUpdateMessageImpl) data).getValueToConflate();
                    if (!(val instanceof byte[])) {
                        if (val instanceof CachedDeserializable) {
                            val = ((CachedDeserializable) val).getSerializedValue();
                        } else {
                            val = CacheServerHelper.serialize(val);
                        }
                        ((ClientUpdateMessageImpl) data).setLatestValue(val);
                    }
                    valueAndIsObject[0] = val;
                    valueAndIsObject[1] = Boolean.valueOf(((ClientUpdateMessageImpl) data).valueIsObject());
                }
            } catch (Exception e) {
                writeException(clientMessage, e, false, serverConnection);
                serverConnection.setAsTrue(RESPONDED);
                return;
            }
            Object data = valueAndIsObject[0];
            boolean isObject = (Boolean) valueAndIsObject[1];
            writeResponse(data, callbackArg, clientMessage, isObject, serverConnection);
            serverConnection.setAsTrue(RESPONDED);
            ccn.getClientProxy(serverConnection.getProxyID()).getStatistics().incDeltaFullMessagesSent();
            if (logger.isDebugEnabled()) {
                logger.debug("{}: Wrote get event value response back to {} for ha container {}", serverConnection.getName(), serverConnection.getSocketString(), haContainer.getName());
            }
        }
    }
}
Also used : ClientUpdateMessageImpl(org.apache.geode.internal.cache.tier.sockets.ClientUpdateMessageImpl) HAContainerWrapper(org.apache.geode.internal.cache.ha.HAContainerWrapper) CachedDeserializable(org.apache.geode.internal.cache.CachedDeserializable) CacheClientNotifier(org.apache.geode.internal.cache.tier.sockets.CacheClientNotifier) IOException(java.io.IOException) CachedRegionHelper(org.apache.geode.internal.cache.tier.CachedRegionHelper) Part(org.apache.geode.internal.cache.tier.sockets.Part) EventID(org.apache.geode.internal.cache.EventID) HAEventWrapper(org.apache.geode.internal.cache.tier.sockets.HAEventWrapper)

Example 13 with CachedDeserializable

use of org.apache.geode.internal.cache.CachedDeserializable 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 14 with CachedDeserializable

use of org.apache.geode.internal.cache.CachedDeserializable in project geode by apache.

the class Bug38741DUnitTest method testPartitionedRegionAndCopyOnRead.

/**
   * Test to ensure that a PartitionedRegion doesn't make more than the expected number of copies
   * when copy-on-read is set to true
   * 
   * @throws Exception
   */
@Test
public void testPartitionedRegionAndCopyOnRead() throws Exception {
    final Host h = Host.getHost(0);
    final VM accessor = h.getVM(2);
    final VM datastore = h.getVM(3);
    final String rName = getUniqueName();
    final String k1 = "k1";
    datastore.invoke(new CacheSerializableRunnable("Create PR DataStore") {

        public void run2() throws CacheException {
            AttributesFactory factory = new AttributesFactory();
            factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(0).create());
            createRootRegion(rName, factory.create());
        }
    });
    accessor.invoke(new CacheSerializableRunnable("Create PR Accessor and put new value") {

        public void run2() throws CacheException {
            AttributesFactory factory = new AttributesFactory();
            factory.setPartitionAttributes(new PartitionAttributesFactory().setLocalMaxMemory(0).setRedundantCopies(0).create());
            Region r = createRootRegion(rName, factory.create());
            SerializationCountingValue val = new SerializationCountingValue();
            r.put(k1, val);
            // First put to a bucket will serialize once to determine the size of the value
            // to know how much extra space the new bucket with the new entry will consume
            // and serialize again to send the bytes
            assertEquals(2, val.count.get());
            // A put to an already created bucket should only be serialized once
            val = new SerializationCountingValue();
            r.put(k1, val);
            assertEquals(1, val.count.get());
        }
    });
    datastore.invoke(new CacheSerializableRunnable("assert datastore entry serialization count") {

        public void run2() throws CacheException {
            PartitionedRegion pr = (PartitionedRegion) getRootRegion(rName);
            // Visit the one bucket (since there is only one value in the entire PR)
            // to directly copy the entry bytes and assert the serialization count.
            // All this extra work is to assure the serialization count does not increase
            // (by de-serializing the value stored in the map, which would then have to be
            // re-serialized).
            pr.getDataStore().visitBuckets(new BucketVisitor() {

                public void visit(Integer bucketId, Region r) {
                    BucketRegion br = (BucketRegion) r;
                    try {
                        KeyInfo keyInfo = new KeyInfo(k1, null, bucketId);
                        RawValue rv = br.getSerialized(keyInfo, false, false, null, null, false);
                        Object val = rv.getRawValue();
                        assertTrue(val instanceof CachedDeserializable);
                        CachedDeserializable cd = (CachedDeserializable) val;
                        SerializationCountingValue scv = (SerializationCountingValue) cd.getDeserializedForReading();
                        assertEquals(1, scv.count.get());
                    } catch (IOException fail) {
                        Assert.fail("Unexpected IOException", fail);
                    }
                }
            });
        }
    });
    accessor.invoke(new CacheSerializableRunnable("assert accessor entry serialization count") {

        public void run2() throws CacheException {
            Region r = getRootRegion(rName);
            SerializationCountingValue v1 = (SerializationCountingValue) r.get(k1);
            // The counter was incremented once to send the data to the datastore
            assertEquals(1, v1.count.get());
            getCache().setCopyOnRead(true);
            // Once to send the data to the datastore, no need to do a serialization
            // when we make copy since it is serialized from datastore to us.
            SerializationCountingValue v2 = (SerializationCountingValue) r.get(k1);
            assertEquals(1, v2.count.get());
            assertTrue(v1 != v2);
        }
    });
    datastore.invoke(new CacheSerializableRunnable("assert value serialization") {

        public void run2() throws CacheException {
            Region r = getRootRegion(rName);
            SerializationCountingValue v1 = (SerializationCountingValue) r.get(k1);
            // Once to send the value from the accessor to the data store
            assertEquals(1, v1.count.get());
            getCache().setCopyOnRead(true);
            // Once to send the value from the accessor to the data store
            // once to make a local copy
            SerializationCountingValue v2 = (SerializationCountingValue) r.get(k1);
            assertEquals(2, v2.count.get());
            assertTrue(v1 != v2);
        }
    });
}
Also used : CachedDeserializable(org.apache.geode.internal.cache.CachedDeserializable) CacheException(org.apache.geode.cache.CacheException) Host(org.apache.geode.test.dunit.Host) BucketVisitor(org.apache.geode.internal.cache.PartitionedRegionDataStore.BucketVisitor) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) BucketRegion(org.apache.geode.internal.cache.BucketRegion) KeyInfo(org.apache.geode.internal.cache.KeyInfo) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) VM(org.apache.geode.test.dunit.VM) LocalRegion(org.apache.geode.internal.cache.LocalRegion) BucketRegion(org.apache.geode.internal.cache.BucketRegion) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) RawValue(org.apache.geode.internal.cache.BucketRegion.RawValue) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 15 with CachedDeserializable

use of org.apache.geode.internal.cache.CachedDeserializable in project geode by apache.

the class DataSerializer method writeObjectAsByteArray.

/**
   * Serialize the given object <code>obj</code> into a byte array using
   * {@link #writeObject(Object, DataOutput)} and then writes the byte array to the given data
   * output <code>out</code> in the same format {@link #writeByteArray(byte[], DataOutput)} does.
   * This method will serialize a <code>null</code> obj and not throw a
   * <code>NullPointerException</code>.
   *
   * @param obj the object to serialize and write
   * @param out the data output to write the byte array to
   * @throws IllegalArgumentException if a problem occurs while serialize <code>obj</code>
   * @throws IOException if a problem occurs while writing to <code>out</code>
   *
   * @see #readByteArray
   * @since GemFire 5.0.2
   */
public static void writeObjectAsByteArray(Object obj, DataOutput out) throws IOException {
    Object object = obj;
    if (obj instanceof CachedDeserializable) {
        if (obj instanceof StoredObject) {
            StoredObject so = (StoredObject) obj;
            if (logger.isTraceEnabled(LogMarker.SERIALIZER)) {
                logger.trace(LogMarker.SERIALIZER, "writeObjectAsByteArray StoredObject");
            }
            so.sendAsByteArray(out);
            return;
        } else {
            object = ((CachedDeserializable) obj).getSerializedValue();
        }
    }
    if (logger.isTraceEnabled(LogMarker.SERIALIZER)) {
        if (object == null) {
            logger.trace(LogMarker.SERIALIZER, "writeObjectAsByteArray null");
        } else {
            logger.trace(LogMarker.SERIALIZER, "writeObjectAsByteArray obj.getClass={}", object.getClass());
        }
    }
    if (object instanceof byte[] || object == null) {
        writeByteArray((byte[]) object, out);
    } else if (out instanceof ObjToByteArraySerializer) {
        ((ObjToByteArraySerializer) out).writeAsSerializedByteArray(object);
    } else /*
       * else if (obj instanceof Sendable) { ((Sendable)obj).sendTo(out); }
       */
    {
        HeapDataOutputStream hdos;
        if (object instanceof HeapDataOutputStream) {
            hdos = (HeapDataOutputStream) object;
        } else {
            Version v = InternalDataSerializer.getVersionForDataStreamOrNull(out);
            if (v == null) {
                v = Version.CURRENT;
            }
            hdos = new HeapDataOutputStream(v);
            try {
                DataSerializer.writeObject(object, hdos);
            } catch (IOException e) {
                RuntimeException e2 = new IllegalArgumentException(LocalizedStrings.DataSerializer_PROBELM_WHILE_SERIALIZING.toLocalizedString());
                e2.initCause(e);
                throw e2;
            }
        }
        InternalDataSerializer.writeArrayLength(hdos.size(), out);
        hdos.sendTo(out);
    }
}
Also used : ObjToByteArraySerializer(org.apache.geode.internal.ObjToByteArraySerializer) CachedDeserializable(org.apache.geode.internal.cache.CachedDeserializable) StoredObject(org.apache.geode.internal.offheap.StoredObject) Version(org.apache.geode.internal.Version) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) StoredObject(org.apache.geode.internal.offheap.StoredObject) IOException(java.io.IOException)

Aggregations

CachedDeserializable (org.apache.geode.internal.cache.CachedDeserializable)20 LocalRegion (org.apache.geode.internal.cache.LocalRegion)6 Test (org.junit.Test)6 StoredObject (org.apache.geode.internal.offheap.StoredObject)5 Retained (org.apache.geode.internal.offheap.annotations.Retained)5 IOException (java.io.IOException)4 UnitTest (org.apache.geode.test.junit.categories.UnitTest)4 ClientProxyMembershipID (org.apache.geode.internal.cache.tier.sockets.ClientProxyMembershipID)3 DataOutputStream (java.io.DataOutputStream)2 ArrayList (java.util.ArrayList)2 AttributesFactory (org.apache.geode.cache.AttributesFactory)2 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)2 HeapDataOutputStream (org.apache.geode.internal.HeapDataOutputStream)2 Version (org.apache.geode.internal.Version)2 EventID (org.apache.geode.internal.cache.EventID)2 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)2 CachedRegionHelper (org.apache.geode.internal.cache.tier.CachedRegionHelper)2 Part (org.apache.geode.internal.cache.tier.sockets.Part)2 Released (org.apache.geode.internal.offheap.annotations.Released)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1