Search in sources :

Example 1 with Execution

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

the class LuceneIndexCommands method getIndexListing.

@SuppressWarnings("unchecked")
protected List<LuceneIndexDetails> getIndexListing() {
    final Execution functionExecutor = getMembersFunctionExecutor(getMembers(getCache()));
    if (functionExecutor instanceof AbstractExecution) {
        ((AbstractExecution) functionExecutor).setIgnoreDepartedMembers(true);
    }
    final ResultCollector resultsCollector = functionExecutor.execute(new LuceneListIndexFunction());
    final List<Set<LuceneIndexDetails>> results = (List<Set<LuceneIndexDetails>>) resultsCollector.getResult();
    List<LuceneIndexDetails> sortedResults = results.stream().flatMap(set -> set.stream()).sorted().collect(Collectors.toList());
    LinkedHashSet<LuceneIndexDetails> uniqResults = new LinkedHashSet<>();
    uniqResults.addAll(sortedResults);
    sortedResults.clear();
    sortedResults.addAll(uniqResults);
    return sortedResults;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AbstractExecution(org.apache.geode.internal.cache.execute.AbstractExecution) Execution(org.apache.geode.cache.execute.Execution) AbstractExecution(org.apache.geode.internal.cache.execute.AbstractExecution) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) LuceneListIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneListIndexFunction) ArrayList(java.util.ArrayList) List(java.util.List) ResultCollector(org.apache.geode.cache.execute.ResultCollector)

Example 2 with Execution

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

the class PageableLuceneQueryResultsImplJUnitTest method setUp.

@Before
public void setUp() {
    hits = new ArrayList<EntryScore<String>>();
    for (int i = 0; i < 23; i++) {
        hits.add(new EntryScore("key_" + i, i));
        expected.add(new LuceneResultStructImpl<String, String>("key_" + i, "value_" + i, i));
    }
    userRegion = mock(Region.class);
    final ResultCollector collector = mock(ResultCollector.class);
    execution = mock(Execution.class);
    when(execution.withFilter(any())).thenReturn(execution);
    when(execution.withCollector(any())).thenReturn(execution);
    when(execution.execute(anyString())).thenReturn(collector);
    when(collector.getResult()).then(new Answer() {

        @Override
        public Map answer(InvocationOnMock invocation) throws Throwable {
            ArgumentCaptor<Set> captor = ArgumentCaptor.forClass(Set.class);
            verify(execution, atLeast(1)).withFilter(captor.capture());
            Collection<String> keys = captor.getValue();
            Map<String, String> results = new HashMap<String, String>();
            for (String key : keys) {
                results.put(key, key.replace("key_", "value_"));
            }
            return results;
        }
    });
}
Also used : Set(java.util.Set) ArgumentCaptor(org.mockito.ArgumentCaptor) Matchers.anyString(org.mockito.Matchers.anyString) Answer(org.mockito.stubbing.Answer) Execution(org.apache.geode.cache.execute.Execution) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) EntryScore(org.apache.geode.cache.lucene.internal.distributed.EntryScore) ResultCollector(org.apache.geode.cache.execute.ResultCollector) HashMap(java.util.HashMap) Map(java.util.Map) Before(org.junit.Before)

Example 3 with Execution

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

the class CommonCrudController method servers.

