Search in sources :

Example 16 with RegionExistsException

use of org.apache.geode.cache.RegionExistsException 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)

Example 17 with RegionExistsException

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

the class LocalManager method startLocalManagement.

/**
   * Managed Node side resources are created
   * 
   * Management Region : its a Replicated NO_ACK region Notification Region : its a Replicated Proxy
   * NO_ACK region
   */
private void startLocalManagement(Map<ObjectName, FederationComponent> federatedComponentMap) {
    synchronized (this) {
        if (repo.getLocalMonitoringRegion() != null) {
            return;
        } else {
            ThreadFactory tf = new ThreadFactory() {

                public Thread newThread(final Runnable command) {
                    final Runnable r = new Runnable() {

                        public void run() {
                            command.run();
                        }
                    };
                    final ThreadGroup group = LoggingThreadGroup.createThreadGroup(ManagementStrings.MANAGEMENT_TASK_THREAD_GROUP.toLocalizedString(), logger);
                    Thread thread = new Thread(group, r, ManagementStrings.MANAGEMENT_TASK.toLocalizedString());
                    thread.setDaemon(true);
                    return thread;
                }
            };
            singleThreadFederationScheduler = Executors.newSingleThreadScheduledExecutor(tf);
            if (logger.isDebugEnabled()) {
                logger.debug("Creating  Management Region :");
            }
            /*
         * Sharing the same Internal Argument for both notification region and monitoring region
         */
            InternalRegionArguments internalArgs = new InternalRegionArguments();
            internalArgs.setIsUsedForMetaRegion(true);
            // Create anonymous stats holder for Management Regions
            final HasCachePerfStats monitoringRegionStats = new HasCachePerfStats() {

                public CachePerfStats getCachePerfStats() {
                    return new CachePerfStats(cache.getDistributedSystem(), "managementRegionStats");
                }
            };
            internalArgs.setCachePerfStatsHolder(monitoringRegionStats);
            AttributesFactory<String, Object> monitorRegionAttributeFactory = new AttributesFactory<String, Object>();
            monitorRegionAttributeFactory.setScope(Scope.DISTRIBUTED_NO_ACK);
            monitorRegionAttributeFactory.setDataPolicy(DataPolicy.REPLICATE);
            monitorRegionAttributeFactory.setConcurrencyChecksEnabled(false);
            MonitoringRegionCacheListener localListener = new MonitoringRegionCacheListener(service);
            monitorRegionAttributeFactory.addCacheListener(localListener);
            RegionAttributes<String, Object> monitoringRegionAttrs = monitorRegionAttributeFactory.create();
            AttributesFactory<NotificationKey, Notification> notificationRegionAttributeFactory = new AttributesFactory<NotificationKey, Notification>();
            notificationRegionAttributeFactory.setScope(Scope.DISTRIBUTED_NO_ACK);
            notificationRegionAttributeFactory.setDataPolicy(DataPolicy.EMPTY);
            notificationRegionAttributeFactory.setConcurrencyChecksEnabled(false);
            RegionAttributes<NotificationKey, Notification> notifRegionAttrs = notificationRegionAttributeFactory.create();
            String appender = MBeanJMXAdapter.getUniqueIDForMember(cache.getDistributedSystem().getDistributedMember());
            boolean monitoringRegionCreated = false;
            boolean notifRegionCreated = false;
            try {
                repo.setLocalMonitoringRegion(cache.createVMRegion(ManagementConstants.MONITORING_REGION + "_" + appender, monitoringRegionAttrs, internalArgs));
                monitoringRegionCreated = true;
            } catch (TimeoutException e) {
                throw new ManagementException(e);
            } catch (RegionExistsException e) {
                throw new ManagementException(e);
            } catch (IOException e) {
                throw new ManagementException(e);
            } catch (ClassNotFoundException e) {
                throw new ManagementException(e);
            }
            try {
                repo.setLocalNotificationRegion(cache.createVMRegion(ManagementConstants.NOTIFICATION_REGION + "_" + appender, notifRegionAttrs, internalArgs));
                notifRegionCreated = true;
            } catch (TimeoutException e) {
                throw new ManagementException(e);
            } catch (RegionExistsException e) {
                throw new ManagementException(e);
            } catch (IOException e) {
                throw new ManagementException(e);
            } catch (ClassNotFoundException e) {
                throw new ManagementException(e);
            } finally {
                if (!notifRegionCreated && monitoringRegionCreated) {
                    repo.getLocalMonitoringRegion().localDestroyRegion();
                }
            }
            managementTask = new ManagementTask(federatedComponentMap);
            // call run to get us initialized immediately with a sync call
            managementTask.run();
            // All local resources are created for the ManagementTask
            // Now Management tasks can proceed.
            int updateRate = cache.getInternalDistributedSystem().getConfig().getJmxManagerUpdateRate();
            singleThreadFederationScheduler.scheduleAtFixedRate(managementTask, updateRate, updateRate, TimeUnit.MILLISECONDS);
            if (logger.isDebugEnabled()) {
                logger.debug("Management Region created with Name : {}", repo.getLocalMonitoringRegion().getName());
                logger.debug("Notification Region created with Name : {}", repo.getLocalNotificationRegion().getName());
            }
        }
    }
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) InternalRegionArguments(org.apache.geode.internal.cache.InternalRegionArguments) RegionExistsException(org.apache.geode.cache.RegionExistsException) Notification(javax.management.Notification) ManagementException(org.apache.geode.management.ManagementException) AttributesFactory(org.apache.geode.cache.AttributesFactory) HasCachePerfStats(org.apache.geode.internal.cache.HasCachePerfStats) LoggingThreadGroup(org.apache.geode.internal.logging.LoggingThreadGroup) TimeoutException(org.apache.geode.cache.TimeoutException) CachePerfStats(org.apache.geode.internal.cache.CachePerfStats) HasCachePerfStats(org.apache.geode.internal.cache.HasCachePerfStats) IOException(java.io.IOException)

