Search in sources :

Example 6 with CqClosedException

use of org.apache.geode.cache.query.CqClosedException in project geode by apache.

the class ClientCQImpl method executeWithInitialResults.

/**
   * Start or resume executing the query. Gets or updates the CQ results and returns them.
   */
@Override
public <E> CqResults<E> executeWithInitialResults() throws CqClosedException, RegionNotFoundException, CqException {
    synchronized (queuedEventsSynchObject) {
        // until first call is completed.
        while (queuedEvents != null) {
            try {
                queuedEventsSynchObject.wait();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
        // At this point we know queuedEvents is null and no one is adding to queuedEvents yet.
        this.queuedEvents = new ConcurrentLinkedQueue<CqEventImpl>();
    }
    if (CqQueryImpl.testHook != null) {
        testHook.pauseUntilReady();
    }
    // Send CQ request to servers.
    // If an exception is thrown, we need to clean up the queuedEvents
    // or else client will hang on next executeWithInitialResults
    CqResults initialResults;
    try {
        initialResults = (CqResults) executeCqOnRedundantsAndPrimary(true);
    } catch (RegionNotFoundException | CqException | RuntimeException e) {
        queuedEvents = null;
        throw e;
    }
    // initial results can be added to the queue.
    synchronized (queuedEventsSynchObject) {
        // Invoke the CQ Listeners with the received CQ Events.
        try {
            if (!this.queuedEvents.isEmpty()) {
                try {
                    Runnable r = new Runnable() {

                        @Override
                        public void run() {
                            Object[] eventArray = null;
                            if (CqQueryImpl.testHook != null) {
                                testHook.setEventCount(queuedEvents.size());
                            }
                            // Synchronization for the executer thread.
                            synchronized (queuedEventsSynchObject) {
                                try {
                                    eventArray = queuedEvents.toArray();
                                    // Process through the events
                                    for (Object cqEvent : eventArray) {
                                        cqService.invokeListeners(cqName, ClientCQImpl.this, (CqEventImpl) cqEvent);
                                        stats.decQueuedCqListenerEvents();
                                    }
                                } finally {
                                    // Make sure that we notify waiting threads or else possible dead lock
                                    queuedEvents.clear();
                                    queuedEvents = null;
                                    queuedEventsSynchObject.notify();
                                }
                            }
                        }
                    };
                    final LoggingThreadGroup group = LoggingThreadGroup.createThreadGroup("CQEventHandler", logger);
                    Thread thread = new Thread(group, r, "CQEventHandler For " + cqName);
                    thread.setDaemon(true);
                    thread.start();
                } catch (Exception ex) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Exception while invoking the CQ Listener with queued events.", ex);
                    }
                }
            } else {
                queuedEvents = null;
            }
        } finally {
            queuedEventsSynchObject.notify();
        }
        return initialResults;
    }
}
Also used : RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CqException(org.apache.geode.cache.query.CqException) LoggingThreadGroup(org.apache.geode.internal.logging.LoggingThreadGroup) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CqException(org.apache.geode.cache.query.CqException) CacheClosedException(org.apache.geode.cache.CacheClosedException) CancelException(org.apache.geode.CancelException) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException) CqClosedException(org.apache.geode.cache.query.CqClosedException) CqResults(org.apache.geode.cache.query.CqResults)

Example 7 with CqClosedException

use of org.apache.geode.cache.query.CqClosedException in project geode by apache.

the class CqServiceImpl method closeCq.

