Search in sources :

Example 16 with CacheClosedException

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

the class GemFireCacheImpl method shutDownOnePRGracefully.

private void shutDownOnePRGracefully(PartitionedRegion partitionedRegion) {
    boolean acquiredLock = false;
    try {
        partitionedRegion.acquireDestroyLock();
        acquiredLock = true;
        synchronized (partitionedRegion.getRedundancyProvider()) {
            if (partitionedRegion.isDataStore() && partitionedRegion.getDataStore() != null && partitionedRegion.getDataPolicy() == DataPolicy.PERSISTENT_PARTITION) {
                int numBuckets = partitionedRegion.getTotalNumberOfBuckets();
                Map<InternalDistributedMember, PersistentMemberID>[] bucketMaps = new Map[numBuckets];
                PartitionedRegionDataStore dataStore = partitionedRegion.getDataStore();
                // lock all the primary buckets
                Set<Entry<Integer, BucketRegion>> bucketEntries = dataStore.getAllLocalBuckets();
                for (Entry e : bucketEntries) {
                    BucketRegion bucket = (BucketRegion) e.getValue();
                    if (bucket == null || bucket.isDestroyed) {
                        // bucket region could be destroyed in race condition
                        continue;
                    }
                    bucket.getBucketAdvisor().tryLockIfPrimary();
                    // get map <InternalDistributedMember, persistentID> for this bucket's
                    // remote members
                    bucketMaps[bucket.getId()] = bucket.getBucketAdvisor().adviseInitializedPersistentMembers();
                    if (logger.isDebugEnabled()) {
                        logger.debug("shutDownAll: PR {}: initialized persistent members for {}:{}", partitionedRegion.getName(), bucket.getId(), bucketMaps[bucket.getId()]);
                    }
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("shutDownAll: All buckets for PR {} are locked.", partitionedRegion.getName());
                }
                // send lock profile update to other members
                partitionedRegion.setShutDownAllStatus(PartitionedRegion.PRIMARY_BUCKETS_LOCKED);
                new UpdateAttributesProcessor(partitionedRegion).distribute(false);
                partitionedRegion.getRegionAdvisor().waitForProfileStatus(PartitionedRegion.PRIMARY_BUCKETS_LOCKED);
                if (logger.isDebugEnabled()) {
                    logger.debug("shutDownAll: PR {}: all bucketLock profiles received.", partitionedRegion.getName());
                }
                // if async write, do flush
                if (!partitionedRegion.getAttributes().isDiskSynchronous()) {
                    // several PRs might share the same diskStore, we will only flush once
                    // even flush is called several times.
                    partitionedRegion.getDiskStore().forceFlush();
                    // send flush profile update to other members
                    partitionedRegion.setShutDownAllStatus(PartitionedRegion.DISK_STORE_FLUSHED);
                    new UpdateAttributesProcessor(partitionedRegion).distribute(false);
                    partitionedRegion.getRegionAdvisor().waitForProfileStatus(PartitionedRegion.DISK_STORE_FLUSHED);
                    if (logger.isDebugEnabled()) {
                        logger.debug("shutDownAll: PR {}: all flush profiles received.", partitionedRegion.getName());
                    }
                }
                // async write
                // persist other members to OFFLINE_EQUAL for each bucket region
                // iterate through all the bucketMaps and exclude the items whose
                // idm is no longer online
                Set<InternalDistributedMember> membersToPersistOfflineEqual = partitionedRegion.getRegionAdvisor().adviseDataStore();
                for (Entry e : bucketEntries) {
                    BucketRegion bucket = (BucketRegion) e.getValue();
                    if (bucket == null || bucket.isDestroyed) {
                        // bucket region could be destroyed in race condition
                        continue;
                    }
                    Map<InternalDistributedMember, PersistentMemberID> persistMap = getSubMapForLiveMembers(membersToPersistOfflineEqual, bucketMaps[bucket.getId()]);
                    if (persistMap != null) {
                        bucket.getPersistenceAdvisor().persistMembersOfflineAndEqual(persistMap);
                        if (logger.isDebugEnabled()) {
                            logger.debug("shutDownAll: PR {}: persisting bucket {}:{}", partitionedRegion.getName(), bucket.getId(), persistMap);
                        }
                    }
                }
                // send persisted profile update to other members, let all members to persist
                // before close the region
                partitionedRegion.setShutDownAllStatus(PartitionedRegion.OFFLINE_EQUAL_PERSISTED);
                new UpdateAttributesProcessor(partitionedRegion).distribute(false);
                partitionedRegion.getRegionAdvisor().waitForProfileStatus(PartitionedRegion.OFFLINE_EQUAL_PERSISTED);
                if (logger.isDebugEnabled()) {
                    logger.debug("shutDownAll: PR {}: all offline_equal profiles received.", partitionedRegion.getName());
                }
            }
            // dataStore
            // after done all steps for buckets, close partitionedRegion
            // close accessor directly
            RegionEventImpl event = new RegionEventImpl(partitionedRegion, Operation.REGION_CLOSE, null, false, getMyId(), true);
            try {
                // not to acquire lock
                partitionedRegion.basicDestroyRegion(event, false, false, true);
            } catch (CacheWriterException e) {
                // not possible with local operation, CacheWriter not called
                throw new Error(LocalizedStrings.LocalRegion_CACHEWRITEREXCEPTION_SHOULD_NOT_BE_THROWN_IN_LOCALDESTROYREGION.toLocalizedString(), e);
            } catch (TimeoutException e) {
                // not possible with local operation, no distributed locks possible
                throw new Error(LocalizedStrings.LocalRegion_TIMEOUTEXCEPTION_SHOULD_NOT_BE_THROWN_IN_LOCALDESTROYREGION.toLocalizedString(), e);
            }
        }
    // synchronized
    } catch (CacheClosedException cce) {
        logger.debug("Encounter CacheClosedException when shutDownAll is closing PR: {}:{}", partitionedRegion.getFullPath(), cce.getMessage());
    } catch (CancelException ce) {
        logger.debug("Encounter CancelException when shutDownAll is closing PR: {}:{}", partitionedRegion.getFullPath(), ce.getMessage());
    } catch (RegionDestroyedException rde) {
        logger.debug("Encounter CacheDestroyedException when shutDownAll is closing PR: {}:{}", partitionedRegion.getFullPath(), rde.getMessage());
    } finally {
        if (acquiredLock) {
            partitionedRegion.releaseDestroyLock();
        }
    }
}
Also used : RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) InternalGemFireError(org.apache.geode.InternalGemFireError) CacheClosedException(org.apache.geode.cache.CacheClosedException) SimpleExtensionPoint(org.apache.geode.internal.cache.extension.SimpleExtensionPoint) ExtensionPoint(org.apache.geode.internal.cache.extension.ExtensionPoint) PersistentMemberID(org.apache.geode.internal.cache.persistence.PersistentMemberID) Entry(java.util.Map.Entry) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) CancelException(org.apache.geode.CancelException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) CacheWriterException(org.apache.geode.cache.CacheWriterException) TimeoutException(org.apache.geode.cache.TimeoutException)

