com.netflix.astyanax.connectionpool.impl.AbstractTopology Maven / Gradle / Ivy
package com.netflix.astyanax.connectionpool.impl;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.netflix.astyanax.connectionpool.HostConnectionPool;
import com.netflix.astyanax.connectionpool.LatencyScoreStrategy;
public class AbstractTopology implements Topology {
/**
* Partition which contains all hosts. This is the fallback partition when no tokens are provided.
*/
private TokenHostConnectionPoolPartition allPools;
/**
* Strategy used to score hosts within a partition.
*/
private LatencyScoreStrategy strategy;
public AbstractTopology(LatencyScoreStrategy strategy) {
this.strategy = strategy;
this.allPools = new TokenHostConnectionPoolPartition(null, this.strategy);
}
@SuppressWarnings("unchecked")
@Override
/**
* Update the list of pools using the provided mapping of start token to collection of hosts
* that own the token
*/
public synchronized boolean setPools(Collection> ring) {
boolean didChange = false;
Set> allPools = Sets.newHashSet();
// Create a mapping of end token to a list of hosts that own the token
for (HostConnectionPool pool : ring) {
allPools.add(pool);
if (!this.allPools.hasPool(pool))
didChange = true;
}
return didChange;
}
@Override
public synchronized void resumePool(HostConnectionPool pool) {
refresh();
}
@Override
public synchronized void suspendPool(HostConnectionPool pool) {
refresh();
}
@Override
public synchronized void refresh() {
allPools.refresh();
}
@Override
public TokenHostConnectionPoolPartition getPartition(ByteBuffer rowkey) {
return getAllPools();
}
@Override
public TokenHostConnectionPoolPartition getAllPools() {
return allPools;
}
@Override
public int getPartitionCount() {
return 1;
}
@Override
public synchronized void removePool(HostConnectionPool pool) {
allPools.removePool(pool);
refresh();
}
@Override
public synchronized void addPool(HostConnectionPool pool) {
allPools.addPool(pool);
allPools.refresh();
}
@Override
public List getPartitionNames() {
return Lists.newArrayList(allPools.id().toString());
}
@Override
public TokenHostConnectionPoolPartition getPartition(String token) {
return allPools;
}
@Override
public Map> getPartitions() {
return ImmutableMap.of(allPools.id().toString(), allPools);
}
}