Search in sources :

Example 16 with InternalCache

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

the class InternalDataSerializer method autoSerialized.

public static boolean autoSerialized(Object o, DataOutput out) throws IOException {
    AutoSerializableManager asm = TypeRegistry.getAutoSerializableManager();
    if (asm != null) {
        AutoClassInfo aci = asm.getExistingClassInfo(o.getClass());
        if (aci != null) {
            InternalCache internalCache = GemFireCacheImpl.getForPdx("PDX registry is unavailable because the Cache has been closed.");
            TypeRegistry tr = internalCache.getPdxRegistry();
            PdxOutputStream os;
            if (out instanceof HeapDataOutputStream) {
                os = new PdxOutputStream((HeapDataOutputStream) out);
            } else {
                os = new PdxOutputStream();
            }
            PdxWriterImpl writer = new PdxWriterImpl(tr, o, aci, os);
            try {
                if (is662SerializationEnabled()) {
                    boolean alreadyInProgress = isPdxSerializationInProgress();
                    if (!alreadyInProgress) {
                        setPdxSerializationInProgress(true);
                        try {
                            asm.writeData(writer, o, aci);
                        } finally {
                            setPdxSerializationInProgress(false);
                        }
                    } else {
                        asm.writeData(writer, o, aci);
                    }
                } else {
                    asm.writeData(writer, o, aci);
                }
            } catch (ToDataException | CancelException | NonPortableClassException | GemFireRethrowable ex) {
                throw ex;
            } catch (VirtualMachineError err) {
                SystemFailure.initiateFailure(err);
                // now, so don't let this thread continue.
                throw err;
            } catch (Throwable t) {
                // Whenever you catch Error or Throwable, you must also
                // catch VirtualMachineError (see above). However, there is
                // _still_ a possibility that you are dealing with a cascading
                // error condition, so you also need to check to see if the JVM
                // is still usable:
                SystemFailure.checkFailure();
                throw new ToDataException("PdxSerializer failed when calling toData on " + o.getClass(), t);
            }
            int bytesWritten = writer.completeByteStreamGeneration();
            getDMStats(internalCache).incPdxSerialization(bytesWritten);
            if (!(out instanceof HeapDataOutputStream)) {
                writer.sendTo(out);
            }
            return true;
        }
    }
    return false;
}
Also used : PdxOutputStream(org.apache.geode.pdx.internal.PdxOutputStream) NonPortableClassException(org.apache.geode.pdx.NonPortableClassException) InternalCache(org.apache.geode.internal.cache.InternalCache) TypeRegistry(org.apache.geode.pdx.internal.TypeRegistry) AutoClassInfo(org.apache.geode.pdx.internal.AutoSerializableManager.AutoClassInfo) AutoSerializableManager(org.apache.geode.pdx.internal.AutoSerializableManager) GemFireRethrowable(org.apache.geode.GemFireRethrowable) ToDataException(org.apache.geode.ToDataException) CancelException(org.apache.geode.CancelException) PdxWriterImpl(org.apache.geode.pdx.internal.PdxWriterImpl)

Example 17 with InternalCache

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

the class InternalInstantiator method getInstantiators.

/**
   * Returns all of the currently registered instantiators
   */
public static Instantiator[] getInstantiators() {
    Collection coll = new ArrayList();
    if (!classNamesToHolders.isEmpty()) {
        Iterator it = classNamesToHolders.values().iterator();
        while (it.hasNext()) {
            try {
                InstantiatorAttributesHolder holder = (InstantiatorAttributesHolder) it.next();
                Class instantiatorClass = InternalDataSerializer.getCachedClass(holder.getInstantiatorClassName());
                Class instantiatedClass = InternalDataSerializer.getCachedClass(holder.getInstantiatedClassName());
                synchronized (InternalInstantiator.class) {
                    if (!idsToInstantiators.containsKey(holder.getId())) {
                        register(instantiatorClass, instantiatedClass, holder.getId(), false, holder.getEventId(), holder.getContext());
                    }
                    classNamesToHolders.remove(holder.getInstantiatedClassName());
                    idsToHolders.remove(holder.getId());
                }
            } catch (ClassNotFoundException cnfe) {
                InternalCache cache = GemFireCacheImpl.getInstance();
                if (cache != null && cache.getLoggerI18n() != null && cache.getLoggerI18n().infoEnabled()) {
                    cache.getLoggerI18n().info(LocalizedStrings.InternalInstantiator_COULD_NOT_LOAD_INSTANTIATOR_CLASS_0, new Object[] { cnfe.getMessage() });
                }
            }
        }
    }
    // Don't move it before the if block above.
    coll.addAll(dsMap.values());
    return (Instantiator[]) coll.toArray(new Instantiator[coll.size()]);
}
Also used : ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Collection(java.util.Collection) InternalCache(org.apache.geode.internal.cache.InternalCache) Instantiator(org.apache.geode.Instantiator)