Example 17 with CacheClosedException

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

the class SingleHopClientExecutor method submitAllHA.

static boolean submitAllHA(List callableTasks, LocalRegion region, boolean isHA, ResultCollector rc, Set<String> failedNodes) {
    ClientMetadataService cms = region.getCache().getClientMetadataService();
    boolean reexecute = false;
    if (callableTasks != null && !callableTasks.isEmpty()) {
        List futures = null;
        try {
            futures = execService.invokeAll(callableTasks);
        } catch (RejectedExecutionException rejectedExecutionEx) {
            throw rejectedExecutionEx;
        } catch (InterruptedException e) {
            throw new InternalGemFireException(e.getMessage());
        }
        if (futures != null) {
            GemFireException functionExecutionException = null;
            Iterator futureItr = futures.iterator();
            Iterator taskItr = callableTasks.iterator();
            final boolean isDebugEnabled = logger.isDebugEnabled();
            while (futureItr.hasNext() && !execService.isShutdown() && !execService.isTerminated()) {
                Future fut = (Future) futureItr.next();
                SingleHopOperationCallable task = (SingleHopOperationCallable) taskItr.next();
                ServerLocation server = task.getServer();
                try {
                    fut.get();
                    if (isDebugEnabled) {
                        logger.debug("ExecuteRegionFunctionSingleHopOp#got result from {}", server);
                    }
                } catch (InterruptedException e) {
                    throw new InternalGemFireException(e.getMessage());
                } catch (ExecutionException ee) {
                    if (ee.getCause() instanceof InternalFunctionInvocationTargetException) {
                        if (isDebugEnabled) {
                            logger.debug("ExecuteRegionFunctionSingleHopOp#ExecutionException.InternalFunctionInvocationTargetException : Caused by :{}", ee.getCause());
                        }
                        try {
                            cms = region.getCache().getClientMetadataService();
                        } catch (CacheClosedException e) {
                            return false;
                        }
                        cms.removeBucketServerLocation(server);
                        cms.scheduleGetPRMetaData(region, false);
                        reexecute = true;
                        failedNodes.addAll(((InternalFunctionInvocationTargetException) ee.getCause()).getFailedNodeSet());
                        // Clear the results only if isHA so that partial results can be returned.
                        if (isHA) {
                            rc.clearResults();
                        } else {
                            if (ee.getCause().getCause() != null) {
                                functionExecutionException = new FunctionInvocationTargetException(ee.getCause().getCause());
                            } else {
                                functionExecutionException = new FunctionInvocationTargetException(new BucketMovedException(LocalizedStrings.FunctionService_BUCKET_MIGRATED_TO_ANOTHER_NODE.toLocalizedString()));
                            }
                        }
                    } else if (ee.getCause() instanceof FunctionException) {
                        if (isDebugEnabled) {
                            logger.debug("ExecuteRegionFunctionSingleHopOp#ExecutionException.FunctionException : Caused by :{}", ee.getCause());
                        }
                        FunctionException fe = (FunctionException) ee.getCause();
                        if (isHA) {
                            throw fe;
                        } else {
                            functionExecutionException = fe;
                        }
                    } else if (ee.getCause() instanceof ServerOperationException) {
                        if (isDebugEnabled) {
                            logger.debug("ExecuteRegionFunctionSingleHopOp#ExecutionException.ServerOperationException : Caused by :{}", ee.getCause());
                        }
                        ServerOperationException soe = (ServerOperationException) ee.getCause();
                        if (isHA) {
                            throw soe;
                        } else {
                            functionExecutionException = soe;
                        }
                    } else if (ee.getCause() instanceof ServerConnectivityException) {
                        if (isDebugEnabled) {
                            logger.debug("ExecuteRegionFunctionSingleHopOp#ExecutionException.ServerConnectivityException : Caused by :{} The failed server is: {}", ee.getCause(), server);
                        }
                        try {
                            cms = region.getCache().getClientMetadataService();
                        } catch (CacheClosedException e) {
                            return false;
                        }
                        cms.removeBucketServerLocation(server);
                        cms.scheduleGetPRMetaData(region, false);
                        // Clear the results only if isHA so that partial results can be returned.
                        if (isHA) {
                            reexecute = true;
                            rc.clearResults();
                        } else {
                            functionExecutionException = (ServerConnectivityException) ee.getCause();
                        }
                    } else {
                        throw executionThrowable(ee.getCause());
                    }
                }
            }
            if (functionExecutionException != null) {
                throw functionExecutionException;
            }
        }
    }
    return reexecute;
}
Also used : InternalGemFireException(org.apache.geode.InternalGemFireException) ServerLocation(org.apache.geode.distributed.internal.ServerLocation) FunctionException(org.apache.geode.cache.execute.FunctionException) BucketMovedException(org.apache.geode.internal.cache.execute.BucketMovedException) CacheClosedException(org.apache.geode.cache.CacheClosedException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ServerConnectivityException(org.apache.geode.cache.client.ServerConnectivityException) InternalGemFireException(org.apache.geode.InternalGemFireException) GemFireException(org.apache.geode.GemFireException) InternalFunctionInvocationTargetException(org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException) Iterator(java.util.Iterator) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) InternalFunctionInvocationTargetException(org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException) Future(java.util.concurrent.Future) VersionedObjectList(org.apache.geode.internal.cache.tier.sockets.VersionedObjectList) List(java.util.List) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException)

