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