Search in sources :

Example 11 with CqException

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

the class CqServiceImpl method processRegionEvent.

private void processRegionEvent(CacheEvent event, Profile localProfile, Profile[] profiles, FilterRoutingInfo frInfo) throws CqException {
    final boolean isDebugEnabled = logger.isDebugEnabled();
    if (isDebugEnabled) {
        logger.debug("CQ service processing region event {}", event);
    }
    Integer cqRegionEvent = generateCqRegionEvent(event);
    for (int i = -1; i < profiles.length; i++) {
        CacheProfile cf;
        if (i < 0) {
            cf = (CacheProfile) localProfile;
            if (cf == null)
                continue;
        } else {
            cf = (CacheProfile) profiles[i];
        }
        FilterProfile pf = cf.filterProfile;
        if (pf == null || pf.getCqMap().isEmpty()) {
            continue;
        }
        Map cqs = pf.getCqMap();
        HashMap<Long, Integer> cqInfo = new HashMap<>();
        Iterator cqIter = cqs.entrySet().iterator();
        while (cqIter.hasNext()) {
            Map.Entry cqEntry = (Map.Entry) cqIter.next();
            ServerCQImpl cQuery = (ServerCQImpl) cqEntry.getValue();
            if (!event.isOriginRemote() && event.getOperation().isRegionDestroy() && !((LocalRegion) event.getRegion()).isUsedForPartitionedRegionBucket()) {
                try {
                    if (isDebugEnabled) {
                        logger.debug("Closing CQ on region destroy event. CqName : {}", cQuery.getName());
                    }
                    cQuery.close(false);
                } catch (Exception ex) {
                    if (isDebugEnabled) {
                        logger.debug("Failed to Close CQ on region destroy. CqName : {}", cQuery.getName(), ex);
                    }
                }
            }
            cqInfo.put(cQuery.getFilterID(), cqRegionEvent);
            cQuery.getVsdStats().updateStats(cqRegionEvent);
        }
        if (pf.isLocalProfile()) {
            frInfo.setLocalCqInfo(cqInfo);
        } else {
            frInfo.setCqRoutingInfo(cf.getDistributedMember(), cqInfo);
        }
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) 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) CacheProfile(org.apache.geode.internal.cache.CacheDistributionAdvisor.CacheProfile) FilterProfile(org.apache.geode.internal.cache.FilterProfile) Iterator(java.util.Iterator) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 12 with CqException

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

the class CqServiceImpl method getAllDurableClientCqs.

@Override
public List<String> getAllDurableClientCqs(ClientProxyMembershipID clientProxyId) throws CqException {
    if (clientProxyId == null) {
        throw new CqException(LocalizedStrings.CqService_UNABLE_TO_RETRIEVE_DURABLE_CQS_FOR_CLIENT_PROXY_ID.toLocalizedString());
    }
    List<ServerCQ> cqs = getAllClientCqs(clientProxyId);
    ArrayList<String> durableClientCqs = new ArrayList<>();
    for (ServerCQ cq : cqs) {
        ServerCQImpl cQuery = (ServerCQImpl) cq;
        if (cQuery != null && cQuery.isDurable()) {
            ClientProxyMembershipID id = cQuery.getClientProxyId();
            if (id != null && id.equals(clientProxyId)) {
                durableClientCqs.add(cQuery.getName());
            }
        }
    }
    return durableClientCqs;
}
Also used : ClientProxyMembershipID(org.apache.geode.internal.cache.tier.sockets.ClientProxyMembershipID) CqException(org.apache.geode.cache.query.CqException) ArrayList(java.util.ArrayList)

Example 13 with CqException

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

the class CqServiceImpl method stopCq.

/**
   * Called directly on server side.
   */
