Search in sources :

Example 1 with FunctionServiceManager

use of org.apache.geode.cache.execute.internal.FunctionServiceManager in project geode by apache.

the class InternalDistributedSystem method disconnect.

/**
   * Disconnects this VM from the distributed system. Shuts down the distribution manager.
   * 
   * @param preparingForReconnect true if called by a reconnect operation
   * @param reason the reason the disconnect is being performed
   * @param keepAlive true if user requested durable subscriptions are to be retained at server.
   */
protected void disconnect(boolean preparingForReconnect, String reason, boolean keepAlive) {
    boolean isShutdownHook = (shutdownHook != null) && (Thread.currentThread() == shutdownHook);
    if (!preparingForReconnect) {
        // logger.info("disconnecting IDS@"+System.identityHashCode(this));
        synchronized (reconnectListeners) {
            reconnectListeners.clear();
        }
        cancelReconnect();
    }
    final boolean isDebugEnabled = logger.isDebugEnabled();
    try {
        HashSet shutdownListeners = null;
        try {
            if (isDebugEnabled) {
                logger.debug("DistributedSystem.disconnect invoked on {}", this);
            }
            synchronized (GemFireCacheImpl.class) {
                // bug 36955, 37014: don't use a disconnect listener on the cache;
                // it takes too long.
                //
                // However, make sure cache is completely closed before starting
                // the distributed system close.
                InternalCache currentCache = GemFireCacheImpl.getInstance();
                if (currentCache != null && !currentCache.isClosed()) {
                    // bug #42663 - this must be set while
                    disconnectListenerThread.set(Boolean.TRUE);
                    // closing the cache
                    try {
                        // fix for 42150
                        currentCache.close(reason, dm.getRootCause(), keepAlive, true);
                    } catch (VirtualMachineError e) {
                        SystemFailure.initiateFailure(e);
                        throw e;
                    } catch (Throwable e) {
                        SystemFailure.checkFailure();
                        // Whenever you catch Error or Throwable, you must also
                        // check for fatal JVM error (see above). However, there is
                        logger.warn(LocalizedMessage.create(LocalizedStrings.InternalDistributedSystem_EXCEPTION_TRYING_TO_CLOSE_CACHE), e);
                    } finally {
                        disconnectListenerThread.set(Boolean.FALSE);
                    }
                }
                // marked as shutting down
                synchronized (this) {
                    if (this.isDisconnecting) {
                        // It's already started, but don't return
                        // to the caller until it has completed.
                        waitDisconnected();
                        return;
                    }
                    // isDisconnecting
                    this.isDisconnecting = true;
                    if (!preparingForReconnect) {
                        // move cancelReconnect above this synchronized block fix for bug 35202
                        if (this.reconnectDS != null) {
                            // break recursion
                            if (isDebugEnabled) {
                                logger.debug("disconnecting reconnected DS: {}", this.reconnectDS);
                            }
                            InternalDistributedSystem r = this.reconnectDS;
                            this.reconnectDS = null;
                            r.disconnect(false, null, false);
                        }
                    }
                // !reconnect
                }
            // synchronized (this)
            }
            if (!isShutdownHook) {
                shutdownListeners = doDisconnects(attemptingToReconnect, reason);
            }
            if (!this.attemptingToReconnect) {
                if (this.logWriterAppender != null) {
                    LogWriterAppenders.stop(LogWriterAppenders.Identifier.MAIN);
                }
                if (this.securityLogWriterAppender != null) {
                    // LOG:SECURITY: old code did NOT invoke this
                    LogWriterAppenders.stop(LogWriterAppenders.Identifier.SECURITY);
                }
            }
            AlertAppender.getInstance().shuttingDown();
        } finally {
            // be ABSOLUTELY CERTAIN that dm closed
            try {
                // Do the bulk of the close...
                this.dm.close();
                // is enabled, loss of the locator doesn't cause the DM to croak
                if (this.startedLocator != null) {
                    this.startedLocator.stop(forcedDisconnect, preparingForReconnect, false);
                    this.startedLocator = null;
                }
            } finally {
                // the DM is closed :-(
                if (!preparingForReconnect) {
                    SystemTimer.cancelSwarm(this);
                }
            }
        // finally timer cancelled
        }
        if (!isShutdownHook) {
            doShutdownListeners(shutdownListeners);
        }
        // closing the Aggregate stats
        if (functionServiceStats != null) {
            functionServiceStats.close();
        }
        // closing individual function stats
        for (FunctionStats functionstats : functionExecutionStatsMap.values()) {
            functionstats.close();
        }
        (new FunctionServiceManager()).unregisterAllFunctions();
        if (this.sampler != null) {
            this.sampler.stop();
            this.sampler = null;
        }
        if (!this.attemptingToReconnect) {
            if (this.logWriterAppender != null) {
                LogWriterAppenders.destroy(LogWriterAppenders.Identifier.MAIN);
            }
            if (this.securityLogWriterAppender != null) {
                LogWriterAppenders.destroy(LogWriterAppenders.Identifier.SECURITY);
            }
        }
        // NOTE: no logging after this point :-)
        // bug35388 - logwriters accumulate, causing mem
        LoggingThreadGroup.cleanUpThreadGroups();
        // leak
        EventID.unsetDS();
    } finally {
        try {
            if (getOffHeapStore() != null) {
                getOffHeapStore().close();
            }
        } finally {
            try {
                removeSystem(this);
                // Close the config object
                this.config.close();
            } finally {
                // Finally, mark ourselves as disconnected
                setDisconnected();
                SystemFailure.stopThreads();
            }
        }
    }
}
Also used : FunctionServiceManager(org.apache.geode.cache.execute.internal.FunctionServiceManager) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) InternalCache(org.apache.geode.internal.cache.InternalCache) FunctionStats(org.apache.geode.internal.cache.execute.FunctionStats) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 FunctionServiceManager (org.apache.geode.cache.execute.internal.FunctionServiceManager)1 GemFireCacheImpl (org.apache.geode.internal.cache.GemFireCacheImpl)1 InternalCache (org.apache.geode.internal.cache.InternalCache)1 FunctionStats (org.apache.geode.internal.cache.execute.FunctionStats)1