X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fentityownership%2FEntityOwnershipListenerSupport.java;h=e944325c69229ade6828dca233043c307b43f6eb;hb=a623206f49e3c376e1a8494ba584ea0018468f12;hp=5220ea29e28e8e74d22d0e45f2727646a896349e;hpb=c38bb5d90ebdb8e867cecc5b0dea0bfafe8ae621;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/EntityOwnershipListenerSupport.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/EntityOwnershipListenerSupport.java index 5220ea29e2..e944325c69 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/EntityOwnershipListenerSupport.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/EntityOwnershipListenerSupport.java @@ -12,11 +12,14 @@ import akka.actor.ActorRef; import akka.actor.PoisonPill; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; +import java.util.Arrays; import java.util.Collection; +import java.util.HashSet; import java.util.IdentityHashMap; import java.util.Map; -import org.opendaylight.controller.cluster.datastore.entityownership.messages.EntityOwnershipChanged; +import java.util.Set; import org.opendaylight.controller.md.sal.common.api.clustering.Entity; +import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange; import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,18 +32,77 @@ import org.slf4j.LoggerFactory; class EntityOwnershipListenerSupport { private static final Logger LOG = LoggerFactory.getLogger(EntityOwnershipListenerSupport.class); + private final String logId; private final ActorContext actorContext; private final Map listenerActorMap = new IdentityHashMap<>(); - private final Multimap entityListenerMap = HashMultimap.create(); + private final Set entitiesWithCandidateSet = new HashSet<>(); + private final Multimap entityTypeListenerMap = HashMultimap.create(); - EntityOwnershipListenerSupport(ActorContext actorContext) { + EntityOwnershipListenerSupport(ActorContext actorContext, String logId) { this.actorContext = actorContext; + this.logId = logId; } - void addEntityOwnershipListener(Entity entity, EntityOwnershipListener listener) { - LOG.debug("Adding EntityOwnershipListener {} for {}", listener, entity); + String getLogId() { + return logId; + } + + boolean hasCandidateForEntity(Entity entity) { + return entitiesWithCandidateSet.contains(entity); + } + + void setHasCandidateForEntity(Entity entity) { + entitiesWithCandidateSet.add(entity); + } + + void unsetHasCandidateForEntity(Entity entity) { + entitiesWithCandidateSet.remove(entity); + } + + void addEntityOwnershipListener(String entityType, EntityOwnershipListener listener) { + LOG.debug("{}: Adding EntityOwnershipListener {} for entity type {}", logId, listener, entityType); + + addListener(listener, entityType, entityTypeListenerMap); + } + + void removeEntityOwnershipListener(String entityType, EntityOwnershipListener listener) { + LOG.debug("{}: Removing EntityOwnershipListener {} for entity type {}", logId, listener, entityType); + + removeListener(listener, entityType, entityTypeListenerMap); + } + + void notifyEntityOwnershipListeners(Entity entity, boolean wasOwner, boolean isOwner, boolean hasOwner) { + notifyListeners(entity, entity.getType(), wasOwner, isOwner, hasOwner, entityTypeListenerMap); + } + + void notifyEntityOwnershipListener(Entity entity, boolean wasOwner, boolean isOwner, boolean hasOwner, + EntityOwnershipListener listener) { + notifyListeners(entity, wasOwner, isOwner, hasOwner, Arrays.asList(listener)); + } + + private void notifyListeners(Entity entity, T mapKey, boolean wasOwner, boolean isOwner, boolean hasOwner, + Multimap listenerMap) { + Collection listeners = listenerMap.get(mapKey); + if(!listeners.isEmpty()) { + notifyListeners(entity, wasOwner, isOwner, hasOwner, listeners); + } + } - if(entityListenerMap.put(entity, listener)) { + private void notifyListeners(Entity entity, boolean wasOwner, boolean isOwner, boolean hasOwner, + Collection listeners) { + EntityOwnershipChange changed = new EntityOwnershipChange(entity, wasOwner, isOwner, hasOwner); + for(EntityOwnershipListener listener: listeners) { + ActorRef listenerActor = listenerActorFor(listener); + + LOG.debug("{}: Notifying EntityOwnershipListenerActor {} with {}", logId, listenerActor, changed); + + listenerActor.tell(changed, ActorRef.noSender()); + } + } + + private void addListener(EntityOwnershipListener listener, T mapKey, + Multimap toListenerMap) { + if(toListenerMap.put(mapKey, listener)) { ListenerActorRefEntry listenerEntry = listenerActorMap.get(listener); if(listenerEntry == null) { listenerActorMap.put(listener, new ListenerActorRefEntry()); @@ -50,13 +112,12 @@ class EntityOwnershipListenerSupport { } } - void removeEntityOwnershipListener(Entity entity, EntityOwnershipListener listener) { - LOG.debug("Removing EntityOwnershipListener {} for {}", listener, entity); - - if(entityListenerMap.remove(entity, listener)) { + private void removeListener(EntityOwnershipListener listener, T mapKey, + Multimap fromListenerMap) { + if(fromListenerMap.remove(mapKey, listener)) { ListenerActorRefEntry listenerEntry = listenerActorMap.get(listener); - LOG.debug("Found {}", listenerEntry); + LOG.debug("{}: Found {}", logId, listenerEntry); listenerEntry.referenceCount--; if(listenerEntry.referenceCount <= 0) { @@ -70,22 +131,6 @@ class EntityOwnershipListenerSupport { } } - void notifyEntityOwnershipListeners(Entity entity, boolean wasOwner, boolean isOwner) { - Collection listeners = entityListenerMap.get(entity); - if(listeners.isEmpty()) { - return; - } - - EntityOwnershipChanged changed = new EntityOwnershipChanged(entity, wasOwner, isOwner); - for(EntityOwnershipListener listener: listeners) { - ActorRef listenerActor = listenerActorFor(listener); - - LOG.debug("Notifying EntityOwnershipListenerActor {} with {}", listenerActor,changed); - - listenerActor.tell(changed, ActorRef.noSender()); - } - } - private ActorRef listenerActorFor(EntityOwnershipListener listener) { return listenerActorMap.get(listener).actorFor(listener); } @@ -98,7 +143,7 @@ class EntityOwnershipListenerSupport { if(actorRef == null) { actorRef = actorContext.actorOf(EntityOwnershipListenerActor.props(listener)); - LOG.debug("Created EntityOwnershipListenerActor {} for listener {}", actorRef, listener); + LOG.debug("{}: Created EntityOwnershipListenerActor {} for listener {}", logId, actorRef, listener); } return actorRef;