2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.entityownership;
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;
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;
61 * The distributed implementation of the EntityOwnershipService.
63 * @author Thomas Pantelis
65 public class DistributedEntityOwnershipService implements DOMEntityOwnershipService, AutoCloseable {
67 static final String ENTITY_OWNERSHIP_SHARD_NAME = "entity-ownership";
69 private static final Logger LOG = LoggerFactory.getLogger(DistributedEntityOwnershipService.class);
70 private static final Timeout MESSAGE_TIMEOUT = new Timeout(1, TimeUnit.MINUTES);
72 private final ConcurrentMap<DOMEntity, DOMEntity> registeredEntities = new ConcurrentHashMap<>();
73 private final ActorUtils context;
75 private volatile ActorRef localEntityOwnershipShard;
76 private volatile DataTree localEntityOwnershipShardDataTree;
78 DistributedEntityOwnershipService(final ActorUtils context) {
79 this.context = requireNonNull(context);
82 public static DistributedEntityOwnershipService start(final ActorUtils context,
83 final EntityOwnerSelectionStrategyConfig strategyConfig) {
84 ActorRef shardManagerActor = context.getShardManager();
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);
92 Future<Object> createFuture = context.executeOperationAsync(shardManagerActor, createShard, MESSAGE_TIMEOUT);
93 createFuture.onComplete(new OnComplete<Object>() {
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);
99 LOG.info("Successfully created {} shard", ENTITY_OWNERSHIP_SHARD_NAME);
102 }, context.getClientDispatcher());
104 return new DistributedEntityOwnershipService(context);
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>() {
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);
116 LOG.debug("{} message to {} succeeded", message, shardActor);
119 }, context.getClientDispatcher());
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>() {
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);
133 localEntityOwnershipShard = shardActor;
134 executeEntityOwnershipShardOperation(localEntityOwnershipShard, message);
137 }, context.getClientDispatcher());
140 executeEntityOwnershipShardOperation(localEntityOwnershipShard, message);
145 public DOMEntityOwnershipCandidateRegistration registerCandidate(final DOMEntity entity)
146 throws CandidateAlreadyRegisteredException {
147 requireNonNull(entity, "entity cannot be null");
149 if (registeredEntities.putIfAbsent(entity, entity) != null) {
150 throw new CandidateAlreadyRegisteredException(entity);
153 RegisterCandidateLocal registerCandidate = new RegisterCandidateLocal(entity);
155 LOG.debug("Registering candidate with message: {}", registerCandidate);
157 executeLocalEntityOwnershipShardOperation(registerCandidate);
158 return new DistributedEntityOwnershipCandidateRegistration(entity, this);
161 void unregisterCandidate(final DOMEntity entity) {
162 LOG.debug("Unregistering candidate for {}", entity);
164 executeLocalEntityOwnershipShardOperation(new UnregisterCandidateLocal(entity));
165 registeredEntities.remove(entity);
169 public DOMEntityOwnershipListenerRegistration registerListener(final String entityType,
170 final DOMEntityOwnershipListener listener) {
171 RegisterListenerLocal registerListener = new RegisterListenerLocal(listener, entityType);
173 LOG.debug("Registering listener with message: {}", registerListener);
175 executeLocalEntityOwnershipShardOperation(registerListener);
176 return new DistributedEntityOwnershipListenerRegistration(listener, entityType, this);
180 public Optional<EntityOwnershipState> getOwnershipState(final DOMEntity forEntity) {
181 requireNonNull(forEntity, "forEntity cannot be null");
183 DataTree dataTree = getLocalEntityOwnershipShardDataTree();
184 if (dataTree == null) {
185 return Optional.empty();
188 Optional<NormalizedNode<?, ?>> entityNode = dataTree.takeSnapshot().readNode(
189 entityPath(forEntity.getType(), forEntity.getIdentifier()));
190 if (!entityNode.isPresent()) {
191 return Optional.empty();
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();
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);
210 return Optional.of(EntityOwnershipState.from(isOwner, hasOwner));
214 public boolean isCandidateRegistered(final DOMEntity entity) {
215 return registeredEntities.get(entity) != null;
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;
227 if (localEntityOwnershipShard == null) {
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);
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);
246 void unregisterListener(final String entityType, final DOMEntityOwnershipListener listener) {
247 LOG.debug("Unregistering listener {} for entity type {}", listener, entityType);
249 executeLocalEntityOwnershipShardOperation(new UnregisterListenerLocal(listener, entityType));
253 public void close() {
256 private static EntityOwnershipShard.Builder newShardBuilder(final ActorUtils context,
257 final EntityOwnerSelectionStrategyConfig strategyConfig) {
258 return EntityOwnershipShard.newBuilder().localMemberName(context.getCurrentMemberName())
259 .ownerSelectionStrategyConfig(strategyConfig);
263 ActorRef getLocalEntityOwnershipShard() {
264 return localEntityOwnershipShard;