Search in sources :

Example 1 with LowMemoryException

use of org.apache.geode.cache.LowMemoryException 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 2 with LowMemoryException

use of org.apache.geode.cache.LowMemoryException 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 3 with LowMemoryException

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

the class MemoryThresholdsOffHeapDUnitTest method prRemotePutRejection.

private void prRemotePutRejection(boolean cacheClose, boolean localDestroy, final boolean useTx) throws Exception {
    final Host host = Host.getHost(0);
    final VM accessor = host.getVM(0);
    final VM[] servers = new VM[3];
    servers[0] = host.getVM(1);
    servers[1] = host.getVM(2);
    servers[2] = host.getVM(3);
    final String regionName = "offHeapPRRemotePutRejection";
    final int redundancy = 1;
    startCacheServer(servers[0], 0f, 90f, regionName, true, /* createPR */
    false, /* notifyBySubscription */
    redundancy);
    startCacheServer(servers[1], 0f, 90f, regionName, true, /* createPR */
    false, /* notifyBySubscription */
    redundancy);
    startCacheServer(servers[2], 0f, 90f, regionName, true, /* createPR */
    false, /* notifyBySubscription */
    redundancy);
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            getSystem(getOffHeapProperties());
            getCache();
            AttributesFactory factory = new AttributesFactory();
            PartitionAttributesFactory paf = new PartitionAttributesFactory();
            paf.setRedundantCopies(redundancy);
            paf.setLocalMaxMemory(0);
            paf.setTotalNumBuckets(11);
            factory.setPartitionAttributes(paf.create());
            factory.setOffHeap(true);
            createRegion(regionName, factory.create());
            return null;
        }
    });
    doPuts(accessor, regionName, false, false);
    final Range r1 = Range.DEFAULT;
    doPutAlls(accessor, regionName, false, false, r1);
    servers[0].invoke(addExpectedException);
    servers[1].invoke(addExpectedException);
    servers[2].invoke(addExpectedException);
    setUsageAboveCriticalThreshold(servers[0], regionName);
    final Set<InternalDistributedMember> criticalMembers = (Set) servers[0].invoke(new SerializableCallable() {

        public Object call() throws Exception {
            final PartitionedRegion pr = (PartitionedRegion) getRootRegion().getSubregion(regionName);
            final int hashKey = PartitionedRegionHelper.getHashKey(pr, null, "oh5", null, null);
            return pr.getRegionAdvisor().getBucketOwners(hashKey);
        }
    });
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            final PartitionedRegion pr = (PartitionedRegion) getRootRegion().getSubregion(regionName);
            WaitCriterion wc = new WaitCriterion() {

                public String description() {
                    return "remote bucket not marked sick";
                }

                public boolean done() {
                    boolean keyFoundOnSickMember = false;
                    boolean caughtException = false;
                    for (int i = 0; i < 20; i++) {
                        Integer key = Integer.valueOf(i);
                        int hKey = PartitionedRegionHelper.getHashKey(pr, null, key, null, null);
                        Set<InternalDistributedMember> owners = pr.getRegionAdvisor().getBucketOwners(hKey);
                        final boolean hasCriticalOwners = owners.removeAll(criticalMembers);
                        if (hasCriticalOwners) {
                            keyFoundOnSickMember = true;
                            try {
                                if (useTx)
                                    getCache().getCacheTransactionManager().begin();
                                pr.getCache().getLogger().fine("SWAP:putting in tx:" + useTx);
                                pr.put(key, "value");
                                if (useTx)
                                    getCache().getCacheTransactionManager().commit();
                            } catch (LowMemoryException ex) {
                                caughtException = true;
                                if (useTx)
                                    getCache().getCacheTransactionManager().rollback();
                            }
                        } else {
                            // puts on healthy member should continue
                            pr.put(key, "value");
                        }
                    }
                    return keyFoundOnSickMember && caughtException;
                }
            };
            Wait.waitForCriterion(wc, 10000, 10, true);
            return null;
        }
    });
    {
        Range r2 = new Range(r1, r1.width() + 1);
        doPutAlls(accessor, regionName, false, true, r2);
    }
    // Find all VMs that have a critical region
    SerializableCallable getMyId = new SerializableCallable() {

        public Object call() throws Exception {
            return ((GemFireCacheImpl) getCache()).getMyId();
        }
    };
    final Set<VM> criticalServers = new HashSet<VM>();
    for (final VM server : servers) {
        DistributedMember member = (DistributedMember) server.invoke(getMyId);
        if (criticalMembers.contains(member)) {
            criticalServers.add(server);
        }
    }
    if (localDestroy) {
        // local destroy the region on sick members
        for (final VM vm : criticalServers) {
            vm.invoke(new SerializableCallable("local destroy sick member") {

                public Object call() throws Exception {
                    Region r = getRootRegion().getSubregion(regionName);
                    LogWriterUtils.getLogWriter().info("PRLocalDestroy");
                    r.localDestroyRegion();
                    return null;
                }
            });
        }
    } else if (cacheClose) {
        // close cache on sick members
        for (final VM vm : criticalServers) {
            vm.invoke(new SerializableCallable("close cache sick member") {

                public Object call() throws Exception {
                    getCache().close();
                    return null;
                }
            });
        }
    } else {
        setUsageBelowEviction(servers[0], regionName);
        servers[0].invoke(removeExpectedException);
        servers[1].invoke(removeExpectedException);
        servers[2].invoke(removeExpectedException);
    }
    // do put all in a loop to allow distribution of message
    accessor.invoke(new SerializableCallable("Put in a loop") {

        public Object call() throws Exception {
            final Region r = getRootRegion().getSubregion(regionName);
            WaitCriterion wc = new WaitCriterion() {

                public String description() {
                    return "pr should have gone un-critical";
                }

                public boolean done() {
                    boolean done = true;
                    for (int i = 0; i < 20; i++) {
                        try {
                            r.put(i, "value");
                        } catch (LowMemoryException e) {
                            // expected
                            done = false;
                        }
                    }
                    return done;
                }
            };
            Wait.waitForCriterion(wc, 10000, 10, true);
            return null;
        }
    });
    doPutAlls(accessor, regionName, false, false, r1);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Host(org.apache.geode.test.dunit.Host) Range(org.apache.geode.cache.management.MemoryThresholdsDUnitTest.Range) IgnoredException(org.apache.geode.test.dunit.IgnoredException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) LowMemoryException(org.apache.geode.cache.LowMemoryException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) CacheException(org.apache.geode.cache.CacheException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) LocalRegion(org.apache.geode.internal.cache.LocalRegion) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) ProxyBucketRegion(org.apache.geode.internal.cache.ProxyBucketRegion) LowMemoryException(org.apache.geode.cache.LowMemoryException) HashSet(java.util.HashSet)

