use of org.apache.geode.cache.DataPolicy in project geode by apache.
the class ServerCQImpl method registerCq.
@Override
public void registerCq(ClientProxyMembershipID p_clientProxyId, CacheClientNotifier p_ccn, int p_cqState) throws CqException, RegionNotFoundException {
CacheClientProxy clientProxy = null;
this.clientProxyId = p_clientProxyId;
if (p_ccn != null) {
this.ccn = p_ccn;
clientProxy = p_ccn.getClientProxy(p_clientProxyId, true);
}
validateCq();
final boolean isDebugEnabled = logger.isDebugEnabled();
StringId msg = LocalizedStrings.ONE_ARG;
Throwable t = null;
try {
this.query = constructServerSideQuery();
if (isDebugEnabled) {
logger.debug("Server side query for the cq: {} is: {}", cqName, this.query.getQueryString());
}
} catch (Exception ex) {
t = ex;
if (ex instanceof ClassNotFoundException) {
msg = LocalizedStrings.CqQueryImpl_CLASS_NOT_FOUND_EXCEPTION_THE_ANTLRJAR_OR_THE_SPCIFIED_CLASS_MAY_BE_MISSING_FROM_SERVER_SIDE_CLASSPATH_ERROR_0;
} else {
msg = LocalizedStrings.CqQueryImpl_ERROR_WHILE_PARSING_THE_QUERY_ERROR_0;
}
} finally {
if (t != null) {
String s = msg.toLocalizedString(t);
if (isDebugEnabled) {
logger.debug(s, t);
}
throw new CqException(s);
}
}
// Update Regions Book keeping.
// TODO replace getRegion() with getRegionByPathForProcessing() so this doesn't block
// if the region is still being initialized
this.cqBaseRegion = (LocalRegion) cqService.getCache().getRegion(regionName);
if (this.cqBaseRegion == null) {
throw new RegionNotFoundException(LocalizedStrings.CqQueryImpl_REGION__0_SPECIFIED_WITH_CQ_NOT_FOUND_CQNAME_1.toLocalizedString(new Object[] { regionName, this.cqName }));
}
// Make sure that the region is partitioned or
// replicated with distributed ack or global.
DataPolicy dp = this.cqBaseRegion.getDataPolicy();
this.isPR = dp.withPartitioning();
if (!(this.isPR || dp.withReplication())) {
String errMsg = null;
// replicated regions with eviction set to local destroy get turned into preloaded
if (dp.withPreloaded() && cqBaseRegion.getAttributes().getEvictionAttributes() != null && cqBaseRegion.getAttributes().getEvictionAttributes().getAction().equals(EvictionAction.LOCAL_DESTROY)) {
errMsg = LocalizedStrings.CqQueryImpl_CQ_NOT_SUPPORTED_FOR_REPLICATE_WITH_LOCAL_DESTROY.toString(this.regionName, cqBaseRegion.getAttributes().getEvictionAttributes().getAction());
} else {
errMsg = "The region " + this.regionName + " specified in CQ creation is neither replicated nor partitioned; " + "only replicated or partitioned regions are allowed in CQ creation.";
}
if (isDebugEnabled) {
logger.debug(errMsg);
}
throw new CqException(errMsg);
}
if ((dp.withReplication() && (!(cqBaseRegion.getAttributes().getScope().isDistributedAck() || cqBaseRegion.getAttributes().getScope().isGlobal())))) {
String errMsg = "The replicated region " + this.regionName + " specified in CQ creation does not have scope supported by CQ." + " The CQ supported scopes are DISTRIBUTED_ACK and GLOBAL.";
if (isDebugEnabled) {
logger.debug(errMsg);
}
throw new CqException(errMsg);
}
// Can be null by the time we are here
if (clientProxy != null) {
clientProxy.incCqCount();
if (clientProxy.hasOneCq()) {
cqService.stats().incClientsWithCqs();
}
if (isDebugEnabled) {
logger.debug("Added CQ to the base region: {} With key as: {}", cqBaseRegion.getFullPath(), serverCqName);
}
}
this.updateCqCreateStats();
// Initialize the state of CQ.
if (this.cqState.getState() != p_cqState) {
setCqState(p_cqState);
}
// it to other matching cqs for performance reasons
if (p_cqState == CqStateImpl.RUNNING) {
// Add to the matchedCqMap.
cqService.addToMatchingCqMap(this);
}
// Initialize CQ results (key) cache.
if (CqServiceProvider.MAINTAIN_KEYS) {
this.cqResultKeys = new HashMap<Object, Object>();
// added to the results cache (not from the CQ Results).
if (this.isPR) {
this.setCqResultsCacheInitialized();
} else {
this.destroysWhileCqResultsInProgress = new HashSet<Object>();
}
}
if (p_ccn != null) {
try {
cqService.addToCqMap(this);
} catch (CqExistsException cqe) {
// Should not happen.
throw new CqException(LocalizedStrings.CqQueryImpl_UNABLE_TO_CREATE_CQ_0_ERROR__1.toLocalizedString(new Object[] { cqName, cqe.getMessage() }));
}
this.cqBaseRegion.getFilterProfile().registerCq(this);
}
}
use of org.apache.geode.cache.DataPolicy in project geode by apache.
the class MultiVMRegionTestCase method testNonblockingGetInitialImage.
/**
* Tests that distributed ack operations do not block while another cache is doing a
* getInitialImage.
*/
@Test
public void testNonblockingGetInitialImage() throws Exception {
assumeTrue(supportsReplication());
// don't run this test if global scope since its too difficult to predict
// how many concurrent operations will occur
assumeFalse(getRegionAttributes().getScope().isGlobal());
final String name = this.getUniqueName();
final byte[][] values = new byte[NB1_NUM_ENTRIES][];
for (int i = 0; i < NB1_NUM_ENTRIES; i++) {
values[i] = new byte[NB1_VALUE_SIZE];
Arrays.fill(values[i], (byte) 0x42);
}
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm2 = host.getVM(2);
SerializableRunnable create = new CacheSerializableRunnable("Create Mirrored Region") {
@Override
public void run2() throws CacheException {
beginCacheXml();
{
// root region must be DACK because its used to sync up async subregions
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setDataPolicy(DataPolicy.NORMAL);
factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("MJT DEBUG: attrs0 are " + factory.create());
createRootRegion(factory.create());
}
{
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
if (getRegionAttributes().getDataPolicy() == DataPolicy.NORMAL) {
factory.setDataPolicy(DataPolicy.PRELOADED);
}
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("MJT DEBUG: attrs1 are " + factory.create());
Region region = createRegion(name, factory.create());
}
finishCacheXml(name);
// reset slow
org.apache.geode.internal.cache.InitialImageOperation.slowImageProcessing = 0;
}
};
vm0.invoke(new CacheSerializableRunnable("Create Nonmirrored Region") {
@Override
public void run2() throws CacheException {
{
// root region must be DACK because its used to sync up async subregions
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setDataPolicy(DataPolicy.EMPTY);
createRootRegion(factory.create());
}
{
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
createRegion(name, factory.create());
}
// reset slow
org.apache.geode.internal.cache.InitialImageOperation.slowImageProcessing = 0;
}
});
vm0.invoke(new CacheSerializableRunnable("Put initial data") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
for (int i = 0; i < NB1_NUM_ENTRIES; i++) {
region.put(new Integer(i), values[i]);
}
assertEquals(NB1_NUM_ENTRIES, region.keySet().size());
}
});
// start asynchronous process that does updates to the data
AsyncInvocation async = vm0.invokeAsync(new CacheSerializableRunnable("Do Nonblocking Operations") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
// wait for profile of getInitialImage cache to show up
final org.apache.geode.internal.cache.CacheDistributionAdvisor adv = ((org.apache.geode.internal.cache.DistributedRegion) region).getCacheDistributionAdvisor();
final int expectedProfiles = 1;
WaitCriterion ev = new WaitCriterion() {
@Override
public boolean done() {
DataPolicy currentPolicy = getRegionAttributes().getDataPolicy();
if (currentPolicy == DataPolicy.PRELOADED) {
return (adv.advisePreloadeds().size() + adv.adviseReplicates().size()) >= expectedProfiles;
} else {
return adv.adviseReplicates().size() >= expectedProfiles;
}
}
@Override
public String description() {
return "replicate count never reached " + expectedProfiles;
}
};
Wait.waitForCriterion(ev, 60 * 1000, 200, true);
DataPolicy currentPolicy = getRegionAttributes().getDataPolicy();
int numProfiles = 0;
if (currentPolicy == DataPolicy.PRELOADED) {
numProfiles = adv.advisePreloadeds().size() + adv.adviseReplicates().size();
} else {
numProfiles = adv.adviseReplicates().size();
}
assertTrue(numProfiles >= expectedProfiles);
// before the get initial image is complete.
for (int i = 1; i < NB1_NUM_ENTRIES; i += 2) {
Object key = new Integer(i);
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("Operation #" + i + " on key " + key);
switch(i % 6) {
case // UPDATE
1:
// use the current timestamp so we know when it happened
// we could have used last modification timestamps, but
// this works without enabling statistics
Object value = new Long(System.currentTimeMillis());
region.put(key, value);
// }
break;
case // INVALIDATE
3:
region.invalidate(key);
if (getRegionAttributes().getScope().isDistributedAck()) {
// do a nonblocking netSearch
assertNull(region.get(key));
}
break;
case // DESTROY
5:
region.destroy(key);
if (getRegionAttributes().getScope().isDistributedAck()) {
// do a nonblocking netSearch
assertNull(region.get(key));
}
break;
default:
fail("unexpected modulus result: " + i);
break;
}
}
// add some new keys
for (int i = NB1_NUM_ENTRIES; i < NB1_NUM_ENTRIES + 200; i++) {
region.create(new Integer(i), new Long(System.currentTimeMillis()));
}
// now do a put and our DACK root region which will not complete
// until processed on otherside which means everything done before this
// point has been processed
getRootRegion().put("DONE", "FLUSH_OPS");
}
});
// slow down image processing to make it more likely to get async updates
if (!getRegionAttributes().getScope().isGlobal()) {
vm2.invoke(new SerializableRunnable("Set slow image processing") {
@Override
public void run() {
// if this is a no_ack test, then we need to slow down more because of the
// pauses in the nonblocking operations
int pause = 200;
org.apache.geode.internal.cache.InitialImageOperation.slowImageProcessing = pause;
}
});
}
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("Before GetInitialImage, data policy is " + getRegionAttributes().getDataPolicy() + ", scope is " + getRegionAttributes().getScope());
AsyncInvocation asyncGII = vm2.invokeAsync(create);
if (!getRegionAttributes().getScope().isGlobal()) {
// wait for nonblocking operations to complete
ThreadUtils.join(async, 30 * 1000);
vm2.invoke(new SerializableRunnable("Set fast image processing") {
@Override
public void run() {
org.apache.geode.internal.cache.InitialImageOperation.slowImageProcessing = 0;
}
});
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("after async nonblocking ops complete");
}
// wait for GII to complete
ThreadUtils.join(asyncGII, 30 * 1000);
final long iiComplete = System.currentTimeMillis();
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("Complete GetInitialImage at: " + System.currentTimeMillis());
if (getRegionAttributes().getScope().isGlobal()) {
// wait for nonblocking operations to complete
ThreadUtils.join(async, 30 * 1000);
}
if (async.exceptionOccurred()) {
fail("async failed", async.getException());
}
if (asyncGII.exceptionOccurred()) {
fail("asyncGII failed", asyncGII.getException());
}
// Locally destroy the region in vm0 so we know that they are not found by
// a netSearch
vm0.invoke(new CacheSerializableRunnable("Locally destroy region") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
region.localDestroyRegion();
}
});
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("after localDestroyRegion");
// invoke repeating so noack regions wait for all updates to get processed
vm2.invokeRepeatingIfNecessary(new CacheSerializableRunnable("Verify entryCount") {
boolean entriesDumped = false;
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
// expected entry count (subtract entries destroyed)
int entryCount = NB1_NUM_ENTRIES + 200 - NB1_NUM_ENTRIES / 6;
int actualCount = region.entrySet(false).size();
if (actualCount == NB1_NUM_ENTRIES + 200) {
// entries not destroyed, dump entries that were supposed to have been destroyed
dumpDestroyedEntries(region);
}
assertEquals(entryCount, actualCount);
}
private void dumpDestroyedEntries(Region region) throws EntryNotFoundException {
if (entriesDumped)
return;
entriesDumped = true;
LogWriter logger = org.apache.geode.test.dunit.LogWriterUtils.getLogWriter();
logger.info("DUMPING Entries with values in VM that should have been destroyed:");
for (int i = 5; i < NB1_NUM_ENTRIES; i += 6) {
try {
logger.info(i + "-->" + ((org.apache.geode.internal.cache.LocalRegion) region).getValueInVM(new Integer(i)));
} catch (EntryNotFoundException expected) {
logger.info(i + "-->" + "CORRECTLY DESTROYED");
}
}
}
}, 5000);
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("after verify entryCount");
vm2.invoke(new CacheSerializableRunnable("Verify keys/values & Nonblocking") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
// expected entry count (subtract entries destroyed)
int entryCount = NB1_NUM_ENTRIES + 200 - NB1_NUM_ENTRIES / 6;
assertEquals(entryCount, region.entrySet(false).size());
// determine how many entries were updated before getInitialImage
// was complete
int numConcurrent = 0;
for (int i = 0; i < NB1_NUM_ENTRIES + 200; i++) {
Region.Entry entry = region.getEntry(new Integer(i));
Object v = entry == null ? null : entry.getValue();
if (i < NB1_NUM_ENTRIES) {
// old keys
switch(i % 6) {
// even keys are originals
case 0:
case 2:
case 4:
assertNotNull(entry);
assertTrue(Arrays.equals(values[i], (byte[]) v));
break;
case // updated
1:
assertNotNull(v);
assertTrue("Value for key " + i + " is not a Long, is a " + v.getClass().getName(), v instanceof Long);
Long timestamp = (Long) entry.getValue();
if (timestamp.longValue() < iiComplete) {
numConcurrent++;
}
break;
case // invalidated
3:
assertNotNull(entry);
assertNull("Expected value for " + i + " to be null, but was " + v, v);
break;
case // destroyed
5:
assertNull(entry);
break;
default:
fail("unexpected modulus result: " + (i % 6));
break;
}
} else {
// new keys
assertNotNull(v);
assertTrue("Value for key " + i + " is not a Long, is a " + v.getClass().getName(), v instanceof Long);
Long timestamp = (Long) entry.getValue();
if (timestamp.longValue() < iiComplete) {
numConcurrent++;
}
}
}
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info(name + ": " + numConcurrent + " entries out of " + entryCount + " were updated concurrently with getInitialImage");
// make sure at least some of them were concurrent
if (region.getAttributes().getScope().isGlobal()) {
assertTrue("Too many concurrent updates when expected to block: " + numConcurrent, numConcurrent < 10);
} else {
int min = 30;
assertTrue("Not enough updates concurrent with getInitialImage occurred to my liking. " + numConcurrent + " entries out of " + entryCount + " were updated concurrently with getInitialImage, and I'd expect at least " + min + " or so", numConcurrent >= min);
}
}
});
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("after verify key/values");
}
use of org.apache.geode.cache.DataPolicy in project geode by apache.
the class CacheXmlGenerator method generate.
/**
* Generates XML for region attributes.
*
* @param id The id of the named region attributes (may be <code>null</code>)
*/
private void generate(String id, RegionAttributes attrs) throws SAXException {
AttributesImpl atts = new AttributesImpl();
if (id != null) {
atts.addAttribute("", "", ID, "", id);
}
// point, the refid information is lost.
if (attrs instanceof RegionAttributesCreation) {
String refId = ((RegionAttributesCreation) attrs).getRefid();
if (refId != null) {
atts.addAttribute("", "", REFID, "", refId);
}
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasScope())) {
String scopeString;
Scope scope = attrs.getScope();
if (scope.equals(Scope.LOCAL)) {
scopeString = LOCAL;
} else if (scope.equals(Scope.DISTRIBUTED_NO_ACK)) {
scopeString = DISTRIBUTED_NO_ACK;
} else if (scope.equals(Scope.DISTRIBUTED_ACK)) {
scopeString = DISTRIBUTED_ACK;
} else if (scope.equals(Scope.GLOBAL)) {
scopeString = GLOBAL;
} else {
throw new InternalGemFireException(LocalizedStrings.CacheXmlGenerator_UNKNOWN_SCOPE_0.toLocalizedString(scope));
}
final boolean isPartitionedRegion;
if (attrs instanceof RegionAttributesCreation) {
RegionAttributesCreation rac = (RegionAttributesCreation) attrs;
isPartitionedRegion = rac.getPartitionAttributes() != null || (rac.hasDataPolicy() && rac.getDataPolicy().withPartitioning());
} else {
isPartitionedRegion = attrs.getPartitionAttributes() != null || attrs.getDataPolicy().withPartitioning();
}
if (!isPartitionedRegion) {
// Partitioned Region don't support setting scope
if (generateDefaults() || !scope.equals(AbstractRegion.DEFAULT_SCOPE))
atts.addAttribute("", "", SCOPE, "", scopeString);
}
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasEarlyAck())) {
if (generateDefaults() || attrs.getEarlyAck())
atts.addAttribute("", "", EARLY_ACK, "", String.valueOf(attrs.getEarlyAck()));
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasMulticastEnabled())) {
if (generateDefaults() || attrs.getMulticastEnabled())
atts.addAttribute("", "", MULTICAST_ENABLED, "", String.valueOf(attrs.getMulticastEnabled()));
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasPublisher())) {
if (generateDefaults() || attrs.getPublisher())
atts.addAttribute("", "", PUBLISHER, "", String.valueOf(attrs.getPublisher()));
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasEnableAsyncConflation())) {
if (generateDefaults() || attrs.getEnableAsyncConflation())
atts.addAttribute("", "", ENABLE_ASYNC_CONFLATION, "", String.valueOf(attrs.getEnableAsyncConflation()));
}
if (this.version.compareTo(CacheXmlVersion.GEMFIRE_5_0) >= 0) {
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasEnableSubscriptionConflation())) {
if (this.version.compareTo(CacheXmlVersion.GEMFIRE_5_7) >= 0) {
// starting with 5.7 it is enable-subscription-conflation
if (generateDefaults() || attrs.getEnableSubscriptionConflation())
atts.addAttribute("", "", ENABLE_SUBSCRIPTION_CONFLATION, "", String.valueOf(attrs.getEnableSubscriptionConflation()));
} else {
// before 5.7 it was enable-bridge-conflation
if (generateDefaults() || attrs.getEnableSubscriptionConflation())
atts.addAttribute("", "", ENABLE_BRIDGE_CONFLATION, "", String.valueOf(attrs.getEnableSubscriptionConflation()));
}
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasDataPolicy())) {
String dpString;
DataPolicy dp = attrs.getDataPolicy();
if (dp.isEmpty()) {
dpString = EMPTY_DP;
} else if (dp.isNormal()) {
dpString = NORMAL_DP;
} else if (dp.isPreloaded()) {
dpString = PRELOADED_DP;
} else if (dp.isReplicate()) {
dpString = REPLICATE_DP;
} else if (dp == DataPolicy.PERSISTENT_REPLICATE) {
dpString = PERSISTENT_REPLICATE_DP;
} else if (dp == DataPolicy.PERSISTENT_PARTITION) {
dpString = PERSISTENT_PARTITION_DP;
} else if (dp.isPartition()) {
if (this.version.compareTo(CacheXmlVersion.GEMFIRE_5_1) >= 0) {
dpString = PARTITION_DP;
} else {
// prior to 5.1 the data policy for partitioned regions was EMPTY
dpString = EMPTY_DP;
}
} else {
throw new InternalGemFireException(LocalizedStrings.CacheXmlGenerator_UNKNOWN_DATA_POLICY_0.toLocalizedString(dp));
}
if (generateDefaults() || !dp.equals(DataPolicy.DEFAULT))
atts.addAttribute("", "", DATA_POLICY, "", dpString);
}
// hasDataPolicy
} else // GEMFIRE_5_0 >= 0
{
// GEMFIRE_5_0 < 0
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasEnableSubscriptionConflation())) {
if (generateDefaults() || attrs.getEnableSubscriptionConflation())
atts.addAttribute("", "", "enable-conflation", "", String.valueOf(attrs.getEnableSubscriptionConflation()));
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasMirrorType())) {
String mirrorString;
MirrorType mirror = attrs.getMirrorType();
if (mirror.equals(MirrorType.NONE))
mirrorString = NONE;
else if (mirror.equals(MirrorType.KEYS))
mirrorString = KEYS;
else if (mirror.equals(MirrorType.KEYS_VALUES))
mirrorString = KEYS_VALUES;
else
throw new InternalGemFireException(LocalizedStrings.CacheXmlGenerator_UNKNOWN_MIRROR_TYPE_0.toLocalizedString(mirror));
atts.addAttribute("", "", MIRROR_TYPE, "", mirrorString);
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasPersistBackup())) {
atts.addAttribute("", "", PERSIST_BACKUP, "", String.valueOf(attrs.getDataPolicy() == DataPolicy.PERSISTENT_REPLICATE));
}
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasInitialCapacity())) {
if (generateDefaults() || attrs.getInitialCapacity() != 16)
atts.addAttribute("", "", INITIAL_CAPACITY, "", String.valueOf(attrs.getInitialCapacity()));
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasLoadFactor())) {
if (generateDefaults() || attrs.getLoadFactor() != 0.75f)
atts.addAttribute("", "", LOAD_FACTOR, "", String.valueOf(attrs.getLoadFactor()));
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasConcurrencyLevel())) {
if (generateDefaults() || attrs.getConcurrencyLevel() != 16)
atts.addAttribute("", "", CONCURRENCY_LEVEL, "", String.valueOf(attrs.getConcurrencyLevel()));
}
if (this.version.compareTo(CacheXmlVersion.GEMFIRE_7_0) >= 0) {
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasConcurrencyChecksEnabled())) {
if (generateDefaults() || attrs.getConcurrencyChecksEnabled() != true)
/* fixes bug 46654 */
atts.addAttribute("", "", CONCURRENCY_CHECKS_ENABLED, "", String.valueOf(attrs.getConcurrencyChecksEnabled()));
}
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasStatisticsEnabled())) {
if (generateDefaults() || attrs.getStatisticsEnabled())
atts.addAttribute("", "", STATISTICS_ENABLED, "", String.valueOf(attrs.getStatisticsEnabled()));
}
if (!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasIgnoreJTA()) {
if (generateDefaults() || attrs.getIgnoreJTA())
atts.addAttribute("", "", IGNORE_JTA, "", String.valueOf(attrs.getIgnoreJTA()));
}
if (this.version.compareTo(CacheXmlVersion.GEMFIRE_4_0) >= 0) {
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasIsLockGrantor())) {
if (generateDefaults() || attrs.isLockGrantor())
atts.addAttribute("", "", IS_LOCK_GRANTOR, "", String.valueOf(attrs.isLockGrantor()));
}
}
if (this.version.compareTo(CacheXmlVersion.GEMFIRE_5_7) >= 0) {
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasPoolName())) {
String cpVal = attrs.getPoolName();
if (cpVal == null) {
cpVal = "";
}
if (generateDefaults() || !cpVal.equals(""))
atts.addAttribute("", "", POOL_NAME, "", cpVal);
}
}
if (this.version.compareTo(CacheXmlVersion.GEMFIRE_6_5) >= 0) {
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasDiskStoreName())) {
String dsVal = attrs.getDiskStoreName();
if (dsVal != null) {
atts.addAttribute("", "", DISK_STORE_NAME, "", dsVal);
}
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasDiskSynchronous())) {
if (generateDefaults() || attrs.isDiskSynchronous() != AttributesFactory.DEFAULT_DISK_SYNCHRONOUS)
atts.addAttribute("", "", DISK_SYNCHRONOUS, "", String.valueOf(attrs.isDiskSynchronous()));
}
}
if (this.version.compareTo(CacheXmlVersion.GEMFIRE_6_1) >= 0)
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasCloningEnabled())) {
if (generateDefaults() || attrs.getCloningEnabled())
atts.addAttribute("", "", CLONING_ENABLED, "", String.valueOf(attrs.getCloningEnabled()));
}
if (this.version.compareTo(CacheXmlVersion.GEMFIRE_7_0) >= 0) {
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasGatewaySenderId())) {
Set<String> senderIds = new HashSet<String>(attrs.getGatewaySenderIds());
StringBuilder senderStringBuff = new StringBuilder();
if (senderIds != null && senderIds.size() != 0) {
for (String senderId : senderIds) {
if (!(senderStringBuff.length() == 0)) {
senderStringBuff.append(",");
}
senderStringBuff.append(senderId);
}
}
if (generateDefaults() || senderStringBuff.length() > 0)
atts.addAttribute("", "", GATEWAY_SENDER_IDS, "", senderStringBuff.toString());
}
}
if (this.version.compareTo(CacheXmlVersion.GEMFIRE_7_0) >= 0) {
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasAsyncEventListeners())) {
Set<String> asyncEventQueueIds = new HashSet<String>(attrs.getAsyncEventQueueIds());
StringBuilder asyncEventQueueStringBuff = new StringBuilder();
if (asyncEventQueueIds != null && asyncEventQueueIds.size() != 0) {
for (String asyncEventQueueId : asyncEventQueueIds) {
if (!(asyncEventQueueStringBuff.length() == 0)) {
asyncEventQueueStringBuff.append(",");
}
asyncEventQueueStringBuff.append(asyncEventQueueId);
}
}
if (generateDefaults() || asyncEventQueueStringBuff.length() > 0)
atts.addAttribute("", "", ASYNC_EVENT_QUEUE_IDS, "", asyncEventQueueStringBuff.toString());
}
}
if (this.version.compareTo(CacheXmlVersion.GEODE_1_0) >= 0) {
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasOffHeap())) {
if (generateDefaults() || attrs.getOffHeap()) {
atts.addAttribute("", "", OFF_HEAP, "", String.valueOf(attrs.getOffHeap()));
}
}
}
handler.startElement("", REGION_ATTRIBUTES, REGION_ATTRIBUTES, atts);
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasKeyConstraint())) {
generate(attrs.getKeyConstraint(), KEY_CONSTRAINT);
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasValueConstraint())) {
generate(attrs.getValueConstraint(), VALUE_CONSTRAINT);
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasRegionTimeToLive())) {
if (generateDefaults() || !attrs.getRegionTimeToLive().equals(ExpirationAttributes.DEFAULT))
generate(REGION_TIME_TO_LIVE, attrs.getRegionTimeToLive(), null);
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasRegionIdleTimeout())) {
if (generateDefaults() || !attrs.getRegionIdleTimeout().equals(ExpirationAttributes.DEFAULT))
generate(REGION_IDLE_TIME, attrs.getRegionIdleTimeout(), null);
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasEntryTimeToLive() || ((RegionAttributesCreation) attrs).hasCustomEntryTimeToLive())) {
if (generateDefaults() || !attrs.getEntryTimeToLive().equals(ExpirationAttributes.DEFAULT) || attrs.getCustomEntryTimeToLive() != null)
generate(ENTRY_TIME_TO_LIVE, attrs.getEntryTimeToLive(), attrs.getCustomEntryTimeToLive());
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasEntryIdleTimeout() || ((RegionAttributesCreation) attrs).hasCustomEntryIdleTimeout())) {
if (generateDefaults() || !attrs.getEntryIdleTimeout().equals(ExpirationAttributes.DEFAULT) || attrs.getCustomEntryIdleTimeout() != null)
generate(ENTRY_IDLE_TIME, attrs.getEntryIdleTimeout(), attrs.getCustomEntryIdleTimeout());
}
if (attrs.getDiskStoreName() == null && (generateDefaults() || this.version.compareTo(CacheXmlVersion.GEMFIRE_6_5) < 0)) {
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasDiskWriteAttributes())) {
generate(attrs.getDiskWriteAttributes());
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasDiskDirs())) {
File[] diskDirs = attrs.getDiskDirs();
int[] diskSizes = attrs.getDiskDirSizes();
if (diskDirs != null && diskDirs.length > 0) {
handler.startElement("", DISK_DIRS, DISK_DIRS, EMPTY);
for (int i = 0; i < diskDirs.length; i++) {
AttributesImpl diskAtts = new AttributesImpl();
if (diskSizes[i] != DiskStoreFactory.DEFAULT_DISK_DIR_SIZE) {
diskAtts.addAttribute("", "", DIR_SIZE, "", String.valueOf(diskSizes[i]));
}
handler.startElement("", DISK_DIR, DISK_DIR, diskAtts);
File dir = diskDirs[i];
String name = generateDefaults() ? dir.getAbsolutePath() : dir.getPath();
handler.characters(name.toCharArray(), 0, name.length());
handler.endElement("", DISK_DIR, DISK_DIR);
}
handler.endElement("", DISK_DIRS, DISK_DIRS);
}
}
}
if (this.version.compareTo(CacheXmlVersion.GEMFIRE_5_0) >= 0) {
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasPartitionAttributes())) {
PartitionAttributes p = attrs.getPartitionAttributes();
if (p != null) {
generate(p);
}
}
}
if (this.version.compareTo(CacheXmlVersion.GEMFIRE_5_0) >= 0) {
MembershipAttributes p = attrs.getMembershipAttributes();
if (p != null && p.hasRequiredRoles()) {
generate(p);
}
}
if (this.version.compareTo(CacheXmlVersion.GEMFIRE_5_0) >= 0) {
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasSubscriptionAttributes())) {
SubscriptionAttributes sa = attrs.getSubscriptionAttributes();
if (sa != null) {
if (generateDefaults() || !sa.equals(new SubscriptionAttributes()))
generate(sa);
}
}
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasCacheLoader())) {
generate(CACHE_LOADER, attrs.getCacheLoader());
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasCacheWriter())) {
generate(CACHE_WRITER, attrs.getCacheWriter());
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasCacheListeners())) {
CacheListener[] listeners = attrs.getCacheListeners();
for (int i = 0; i < listeners.length; i++) {
generate(CACHE_LISTENER, listeners[i]);
}
}
if (this.version.compareTo(CacheXmlVersion.GEMFIRE_8_0) >= 0) {
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasCompressor())) {
generate(COMPRESSOR, attrs.getCompressor());
}
}
if ((!(attrs instanceof RegionAttributesCreation) || ((RegionAttributesCreation) attrs).hasEvictionAttributes())) {
generate(attrs.getEvictionAttributes());
}
handler.endElement("", REGION_ATTRIBUTES, REGION_ATTRIBUTES);
}
use of org.apache.geode.cache.DataPolicy in project geode by apache.
the class RegionCreateFunction method createRegion.
public static <K, V> Region<?, ?> createRegion(Cache cache, RegionFunctionArgs regionCreateArgs) {
Region<K, V> createdRegion = null;
final String regionPath = regionCreateArgs.getRegionPath();
final RegionShortcut regionShortcut = regionCreateArgs.getRegionShortcut();
final String useAttributesFrom = regionCreateArgs.getUseAttributesFrom();
// If a region path indicates a sub-region, check whether the parent region exists
RegionPath regionPathData = new RegionPath(regionPath);
String parentRegionPath = regionPathData.getParent();
Region<?, ?> parentRegion = null;
if (parentRegionPath != null && !Region.SEPARATOR.equals(parentRegionPath)) {
parentRegion = cache.getRegion(parentRegionPath);
if (parentRegion == null) {
throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__PARENT_REGION_FOR_0_DOESNOT_EXIST, new Object[] { regionPath }));
}
if (parentRegion.getAttributes().getPartitionAttributes() != null) {
// For a PR, sub-regions are not supported.
throw new CreateSubregionException(CliStrings.format(CliStrings.CREATE_REGION__MSG__0_IS_A_PR_CANNOT_HAVE_SUBREGIONS, parentRegion.getFullPath()));
}
}
// One of Region Shortcut OR Use Attributes From has to be given
if (regionShortcut == null && useAttributesFrom == null) {
throw new IllegalArgumentException(CliStrings.CREATE_REGION__MSG__ONE_OF_REGIONSHORTCUT_AND_USEATTRIBUESFROM_IS_REQUIRED);
}
boolean isPartitioned = false;
RegionFactory<K, V> factory = null;
RegionAttributes<K, V> regionAttributes = null;
if (regionShortcut != null) {
regionAttributes = cache.getRegionAttributes(regionShortcut.toString());
if (logger.isDebugEnabled()) {
logger.debug("Using shortcut {} for {} region attributes : {}", regionShortcut, regionPath, regionAttributes);
}
if (regionAttributes == null) {
if (logger.isDebugEnabled()) {
logger.debug("Shortcut {} doesn't have attributes in {}", regionShortcut, cache.listRegionAttributes());
}
throw new IllegalStateException(CliStrings.format(CliStrings.CREATE_REGION__MSG__COULDNOT_LOAD_REGION_ATTRIBUTES_FOR_SHORTCUT_0, regionShortcut));
}
} else {
if (logger.isDebugEnabled()) {
logger.debug("Using Manager's region attributes for {}", regionPath);
}
regionAttributes = regionCreateArgs.getRegionAttributes();
if (logger.isDebugEnabled()) {
logger.debug("Using Attributes : {}", regionAttributes);
}
}
isPartitioned = regionAttributes.getPartitionAttributes() != null;
factory = cache.createRegionFactory(regionAttributes);
if (!isPartitioned && regionCreateArgs.hasPartitionAttributes()) {
throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__OPTION_0_CAN_BE_USED_ONLY_FOR_PARTITIONEDREGION, regionCreateArgs.getPartitionArgs().getUserSpecifiedPartitionAttributes()));
}
if (isPartitioned) {
PartitionAttributes<K, V> partitionAttributes = extractPartitionAttributes(cache, regionAttributes, regionCreateArgs);
DataPolicy originalDataPolicy = regionAttributes.getDataPolicy();
factory.setPartitionAttributes(partitionAttributes);
// We have to do this because AttributesFactory.setPartitionAttributes()
// checks RegionAttributes.hasDataPolicy() which is set only when the data
// policy is set explicitly
factory.setDataPolicy(originalDataPolicy);
}
// Set Constraints
final String keyConstraint = regionCreateArgs.getKeyConstraint();
final String valueConstraint = regionCreateArgs.getValueConstraint();
if (keyConstraint != null && !keyConstraint.isEmpty()) {
Class<K> keyConstraintClass = CliUtil.forName(keyConstraint, CliStrings.CREATE_REGION__KEYCONSTRAINT);
factory.setKeyConstraint(keyConstraintClass);
}
if (valueConstraint != null && !valueConstraint.isEmpty()) {
Class<V> valueConstraintClass = CliUtil.forName(valueConstraint, CliStrings.CREATE_REGION__VALUECONSTRAINT);
factory.setValueConstraint(valueConstraintClass);
}
// Expiration attributes
final RegionFunctionArgs.ExpirationAttrs entryExpirationIdleTime = regionCreateArgs.getEntryExpirationIdleTime();
if (entryExpirationIdleTime != null) {
factory.setEntryIdleTimeout(entryExpirationIdleTime.convertToExpirationAttributes());
}
final RegionFunctionArgs.ExpirationAttrs entryExpirationTTL = regionCreateArgs.getEntryExpirationTTL();
if (entryExpirationTTL != null) {
factory.setEntryTimeToLive(entryExpirationTTL.convertToExpirationAttributes());
}
final RegionFunctionArgs.ExpirationAttrs regionExpirationIdleTime = regionCreateArgs.getRegionExpirationIdleTime();
if (regionExpirationIdleTime != null) {
factory.setEntryIdleTimeout(regionExpirationIdleTime.convertToExpirationAttributes());
}
final RegionFunctionArgs.ExpirationAttrs regionExpirationTTL = regionCreateArgs.getRegionExpirationTTL();
if (regionExpirationTTL != null) {
factory.setEntryTimeToLive(regionExpirationTTL.convertToExpirationAttributes());
}
// Associate a Disk Store
final String diskStore = regionCreateArgs.getDiskStore();
if (diskStore != null && !diskStore.isEmpty()) {
factory.setDiskStoreName(diskStore);
}
if (regionCreateArgs.isSetDiskSynchronous()) {
factory.setDiskSynchronous(regionCreateArgs.isDiskSynchronous());
}
if (regionCreateArgs.isSetOffHeap()) {
factory.setOffHeap(regionCreateArgs.isOffHeap());
}
// Set stats enabled
if (regionCreateArgs.isSetStatisticsEnabled()) {
factory.setStatisticsEnabled(regionCreateArgs.isStatisticsEnabled());
}
// Set conflation
if (regionCreateArgs.isSetEnableAsyncConflation()) {
factory.setEnableAsyncConflation(regionCreateArgs.isEnableAsyncConflation());
}
if (regionCreateArgs.isSetEnableSubscriptionConflation()) {
factory.setEnableSubscriptionConflation(regionCreateArgs.isEnableSubscriptionConflation());
}
// Gateway Sender Ids
final Set<String> gatewaySenderIds = regionCreateArgs.getGatewaySenderIds();
if (gatewaySenderIds != null && !gatewaySenderIds.isEmpty()) {
for (String gatewaySenderId : gatewaySenderIds) {
factory.addGatewaySenderId(gatewaySenderId);
}
}
// Async Queue Ids
final Set<String> asyncEventQueueIds = regionCreateArgs.getAsyncEventQueueIds();
if (asyncEventQueueIds != null && !asyncEventQueueIds.isEmpty()) {
for (String asyncEventQueueId : asyncEventQueueIds) {
factory.addAsyncEventQueueId(asyncEventQueueId);
}
}
// concurrency check enabled & concurrency level
if (regionCreateArgs.isSetConcurrencyChecksEnabled()) {
factory.setConcurrencyChecksEnabled(regionCreateArgs.isConcurrencyChecksEnabled());
}
if (regionCreateArgs.isSetConcurrencyLevel()) {
factory.setConcurrencyLevel(regionCreateArgs.getConcurrencyLevel());
}
// cloning enabled for delta
if (regionCreateArgs.isSetCloningEnabled()) {
factory.setCloningEnabled(regionCreateArgs.isCloningEnabled());
}
// multicast enabled for replication
if (regionCreateArgs.isSetMcastEnabled()) {
factory.setMulticastEnabled(regionCreateArgs.isMcastEnabled());
}
// Set plugins
final Set<String> cacheListeners = regionCreateArgs.getCacheListeners();
if (cacheListeners != null && !cacheListeners.isEmpty()) {
for (String cacheListener : cacheListeners) {
Class<CacheListener<K, V>> cacheListenerKlass = CliUtil.forName(cacheListener, CliStrings.CREATE_REGION__CACHELISTENER);
factory.addCacheListener(CliUtil.newInstance(cacheListenerKlass, CliStrings.CREATE_REGION__CACHELISTENER));
}
}
// Compression provider
if (regionCreateArgs.isSetCompressor()) {
Class<Compressor> compressorKlass = CliUtil.forName(regionCreateArgs.getCompressor(), CliStrings.CREATE_REGION__COMPRESSOR);
factory.setCompressor(CliUtil.newInstance(compressorKlass, CliStrings.CREATE_REGION__COMPRESSOR));
}
final String cacheLoader = regionCreateArgs.getCacheLoader();
if (cacheLoader != null) {
Class<CacheLoader<K, V>> cacheLoaderKlass = CliUtil.forName(cacheLoader, CliStrings.CREATE_REGION__CACHELOADER);
factory.setCacheLoader(CliUtil.newInstance(cacheLoaderKlass, CliStrings.CREATE_REGION__CACHELOADER));
}
final String cacheWriter = regionCreateArgs.getCacheWriter();
if (cacheWriter != null) {
Class<CacheWriter<K, V>> cacheWriterKlass = CliUtil.forName(cacheWriter, CliStrings.CREATE_REGION__CACHEWRITER);
factory.setCacheWriter(CliUtil.newInstance(cacheWriterKlass, CliStrings.CREATE_REGION__CACHEWRITER));
}
String regionName = regionPathData.getName();
if (parentRegion != null) {
createdRegion = factory.createSubregion(parentRegion, regionName);
} else {
createdRegion = factory.create(regionName);
}
return createdRegion;
}
use of org.apache.geode.cache.DataPolicy in project geode by apache.
the class Fakes method region.
/**
* A fake region, which contains a fake cache and some other fake attributes
*/
public static Region region(String name, Cache cache) {
Region region = mock(Region.class);
RegionAttributes attributes = mock(RegionAttributes.class);
DataPolicy policy = mock(DataPolicy.class);
when(region.getAttributes()).thenReturn(attributes);
when(attributes.getDataPolicy()).thenReturn(policy);
when(region.getCache()).thenReturn(cache);
when(region.getRegionService()).thenReturn(cache);
return region;
}
Aggregations