Search in sources :

Example 46 with FunctionException

use of org.apache.geode.cache.execute.FunctionException in project geode by apache.

the class QueryUsingFunctionContextDUnitTest method testQueriesWithFilterKeysOnPRWithBucketDestroy.

/**
   *
   */
@Test
public void testQueriesWithFilterKeysOnPRWithBucketDestroy() {
    IgnoredException.addIgnoredException("QueryInvocationTargetException");
    Object[][] r = new Object[queries.length][2];
    Set filter = new HashSet();
    // Close cache on server1
    server1.invoke(new CacheSerializableRunnable("Set QueryObserver in cache on server1") {

        @Override
        public void run2() throws CacheException {
            class MyQueryObserver extends IndexTrackingQueryObserver {

                @Override
                public void startQuery(Query query) {
                    Region pr = CacheFactory.getAnyInstance().getRegion(PartitionedRegionName1);
                    Region KeyRegion = null;
                    for (int i = 0; i < 7; i++) {
                        KeyRegion = ((PartitionedRegion) pr).getBucketRegion(i);
                        if (KeyRegion != null)
                            KeyRegion.destroyRegion();
                    }
                }
            }
            ;
            QueryObserverHolder.setInstance(new MyQueryObserver());
        }
    });
    client.invoke(new CacheSerializableRunnable("Run function on PR") {

        @Override
        public void run2() throws CacheException {
            Set filter = new HashSet();
            ResultCollector rcollector = null;
            filter.addAll(getFilter(0, 19));
            for (int i = 0; i < queries.length; i++) {
                try {
                    function = new TestQueryFunction("queryFunctionBucketDestroy");
                    rcollector = FunctionService.onRegion(CacheFactory.getAnyInstance().getRegion(PartitionedRegionName1)).setArguments(queries[i]).withFilter(filter).execute(function);
                    // Should not come here, an exception is expected from above function call.
                    fail("Function call did not fail for query with function context");
                } catch (FunctionException ex) {
                    // ex.printStackTrace();
                    if (!(ex.getCause() instanceof QueryInvocationTargetException)) {
                        fail("Should have received an QueryInvocationTargetException but recieved" + ex.getMessage());
                    }
                }
            }
        // For loop ends here.
        }
    });
    // Close cache on server1
    server1.invoke(new CacheSerializableRunnable("Reset Query Observer on server1") {

        @Override
        public void run2() throws CacheException {
            QueryObserverHolder.reset();
        }
    });
}
Also used : LocalDataSet(org.apache.geode.internal.cache.LocalDataSet) Set(java.util.Set) StructSetOrResultsSet(org.apache.geode.cache.query.functional.StructSetOrResultsSet) HashSet(java.util.HashSet) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Query(org.apache.geode.cache.query.Query) CacheException(org.apache.geode.cache.CacheException) FunctionException(org.apache.geode.cache.execute.FunctionException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) ResultCollector(org.apache.geode.cache.execute.ResultCollector) HashSet(java.util.HashSet) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 47 with FunctionException

use of org.apache.geode.cache.execute.FunctionException in project geode by apache.

the class QueryUsingFunctionContextDUnitTest method testInvalidQueries.

@Test
public void testInvalidQueries() {
    IgnoredException.addIgnoredException("Syntax error");
    client.invoke(new CacheSerializableRunnable("Test query on client and server") {

        @Override
        public void run2() throws CacheException {
            Set filter = new HashSet();
            filter.add(0);
            String query = "select * from / " + repRegionName + " where ID>=0";
            TestServerQueryFunction func = new TestServerQueryFunction("LDS Server function-1");
            function = new TestQueryFunction("queryFunction-1");
            QueryUsingFunctionContextDUnitTest test = new QueryUsingFunctionContextDUnitTest();
            try {
                test.runQueryOnClientUsingFunc(function, repRegionName, filter, query);
                fail("Query execution should have failed.");
            } catch (FunctionException ex) {
                assertTrue("The exception message should mention QueryInvalidException. ", ex.getLocalizedMessage().contains("QueryInvalidException"));
            }
            query = "select * from / " + PartitionedRegionName1 + " where ID>=0";
            func = new TestServerQueryFunction("LDS Server function-1");
            function = new TestQueryFunction("queryFunction-1");
            test = new QueryUsingFunctionContextDUnitTest();
            try {
                test.runQueryOnClientUsingFunc(function, PartitionedRegionName1, filter, query);
                fail("Query execution should have failed.");
            } catch (FunctionException ex) {
                assertTrue("The exception message should mention QueryInvalidException. ", ex.getLocalizedMessage().contains("QueryInvalidException"));
            }
        }
    });
}
Also used : LocalDataSet(org.apache.geode.internal.cache.LocalDataSet) Set(java.util.Set) StructSetOrResultsSet(org.apache.geode.cache.query.functional.StructSetOrResultsSet) HashSet(java.util.HashSet) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) FunctionException(org.apache.geode.cache.execute.FunctionException) HashSet(java.util.HashSet) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 48 with FunctionException

use of org.apache.geode.cache.execute.FunctionException in project geode by apache.

