Search in sources :

Example 86 with Cache

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

the class OnGroupsFunctionExecutionDUnitTest method testOnServer.

@Test
public void testOnServer() {
    Host host = Host.getHost(0);
    VM server0 = host.getVM(0);
    VM server1 = host.getVM(1);
    VM server2 = host.getVM(2);
    VM client = host.getVM(3);
    VM locator = Host.getLocator();
    final String regionName = getName();
    initVM(server0, "mg,g0", regionName, true);
    initVM(server1, "g1", regionName, true);
    initVM(server2, "g0,g1,g2", regionName, true);
    final int locatorPort = getLocatorPort(locator);
    final String hostName = host.getHostName();
    client.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            try {
                Cache c = CacheFactory.getAnyInstance();
                c.close();
            } catch (CacheClosedException cce) {
            }
            disconnectFromDS();
            LogWriterUtils.getLogWriter().fine("SWAP:creating client cache");
            ClientCacheFactory ccf = new ClientCacheFactory();
            ccf.addPoolLocator(hostName, locatorPort);
            ccf.setPoolServerGroup("mg");
            ccf.set(LOG_LEVEL, LogWriterUtils.getDUnitLogLevel());
            ClientCache c = ccf.create();
            IgnoredException ex = IgnoredException.addIgnoredException("No member found");
            try {
                InternalFunctionService.onServer(c, "no such group").execute(new OnGroupsFunction()).getResult();
                fail("expected exception not thrown");
            } catch (FunctionException e) {
            } finally {
                ex.remove();
            }
            InternalFunctionService.onServer(c, "g1").execute(new OnGroupsFunction()).getResult();
            return null;
        }
    });
    int c0 = getAndResetInvocationCount(server0);
    int c1 = getAndResetInvocationCount(server1);
    int c2 = getAndResetInvocationCount(server2);
    assertEquals(1, c0 + c1 + c2);
    client.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            ClientCache c = ClientCacheFactory.getAnyInstance();
            InternalFunctionService.onServer(c, "g0").execute(new OnGroupsFunction()).getResult();
            return null;
        }
    });
    verifyAndResetInvocationCount(server0, 1);
    verifyAndResetInvocationCount(server1, 0);
    verifyAndResetInvocationCount(server2, 0);
    client.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            ClientCache c = ClientCacheFactory.getAnyInstance();
            InternalFunctionService.onServer(c, "mg", "g1").execute(new OnGroupsFunction()).getResult();
            return null;
        }
    });
    c0 = getAndResetInvocationCount(server0);
    c1 = getAndResetInvocationCount(server1);
    c2 = getAndResetInvocationCount(server2);
    assertEquals(2, c0 + c1 + c2);
}
Also used : FunctionException(org.apache.geode.cache.execute.FunctionException) Host(org.apache.geode.test.dunit.Host) CacheClosedException(org.apache.geode.cache.CacheClosedException) ClientCache(org.apache.geode.cache.client.ClientCache) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheClosedException(org.apache.geode.cache.CacheClosedException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) IgnoredException(org.apache.geode.test.dunit.IgnoredException) Cache(org.apache.geode.cache.Cache) ClientCache(org.apache.geode.cache.client.ClientCache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 87 with Cache

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

the class OnGroupsFunctionExecutionDUnitTest method dotestBasicClientServerFunction.

private void dotestBasicClientServerFunction(final boolean register, final boolean withArgs) {
    Host host = Host.getHost(0);
    VM server0 = host.getVM(0);
    VM server1 = host.getVM(1);
    VM server2 = host.getVM(2);
    VM client = host.getVM(3);
    VM locator = Host.getLocator();
    final String regionName = getName();
    initVM(server0, "mg,g0", regionName, true);
    initVM(server1, "g1", regionName, true);
    initVM(server2, "g0,g1", regionName, true);
    if (register) {
        registerFunction(server0);
        registerFunction(server1);
        registerFunction(server2);
    }
    final int locatorPort = getLocatorPort(locator);
    final String hostName = host.getHostName();
    client.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            try {
                Cache c = CacheFactory.getAnyInstance();
                c.close();
            } catch (CacheClosedException cce) {
            }
            disconnectFromDS();
            LogWriterUtils.getLogWriter().fine("SWAP:creating client cache");
            ClientCacheFactory ccf = new ClientCacheFactory();
            ccf.addPoolLocator(hostName, locatorPort);
            ccf.setPoolServerGroup("mg");
            ccf.set(LOG_LEVEL, LogWriterUtils.getDUnitLogLevel());
            ClientCache c = ccf.create();
            c.getLogger().info("SWAP:invoking function from client on g0");
            Execution e = InternalFunctionService.onServers(c, "g0");
            if (withArgs) {
                ArrayList<String> args = new ArrayList<String>();
                args.add("g0");
                e = e.setArguments(args);
            }
            if (register) {
                e.execute(OnGroupsFunction.Id).getResult();
            } else {
                e.execute(new OnGroupsFunction()).getResult();
            }
            return null;
        }
    });
    verifyAndResetInvocationCount(server0, 1);
    verifyAndResetInvocationCount(server1, 0);
    verifyAndResetInvocationCount(server2, 1);
    client.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            ClientCache c = ClientCacheFactory.getAnyInstance();
            c.getLogger().fine("SWAP:invoking function from client on mg");
            Execution e = InternalFunctionService.onServers(c, "mg");
            if (withArgs) {
                ArrayList<String> args = new ArrayList<String>();
                args.add("mg");
                e = e.setArguments(args);
            }
            if (register) {
                e.execute(OnGroupsFunction.Id).getResult();
            } else {
                e.execute(new OnGroupsFunction()).getResult();
            }
            return null;
        }
    });
    verifyAndResetInvocationCount(server0, 1);
    verifyAndResetInvocationCount(server1, 0);
    verifyAndResetInvocationCount(server2, 0);
    client.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            ClientCache c = ClientCacheFactory.getAnyInstance();
            c.getLogger().fine("SWAP:invoking function from client on g0 g1");
            Execution e = InternalFunctionService.onServers(c, "g0", "g1");
            if (withArgs) {
                ArrayList<String> args = new ArrayList<String>();
                args.add("g0");
                args.add("g1");
                e = e.setArguments(args);
            }
            if (register) {
                e.execute(OnGroupsFunction.Id).getResult();
            } else {
                e.execute(new OnGroupsFunction()).getResult();
            }
            return null;
        }
    });
    verifyAndResetInvocationCount(server0, 1);
    verifyAndResetInvocationCount(server1, 1);
    verifyAndResetInvocationCount(server2, 1);
}
Also used : ArrayList(java.util.ArrayList) Host(org.apache.geode.test.dunit.Host) CacheClosedException(org.apache.geode.cache.CacheClosedException) ClientCache(org.apache.geode.cache.client.ClientCache) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheClosedException(org.apache.geode.cache.CacheClosedException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) Execution(org.apache.geode.cache.execute.Execution) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Cache(org.apache.geode.cache.Cache) ClientCache(org.apache.geode.cache.client.ClientCache)

