Search in sources :

Example 1 with ServerLoad

use of org.apache.geode.cache.server.ServerLoad in project geode by apache.

the class LocatorLoadSnapshotIntegrationTest method testConcurrentBalancing.

/**
   * A basic test of concurrent functionality. Starts a number of threads making requests and
   * expects the load to be balanced between three servers.
   * 
   * @throws InterruptedException
   */
@Test
public void testConcurrentBalancing() throws InterruptedException {
    int NUM_THREADS = 50;
    final int NUM_REQUESTS = 10000;
    // We should never be off by more than
    int ALLOWED_THRESHOLD = 50;
    // the number of concurrent threads.
    final LocatorLoadSnapshot sn = new LocatorLoadSnapshot();
    final ServerLocation l1 = new ServerLocation("localhost", 1);
    final ServerLocation l2 = new ServerLocation("localhost", 2);
    final ServerLocation l3 = new ServerLocation("localhost", 3);
    int initialLoad1 = (int) (Math.random() * (NUM_REQUESTS / 2));
    int initialLoad2 = (int) (Math.random() * (NUM_REQUESTS / 2));
    int initialLoad3 = (int) (Math.random() * (NUM_REQUESTS / 2));
    sn.addServer(l1, new String[0], new ServerLoad(initialLoad1, 1, 0, 1));
    sn.addServer(l2, new String[0], new ServerLoad(initialLoad2, 1, 0, 1));
    sn.addServer(l3, new String[0], new ServerLoad(initialLoad3, 1, 0, 1));
    final Map loadCounts = new HashMap();
    loadCounts.put(l1, new AtomicInteger(initialLoad1));
    loadCounts.put(l2, new AtomicInteger(initialLoad2));
    loadCounts.put(l3, new AtomicInteger(initialLoad3));
    Thread[] threads = new Thread[NUM_THREADS];
    // final Object lock = new Object();
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i] = new Thread("Thread-" + i) {

            public void run() {
                for (int ii = 0; ii < NUM_REQUESTS; ii++) {
                    ServerLocation location;
                    // synchronized(lock) {
                    location = sn.getServerForConnection(null, Collections.EMPTY_SET);
                    // }
                    AtomicInteger count = (AtomicInteger) loadCounts.get(location);
                    count.incrementAndGet();
                }
            }
        };
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i].start();
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        Thread t = threads[i];
        long ms = 30 * 1000;
        t.join(30 * 1000);
        if (t.isAlive()) {
            for (int j = 0; j < NUM_THREADS; j++) {
                threads[j].interrupt();
            }
            fail("Thread did not terminate after " + ms + " ms: " + t);
        }
    }
    double expectedPerServer = (initialLoad1 + initialLoad2 + initialLoad3 + NUM_REQUESTS * NUM_THREADS) / (double) loadCounts.size();
    for (Iterator itr = loadCounts.entrySet().iterator(); itr.hasNext(); ) {
        Map.Entry entry = (Map.Entry) itr.next();
        ServerLocation location = (ServerLocation) entry.getKey();
        AtomicInteger count = (AtomicInteger) entry.getValue();
        int difference = (int) Math.abs(count.get() - expectedPerServer);
        assertTrue("Count " + count + " for server " + location + " is not within " + ALLOWED_THRESHOLD + " of " + expectedPerServer, difference < ALLOWED_THRESHOLD);
    }
}
Also used : HashMap(java.util.HashMap) ServerLoad(org.apache.geode.cache.server.ServerLoad) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Iterator(java.util.Iterator) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 2 with ServerLoad

use of org.apache.geode.cache.server.ServerLoad in project geode by apache.

the class LocatorLoadSnapshotJUnitTest method testGroups.

/**
   * Test of server groups. Make sure that the snapshot returns only servers from the correct group.
   */