Example 18 with RegionExistsException

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

the class PeerTypeRegistration method initialize.

public void initialize() {
    AttributesFactory<Object, Object> factory = new AttributesFactory<Object, Object>();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    if (cache.getPdxPersistent()) {
        if (cache.getCacheConfig().pdxDiskStoreUserSet) {
            factory.setDiskStoreName(cache.getPdxDiskStore());
        } else {
            factory.setDiskStoreName(cache.getOrCreateDefaultDiskStore().getName());
        }
        factory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
    } else {
        factory.setDataPolicy(DataPolicy.REPLICATE);
    }
    // Add a listener that makes sure that if anyone in the DS is using PDX
    // Our PDX configuration is valid for this member. This is important if
    // we are the gateway, we need to validate that we have a distributed system
    // id.
    factory.addCacheListener(new CacheListenerAdapter<Object, Object>() {

        @Override
        public void afterCreate(EntryEvent<Object, Object> event) {
            verifyConfiguration();
            // update a local map with the pdxtypes registered
            Object value = event.getNewValue();
            if (value instanceof PdxType) {
                updateClassToTypeMap((PdxType) value);
            }
        }
    });
    factory.setCacheWriter(new CacheWriterAdapter<Object, Object>() {

        @Override
        public void beforeCreate(EntryEvent<Object, Object> event) throws CacheWriterException {
            Object newValue = event.getNewValue();
            if (newValue instanceof PdxType) {
                logger.info("Adding new type: {}", ((PdxType) event.getNewValue()).toFormattedString());
            } else {
                logger.info("Adding new type: {} {}", event.getKey(), ((EnumInfo) newValue).toFormattedString());
            }
        }

        @Override
        public void beforeUpdate(EntryEvent<Object, Object> event) throws CacheWriterException {
            if (!event.getRegion().get(event.getKey()).equals(event.getNewValue())) {
                PdxRegistryMismatchException ex = new PdxRegistryMismatchException("Trying to add a PDXType with the same id as an existing PDX type. id=" + event.getKey() + ", existing pdx type " + event.getOldValue() + ", new type " + event.getNewValue());
                throw new CacheWriterException(ex);
            }
        }
    });
    RegionAttributes<Object, Object> regionAttrs = factory.create();
    InternalRegionArguments internalArgs = new InternalRegionArguments();
    internalArgs.setIsUsedForMetaRegion(true);
    internalArgs.setMetaRegionWithTransactions(true);
    try {
        this.idToType = cache.createVMRegion(REGION_NAME, regionAttrs, internalArgs);
    } catch (IOException ex) {
        throw new PdxInitializationException("Could not create pdx registry", ex);
    } catch (TimeoutException ex) {
        throw new PdxInitializationException("Could not create pdx registry", ex);
    } catch (RegionExistsException ex) {
        throw new PdxInitializationException("Could not create pdx registry", ex);
    } catch (ClassNotFoundException ex) {
        throw new PdxInitializationException("Could not create pdx registry", ex);
    }
    // And send those types to any existing gateways.
    if (!getIdToType().isEmpty()) {
        verifyConfiguration();
    }
}
Also used : InternalRegionArguments(org.apache.geode.internal.cache.InternalRegionArguments) RegionExistsException(org.apache.geode.cache.RegionExistsException) IOException(java.io.IOException) PdxInitializationException(org.apache.geode.pdx.PdxInitializationException) AttributesFactory(org.apache.geode.cache.AttributesFactory) PdxRegistryMismatchException(org.apache.geode.pdx.PdxRegistryMismatchException) CacheWriterException(org.apache.geode.cache.CacheWriterException) TimeoutException(org.apache.geode.cache.TimeoutException)

Aggregations

RegionExistsException (org.apache.geode.cache.RegionExistsException)18 AttributesFactory (org.apache.geode.cache.AttributesFactory)9 Region (org.apache.geode.cache.Region)7 RegionAttributes (org.apache.geode.cache.RegionAttributes)6 Cache (org.apache.geode.cache.Cache)5 IOException (java.io.IOException)4 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)4 CancelException (org.apache.geode.CancelException)3 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)3 InternalRegionArguments (org.apache.geode.internal.cache.InternalRegionArguments)3 LocalRegion (org.apache.geode.internal.cache.LocalRegion)3 Test (org.junit.Test)3 InputStream (java.io.InputStream)2 InternalGemFireError (org.apache.geode.InternalGemFireError)2 CacheWriterException (org.apache.geode.cache.CacheWriterException)2 CacheXmlException (org.apache.geode.cache.CacheXmlException)2 TimeoutException (org.apache.geode.cache.TimeoutException)2 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)2 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)2 CachePerfStats (org.apache.geode.internal.cache.CachePerfStats)2