use of org.apache.geode.internal.cache.tier.sockets.CacheClientNotifier in project geode by apache.
the class Simple2CacheServerDUnitTest method checkProxyIsPrimary.
private boolean checkProxyIsPrimary(VM vm) {
SerializableCallable checkProxyIsPrimary = new SerializableCallable() {
@Override
public Object call() throws Exception {
final CacheClientNotifier ccn = CacheClientNotifier.getInstance();
Awaitility.waitAtMost(20, TimeUnit.SECONDS).until(() -> {
return (ccn.getClientProxies().size() == 1);
});
Iterator iter_prox = ccn.getClientProxies().iterator();
CacheClientProxy proxy = (CacheClientProxy) iter_prox.next();
return proxy.isPrimary();
}
};
return (Boolean) vm.invoke(checkProxyIsPrimary);
}
use of org.apache.geode.internal.cache.tier.sockets.CacheClientNotifier in project geode by apache.
the class RequestEventValue method cmdExecute.
public void cmdExecute(Message clientMessage, ServerConnection serverConnection, long start) throws IOException {
Part eventIDPart = null, valuePart = null;
EventID event = null;
Object callbackArg = null;
CachedRegionHelper crHelper = serverConnection.getCachedRegionHelper();
StringBuffer errMessage = new StringBuffer();
serverConnection.setAsTrue(REQUIRES_RESPONSE);
// Retrieve the data from the message parts
int parts = clientMessage.getNumberOfParts();
eventIDPart = clientMessage.getPart(0);
if (eventIDPart == null) {
logger.warn(LocalizedMessage.create(LocalizedStrings.RequestEventValue_0_THE_EVENT_ID_FOR_THE_GET_EVENT_VALUE_REQUEST_IS_NULL, serverConnection.getName()));
errMessage.append(" The event id for the get event value request is null.");
writeErrorResponse(clientMessage, MessageType.REQUESTDATAERROR, errMessage.toString(), serverConnection);
serverConnection.setAsTrue(RESPONDED);
} else {
try {
event = (EventID) eventIDPart.getObject();
} catch (Exception e) {
writeException(clientMessage, e, false, serverConnection);
serverConnection.setAsTrue(RESPONDED);
return;
}
if (parts > 1) {
valuePart = clientMessage.getPart(1);
try {
if (valuePart != null) {
callbackArg = valuePart.getObject();
}
} catch (Exception e) {
writeException(clientMessage, e, false, serverConnection);
serverConnection.setAsTrue(RESPONDED);
return;
}
}
if (logger.isTraceEnabled()) {
logger.trace("{}: Received get event value request ({} bytes) from {}", serverConnection.getName(), clientMessage.getPayloadLength(), serverConnection.getSocketString());
}
CacheClientNotifier ccn = serverConnection.getAcceptor().getCacheClientNotifier();
// Get the ha container.
HAContainerWrapper haContainer = (HAContainerWrapper) ccn.getHaContainer();
if (haContainer == null) {
String reason = " was not found during get event value request";
writeRegionDestroyedEx(clientMessage, "ha container", reason, serverConnection);
serverConnection.setAsTrue(RESPONDED);
} else {
Object[] valueAndIsObject = new Object[2];
try {
Object data = haContainer.get(new HAEventWrapper(event));
if (data == null) {
logger.warn(LocalizedMessage.create(LocalizedStrings.RequestEventValue_UNABLE_TO_FIND_A_CLIENT_UPDATE_MESSAGE_FOR_0, event));
String msgStr = "No value found for " + event + " in " + haContainer.getName();
writeErrorResponse(clientMessage, MessageType.REQUEST_EVENT_VALUE_ERROR, msgStr, serverConnection);
serverConnection.setAsTrue(RESPONDED);
return;
} else {
if (logger.isDebugEnabled()) {
logger.debug("Value retrieved for event {}", event);
}
Object val = ((ClientUpdateMessageImpl) data).getValueToConflate();
if (!(val instanceof byte[])) {
if (val instanceof CachedDeserializable) {
val = ((CachedDeserializable) val).getSerializedValue();
} else {
val = CacheServerHelper.serialize(val);
}
((ClientUpdateMessageImpl) data).setLatestValue(val);
}
valueAndIsObject[0] = val;
valueAndIsObject[1] = Boolean.valueOf(((ClientUpdateMessageImpl) data).valueIsObject());
}
} catch (Exception e) {
writeException(clientMessage, e, false, serverConnection);
serverConnection.setAsTrue(RESPONDED);
return;
}
Object data = valueAndIsObject[0];
boolean isObject = (Boolean) valueAndIsObject[1];
writeResponse(data, callbackArg, clientMessage, isObject, serverConnection);
serverConnection.setAsTrue(RESPONDED);
ccn.getClientProxy(serverConnection.getProxyID()).getStatistics().incDeltaFullMessagesSent();
if (logger.isDebugEnabled()) {
logger.debug("{}: Wrote get event value response back to {} for ha container {}", serverConnection.getName(), serverConnection.getSocketString(), haContainer.getName());
}
}
}
}
use of org.apache.geode.internal.cache.tier.sockets.CacheClientNotifier in project geode by apache.
the class ConnectionPoolDUnitTest method test028DynamicRegionCreation.
/**
* Test dynamic region creation instantiated from a bridge client causing regions to be created on
* two different bridge servers.
*
* Also tests the reverse situation, a dynamic region is created on the bridge server expecting
* the same region to be created on the client.
*
* Note: This test re-creates Distributed Systems for its own purposes and uses a Loner
* distributed systems to isolate the Bridge Client.
*
* @throws Exception
*/
@Test
public void test028DynamicRegionCreation() throws Exception {
final String name = this.getName();
final Host host = Host.getHost(0);
final VM client1 = host.getVM(0);
// VM client2 = host.getVM(1);
final VM srv1 = host.getVM(2);
final VM srv2 = host.getVM(3);
final String k1 = name + "-key1";
final String v1 = name + "-val1";
final String k2 = name + "-key2";
final String v2 = name + "-val2";
final String k3 = name + "-key3";
final String v3 = name + "-val3";
client1.invoke(() -> disconnectFromDS());
srv1.invoke(() -> disconnectFromDS());
srv2.invoke(() -> disconnectFromDS());
try {
// setup servers
CacheSerializableRunnable ccs = new CacheSerializableRunnable("Create Cache Server") {
public void run2() throws CacheException {
// Creates a new DS and Cache
createDynamicRegionCache(name, (String) null);
assertTrue(DynamicRegionFactory.get().isOpen());
try {
startBridgeServer(0);
} catch (IOException ugh) {
fail("Bridge Server startup failed");
}
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setDataPolicy(DataPolicy.REPLICATE);
factory.setConcurrencyChecksEnabled(false);
Region region = createRootRegion(name, factory.create());
region.put(k1, v1);
Assert.assertTrue(region.get(k1).equals(v1));
}
};
srv1.invoke(ccs);
srv2.invoke(ccs);
final String srv1Host = NetworkUtils.getServerHostName(srv1.getHost());
final int srv1Port = srv1.invoke(() -> ConnectionPoolDUnitTest.getCacheServerPort());
final int srv2Port = srv2.invoke(() -> ConnectionPoolDUnitTest.getCacheServerPort());
// final String srv2Host = getServerHostName(srv2.getHost());
// setup clients, do basic tests to make sure pool with notifier work as advertised
client1.invoke(new CacheSerializableRunnable("Create Cache Client") {
public void run2() throws CacheException {
createLonerDS();
AttributesFactory factory = new AttributesFactory();
factory.setConcurrencyChecksEnabled(false);
Pool cp = ClientServerTestCase.configureConnectionPool(factory, srv1Host, srv1Port, srv2Port, true, -1, -1, null);
{
final PoolImpl pool = (PoolImpl) cp;
WaitCriterion ev = new WaitCriterion() {
public boolean done() {
if (pool.getPrimary() == null) {
return false;
}
if (pool.getRedundants().size() < 1) {
return false;
}
return true;
}
public String description() {
return null;
}
};
Wait.waitForCriterion(ev, 30 * 1000, 200, true);
assertNotNull(pool.getPrimary());
assertTrue("backups=" + pool.getRedundants() + " expected=" + 1, pool.getRedundants().size() >= 1);
}
createDynamicRegionCache(name, "testPool");
assertTrue(DynamicRegionFactory.get().isOpen());
factory.setScope(Scope.LOCAL);
factory.setConcurrencyChecksEnabled(false);
factory.setCacheListener(new CertifiableTestCacheListener(org.apache.geode.test.dunit.LogWriterUtils.getLogWriter()));
Region region = createRootRegion(name, factory.create());
assertNull(region.getEntry(k1));
// this should match
region.registerInterestRegex(".*", InterestResultPolicy.KEYS_VALUES);
// the key
// Update via registered interest
assertEquals(v1, region.getEntry(k1).getValue());
assertNull(region.getEntry(k2));
// use the Pool
region.put(k2, v2);
// Ensure that the notifier didn't un-do
assertEquals(v2, region.getEntry(k2).getValue());
// the put, bug 35355
// setup a key for invalidation from a notifier
region.put(k3, v3);
}
});
srv1.invoke(new CacheSerializableRunnable("Validate Server1 update") {
public void run2() throws CacheException {
CacheClientNotifier ccn = CacheClientNotifier.getInstance();
final CacheClientNotifierStats ccnStats = ccn.getStats();
final int eventCount = ccnStats.getEvents();
Region r = getRootRegion(name);
assertNotNull(r);
// Validate the Pool worked, getEntry works
assertEquals(v2, r.getEntry(k2).getValue());
// because of the mirror
// Make sure we have the other entry to use
assertEquals(v3, r.getEntry(k3).getValue());
// for notification
// Change k3, sending some data to the client notifier
r.put(k3, v1);
// Wait for the update to propagate to the clients
final int maxTime = 20000;
// long start = System.currentTimeMillis();
WaitCriterion ev = new WaitCriterion() {
public boolean done() {
return ccnStats.getEvents() > eventCount;
}
public String description() {
return "waiting for ccnStat";
}
};
Wait.waitForCriterion(ev, maxTime, 200, true);
// Set prox = ccn.getClientProxies();
// assertIndexDetailsEquals(1, prox.size());
// for (Iterator cpi = prox.iterator(); cpi.hasNext(); ) {
// CacheClientProxy ccp = (CacheClientProxy) cpi.next();
// start = System.currentTimeMillis();
// while (ccp.getMessagesProcessed() < 1) {
// assertTrue("Waited more than " + maxTime + "ms for client notification",
// (System.currentTimeMillis() - start) < maxTime);
// try {
// Thread.sleep(100);
// } catch (InterruptedException ine) { fail("Interrupted while waiting for client
// notifier to complete"); }
// }
// }
}
});
srv2.invoke(new CacheSerializableRunnable("Validate Server2 update") {
public void run2() throws CacheException {
Region r = getRootRegion(name);
assertNotNull(r);
// Validate the Pool worked, getEntry works
assertEquals(v2, r.getEntry(k2).getValue());
// because of the mirror
// From peer update
assertEquals(v1, r.getEntry(k3).getValue());
}
});
client1.invoke(new CacheSerializableRunnable("Validate Client notification") {
public void run2() throws CacheException {
Region r = getRootRegion(name);
assertNotNull(r);
CertifiableTestCacheListener ctl = (CertifiableTestCacheListener) r.getAttributes().getCacheListener();
ctl.waitForUpdated(k3);
// Ensure that the notifier updated the entry
assertEquals(v1, r.getEntry(k3).getValue());
}
});
// Ok, now we are ready to do some dynamic region action!
final String v1Dynamic = v1 + "dynamic";
final String dynFromClientName = name + "-dynamic-client";
final String dynFromServerName = name + "-dynamic-server";
client1.invoke(new CacheSerializableRunnable("Client dynamic region creation") {
public void run2() throws CacheException {
assertTrue(DynamicRegionFactory.get().isOpen());
Region r = getRootRegion(name);
assertNotNull(r);
Region dr = DynamicRegionFactory.get().createDynamicRegion(name, dynFromClientName);
// This should be enough to validate the creation on the server
assertNull(dr.get(k1));
dr.put(k1, v1Dynamic);
assertEquals(v1Dynamic, dr.getEntry(k1).getValue());
}
});
// Assert the servers have the dynamic region and the new value
CacheSerializableRunnable valDR = new CacheSerializableRunnable("Validate dynamic region creation on server") {
public void run2() throws CacheException {
Region r = getRootRegion(name);
assertNotNull(r);
long end = System.currentTimeMillis() + 10000;
Region dr = null;
for (; ; ) {
try {
dr = r.getSubregion(dynFromClientName);
assertNotNull(dr);
assertNotNull(getCache().getRegion(name + Region.SEPARATOR + dynFromClientName));
break;
} catch (AssertionError e) {
if (System.currentTimeMillis() > end) {
throw e;
}
}
}
assertEquals(v1Dynamic, dr.getEntry(k1).getValue());
}
};
srv1.invoke(valDR);
srv2.invoke(valDR);
// now delete the dynamic region and see if it goes away on servers
client1.invoke(new CacheSerializableRunnable("Client dynamic region destruction") {
public void run2() throws CacheException {
assertTrue(DynamicRegionFactory.get().isActive());
Region r = getRootRegion(name);
assertNotNull(r);
String drName = r.getFullPath() + Region.SEPARATOR + dynFromClientName;
assertNotNull(getCache().getRegion(drName));
DynamicRegionFactory.get().destroyDynamicRegion(drName);
assertNull(getCache().getRegion(drName));
}
});
// Assert the servers no longer have the dynamic region
CacheSerializableRunnable valNoDR = new CacheSerializableRunnable("Validate dynamic region destruction on server") {
public void run2() throws CacheException {
Region r = getRootRegion(name);
assertNotNull(r);
String drName = r.getFullPath() + Region.SEPARATOR + dynFromClientName;
assertNull(getCache().getRegion(drName));
try {
DynamicRegionFactory.get().destroyDynamicRegion(drName);
fail("expected RegionDestroyedException");
} catch (RegionDestroyedException expected) {
}
}
};
srv1.invoke(valNoDR);
srv2.invoke(valNoDR);
// Now try the reverse, create a dynamic region on the server and see if the client
// has it
srv2.invoke(new CacheSerializableRunnable("Server dynamic region creation") {
public void run2() throws CacheException {
Region r = getRootRegion(name);
assertNotNull(r);
Region dr = DynamicRegionFactory.get().createDynamicRegion(name, dynFromServerName);
assertNull(dr.get(k1));
dr.put(k1, v1Dynamic);
assertEquals(v1Dynamic, dr.getEntry(k1).getValue());
}
});
// Assert the servers have the dynamic region and the new value
srv1.invoke(new CacheSerializableRunnable("Validate dynamic region creation propagation to other server") {
public void run2() throws CacheException {
Region r = getRootRegion(name);
assertNotNull(r);
Region dr = waitForSubRegion(r, dynFromServerName);
assertNotNull(dr);
assertNotNull(getCache().getRegion(name + Region.SEPARATOR + dynFromServerName));
waitForEntry(dr, k1);
assertNotNull(dr.getEntry(k1));
assertEquals(v1Dynamic, dr.getEntry(k1).getValue());
}
});
// Assert the clients have the dynamic region and the new value
client1.invoke(new CacheSerializableRunnable("Validate dynamic region creation on client") {
public void run2() throws CacheException {
Region r = getRootRegion(name);
assertNotNull(r);
long end = System.currentTimeMillis() + 10000;
Region dr = null;
for (; ; ) {
try {
dr = r.getSubregion(dynFromServerName);
assertNotNull(dr);
assertNotNull(getCache().getRegion(name + Region.SEPARATOR + dynFromServerName));
break;
} catch (AssertionError e) {
if (System.currentTimeMillis() > end) {
throw e;
} else {
Wait.pause(1000);
}
}
}
waitForEntry(dr, k1);
assertNotNull(dr.getEntry(k1));
assertEquals(v1Dynamic, dr.getEntry(k1).getValue());
}
});
// now delete the dynamic region on a server and see if it goes away on client
srv2.invoke(new CacheSerializableRunnable("Server dynamic region destruction") {
public void run2() throws CacheException {
assertTrue(DynamicRegionFactory.get().isActive());
Region r = getRootRegion(name);
assertNotNull(r);
String drName = r.getFullPath() + Region.SEPARATOR + dynFromServerName;
assertNotNull(getCache().getRegion(drName));
DynamicRegionFactory.get().destroyDynamicRegion(drName);
assertNull(getCache().getRegion(drName));
}
});
srv1.invoke(new CacheSerializableRunnable("Validate dynamic region destruction on other server") {
public void run2() throws CacheException {
Region r = getRootRegion(name);
assertNotNull(r);
String drName = r.getFullPath() + Region.SEPARATOR + dynFromServerName;
{
int retry = 100;
while (retry-- > 0 && getCache().getRegion(drName) != null) {
try {
Thread.sleep(100);
} catch (InterruptedException ignore) {
fail("interrupted");
}
}
}
assertNull(getCache().getRegion(drName));
}
});
// Assert the clients no longer have the dynamic region
client1.invoke(new CacheSerializableRunnable("Validate dynamic region destruction on client") {
public void run2() throws CacheException {
Region r = getRootRegion(name);
assertNotNull(r);
String drName = r.getFullPath() + Region.SEPARATOR + dynFromServerName;
{
int retry = 100;
while (retry-- > 0 && getCache().getRegion(drName) != null) {
try {
Thread.sleep(100);
} catch (InterruptedException ignore) {
fail("interrupted");
}
}
}
assertNull(getCache().getRegion(drName));
// region,dynamicRegionList in DynamicRegionFactory // ?
try {
Thread.sleep(10000);
} catch (InterruptedException ignore) {
fail("interrupted");
}
try {
DynamicRegionFactory.get().destroyDynamicRegion(drName);
fail("expected RegionDestroyedException");
} catch (RegionDestroyedException expected) {
}
}
});
} finally {
// clean-up loner
client1.invoke(() -> disconnectFromDS());
srv1.invoke(() -> disconnectFromDS());
srv2.invoke(() -> disconnectFromDS());
}
}
use of org.apache.geode.internal.cache.tier.sockets.CacheClientNotifier in project geode by apache.
the class CloseDurableCqFunction method execute.
@Override
public void execute(FunctionContext context) {
final Cache cache = CliUtil.getCacheIfExists();
final String memberNameOrId = CliUtil.getMemberNameOrId(cache.getDistributedSystem().getDistributedMember());
CacheClientNotifier cacheClientNotifier = CacheClientNotifier.getInstance();
String[] args = (String[]) context.getArguments();
String durableClientId = args[0];
String cqName = args[1];
MemberResult memberResult = new MemberResult(memberNameOrId);
try {
if (cacheClientNotifier != null) {
CacheClientProxy cacheClientProxy = cacheClientNotifier.getClientProxy(durableClientId);
if (cacheClientProxy != null) {
if (cacheClientNotifier.closeClientCq(durableClientId, cqName)) {
memberResult.setSuccessMessage(CliStrings.format(CliStrings.CLOSE_DURABLE_CQS__SUCCESS, cqName, durableClientId));
} else {
memberResult.setErrorMessage(CliStrings.format(CliStrings.CLOSE_DURABLE_CQS__UNABLE__TO__CLOSE__CQ, cqName, durableClientId));
}
} else {
memberResult.setErrorMessage(CliStrings.format(CliStrings.NO_CLIENT_FOUND_WITH_CLIENT_ID, durableClientId));
}
} else {
memberResult.setErrorMessage(CliStrings.NO_CLIENT_FOUND);
}
} catch (Exception e) {
memberResult.setExceptionMessage(e.getMessage());
} finally {
context.getResultSender().lastResult(memberResult);
}
}
use of org.apache.geode.internal.cache.tier.sockets.CacheClientNotifier in project geode by apache.
the class Bug48571DUnitTest method verifyStats.
public static void verifyStats() throws Exception {
CacheClientNotifier ccn = CacheClientNotifier.getInstance();
CacheClientProxy ccp = ccn.getClientProxies().iterator().next();
cache.getLoggerI18n().info(LocalizedStrings.DEBUG, "getQueueSize() " + ccp.getQueueSize());
cache.getLoggerI18n().info(LocalizedStrings.DEBUG, "getQueueSizeStat() " + ccp.getQueueSizeStat());
cache.getLoggerI18n().info(LocalizedStrings.DEBUG, "getEventsEnqued() " + ccp.getHARegionQueue().getStatistics().getEventsEnqued());
cache.getLoggerI18n().info(LocalizedStrings.DEBUG, "getEventsDispatched() " + ccp.getHARegionQueue().getStatistics().getEventsDispatched());
cache.getLoggerI18n().info(LocalizedStrings.DEBUG, "getEventsRemoved() " + ccp.getHARegionQueue().getStatistics().getEventsRemoved());
cache.getLoggerI18n().info(LocalizedStrings.DEBUG, "getNumVoidRemovals() " + ccp.getHARegionQueue().getStatistics().getNumVoidRemovals());
assertEquals(ccp.getQueueSize(), ccp.getQueueSizeStat());
}
Aggregations