Search in sources :

Example 1 with FunctionDomainException

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

the class AddFreeItemToOrders method execute.

public void execute(FunctionContext context) {
    Region region = null;
    List<Object> vals = new ArrayList<Object>();
    List<Object> keys = new ArrayList<Object>();
    List<Object> argsList = new ArrayList<Object>();
    Object[] argsArray = null;
    if (context.getArguments() instanceof Boolean) {
    } else if (context.getArguments() instanceof String) {
        String arg = (String) context.getArguments();
    } else if (context.getArguments() instanceof Vector) {
    } else if (context.getArguments() instanceof Object[]) {
        argsArray = (Object[]) context.getArguments();
        argsList = Arrays.asList(argsArray);
    } else {
        System.out.println("AddFreeItemToOrders : Invalid Arguments");
    }
    InternalCache cache = null;
    try {
        cache = (InternalCache) CacheFactory.getAnyInstance();
        cache.getCacheConfig().setPdxReadSerialized(true);
        region = cache.getRegion("orders");
    } catch (CacheClosedException ex) {
        vals.add("NoCacheFoundResult");
        context.getResultSender().lastResult(vals);
    }
    String oql = "SELECT DISTINCT entry.key FROM /orders.entries entry WHERE entry.value.totalPrice > $1";
    Object[] queryArgs = new Object[1];
    queryArgs[0] = argsList.get(0);
    final Query query = cache.getQueryService().newQuery(oql);
    SelectResults result = null;
    try {
        result = (SelectResults) query.execute(queryArgs);
        int resultSize = result.size();
        if (result instanceof Collection<?>)
            for (Object item : result) {
                keys.add(item);
            }
    } catch (FunctionDomainException e) {
        if (cache != null)
            cache.getLogger().info("Caught FunctionDomainException while executing function AddFreeItemToOrders: " + e.getMessage());
    } catch (TypeMismatchException e) {
        if (cache != null)
            cache.getLogger().info("Caught TypeMismatchException while executing function AddFreeItemToOrders: " + e.getMessage());
    } catch (NameResolutionException e) {
        if (cache != null)
            cache.getLogger().info("Caught NameResolutionException while executing function AddFreeItemToOrders: " + e.getMessage());
    } catch (QueryInvocationTargetException e) {
        if (cache != null)
            cache.getLogger().info("Caught QueryInvocationTargetException while executing function AddFreeItemToOrders" + e.getMessage());
    }
    // class has to be in classpath.
    try {
        Item it = (Item) (argsList.get(1));
        for (Object key : keys) {
            Object obj = region.get(key);
            if (obj instanceof PdxInstance) {
                PdxInstance pi = (PdxInstance) obj;
                Order receivedOrder = (Order) pi.getObject();
                receivedOrder.addItem(it);
                region.put(key, receivedOrder);
            }
        }
        context.getResultSender().lastResult("success");
    } catch (ClassCastException e) {
        context.getResultSender().lastResult("failure");
    } catch (Exception e) {
        context.getResultSender().lastResult("failure");
    }
}
Also used : Query(org.apache.geode.cache.query.Query) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) ArrayList(java.util.ArrayList) InternalCache(org.apache.geode.internal.cache.InternalCache) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) CacheClosedException(org.apache.geode.cache.CacheClosedException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) CacheClosedException(org.apache.geode.cache.CacheClosedException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) SelectResults(org.apache.geode.cache.query.SelectResults) PdxInstance(org.apache.geode.pdx.PdxInstance) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) Vector(java.util.Vector)

Example 2 with FunctionDomainException

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

the class LocalRegion method query.