Example 18 with InternalCache

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

the class InternalDataSerializer method sendRegistrationMessageToClients.

private static void sendRegistrationMessageToClients(DataSerializer dataSerializer) {
    InternalCache cache = GemFireCacheImpl.getInstance();
    if (cache == null) {
        // we can't propagate it to clients
        return;
    }
    byte[][] serializedDataSerializer = new byte[2][];
    try {
        serializedDataSerializer[0] = CacheServerHelper.serialize(dataSerializer.getClass().toString().substring(6));
        byte[] idBytes = new byte[4];
        Part.encodeInt(dataSerializer.getId(), idBytes);
        serializedDataSerializer[1] = idBytes;
    } catch (IOException ignored) {
        if (logger.isTraceEnabled(LogMarker.SERIALIZER)) {
            logger.trace(LogMarker.SERIALIZER, "InternalDataSerializer encountered an IOException while serializing DataSerializer :{}", dataSerializer);
        }
    }
    ClientDataSerializerMessage clientDataSerializerMessage = new ClientDataSerializerMessage(EnumListenerEvent.AFTER_REGISTER_DATASERIALIZER, serializedDataSerializer, (ClientProxyMembershipID) dataSerializer.getContext(), (EventID) dataSerializer.getEventId(), new Class[][] { dataSerializer.getSupportedClasses() });
    // Deliver it to all the clients
    CacheClientNotifier.routeClientMessage(clientDataSerializerMessage);
}
Also used : ClientDataSerializerMessage(org.apache.geode.internal.cache.tier.sockets.ClientDataSerializerMessage) InternalCache(org.apache.geode.internal.cache.InternalCache) IOException(java.io.IOException) GemFireIOException(org.apache.geode.GemFireIOException)

Example 19 with InternalCache

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

the class PartitionMessage method process.

/**
   * Upon receipt of the message, both process the message and send an acknowledgement, not
   * necessarily in that order. Note: Any hang in this message may cause a distributed deadlock for
   * those threads waiting for an acknowledgement.
   * 
   * @throws PartitionedRegionException if the region does not exist (typically, if it has been
   *         destroyed)
   */