Example 88 with Cache

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

the class OnGroupsFunctionExecutionDUnitTest method testClientServerOneMemberFailure.

@Test
public void testClientServerOneMemberFailure() {
    Host host = Host.getHost(0);
    VM server0 = host.getVM(0);
    VM server1 = host.getVM(1);
    VM server2 = host.getVM(2);
    VM client = host.getVM(3);
    VM locator = Host.getLocator();
    final String regionName = getName();
    initVM(server0, "mg,g0", regionName, true);
    initVM(server1, "g1", regionName, true);
    initVM(server2, "g0,g1,g2", regionName, true);
    final int locatorPort = getLocatorPort(locator);
    final String hostName = host.getHostName();
    client.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            try {
                Cache c = CacheFactory.getAnyInstance();
                c.close();
            } catch (CacheClosedException cce) {
            }
            disconnectFromDS();
            LogWriterUtils.getLogWriter().fine("SWAP:creating client cache");
            ClientCacheFactory ccf = new ClientCacheFactory();
            ccf.addPoolLocator(hostName, locatorPort);
            ccf.setPoolServerGroup("mg");
            ccf.set(LOG_LEVEL, LogWriterUtils.getDUnitLogLevel());
            ClientCache c = ccf.create();
            Execution e = InternalFunctionService.onServers(c, "g1");
            ArrayList<String> args = new ArrayList<String>();
            args.add("disconnect");
            args.add("g2");
            e = e.setArguments(args);
            IgnoredException.addIgnoredException("FunctionInvocationTargetException");
            try {
                e.execute(new OnGroupsExceptionFunction()).getResult();
                fail("expected exception not thrown");
            } catch (FunctionException ex) {
                assertTrue(ex.getCause() instanceof FunctionInvocationTargetException);
            }
            return null;
        }
    });
}
Also used : ArrayList(java.util.ArrayList) FunctionException(org.apache.geode.cache.execute.FunctionException) Host(org.apache.geode.test.dunit.Host) CacheClosedException(org.apache.geode.cache.CacheClosedException) ClientCache(org.apache.geode.cache.client.ClientCache) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheClosedException(org.apache.geode.cache.CacheClosedException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) Execution(org.apache.geode.cache.execute.Execution) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) Cache(org.apache.geode.cache.Cache) ClientCache(org.apache.geode.cache.client.ClientCache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 89 with Cache

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

the class CreateMockCacheExtensionFunction method execute.

@Override
public void execute(FunctionContext context) {
    final Cache cache = CacheFactory.getAnyInstance();
    if (!(cache instanceof Extensible)) {
        throw new FunctionException("Not extensible cache.");
    }
    final String value = (String) ((Object[]) context.getArguments())[0];
    @SuppressWarnings("unchecked") final Extensible<Cache> extensible = (Extensible<Cache>) cache;
    final MockCacheExtension extension = new MockCacheExtension(value);
    extension.beforeCreate(extensible, cache);
    extension.onCreate(extensible, extensible);
    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 created 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 90 with Cache

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

the class CreateMockRegionExtensionFunction 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.");
    }
    final String value = (String) ((Object[]) context.getArguments())[1];
    @SuppressWarnings("unchecked") final Extensible<Region<?, ?>> extensible = (Extensible<Region<?, ?>>) region;
    final MockRegionExtension extension = new MockRegionExtension(value);
    extension.beforeCreate(extensible, cache);
    extension.onCreate(extensible, extensible);
    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}\" created 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)

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