@Override
public SelectResults query(String predicate) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    if (predicate == null) {
        throw new IllegalArgumentException("The input query predicate is null. A null predicate is not allowed.");
    }
    predicate = predicate.trim();
    SelectResults results;
    if (hasServerProxy()) {
        // Trim whitespace
        String queryString = constructRegionQueryString(predicate.trim());
        try {
            results = getServerProxy().query(queryString, null);
        } catch (Exception e) {
            Throwable cause = e.getCause();
            if (cause == null) {
                cause = e;
            }
            throw new QueryInvocationTargetException(e.getMessage(), cause);
        }
    } else {
        // TODO: params size is always zero so this whole block is wasted
        Object[] params = new Object[0];
        QueryService qs = getGemFireCache().getLocalQueryService();
        String queryStr = constructRegionQueryString(predicate.trim());
        DefaultQuery query = (DefaultQuery) qs.newQuery(queryStr);
        if (query.getRegionsInQuery(params).size() != 1) {
            throw new QueryInvalidException("Prevent multiple region query from being executed through region.query()");
        }
        results = (SelectResults) query.execute(params);
    }
    return results;
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) QueryService(org.apache.geode.cache.query.QueryService) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) StoredObject(org.apache.geode.internal.offheap.StoredObject) TimeoutException(org.apache.geode.cache.TimeoutException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) InternalGemFireException(org.apache.geode.InternalGemFireException) ConflictingPersistentDataException(org.apache.geode.cache.persistence.ConflictingPersistentDataException) CacheRuntimeException(org.apache.geode.cache.CacheRuntimeException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) EntryDestroyedException(org.apache.geode.cache.EntryDestroyedException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) ExecutionException(java.util.concurrent.ExecutionException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) EntryExistsException(org.apache.geode.cache.EntryExistsException) PartitionedRegionStorageException(org.apache.geode.cache.PartitionedRegionStorageException) StatisticsDisabledException(org.apache.geode.cache.StatisticsDisabledException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) FailedSynchronizationException(org.apache.geode.cache.FailedSynchronizationException) NoSuchElementException(java.util.NoSuchElementException) QueryException(org.apache.geode.cache.query.QueryException) RedundancyAlreadyMetException(org.apache.geode.internal.cache.partitioned.RedundancyAlreadyMetException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) LowMemoryException(org.apache.geode.cache.LowMemoryException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) SystemException(javax.transaction.SystemException) SubscriptionNotEnabledException(org.apache.geode.cache.client.SubscriptionNotEnabledException) RegionExistsException(org.apache.geode.cache.RegionExistsException) RegionReinitializedException(org.apache.geode.cache.RegionReinitializedException) CancelException(org.apache.geode.CancelException) DiskAccessException(org.apache.geode.cache.DiskAccessException) CacheWriterException(org.apache.geode.cache.CacheWriterException) IndexMaintenanceException(org.apache.geode.cache.query.IndexMaintenanceException) TransactionException(org.apache.geode.cache.TransactionException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) CacheClosedException(org.apache.geode.cache.CacheClosedException) RollbackException(javax.transaction.RollbackException) ConcurrentCacheModificationException(org.apache.geode.internal.cache.versions.ConcurrentCacheModificationException) MultiIndexCreationException(org.apache.geode.cache.query.MultiIndexCreationException) DeltaSerializationException(org.apache.geode.DeltaSerializationException)

Example 3 with FunctionDomainException

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

the class DataCommandFunction method select.

@SuppressWarnings("rawtypes")
private DataCommandResult select(Object principal, String queryString) {
    InternalCache cache = getCache();
    AtomicInteger nestedObjectCount = new AtomicInteger(0);
    if (StringUtils.isEmpty(queryString)) {
        return DataCommandResult.createSelectInfoResult(null, null, -1, null, CliStrings.QUERY__MSG__QUERY_EMPTY, false);
    }
    QueryService qs = cache.getQueryService();
    // TODO : Find out if is this optimised use. Can you have something equivalent of parsed
    // queries with names where name can be retrieved to avoid parsing every-time
    Query query = qs.newQuery(queryString);
    DefaultQuery tracedQuery = (DefaultQuery) query;
    WrappedIndexTrackingQueryObserver queryObserver = null;
    String queryVerboseMsg = null;
    long startTime = -1;
    if (tracedQuery.isTraced()) {
        startTime = NanoTimer.getTime();
        queryObserver = new WrappedIndexTrackingQueryObserver();
        QueryObserverHolder.setInstance(queryObserver);
    }
    List<SelectResultRow> list = new ArrayList<>();
    try {
        Object results = query.execute();
        if (tracedQuery.isTraced()) {
            queryVerboseMsg = getLogMessage(queryObserver, startTime, queryString);
            queryObserver.reset2();
        }
        if (results instanceof SelectResults) {
            select_SelectResults((SelectResults) results, principal, list, nestedObjectCount);
        } else {
            select_NonSelectResults(results, list);
        }
        return DataCommandResult.createSelectResult(queryString, list, queryVerboseMsg, null, null, true);
    } catch (FunctionDomainException | GfJsonException | QueryInvocationTargetException | NameResolutionException | TypeMismatchException e) {
        logger.warn(e.getMessage(), e);
        return DataCommandResult.createSelectResult(queryString, null, queryVerboseMsg, e, e.getMessage(), false);
    } finally {
        if (queryObserver != null) {
            QueryObserverHolder.reset();
        }
    }
}
Also used : DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Query(org.apache.geode.cache.query.Query) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) ArrayList(java.util.ArrayList) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) InternalCache(org.apache.geode.internal.cache.InternalCache) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) SelectResults(org.apache.geode.cache.query.SelectResults) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueryService(org.apache.geode.cache.query.QueryService) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) SelectResultRow(org.apache.geode.management.internal.cli.domain.DataCommandResult.SelectResultRow) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject)

