RoundRobinReplicaLoadBalanceAlgorithm implements ReplicaLoadBalanceAlgorithm { @Override public String getDataSource(final String name, final String primaryDataSourceName, final List<String> replicaDataSourceNames) { AtomicInteger count = COUNTS.containsKey(name) ? COUNTS.get(name) : new AtomicInteger(0); COUNTS.putIfAbsent(name, count); count.compareAndSet(replicaDataSourceNames.size(), 0); return replicaDataSourceNames.get(Math.abs(count.getAndIncrement()) % replicaDataSourceNames.size()); }  @Override String getDataSource(final String name, final String primaryDataSourceName, final List<String> replicaDataSourceNames); @Override String getType();  }
@Test public void assertGetDataSource() { String primaryDataSourceName = "test_primary_ds"; String replicaDataSourceName1 = "test_replica_ds_1"; String replicaDataSourceName2 = "test_replica_ds_2"; List<String> replicaDataSourceNames = Arrays.asList(replicaDataSourceName1, replicaDataSourceName2); assertThat(roundRobinReplicaLoadBalanceAlgorithm.getDataSource("ds", primaryDataSourceName, replicaDataSourceNames), is(replicaDataSourceName1)); assertThat(roundRobinReplicaLoadBalanceAlgorithm.getDataSource("ds", primaryDataSourceName, replicaDataSourceNames), is(replicaDataSourceName2)); assertThat(roundRobinReplicaLoadBalanceAlgorithm.getDataSource("ds", primaryDataSourceName, replicaDataSourceNames), is(replicaDataSourceName1)); }