Switch default stream output to Magnesium
[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 java.util.Objects.requireNonNull;
11 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.CANDIDATE_NODE_ID;
12 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNER_NODE_ID;
13 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityPath;
14
15 import akka.actor.ActorRef;
16 import akka.dispatch.OnComplete;
17 import akka.pattern.Patterns;
18 import akka.util.Timeout;
19 import com.google.common.annotations.VisibleForTesting;
20 import com.google.common.base.Strings;
21 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
22 import java.util.Collection;
23 import java.util.Optional;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.concurrent.ConcurrentMap;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.TimeoutException;
28 import org.opendaylight.controller.cluster.access.concepts.MemberName;
29 import org.opendaylight.controller.cluster.datastore.config.Configuration;
30 import org.opendaylight.controller.cluster.datastore.config.ModuleShardConfiguration;
31 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal;
32 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterListenerLocal;
33 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal;
34 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterListenerLocal;
35 import org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.EntityOwnerSelectionStrategyConfig;
36 import org.opendaylight.controller.cluster.datastore.messages.CreateShard;
37 import org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree;
38 import org.opendaylight.controller.cluster.datastore.shardstrategy.ModuleShardStrategy;
39 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
40 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
41 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
42 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
43 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipCandidateRegistration;
44 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener;
45 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListenerRegistration;
46 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipService;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.EntityOwners;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
49 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
50 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import scala.concurrent.Await;
57 import scala.concurrent.Future;
58 import scala.concurrent.duration.Duration;
59
60 /**
61  * The distributed implementation of the EntityOwnershipService.
62  *
63  * @author Thomas Pantelis
64  */
65 public class DistributedEntityOwnershipService implements DOMEntityOwnershipService, AutoCloseable {
66     @VisibleForTesting
67     static final String ENTITY_OWNERSHIP_SHARD_NAME = "entity-ownership";
68
69     private static final Logger LOG = LoggerFactory.getLogger(DistributedEntityOwnershipService.class);
70     private static final Timeout MESSAGE_TIMEOUT = new Timeout(1, TimeUnit.MINUTES);
71
72     private final ConcurrentMap<DOMEntity, DOMEntity> registeredEntities = new ConcurrentHashMap<>();
73     private final ActorUtils context;
74
75     private volatile ActorRef localEntityOwnershipShard;
76     private volatile DataTree localEntityOwnershipShardDataTree;
77
78     DistributedEntityOwnershipService(final ActorUtils context) {
79         this.context = requireNonNull(context);
80     }
81
82     public static DistributedEntityOwnershipService start(final ActorUtils context,
83             final EntityOwnerSelectionStrategyConfig strategyConfig) {
84         ActorRef shardManagerActor = context.getShardManager();
85
86         Configuration configuration = context.getConfiguration();
87         Collection<MemberName> entityOwnersMemberNames = configuration.getUniqueMemberNamesForAllShards();
88         CreateShard createShard = new CreateShard(new ModuleShardConfiguration(EntityOwners.QNAME.getNamespace(),
89                 "entity-owners", ENTITY_OWNERSHIP_SHARD_NAME, ModuleShardStrategy.NAME, entityOwnersMemberNames),
90                         newShardBuilder(context, strategyConfig), null);
91
92         Future<Object> createFuture = context.executeOperationAsync(shardManagerActor, createShard, MESSAGE_TIMEOUT);
93         createFuture.onComplete(new OnComplete<Object>() {
94             @Override
95             public void onComplete(final Throwable failure, final Object response) {
96                 if (failure != null) {
97                     LOG.error("Failed to create {} shard", ENTITY_OWNERSHIP_SHARD_NAME, failure);
98                 } else {
99                     LOG.info("Successfully created {} shard", ENTITY_OWNERSHIP_SHARD_NAME);
100                 }
101             }
102         }, context.getClientDispatcher());
103
104         return new DistributedEntityOwnershipService(context);
105     }
106
107     private void executeEntityOwnershipShardOperation(final ActorRef shardActor, final Object message) {
108         Future<Object> future = context.executeOperationAsync(shardActor, message, MESSAGE_TIMEOUT);
109         future.onComplete(new OnComplete<Object>() {
110             @Override
111             public void onComplete(final Throwable failure, final Object response) {
112                 if (failure != null) {
113                     // FIXME: CONTROLLER-1904: reduce the severity to info once we have a retry mechanism
114                     LOG.error("Error sending message {} to {}", message, shardActor, failure);
115                 } else {
116                     LOG.debug("{} message to {} succeeded", message, shardActor);
117                 }
118             }
119         }, context.getClientDispatcher());
120     }
121
122     @VisibleForTesting
123     void executeLocalEntityOwnershipShardOperation(final Object message) {
124         if (localEntityOwnershipShard == null) {
125             Future<ActorRef> future = context.findLocalShardAsync(ENTITY_OWNERSHIP_SHARD_NAME);
126             future.onComplete(new OnComplete<ActorRef>() {
127                 @Override
128                 public void onComplete(final Throwable failure, final ActorRef shardActor) {
129                     if (failure != null) {
130                         // FIXME: CONTROLLER-1904: reduce the severity to info once we have a retry mechanism
131                         LOG.error("Failed to find local {} shard", ENTITY_OWNERSHIP_SHARD_NAME, failure);
132                     } else {
133                         localEntityOwnershipShard = shardActor;
134                         executeEntityOwnershipShardOperation(localEntityOwnershipShard, message);
135                     }
136                 }
137             }, context.getClientDispatcher());
138
139         } else {
140             executeEntityOwnershipShardOperation(localEntityOwnershipShard, message);
141         }
142     }
143
144     @Override
145     public DOMEntityOwnershipCandidateRegistration registerCandidate(final DOMEntity entity)
146             throws CandidateAlreadyRegisteredException {
147         requireNonNull(entity, "entity cannot be null");
148
149         if (registeredEntities.putIfAbsent(entity, entity) != null) {
150             throw new CandidateAlreadyRegisteredException(entity);
151         }
152
153         RegisterCandidateLocal registerCandidate = new RegisterCandidateLocal(entity);
154
155         LOG.debug("Registering candidate with message: {}", registerCandidate);
156
157         executeLocalEntityOwnershipShardOperation(registerCandidate);
158         return new DistributedEntityOwnershipCandidateRegistration(entity, this);
159     }
160
161     void unregisterCandidate(final DOMEntity entity) {
162         LOG.debug("Unregistering candidate for {}", entity);
163
164         executeLocalEntityOwnershipShardOperation(new UnregisterCandidateLocal(entity));
165         registeredEntities.remove(entity);
166     }
167
168     @Override
169     public DOMEntityOwnershipListenerRegistration registerListener(final String entityType,
170             final DOMEntityOwnershipListener listener) {
171         RegisterListenerLocal registerListener = new RegisterListenerLocal(listener, entityType);
172
173         LOG.debug("Registering listener with message: {}", registerListener);
174
175         executeLocalEntityOwnershipShardOperation(registerListener);
176         return new DistributedEntityOwnershipListenerRegistration(listener, entityType, this);
177     }
178
179     @Override
180     public Optional<EntityOwnershipState> getOwnershipState(final DOMEntity forEntity) {
181         requireNonNull(forEntity, "forEntity cannot be null");
182
183         DataTree dataTree = getLocalEntityOwnershipShardDataTree();
184         if (dataTree == null) {
185             return Optional.empty();
186         }
187
188         Optional<NormalizedNode<?, ?>> entityNode = dataTree.takeSnapshot().readNode(
189                 entityPath(forEntity.getType(), forEntity.getIdentifier()));
190         if (!entityNode.isPresent()) {
191             return Optional.empty();
192         }
193
194         // Check if there are any candidates, if there are none we do not really have ownership state
195         final MapEntryNode entity = (MapEntryNode) entityNode.get();
196         final Optional<DataContainerChild<? extends PathArgument, ?>> optionalCandidates =
197                 entity.getChild(CANDIDATE_NODE_ID);
198         final boolean hasCandidates = optionalCandidates.isPresent()
199                 && ((MapNode) optionalCandidates.get()).getValue().size() > 0;
200         if (!hasCandidates) {
201             return Optional.empty();
202         }
203
204         MemberName localMemberName = context.getCurrentMemberName();
205         Optional<DataContainerChild<? extends PathArgument, ?>> ownerLeaf = entity.getChild(ENTITY_OWNER_NODE_ID);
206         String owner = ownerLeaf.isPresent() ? ownerLeaf.get().getValue().toString() : null;
207         boolean hasOwner = !Strings.isNullOrEmpty(owner);
208         boolean isOwner = hasOwner && localMemberName.getName().equals(owner);
209
210         return Optional.of(EntityOwnershipState.from(isOwner, hasOwner));
211     }
212
213     @Override
214     public boolean isCandidateRegistered(final DOMEntity entity) {
215         return registeredEntities.get(entity) != null;
216     }
217
218     @VisibleForTesting
219     @SuppressWarnings("checkstyle:IllegalCatch")
220     @SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "Akka's Await.result() API contract")
221     DataTree getLocalEntityOwnershipShardDataTree() {
222         final DataTree local = localEntityOwnershipShardDataTree;
223         if (local != null) {
224             return local;
225         }
226
227         if (localEntityOwnershipShard == null) {
228             try {
229                 localEntityOwnershipShard = Await.result(context.findLocalShardAsync(
230                     ENTITY_OWNERSHIP_SHARD_NAME), Duration.Inf());
231             } catch (TimeoutException | InterruptedException e) {
232                 LOG.error("Failed to find local {} shard", ENTITY_OWNERSHIP_SHARD_NAME, e);
233                 return null;
234             }
235         }
236
237         try {
238             return localEntityOwnershipShardDataTree = (DataTree) Await.result(Patterns.ask(localEntityOwnershipShard,
239                 GetShardDataTree.INSTANCE, MESSAGE_TIMEOUT), Duration.Inf());
240         } catch (TimeoutException | InterruptedException e) {
241             LOG.error("Failed to find local {} shard", ENTITY_OWNERSHIP_SHARD_NAME, e);
242             return null;
243         }
244     }
245
246     void unregisterListener(final String entityType, final DOMEntityOwnershipListener listener) {
247         LOG.debug("Unregistering listener {} for entity type {}", listener, entityType);
248
249         executeLocalEntityOwnershipShardOperation(new UnregisterListenerLocal(listener, entityType));
250     }
251
252     @Override
253     public void close() {
254     }
255
256     private static EntityOwnershipShard.Builder newShardBuilder(final ActorUtils context,
257             final EntityOwnerSelectionStrategyConfig strategyConfig) {
258         return EntityOwnershipShard.newBuilder().localMemberName(context.getCurrentMemberName())
259                 .ownerSelectionStrategyConfig(strategyConfig);
260     }
261
262     @VisibleForTesting
263     ActorRef getLocalEntityOwnershipShard() {
264         return localEntityOwnershipShard;
265     }
266 }