Search in sources :

Example 91 with Cache

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 })));
}
Also used : XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) Extensible(org.apache.geode.internal.cache.extension.Extensible) FunctionException(org.apache.geode.cache.execute.FunctionException) Cache(org.apache.geode.cache.Cache)

Example 92 with Cache

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 })));
}
Also used : Extensible(org.apache.geode.internal.cache.extension.Extensible) FunctionException(org.apache.geode.cache.execute.FunctionException) XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache)

Example 93 with Cache

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Cache(org.apache.geode.cache.Cache) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 94 with Cache

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);
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) ForceReattemptException(org.apache.geode.internal.cache.ForceReattemptException) LogWriter(org.apache.geode.LogWriter) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) CancelException(org.apache.geode.CancelException) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 95 with Cache

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;
}
Also used : DistributedSystem(org.apache.geode.distributed.DistributedSystem) Cache(org.apache.geode.cache.Cache)

Aggregations

Cache (org.apache.geode.cache.Cache)1044 Region (org.apache.geode.cache.Region)478 Test (org.junit.Test)476 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)292 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)277 VM (org.apache.geode.test.dunit.VM)264 Host (org.apache.geode.test.dunit.Host)230 AttributesFactory (org.apache.geode.cache.AttributesFactory)229 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)177 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)176 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)164 LocalRegion (org.apache.geode.internal.cache.LocalRegion)153 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)123 ClientCache (org.apache.geode.cache.client.ClientCache)117 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)112 Properties (java.util.Properties)101 CacheException (org.apache.geode.cache.CacheException)101 RegionAttributes (org.apache.geode.cache.RegionAttributes)99 QueryService (org.apache.geode.cache.query.QueryService)95 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)93