Example 4 with LowMemoryException

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

the class MemoryThresholdsDUnitTest method prRemotePutRejection.

private void prRemotePutRejection(boolean cacheClose, boolean localDestroy, final boolean useTx) throws Exception {
    final Host host = Host.getHost(0);
    final VM accessor = host.getVM(0);
    final VM server1 = host.getVM(1);
    final VM server2 = host.getVM(2);
    final VM server3 = host.getVM(3);
    final String regionName = "testPrRejection";
    final int redundancy = 1;
    final ServerPorts ports1 = startCacheServer(server1, 80f, 90f, regionName, true, /* createPR */
    false, /* notifyBySubscription */
    redundancy);
    ServerPorts ports2 = startCacheServer(server2, 80f, 90f, regionName, true, /* createPR */
    false, /* notifyBySubscription */
    redundancy);
    ServerPorts ports3 = startCacheServer(server3, 80f, 90f, regionName, true, /* createPR */
    false, /* notifyBySubscription */
    redundancy);
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            getSystem(getServerProperties());
            getCache();
            AttributesFactory factory = new AttributesFactory();
            PartitionAttributesFactory paf = new PartitionAttributesFactory();
            paf.setRedundantCopies(redundancy);
            paf.setLocalMaxMemory(0);
            paf.setTotalNumBuckets(11);
            factory.setPartitionAttributes(paf.create());
            createRegion(regionName, factory.create());
            return null;
        }
    });
    doPuts(accessor, regionName, false, false);
    final Range r1 = Range.DEFAULT;
    doPutAlls(accessor, regionName, false, false, r1);
    SerializableCallable getMyId = new SerializableCallable() {

        public Object call() throws Exception {
            return ((GemFireCacheImpl) getCache()).getMyId();
        }
    };
    final DistributedMember server1Id = (DistributedMember) server1.invoke(getMyId);
    setUsageAboveCriticalThreshold(server1);
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            final PartitionedRegion pr = (PartitionedRegion) getRootRegion().getSubregion(regionName);
            final String regionPath = getRootRegion().getSubregion(regionName).getFullPath();
            // server1 is sick, look for a key on server1, and attempt put again
            WaitCriterion wc = new WaitCriterion() {

                public String description() {
                    return "remote bucket not marked sick";
                }

                public boolean done() {
                    boolean keyFoundOnSickMember = false;
                    boolean caughtException = false;
                    for (int i = 0; i < 20; i++) {
                        Integer key = Integer.valueOf(i);
                        int hKey = PartitionedRegionHelper.getHashKey(pr, null, key, null, null);
                        Set<InternalDistributedMember> owners = pr.getRegionAdvisor().getBucketOwners(hKey);
                        if (owners.contains(server1Id)) {
                            keyFoundOnSickMember = true;
                            try {
                                if (useTx)
                                    getCache().getCacheTransactionManager().begin();
                                pr.getCache().getLogger().fine("SWAP:putting in tx:" + useTx);
                                pr.put(key, "value");
                                if (useTx)
                                    getCache().getCacheTransactionManager().commit();
                            } catch (LowMemoryException ex) {
                                caughtException = true;
                                if (useTx)
                                    getCache().getCacheTransactionManager().rollback();
                            }
                        } else {
                            // puts on healthy member should continue
                            pr.put(key, "value");
                        }
                    }
                    return keyFoundOnSickMember && caughtException;
                }
            };
            Wait.waitForCriterion(wc, 30000, 10, true);
            return null;
        }
    });
    {
        Range r2 = new Range(r1, r1.width() + 1);
        doPutAlls(accessor, regionName, false, true, r2);
    }
    if (localDestroy) {
        // local destroy the region on sick member
        server1.invoke(new SerializableCallable("local destroy sick member") {

            public Object call() throws Exception {
                Region r = getRootRegion().getSubregion(regionName);
                LogWriterUtils.getLogWriter().info("PRLocalDestroy");
                r.localDestroyRegion();
                return null;
            }
        });
    } else if (cacheClose) {
        // close cache on sick member
        server1.invoke(new SerializableCallable("close cache sick member") {

            public Object call() throws Exception {
                getCache().close();
                return null;
            }
        });
    } else {
        setUsageBelowEviction(server1);
    }
    // do put all in a loop to allow distribution of message
    accessor.invoke(new SerializableCallable("Put in a loop") {

        public Object call() throws Exception {
            final Region r = getRootRegion().getSubregion(regionName);
            WaitCriterion wc = new WaitCriterion() {

                public String description() {
                    return "pr should have gone un-critical";
                }

                public boolean done() {
                    boolean done = true;
                    for (int i = 0; i < 20; i++) {
                        try {
                            r.put(i, "value");
                        } catch (LowMemoryException e) {
                            // expected
                            done = false;
                        }
                    }
                    return done;
                }
            };
            Wait.waitForCriterion(wc, 30000, 10, true);
            return null;
        }
    });
    doPutAlls(accessor, regionName, false, false, r1);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Host(org.apache.geode.test.dunit.Host) IgnoredException(org.apache.geode.test.dunit.IgnoredException) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) LowMemoryException(org.apache.geode.cache.LowMemoryException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) CacheException(org.apache.geode.cache.CacheException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) LowMemoryException(org.apache.geode.cache.LowMemoryException)

