Bug 4105: Implement RegisterCandidate in EntityOwnershipShard
[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 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;
31
32 /**
33  * The distributed implementation of the EntityOwnershipService.
34  *
35  * @author Thomas Pantelis
36  */
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);
41
42     private final DistributedDataStore datastore;
43     private final ConcurrentMap<Entity, EntityOwnershipCandidate> registeredEntities = new ConcurrentHashMap<>();
44     private volatile ActorRef localEntityOwnershipShard;
45
46     public DistributedEntityOwnershipService(DistributedDataStore datastore) {
47         this.datastore = datastore;
48     }
49
50     public void start() {
51         ActorRef shardManagerActor = datastore.getActorContext().getShardManager();
52
53         CreateShard createShard = new CreateShard(ENTITY_OWNERSHIP_SHARD_NAME,
54                 datastore.getActorContext().getConfiguration().getUniqueMemberNamesForAllShards(),
55                 newShardPropsCreator(), null);
56
57         Future<Object> createFuture = datastore.getActorContext().executeOperationAsync(shardManagerActor,
58                 createShard, MESSAGE_TIMEOUT);
59
60         createFuture.onComplete(new OnComplete<Object>() {
61             @Override
62             public void onComplete(Throwable failure, Object response) {
63                 if(failure != null) {
64                     LOG.error("Failed to create {} shard", ENTITY_OWNERSHIP_SHARD_NAME);
65                 } else {
66                     LOG.info("Successfully created {} shard", ENTITY_OWNERSHIP_SHARD_NAME);
67                 }
68             }
69         }, datastore.getActorContext().getClientDispatcher());
70     }
71
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>() {
75             @Override
76             public void onComplete(Throwable failure, Object response) {
77                 if(failure != null) {
78                     LOG.debug("Error sending message {} to {}", message, shardActor, failure);
79                     // TODO - queue for retry
80                 } else {
81                     LOG.debug("{} message to {} succeeded", message, shardActor, failure);
82                 }
83             }
84         }, datastore.getActorContext().getClientDispatcher());
85     }
86
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>() {
91                 @Override
92                 public void onComplete(Throwable failure, ActorRef shardActor) {
93                     if(failure != null) {
94                         LOG.error("Failed to find local {} shard", ENTITY_OWNERSHIP_SHARD_NAME, failure);
95                     } else {
96                         localEntityOwnershipShard = shardActor;
97                         executeEntityOwnershipShardOperation(localEntityOwnershipShard, message);
98                     }
99                 }
100             }, datastore.getActorContext().getClientDispatcher());
101
102         } else {
103             executeEntityOwnershipShardOperation(localEntityOwnershipShard, message);
104         }
105     }
106
107     @Override
108     public EntityOwnershipCandidateRegistration registerCandidate(Entity entity, EntityOwnershipCandidate candidate)
109             throws CandidateAlreadyRegisteredException {
110
111         EntityOwnershipCandidate currentCandidate = registeredEntities.putIfAbsent(entity, candidate);
112         if(currentCandidate != null) {
113             throw new CandidateAlreadyRegisteredException(entity, currentCandidate);
114         }
115
116         RegisterCandidateLocal registerCandidate = new RegisterCandidateLocal(candidate, entity);
117
118         LOG.debug("Registering candidate with message: {}", registerCandidate);
119
120         executeLocalEntityOwnershipShardOperation(registerCandidate);
121         return new DistributedEntityOwnershipCandidateRegistration(candidate, entity, this);
122     }
123
124     void unregisterCandidate(Entity entity) {
125         LOG.debug("Unregistering candidate for {}", entity);
126
127         executeLocalEntityOwnershipShardOperation(new UnregisterCandidateLocal(entity));
128         registeredEntities.remove(entity);
129     }
130
131     @Override
132     public EntityOwnershipListenerRegistration registerListener(Entity entity, EntityOwnershipListener listener) {
133         // TODO Auto-generated method stub
134         return null;
135     }
136
137     @Override
138     public void close() {
139     }
140
141     protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
142         return new EntityOwnershipShardPropsCreator(datastore.getActorContext().getCurrentMemberName());
143     }
144
145     @VisibleForTesting
146     ActorRef getLocalEntityOwnershipShard() {
147         return localEntityOwnershipShard;
148     }
149 }