BUG 5656 : Entity ownership candidates not removed consistently on leadership change
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / entityownership / DistributedEntityOwnershipService.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.datastore.entityownership;
9
10 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.CANDIDATE_NODE_ID;
11 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNER_NODE_ID;
12 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityPath;
13 import akka.actor.ActorRef;
14 import akka.dispatch.OnComplete;
15 import akka.pattern.Patterns;
16 import akka.util.Timeout;
17 import com.google.common.annotations.VisibleForTesting;
18 import com.google.common.base.Optional;
19 import com.google.common.base.Preconditions;
20 import com.google.common.base.Strings;
21 import java.util.Collection;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.ConcurrentMap;
24 import java.util.concurrent.TimeUnit;
25 import javax.annotation.Nonnull;
26 import org.opendaylight.controller.cluster.datastore.config.Configuration;
27 import org.opendaylight.controller.cluster.datastore.config.ModuleShardConfiguration;
28 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal;
29 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterListenerLocal;
30 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal;
31 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterListenerLocal;
32 import org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.EntityOwnerSelectionStrategyConfig;
33 import org.opendaylight.controller.cluster.datastore.messages.CreateShard;
34 import org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree;
35 import org.opendaylight.controller.cluster.datastore.shardstrategy.ModuleShardStrategy;
36 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
37 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
38 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
39 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
40 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
41 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
42 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
43 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.EntityOwners;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
46 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
47 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import scala.concurrent.Await;
54 import scala.concurrent.Future;
55 import scala.concurrent.duration.Duration;
56
57 /**
58  * The distributed implementation of the EntityOwnershipService.
59  *
60  * @author Thomas Pantelis
61  */
62 public class DistributedEntityOwnershipService implements EntityOwnershipService, AutoCloseable {
63     @VisibleForTesting
64     static final String ENTITY_OWNERSHIP_SHARD_NAME = "entity-ownership";
65
66     private static final Logger LOG = LoggerFactory.getLogger(DistributedEntityOwnershipService.class);
67     private static final Timeout MESSAGE_TIMEOUT = new Timeout(1, TimeUnit.MINUTES);
68
69     private final ConcurrentMap<Entity, Entity> registeredEntities = new ConcurrentHashMap<>();
70     private final ActorContext context;
71
72     private volatile ActorRef localEntityOwnershipShard;
73     private volatile DataTree localEntityOwnershipShardDataTree;
74
75     private DistributedEntityOwnershipService(final ActorContext context) {
76         this.context = Preconditions.checkNotNull(context);
77     }
78
79     public static DistributedEntityOwnershipService start(final ActorContext context,
80             final EntityOwnerSelectionStrategyConfig strategyConfig) {
81         ActorRef shardManagerActor = context.getShardManager();
82
83         Configuration configuration = context.getConfiguration();
84         Collection<String> entityOwnersMemberNames = configuration.getUniqueMemberNamesForAllShards();
85         CreateShard createShard = new CreateShard(new ModuleShardConfiguration(EntityOwners.QNAME.getNamespace(),
86                 "entity-owners", ENTITY_OWNERSHIP_SHARD_NAME, ModuleShardStrategy.NAME, entityOwnersMemberNames),
87                         newShardBuilder(context, strategyConfig), null);
88
89         Future<Object> createFuture = context.executeOperationAsync(shardManagerActor,
90                 createShard, MESSAGE_TIMEOUT);
91
92         createFuture.onComplete(new OnComplete<Object>() {
93             @Override
94             public void onComplete(final Throwable failure, final Object response) {
95                 if (failure != null) {
96                     LOG.error("Failed to create {} shard", ENTITY_OWNERSHIP_SHARD_NAME, failure);
97                 } else {
98                     LOG.info("Successfully created {} shard", ENTITY_OWNERSHIP_SHARD_NAME);
99                 }
100             }
101         }, context.getClientDispatcher());
102
103         return new DistributedEntityOwnershipService(context);
104     }
105
106     private void executeEntityOwnershipShardOperation(final ActorRef shardActor, final Object message) {
107         Future<Object> future = context.executeOperationAsync(shardActor, message, MESSAGE_TIMEOUT);
108         future.onComplete(new OnComplete<Object>() {
109             @Override
110             public void onComplete(final Throwable failure, final Object response) {
111                 if(failure != null) {
112                     LOG.debug("Error sending message {} to {}", message, shardActor, failure);
113                 } else {
114                     LOG.debug("{} message to {} succeeded", message, shardActor, failure);
115                 }
116             }
117         }, context.getClientDispatcher());
118     }
119
120     @VisibleForTesting
121     void executeLocalEntityOwnershipShardOperation(final Object message) {
122         if(localEntityOwnershipShard == null) {
123             Future<ActorRef> future = context.findLocalShardAsync(ENTITY_OWNERSHIP_SHARD_NAME);
124             future.onComplete(new OnComplete<ActorRef>() {
125                 @Override
126                 public void onComplete(final Throwable failure, final ActorRef shardActor) {
127                     if(failure != null) {
128                         LOG.error("Failed to find local {} shard", ENTITY_OWNERSHIP_SHARD_NAME, failure);
129                     } else {
130                         localEntityOwnershipShard = shardActor;
131                         executeEntityOwnershipShardOperation(localEntityOwnershipShard, message);
132                     }
133                 }
134             }, context.getClientDispatcher());
135
136         } else {
137             executeEntityOwnershipShardOperation(localEntityOwnershipShard, message);
138         }
139     }
140
141     @Override
142     public EntityOwnershipCandidateRegistration registerCandidate(final Entity entity)
143             throws CandidateAlreadyRegisteredException {
144         Preconditions.checkNotNull(entity, "entity cannot be null");
145
146         if(registeredEntities.putIfAbsent(entity, entity) != null) {
147             throw new CandidateAlreadyRegisteredException(entity);
148         }
149
150         RegisterCandidateLocal registerCandidate = new RegisterCandidateLocal(entity);
151
152         LOG.debug("Registering candidate with message: {}", registerCandidate);
153
154         executeLocalEntityOwnershipShardOperation(registerCandidate);
155         return new DistributedEntityOwnershipCandidateRegistration(entity, this);
156     }
157
158     void unregisterCandidate(final Entity entity) {
159         LOG.debug("Unregistering candidate for {}", entity);
160
161         executeLocalEntityOwnershipShardOperation(new UnregisterCandidateLocal(entity));
162         registeredEntities.remove(entity);
163     }
164
165     @Override
166     public EntityOwnershipListenerRegistration registerListener(final String entityType, final EntityOwnershipListener listener) {
167         Preconditions.checkNotNull(entityType, "entityType cannot be null");
168         Preconditions.checkNotNull(listener, "listener cannot be null");
169
170         RegisterListenerLocal registerListener = new RegisterListenerLocal(listener, entityType);
171
172         LOG.debug("Registering listener with message: {}", registerListener);
173
174         executeLocalEntityOwnershipShardOperation(registerListener);
175         return new DistributedEntityOwnershipListenerRegistration(listener, entityType, this);
176     }
177
178     @Override
179     public Optional<EntityOwnershipState> getOwnershipState(final Entity forEntity) {
180         Preconditions.checkNotNull(forEntity, "forEntity cannot be null");
181
182         DataTree dataTree = getLocalEntityOwnershipShardDataTree();
183         if (dataTree == null) {
184             return Optional.absent();
185         }
186
187         Optional<NormalizedNode<?, ?>> entityNode = dataTree.takeSnapshot().readNode(
188                 entityPath(forEntity.getType(), forEntity.getId()));
189         if (!entityNode.isPresent()) {
190             return Optional.absent();
191         }
192
193         // Check if there are any candidates, if there are none we do not really have ownership state
194         final MapEntryNode entity = (MapEntryNode) entityNode.get();
195         final Optional<DataContainerChild<? extends PathArgument, ?>> optionalCandidates = entity.getChild(CANDIDATE_NODE_ID);
196         final boolean hasCandidates = optionalCandidates.isPresent() && ((MapNode) optionalCandidates.get()).getValue().size() > 0;
197         if(!hasCandidates){
198             return Optional.absent();
199         }
200
201         String localMemberName = context.getCurrentMemberName();
202         Optional<DataContainerChild<? extends PathArgument, ?>> ownerLeaf = entity.getChild(ENTITY_OWNER_NODE_ID);
203         String owner = ownerLeaf.isPresent() ? ownerLeaf.get().getValue().toString() : null;
204         boolean hasOwner = !Strings.isNullOrEmpty(owner);
205         boolean isOwner = hasOwner && localMemberName.equals(owner);
206
207         return Optional.of(new EntityOwnershipState(isOwner, hasOwner));
208     }
209
210     @Override
211     public boolean isCandidateRegistered(@Nonnull final Entity entity) {
212         return registeredEntities.get(entity) != null;
213     }
214
215     @VisibleForTesting
216     DataTree getLocalEntityOwnershipShardDataTree() {
217         if (localEntityOwnershipShardDataTree == null) {
218             try {
219                 if(localEntityOwnershipShard == null) {
220                     localEntityOwnershipShard = Await.result(context.findLocalShardAsync(
221                             ENTITY_OWNERSHIP_SHARD_NAME), Duration.Inf());
222                 }
223
224                 localEntityOwnershipShardDataTree = (DataTree) Await.result(Patterns.ask(localEntityOwnershipShard,
225                         GetShardDataTree.INSTANCE, MESSAGE_TIMEOUT), Duration.Inf());
226             } catch (Exception e) {
227                 LOG.error("Failed to find local {} shard", ENTITY_OWNERSHIP_SHARD_NAME, e);
228             }
229         }
230
231         return localEntityOwnershipShardDataTree;
232     }
233
234     void unregisterListener(final String entityType, final EntityOwnershipListener listener) {
235         LOG.debug("Unregistering listener {} for entity type {}", listener, entityType);
236
237         executeLocalEntityOwnershipShardOperation(new UnregisterListenerLocal(listener, entityType));
238     }
239
240     @Override
241     public void close() {
242     }
243
244     private static EntityOwnershipShard.Builder newShardBuilder(final ActorContext context,
245             final EntityOwnerSelectionStrategyConfig strategyConfig) {
246         return EntityOwnershipShard.newBuilder().localMemberName(context.getCurrentMemberName())
247                 .ownerSelectionStrategyConfig(strategyConfig);
248     }
249
250     @VisibleForTesting
251     ActorRef getLocalEntityOwnershipShard() {
252         return localEntityOwnershipShard;
253     }
254 }