/** * Pick the least loaded servers in the given group. * * @param group * the group, or null or "" if the client has no server group. * @param excludedServers * a list of servers to exclude as choices * @param count * how many distinct servers to pick. * @return a list containing the best servers. The size of the list will be * less than or equal to count, depending on if there are enough * servers available. */ public List getServersForQueue(String group, Set excludedServers, int count) { return getServersForQueue(null/* no id */, group, excludedServers, count); }
/** * Pick the least loaded servers in the given group. * * @param group * the group, or null or "" if the client has no server group. * @param excludedServers * a list of servers to exclude as choices * @param count * how many distinct servers to pick. * @return a list containing the best servers. The size of the list will be * less than or equal to count, depending on if there are enough * servers available. */ public List getServersForQueue(String group, Set excludedServers, int count) { return getServersForQueue(null/* no id */, group, excludedServers, count); }
/** * Test to make sure than an empty snapshot returns the * correct values. */ public void testEmptySnapshot() { LocatorLoadSnapshot sn = new LocatorLoadSnapshot(); Assert.assertNull(sn.getServerForConnection("group", Collections.EMPTY_SET)); Assert.assertNull(sn.getServerForConnection(null, Collections.EMPTY_SET)); Assert.assertEquals(Collections.EMPTY_LIST, sn.getServersForQueue(null, Collections.EMPTY_SET, 5)); }
/** * Test that we can specify a list of servers to exclude and the snapshot honors the request when * picking the best server. */ 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); Assert.assertEquals(l1, sn.getServerForConnection(null, Collections.EMPTY_SET)); Assert.assertEquals(l2, sn.getServerForConnection(null, Collections.singleton(l1))); Assert.assertEquals(null, sn.getServerForConnection(null, excludeAll)); Assert.assertEquals(Arrays.asList(new ServerLocation[] {l2} ), sn.getServersForQueue(null, Collections.singleton(l1), 3)); Assert.assertEquals(Arrays.asList(new ServerLocation[] {} ), sn.getServersForQueue(null, excludeAll, 3)); }
Assert.assertEquals(Collections.EMPTY_LIST, sn.getServersForQueue("group", Collections.EMPTY_SET, 5)); Assert.assertEquals(l2, sn.getServerForConnection(null, Collections.EMPTY_SET)); Assert.assertEquals(Collections.singletonList(l2), sn.getServersForQueue(null, Collections.EMPTY_SET, 1)); Assert.assertEquals(Collections.singletonList(l1), sn.getServersForQueue(null, Collections.EMPTY_SET, 1)); Assert.assertEquals(Collections.singletonList(l2), sn.getServersForQueue(null, Collections.EMPTY_SET, 1)); Assert.assertEquals(Arrays.asList(new ServerLocation[] {l2, l1} ), sn.getServersForQueue(null, Collections.EMPTY_SET, 5)); Assert.assertEquals(Arrays.asList(new ServerLocation[] {l2, l1} ), sn.getServersForQueue(null, Collections.EMPTY_SET, -1));
sn.updateLoad(l2, new ServerLoad(100,1,1,1)); Assert.assertEquals(l1, sn.getServerForConnection("b", Collections.EMPTY_SET)); Assert.assertEquals(Arrays.asList(new ServerLocation[] {l1} ), sn.getServersForQueue("a", Collections.EMPTY_SET, -1)); Assert.assertEquals(Arrays.asList(new ServerLocation[] {l2} ), sn.getServersForQueue("c", Collections.EMPTY_SET, -1)); Assert.assertEquals(Arrays.asList(new ServerLocation[] {l1, l2} ), sn.getServersForQueue("b", Collections.EMPTY_SET, -1)); Assert.assertEquals(Arrays.asList(new ServerLocation[] {l1, l2} ), sn.getServersForQueue(null, Collections.EMPTY_SET, -1)); Assert.assertEquals(Arrays.asList(new ServerLocation[] {l1, l2} ), sn.getServersForQueue("b", Collections.EMPTY_SET, 5)); Assert.assertNull(sn.getServerForConnection("a", Collections.EMPTY_SET)); Assert.assertEquals(l2, sn.getServerForConnection("c", Collections.EMPTY_SET)); Assert.assertEquals(Arrays.asList(new ServerLocation[] {} ), sn.getServersForQueue("a", Collections.EMPTY_SET, -1)); Assert.assertEquals(Arrays.asList(new ServerLocation[] {l2} ), sn.getServersForQueue("b", Collections.EMPTY_SET, 5));
/** * Test that we can remove a server from the snapshot. It should not suggest * that server after it has been removed. */ 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)); Assert.assertEquals(l1, sn.getServerForConnection(null, Collections.EMPTY_SET)); Assert.assertEquals(Arrays.asList(new ServerLocation[] {l1, l2} ), sn.getServersForQueue(null, Collections.EMPTY_SET, -1)); sn.removeServer(l1); Assert.assertEquals(l2, sn.getServerForConnection(null, Collections.EMPTY_SET)); Assert.assertEquals(Collections.singletonList(l2), sn.getServersForQueue(null, Collections.EMPTY_SET, -1)); }
if(clientRequest.getRedundantCopies() == -1) { candidates = loadSnapshot.getServersForQueue(clientRequest.getProxyId(), clientRequest.getServerGroup(), excludedServers,-1); candidates = loadSnapshot.getServersForQueue(clientRequest.getProxyId(), clientRequest.getServerGroup(), excludedServers,
if(clientRequest.getRedundantCopies() == -1) { candidates = loadSnapshot.getServersForQueue(clientRequest.getProxyId(), clientRequest.getServerGroup(), excludedServers,-1); candidates = loadSnapshot.getServersForQueue(clientRequest.getProxyId(), clientRequest.getServerGroup(), excludedServers,