@Override
public void closeCq(String cqName, ClientProxyMembershipID clientProxyId) throws CqException {
    String serverCqName = cqName;
    if (clientProxyId != null) {
        serverCqName = this.constructServerCqName(cqName, clientProxyId);
        removeFromCacheForServerToConstructedCQName(cqName, clientProxyId);
    }
    ServerCQImpl cQuery = null;
    StringId errMsg = null;
    Exception ex = null;
    try {
        HashMap<String, CqQueryImpl> cqMap = cqQueryMap;
        if (!cqMap.containsKey(serverCqName)) {
            /*
         * gregp 052808: We should silently fail here instead of throwing error. This is to deal
         * with races in recovery
         */
            return;
        }
        cQuery = (ServerCQImpl) cqMap.get(serverCqName);
    } catch (CacheLoaderException e1) {
        errMsg = LocalizedStrings.CqService_CQ_NOT_FOUND_IN_THE_CQ_META_REGION_CQNAME_0;
        ex = e1;
    } catch (TimeoutException e2) {
        errMsg = LocalizedStrings.CqService_TIMEOUT_WHILE_TRYING_TO_GET_CQ_FROM_META_REGION_CQNAME_0;
        ex = e2;
    } finally {
        if (ex != null) {
            String s = errMsg.toLocalizedString(cqName);
            if (logger.isDebugEnabled()) {
                logger.debug(s);
            }
            throw new CqException(s, ex);
        }
    }
    try {
        cQuery.close(false);
        // CqBaseRegion
        try {
            LocalRegion baseRegion = cQuery.getCqBaseRegion();
            if (baseRegion != null && !baseRegion.isDestroyed()) {
                // Server specific clean up.
                if (isServer()) {
                    FilterProfile fp = baseRegion.getFilterProfile();
                    if (fp != null) {
                        fp.closeCq(cQuery);
                    }
                    CacheClientProxy clientProxy = cQuery.getCacheClientNotifier().getClientProxy(clientProxyId);
                    clientProxy.decCqCount();
                    if (clientProxy.hasNoCq()) {
                        this.stats.decClientsWithCqs();
                    }
                }
            }
        } catch (Exception e) {
            // May be cache is being shutdown
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to remove CQ from the base region. CqName : {}", cqName);
            }
        }
        if (isServer()) {
            removeFromBaseRegionToCqNameMap(cQuery.getRegionName(), serverCqName);
        }
        LocalRegion baseRegion = cQuery.getCqBaseRegion();
        if (baseRegion.getFilterProfile().getCqCount() <= 0) {
            if (logger.isDebugEnabled()) {
                logger.debug("Should update the profile for this partitioned region {} for not requiring old value", baseRegion);
            }
        }
    } catch (CqClosedException cce) {
        throw new CqException(cce.getMessage());
    } finally {
        this.removeFromMatchingCqMap(cQuery);
    }
}
Also used : CacheClientProxy(org.apache.geode.internal.cache.tier.sockets.CacheClientProxy) CqException(org.apache.geode.cache.query.CqException) CqClosedException(org.apache.geode.cache.query.CqClosedException) LocalRegion(org.apache.geode.internal.cache.LocalRegion) TimeoutException(org.apache.geode.cache.TimeoutException) CqExistsException(org.apache.geode.cache.query.CqExistsException) CqException(org.apache.geode.cache.query.CqException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) InvalidDeltaException(org.apache.geode.InvalidDeltaException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) CqClosedException(org.apache.geode.cache.query.CqClosedException) QueryException(org.apache.geode.cache.query.QueryException) FilterProfile(org.apache.geode.internal.cache.FilterProfile) StringId(org.apache.geode.i18n.StringId) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) TimeoutException(org.apache.geode.cache.TimeoutException)

Example 8 with CqClosedException

use of org.apache.geode.cache.query.CqClosedException in project geode by apache.

the class CqServiceImpl method executeCqs.

@Override
public synchronized void executeCqs(Collection<? extends InternalCqQuery> cqs) throws CqException {
    if (cqs == null) {
        return;
    }
    String cqName = null;
    for (InternalCqQuery internalCq : cqs) {
        CqQuery cq = internalCq;
        if (!cq.isClosed() && cq.isStopped()) {
            try {
                cqName = cq.getName();
                cq.execute();
            } catch (QueryException | CqClosedException e) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Failed to execute the CQ, CqName : {} Error : {}", cqName, e.getMessage());
                }
            }
        }
    }
}
Also used : QueryException(org.apache.geode.cache.query.QueryException) CqClosedException(org.apache.geode.cache.query.CqClosedException) CqQuery(org.apache.geode.cache.query.CqQuery)

Example 9 with CqClosedException

use of org.apache.geode.cache.query.CqClosedException in project geode by apache.

the class ClientCQImpl method close.