the class MemberFunctionStreamingMessage method process.

@Override
protected void process(final DistributionManager dm) {
    Throwable thr = null;
    ReplyException rex = null;
    if (this.functionObject == null) {
        rex = new ReplyException(new FunctionException(LocalizedStrings.ExecuteFunction_FUNCTION_NAMED_0_IS_NOT_REGISTERED.toLocalizedString(this.functionName)));
        replyWithException(dm, rex);
        return;
    }
    FunctionStats stats = FunctionStats.getFunctionStats(this.functionObject.getId(), dm.getSystem());
    TXStateProxy tx = null;
    try {
        tx = prepForTransaction();
        ResultSender resultSender = new MemberFunctionResultSender(dm, this, this.functionObject);
        Set<Region> regions = new HashSet<Region>();
        if (this.regionPathSet != null) {
            InternalCache cache = GemFireCacheImpl.getInstance();
            for (String regionPath : this.regionPathSet) {
                if (checkCacheClosing(dm) || checkDSClosing(dm)) {
                    thr = new CacheClosedException(LocalizedStrings.PartitionMessage_REMOTE_CACHE_IS_CLOSED_0.toLocalizedString(dm.getId()));
                    return;
                }
                regions.add(cache.getRegion(regionPath));
            }
        }
        FunctionContextImpl context = new MultiRegionFunctionContextImpl(this.functionObject.getId(), this.args, resultSender, regions, isReExecute);
        long start = stats.startTime();
        stats.startFunctionExecution(this.functionObject.hasResult());
        if (logger.isDebugEnabled()) {
            logger.debug("Executing Function: {} on remote member with context: {}", this.functionObject.getId(), context.toString());
        }
        this.functionObject.execute(context);
        if (!this.replyLastMsg && this.functionObject.hasResult()) {
            throw new FunctionException(LocalizedStrings.ExecuteFunction_THE_FUNCTION_0_DID_NOT_SENT_LAST_RESULT.toString(functionObject.getId()));
        }
        stats.endFunctionExecution(start, this.functionObject.hasResult());
    } catch (FunctionException functionException) {
        if (logger.isDebugEnabled()) {
            logger.debug("FunctionException occurred on remote member while executing Function: {}", this.functionObject.getId(), functionException);
        }
        stats.endFunctionExecutionWithException(this.functionObject.hasResult());
        rex = new ReplyException(functionException);
        replyWithException(dm, rex);
    // thr = functionException.getCause();
    } catch (CancelException exception) {
        // 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 = new FunctionInvocationTargetException(exception);
        stats.endFunctionExecutionWithException(this.functionObject.hasResult());
        rex = new ReplyException(thr);
        replyWithException(dm, rex);
    } catch (Exception exception) {
        if (logger.isDebugEnabled()) {
            logger.debug("Exception occurred on remote member while executing Function: {}", this.functionObject.getId(), exception);
        }
        stats.endFunctionExecutionWithException(this.functionObject.hasResult());
        rex = new ReplyException(exception);
        replyWithException(dm, rex);
    // thr = e.getCause();
    } 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();
        thr = t;
    } finally {
        cleanupTransaction(tx);
        if (thr != null) {
            rex = new ReplyException(thr);
            replyWithException(dm, rex);
        }
    }
}
Also used : FunctionContextImpl(org.apache.geode.internal.cache.execute.FunctionContextImpl) MultiRegionFunctionContextImpl(org.apache.geode.internal.cache.execute.MultiRegionFunctionContextImpl) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheClosedException(org.apache.geode.cache.CacheClosedException) ReplyException(org.apache.geode.distributed.internal.ReplyException) MemberFunctionResultSender(org.apache.geode.internal.cache.execute.MemberFunctionResultSender) ResultSender(org.apache.geode.cache.execute.ResultSender) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheClosedException(org.apache.geode.cache.CacheClosedException) CancelException(org.apache.geode.CancelException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) ReplyException(org.apache.geode.distributed.internal.ReplyException) QueryException(org.apache.geode.cache.query.QueryException) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) FunctionStats(org.apache.geode.internal.cache.execute.FunctionStats) MemberFunctionResultSender(org.apache.geode.internal.cache.execute.MemberFunctionResultSender) Region(org.apache.geode.cache.Region) CancelException(org.apache.geode.CancelException) MultiRegionFunctionContextImpl(org.apache.geode.internal.cache.execute.MultiRegionFunctionContextImpl) HashSet(java.util.HashSet)

Example 49 with FunctionException

use of org.apache.geode.cache.execute.FunctionException in project geode by apache.

the class PartitionedRegionFunctionResultSender method sendResult.