@Override
public void process(final DistributionManager dm) {
    Throwable thr = null;
    boolean sendReply = true;
    PartitionedRegion pr = null;
    long startTime = 0;
    EntryLogger.setSource(getSender(), "PR");
    try {
        if (checkCacheClosing(dm) || checkDSClosing(dm)) {
            thr = new CacheClosedException(LocalizedStrings.PartitionMessage_REMOTE_CACHE_IS_CLOSED_0.toLocalizedString(dm.getId()));
            return;
        }
        pr = getPartitionedRegion();
        if (pr == null && failIfRegionMissing()) {
            // if the distributed system is disconnecting, don't send a reply saying
            // the partitioned region can't be found (bug 36585)
            thr = new ForceReattemptException(LocalizedStrings.PartitionMessage_0_COULD_NOT_FIND_PARTITIONED_REGION_WITH_ID_1.toLocalizedString(dm.getDistributionManagerId(), regionId));
            // reply sent in finally block below
            return;
        }
        if (pr != null) {
            startTime = getStartPartitionMessageProcessingTime(pr);
        }
        thr = UNHANDLED_EXCEPTION;
        InternalCache cache = getInternalCache();
        if (cache == null) {
            throw new ForceReattemptException(LocalizedStrings.PartitionMessage_REMOTE_CACHE_IS_CLOSED_0.toLocalizedString());
        }
        TXManagerImpl txMgr = getTXManagerImpl(cache);
        TXStateProxy tx = txMgr.masqueradeAs(this);
        if (tx == null) {
            sendReply = operateOnPartitionedRegion(dm, pr, startTime);
        } else {
            try {
                if (txMgr.isClosed()) {
                    // NO DISTRIBUTED MESSAGING CAN BE DONE HERE!
                    sendReply = false;
                } else if (tx.isInProgress()) {
                    sendReply = operateOnPartitionedRegion(dm, pr, startTime);
                    tx.updateProxyServer(this.getSender());
                }
            } finally {
                txMgr.unmasquerade(tx);
            }
        }
        thr = null;
    } catch (ForceReattemptException fre) {
        thr = fre;
    } catch (DataLocationException fre) {
        thr = new ForceReattemptException(fre.getMessage(), fre);
    } catch (DistributedSystemDisconnectedException se) {
        // bug 37026: this is too noisy...
        // throw new CacheClosedException("remote system shutting down");
        // thr = se; cache is closed, no point trying to send a reply
        thr = null;
        sendReply = false;
        if (logger.isDebugEnabled()) {
            logger.debug("shutdown caught, abandoning message: {}", se.getMessage(), se);
        }
    } catch (RegionDestroyedException | RegionNotFoundException rde) {
        // [bruce] RDE does not always mean that the sender's region is also
        // destroyed, so we must send back an exception. If the sender's
        // region is also destroyed, who cares if we send it an exception
        // if (pr != null && pr.isClosed) {
        thr = new ForceReattemptException(LocalizedStrings.PartitionMessage_REGION_IS_DESTROYED_IN_0.toLocalizedString(dm.getDistributionManagerId()), rde);
    // }
    } catch (VirtualMachineError err) {
        SystemFailure.initiateFailure(err);
        // now, so don't let this thread continue.
        throw err;
    } catch (Throwable t) {
        // Whenever you catch Error or Throwable, you must also
        // catch VirtualMachineError (see above). However, there is
        // _still_ a possibility that you are dealing with a cascading
        // error condition, so you also need to check to see if the JVM
        // is still usable:
        SystemFailure.checkFailure();
        // log the exception at fine level if there is no reply to the message
        thr = null;
        if (sendReply) {
            if (!checkDSClosing(dm)) {
                thr = t;
            } else {
                // don't pass arbitrary runtime exceptions and errors back if this
                // cache/vm is closing
                thr = new ForceReattemptException(LocalizedStrings.PartitionMessage_DISTRIBUTED_SYSTEM_IS_DISCONNECTING.toLocalizedString());
            }
        }
        if (logger.isTraceEnabled(LogMarker.DM) && t instanceof RuntimeException) {
            logger.trace(LogMarker.DM, "Exception caught while processing message: {}", t.getMessage(), t);
        }
    } finally {
        if (sendReply) {
            ReplyException rex = null;
            if (thr != null) {
                // don't transmit the exception if this message was to a listener
                // and this listener is shutting down
                boolean excludeException = this.notificationOnly && ((thr instanceof CancelException) || (thr instanceof ForceReattemptException));
                if (!excludeException) {
                    rex = new ReplyException(thr);
                }
            }
            // Send the reply if the operateOnPartitionedRegion returned true
            sendReply(getSender(), this.processorId, dm, rex, pr, startTime);
            EntryLogger.clearSource();
        }
    }
}
Also used : DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException) TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) DataLocationException(org.apache.geode.internal.cache.DataLocationException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) InternalCache(org.apache.geode.internal.cache.InternalCache) CacheClosedException(org.apache.geode.cache.CacheClosedException) ReplyException(org.apache.geode.distributed.internal.ReplyException) ForceReattemptException(org.apache.geode.internal.cache.ForceReattemptException) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) CancelException(org.apache.geode.CancelException)

Example 20 with InternalCache

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

the class PartitionedRegionRebalanceOp method execute.

/**
   * Do the actual rebalance
   * 
   * @return the details of the rebalance.
   */