Example 18 with CacheClosedException

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

the class CompiledSelect method getRegionIteratorSize.

/**
   * Returns the size of region iterator for count(*) on a region without whereclause.
   * 
   * @since GemFire 6.6.2
   */
private int getRegionIteratorSize(ExecutionContext context, CompiledValue collExpr) throws RegionNotFoundException {
    Region region;
    String regionPath = ((CompiledRegion) collExpr).getRegionPath();
    if (context.getBucketRegion() == null) {
        region = context.getCache().getRegion(regionPath);
    } else {
        region = context.getBucketRegion();
    }
    if (region != null) {
        return region.size();
    } else {
        // if we couldn't find the region because the cache is closed, throw
        // a CacheClosedException
        Cache cache = context.getCache();
        if (cache.isClosed()) {
            throw new CacheClosedException();
        }
        throw new RegionNotFoundException(LocalizedStrings.CompiledRegion_REGION_NOT_FOUND_0.toLocalizedString(regionPath));
    }
}
Also used : RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) Region(org.apache.geode.cache.Region) PdxString(org.apache.geode.pdx.internal.PdxString) CacheClosedException(org.apache.geode.cache.CacheClosedException) InternalCache(org.apache.geode.internal.cache.InternalCache) Cache(org.apache.geode.cache.Cache)

