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 akka.actor.ActorRef;
11 import akka.dispatch.OnComplete;
12 import akka.util.Timeout;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.base.Preconditions;
15 import java.util.Collection;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.ConcurrentMap;
18 import java.util.concurrent.TimeUnit;
19 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
20 import org.opendaylight.controller.cluster.datastore.config.Configuration;
21 import org.opendaylight.controller.cluster.datastore.config.ModuleShardConfiguration;
22 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal;
23 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterListenerLocal;
24 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal;
25 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterListenerLocal;
26 import org.opendaylight.controller.cluster.datastore.messages.CreateShard;
27 import org.opendaylight.controller.cluster.datastore.shardstrategy.ModuleShardStrategy;
28 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
29 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
30 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidate;
31 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
32 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
33 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
34 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.EntityOwners;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import scala.concurrent.Future;
41 * The distributed implementation of the EntityOwnershipService.
43 * @author Thomas Pantelis
45 public class DistributedEntityOwnershipService implements EntityOwnershipService, AutoCloseable {
46 private static final Logger LOG = LoggerFactory.getLogger(DistributedEntityOwnershipService.class);
47 static final String ENTITY_OWNERSHIP_SHARD_NAME = "entity-ownership";
48 private static final Timeout MESSAGE_TIMEOUT = new Timeout(1, TimeUnit.MINUTES);
50 private final DistributedDataStore datastore;
51 private final ConcurrentMap<Entity, EntityOwnershipCandidate> registeredEntities = new ConcurrentHashMap<>();
52 private volatile ActorRef localEntityOwnershipShard;
54 public DistributedEntityOwnershipService(DistributedDataStore datastore) {
55 this.datastore = datastore;
59 ActorRef shardManagerActor = datastore.getActorContext().getShardManager();
61 Configuration configuration = datastore.getActorContext().getConfiguration();
62 Collection<String> entityOwnersMemberNames = configuration.getUniqueMemberNamesForAllShards();
63 CreateShard createShard = new CreateShard(new ModuleShardConfiguration(EntityOwners.QNAME.getNamespace(),
64 "entity-owners", ENTITY_OWNERSHIP_SHARD_NAME, ModuleShardStrategy.NAME, entityOwnersMemberNames),
65 newShardPropsCreator(), null);
67 Future<Object> createFuture = datastore.getActorContext().executeOperationAsync(shardManagerActor,
68 createShard, MESSAGE_TIMEOUT);
70 createFuture.onComplete(new OnComplete<Object>() {
72 public void onComplete(Throwable failure, Object response) {
74 LOG.error("Failed to create {} shard", ENTITY_OWNERSHIP_SHARD_NAME);
76 LOG.info("Successfully created {} shard", ENTITY_OWNERSHIP_SHARD_NAME);
79 }, datastore.getActorContext().getClientDispatcher());
82 private void executeEntityOwnershipShardOperation(final ActorRef shardActor, final Object message) {
83 Future<Object> future = datastore.getActorContext().executeOperationAsync(shardActor, message, MESSAGE_TIMEOUT);
84 future.onComplete(new OnComplete<Object>() {
86 public void onComplete(Throwable failure, Object response) {
88 LOG.debug("Error sending message {} to {}", message, shardActor, failure);
90 LOG.debug("{} message to {} succeeded", message, shardActor, failure);
93 }, datastore.getActorContext().getClientDispatcher());
96 private void executeLocalEntityOwnershipShardOperation(final Object message) {
97 if(localEntityOwnershipShard == null) {
98 Future<ActorRef> future = datastore.getActorContext().findLocalShardAsync(ENTITY_OWNERSHIP_SHARD_NAME);
99 future.onComplete(new OnComplete<ActorRef>() {
101 public void onComplete(Throwable failure, ActorRef shardActor) {
102 if(failure != null) {
103 LOG.error("Failed to find local {} shard", ENTITY_OWNERSHIP_SHARD_NAME, failure);
105 localEntityOwnershipShard = shardActor;
106 executeEntityOwnershipShardOperation(localEntityOwnershipShard, message);
109 }, datastore.getActorContext().getClientDispatcher());
112 executeEntityOwnershipShardOperation(localEntityOwnershipShard, message);
117 public EntityOwnershipCandidateRegistration registerCandidate(Entity entity, EntityOwnershipCandidate candidate)
118 throws CandidateAlreadyRegisteredException {
119 Preconditions.checkNotNull(entity, "entity cannot be null");
120 Preconditions.checkNotNull(candidate, "candidate cannot be null");
122 EntityOwnershipCandidate currentCandidate = registeredEntities.putIfAbsent(entity, candidate);
123 if(currentCandidate != null) {
124 throw new CandidateAlreadyRegisteredException(entity, currentCandidate);
127 RegisterCandidateLocal registerCandidate = new RegisterCandidateLocal(candidate, entity);
129 LOG.debug("Registering candidate with message: {}", registerCandidate);
131 executeLocalEntityOwnershipShardOperation(registerCandidate);
132 return new DistributedEntityOwnershipCandidateRegistration(candidate, entity, this);
135 void unregisterCandidate(Entity entity, EntityOwnershipCandidate entityOwnershipCandidate) {
136 LOG.debug("Unregistering candidate {} for {}", entityOwnershipCandidate, entity);
138 executeLocalEntityOwnershipShardOperation(new UnregisterCandidateLocal(entityOwnershipCandidate, entity));
139 registeredEntities.remove(entity);
143 public EntityOwnershipListenerRegistration registerListener(String entityType, EntityOwnershipListener listener) {
144 Preconditions.checkNotNull(entityType, "entityType cannot be null");
145 Preconditions.checkNotNull(listener, "listener cannot be null");
147 RegisterListenerLocal registerListener = new RegisterListenerLocal(listener, entityType);
149 LOG.debug("Registering listener with message: {}", registerListener);
151 executeLocalEntityOwnershipShardOperation(registerListener);
152 return new DistributedEntityOwnershipListenerRegistration(listener, entityType, this);
155 void unregisterListener(String entityType, EntityOwnershipListener listener) {
156 LOG.debug("Unregistering listener {} for entity type {}", listener, entityType);
158 executeLocalEntityOwnershipShardOperation(new UnregisterListenerLocal(listener, entityType));
162 public void close() {
165 protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
166 return new EntityOwnershipShardPropsCreator(datastore.getActorContext().getCurrentMemberName());
170 ActorRef getLocalEntityOwnershipShard() {
171 return localEntityOwnershipShard;