bfdda4ce706aab635cc5c2fe7ed68c289b8e9910
[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                 } 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, EntityOwnershipCandidate entityOwnershipCandidate) {
124         LOG.debug("Unregistering candidate for {}", entity);
125
126         executeLocalEntityOwnershipShardOperation(new UnregisterCandidateLocal(entityOwnershipCandidate, 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(datastore.getActorContext().getCurrentMemberName());
142     }
143
144     @VisibleForTesting
145     ActorRef getLocalEntityOwnershipShard() {
146         return localEntityOwnershipShard;
147     }
148 }