use of org.apache.geode.cache.Cache in project geode by apache.
the class DestroyMockCacheExtensionFunction method execute.
@Override
public void execute(FunctionContext context) {
final Cache cache = CacheFactory.getAnyInstance();
if (!(cache instanceof Extensible)) {
throw new FunctionException("Not extensible cache.");
}
@SuppressWarnings("unchecked") final Extensible<Cache> extensible = (Extensible<Cache>) cache;
for (Extension<Cache> extension : extensible.getExtensionPoint().getExtensions()) {
if (extension instanceof MockCacheExtension) {
extensible.getExtensionPoint().removeExtension(extension);
break;
}
}
final XmlEntity xmlEntity = XmlEntity.builder().withType(ELEMENT_CACHE).withNamespace(PREFIX, NAMESPACE).build();
final ResultSender<Object> resultSender = context.getResultSender();
final String memberNameOrId = CliUtil.getMemberNameOrId(cache.getDistributedSystem().getDistributedMember());
resultSender.lastResult(new CliFunctionResult(memberNameOrId, xmlEntity, CliStrings.format("Mock cache extension destroyed on \"{0}\"", new Object[] { memberNameOrId })));
}
use of org.apache.geode.cache.Cache in project geode by apache.
the class DestroyMockRegionExtensionFunction method execute.
@Override
public void execute(FunctionContext context) {
final Cache cache = CacheFactory.getAnyInstance();
final Region<?, ?> region = cache.getRegion((String) ((Object[]) context.getArguments())[0]);
if (!(region instanceof Extensible)) {
throw new FunctionException("Not extensible region.");
}
@SuppressWarnings("unchecked") final Extensible<Region<?, ?>> extensible = (Extensible<Region<?, ?>>) region;
for (Extension<Region<?, ?>> extension : extensible.getExtensionPoint().getExtensions()) {
if (extension instanceof MockRegionExtension) {
extensible.getExtensionPoint().removeExtension(extension);
break;
}
}
XmlEntity xmlEntity = new XmlEntity(CacheXml.REGION, "name", region.getName());
final ResultSender<Object> resultSender = context.getResultSender();
final String memberNameOrId = CliUtil.getMemberNameOrId(cache.getDistributedSystem().getDistributedMember());
resultSender.lastResult(new CliFunctionResult(memberNameOrId, xmlEntity, CliStrings.format("Mock region extension \"{0}\" destroyed on \"{1}\"", new Object[] { region.getFullPath(), memberNameOrId })));
}
use of org.apache.geode.cache.Cache in project geode by apache.
the class SimpleExtensionPointJUnitTest method testBeforeCreate.
/**
* Test method for {@link SimpleExtensionPoint#beforeCreate(Cache)} .
*/
@Test
public void testBeforeCreate() {
final MockImpl m = new MockImpl();
final Cache c = Fakes.cache();
final AtomicInteger counter = new AtomicInteger(0);
final MockExtension extension = new MockExtension() {
@Override
public void beforeCreate(Extensible<MockInterface> source, Cache cache) {
counter.incrementAndGet();
}
};
counter.set(0);
m.getExtensionPoint().addExtension(extension);
// Verify beforeCreate is invoked when the extension is added
m.extensionPoint.beforeCreate(c);
assertEquals(1, counter.get());
counter.set(0);
m.getExtensionPoint().removeExtension(extension);
// Verify beforeCreate is not invoked when the extension is removed
m.extensionPoint.beforeCreate(c);
assertEquals(0, counter.get());
}
use of org.apache.geode.cache.Cache in project geode by apache.
the class Bug39356DUnitTest method testCrashWhileCreatingABucket.
/**
* This tests the case where the VM forcing other VMs to create a bucket crashes while creating
* the bucket.
*/
@Test
public void testCrashWhileCreatingABucket() {
Host host = Host.getHost(0);
final VM vm0 = host.getVM(0);
final VM vm1 = host.getVM(1);
final VM vm2 = host.getVM(2);
SerializableRunnable createParReg = new SerializableRunnable("Create parReg") {
public void run() {
DistributionMessageObserver.setInstance(new MyRegionObserver(vm0));
Cache cache = getCache();
AttributesFactory af = new AttributesFactory();
PartitionAttributesFactory pf = new PartitionAttributesFactory();
pf.setRedundantCopies(1);
pf.setRecoveryDelay(0);
af.setDataPolicy(DataPolicy.PARTITION);
af.setPartitionAttributes(pf.create());
cache.createRegion(REGION_NAME, af.create());
}
};
vm1.invoke(createParReg);
vm2.invoke(createParReg);
SerializableRunnable createParRegAccessor = new SerializableRunnable("Create parReg") {
public void run() {
Cache cache = getCache();
AttributesFactory af = new AttributesFactory();
PartitionAttributesFactory pf = new PartitionAttributesFactory();
pf.setRedundantCopies(1);
pf.setLocalMaxMemory(0);
af.setDataPolicy(DataPolicy.PARTITION);
af.setPartitionAttributes(pf.create());
Region r = cache.createRegion(REGION_NAME, af.create());
// trigger the creation of a bucket, which should trigger the destruction of this VM.
try {
r.put("ping", "pong");
fail("Should have gotten a CancelException");
} catch (CancelException e) {
// this is ok, we expect our observer to close this cache.
}
}
};
vm0.invoke(createParRegAccessor);
SerializableRunnable verifyBuckets = new SerializableRunnable("Verify buckets") {
public void run() {
LogWriter log = org.apache.geode.test.dunit.LogWriterUtils.getLogWriter();
Cache cache = getCache();
PartitionedRegion r = (PartitionedRegion) cache.getRegion(REGION_NAME);
for (int i = 0; i < r.getAttributes().getPartitionAttributes().getTotalNumBuckets(); i++) {
List owners = null;
while (owners == null) {
try {
owners = r.getBucketOwnersForValidation(i);
} catch (ForceReattemptException e) {
log.info(Bug39356DUnitTest.class + " verify buckets Caught a ForceReattemptException");
Wait.pause(1000);
}
}
if (owners.isEmpty()) {
log.info("skipping bucket " + i + " because it has no data");
continue;
}
assertEquals("Expecting bucket " + i + " to have two copies", 2, owners.size());
log.info("bucket " + i + " had two copies");
}
}
};
vm1.invoke(verifyBuckets);
vm2.invoke(verifyBuckets);
}
use of org.apache.geode.cache.Cache in project geode by apache.
the class StatsBugDUnitTest method createCache.
/**
* Create the cache
*
* @param props - properties for DS
* @return the cache instance
* @throws Exception - thrown if any problem occurs in cache creation
*/
private Cache createCache(Properties props) throws Exception {
DistributedSystem ds = getSystem(props);
ds.disconnect();
ds = getSystem(props);
Cache cache = null;
cache = CacheFactory.create(ds);
if (cache == null) {
throw new Exception("CacheFactory.create() returned null ");
}
return cache;
}
Aggregations