@Override
public void stopCq(String cqName, ClientProxyMembershipID clientId) throws CqException {
    String serverCqName = cqName;
    if (clientId != null) {
        serverCqName = this.constructServerCqName(cqName, clientId);
        removeFromCacheForServerToConstructedCQName(cqName, clientId);
    }
    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) getCq(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 {
        if (!cQuery.isStopped()) {
            cQuery.stop();
        }
    } catch (CqClosedException cce) {
        throw new CqException(cce.getMessage());
    } finally {
        // If this CQ is stopped, disable caching event keys for this CQ.
        // this.removeCQFromCaching(cQuery.getServerCqName());
        this.removeFromMatchingCqMap(cQuery);
    }
    // Send stop message to peers.
    cQuery.getCqBaseRegion().getFilterProfile().stopCq(cQuery);
}
Also used : StringId(org.apache.geode.i18n.StringId) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) CqException(org.apache.geode.cache.query.CqException) CqClosedException(org.apache.geode.cache.query.CqClosedException) 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) TimeoutException(org.apache.geode.cache.TimeoutException)

Example 14 with CqException

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

the class ClientCQImpl method initConnectionProxy.

/**
   * Initializes the connection using the pool from the client region. Also sets the cqBaseRegion
   * value of this CQ.
   */
private void initConnectionProxy() throws CqException, RegionNotFoundException {
    cqBaseRegion = (LocalRegion) cqService.getCache().getRegion(regionName);
    // is obtained by the Bridge Client/writer/loader on the local region.
    if (cqBaseRegion == null) {
        throw new RegionNotFoundException(LocalizedStrings.CqQueryImpl_REGION_ON_WHICH_QUERY_IS_SPECIFIED_NOT_FOUND_LOCALLY_REGIONNAME_0.toLocalizedString(regionName));
    }
    ServerRegionProxy srp = cqBaseRegion.getServerProxy();
    if (srp != null) {
        if (logger.isTraceEnabled()) {
            logger.trace("Found server region proxy on region. RegionName: {}", regionName);
        }
        this.cqProxy = new ServerCQProxyImpl(srp);
        if (!srp.getPool().getSubscriptionEnabled()) {
            throw new CqException("The 'queueEnabled' flag on Pool installed on Region " + regionName + " is set to false.");
        }
    } else {
        throw new CqException("Unable to get the connection pool. The Region does not have a pool configured.");
    }
}
Also used : RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) ServerRegionProxy(org.apache.geode.cache.client.internal.ServerRegionProxy) CqException(org.apache.geode.cache.query.CqException) ServerCQProxyImpl(org.apache.geode.cache.client.internal.ServerCQProxyImpl)

Example 15 with CqException

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

the class ClientCQImpl method executeCqOnRedundantsAndPrimary.

/**
   * This executes the CQ first on the redundant server and then on the primary server. This is
   * required to keep the redundancy behavior in accordance with the HAQueue expectation (wherein
   * the events are delivered only from the primary).
   * 
   * @param executeWithInitialResults boolean
   * @return Object SelectResults in case of executeWithInitialResults
   */
