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 java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15 import java.util.concurrent.TimeUnit;
16 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
17 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal;
18 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal;
19 import org.opendaylight.controller.cluster.datastore.messages.CreateShard;
20 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
21 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidate;
23 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
24 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
25 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
26 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import scala.concurrent.Future;
32 * The distributed implementation of the EntityOwnershipService.
34 * @author Thomas Pantelis
36 public class DistributedEntityOwnershipService implements EntityOwnershipService, AutoCloseable {
37 private static final Logger LOG = LoggerFactory.getLogger(DistributedEntityOwnershipService.class);
38 static final String ENTITY_OWNERSHIP_SHARD_NAME = "entity-ownership";
39 private static final Timeout MESSAGE_TIMEOUT = new Timeout(1, TimeUnit.MINUTES);
41 private final DistributedDataStore datastore;
42 private final ConcurrentMap<Entity, EntityOwnershipCandidate> registeredEntities = new ConcurrentHashMap<>();
43 private volatile ActorRef localEntityOwnershipShard;
45 public DistributedEntityOwnershipService(DistributedDataStore datastore) {
46 this.datastore = datastore;
50 ActorRef shardManagerActor = datastore.getActorContext().getShardManager();
52 CreateShard createShard = new CreateShard(ENTITY_OWNERSHIP_SHARD_NAME,
53 datastore.getActorContext().getConfiguration().getUniqueMemberNamesForAllShards(),
54 newShardPropsCreator(), null);
56 Future<Object> createFuture = datastore.getActorContext().executeOperationAsync(shardManagerActor,
57 createShard, MESSAGE_TIMEOUT);
59 createFuture.onComplete(new OnComplete<Object>() {
61 public void onComplete(Throwable failure, Object response) {
63 LOG.error("Failed to create {} shard", ENTITY_OWNERSHIP_SHARD_NAME);
65 LOG.info("Successfully created {} shard", ENTITY_OWNERSHIP_SHARD_NAME);
68 }, datastore.getActorContext().getClientDispatcher());
71 private void executeEntityOwnershipShardOperation(final ActorRef shardActor, final Object message) {
72 Future<Object> future = datastore.getActorContext().executeOperationAsync(shardActor, message, MESSAGE_TIMEOUT);
73 future.onComplete(new OnComplete<Object>() {
75 public void onComplete(Throwable failure, Object response) {
77 LOG.debug("Error sending message {} to {}", message, shardActor, failure);
78 // TODO - queue for retry
80 LOG.debug("{} message to {} succeeded", message, shardActor, failure);
83 }, datastore.getActorContext().getClientDispatcher());
86 private void executeLocalEntityOwnershipShardOperation(final Object message) {
87 if(localEntityOwnershipShard == null) {
88 Future<ActorRef> future = datastore.getActorContext().findLocalShardAsync(ENTITY_OWNERSHIP_SHARD_NAME);
89 future.onComplete(new OnComplete<ActorRef>() {
91 public void onComplete(Throwable failure, ActorRef shardActor) {
93 LOG.error("Failed to find local {} shard", ENTITY_OWNERSHIP_SHARD_NAME, failure);
95 localEntityOwnershipShard = shardActor;
96 executeEntityOwnershipShardOperation(localEntityOwnershipShard, message);
99 }, datastore.getActorContext().getClientDispatcher());
102 executeEntityOwnershipShardOperation(localEntityOwnershipShard, message);
107 public EntityOwnershipCandidateRegistration registerCandidate(Entity entity, EntityOwnershipCandidate candidate)
108 throws CandidateAlreadyRegisteredException {
110 EntityOwnershipCandidate currentCandidate = registeredEntities.putIfAbsent(entity, candidate);
111 if(currentCandidate != null) {
112 throw new CandidateAlreadyRegisteredException(entity, currentCandidate);
115 RegisterCandidateLocal registerCandidate = new RegisterCandidateLocal(candidate, entity);
117 LOG.debug("Registering candidate with message: {}", registerCandidate);
119 executeLocalEntityOwnershipShardOperation(registerCandidate);
120 return new DistributedEntityOwnershipCandidateRegistration(candidate, entity, this);
123 void unregisterCandidate(Entity entity) {
124 LOG.debug("Unregistering candidate for {}", entity);
126 executeLocalEntityOwnershipShardOperation(new UnregisterCandidateLocal(entity));
127 registeredEntities.remove(entity);
131 public EntityOwnershipListenerRegistration registerListener(Entity entity, EntityOwnershipListener listener) {
132 // TODO Auto-generated method stub
137 public void close() {
140 protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
141 return new EntityOwnershipShardPropsCreator();