ReplicaSelection { public abstract ServerInstance selectServer(SegmentId p, List<ServerInstance> orderedServers, Object hashKey);  abstract void reset(SegmentId p); abstract void reset(SegmentIdSet p); abstract ServerInstance selectServer(SegmentId p, List<ServerInstance> orderedServers, Object hashKey);  }
@Test public void testRandomSelection() { RandomReplicaSelection sel1 = new RandomReplicaSelection(0); RandomReplicaSelection sel2 = new RandomReplicaSelection(0); ServerInstance s1 = new ServerInstance("localhost", 8080); ServerInstance s2 = new ServerInstance("localhost", 8081); ServerInstance s3 = new ServerInstance("localhost", 8082); ServerInstance[] servers = { s1, s2, s3 }; List<ServerInstance> candidates = new ArrayList<ServerInstance>(); Assert.assertNull(sel1.selectServer(new SegmentId("1"), candidates, null)); }
@Test public void testRoundRobinSelection() { ReplicaSelection sel1 = new RoundRobinReplicaSelection(); ServerInstance s1 = new ServerInstance("localhost", 8080); ServerInstance s2 = new ServerInstance("localhost", 8081); ServerInstance s3 = new ServerInstance("localhost", 8082); ServerInstance[] servers = { s1, s2, s3 }; List<ServerInstance> candidates = new ArrayList<ServerInstance>(); Assert.assertNull(sel1.selectServer(new SegmentId("1"), candidates, null)); }
@Test public void testHashBasedSelection() { ReplicaSelection sel1 = new HashReplicaSelection(); ServerInstance s1 = new ServerInstance("localhost", 8080); ServerInstance s2 = new ServerInstance("localhost", 8081); ServerInstance s3 = new ServerInstance("localhost", 8082); BucketKey k1 = new BucketKey(0); BucketKey k2 = new BucketKey(1); BucketKey k3 = new BucketKey(2); BucketKey[] ks = { k1, k2, k3 }; ServerInstance[] servers = { s1, s2, s3 }; List<ServerInstance> candidates = new ArrayList<ServerInstance>(); Assert.assertNull(sel1.selectServer(new SegmentId("1"), candidates, null)); candidates.addAll(Arrays.asList(servers)); for (int i = 0; i < 10; i++) { int num = i % candidates.size(); BucketKey k = ks[num]; Assert .assertEquals(sel1.selectServer(new SegmentId("0"), candidates, k), candidates.get(num), "Round :" + i); } }