Search in sources :

Example 1 with CacheClosedException

use of org.apache.geode.cache.CacheClosedException in project geode by apache.

the class LuceneGetPageFunction method execute.

@Override
public void execute(FunctionContext context) {
    try {
        RegionFunctionContext ctx = (RegionFunctionContext) context;
        Region region = PartitionRegionHelper.getLocalDataForContext(ctx);
        Set<?> keys = ctx.getFilter();
        List<PageEntry> results = new PageResults(keys.size());
        for (Object key : keys) {
            PageEntry entry = getEntry(region, key);
            if (entry != null) {
                results.add(entry);
            }
        }
        ctx.getResultSender().lastResult(results);
    } catch (CacheClosedException | PrimaryBucketException e) {
        logger.debug("Exception during lucene query function", e);
        throw new InternalFunctionInvocationTargetException(e);
    }
}
Also used : InternalFunctionInvocationTargetException(org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException) Region(org.apache.geode.cache.Region) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) CacheClosedException(org.apache.geode.cache.CacheClosedException) PrimaryBucketException(org.apache.geode.internal.cache.PrimaryBucketException)

Example 2 with CacheClosedException

use of org.apache.geode.cache.CacheClosedException 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)

Example 3 with CacheClosedException

use of org.apache.geode.cache.CacheClosedException in project geode by apache.

the class MemoryBlockNodeJUnitTest method getDataValueCatchesCacheClosedException.

@Test
public void getDataValueCatchesCacheClosedException() {
    Object obj = getValue();
    storedObject = createValueAsSerializedStoredObject(obj);
    OffHeapStoredObject spyStoredObject = spy((OffHeapStoredObject) storedObject);
    doReturn("java.lang.Long").when(spyStoredObject).getDataType();
    doThrow(new CacheClosedException("Unit test forced exception")).when(spyStoredObject).getRawBytes();
    ByteArrayOutputStream errContent = new ByteArrayOutputStream();
    System.setErr(new PrintStream(errContent));
    MemoryBlock mb = new MemoryBlockNode(ma, spyStoredObject);
    softly.assertThat(mb.getDataValue()).isEqualTo("CacheClosedException:Unit test forced exception");
}
Also used : PrintStream(java.io.PrintStream) CacheClosedException(org.apache.geode.cache.CacheClosedException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Example 4 with CacheClosedException

use of org.apache.geode.cache.CacheClosedException in project geode by apache.

the class SingleHopStatsDUnitTest method closeCacheAndDisconnect.

private void closeCacheAndDisconnect() {
    try {
        Cache cache = CacheFactory.getAnyInstance();
        if (cache != null && !cache.isClosed()) {
            cache.close();
            cache.getDistributedSystem().disconnect();
        }
    } catch (CacheClosedException e) {
    }
}
Also used : CacheClosedException(org.apache.geode.cache.CacheClosedException) Cache(org.apache.geode.cache.Cache)

Example 5 with CacheClosedException

use of org.apache.geode.cache.CacheClosedException in project geode by apache.

the class OnGroupsFunctionExecutionDUnitTest method testStreamingClientServerFunction.

@Test
public void testStreamingClientServerFunction() {
    Host host = Host.getHost(0);
    VM server0 = host.getVM(0);
    VM server1 = host.getVM(1);
    VM server2 = host.getVM(2);
    VM client = host.getVM(3);
    VM locator = Host.getLocator();
    final String regionName = getName();
    initVM(server0, "mg,g0", regionName, true);
    initVM(server1, "g1", regionName, true);
    initVM(server2, "g0,g1", regionName, true);
    final int locatorPort = getLocatorPort(locator);
    final String hostName = host.getHostName();
    client.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            try {
                Cache c = CacheFactory.getAnyInstance();
                c.close();
            } catch (CacheClosedException cce) {
            }
            disconnectFromDS();
            LogWriterUtils.getLogWriter().fine("SWAP:creating client cache");
            ClientCacheFactory ccf = new ClientCacheFactory();
            ccf.addPoolLocator(hostName, locatorPort);
            ccf.setPoolServerGroup("mg");
            ccf.set(LOG_LEVEL, LogWriterUtils.getDUnitLogLevel());
            ClientCache c = ccf.create();
            c.getLogger().info("SWAP:invoking function from client on g0");
            Execution e = InternalFunctionService.onServers(c, "g0");
            ArrayList<Integer> l = (ArrayList<Integer>) e.execute(new OnGroupMultiResultFunction()).getResult();
            int sum = 0;
            for (int i = 0; i < l.size(); i++) {
                sum += l.get(i);
            }
            assertEquals(10, sum);
            return null;
        }
    });
    client.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            ClientCache c = ClientCacheFactory.getAnyInstance();
            c.getLogger().fine("SWAP:invoking function from client on mg");
            Execution e = InternalFunctionService.onServers(c, "mg");
            ArrayList<Integer> l = (ArrayList<Integer>) e.execute(new OnGroupMultiResultFunction()).getResult();
            int sum = 0;
            for (int i = 0; i < l.size(); i++) {
                sum += l.get(i);
            }
            assertEquals(5, sum);
            return null;
        }
    });
    client.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            ClientCache c = ClientCacheFactory.getAnyInstance();
            c.getLogger().fine("SWAP:invoking function from client on g0 g1");
            Execution e = InternalFunctionService.onServers(c, "g0", "g1");
            ArrayList<Integer> l = (ArrayList<Integer>) e.execute(new OnGroupMultiResultFunction()).getResult();
            int sum = 0;
            for (int i = 0; i < l.size(); i++) {
                sum += l.get(i);
            }
            assertEquals(15, sum);
            return null;
        }
    });
}
Also used : ArrayList(java.util.ArrayList) Host(org.apache.geode.test.dunit.Host) CacheClosedException(org.apache.geode.cache.CacheClosedException) ClientCache(org.apache.geode.cache.client.ClientCache) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheClosedException(org.apache.geode.cache.CacheClosedException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) Execution(org.apache.geode.cache.execute.Execution) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Cache(org.apache.geode.cache.Cache) ClientCache(org.apache.geode.cache.client.ClientCache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

CacheClosedException (org.apache.geode.cache.CacheClosedException)96 Cache (org.apache.geode.cache.Cache)26 Test (org.junit.Test)21 IOException (java.io.IOException)20 ArrayList (java.util.ArrayList)20 FunctionException (org.apache.geode.cache.execute.FunctionException)20 FunctionInvocationTargetException (org.apache.geode.cache.execute.FunctionInvocationTargetException)20 CancelException (org.apache.geode.CancelException)18 Region (org.apache.geode.cache.Region)18 Host (org.apache.geode.test.dunit.Host)17 VM (org.apache.geode.test.dunit.VM)17 InternalCache (org.apache.geode.internal.cache.InternalCache)16 IgnoredException (org.apache.geode.test.dunit.IgnoredException)16 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)16 DistributedMember (org.apache.geode.distributed.DistributedMember)14 ReplyException (org.apache.geode.distributed.internal.ReplyException)14 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)12 Execution (org.apache.geode.cache.execute.Execution)11 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)11 HashMap (java.util.HashMap)10