Example 19 with CacheClosedException

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

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

the class DataCommands method importData.

@CliCommand(value = CliStrings.IMPORT_DATA, help = CliStrings.IMPORT_DATA__HELP)
@CliMetaData(relatedTopic = { CliStrings.TOPIC_GEODE_DATA, CliStrings.TOPIC_GEODE_REGION })
public Result importData(@CliOption(key = CliStrings.IMPORT_DATA__REGION, optionContext = ConverterHint.REGION_PATH, mandatory = true, help = CliStrings.IMPORT_DATA__REGION__HELP) String regionName, @CliOption(key = CliStrings.IMPORT_DATA__FILE, mandatory = true, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.IMPORT_DATA__FILE__HELP) String filePath, @CliOption(key = CliStrings.IMPORT_DATA__MEMBER, mandatory = true, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, optionContext = ConverterHint.MEMBERIDNAME, help = CliStrings.IMPORT_DATA__MEMBER__HELP) String memberNameOrId, @CliOption(key = CliStrings.IMPORT_DATA__INVOKE_CALLBACKS, unspecifiedDefaultValue = "false", help = CliStrings.IMPORT_DATA__INVOKE_CALLBACKS__HELP) boolean invokeCallbacks) {
    this.securityService.authorizeRegionWrite(regionName);
    Result result;
    try {
        final DistributedMember targetMember = CliUtil.getDistributedMemberByNameOrId(memberNameOrId);
        if (!filePath.endsWith(CliStrings.GEODE_DATA_FILE_EXTENSION)) {
            return ResultBuilder.createUserErrorResult(CliStrings.format(CliStrings.INVALID_FILE_EXTENSION, CliStrings.GEODE_DATA_FILE_EXTENSION));
        }
        if (targetMember != null) {
            final Object[] args = { regionName, filePath, invokeCallbacks };
            ResultCollector<?, ?> rc = CliUtil.executeFunction(importDataFunction, args, targetMember);
            List<Object> results = (List<Object>) rc.getResult();
            if (results != null) {
                Object resultObj = results.get(0);
                if (resultObj instanceof String) {
                    result = ResultBuilder.createInfoResult((String) resultObj);
                } else if (resultObj instanceof Exception) {
                    result = ResultBuilder.createGemFireErrorResult(((Exception) resultObj).getMessage());
                } else {
                    result = ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.COMMAND_FAILURE_MESSAGE, CliStrings.IMPORT_DATA));
                }
            } else {
                result = ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.COMMAND_FAILURE_MESSAGE, CliStrings.IMPORT_DATA));
            }
        } else {
            result = ResultBuilder.createUserErrorResult(CliStrings.format(CliStrings.IMPORT_DATA__MEMBER__NOT__FOUND, memberNameOrId));
        }
    } catch (CacheClosedException e) {
        result = ResultBuilder.createGemFireErrorResult(e.getMessage());
    } catch (FunctionInvocationTargetException e) {
        result = ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.COMMAND_FAILURE_MESSAGE, CliStrings.IMPORT_DATA));
    }
    return result;
}
Also used : DistributedMember(org.apache.geode.distributed.DistributedMember) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) List(java.util.List) ArrayList(java.util.ArrayList) CacheClosedException(org.apache.geode.cache.CacheClosedException) TimeoutException(java.util.concurrent.TimeoutException) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) CacheClosedException(org.apache.geode.cache.CacheClosedException) Result(org.apache.geode.management.cli.Result) DataCommandResult(org.apache.geode.management.internal.cli.domain.DataCommandResult) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Aggregations

CacheClosedException (org.apache.geode.cache.CacheClosedException)95 Cache (org.apache.geode.cache.Cache)26 Test (org.junit.Test)21 IOException (java.io.IOException)20 ArrayList (java.util.ArrayList)20 FunctionException (org.apache.geode.cache.execute.FunctionException)20 FunctionInvocationTargetException (org.apache.geode.cache.execute.FunctionInvocationTargetException)20 CancelException (org.apache.geode.CancelException)18 Region (org.apache.geode.cache.Region)18 Host (org.apache.geode.test.dunit.Host)17 VM (org.apache.geode.test.dunit.VM)17 InternalCache (org.apache.geode.internal.cache.InternalCache)16 IgnoredException (org.apache.geode.test.dunit.IgnoredException)16 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)16 DistributedMember (org.apache.geode.distributed.DistributedMember)14 ReplyException (org.apache.geode.distributed.internal.ReplyException)14 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)12 Execution (org.apache.geode.cache.execute.Execution)11 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)11 HashMap (java.util.HashMap)10