private Object executeCqOnRedundantsAndPrimary(boolean executeWithInitialResults) throws CqClosedException, RegionNotFoundException, CqException {
    Object initialResults = null;
    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_IN_RUNNING_STATE_CQNAME_0.toLocalizedString(this.cqName));
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Performing Execute {} request for CQ. CqName: {}", ((executeWithInitialResults) ? "WithInitialResult" : ""), this.cqName);
        }
        this.cqBaseRegion = (LocalRegion) cqService.getCache().getRegion(this.regionName);
        // If not server send the request to server.
        if (!cqService.isServer()) {
            // pool that initializes the CQ. Else its set using the Region proxy.
            if (this.cqProxy == null) {
                initConnectionProxy();
            }
            boolean success = false;
            try {
                if (this.proxyCache != null) {
                    if (this.proxyCache.isClosed()) {
                        throw new CacheClosedException("Cache is closed for this user.");
                    }
                    UserAttributes.userAttributes.set(this.proxyCache.getUserAttributes());
                }
                if (executeWithInitialResults) {
                    initialResults = cqProxy.createWithIR(this);
                    if (initialResults == null) {
                        String errMsg = "Failed to execute the CQ.  CqName: " + this.cqName + ", Query String is: " + this.queryString;
                        throw new CqException(errMsg);
                    }
                } else {
                    cqProxy.create(this);
                }
                success = true;
            } catch (Exception ex) {
                // Check for system shutdown.
                if (this.shutdownInProgress()) {
                    throw new CqException("System shutdown in progress.");
                }
                if (ex.getCause() instanceof GemFireSecurityException) {
                    if (securityLogWriter.warningEnabled()) {
                        securityLogWriter.warning(LocalizedStrings.CqQueryImpl_EXCEPTION_WHILE_EXECUTING_CQ_EXCEPTION_0, ex, null);
                    }
                    throw new CqException(ex.getCause().getMessage(), ex.getCause());
                } else if (ex instanceof CqException) {
                    throw (CqException) ex;
                } else {
                    String errMsg = LocalizedStrings.CqQueryImpl_FAILED_TO_EXECUTE_THE_CQ_CQNAME_0_QUERY_STRING_IS_1_ERROR_FROM_LAST_SERVER_2.toLocalizedString(this.cqName, this.queryString, ex.getLocalizedMessage());
                    if (logger.isDebugEnabled()) {
                        logger.debug(errMsg, ex);
                    }
                    throw new CqException(errMsg, ex);
                }
            } finally {
                if (!success && !this.shutdownInProgress()) {
                    try {
                        cqProxy.close(this);
                    } catch (Exception e) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("Exception cleaning up failed cq", e);
                        }
                        UserAttributes.userAttributes.set(null);
                    }
                }
                UserAttributes.userAttributes.set(null);
            }
        }
        this.cqState.setState(CqStateImpl.RUNNING);
    }
    // If client side, alert listeners that a cqs have been connected
    if (!cqService.isServer()) {
        connected = true;
        CqListener[] cqListeners = getCqAttributes().getCqListeners();
        for (int lCnt = 0; lCnt < cqListeners.length; lCnt++) {
            if (cqListeners[lCnt] != null) {
                if (cqListeners[lCnt] instanceof CqStatusListener) {
                    CqStatusListener listener = (CqStatusListener) cqListeners[lCnt];
                    listener.onCqConnected();
                }
            }
        }
    }
    // Update CQ-base region for book keeping.
    this.cqService.stats().incCqsActive();
    this.cqService.stats().decCqsStopped();
    return initialResults;
}
Also used : CqException(org.apache.geode.cache.query.CqException) CqListener(org.apache.geode.cache.query.CqListener) 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) CqStatusListener(org.apache.geode.cache.query.CqStatusListener) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException)

Aggregations

CqException (org.apache.geode.cache.query.CqException)46 CacheException (org.apache.geode.cache.CacheException)20 RegionNotFoundException (org.apache.geode.cache.query.RegionNotFoundException)20 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)20 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)16 Test (org.junit.Test)16 CqExistsException (org.apache.geode.cache.query.CqExistsException)15 QueryService (org.apache.geode.cache.query.QueryService)14 CqClosedException (org.apache.geode.cache.query.CqClosedException)12 ClientSubscriptionTest (org.apache.geode.test.junit.categories.ClientSubscriptionTest)9 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)9 IOException (java.io.IOException)8 List (java.util.List)8 CqQuery (org.apache.geode.cache.query.CqQuery)8 DefaultQueryService (org.apache.geode.cache.query.internal.DefaultQueryService)8 CqService (org.apache.geode.cache.query.internal.cq.CqService)8 Set (java.util.Set)7 CqAttributesFactory (org.apache.geode.cache.query.CqAttributesFactory)7 QueryException (org.apache.geode.cache.query.QueryException)7 InternalCqQuery (org.apache.geode.cache.query.internal.cq.InternalCqQuery)7