Skip to content

Commit

Permalink
GEODE-7803: provide undeprecated internal region create (apache#4722)
Browse files Browse the repository at this point in the history
You can now use InternalRegionFactory to create a region configured with InternalRegionArguments. No need to use the deprecated AttributesFactory.
InternalRegionFactory used to be named RegionFactoryImpl.
  • Loading branch information
dschneider-pivotal authored Feb 24, 2020
1 parent aff1549 commit b082c03
Show file tree
Hide file tree
Showing 41 changed files with 683 additions and 659 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,20 @@
import java.util.Collections;

import org.apache.geode.DataSerializable;
import org.apache.geode.InternalGemFireError;
import org.apache.geode.cache.Cache;
import org.apache.geode.cache.CacheFactory;
import org.apache.geode.cache.DataPolicy;
import org.apache.geode.cache.Declarable;
import org.apache.geode.cache.EvictionAction;
import org.apache.geode.cache.EvictionAttributes;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.RegionAttributes;
import org.apache.geode.cache.Scope;
import org.apache.geode.cache.RegionShortcut;
import org.apache.geode.cache.execute.Function;
import org.apache.geode.cache.execute.FunctionContext;
import org.apache.geode.cache.partition.PartitionRegionHelper;
import org.apache.geode.distributed.DistributedLockService;
import org.apache.geode.distributed.internal.locks.DistributedMemberLock;
import org.apache.geode.internal.cache.GemFireCacheImpl;
import org.apache.geode.internal.cache.InternalRegionArguments;
import org.apache.geode.internal.cache.CacheFactoryStatics;
import org.apache.geode.internal.cache.InternalCache;
import org.apache.geode.internal.cache.InternalRegionFactory;
import org.apache.geode.internal.cache.PartitionedRegion;
import org.apache.geode.internal.cache.xmlcache.CacheXmlGenerator;
import org.apache.geode.internal.cache.xmlcache.RegionAttributesCreation;
Expand All @@ -55,11 +52,11 @@ public class CreateRegionFunction implements Function, Declarable, DataSerializa
static final String REGION_CONFIGURATION_METADATA_REGION =
"__regionConfigurationMetadata";

private final Cache cache;
private final InternalCache cache;
private final Region<String, RegionConfiguration> regionConfigurationsRegion;

public CreateRegionFunction() {
this.cache = CacheFactory.getAnyInstance();
this.cache = CacheFactoryStatics.getAnyInstance();
this.regionConfigurationsRegion = createRegionConfigurationMetadataRegion();
}

Expand Down Expand Up @@ -251,7 +248,6 @@ private void createBuckets(PartitionedRegion region) {
PartitionRegionHelper.assignBucketsToPartitions(region);
}

@SuppressWarnings("unchecked")
private Region<String, RegionConfiguration> createRegionConfigurationMetadataRegion() {
// a sessionFactory in hibernate could have been re-started
// so, it is possible that this region exists already
Expand All @@ -262,22 +258,11 @@ private Region<String, RegionConfiguration> createRegionConfigurationMetadataReg
return region;
}

GemFireCacheImpl gemFireCache = (GemFireCacheImpl) cache;
InternalRegionArguments ira = new InternalRegionArguments().setInternalRegion(true);
RegionAttributesCreation regionAttributesCreation = new RegionAttributesCreation();
regionAttributesCreation.setScope(Scope.DISTRIBUTED_ACK);
regionAttributesCreation.setDataPolicy(DataPolicy.REPLICATE);
regionAttributesCreation.addCacheListener(new RegionConfigurationCacheListener());

try {
return gemFireCache.createVMRegion(REGION_CONFIGURATION_METADATA_REGION,
regionAttributesCreation, ira);
} catch (IOException | ClassNotFoundException e) {
InternalGemFireError assErr = new InternalGemFireError("unexpected exception");
assErr.initCause(e);

throw assErr;
}
InternalRegionFactory<String, RegionConfiguration> regionFactory =
cache.createInternalRegionFactory(RegionShortcut.REPLICATE);
regionFactory.addCacheListener(new RegionConfigurationCacheListener());
regionFactory.setInternalRegion(true);
return regionFactory.create(REGION_CONFIGURATION_METADATA_REGION);
}

