UserDefinedPriorityKeeperActiveElectAlgorithm extends AbstractActiveElectAlgorithm { @Override public KeeperMeta select(String clusterId, String shardId, List<KeeperMeta> toBeSelected) { if(toBeSelected.size() == 0){ return null; } for(KeeperMeta keeperMeta : userDefinedPriority){ for(KeeperMeta select : toBeSelected){ if(MetaUtils.same(keeperMeta, select)){ return select; } } } logger.warn("[select][no keeper in given list, use first]{}, {}", userDefinedPriority, toBeSelected); return toBeSelected.get(0); } UserDefinedPriorityKeeperActiveElectAlgorithm(List<KeeperMeta> userDefinedPriority); @Override KeeperMeta select(String clusterId, String shardId, List<KeeperMeta> toBeSelected);  }
@Test public void test(){ List<KeeperMeta> priority = getDcKeepers(getDc(), getClusterId(), getShardId()); UserDefinedPriorityKeeperActiveElectAlgorithm algorithm = new UserDefinedPriorityKeeperActiveElectAlgorithm(priority); Assert.assertEquals(priority.get(0), algorithm.select(getClusterId(), getShardId(), priority)); List<KeeperMeta> onlyOne = new LinkedList<>(); onlyOne.add(priority.get(1)); Assert.assertEquals(priority.get(1), algorithm.select(getClusterId(), getShardId(), onlyOne)); List<KeeperMeta> none = new LinkedList<>(); none.add(differentKeeper(priority)); Assert.assertEquals(none.get(0), algorithm.select(getClusterId(), getShardId(), none)); List<KeeperMeta> reverse = new LinkedList<>(priority); Collections.reverse(reverse); Assert.assertEquals(priority.get(0), algorithm.select(getClusterId(), getShardId(), reverse)); }