Example 5 with LowMemoryException

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

the class MemoryThresholdsDUnitTest method testFunctionExecutionRejection.

@Test
public void testFunctionExecutionRejection() throws Exception {
    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 String regionName = "FuncRej";
    ServerPorts ports1 = startCacheServer(server1, 80f, 90f, regionName, false, /* createPR */
    false, /* notifyBySubscription */
    0);
    ServerPorts ports2 = startCacheServer(server2, 80f, 90f, regionName, false, /* createPR */
    false, /* notifyBySubscription */
    0);
    startClient(client, server1, ports1.getPort(), regionName);
    registerTestMemoryThresholdListener(server1);
    registerTestMemoryThresholdListener(server2);
    final RejectFunction function = new RejectFunction();
    final RejectFunction function2 = new RejectFunction("noRejFunc", false);
    Invoke.invokeInEveryVM(new SerializableCallable("register function") {

        public Object call() throws Exception {
            FunctionService.registerFunction(function);
            FunctionService.registerFunction(function2);
            return null;
        }
    });
    client.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Pool p = PoolManager.find("pool1");
            assertTrue(p != null);
            FunctionService.onServers(p).execute(function);
            FunctionService.onServers(p).execute(function2);
            return null;
        }
    });
    final DistributedMember s1 = (DistributedMember) server1.invoke(getDistributedMember);
    server2.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            FunctionService.onMembers().execute(function);
            FunctionService.onMember(s1).execute(function);
            FunctionService.onMembers().execute(function2);
            FunctionService.onMember(s1).execute(function2);
            return null;
        }
    });
    setUsageAboveCriticalThreshold(server1);
    verifyListenerValue(server2, MemoryState.CRITICAL, 1, true);
    server1.invoke(addExpectedFunctionException);
    server2.invoke(addExpectedFunctionException);
    client.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Pool p = PoolManager.find("pool1");
            assertTrue(p != null);
            try {
                FunctionService.onServers(p).execute(function);
                fail("expected LowMemoryExcception was not thrown");
            } catch (ServerOperationException e) {
                if (!(e.getCause().getMessage().matches(".*low.*memory.*"))) {
                    Assert.fail("unexpected exception", e);
                }
            // expected
            }
            FunctionService.onServers(p).execute(function2);
            return null;
        }
    });
    server2.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            try {
                FunctionService.onMembers().execute(function);
                fail("expected LowMemoryExcception was not thrown");
            } catch (LowMemoryException e) {
            // expected
            }
            try {
                FunctionService.onMember(s1).execute(function);
                fail("expected LowMemoryExcception was not thrown");
            } catch (LowMemoryException e) {
            // expected
            }
            FunctionService.onMembers().execute(function2);
            FunctionService.onMember(s1).execute(function2);
            return null;
        }
    });
    server1.invoke(removeExpectedFunctionException);
    server2.invoke(removeExpectedFunctionException);
}
Also used : VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) Host(org.apache.geode.test.dunit.Host) Pool(org.apache.geode.cache.client.Pool) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) LowMemoryException(org.apache.geode.cache.LowMemoryException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) CacheException(org.apache.geode.cache.CacheException) LowMemoryException(org.apache.geode.cache.LowMemoryException) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Aggregations

LowMemoryException (org.apache.geode.cache.LowMemoryException)23 DistributedMember (org.apache.geode.distributed.DistributedMember)16 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)15 FunctionException (org.apache.geode.cache.execute.FunctionException)12 Set (java.util.Set)9 CacheException (org.apache.geode.cache.CacheException)8 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)8 ServerOperationException (org.apache.geode.cache.client.ServerOperationException)8 InternalCache (org.apache.geode.internal.cache.InternalCache)8 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)8 HashSet (java.util.HashSet)7 Host (org.apache.geode.test.dunit.Host)7 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)7 VM (org.apache.geode.test.dunit.VM)7 TransactionException (org.apache.geode.cache.TransactionException)6 IgnoredException (org.apache.geode.test.dunit.IgnoredException)6 IOException (java.io.IOException)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 HashMap (java.util.HashMap)4 AttributesFactory (org.apache.geode.cache.AttributesFactory)4