use of org.apache.geode.cache.AttributesFactory in project geode by apache.
the class DiskRegionDUnitTest method testEvictValueOnRegionWithEvictionAttributes.
/**
* Tests calling region.evictValue() on region with eviction-attribute set.
*/
@Test
public void testEvictValueOnRegionWithEvictionAttributes() throws Exception {
final String name = this.getUniqueName() + "testRegionEvictValue";
File d = new File("DiskRegions" + OSProcess.getId());
d.mkdirs();
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
factory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(100, EvictionAction.OVERFLOW_TO_DISK));
DiskStoreFactory dsf = getCache().createDiskStoreFactory();
dsf.setDiskDirs(new File[] { d });
DiskStore ds = dsf.create(name);
factory.setDiskStoreName(ds.getName());
Region region = createRegion(name, factory.create());
int size = 200;
for (int i = 0; i < size; i++) {
region.put("Key-" + i, new Integer(i));
}
// Evict alternate values.
for (int i = 0; i < size / 4; i++) {
try {
((LocalRegion) region).evictValue("Key-" + i);
fail("Should have thrown exception with, evictValue not supported on region with eviction attributes.");
} catch (Exception ex) {
// Expected exception.
// continue.
}
}
}
use of org.apache.geode.cache.AttributesFactory in project geode by apache.
the class DiskRegionDUnitTest method testLRUCapacityController.
/**
* Tests disk overflow with an entry-based {@link LRUCapacityController}.
*/
@Test
public void testLRUCapacityController() throws CacheException {
final String name = this.getUniqueName();
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(1000, EvictionAction.OVERFLOW_TO_DISK));
DiskStoreFactory dsf = getCache().createDiskStoreFactory();
factory.setDiskSynchronous(true);
File d = new File("DiskRegions" + OSProcess.getId());
d.mkdirs();
dsf.setDiskDirs(new File[] { d });
DiskStore ds = dsf.create(name);
factory.setDiskStoreName(ds.getName());
Region region = createRegion(name, factory.create());
DiskRegion dr = ((LocalRegion) region).getDiskRegion();
DiskRegionStats diskStats = dr.getStats();
LRUStatistics lruStats = getLRUStats(region);
flush(region);
assertEquals(0, diskStats.getWrites());
assertEquals(0, diskStats.getReads());
assertEquals(0, lruStats.getEvictions());
// Put in larger stuff until we start evicting
for (int i = 1; i <= 1000; i++) {
// System.out.println("total " + i + ", evictions " +
// lruStats.getEvictions());
Object key = new Integer(i);
Object value = String.valueOf(i);
region.put(key, value);
assertEquals(i, lruStats.getCounter());
assertEquals(0, lruStats.getEvictions());
assertEquals("On iteration " + i, 0, diskStats.getWrites());
assertEquals(0, diskStats.getReads());
assertEquals(0, diskStats.getNumOverflowOnDisk());
}
assertEquals(0, diskStats.getWrites());
assertEquals(0, diskStats.getReads());
assertEquals(0, diskStats.getNumOverflowOnDisk());
// Add a new value
region.put(new Integer(1000 + 1), String.valueOf(1000 + 1));
assertEquals(1000, lruStats.getCounter());
assertEquals(1, lruStats.getEvictions());
assertEquals(1, diskStats.getWrites());
assertEquals(0, diskStats.getReads());
assertEquals(1, diskStats.getNumOverflowOnDisk());
assertEquals(1000, diskStats.getNumEntriesInVM());
// Add another new value
region.put(new Integer(1000 + 2), String.valueOf(1000 + 2));
assertEquals(1000, lruStats.getCounter());
assertEquals(2, lruStats.getEvictions());
assertEquals(2, diskStats.getWrites());
assertEquals(0, diskStats.getReads());
assertEquals(2, diskStats.getNumOverflowOnDisk());
assertEquals(1000, diskStats.getNumEntriesInVM());
// Replace a value
region.put(new Integer(1000), String.valueOf(1000));
assertEquals(1000, lruStats.getCounter());
assertEquals(2, lruStats.getEvictions());
assertEquals(2, diskStats.getWrites());
assertEquals(0, diskStats.getReads());
assertEquals(2, diskStats.getNumOverflowOnDisk());
assertEquals(1000, diskStats.getNumEntriesInVM());
}
use of org.apache.geode.cache.AttributesFactory in project geode by apache.
the class ClientMembershipDUnitTest method testClientMembershipEventsInServer.
/**
* Tests notification of events in server process. Bridge servers detect client joins when the
* client connects to the server.
*/
@Test
public void testClientMembershipEventsInServer() throws Exception {
final boolean[] fired = new boolean[3];
final DistributedMember[] member = new DistributedMember[3];
final String[] memberId = new String[3];
final boolean[] isClient = new boolean[3];
// create and register ClientMembershipListener in controller vm...
ClientMembershipListener listener = new ClientMembershipListener() {
public void memberJoined(ClientMembershipEvent event) {
System.out.println("[testClientMembershipEventsInServer] memberJoined: " + event);
fired[JOINED] = true;
member[JOINED] = event.getMember();
memberId[JOINED] = event.getMemberId();
isClient[JOINED] = event.isClient();
assertFalse(fired[LEFT] || fired[CRASHED]);
}
public void memberLeft(ClientMembershipEvent event) {
System.out.println("[testClientMembershipEventsInServer] memberLeft: " + event);
fired[LEFT] = true;
member[LEFT] = event.getMember();
memberId[LEFT] = event.getMemberId();
isClient[LEFT] = event.isClient();
assertFalse(fired[JOINED] || fired[CRASHED]);
}
public void memberCrashed(ClientMembershipEvent event) {
System.out.println("[testClientMembershipEventsInServer] memberCrashed: " + event);
fired[CRASHED] = true;
member[CRASHED] = event.getMember();
memberId[CRASHED] = event.getMemberId();
isClient[CRASHED] = event.isClient();
assertFalse(fired[JOINED] || fired[LEFT]);
}
};
ClientMembership.registerClientMembershipListener(listener);
final VM vm0 = Host.getHost(0).getVM(0);
final String name = this.getUniqueName();
final int[] ports = new int[1];
// create BridgeServer in controller vm...
System.out.println("[testClientMembershipEventsInServer] Create BridgeServer");
getSystem();
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
Region region = createRegion(name, factory.create());
assertNotNull(region);
assertNotNull(getRootRegion().getSubregion(name));
ports[0] = startBridgeServer(0);
assertTrue(ports[0] != 0);
DistributedMember serverMember = getMemberId();
String serverMemberId = serverMember.toString();
System.out.println("[testClientMembershipEventsInServer] ports[0]=" + ports[0]);
System.out.println("[testClientMembershipEventsInServer] serverMemberId=" + serverMemberId);
System.out.println("[testClientMembershipEventsInServer] serverMember=" + serverMember);
assertFalse(fired[JOINED]);
assertNull(member[JOINED]);
assertNull(memberId[JOINED]);
assertFalse(isClient[JOINED]);
assertFalse(fired[LEFT]);
assertNull(member[LEFT]);
assertNull(memberId[LEFT]);
assertFalse(isClient[LEFT]);
assertFalse(fired[CRASHED]);
assertNull(member[CRASHED]);
assertNull(memberId[CRASHED]);
assertFalse(isClient[CRASHED]);
// sanity check...
System.out.println("[testClientMembershipEventsInServer] sanity check");
DistributedMember test = new TestDistributedMember("test");
InternalClientMembership.notifyClientJoined(test);
Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
return fired[JOINED] || fired[LEFT] || fired[CRASHED];
});
assertTrue(fired[JOINED]);
assertEquals(test, member[JOINED]);
assertEquals(test.getId(), memberId[JOINED]);
assertTrue(isClient[JOINED]);
assertFalse(fired[LEFT]);
assertNull(member[LEFT]);
assertNull(memberId[LEFT]);
assertFalse(isClient[LEFT]);
assertFalse(fired[CRASHED]);
assertNull(member[CRASHED]);
assertNull(memberId[CRASHED]);
assertFalse(isClient[CRASHED]);
resetArraysForTesting(fired, member, memberId, isClient);
final Host host = Host.getHost(0);
SerializableCallable createConnectionPool = new SerializableCallable("Create connectionPool") {
public Object call() {
System.out.println("[testClientMembershipEventsInServer] create bridge client");
Properties config = new Properties();
config.setProperty(MCAST_PORT, "0");
config.setProperty(LOCATORS, "");
config.setProperty(ENABLE_NETWORK_PARTITION_DETECTION, "false");
properties = config;
DistributedSystem s = getSystem(config);
AttributesFactory factory = new AttributesFactory();
Pool pool = ClientServerTestCase.configureConnectionPool(factory, NetworkUtils.getServerHostName(host), ports, true, -1, 2, null);
createRegion(name, factory.create());
assertNotNull(getRootRegion().getSubregion(name));
// see geode-1078
assertTrue(s == basicGetSystem());
return getMemberId();
}
};
// create bridge client in vm0...
DistributedMember clientMember = (DistributedMember) vm0.invoke(createConnectionPool);
String clientMemberId = clientMember.toString();
Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
return fired[JOINED] || fired[LEFT] || fired[CRASHED];
});
System.out.println("[testClientMembershipEventsInServer] assert server detected client join");
assertTrue(fired[JOINED]);
assertEquals(member[JOINED] + " should equal " + clientMember, clientMember, member[JOINED]);
assertEquals(memberId[JOINED] + " should equal " + clientMemberId, clientMemberId, memberId[JOINED]);
assertTrue(isClient[JOINED]);
assertFalse(fired[LEFT]);
assertNull(member[LEFT]);
assertNull(memberId[LEFT]);
assertFalse(isClient[LEFT]);
assertFalse(fired[CRASHED]);
assertNull(member[CRASHED]);
assertNull(memberId[CRASHED]);
assertFalse(isClient[CRASHED]);
resetArraysForTesting(fired, member, memberId, isClient);
pauseForClientToJoin();
vm0.invoke(new SerializableRunnable("Stop bridge client") {
public void run() {
System.out.println("[testClientMembershipEventsInServer] Stop bridge client");
getRootRegion().getSubregion(name).close();
Map m = PoolManager.getAll();
Iterator mit = m.values().iterator();
while (mit.hasNext()) {
Pool p = (Pool) mit.next();
p.destroy();
}
}
});
Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
return fired[JOINED] || fired[LEFT] || fired[CRASHED];
});
System.out.println("[testClientMembershipEventsInServer] assert server detected client left");
assertFalse(fired[JOINED]);
assertNull(member[JOINED]);
assertNull(memberId[JOINED]);
assertFalse(isClient[JOINED]);
assertTrue(fired[LEFT]);
assertEquals(clientMember, member[LEFT]);
assertEquals(clientMemberId, memberId[LEFT]);
assertTrue(isClient[LEFT]);
assertFalse(fired[CRASHED]);
assertNull(member[CRASHED]);
assertNull(memberId[CRASHED]);
assertFalse(isClient[CRASHED]);
resetArraysForTesting(fired, member, memberId, isClient);
// reconnect bridge client to test for crashed event
clientMemberId = vm0.invoke(createConnectionPool).toString();
Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
return fired[JOINED] || fired[LEFT] || fired[CRASHED];
});
System.out.println("[testClientMembershipEventsInServer] assert server detected client re-join");
assertTrue(fired[JOINED]);
assertEquals(clientMember, member[JOINED]);
assertEquals(clientMemberId, memberId[JOINED]);
assertTrue(isClient[JOINED]);
assertFalse(fired[LEFT]);
assertNull(member[LEFT]);
assertNull(memberId[LEFT]);
assertFalse(isClient[LEFT]);
assertFalse(fired[CRASHED]);
assertNull(member[CRASHED]);
assertNull(memberId[CRASHED]);
assertFalse(isClient[CRASHED]);
resetArraysForTesting(fired, member, memberId, isClient);
pauseForClientToJoin();
ServerConnection.setForceClientCrashEvent(true);
try {
vm0.invoke(new SerializableRunnable("Stop bridge client") {
public void run() {
System.out.println("[testClientMembershipEventsInServer] Stop bridge client");
getRootRegion().getSubregion(name).close();
Map m = PoolManager.getAll();
Iterator mit = m.values().iterator();
while (mit.hasNext()) {
Pool p = (Pool) mit.next();
p.destroy();
}
}
});
Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
return fired[JOINED] || fired[LEFT] || fired[CRASHED];
});
System.out.println("[testClientMembershipEventsInServer] assert server detected client crashed");
assertFalse(fired[JOINED]);
assertNull(member[JOINED]);
assertNull(memberId[JOINED]);
assertFalse(isClient[JOINED]);
assertFalse(fired[LEFT]);
assertNull(member[LEFT]);
assertNull(memberId[LEFT]);
assertFalse(isClient[LEFT]);
assertTrue(fired[CRASHED]);
assertEquals(clientMember, member[CRASHED]);
assertEquals(clientMemberId, memberId[CRASHED]);
assertTrue(isClient[CRASHED]);
} finally {
ServerConnection.setForceClientCrashEvent(false);
}
}
use of org.apache.geode.cache.AttributesFactory in project geode by apache.
the class ClientMembershipDUnitTest method testGetConnectedServers.
/**
* Starts up 4 server and the controller vm as a client, then calls and tests
* ClientMembership.getConnectedServers().
*/
@Test
public void testGetConnectedServers() throws Exception {
final Host host = Host.getHost(0);
final String name = this.getUniqueName();
final int[] ports = new int[host.getVMCount()];
for (int i = 0; i < host.getVMCount(); i++) {
final int whichVM = i;
final VM vm = Host.getHost(0).getVM(i);
vm.invoke("Create bridge server", () -> {
// create BridgeServer in controller vm...
System.out.println("[testGetConnectedServers] Create BridgeServer");
getSystem();
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
Region region = createRegion(name + "_" + whichVM, factory.create());
assertNotNull(region);
assertNotNull(getRootRegion().getSubregion(name + "_" + whichVM));
region.put("KEY-1", "VAL-1");
try {
testGetConnectedServers_port = startBridgeServer(0);
} catch (IOException e) {
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().error("startBridgeServer threw IOException", e);
fail("startBridgeServer threw IOException " + e.getMessage());
}
assertTrue(testGetConnectedServers_port != 0);
System.out.println("[testGetConnectedServers] port=" + ports[whichVM]);
System.out.println("[testGetConnectedServers] serverMemberId=" + getDistributedMember());
});
ports[whichVM] = vm.invoke("getTestGetConnectedServers_port", () -> ClientMembershipDUnitTest.getTestGetConnectedServers_port());
assertTrue(ports[whichVM] != 0);
}
System.out.println("[testGetConnectedServers] create bridge client");
Properties config = new Properties();
config.setProperty(MCAST_PORT, "0");
config.setProperty(LOCATORS, "");
properties = config;
getSystem(config);
getCache();
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
for (int i = 0; i < ports.length; i++) {
System.out.println("[testGetConnectedServers] creating connectionpool for " + NetworkUtils.getServerHostName(host) + " " + ports[i]);
int[] thisServerPorts = new int[] { ports[i] };
ClientServerTestCase.configureConnectionPoolWithName(factory, NetworkUtils.getServerHostName(host), thisServerPorts, false, -1, -1, null, "pooly" + i);
Region region = createRegion(name + "_" + i, factory.create());
assertNotNull(getRootRegion().getSubregion(name + "_" + i));
region.get("KEY-1");
}
final int expectedVMCount = host.getVMCount();
Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
if (PoolManager.getAll().size() != expectedVMCount) {
return false;
}
Map connectedServers = InternalClientMembership.getConnectedServers();
if (connectedServers == null) {
return false;
}
if (connectedServers.size() != expectedVMCount) {
return false;
}
return true;
});
assertEquals(host.getVMCount(), PoolManager.getAll().size());
Map connectedServers = InternalClientMembership.getConnectedServers();
assertNotNull(connectedServers);
assertEquals(host.getVMCount(), connectedServers.size());
for (Iterator iter = connectedServers.keySet().iterator(); iter.hasNext(); ) {
String connectedServer = (String) iter.next();
System.out.println("[testGetConnectedServers] value for connectedServer: " + connectedServers.get(connectedServer));
}
}
use of org.apache.geode.cache.AttributesFactory in project geode by apache.
the class DiskRegionDUnitTest method testValues.
/**
* Tests iterating over all of the values when some have been overflowed.
*/
@Test
public void testValues() throws Exception {
final String name = this.getUniqueName();
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(100, EvictionAction.OVERFLOW_TO_DISK));
File d = new File("DiskRegions" + OSProcess.getId());
d.mkdirs();
DiskStoreFactory dsf = getCache().createDiskStoreFactory();
dsf.setDiskDirs(new File[] { d });
DiskStore ds = dsf.create(name);
factory.setDiskStoreName(ds.getName());
Region region = createRegion(name, factory.create());
// DiskRegion dr = ((LocalRegion) region).getDiskRegion();
// DiskRegionStats diskStats = dr.getStats();
LRUStatistics lruStats = getLRUStats(region);
// Put in larger stuff until we start evicting
int total;
for (total = 0; lruStats.getEvictions() <= 0; total++) {
int[] array = new int[250];
array[0] = total;
region.put(new Integer(total), array);
}
BitSet bits = new BitSet();
Collection values = region.values();
assertEquals(total, values.size());
for (Iterator iter = values.iterator(); iter.hasNext(); ) {
Object value = iter.next();
assertNotNull(value);
int[] array = (int[]) value;
int i = array[0];
assertFalse("Bit " + i + " is already set", bits.get(i));
bits.set(i);
}
}
Aggregations