Example 4 with FunctionDomainException

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

the class ResourceManagerWithQueryMonitorDUnitTest method doCriticalMemoryHitAddResultsTestWithMultipleServers.

private void doCriticalMemoryHitAddResultsTestWithMultipleServers(final String regionName, boolean createPR, final int criticalThreshold, final boolean disabledQueryMonitorForLowMem, final int queryTimeout, final boolean hitCriticalThreshold) throws Exception {
    // create region on the server
    final Host host = Host.getHost(0);
    final VM server1 = host.getVM(0);
    final VM server2 = host.getVM(1);
    final VM client = host.getVM(2);
    final int numObjects = 200;
    try {
        final int[] port = AvailablePortHelper.getRandomAvailableTCPPorts(2);
        startCacheServer(server1, port[0], criticalThreshold, disabledQueryMonitorForLowMem, queryTimeout, regionName, createPR, 0);
        startCacheServer(server2, port[1], criticalThreshold, true, -1, regionName, createPR, 0);
        startClient(client, server1, port[0], regionName);
        populateData(server2, regionName, numObjects);
        createCancelDuringAddResultsTestHook(server1);
        client.invoke(new SerializableCallable("executing query to be canceled during add results") {

            public Object call() {
                QueryService qs = null;
                try {
                    qs = getCache().getQueryService();
                    Query query = qs.newQuery("Select * From /" + regionName);
                    SelectResults results = (SelectResults) query.execute();
                    if (hitCriticalThreshold && disabledQueryMonitorForLowMem == false) {
                        throw new CacheException("should have hit low memory") {
                        };
                    }
                } catch (Exception e) {
                    handleException(e, hitCriticalThreshold, disabledQueryMonitorForLowMem, queryTimeout);
                }
                return 0;
            }
        });
        verifyRejectedObjects(server1, disabledQueryMonitorForLowMem, queryTimeout, hitCriticalThreshold);
        // Pause for a second and then let's recover
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        // Recover from critical heap
        if (hitCriticalThreshold) {
            vmRecoversFromCriticalHeap(server1);
        }
        // Check to see if query execution is ok under "normal" or "healthy" conditions
        client.invoke(new CacheSerializableRunnable("Executing query when system is 'Normal'") {

            public void run2() {
                try {
                    QueryService qs = getCache().getQueryService();
                    Query query = qs.newQuery("Select * From /" + regionName);
                    SelectResults results = (SelectResults) query.execute();
                    assertEquals(numObjects, results.size());
                } catch (QueryInvocationTargetException e) {
                    assertFalse(true);
                } catch (NameResolutionException e) {
                    assertFalse(true);
                } catch (TypeMismatchException e) {
                    assertFalse(true);
                } catch (FunctionDomainException e) {
                    assertFalse(true);
                }
            }
        });
        // Recover from critical heap
        if (hitCriticalThreshold) {
            vmRecoversFromCriticalHeap(server1);
        }
    } finally {
        stopServer(server1);
        stopServer(server2);
    }
}
Also used : DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Query(org.apache.geode.cache.query.Query) CacheException(org.apache.geode.cache.CacheException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) Host(org.apache.geode.test.dunit.Host) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) QueryExecutionTimeoutException(org.apache.geode.cache.query.QueryExecutionTimeoutException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException) CacheException(org.apache.geode.cache.CacheException) QueryExecutionLowMemoryException(org.apache.geode.cache.query.QueryExecutionLowMemoryException) QueryException(org.apache.geode.cache.query.QueryException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) SelectResults(org.apache.geode.cache.query.SelectResults) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryService(org.apache.geode.cache.query.QueryService) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable)