@Test
public void testGroups() {
    LocatorLoadSnapshot sn = new LocatorLoadSnapshot();
    ServerLocation l1 = new ServerLocation("localhost", 1);
    ServerLocation l2 = new ServerLocation("localhost", 2);
    sn.addServer(l1, new String[] { "a", "b" }, new ServerLoad(1, 1, 1, 1));
    sn.addServer(l2, new String[] { "b", "c" }, new ServerLoad(1, 1, 1, 1));
    assertNotNull(sn.getServerForConnection(null, Collections.EMPTY_SET));
    assertEquals(l1, sn.getServerForConnection("a", Collections.EMPTY_SET));
    assertEquals(l2, sn.getServerForConnection("c", Collections.EMPTY_SET));
    sn.updateLoad(l1, new ServerLoad(10, 1, 1, 1));
    assertEquals(l2, sn.getServerForConnection("b", Collections.EMPTY_SET));
    sn.updateLoad(l2, new ServerLoad(100, 1, 1, 1));
    assertEquals(l1, sn.getServerForConnection("b", Collections.EMPTY_SET));
    assertEquals(Arrays.asList(new ServerLocation[] { l1 }), sn.getServersForQueue("a", Collections.EMPTY_SET, -1));
    assertEquals(Arrays.asList(new ServerLocation[] { l2 }), sn.getServersForQueue("c", Collections.EMPTY_SET, -1));
    assertEquals(Arrays.asList(new ServerLocation[] { l1, l2 }), sn.getServersForQueue("b", Collections.EMPTY_SET, -1));
    assertEquals(Arrays.asList(new ServerLocation[] { l1, l2 }), sn.getServersForQueue(null, Collections.EMPTY_SET, -1));
    assertEquals(Arrays.asList(new ServerLocation[] { l1, l2 }), sn.getServersForQueue("b", Collections.EMPTY_SET, 5));
    sn.removeServer(l1);
    assertEquals(l2, sn.getServerForConnection("b", Collections.EMPTY_SET));
    assertEquals(l2, sn.getServerForConnection("b", Collections.EMPTY_SET));
    assertNull(sn.getServerForConnection("a", Collections.EMPTY_SET));
    assertEquals(l2, sn.getServerForConnection("c", Collections.EMPTY_SET));
    assertEquals(Arrays.asList(new ServerLocation[] {}), sn.getServersForQueue("a", Collections.EMPTY_SET, -1));
    assertEquals(Arrays.asList(new ServerLocation[] { l2 }), sn.getServersForQueue("b", Collections.EMPTY_SET, 5));
}
Also used : ServerLoad(org.apache.geode.cache.server.ServerLoad) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 3 with ServerLoad

use of org.apache.geode.cache.server.ServerLoad in project geode by apache.

the class LocatorLoadSnapshotJUnitTest method testExcludes.

/**
   * Test that we can specify a list of servers to exclude and the snapshot honors the request when
   * picking the best server.
   */
@Test
public void testExcludes() {
    LocatorLoadSnapshot sn = new LocatorLoadSnapshot();
    ServerLocation l1 = new ServerLocation("localhost", 1);
    ServerLocation l2 = new ServerLocation("localhost", 2);
    sn.addServer(l1, new String[0], new ServerLoad(1, 1, 1, 1));
    sn.addServer(l2, new String[0], new ServerLoad(100, 1, 100, 1));
    HashSet excludeAll = new HashSet();
    excludeAll.add(l1);
    excludeAll.add(l2);
    assertEquals(l1, sn.getServerForConnection(null, Collections.EMPTY_SET));
    assertEquals(l2, sn.getServerForConnection(null, Collections.singleton(l1)));
    assertEquals(null, sn.getServerForConnection(null, excludeAll));
    assertEquals(Arrays.asList(new ServerLocation[] { l2 }), sn.getServersForQueue(null, Collections.singleton(l1), 3));
    assertEquals(Arrays.asList(new ServerLocation[] {}), sn.getServersForQueue(null, excludeAll, 3));
}
Also used : ServerLoad(org.apache.geode.cache.server.ServerLoad) HashSet(java.util.HashSet) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 4 with ServerLoad

use of org.apache.geode.cache.server.ServerLoad in project geode by apache.

the class LocatorLoadSnapshotJUnitTest method testTwoServers.

/**
   * Test a snapshot with two servers. The servers are initialized with unequal load, and then and
   * then we test that after several requests, the load balancer starts sending connections to the
   * second server.
   */