public void sendResult(Object oneResult) {
    if (!this.function.hasResult()) {
        throw new IllegalStateException(LocalizedStrings.ExecuteFunction_CANNOT_0_RESULTS_HASRESULT_FALSE.toLocalizedString("send"));
    }
    if (this.serverSender != null) {
        logger.debug("PartitionedRegionFunctionResultSender sending result from local node to client {}", oneResult);
        clientSend(oneResult, dm.getDistributionManagerId());
    } else {
        // P2P
        if (this.msg != null) {
            try {
                logger.debug("PartitionedRegionFunctionResultSender sending result from remote node {}", oneResult);
                this.msg.sendReplyForOneResult(dm, pr, time, oneResult, false, enableOrderedResultStreming);
            } catch (ForceReattemptException e) {
                throw new FunctionException(e);
            } catch (InterruptedException e) {
                throw new FunctionException(e);
            }
        } else {
            logger.debug("PartitionedRegionFunctionResultSender adding result to ResultCollector on local node {}", oneResult);
            this.rc.addResult(dm.getDistributionManagerId(), oneResult);
            FunctionStats.getFunctionStats(function.getId(), this.dm.getSystem()).incResultsReceived();
        }
        // incrementing result sent stats.
        FunctionStats.getFunctionStats(function.getId(), this.dm.getSystem()).incResultsReturned();
    }
}
Also used : ForceReattemptException(org.apache.geode.internal.cache.ForceReattemptException) FunctionException(org.apache.geode.cache.execute.FunctionException)

Example 50 with FunctionException

use of org.apache.geode.cache.execute.FunctionException in project geode by apache.

the class PartitionedRegionFunctionResultSender method lastResult.

// this must be getting called directly from function
public void lastResult(Object oneResult) {
    if (!this.function.hasResult()) {
        throw new IllegalStateException(LocalizedStrings.ExecuteFunction_CANNOT_0_RESULTS_HASRESULT_FALSE.toLocalizedString("send"));
    }
    if (this.serverSender != null) {
        // Client-Server
        if (this.localLastResultRecieved) {
            return;
        }
        if (onlyLocal) {
            checkForBucketMovement(oneResult);
            if (bme != null) {
                clientSend(oneResult, dm.getDistributionManagerId());
                lastClientSend(dm.getDistributionManagerId(), bme);
            } else {
                lastClientSend(dm.getDistributionManagerId(), oneResult);
            }
            this.rc.endResults();
            this.localLastResultRecieved = true;
        } else {
            // call a synchronized method as local node is also waiting to send lastResult
            lastResult(oneResult, rc, false, true, dm.getDistributionManagerId());
        }
    } else {
        if (this.msg != null) {
            checkForBucketMovement(oneResult);
            try {
                if (this.bme != null) {
                    this.msg.sendReplyForOneResult(dm, pr, time, oneResult, false, enableOrderedResultStreming);
                    throw bme;
                } else {
                    this.msg.sendReplyForOneResult(dm, pr, time, oneResult, true, enableOrderedResultStreming);
                }
            } catch (ForceReattemptException e) {
                throw new FunctionException(e);
            } catch (InterruptedException e) {
                throw new FunctionException(e);
            }
        } else {
            if (this.localLastResultRecieved) {
                return;
            }
            if (onlyLocal) {
                checkForBucketMovement(oneResult);
                if (bme != null) {
                    this.rc.addResult(dm.getDistributionManagerId(), oneResult);
                    this.rc.addResult(dm.getDistributionManagerId(), bme);
                } else {
                    this.rc.addResult(dm.getDistributionManagerId(), oneResult);
                }
                // exception thrown will do end result
                this.rc.endResults();
                this.localLastResultRecieved = true;
            } else {
                // call a synchronized method as local node is also waiting to send lastResult
                lastResult(oneResult, rc, false, true, dm.getDistributionManagerId());
            }
            FunctionStats.getFunctionStats(function.getId(), this.dm.getSystem()).incResultsReceived();
        }
        // incrementing result sent stats.
        // Bug : remote node as well as local node calls this method to send
        // the result When the remote nodes are added to the local result collector at that
        // time the stats for the result sent is again incremented : Once the PR team comes with the
        // concept of the Streaming FunctionOperation
        // for the partitioned Region then it will be simple to fix this problem.
        FunctionStats.getFunctionStats(function.getId(), this.dm.getSystem()).incResultsReturned();
    }
}
Also used : ForceReattemptException(org.apache.geode.internal.cache.ForceReattemptException) FunctionException(org.apache.geode.cache.execute.FunctionException)

Aggregations

FunctionException (org.apache.geode.cache.execute.FunctionException)140 Function (org.apache.geode.cache.execute.Function)45 Execution (org.apache.geode.cache.execute.Execution)39 ResultCollector (org.apache.geode.cache.execute.ResultCollector)39 ArrayList (java.util.ArrayList)38 Test (org.junit.Test)38 HashSet (java.util.HashSet)36 CacheClosedException (org.apache.geode.cache.CacheClosedException)31 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)31 IOException (java.io.IOException)30 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)30 List (java.util.List)26 FunctionInvocationTargetException (org.apache.geode.cache.execute.FunctionInvocationTargetException)26 Host (org.apache.geode.test.dunit.Host)25 VM (org.apache.geode.test.dunit.VM)25 Region (org.apache.geode.cache.Region)24 IgnoredException (org.apache.geode.test.dunit.IgnoredException)24 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)24 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)22 Set (java.util.Set)21