This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
forked from hector-client/hector
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zznate
committed
Oct 14, 2010
1 parent
da75136
commit a27dd36
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/main/java/me/prettyprint/cassandra/connection/RoundRobinBalancingPolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package me.prettyprint.cassandra.connection; | ||
|
||
import java.util.Collection; | ||
import java.util.Set; | ||
|
||
import org.cliffc.high_scale_lib.Counter; | ||
|
||
import me.prettyprint.cassandra.service.CassandraHost; | ||
|
||
public class RoundRobinBalancingPolicy implements LoadBalancingPolicy { | ||
|
||
private final Counter counter; | ||
|
||
public RoundRobinBalancingPolicy() { | ||
counter = new Counter(); | ||
counter.set(0); | ||
} | ||
|
||
@Override | ||
public ConcurrentHClientPool getPool(Collection<ConcurrentHClientPool> pools, | ||
Set<CassandraHost> excludeHosts) { | ||
ConcurrentHClientPool[] pa = pools.toArray(new ConcurrentHClientPool[pools.size()]); | ||
int location = getAndIncrement(pa.length); | ||
ConcurrentHClientPool pool = pa[location]; | ||
if ( excludeHosts != null && excludeHosts.size() > 0 ) { | ||
while ( excludeHosts.contains(pool.getCassandraHost()) ) { | ||
pool = pa[++location == pa.length ? 0 : location]; | ||
} | ||
} | ||
return pool; | ||
} | ||
|
||
private int getAndIncrement(int size) { | ||
int val = (int)(counter.get() % size); | ||
counter.increment(); | ||
return val; | ||
} | ||
|
||
} |