@Test
public void testTwoServers() {
    LocatorLoadSnapshot sn = new LocatorLoadSnapshot();
    ServerLocation l1 = new ServerLocation("localhost", 1);
    ServerLocation l2 = new ServerLocation("localhost", 2);
    ServerLoad ld1 = new ServerLoad(3, 1, 1.01f, 1);
    ServerLoad ld2 = new ServerLoad(5, .2f, 1f, .2f);
    sn.addServer(l1, new String[0], ld1);
    sn.addServer(l2, new String[0], ld2);
    HashMap expectedLoad = new HashMap();
    expectedLoad.put(l1, ld1);
    expectedLoad.put(l2, ld2);
    assertEquals(expectedLoad, sn.getLoadMap());
    assertNull(sn.getServerForConnection("group", Collections.EMPTY_SET));
    assertEquals(Collections.EMPTY_LIST, sn.getServersForQueue("group", Collections.EMPTY_SET, 5));
    assertEquals(l1, sn.getServerForConnection(null, Collections.EMPTY_SET));
    assertEquals(l1, sn.getServerForConnection(null, Collections.EMPTY_SET));
    // the load should be equal here, so we don't know which server to expect
    sn.getServerForConnection(null, Collections.EMPTY_SET);
    sn.getServerForConnection(null, Collections.EMPTY_SET);
    assertEquals(l2, sn.getServerForConnection(null, Collections.EMPTY_SET));
    assertEquals(l2, sn.getServerForConnection(null, Collections.EMPTY_SET));
    assertEquals(Collections.singletonList(l2), sn.getServersForQueue(null, Collections.EMPTY_SET, 1));
    assertEquals(Collections.singletonList(l1), sn.getServersForQueue(null, Collections.EMPTY_SET, 1));
    assertEquals(Collections.singletonList(l2), sn.getServersForQueue(null, Collections.EMPTY_SET, 1));
    assertEquals(Arrays.asList(new ServerLocation[] { l2, l1 }), sn.getServersForQueue(null, Collections.EMPTY_SET, 5));
    assertEquals(Arrays.asList(new ServerLocation[] { l2, l1 }), sn.getServersForQueue(null, Collections.EMPTY_SET, -1));
}
Also used : ServerLoad(org.apache.geode.cache.server.ServerLoad) HashMap(java.util.HashMap) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 5 with ServerLoad

use of org.apache.geode.cache.server.ServerLoad in project geode by apache.

the class LocatorLoadSnapshotJUnitTest method testRemoveServer.

/**
   * Test that we can remove a server from the snapshot. It should not suggest that server after it
   * has been removed.
   */
@Test
public void testRemoveServer() {
    LocatorLoadSnapshot sn = new LocatorLoadSnapshot();
    ServerLocation l1 = new ServerLocation("localhost", 1);
    ServerLocation l2 = new ServerLocation("localhost", 2);
    sn.addServer(l1, new String[0], new ServerLoad(1, 1, 1, 1));
    sn.addServer(l2, new String[0], new ServerLoad(100, .2f, 10, .2f));
    assertEquals(l1, sn.getServerForConnection(null, Collections.EMPTY_SET));
    assertEquals(Arrays.asList(new ServerLocation[] { l1, l2 }), sn.getServersForQueue(null, Collections.EMPTY_SET, -1));
    sn.removeServer(l1);
    assertEquals(l2, sn.getServerForConnection(null, Collections.EMPTY_SET));
    assertEquals(Collections.singletonList(l2), sn.getServersForQueue(null, Collections.EMPTY_SET, -1));
}
Also used : ServerLoad(org.apache.geode.cache.server.ServerLoad) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Aggregations

ServerLoad (org.apache.geode.cache.server.ServerLoad)19 Test (org.junit.Test)15 HashMap (java.util.HashMap)8 UnitTest (org.apache.geode.test.junit.categories.UnitTest)8 Map (java.util.Map)6 ServerLocation (org.apache.geode.distributed.internal.ServerLocation)5 Host (org.apache.geode.test.dunit.Host)4 VM (org.apache.geode.test.dunit.VM)4 ClientServerTest (org.apache.geode.test.junit.categories.ClientServerTest)4 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)4 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)3 Iterator (java.util.Iterator)2 PoolFactoryImpl (org.apache.geode.internal.cache.PoolFactoryImpl)2 HashSet (java.util.HashSet)1 Entry (java.util.Map.Entry)1 Properties (java.util.Properties)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ClientConnectionRequest (org.apache.geode.cache.client.internal.locator.ClientConnectionRequest)1