Search in sources :

Example 1 with RedundancyAlreadyMetException

use of org.apache.geode.internal.cache.partitioned.RedundancyAlreadyMetException in project geode by apache.

the class GemFireCacheImpl method createVMRegion.

// TODO: createVMRegion method is too complex for IDE to analyze
@Override
public <K, V> Region<K, V> createVMRegion(String name, RegionAttributes<K, V> p_attrs, InternalRegionArguments internalRegionArgs) throws RegionExistsException, TimeoutException, IOException, ClassNotFoundException {
    if (getMyId().getVmKind() == DistributionManager.LOCATOR_DM_TYPE) {
        if (!internalRegionArgs.isUsedForMetaRegion() && internalRegionArgs.getInternalMetaRegion() == null) {
            throw new IllegalStateException("Regions can not be created in a locator.");
        }
    }
    this.stopper.checkCancelInProgress(null);
    LocalRegion.validateRegionName(name, internalRegionArgs);
    RegionAttributes<K, V> attrs = p_attrs;
    attrs = invokeRegionBefore(null, name, attrs, internalRegionArgs);
    if (attrs == null) {
        throw new IllegalArgumentException(LocalizedStrings.GemFireCache_ATTRIBUTES_MUST_NOT_BE_NULL.toLocalizedString());
    }
    LocalRegion region;
    final InputStream snapshotInputStream = internalRegionArgs.getSnapshotInputStream();
    InternalDistributedMember imageTarget = internalRegionArgs.getImageTarget();
    final boolean recreate = internalRegionArgs.getRecreateFlag();
    final boolean isPartitionedRegion = attrs.getPartitionAttributes() != null;
    final boolean isReInitCreate = snapshotInputStream != null || imageTarget != null || recreate;
    try {
        for (; ; ) {
            getCancelCriterion().checkCancelInProgress(null);
            Future future = null;
            synchronized (this.rootRegions) {
                region = this.rootRegions.get(name);
                if (region != null) {
                    throw new RegionExistsException(region);
                }
                if (!isReInitCreate) {
                    // fix bug 33523
                    String fullPath = Region.SEPARATOR + name;
                    future = (Future) this.reinitializingRegions.get(fullPath);
                }
                if (future == null) {
                    if (internalRegionArgs.getInternalMetaRegion() != null) {
                        region = internalRegionArgs.getInternalMetaRegion();
                    } else if (isPartitionedRegion) {
                        region = new PartitionedRegion(name, attrs, null, this, internalRegionArgs);
                    } else {
                        if (attrs.getScope().isLocal()) {
                            region = new LocalRegion(name, attrs, null, this, internalRegionArgs);
                        } else {
                            region = new DistributedRegion(name, attrs, null, this, internalRegionArgs);
                        }
                    }
                    this.rootRegions.put(name, region);
                    if (isReInitCreate) {
                        regionReinitialized(region);
                    }
                    break;
                }
            }
            // synchronized
            boolean interrupted = Thread.interrupted();
            try {
                // future != null
                // wait on Future
                LocalRegion localRegion = (LocalRegion) future.get();
                throw new RegionExistsException(localRegion);
            } catch (InterruptedException ignore) {
                interrupted = true;
            } catch (ExecutionException e) {
                throw new Error(LocalizedStrings.GemFireCache_UNEXPECTED_EXCEPTION.toLocalizedString(), e);
            } catch (CancellationException e) {
                // future was cancelled
                if (logger.isTraceEnabled()) {
                    logger.trace("future cancelled", e);
                }
            } finally {
                if (interrupted) {
                    Thread.currentThread().interrupt();
                }
            }
        }
        // for
        boolean success = false;
        try {
            setRegionByPath(region.getFullPath(), region);
            region.initialize(snapshotInputStream, imageTarget, internalRegionArgs);
            success = true;
        } catch (CancelException | RedundancyAlreadyMetException e) {
            // don't print a call stack
            throw e;
        } catch (RuntimeException validationException) {
            logger.warn(LocalizedMessage.create(LocalizedStrings.GemFireCache_INITIALIZATION_FAILED_FOR_REGION_0, region.getFullPath()), validationException);
            throw validationException;
        } finally {
            if (!success) {
                try {
                    // do this before removing the region from
                    // the root set to fix bug 41982.
                    region.cleanupFailedInitialization();
                } catch (VirtualMachineError e) {
                    SystemFailure.initiateFailure(e);
                    throw e;
                } catch (Throwable t) {
                    SystemFailure.checkFailure();
                    this.stopper.checkCancelInProgress(t);
                    // bug #44672 - log the failure but don't override the original exception
                    logger.warn(LocalizedMessage.create(LocalizedStrings.GemFireCache_INIT_CLEANUP_FAILED_FOR_REGION_0, region.getFullPath()), t);
                } finally {
                    // clean up if initialize fails for any reason
                    setRegionByPath(region.getFullPath(), null);
                    synchronized (this.rootRegions) {
                        Region rootRegion = this.rootRegions.get(name);
                        if (rootRegion == region) {
                            this.rootRegions.remove(name);
                        }
                    }
                // synchronized
                }
            }
        // success
        }
        region.postCreateRegion();
    } catch (RegionExistsException ex) {
        // outside of sync make sure region is initialized to fix bug 37563
        LocalRegion localRegion = (LocalRegion) ex.getRegion();
        // don't give out ref until initialized
        localRegion.waitOnInitialization();
        throw ex;
    }
    invokeRegionAfter(region);
    // Added for M&M . Putting the callback here to avoid creating RegionMBean in case of Exception
    if (!region.isInternalRegion()) {
        this.system.handleResourceEvent(ResourceEvent.REGION_CREATE, region);
    }
    return region;
}
Also used : RegionExistsException(org.apache.geode.cache.RegionExistsException) ExecutionException(java.util.concurrent.ExecutionException) ByteArrayInputStream(java.io.ByteArrayInputStream) StringBufferInputStream(java.io.StringBufferInputStream) InputStream(java.io.InputStream) InternalGemFireError(org.apache.geode.InternalGemFireError) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) CancellationException(java.util.concurrent.CancellationException) RedundancyAlreadyMetException(org.apache.geode.internal.cache.partitioned.RedundancyAlreadyMetException) Future(java.util.concurrent.Future) Region(org.apache.geode.cache.Region) CancelException(org.apache.geode.CancelException)