Example 5 with FunctionDomainException

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

the class ResourceManagerWithQueryMonitorDUnitTest method doCriticalMemoryHitTest.

private void doCriticalMemoryHitTest(final String regionName, boolean createPR, final int criticalThreshold, final boolean disabledQueryMonitorForLowMem, final int queryTimeout, final boolean hitCriticalThreshold) throws Exception {
    // create region on the server
    final Host host = Host.getHost(0);
    final VM server = host.getVM(0);
    final VM client = host.getVM(1);
    final int numObjects = 200;
    try {
        final int port = AvailablePortHelper.getRandomAvailableTCPPort();
        startCacheServer(server, port, criticalThreshold, disabledQueryMonitorForLowMem, queryTimeout, regionName, createPR, 0);
        startClient(client, server, port, regionName);
        populateData(server, regionName, numObjects);
        doTestCriticalHeapAndQueryTimeout(server, client, regionName, disabledQueryMonitorForLowMem, queryTimeout, hitCriticalThreshold);
        // Pause for a second and then let's recover
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        // Recover from critical heap
        if (hitCriticalThreshold) {
            vmRecoversFromCriticalHeap(server);
        }
        // Check to see if query execution is ok under "normal" or "healthy" conditions
        client.invoke(new CacheSerializableRunnable("Executing query when system is 'Normal'") {

            public void run2() {
                try {
                    QueryService qs = getCache().getQueryService();
                    Query query = qs.newQuery("Select * From /" + regionName);
                    SelectResults results = (SelectResults) query.execute();
                    assertEquals(numObjects, results.size());
                } catch (QueryInvocationTargetException e) {
                    assertFalse(true);
                } catch (NameResolutionException e) {
                    assertFalse(true);
                } catch (TypeMismatchException e) {
                    assertFalse(true);
                } catch (FunctionDomainException e) {
                    assertFalse(true);
                }
            }
        });
        // Execute a critical heap event/ query timeout test again
        doTestCriticalHeapAndQueryTimeout(server, client, regionName, disabledQueryMonitorForLowMem, queryTimeout, hitCriticalThreshold);
        // Recover from critical heap
        if (hitCriticalThreshold) {
            vmRecoversFromCriticalHeap(server);
        }
    } finally {
        stopServer(server);
    }
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Query(org.apache.geode.cache.query.Query) QueryService(org.apache.geode.cache.query.QueryService) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) VM(org.apache.geode.test.dunit.VM) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) Host(org.apache.geode.test.dunit.Host) NameResolutionException(org.apache.geode.cache.query.NameResolutionException)

Aggregations

FunctionDomainException (org.apache.geode.cache.query.FunctionDomainException)15 NameResolutionException (org.apache.geode.cache.query.NameResolutionException)14 QueryInvocationTargetException (org.apache.geode.cache.query.QueryInvocationTargetException)14 TypeMismatchException (org.apache.geode.cache.query.TypeMismatchException)14 Query (org.apache.geode.cache.query.Query)12 SelectResults (org.apache.geode.cache.query.SelectResults)11 QueryService (org.apache.geode.cache.query.QueryService)9 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)9 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)6 Host (org.apache.geode.test.dunit.Host)6 VM (org.apache.geode.test.dunit.VM)6 QueryException (org.apache.geode.cache.query.QueryException)4 QueryExecutionLowMemoryException (org.apache.geode.cache.query.QueryExecutionLowMemoryException)4 QueryExecutionTimeoutException (org.apache.geode.cache.query.QueryExecutionTimeoutException)4 ArrayList (java.util.ArrayList)3 Cache (org.apache.geode.cache.Cache)3 CacheClosedException (org.apache.geode.cache.CacheClosedException)3 CacheException (org.apache.geode.cache.CacheException)3 ServerOperationException (org.apache.geode.cache.client.ServerOperationException)3 QueryInvalidException (org.apache.geode.cache.query.QueryInvalidException)3