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 java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.ConcurrentMap;
16 import java.util.concurrent.TimeUnit;
17 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
18 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal;
19 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal;
20 import org.opendaylight.controller.cluster.datastore.messages.CreateShard;
21 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
22 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
23 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidate;
24 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
25 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
26 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
27 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import scala.concurrent.Future;
33 * The distributed implementation of the EntityOwnershipService.
35 * @author Thomas Pantelis
37 public class DistributedEntityOwnershipService implements EntityOwnershipService, AutoCloseable {
38 private static final Logger LOG = LoggerFactory.getLogger(DistributedEntityOwnershipService.class);
39 static final String ENTITY_OWNERSHIP_SHARD_NAME = "entity-ownership";
40 private static final Timeout MESSAGE_TIMEOUT = new Timeout(1, TimeUnit.MINUTES);
42 private final DistributedDataStore datastore;
43 private final ConcurrentMap<Entity, EntityOwnershipCandidate> registeredEntities = new ConcurrentHashMap<>();
44 private volatile ActorRef localEntityOwnershipShard;
46 public DistributedEntityOwnershipService(DistributedDataStore datastore) {
47 this.datastore = datastore;
51 ActorRef shardManagerActor = datastore.getActorContext().getShardManager();
53 CreateShard createShard = new CreateShard(ENTITY_OWNERSHIP_SHARD_NAME,
54 datastore.getActorContext().getConfiguration().getUniqueMemberNamesForAllShards(),
55 newShardPropsCreator(), null);
57 Future<Object> createFuture = datastore.getActorContext().executeOperationAsync(shardManagerActor,
58 createShard, MESSAGE_TIMEOUT);
60 createFuture.onComplete(new OnComplete<Object>() {
62 public void onComplete(Throwable failure, Object response) {
64 LOG.error("Failed to create {} shard", ENTITY_OWNERSHIP_SHARD_NAME);
66 LOG.info("Successfully created {} shard", ENTITY_OWNERSHIP_SHARD_NAME);
69 }, datastore.getActorContext().getClientDispatcher());
72 private void executeEntityOwnershipShardOperation(final ActorRef shardActor, final Object message) {
73 Future<Object> future = datastore.getActorContext().executeOperationAsync(shardActor, message, MESSAGE_TIMEOUT);
74 future.onComplete(new OnComplete<Object>() {
76 public void onComplete(Throwable failure, Object response) {
78 LOG.debug("Error sending message {} to {}", message, shardActor, failure);
79 // TODO - queue for retry
81 LOG.debug("{} message to {} succeeded", message, shardActor, failure);
84 }, datastore.getActorContext().getClientDispatcher());
87 private void executeLocalEntityOwnershipShardOperation(final Object message) {
88 if(localEntityOwnershipShard == null) {
89 Future<ActorRef> future = datastore.getActorContext().findLocalShardAsync(ENTITY_OWNERSHIP_SHARD_NAME);
90 future.onComplete(new OnComplete<ActorRef>() {
92 public void onComplete(Throwable failure, ActorRef shardActor) {
94 LOG.error("Failed to find local {} shard", ENTITY_OWNERSHIP_SHARD_NAME, failure);
96 localEntityOwnershipShard = shardActor;
97 executeEntityOwnershipShardOperation(localEntityOwnershipShard, message);
100 }, datastore.getActorContext().getClientDispatcher());
103 executeEntityOwnershipShardOperation(localEntityOwnershipShard, message);
108 public EntityOwnershipCandidateRegistration registerCandidate(Entity entity, EntityOwnershipCandidate candidate)
109 throws CandidateAlreadyRegisteredException {
111 EntityOwnershipCandidate currentCandidate = registeredEntities.putIfAbsent(entity, candidate);
112 if(currentCandidate != null) {
113 throw new CandidateAlreadyRegisteredException(entity, currentCandidate);
116 RegisterCandidateLocal registerCandidate = new RegisterCandidateLocal(candidate, entity);
118 LOG.debug("Registering candidate with message: {}", registerCandidate);
120 executeLocalEntityOwnershipShardOperation(registerCandidate);
121 return new DistributedEntityOwnershipCandidateRegistration(candidate, entity, this);
124 void unregisterCandidate(Entity entity) {
125 LOG.debug("Unregistering candidate for {}", entity);
127 executeLocalEntityOwnershipShardOperation(new UnregisterCandidateLocal(entity));
128 registeredEntities.remove(entity);
132 public EntityOwnershipListenerRegistration registerListener(Entity entity, EntityOwnershipListener listener) {
133 // TODO Auto-generated method stub
138 public void close() {
141 protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
142 return new EntityOwnershipShardPropsCreator(datastore.getActorContext().getCurrentMemberName());
146 ActorRef getLocalEntityOwnershipShard() {
147 return localEntityOwnershipShard;