Search in sources :

Example 21 with RegionFunctionContext

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

the class LuceneQueryFunction method execute.

@Override
public void execute(FunctionContext context) {
    RegionFunctionContext ctx = (RegionFunctionContext) context;
    ResultSender<TopEntriesCollector> resultSender = ctx.getResultSender();
    Region region = ctx.getDataSet();
    LuceneFunctionContext<IndexResultCollector> searchContext = (LuceneFunctionContext) ctx.getArguments();
    if (searchContext == null) {
        throw new IllegalArgumentException("Missing search context");
    }
    LuceneQueryProvider queryProvider = searchContext.getQueryProvider();
    if (queryProvider == null) {
        throw new IllegalArgumentException("Missing query provider");
    }
    LuceneIndexImpl index = getLuceneIndex(region, searchContext);
    if (index == null) {
        throw new LuceneIndexNotFoundException(searchContext.getIndexName(), region.getFullPath());
    }
    RepositoryManager repoManager = index.getRepositoryManager();
    LuceneIndexStats stats = index.getIndexStats();
    Query query = getQuery(queryProvider, index);
    if (logger.isDebugEnabled()) {
        logger.debug("Executing lucene query: {}, on region {}", query, region.getFullPath());
    }
    int resultLimit = searchContext.getLimit();
    CollectorManager manager = (searchContext == null) ? null : searchContext.getCollectorManager();
    if (manager == null) {
        manager = new TopEntriesCollectorManager(null, resultLimit);
    }
    Collection<IndexResultCollector> results = new ArrayList<>();
    TopEntriesCollector mergedResult = null;
    try {
        long start = stats.startQuery();
        Collection<IndexRepository> repositories = null;
        try {
            repositories = repoManager.getRepositories(ctx);
            for (IndexRepository repo : repositories) {
                IndexResultCollector collector = manager.newCollector(repo.toString());
                if (logger.isDebugEnabled()) {
                    logger.debug("Executing search on repo: " + repo.toString());
                }
                repo.query(query, resultLimit, collector);
                results.add(collector);
            }
            mergedResult = (TopEntriesCollector) manager.reduce(results);
        } finally {
            stats.endQuery(start, mergedResult == null ? 0 : mergedResult.size());
        }
        stats.incNumberOfQueryExecuted();
        resultSender.lastResult(mergedResult);
    } catch (IOException | BucketNotFoundException | CacheClosedException | PrimaryBucketException e) {
        logger.debug("Exception during lucene query function", e);
        throw new InternalFunctionInvocationTargetException(e);
    }
}
Also used : LuceneIndexStats(org.apache.geode.cache.lucene.internal.LuceneIndexStats) Query(org.apache.lucene.search.Query) LuceneQueryProvider(org.apache.geode.cache.lucene.LuceneQueryProvider) ArrayList(java.util.ArrayList) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) PrimaryBucketException(org.apache.geode.internal.cache.PrimaryBucketException) IndexResultCollector(org.apache.geode.cache.lucene.internal.repository.IndexResultCollector) RepositoryManager(org.apache.geode.cache.lucene.internal.repository.RepositoryManager) IOException(java.io.IOException) CacheClosedException(org.apache.geode.cache.CacheClosedException) IndexRepository(org.apache.geode.cache.lucene.internal.repository.IndexRepository) InternalFunctionInvocationTargetException(org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException) LuceneIndexNotFoundException(org.apache.geode.cache.lucene.LuceneIndexNotFoundException) Region(org.apache.geode.cache.Region) BucketNotFoundException(org.apache.geode.internal.cache.BucketNotFoundException) LuceneIndexImpl(org.apache.geode.cache.lucene.internal.LuceneIndexImpl)

Example 22 with RegionFunctionContext

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

the class WaitUntilFlushedFunction method execute.

