// remove it from the downPeerMemberNames.
downPeerMemberNames.remove(message.getNewCandidate());
- String currentOwner = getCurrentOwner(message.getEntityPath());
- EntityOwnerSelectionStrategy strategy = getEntityOwnerElectionStrategy(message.getEntityPath());
+ final String currentOwner = getCurrentOwner(message.getEntityPath());
+ final EntityOwnerSelectionStrategy strategy = getEntityOwnerElectionStrategy(message.getEntityPath());
+ final String entityType = EntityOwnersModel.entityTypeFromEntityPath(message.getEntityPath());
+
+ // Available members is all the known peers - the number of peers that are down + self
+ // So if there are 2 peers and 1 is down then availableMembers will be 2
+ final int availableMembers = (peerIdToMemberNames.size() - downPeerMemberNames.size()) + 1;
LOG.debug("{}: Using strategy {} to select owner", persistenceId(), strategy);
if(Strings.isNullOrEmpty(currentOwner)){
if(strategy.getSelectionDelayInMillis() == 0L) {
- String entityType = EntityOwnersModel.entityTypeFromEntityPath(message.getEntityPath());
+ writeNewOwner(message.getEntityPath(), newOwner(message.getAllCandidates(),
+ entityOwnershipStatistics.byEntityType(entityType), strategy));
+ } else if(message.getAllCandidates().size() == availableMembers) {
+ LOG.debug("{}: Received the maximum candidates requests : {} writing new owner",
+ persistenceId(), availableMembers);
+ cancelOwnerSelectionTask(message.getEntityPath());
writeNewOwner(message.getEntityPath(), newOwner(message.getAllCandidates(),
entityOwnershipStatistics.byEntityType(entityType), strategy));
} else {
*/
public void scheduleOwnerSelection(YangInstanceIdentifier entityPath, Collection<String> allCandidates,
EntityOwnerSelectionStrategy strategy){
- Cancellable lastScheduledTask = entityToScheduledOwnershipTask.get(entityPath);
- if(lastScheduledTask != null && !lastScheduledTask.isCancelled()){
- lastScheduledTask.cancel();
- }
+ cancelOwnerSelectionTask(entityPath);
LOG.debug("{}: Scheduling owner selection after {} ms", persistenceId(), strategy.getSelectionDelayInMillis());
- lastScheduledTask = context().system().scheduler().scheduleOnce(
+ final Cancellable lastScheduledTask = context().system().scheduler().scheduleOnce(
FiniteDuration.apply(strategy.getSelectionDelayInMillis(), TimeUnit.MILLISECONDS)
, self(), new SelectOwner(entityPath, allCandidates, strategy)
, context().system().dispatcher(), self());
entityToScheduledOwnershipTask.put(entityPath, lastScheduledTask);
}
+ private void cancelOwnerSelectionTask(YangInstanceIdentifier entityPath){
+ final Cancellable lastScheduledTask = entityToScheduledOwnershipTask.get(entityPath);
+ if(lastScheduledTask != null && !lastScheduledTask.isCancelled()){
+ lastScheduledTask.cancel();
+ }
+ }
+
private String newOwner(Collection<String> candidates, Map<String, Long> statistics, EntityOwnerSelectionStrategy ownerSelectionStrategy) {
Collection<String> viableCandidates = getViableCandidates(candidates);
if(viableCandidates.size() == 0){
package org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy;
+import com.google.common.base.MoreObjects;
import java.util.Collection;
+import java.util.HashMap;
import java.util.Map;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* The LeastLoadedCandidateSelectionStrategy assigns ownership for an entity to the candidate which owns the least
* number of entities.
*/
public class LeastLoadedCandidateSelectionStrategy extends AbstractEntityOwnerSelectionStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(LeastLoadedCandidateSelectionStrategy.class);
+
+ private Map<String, Long> localStatistics = new HashMap<>();
+
protected LeastLoadedCandidateSelectionStrategy(long selectionDelayInMillis) {
super(selectionDelayInMillis);
}
long leastLoadedCount = Long.MAX_VALUE;
for(String candidateName : viableCandidates){
- Long val = statistics.get(candidateName);
- if(val != null && val < leastLoadedCount){
+ long val = MoreObjects.firstNonNull(statistics.get(candidateName), 0L);
+ long localVal = MoreObjects.firstNonNull(localStatistics.get(candidateName), 0L);
+ if(val < localVal){
+ LOG.debug("Local statistic higher - Candidate : {}, local statistic : {}, provided statistic : {}",
+ candidateName, localVal, val);
+ val = localVal;
+ } else {
+ LOG.debug("Provided statistic higher - Candidate : {}, local statistic : {}, provided statistic : {}",
+ candidateName, localVal, val);
+ localStatistics.put(candidateName, val);
+ }
+ if(val < leastLoadedCount){
leastLoadedCount = val;
leastLoadedCandidate = candidateName;
}
}
if(leastLoadedCandidate == null){
- return viableCandidates.iterator().next();
+ leastLoadedCandidate = viableCandidates.iterator().next();
}
+
+ localStatistics.put(leastLoadedCandidate, leastLoadedCount + 1);
return leastLoadedCandidate;
}
}
LOCAL_MEMBER_NAME, strategyConfig);
}
+ private Props newShardProps(EntityOwnerSelectionStrategyConfig strategyConfig, Map<String, String> peers) {
+ return newShardProps(newShardId(LOCAL_MEMBER_NAME), peers, LOCAL_MEMBER_NAME, strategyConfig);
+ }
+
+
private Props newShardProps(Map<String,String> peers) {
return newShardProps(newShardId(LOCAL_MEMBER_NAME), peers, LOCAL_MEMBER_NAME, EntityOwnerSelectionStrategyConfig.newBuilder().build());
}
@Test
- public void testDelayedEntityOwnerSelection() throws Exception {
+ public void testDelayedEntityOwnerSelectionWhenMaxPeerRequestsReceived() throws Exception {
ShardTestKit kit = new ShardTestKit(getSystem());
EntityOwnerSelectionStrategyConfig.Builder builder
= EntityOwnerSelectionStrategyConfig.newBuilder().addStrategy(ENTITY_TYPE, LastCandidateSelectionStrategy.class, 500);
- TestActorRef<EntityOwnershipShard> shard = actorFactory.createTestActor(newShardProps(builder.build()));
+
+ String peerId = newShardId("follower").toString();
+ TestActorRef<MockFollower> peer = actorFactory.createTestActor(Props.create(MockFollower.class, peerId, false).
+ withDispatcher(Dispatchers.DefaultDispatcherId()), peerId);
+
+ peer.underlyingActor().grantVote = true;
+
+ TestActorRef<EntityOwnershipShard> shard = actorFactory.createTestActor(newShardProps(builder.build(),
+ ImmutableMap.of(peerId.toString(), peer.path().toString())));
kit.waitUntilLeader(shard);
Entity entity = new Entity(ENTITY_TYPE, ENTITY_ID1);
// Add a remote candidate
- String remoteMemberName1 = "remoteMember1";
+ String remoteMemberName1 = "follower";
writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, remoteMemberName1), shardDataTree);
+ // Register local
+
+ shard.tell(new RegisterCandidateLocal(entity), kit.getRef());
+ kit.expectMsgClass(SuccessReply.class);
+
+ // Verify the local candidate becomes owner
+
+ verifyCommittedEntityCandidate(shard, ENTITY_TYPE, ENTITY_ID1, remoteMemberName1);
+ verifyCommittedEntityCandidate(shard, ENTITY_TYPE, ENTITY_ID1, LOCAL_MEMBER_NAME);
+ verifyOwner(shard, ENTITY_TYPE, ENTITY_ID1, LOCAL_MEMBER_NAME);
+ }
+
+ @Test
+ public void testDelayedEntityOwnerSelection() throws Exception {
+ ShardTestKit kit = new ShardTestKit(getSystem());
+ EntityOwnerSelectionStrategyConfig.Builder builder
+ = EntityOwnerSelectionStrategyConfig.newBuilder().addStrategy(ENTITY_TYPE, LastCandidateSelectionStrategy.class, 500);
+
+ String follower1Id = newShardId("follower1").toString();
+ TestActorRef<MockFollower> follower1 = actorFactory.createTestActor(Props.create(MockFollower.class, follower1Id, false).
+ withDispatcher(Dispatchers.DefaultDispatcherId()), follower1Id);
+
+ follower1.underlyingActor().grantVote = true;
+
+ String follower2Id = newShardId("follower").toString();
+ TestActorRef<MockFollower> follower2 = actorFactory.createTestActor(Props.create(MockFollower.class, follower2Id, false).
+ withDispatcher(Dispatchers.DefaultDispatcherId()), follower2Id);
+
+ follower2.underlyingActor().grantVote = true;
+
+
+ TestActorRef<EntityOwnershipShard> shard = actorFactory.createTestActor(newShardProps(builder.build(),
+ ImmutableMap.of(follower1Id.toString(), follower2.path().toString(), follower2Id.toString(), follower2.path().toString())));
+ kit.waitUntilLeader(shard);
+
+ Entity entity = new Entity(ENTITY_TYPE, ENTITY_ID1);
+ ShardDataTree shardDataTree = shard.underlyingActor().getDataStore();
+
+ // Add a remote candidate
+
+ String remoteMemberName1 = "follower";
+ writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, remoteMemberName1), shardDataTree);
// Register local