public Set<PartitionRebalanceInfo> execute() {
    long start = System.nanoTime();
    InternalResourceManager resourceManager = InternalResourceManager.getInternalResourceManager(leaderRegion.getCache());
    MembershipListener listener = new MembershipChangeListener();
    if (isRebalance) {
        InternalResourceManager.getResourceObserver().rebalancingStarted(targetRegion);
    } else {
        InternalResourceManager.getResourceObserver().recoveryStarted(targetRegion);
    }
    RecoveryLock lock = null;
    try {
        if (!checkAndSetColocatedRegions()) {
            return Collections.emptySet();
        }
        // have full redundancy.
        if (!isRebalanceNecessary()) {
            return Collections.emptySet();
        }
        if (!simulate) {
            lock = leaderRegion.getRecoveryLock();
            lock.lock();
        }
        // have fixed it already.
        if (!isRebalanceNecessary()) {
            return Collections.emptySet();
        }
        // register a listener to notify us if the new members leave or join.
        // When a membership change occurs, we want to restart the rebalancing
        // from the beginning.
        // TODO rebalance - we should really add a membership listener to ALL of
        // the colocated regions.
        leaderRegion.getRegionAdvisor().addMembershipListener(listener);
        PartitionedRegionLoadModel model = null;
        InternalCache cache = leaderRegion.getCache();
        Map<PartitionedRegion, InternalPRInfo> detailsMap = fetchDetails(cache);
        BucketOperatorWrapper serialOperator = getBucketOperator(detailsMap);
        ParallelBucketOperator parallelOperator = new ParallelBucketOperator(MAX_PARALLEL_OPERATIONS, cache.getDistributionManager().getWaitingThreadPool(), serialOperator);
        model = buildModel(parallelOperator, detailsMap, resourceManager);
        for (PartitionRebalanceDetailsImpl details : serialOperator.getDetailSet()) {
            details.setPartitionMemberDetailsBefore(model.getPartitionedMemberDetails(details.getRegionPath()));
        }
        director.initialize(model);
        for (; ; ) {
            if (cancelled.get()) {
                return Collections.emptySet();
            }
            if (membershipChange) {
                membershipChange = false;
                // refetch the partitioned region details after
                // a membership change.
                debug("Rebalancing {} detected membership changes. Refetching details", leaderRegion);
                if (this.stats != null) {
                    this.stats.incRebalanceMembershipChanges(1);
                }
                model.waitForOperations();
                detailsMap = fetchDetails(cache);
                model = buildModel(parallelOperator, detailsMap, resourceManager);
                director.membershipChanged(model);
            }
            leaderRegion.checkClosed();
            cache.getCancelCriterion().checkCancelInProgress(null);
            if (logger.isDebugEnabled()) {
                logger.debug("Rebalancing {} Model:{}\n", leaderRegion, model);
            }
            if (!director.nextStep()) {
                // Stop when the director says we can't rebalance any more.
                break;
            }
        }
        debug("Rebalancing {} complete. Model:{}\n", leaderRegion, model);
        long end = System.nanoTime();
        for (PartitionRebalanceDetailsImpl details : serialOperator.getDetailSet()) {
            if (!simulate) {
                details.setTime(end - start);
            }
            details.setPartitionMemberDetailsAfter(model.getPartitionedMemberDetails(details.getRegionPath()));
        }
        return Collections.<PartitionRebalanceInfo>unmodifiableSet(serialOperator.getDetailSet());
    } finally {
        if (lock != null) {
            try {
                lock.unlock();
            } catch (CancelException e) {
            // lock service has been destroyed
            } catch (Exception e) {
                logger.error(LocalizedMessage.create(LocalizedStrings.PartitionedRegionRebalanceOp_UNABLE_TO_RELEASE_RECOVERY_LOCK), e);
            }
        }
        try {
            if (isRebalance) {
                InternalResourceManager.getResourceObserver().rebalancingFinished(targetRegion);
            } else {
                InternalResourceManager.getResourceObserver().recoveryFinished(targetRegion);
            }
        } catch (Exception e) {
            logger.error(LocalizedMessage.create(LocalizedStrings.PartitionedRegionRebalanceOp_ERROR_IN_RESOURCE_OBSERVER), e);
        }
        try {
            leaderRegion.getRegionAdvisor().removeMembershipListener(listener);
        } catch (Exception e) {
            logger.error(LocalizedMessage.create(LocalizedStrings.PartitionedRegionRebalanceOp_ERROR_IN_RESOURCE_OBSERVER), e);
        }
    }
}
Also used : PartitionRebalanceInfo(org.apache.geode.cache.partition.PartitionRebalanceInfo) InternalCache(org.apache.geode.internal.cache.InternalCache) RecoveryLock(org.apache.geode.internal.cache.PartitionedRegion.RecoveryLock) PartitionRebalanceDetailsImpl(org.apache.geode.internal.cache.control.PartitionRebalanceDetailsImpl) CancelException(org.apache.geode.CancelException) InternalResourceManager(org.apache.geode.internal.cache.control.InternalResourceManager) PartitionedRegionLoadModel(org.apache.geode.internal.cache.partitioned.rebalance.PartitionedRegionLoadModel) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) ParallelBucketOperator(org.apache.geode.internal.cache.partitioned.rebalance.ParallelBucketOperator) BucketOperatorWrapper(org.apache.geode.internal.cache.partitioned.rebalance.BucketOperatorWrapper) CancelException(org.apache.geode.CancelException) MembershipListener(org.apache.geode.distributed.internal.MembershipListener)

Aggregations

InternalCache (org.apache.geode.internal.cache.InternalCache)267 DistributedMember (org.apache.geode.distributed.DistributedMember)78 Test (org.junit.Test)64 UnitTest (org.apache.geode.test.junit.categories.UnitTest)52 IOException (java.io.IOException)48 ArrayList (java.util.ArrayList)35 HashSet (java.util.HashSet)35 CliMetaData (org.apache.geode.management.cli.CliMetaData)34 CliCommand (org.springframework.shell.core.annotation.CliCommand)34 TabularResultData (org.apache.geode.management.internal.cli.result.TabularResultData)32 Region (org.apache.geode.cache.Region)31 Result (org.apache.geode.management.cli.Result)30 ResourceOperation (org.apache.geode.management.internal.security.ResourceOperation)30 Expectations (org.jmock.Expectations)30 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)26 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)25 CliFunctionResult (org.apache.geode.management.internal.cli.functions.CliFunctionResult)24 Set (java.util.Set)23 ResultCollector (org.apache.geode.cache.execute.ResultCollector)22 CommandResultException (org.apache.geode.management.internal.cli.result.CommandResultException)20