@Override
public void close(boolean sendRequestToServer) throws CqClosedException, CqException {
    final boolean isDebugEnabled = logger.isDebugEnabled();
    if (isDebugEnabled) {
        logger.debug("Started closing CQ CqName: {} SendRequestToServer: {}", cqName, sendRequestToServer);
    }
    // Synchronize with stop and execute CQ commands
    synchronized (this.cqState) {
        // Check if the cq is already closed.
        if (this.isClosed()) {
            // throw new CqClosedException("CQ is already closed, CqName : " + this.cqName);
            if (isDebugEnabled) {
                logger.debug("CQ is already closed, CqName: {}", this.cqName);
            }
            return;
        }
        int stateBeforeClosing = this.cqState.getState();
        this.cqState.setState(CqStateImpl.CLOSING);
        boolean isClosed = false;
        // Client Close. Proxy is null in case of server.
        // Check if this has been sent to server, if so send
        // Close request to server.
        Exception exception = null;
        if (this.cqProxy != null && sendRequestToServer) {
            try {
                if (this.proxyCache != null) {
                    if (this.proxyCache.isClosed()) {
                        throw new CacheClosedException("Cache is closed for this user.");
                    }
                    UserAttributes.userAttributes.set(this.proxyCache.getUserAttributes());
                }
                cqProxy.close(this);
                isClosed = true;
            } catch (CancelException e) {
                throw e;
            } catch (Exception ex) {
                if (shutdownInProgress()) {
                    return;
                }
                exception = ex;
            } finally {
                UserAttributes.userAttributes.set(null);
            }
        }
        // Cleanup the resource used by cq.
        this.removeFromCqMap();
        if (cqProxy == null || !sendRequestToServer || isClosed) {
            // Stat update.
            if (stateBeforeClosing == CqStateImpl.RUNNING) {
                cqService.stats().decCqsActive();
            } else if (stateBeforeClosing == CqStateImpl.STOPPED) {
                cqService.stats().decCqsStopped();
            }
            // Set the state to close, and update stats
            this.cqState.setState(CqStateImpl.CLOSED);
            cqService.stats().incCqsClosed();
            cqService.stats().decCqsOnClient();
            if (this.stats != null)
                this.stats.close();
        } else {
            if (shutdownInProgress()) {
                return;
            }
            // Hasn't able to send close request to any server.
            if (exception != null) {
                throw new CqException(LocalizedStrings.CqQueryImpl_FAILED_TO_CLOSE_THE_CQ_CQNAME_0_ERROR_FROM_LAST_ENDPOINT_1.toLocalizedString(this.cqName, exception.getLocalizedMessage()), exception.getCause());
            } else {
                throw new CqException(LocalizedStrings.CqQueryImpl_FAILED_TO_CLOSE_THE_CQ_CQNAME_0_THE_SERVER_ENDPOINTS_ON_WHICH_THIS_CQ_WAS_REGISTERED_WERE_NOT_FOUND.toLocalizedString(this.cqName));
            }
        }
    }
    // Invoke close on Listeners if any.
    if (this.cqAttributes != null) {
        CqListener[] cqListeners = this.getCqAttributes().getCqListeners();
        if (cqListeners != null) {
            if (isDebugEnabled) {
                logger.debug("Invoking CqListeners close() api for the CQ, CqName: {} Number of CqListeners: {}", cqName, cqListeners.length);
            }
            for (int lCnt = 0; lCnt < cqListeners.length; lCnt++) {
                try {
                    cqListeners[lCnt].close();
                // Handle client side exceptions.
                } catch (Exception ex) {
                    logger.warn(LocalizedMessage.create(LocalizedStrings.CqQueryImpl_EXCEPTION_OCCOURED_IN_THE_CQLISTENER_OF_THE_CQ_CQNAME_0_ERROR_1, new Object[] { cqName, ex.getLocalizedMessage() }));
                    if (isDebugEnabled) {
                        logger.debug(ex.getMessage(), 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();
                    logger.warn(LocalizedMessage.create(LocalizedStrings.CqQueryImpl_RUNTIMEEXCEPTION_OCCOURED_IN_THE_CQLISTENER_OF_THE_CQ_CQNAME_0_ERROR_1, new Object[] { cqName, t.getLocalizedMessage() }));
                    if (isDebugEnabled) {
                        logger.debug(t.getMessage(), t);
                    }
                }
            }
        }
    }
    if (isDebugEnabled) {
        logger.debug("Successfully closed the CQ. {}", cqName);
    }
}
Also used : CqException(org.apache.geode.cache.query.CqException) CqListener(org.apache.geode.cache.query.CqListener) CacheClosedException(org.apache.geode.cache.CacheClosedException) CancelException(org.apache.geode.CancelException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CqException(org.apache.geode.cache.query.CqException) CacheClosedException(org.apache.geode.cache.CacheClosedException) CancelException(org.apache.geode.CancelException) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException) CqClosedException(org.apache.geode.cache.query.CqClosedException)

Example 10 with CqClosedException

use of org.apache.geode.cache.query.CqClosedException in project geode by apache.

the class ClientCQImpl method stop.

/**
   * Stop or pause executing the query.
   */
@Override
public void stop() throws CqClosedException, CqException {
    boolean isStopped = false;
    synchronized (this.cqState) {
        if (this.isClosed()) {
            throw new CqClosedException(LocalizedStrings.CqQueryImpl_CQ_IS_CLOSED_CQNAME_0.toLocalizedString(this.cqName));
        }
        if (!(this.isRunning())) {
            throw new IllegalStateException(LocalizedStrings.CqQueryImpl_CQ_IS_NOT_IN_RUNNING_STATE_STOP_CQ_DOES_NOT_APPLY_CQNAME_0.toLocalizedString(this.cqName));
        }
        Exception exception = null;
        try {
            if (this.proxyCache != null) {
                if (this.proxyCache.isClosed()) {
                    throw new CacheClosedException("Cache is closed for this user.");
                }
                UserAttributes.userAttributes.set(this.proxyCache.getUserAttributes());
            }
            cqProxy.stop(this);
            isStopped = true;
        } catch (Exception e) {
            exception = e;
        } finally {
            UserAttributes.userAttributes.set(null);
        }
        if (cqProxy == null || isStopped) {
            // Change state and stats on the client side
            this.cqState.setState(CqStateImpl.STOPPED);
            this.cqService.stats().incCqsStopped();
            this.cqService.stats().decCqsActive();
            if (logger.isDebugEnabled()) {
                logger.debug("Successfully stopped the CQ. {}", cqName);
            }
        } else {
            // Hasn't able to send stop request to any server.
            if (exception != null) {
                throw new CqException(LocalizedStrings.CqQueryImpl_FAILED_TO_STOP_THE_CQ_CQNAME_0_ERROR_FROM_LAST_SERVER_1.toLocalizedString(this.cqName, exception.getLocalizedMessage()), exception.getCause());
            } else {
                throw new CqException(LocalizedStrings.CqQueryImpl_FAILED_TO_STOP_THE_CQ_CQNAME_0_THE_SERVER_ENDPOINTS_ON_WHICH_THIS_CQ_WAS_REGISTERED_WERE_NOT_FOUND.toLocalizedString(this.cqName));
            }
        }
    }
}
Also used : CqException(org.apache.geode.cache.query.CqException) CqClosedException(org.apache.geode.cache.query.CqClosedException) CacheClosedException(org.apache.geode.cache.CacheClosedException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CqException(org.apache.geode.cache.query.CqException) CacheClosedException(org.apache.geode.cache.CacheClosedException) CancelException(org.apache.geode.CancelException) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException) CqClosedException(org.apache.geode.cache.query.CqClosedException)

Aggregations

CqClosedException (org.apache.geode.cache.query.CqClosedException)10 RegionNotFoundException (org.apache.geode.cache.query.RegionNotFoundException)8 CqException (org.apache.geode.cache.query.CqException)7 CancelException (org.apache.geode.CancelException)4 CacheClosedException (org.apache.geode.cache.CacheClosedException)4 CqExistsException (org.apache.geode.cache.query.CqExistsException)4 CqQuery (org.apache.geode.cache.query.CqQuery)4 QueryException (org.apache.geode.cache.query.QueryException)4 GemFireSecurityException (org.apache.geode.security.GemFireSecurityException)4 QueryInvalidException (org.apache.geode.cache.query.QueryInvalidException)3 InvalidDeltaException (org.apache.geode.InvalidDeltaException)2 CacheException (org.apache.geode.cache.CacheException)2 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)2 TimeoutException (org.apache.geode.cache.TimeoutException)2 CqAttributes (org.apache.geode.cache.query.CqAttributes)2 CqAttributesFactory (org.apache.geode.cache.query.CqAttributesFactory)2 CqListener (org.apache.geode.cache.query.CqListener)2 QueryService (org.apache.geode.cache.query.QueryService)2 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)2 StringId (org.apache.geode.i18n.StringId)2