Example 2 with RedundancyAlreadyMetException

use of org.apache.geode.internal.cache.partitioned.RedundancyAlreadyMetException in project geode by apache.

the class LocalRegion method createSubregion.

// TODO: createSubregion method is too complex for IDE to analyze
public Region createSubregion(String subregionName, RegionAttributes attrs, InternalRegionArguments internalRegionArgs) throws RegionExistsException, TimeoutException, IOException, ClassNotFoundException {
    checkReadiness();
    RegionAttributes regionAttributes = attrs;
    // TODO: attrs is reassigned but never used
    attrs = this.cache.invokeRegionBefore(this, subregionName, attrs, internalRegionArgs);
    final InputStream snapshotInputStream = internalRegionArgs.getSnapshotInputStream();
    final boolean getDestroyLock = internalRegionArgs.getDestroyLockFlag();
    final InternalDistributedMember imageTarget = internalRegionArgs.getImageTarget();
    LocalRegion newRegion = null;
    try {
        if (getDestroyLock)
            acquireDestroyLock();
        LocalRegion existing = null;
        try {
            if (isDestroyed()) {
                if (this.reinitialized_old) {
                    throw new RegionReinitializedException(toString(), getFullPath());
                }
                throw new RegionDestroyedException(toString(), getFullPath());
            }
            validateRegionName(subregionName, internalRegionArgs);
            validateSubregionAttributes(regionAttributes);
            // deadlock)
            synchronized (this.subregionsLock) {
                existing = (LocalRegion) this.subregions.get(subregionName);
                if (existing == null) {
                    if (regionAttributes.getScope().isDistributed() && internalRegionArgs.isUsedForPartitionedRegionBucket()) {
                        final PartitionedRegion pr = internalRegionArgs.getPartitionedRegion();
                        internalRegionArgs.setUserAttribute(pr.getUserAttribute());
                        if (pr.isShadowPR()) {
                            newRegion = new BucketRegionQueue(subregionName, regionAttributes, this, this.cache, internalRegionArgs);
                        } else {
                            newRegion = new BucketRegion(subregionName, regionAttributes, this, this.cache, internalRegionArgs);
                        }
                    } else if (regionAttributes.getPartitionAttributes() != null) {
                        newRegion = new PartitionedRegion(subregionName, regionAttributes, this, this.cache, internalRegionArgs);
                    } else {
                        boolean local = regionAttributes.getScope().isLocal();
                        newRegion = local ? new LocalRegion(subregionName, regionAttributes, this, this.cache, internalRegionArgs) : new DistributedRegion(subregionName, regionAttributes, this, this.cache, internalRegionArgs);
                    }
                    Object previousValue = this.subregions.putIfAbsent(subregionName, newRegion);
                    Assert.assertTrue(previousValue == null);
                    Assert.assertTrue(!newRegion.isInitialized());
                    if (logger.isDebugEnabled()) {
                        logger.debug("Subregion created: {}", newRegion.getFullPath());
                    }
                    if (snapshotInputStream != null || imageTarget != null || internalRegionArgs.getRecreateFlag()) {
                        // fix for bug 33534
                        this.cache.regionReinitialized(newRegion);
                    }
                }
            // endif: existing == null
            }
        // end synchronization
        } finally {
            if (getDestroyLock) {
                releaseDestroyLock();
            }
        }
        // Fix for bug 42127 - moved to outside of the destroy lock.
        if (existing != null) {
            // now outside of synchronization we must wait for appropriate
            // initialization on existing region before returning a reference to
            // it
            existing.waitOnInitialization();
            // fix for bug 32570
            throw new RegionExistsException(existing);
        }
        boolean success = false;
        try {
            newRegion.checkReadiness();
            this.cache.setRegionByPath(newRegion.getFullPath(), newRegion);
            if (regionAttributes instanceof UserSpecifiedRegionAttributes) {
                internalRegionArgs.setIndexes(((UserSpecifiedRegionAttributes) regionAttributes).getIndexes());
            }
            // releases initialization Latches
            newRegion.initialize(snapshotInputStream, imageTarget, internalRegionArgs);
            // register the region with resource manager to get memory events
            if (!newRegion.isInternalRegion()) {
                if (!newRegion.isDestroyed) {
                    this.cache.getInternalResourceManager().addResourceListener(ResourceType.MEMORY, newRegion);
                    if (!newRegion.getOffHeap()) {
                        newRegion.initialCriticalMembers(this.cache.getInternalResourceManager().getHeapMonitor().getState().isCritical(), this.cache.getResourceAdvisor().adviseCritialMembers());
                    } else {
                        newRegion.initialCriticalMembers(this.cache.getInternalResourceManager().getHeapMonitor().getState().isCritical() || this.cache.getInternalResourceManager().getOffHeapMonitor().getState().isCritical(), this.cache.getResourceAdvisor().adviseCritialMembers());
                    }
                    // synchronization would be done on ManagementAdapter.regionOpLock
                    // instead of destroyLock in LocalRegion? ManagementAdapter is one
                    // of the Resource Event listeners
                    InternalDistributedSystem system = this.cache.getInternalDistributedSystem();
                    system.handleResourceEvent(ResourceEvent.REGION_CREATE, newRegion);
                }
            }
            success = true;
        } catch (CancelException | RegionDestroyedException | RedundancyAlreadyMetException e) {
            // don't print a call stack
            throw e;
        } catch (RuntimeException validationException) {
            logger.warn(LocalizedMessage.create(LocalizedStrings.LocalRegion_INITIALIZATION_FAILED_FOR_REGION_0, getFullPath()), validationException);
            throw validationException;
        } finally {
            if (!success) {
                this.cache.setRegionByPath(newRegion.getFullPath(), null);
                initializationFailed(newRegion);
                this.cache.getInternalResourceManager(false).removeResourceListener(newRegion);
            }
        }
        newRegion.postCreateRegion();
    } finally {
        // have occurred
        if (newRegion != null && !newRegion.isInitialized()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Region initialize latch is closed, Error must have occurred");
            }
        }
    }
    this.cache.invokeRegionAfter(newRegion);
    return newRegion;
}
Also used : RegionAttributes(org.apache.geode.cache.RegionAttributes) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) RegionExistsException(org.apache.geode.cache.RegionExistsException) CacheRuntimeException(org.apache.geode.cache.CacheRuntimeException) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) RedundancyAlreadyMetException(org.apache.geode.internal.cache.partitioned.RedundancyAlreadyMetException) StoredObject(org.apache.geode.internal.offheap.StoredObject) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) CancelException(org.apache.geode.CancelException) RegionReinitializedException(org.apache.geode.cache.RegionReinitializedException)

Aggregations

InputStream (java.io.InputStream)2 CancelException (org.apache.geode.CancelException)2 RegionExistsException (org.apache.geode.cache.RegionExistsException)2 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)2 RedundancyAlreadyMetException (org.apache.geode.internal.cache.partitioned.RedundancyAlreadyMetException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 StringBufferInputStream (java.io.StringBufferInputStream)1 CancellationException (java.util.concurrent.CancellationException)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 InternalGemFireError (org.apache.geode.InternalGemFireError)1 CacheRuntimeException (org.apache.geode.cache.CacheRuntimeException)1 Region (org.apache.geode.cache.Region)1 RegionAttributes (org.apache.geode.cache.RegionAttributes)1 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)1 RegionReinitializedException (org.apache.geode.cache.RegionReinitializedException)1 InternalDistributedSystem (org.apache.geode.distributed.internal.InternalDistributedSystem)1 StoredObject (org.apache.geode.internal.offheap.StoredObject)1