@Override
public void execute(FunctionContext context) {
    RegionFunctionContext ctx = (RegionFunctionContext) context;
    ResultSender<Boolean> resultSender = ctx.getResultSender();
    Region region = ctx.getDataSet();
    Cache cache = region.getCache();
    WaitUntilFlushedFunctionContext arg = (WaitUntilFlushedFunctionContext) ctx.getArguments();
    String indexName = arg.getIndexName();
    if (indexName == null) {
        throw new IllegalArgumentException("Missing index name");
    }
    long timeout = arg.getTimeout();
    TimeUnit unit = arg.getTimeunit();
    LuceneService service = LuceneServiceProvider.get(cache);
    LuceneIndexImpl index = (LuceneIndexImpl) service.getIndex(indexName, region.getFullPath());
    boolean result = false;
    String aeqId = LuceneServiceImpl.getUniqueIndexName(indexName, region.getFullPath());
    AsyncEventQueueImpl queue = (AsyncEventQueueImpl) cache.getAsyncEventQueue(aeqId);
    if (queue != null) {
        try {
            result = queue.waitUntilFlushed(timeout, unit);
        } catch (InterruptedException e) {
        }
    } else {
        throw new IllegalArgumentException("The AEQ does not exist for the index " + indexName + " region " + region.getFullPath());
    }
    resultSender.lastResult(result);
}
Also used : RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) LuceneService(org.apache.geode.cache.lucene.LuceneService) Region(org.apache.geode.cache.Region) TimeUnit(java.util.concurrent.TimeUnit) AsyncEventQueueImpl(org.apache.geode.cache.asyncqueue.internal.AsyncEventQueueImpl) LuceneIndexImpl(org.apache.geode.cache.lucene.internal.LuceneIndexImpl) Cache(org.apache.geode.cache.Cache)

Example 23 with RegionFunctionContext

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

the class DumpDirectoryFiles method execute.

@Override
public void execute(FunctionContext context) {
    RegionFunctionContext ctx = (RegionFunctionContext) context;
    if (!(context.getArguments() instanceof String[])) {
        throw new IllegalArgumentException("Arguments should be a string array");
    }
    String[] args = (String[]) context.getArguments();
    if (args.length != 2) {
        throw new IllegalArgumentException("Expected 2 arguments: exportLocation, indexName");
    }
    String exportLocation = args[0];
    String indexName = args[1];
    final Region<Object, Object> region = ctx.getDataSet();
    LuceneService service = LuceneServiceProvider.get(ctx.getDataSet().getCache());
    InternalLuceneIndex index = (InternalLuceneIndex) service.getIndex(indexName, region.getFullPath());
    if (index == null) {
        throw new IllegalStateException("Index not found for region " + region + " index " + indexName);
    }
    final RepositoryManager repoManager = index.getRepositoryManager();
    try {
        final Collection<IndexRepository> repositories = repoManager.getRepositories(ctx);
        repositories.stream().forEach(repo -> {
            final IndexWriter writer = repo.getWriter();
            RegionDirectory directory = (RegionDirectory) writer.getDirectory();
            FileSystem fs = directory.getFileSystem();
            String bucketName = index.getName() + "_" + repo.getRegion().getFullPath();
            bucketName = bucketName.replace("/", "_");
            File bucketDirectory = new File(exportLocation, bucketName);
            bucketDirectory.mkdirs();
            fs.export(bucketDirectory);
        });
        context.getResultSender().lastResult(null);
    } catch (BucketNotFoundException e) {
        throw new FunctionException(e);
    }
}
Also used : FunctionException(org.apache.geode.cache.execute.FunctionException) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) LuceneService(org.apache.geode.cache.lucene.LuceneService) IndexRepository(org.apache.geode.cache.lucene.internal.repository.IndexRepository) IndexWriter(org.apache.lucene.index.IndexWriter) InternalLuceneIndex(org.apache.geode.cache.lucene.internal.InternalLuceneIndex) FileSystem(org.apache.geode.cache.lucene.internal.filesystem.FileSystem) BucketNotFoundException(org.apache.geode.internal.cache.BucketNotFoundException) RepositoryManager(org.apache.geode.cache.lucene.internal.repository.RepositoryManager) File(java.io.File)

Example 24 with RegionFunctionContext

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

the class PRClientServerRegionFunctionExecutionNoSingleHopDUnitTest method serverMultiKeyExecution_FunctionInvocationTargetException.

