ConsistentHashPartitionFilter implements PartitionFilter { @Override public Iterable<ServiceEndPoint> filter(Iterable<ServiceEndPoint> endPoints, PartitionContext partitionContext) { HashCode partitionHash = getPartitionHash(partitionContext); if (partitionHash == null) { return endPoints; } Map<String, ServiceEndPoint> endPointsById = indexById(endPoints); ServiceEndPoint endPoint = choose(endPointsById, partitionHash); return Collections.singleton(endPoint); } ConsistentHashPartitionFilter();  ConsistentHashPartitionFilter(String... partitionKeys);  ConsistentHashPartitionFilter(List<String> partitionKeys); private  ConsistentHashPartitionFilter(List<String> partitionKeys, int entriesPerEndPoint); @Override Iterable<ServiceEndPoint> filter(Iterable<ServiceEndPoint> endPoints, PartitionContext partitionContext);  }
@Test public void testEmptyContext() { ConsistentHashPartitionFilter filter = new ConsistentHashPartitionFilter(); List<ServiceEndPoint> endPoints = ImmutableList.of(FOO, BAR); assertEquals(endPoints, filter.filter(endPoints, PartitionContextBuilder.empty())); }
@Test public void testHashToFoo() { ConsistentHashPartitionFilter filter = new ConsistentHashPartitionFilter(); List<ServiceEndPoint> endPoints = ImmutableList.of(FOO, BAR); assertEquals(singleton(FOO), filter.filter(endPoints, PartitionContextBuilder.of("a"))); }
@Test public void testHashToBar() { ConsistentHashPartitionFilter filter = new ConsistentHashPartitionFilter(); List<ServiceEndPoint> endPoints = ImmutableList.of(FOO, BAR); assertEquals(singleton(BAR), filter.filter(endPoints, PartitionContextBuilder.of("c"))); }
@Test public void testConsistency() { ConsistentHashPartitionFilter filter = new ConsistentHashPartitionFilter(); List<ServiceEndPoint> endPoints = ImmutableList.of(FOO, BAR); PartitionContext context = PartitionContextBuilder.of(UUID.randomUUID().toString()); assertEquals(filter.filter(endPoints, context), filter.filter(endPoints, context)); }
@Test public void testServiceIdConflict() { ConsistentHashPartitionFilter filter = new ConsistentHashPartitionFilter(); assertEquals(singleton(FOO), filter.filter(ImmutableList.of(FOO, BAR), PartitionContextBuilder.of("a"))); assertEquals(singleton(FOO), filter.filter(ImmutableList.of(FOO2, FOO, BAR), PartitionContextBuilder.of("a"))); assertEquals(singleton(FOO2), filter.filter(ImmutableList.of(FOO2, BAR), PartitionContextBuilder.of("a"))); }
@Test public void testIrrelevantPartitionContext() { ConsistentHashPartitionFilter filter = new ConsistentHashPartitionFilter("cluster", "ensemble", "group"); List<ServiceEndPoint> endPoints = ImmutableList.of(FOO, BAR); assertEquals(endPoints, filter.filter(endPoints, PartitionContextBuilder.of("partition", "aaa"))); }
@Test public void testRelevantPartitionContext() { ConsistentHashPartitionFilter filter = new ConsistentHashPartitionFilter("cluster", "ensemble", "group"); List<ServiceEndPoint> endPoints = ImmutableList.of(FOO, BAR); assertEquals(singleton(FOO), filter.filter(endPoints, PartitionContextBuilder.of("ensemble", "aaa"))); }