private void writeCacheXml() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
Expand All @@ -59,7 +58,6 @@
import org.apache.geode.cache.Region;
import org.apache.geode.cache.RegionAttributes;
import org.apache.geode.cache.Scope;
import org.apache.geode.cache.TimeoutException;
import org.apache.geode.distributed.internal.ResourceEvent;
import org.apache.geode.distributed.internal.ResourceEventsListener;
import org.apache.geode.distributed.internal.locks.DLockBatch;
Expand All @@ -68,7 +66,7 @@
import org.apache.geode.distributed.internal.membership.api.MembershipManagerHelper;
import org.apache.geode.internal.cache.CommitReplyException;
import org.apache.geode.internal.cache.GemFireCacheImpl;
import org.apache.geode.internal.cache.InternalRegionArguments;
import org.apache.geode.internal.cache.InternalRegionFactory;
import org.apache.geode.internal.cache.LocalRegion;
import org.apache.geode.internal.cache.RegionEntry;
import org.apache.geode.internal.cache.TXManagerImpl;
Expand Down Expand Up @@ -1434,27 +1432,17 @@ public void run2() {
new CacheSerializableRunnable("Initialize regions that cause trouble") {
@Override
public void run2() {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
InternalRegionArguments ira =
new InternalRegionArguments().setTestCallable(new TXTroubleMaker());
try {
getCache().createDiskStoreFactory().setDiskDirs(getDiskDirs())
.create(diskStoreName);
TXManagerImpl.ALLOW_PERSISTENT_TRANSACTIONS = true;
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
af.setScope(Scope.DISTRIBUTED_ACK);
af.setDiskStoreName(diskStoreName);
gfc.createVMRegion(rgnName1, af.create(), ira);
gfc.createVMRegion(rgnName2, af.create(), ira);
gfc.getInternalDistributedSystem().addResourceListener(new ShutdownListener());
} catch (IOException ioe) {
fail(ioe.toString());
} catch (TimeoutException e) {
fail(e.toString());
} catch (ClassNotFoundException e) {
fail(e.toString());
}
getCache().createDiskStoreFactory().setDiskDirs(getDiskDirs())
.create(diskStoreName);
TXManagerImpl.ALLOW_PERSISTENT_TRANSACTIONS = true;
InternalRegionFactory factory = getCache().createInternalRegionFactory();
factory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setDiskStoreName(diskStoreName);
factory.setTestCallable(new TXTroubleMaker());
factory.create(rgnName1);
factory.create(rgnName2);
getCache().getInternalDistributedSystem().addResourceListener(new ShutdownListener());
}
};
trouble1.invoke(initTroulbeRegions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.geode.cache.DiskStore;
import org.apache.geode.cache.DiskStoreFactory;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.RegionShortcut;
import org.apache.geode.cache.Scope;
import org.apache.geode.internal.cache.entries.VersionedThinDiskRegionEntryHeapObjectKey;
import org.apache.geode.test.dunit.VM;
Expand Down Expand Up @@ -140,22 +141,22 @@ private void createCacheForVM1() throws IOException, ClassNotFoundException {

DiskStore diskStore = dsf.create(uniqueName);

AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
InternalRegionFactory factory =
getCache().createInternalRegionFactory(RegionShortcut.REPLICATE_PERSISTENT);
factory.setDiskSynchronous(false);
factory.setDiskStoreName(diskStore.getName());

DistributedRegion distRegion = new DistributedRegion(regionName, factory.create(), null,
getCache(), new InternalRegionArguments().setDestroyLockFlag(true).setRecreateFlag(false)
.setSnapshotInputStream(null).setImageTarget(null),
disabledClock());
DistributedRegion distRegion =
new DistributedRegion(regionName, factory.getCreateAttributes(), null,
getCache(),
new InternalRegionArguments().setDestroyLockFlag(true).setRecreateFlag(false)
.setSnapshotInputStream(null).setImageTarget(null),
disabledClock());

distRegion.entries.setEntryFactory(new TestableDiskRegionEntryFactory());

getCache().createVMRegion(regionName, factory.create(),
new InternalRegionArguments().setInternalMetaRegion(distRegion).setDestroyLockFlag(true)
.setSnapshotInputStream(null).setImageTarget(null));
factory.setInternalMetaRegion(distRegion).setDestroyLockFlag(true)
.setSnapshotInputStream(null).setImageTarget(null);
factory.create(regionName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ public void diskAccessExceptionDuringGiiShouldShutdown() throws Exception {

DiskStore diskStore = diskStoreFactory.create(uniqueName);

AttributesFactory factory = new AttributesFactory();
InternalRegionFactory factory = getCache().createInternalRegionFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
factory.setDiskSynchronous(false);
factory.setDiskStoreName(diskStore.getName());

Region<Integer, Integer> region = getCache().createRegion(uniqueName, factory.create());
Region<Integer, Integer> region = factory.create(uniqueName);

// Now put entries in the disk region
for (int i = 0; i < 100; ++i) {
Expand All @@ -129,22 +129,22 @@ public void diskAccessExceptionDuringGiiShouldShutdown() throws Exception {
// Now recreate the region but set the factory such that disk region entry object
// used is customized by us to throw exception while writing to disk

DistributedRegion distributedRegion = new DistributedRegion(uniqueName, factory.create(), null,
getCache(), new InternalRegionArguments().setDestroyLockFlag(true).setRecreateFlag(false)
.setSnapshotInputStream(null).setImageTarget(null),
disabledClock());
DistributedRegion distributedRegion =
new DistributedRegion(uniqueName, factory.getCreateAttributes(), null,
getCache(),
new InternalRegionArguments().setDestroyLockFlag(true).setRecreateFlag(false)
.setSnapshotInputStream(null).setImageTarget(null),
disabledClock());

distributedRegion.entries.setEntryFactory(new DiskRegionEntryThrowsFactory());

InternalRegionArguments internalRegionArguments = new InternalRegionArguments();
internalRegionArguments.setInternalMetaRegion(distributedRegion);
internalRegionArguments.setDestroyLockFlag(true);
internalRegionArguments.setSnapshotInputStream(null);
internalRegionArguments.setImageTarget(null);
factory.setInternalMetaRegion(distributedRegion);
factory.setDestroyLockFlag(true);
factory.setSnapshotInputStream(null);
factory.setImageTarget(null);

assertThatThrownBy(
() -> getCache().createVMRegion(uniqueName, factory.create(), internalRegionArguments))
.isInstanceOf(DiskAccessException.class);
assertThatThrownBy(() -> factory.create(uniqueName))
.isInstanceOf(DiskAccessException.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.junit.rules.TemporaryFolder;
import org.junit.rules.TestName;

import org.apache.geode.cache.AttributesFactory;
import org.apache.geode.cache.CacheFactory;
import org.apache.geode.cache.DataPolicy;
import org.apache.geode.cache.DiskStoreFactory;
Expand Down Expand Up @@ -63,7 +62,7 @@ public void setUp() throws Exception {

cache = (InternalCache) new CacheFactory().set("locators", "").set("mcast-port", "0").create();

AttributesFactory<Integer, Integer> factory = new AttributesFactory();
InternalRegionFactory<Integer, Integer> factory = cache.createInternalRegionFactory();

DiskStoreFactory diskStoreFactory = cache.createDiskStoreFactory();
diskStoreFactory.setDiskDirsAndSizes(new File[] {dir}, new int[] {Integer.MAX_VALUE});
Expand All @@ -78,17 +77,17 @@ public void setUp() throws Exception {
factory.setEvictionAttributes(
EvictionAttributes.createLRUEntryAttributes(1, EvictionAction.OVERFLOW_TO_DISK));

RegionAttributes<Integer, Integer> regionAttributes = factory.create();
RegionAttributes<Integer, Integer> regionAttributes = factory.getCreateAttributes();

InternalRegionArguments args = new InternalRegionArguments().setDestroyLockFlag(true)
.setRecreateFlag(false).setSnapshotInputStream(null).setImageTarget(null);

DistributedRegion distributedRegion =
new DistributedRegion(regionName, regionAttributes, null, cache, args, disabledClock());

region = cache.createVMRegion(regionName, regionAttributes,
new InternalRegionArguments().setInternalMetaRegion(distributedRegion)
.setDestroyLockFlag(true).setSnapshotInputStream(null).setImageTarget(null));
factory.setInternalMetaRegion(distributedRegion)
.setDestroyLockFlag(true).setSnapshotInputStream(null).setImageTarget(null);
region = factory.create(regionName);
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@ public class PRTXJUnitTest extends TXJUnitTest {

@Override
protected void createRegion() throws Exception {
AttributesFactory attributesFactory = new AttributesFactory();
InternalRegionFactory regionFactory = cache.createInternalRegionFactory();
// test validation expects this behavior
attributesFactory.setConcurrencyChecksEnabled(false);
attributesFactory
regionFactory.setConcurrencyChecksEnabled(false);
regionFactory
.setPartitionAttributes(new PartitionAttributesFactory().setTotalNumBuckets(3).create());
regionFactory.setDestroyLockFlag(true).setRecreateFlag(false)
.setSnapshotInputStream(null).setImageTarget(null);

this.region = new PRWithLocalOps(getClass().getSimpleName(), attributesFactory.create(), null,
this.cache, new InternalRegionArguments().setDestroyLockFlag(true).setRecreateFlag(false)
.setSnapshotInputStream(null).setImageTarget(null));
this.region =
new PRWithLocalOps(getClass().getSimpleName(), regionFactory.getCreateAttributes(), null,
this.cache, regionFactory.getInternalRegionArguments());

((PartitionedRegion) this.region).initialize(null, null, null);
((PartitionedRegion) this.region).postCreateRegion();
Expand Down
Loading

0 comments on commit b082c03

Please sign in to comment.