public static void serverMultiKeyExecution_FunctionInvocationTargetException() {
    Region region = cache.getRegion(PartitionedRegionName);
    assertNotNull(region);
    final HashSet testKeysSet = new HashSet();
    for (int i = (totalNumBuckets.intValue() * 2); i > 0; i--) {
        testKeysSet.add("execKey-" + i);
    }
    DistributedSystem.setThreadsSocketPolicy(false);
    Execution dataSet = FunctionService.onRegion(region);
    int j = 0;
    HashSet origVals = new HashSet();
    for (Iterator i = testKeysSet.iterator(); i.hasNext(); ) {
        Integer val = new Integer(j++);
        origVals.add(val);
        region.put(i.next(), val);
    }
    ResultCollector rc1 = null;
    try {
        rc1 = dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(new FunctionAdapter() {

            public void execute(FunctionContext context) {
                if (((RegionFunctionContext) context).isPossibleDuplicate()) {
                    context.getResultSender().lastResult(new Integer(retryCount));
                    return;
                }
                if (context.getArguments() instanceof Boolean) {
                    throw new FunctionInvocationTargetException("I have been thrown from TestFunction");
                }
            }

            public String getId() {
                return getClass().getName();
            }

            public boolean hasResult() {
                return true;
            }
        });
        List list = (ArrayList) rc1.getResult();
        assertEquals(list.get(0), 0);
    } catch (Throwable e) {
        e.printStackTrace();
        Assert.fail("This is not expected Exception", e);
    }
}
Also used : ArrayList(java.util.ArrayList) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) FunctionContext(org.apache.geode.cache.execute.FunctionContext) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) Execution(org.apache.geode.cache.execute.Execution) Iterator(java.util.Iterator) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) ArrayList(java.util.ArrayList) List(java.util.List) ResultCollector(org.apache.geode.cache.execute.ResultCollector) HashSet(java.util.HashSet)

Example 25 with RegionFunctionContext

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

the class PRClientServerRegionFunctionExecutionSelectorNoSingleHopDUnitTest method serverMultiKeyExecution_FunctionInvocationTargetException.

public static void serverMultiKeyExecution_FunctionInvocationTargetException() {
    Region region = cache.getRegion(PartitionedRegionName);
    assertNotNull(region);
    final HashSet testKeysSet = new HashSet();
    for (int i = (totalNumBuckets.intValue() * 2); i > 0; i--) {
        testKeysSet.add("execKey-" + i);
    }
    DistributedSystem.setThreadsSocketPolicy(false);
    Execution dataSet = FunctionService.onRegion(region);
    int j = 0;
    HashSet origVals = new HashSet();
    for (Iterator i = testKeysSet.iterator(); i.hasNext(); ) {
        Integer val = new Integer(j++);
        origVals.add(val);
        region.put(i.next(), val);
    }
    ResultCollector rc1 = null;
    try {
        rc1 = dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(new FunctionAdapter() {

            public void execute(FunctionContext context) {
                if (((RegionFunctionContext) context).isPossibleDuplicate()) {
                    context.getResultSender().lastResult(new Integer(retryCount));
                    return;
                }
                if (context.getArguments() instanceof Boolean) {
                    throw new FunctionInvocationTargetException("I have been thrown from TestFunction");
                }
            }

            public String getId() {
                return getClass().getName();
            }

            public boolean hasResult() {
                return true;
            }
        });
        List list = (ArrayList) rc1.getResult();
        assertEquals(list.get(0), 0);
    } catch (Throwable e) {
        e.printStackTrace();
        Assert.fail("This is not expected Exception", e);
    }
}
Also used : ArrayList(java.util.ArrayList) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) FunctionContext(org.apache.geode.cache.execute.FunctionContext) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) Execution(org.apache.geode.cache.execute.Execution) Iterator(java.util.Iterator) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) ArrayList(java.util.ArrayList) List(java.util.List) ResultCollector(org.apache.geode.cache.execute.ResultCollector) HashSet(java.util.HashSet)

Aggregations

RegionFunctionContext (org.apache.geode.cache.execute.RegionFunctionContext)38 Region (org.apache.geode.cache.Region)23 ArrayList (java.util.ArrayList)21 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)17 Set (java.util.Set)15 Iterator (java.util.Iterator)14 FunctionContext (org.apache.geode.cache.execute.FunctionContext)12 FunctionAdapter (org.apache.geode.cache.execute.FunctionAdapter)10 HashSet (java.util.HashSet)8 Serializable (java.io.Serializable)6 Map (java.util.Map)6 FunctionException (org.apache.geode.cache.execute.FunctionException)6 IgnoredException (org.apache.geode.test.dunit.IgnoredException)6 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)6 Test (org.junit.Test)6 HashMap (java.util.HashMap)5 List (java.util.List)5 CacheClosedException (org.apache.geode.cache.CacheClosedException)5 Execution (org.apache.geode.cache.execute.Execution)5 ResultCollector (org.apache.geode.cache.execute.ResultCollector)5