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);
}
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);
}
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;
}
});
}
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 })));
}
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 })));
}
Aggregations