@RequestMapping(method = { RequestMethod.GET }, value = "/servers", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
@ApiOperation(value = "fetch all REST enabled servers in the DS", notes = "Find all gemfire node where developer REST service is up and running!", response = void.class)
@ApiResponses({ @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 401, message = "Invalid Username or Password."), @ApiResponse(code = 403, message = "Insufficient privileges for operation."), @ApiResponse(code = 500, message = "if GemFire throws an error or exception") })
@PreAuthorize("@securityService.authorize('CLUSTER', 'READ')")
public ResponseEntity<?> servers() {
    logger.debug("Executing function to get REST enabled gemfire nodes in the DS!");
    Execution function;
    try {
        function = FunctionService.onMembers(getAllMembersInDS());
    } catch (FunctionException fe) {
        throw new GemfireRestException("Disributed system does not contain any valid data node that can host REST service!", fe);
    }
    try {
        final ResultCollector<?, ?> results = function.withCollector(new RestServersResultCollector()).execute(FindRestEnabledServersFunction.FIND_REST_ENABLED_SERVERS_FUNCTION_ID);
        Object functionResult = results.getResult();
        if (functionResult instanceof List<?>) {
            final HttpHeaders headers = new HttpHeaders();
            headers.setLocation(toUri("servers"));
            try {
                String functionResultAsJson = JSONUtils.convertCollectionToJson((ArrayList<Object>) functionResult);
                return new ResponseEntity<>(functionResultAsJson, headers, HttpStatus.OK);
            } catch (JSONException e) {
                throw new GemfireRestException("Could not convert function results into Restful (JSON) format!", e);
            }
        } else {
            throw new GemfireRestException("Function has returned results that could not be converted into Restful (JSON) format!");
        }
    } catch (ClassCastException cce) {
        throw new GemfireRestException("Key is of an inappropriate type for this region!", cce);
    } catch (NullPointerException npe) {
        throw new GemfireRestException("Specified key is null and this region does not permit null keys!", npe);
    } catch (LowMemoryException lme) {
        throw new GemfireRestException("Server has encountered low memory condition!", lme);
    } catch (IllegalArgumentException ie) {
        throw new GemfireRestException("Input parameter is null! ", ie);
    } catch (FunctionException fe) {
        throw new GemfireRestException("Server has encountered error while executing the function!", fe);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) FunctionException(org.apache.geode.cache.execute.FunctionException) JSONException(org.json.JSONException) GemfireRestException(org.apache.geode.rest.internal.web.exception.GemfireRestException) ResponseEntity(org.springframework.http.ResponseEntity) Execution(org.apache.geode.cache.execute.Execution) RestServersResultCollector(org.apache.geode.rest.internal.web.controllers.support.RestServersResultCollector) ArrayList(java.util.ArrayList) List(java.util.List) LowMemoryException(org.apache.geode.cache.LowMemoryException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with Execution

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

the class FunctionAccessController method execute.

/**
   * Execute a function on Gemfire data node using REST API call. Arguments to the function are
   * passed as JSON string in the request body.
   * 
   * @param functionId represents function to be executed
   * @param region list of regions on which function to be executed.
   * @param members list of nodes on which function to be executed.
   * @param groups list of groups on which function to be executed.
   * @param filter list of keys which the function will use to determine on which node to execute
   *        the function.
   * @param argsInBody function argument as a JSON document
   *
   * @return result as a JSON document
   */
@RequestMapping(method = RequestMethod.POST, value = "/{functionId:.+}", produces = { MediaType.APPLICATION_JSON_VALUE })
@ApiOperation(value = "execute function", notes = "Execute function with arguments on regions, members, or group(s). By default function will be executed on all nodes if none of (onRegion, onMembers, onGroups) specified", response = void.class)
@ApiResponses({ @ApiResponse(code = 200, message = "OK."), @ApiResponse(code = 401, message = "Invalid Username or Password."), @ApiResponse(code = 403, message = "Insufficient privileges for operation."), @ApiResponse(code = 500, message = "if GemFire throws an error or exception"), @ApiResponse(code = 400, message = "if Function arguments specified as JSON document in the request body is invalid") })
@ResponseBody
@ResponseStatus(HttpStatus.OK)
@PreAuthorize("@securityService.authorize('DATA', 'WRITE')")
public ResponseEntity<String> execute(@PathVariable("functionId") String functionId, @RequestParam(value = "onRegion", required = false) String region, @RequestParam(value = "onMembers", required = false) final String[] members, @RequestParam(value = "onGroups", required = false) final String[] groups, @RequestParam(value = "filter", required = false) final String[] filter, @RequestBody(required = false) final String argsInBody) {
    Execution function = null;
    functionId = decode(functionId);
    if (StringUtils.hasText(region)) {
        logger.debug("Executing Function ({}) with arguments ({}) on Region ({})...", functionId, ArrayUtils.toString(argsInBody), region);
        region = decode(region);
        try {
            function = FunctionService.onRegion(getRegion(region));
        } catch (FunctionException fe) {
            throw new GemfireRestException(String.format("The Region identified by name (%1$s) could not found!", region), fe);
        }
    } else if (ArrayUtils.isNotEmpty(members)) {
        logger.debug("Executing Function ({}) with arguments ({}) on Member ({})...", functionId, ArrayUtils.toString(argsInBody), ArrayUtils.toString(members));
        try {
            function = FunctionService.onMembers(getMembers(members));
        } catch (FunctionException fe) {
            throw new GemfireRestException("Could not found the specified members in distributed system!", fe);
        }
    } else if (ArrayUtils.isNotEmpty(groups)) {
        logger.debug("Executing Function ({}) with arguments ({}) on Groups ({})...", functionId, ArrayUtils.toString(argsInBody), ArrayUtils.toString(groups));
        try {
            function = FunctionService.onMembers(groups);
        } catch (FunctionException fe) {
            throw new GemfireRestException("no member(s) are found belonging to the provided group(s)!", fe);
        }
    } else {
        // Default case is to execute function on all existing data node in DS, document this.
        logger.debug("Executing Function ({}) with arguments ({}) on all Members...", functionId, ArrayUtils.toString(argsInBody));
        try {
            function = FunctionService.onMembers(getAllMembersInDS());
        } catch (FunctionException fe) {
            throw new GemfireRestException("Distributed system does not contain any valid data node to run the specified  function!", fe);
        }
    }
    if (!ArrayUtils.isEmpty(filter)) {
        logger.debug("Executing Function ({}) with filter ({})", functionId, ArrayUtils.toString(filter));
        Set filter1 = ArrayUtils.asSet(filter);
        function = function.withFilter(filter1);
    }
    final ResultCollector<?, ?> results;
    try {
        if (argsInBody != null) {
            Object[] args = jsonToObjectArray(argsInBody);
            // execute function with specified arguments
            if (args.length == 1) {
                results = function.setArguments(args[0]).execute(functionId);
            } else {
                results = function.setArguments(args).execute(functionId);
            }
        } else {
            // execute function with no args
            results = function.execute(functionId);
        }
    } catch (ClassCastException cce) {
        throw new GemfireRestException("Key is of an inappropriate type for this region!", cce);
    } catch (NullPointerException npe) {
        throw new GemfireRestException("Specified key is null and this region does not permit null keys!", npe);
    } catch (LowMemoryException lme) {
        throw new GemfireRestException("Server has encountered low memory condition!", lme);
    } catch (IllegalArgumentException ie) {
        throw new GemfireRestException("Input parameter is null! ", ie);
    } catch (FunctionException fe) {
        throw new GemfireRestException("Server has encountered error while executing the function!", fe);
    }
    try {
        final HttpHeaders headers = new HttpHeaders();
        headers.setLocation(toUri("functions", functionId));
        Object functionResult = null;
        if (results instanceof NoResult)
            return new ResponseEntity<>("", headers, HttpStatus.OK);
        functionResult = results.getResult();
        if (functionResult instanceof List<?>) {
            try {
                @SuppressWarnings("unchecked") String functionResultAsJson = JSONUtils.convertCollectionToJson((ArrayList<Object>) functionResult);
                return new ResponseEntity<>(functionResultAsJson, headers, HttpStatus.OK);
            } catch (JSONException e) {
                throw new GemfireRestException("Could not convert function results into Restful (JSON) format!", e);
            }
        } else {
            throw new GemfireRestException("Function has returned results that could not be converted into Restful (JSON) format!");
        }
    } catch (FunctionException fe) {
        fe.printStackTrace();
        throw new GemfireRestException("Server has encountered an error while processing function execution!", fe);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Set(java.util.Set) FunctionException(org.apache.geode.cache.execute.FunctionException) JSONException(org.json.JSONException) GemfireRestException(org.apache.geode.rest.internal.web.exception.GemfireRestException) ResponseEntity(org.springframework.http.ResponseEntity) Execution(org.apache.geode.cache.execute.Execution) ArrayList(java.util.ArrayList) List(java.util.List) NoResult(org.apache.geode.internal.cache.execute.NoResult) LowMemoryException(org.apache.geode.cache.LowMemoryException) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 5 with Execution

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

the class CommitFunction method execute.

public void execute(FunctionContext context) {
    Cache cache = CacheFactory.getAnyInstance();
    TXId txId = null;
    try {
        txId = (TXId) context.getArguments();
    } catch (ClassCastException e) {
        logger.info("CommitFunction should be invoked with a TransactionId as an argument i.e. setArguments(txId).execute(function)");
        throw e;
    }
    DistributedMember member = txId.getMemberId();
    Boolean result = false;
    final boolean isDebugEnabled = logger.isDebugEnabled();
    if (cache.getDistributedSystem().getDistributedMember().equals(member)) {
        if (isDebugEnabled) {
            logger.debug("CommitFunction: for transaction: {} committing locally", txId);
        }
        CacheTransactionManager txMgr = cache.getCacheTransactionManager();
        if (txMgr.tryResume(txId)) {
            if (isDebugEnabled) {
                logger.debug("CommitFunction: resumed transaction: {}", txId);
            }
            txMgr.commit();
            result = true;
        }
    } else {
        ArrayList args = new ArrayList();
        args.add(txId);
        args.add(NestedTransactionFunction.COMMIT);
        Execution ex = FunctionService.onMember(member).setArguments(args);
        if (isDebugEnabled) {
            logger.debug("CommitFunction: for transaction: {} executing NestedTransactionFunction on member: {}", txId, member);
        }
        try {
            List list = (List) ex.execute(new NestedTransactionFunction()).getResult();
            result = (Boolean) list.get(0);
        } catch (FunctionException fe) {
            if (fe.getCause() instanceof FunctionInvocationTargetException) {
                throw new TransactionDataNodeHasDepartedException("Could not commit on member:" + member);
            } else {
                throw fe;
            }
        }
    }
    if (isDebugEnabled) {
        logger.debug("CommitFunction: for transaction: {} returning result: {}", txId, result);
    }
    context.getResultSender().lastResult(result);
}
Also used : ArrayList(java.util.ArrayList) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) TransactionDataNodeHasDepartedException(org.apache.geode.cache.TransactionDataNodeHasDepartedException) Execution(org.apache.geode.cache.execute.Execution) TXId(org.apache.geode.internal.cache.TXId) DistributedMember(org.apache.geode.distributed.DistributedMember) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) ArrayList(java.util.ArrayList) List(java.util.List) Cache(org.apache.geode.cache.Cache)

Aggregations

Execution (org.apache.geode.cache.execute.Execution)217 ResultCollector (org.apache.geode.cache.execute.ResultCollector)163 HashSet (java.util.HashSet)144 ArrayList (java.util.ArrayList)139 FunctionException (org.apache.geode.cache.execute.FunctionException)131 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)127 Function (org.apache.geode.cache.execute.Function)121 TestFunction (org.apache.geode.internal.cache.functions.TestFunction)108 IgnoredException (org.apache.geode.test.dunit.IgnoredException)108 List (java.util.List)106 Test (org.junit.Test)85 Iterator (java.util.Iterator)79 Region (org.apache.geode.cache.Region)79 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)74 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)74 VM (org.apache.geode.test.dunit.VM)70 FunctionInvocationTargetException (org.apache.geode.cache.execute.FunctionInvocationTargetException)69 Host (org.apache.geode.test.dunit.Host)69 Set (java.util.Set)65 CacheClosedException (org.apache.geode.cache.CacheClosedException)59