Search in sources :

Example 1 with RegionReinitializedException

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

the class RegionTestCase method remoteTestPostSnapshot.

public void remoteTestPostSnapshot(String name, boolean isController, boolean isRoot) throws CacheException {
    assertTrue(preSnapshotRegion.isDestroyed());
    try {
        preSnapshotRegion.get("0");
        fail("Should have thrown a RegionReinitializedException");
    } catch (RegionReinitializedException e) {
    // pass
    }
    LogWriter log = getCache().getLogger();
    // get new reference to region
    Region postSnapshotRegion = isRoot ? getRootRegion(name) : getRootRegion().getSubregion(name);
    assertNotNull("Could not get reference to reinitialized region", postSnapshotRegion);
    boolean expectData = isController || postSnapshotRegion.getAttributes().getMirrorType().isMirrored() || postSnapshotRegion.getAttributes().getDataPolicy().isPreloaded();
    log.info("region has " + postSnapshotRegion.keySet().size() + " entries");
    assertEquals(expectData ? MAX_KEYS : 0, postSnapshotRegion.keySet().size());
    // gets the data either locally or by netSearch
    assertEquals(new Integer(3), postSnapshotRegion.get("3"));
    // bug 33311 coverage
    if (expectData) {
        assertFalse(postSnapshotRegion.containsValueForKey("9"));
        assertTrue(postSnapshotRegion.containsKey("9"));
    }
}
Also used : LogWriter(org.apache.geode.LogWriter) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) RegionReinitializedException(org.apache.geode.cache.RegionReinitializedException)

Example 2 with RegionReinitializedException

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

the class RegionReliabilityTestCase method testReinitialization.

@Test
public void testReinitialization() throws Exception {
    final String name = this.getUniqueName();
    final String roleA = name + "-A";
    final String[] requiredRoles = { roleA };
    Set requiredRolesSet = new HashSet();
    for (int i = 0; i < requiredRoles.length; i++) {
        requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
    }
    assertEquals(requiredRoles.length, requiredRolesSet.size());
    // connect controller to system...
    Properties config = new Properties();
    config.setProperty(ROLES, "");
    getSystem(config);
    getCache();
    // create region in controller...
    MembershipAttributes ra = new MembershipAttributes(requiredRoles, LossAction.NO_ACCESS, ResumptionAction.REINITIALIZE);
    AttributesFactory fac = new AttributesFactory();
    fac.setMembershipAttributes(ra);
    fac.setScope(getRegionScope());
    fac.setDataPolicy(DataPolicy.REPLICATE);
    RegionAttributes attr = fac.create();
    Region region = createRootRegion(name, attr);
    assertTrue(((AbstractRegion) region).requiresReliabilityCheck());
    assertFalse(RequiredRoles.checkForRequiredRoles(region).isEmpty());
    final String key = "KEY-testReinitialization";
    final String val = "VALUE-testReinitialization";
    Host.getHost(0).getVM(0).invoke(new CacheSerializableRunnable("Create Data") {

        public void run2() throws CacheException {
            createConnection(new String[] {});
            AttributesFactory fac = new AttributesFactory();
            fac.setScope(getRegionScope());
            fac.setDataPolicy(DataPolicy.REPLICATE);
            RegionAttributes attr = fac.create();
            Region region = createRootRegion(name, attr);
            region.put(key, val);
        }
    });
    final Region finalRegion = region;
    Thread thread = new Thread(new Runnable() {

        public void run() {
            try {
                RequiredRoles.waitForRequiredRoles(finalRegion, -1);
            } catch (InterruptedException e) {
                fail("interrupted");
            } catch (RegionReinitializedException e) {
            }
        }
    });
    thread.start();
    // create role and verify reinitialization took place
    Host.getHost(0).getVM(1).invokeAsync(new CacheSerializableRunnable("Create Role") {

        public void run2() throws CacheException {
            createConnection(new String[] { roleA });
            AttributesFactory fac = new AttributesFactory();
            fac.setScope(getRegionScope());
            RegionAttributes attr = fac.create();
            createRootRegion(name, attr);
        }
    });
    ThreadUtils.join(thread, 30 * 1000);
    assertTrue(region.isDestroyed());
    try {
        region.put("fee", "fi");
        fail("Should have thrown RegionReinitializedException");
    } catch (RegionReinitializedException e) {
    // pass
    }
    try {
        RequiredRoles.checkForRequiredRoles(region);
        fail("Should have thrown RegionReinitializedException");
    } catch (RegionReinitializedException e) {
    // pass
    }
    try {
        Role role = (Role) requiredRolesSet.iterator().next();
        RequiredRoles.isRoleInRegionMembership(region, role);
        fail("Should have thrown RegionReinitializedException");
    } catch (RegionReinitializedException e) {
    // pass
    }
    region = getRootRegion(name);
    assertNotNull(region);
    assertTrue(((AbstractRegion) region).requiresReliabilityCheck());
    assertTrue(RequiredRoles.checkForRequiredRoles(region).isEmpty());
    assertNotNull(region.getEntry(key));
    assertEquals(val, region.getEntry(key).getValue());
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) RegionAttributes(org.apache.geode.cache.RegionAttributes) CacheException(org.apache.geode.cache.CacheException) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) Role(org.apache.geode.distributed.Role) InternalRole(org.apache.geode.distributed.internal.membership.InternalRole) AttributesFactory(org.apache.geode.cache.AttributesFactory) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) RegionReinitializedException(org.apache.geode.cache.RegionReinitializedException) HashSet(java.util.HashSet) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 3 with RegionReinitializedException

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

RegionReinitializedException (org.apache.geode.cache.RegionReinitializedException)3 Region (org.apache.geode.cache.Region)2 RegionAttributes (org.apache.geode.cache.RegionAttributes)2 LocalRegion (org.apache.geode.internal.cache.LocalRegion)2 DataInputStream (java.io.DataInputStream)1 InputStream (java.io.InputStream)1 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1 Set (java.util.Set)1 CancelException (org.apache.geode.CancelException)1 LogWriter (org.apache.geode.LogWriter)1 AttributesFactory (org.apache.geode.cache.AttributesFactory)1 CacheException (org.apache.geode.cache.CacheException)1 CacheRuntimeException (org.apache.geode.cache.CacheRuntimeException)1 MembershipAttributes (org.apache.geode.cache.MembershipAttributes)1 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)1 RegionExistsException (org.apache.geode.cache.RegionExistsException)1 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)1 Role (org.apache.geode.distributed.Role)1 InternalDistributedSystem (org.apache.geode.distributed.internal.InternalDistributedSystem)1