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.messages.CreateShard;
19 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
20 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
21 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidate;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
23 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
24 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
25 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import scala.concurrent.Future;
31 * The distributed implementation of the EntityOwnershipService.
33 * @author Thomas Pantelis
35 public class DistributedEntityOwnershipService implements EntityOwnershipService, AutoCloseable {
36 private static final Logger LOG = LoggerFactory.getLogger(DistributedEntityOwnershipService.class);
37 static final String ENTITY_OWNERSHIP_SHARD_NAME = "entity-ownership";
38 private static final Timeout MESSAGE_TIMEOUT = new Timeout(1, TimeUnit.MINUTES);
40 private final DistributedDataStore datastore;
41 private final ConcurrentMap<Entity, EntityOwnershipCandidate> registeredEntities = new ConcurrentHashMap<>();
42 private volatile ActorRef localEntityOwnershipShard;
44 public DistributedEntityOwnershipService(DistributedDataStore datastore) {
45 this.datastore = datastore;
49 ActorRef shardManagerActor = datastore.getActorContext().getShardManager();
51 CreateShard createShard = new CreateShard(ENTITY_OWNERSHIP_SHARD_NAME,
52 datastore.getActorContext().getConfiguration().getUniqueMemberNamesForAllShards(),
53 newShardPropsCreator(), null);
55 Future<Object> createFuture = datastore.getActorContext().executeOperationAsync(shardManagerActor,
56 createShard, MESSAGE_TIMEOUT);
58 createFuture.onComplete(new OnComplete<Object>() {
60 public void onComplete(Throwable failure, Object response) {
62 LOG.error("Failed to create {} shard", ENTITY_OWNERSHIP_SHARD_NAME);
64 LOG.info("Successfully created {} shard", ENTITY_OWNERSHIP_SHARD_NAME);
67 }, datastore.getActorContext().getClientDispatcher());
70 private void executeEntityOwnershipShardOperation(final ActorRef shardActor, final Object message) {
71 Future<Object> future = datastore.getActorContext().executeOperationAsync(shardActor, message, MESSAGE_TIMEOUT);
72 future.onComplete(new OnComplete<Object>() {
74 public void onComplete(Throwable failure, Object response) {
76 LOG.debug("Error sending message {} to {}", message, shardActor, failure);
77 // TODO - queue for retry
79 LOG.debug("{} message to {} succeeded", message, shardActor, failure);
82 }, datastore.getActorContext().getClientDispatcher());
85 private void executeLocalEntityOwnershipShardOperation(final Object message) {
86 if(localEntityOwnershipShard == null) {
87 Future<ActorRef> future = datastore.getActorContext().findLocalShardAsync(ENTITY_OWNERSHIP_SHARD_NAME);
88 future.onComplete(new OnComplete<ActorRef>() {
90 public void onComplete(Throwable failure, ActorRef shardActor) {
92 LOG.error("Failed to find local {} shard", ENTITY_OWNERSHIP_SHARD_NAME, failure);
94 localEntityOwnershipShard = shardActor;
95 executeEntityOwnershipShardOperation(localEntityOwnershipShard, message);
98 }, datastore.getActorContext().getClientDispatcher());
101 executeEntityOwnershipShardOperation(localEntityOwnershipShard, message);
106 public EntityOwnershipCandidateRegistration registerCandidate(Entity entity, EntityOwnershipCandidate candidate)
107 throws CandidateAlreadyRegisteredException {
109 EntityOwnershipCandidate currentCandidate = registeredEntities.putIfAbsent(entity,candidate);
110 if(currentCandidate != null) {
111 throw new CandidateAlreadyRegisteredException(entity, currentCandidate);
114 RegisterCandidateLocal registerCandidate = new RegisterCandidateLocal(candidate, entity);
116 LOG.debug("Registering candidate with message: " + registerCandidate);
118 executeLocalEntityOwnershipShardOperation(registerCandidate);
119 return new DistributedEntityOwnershipCandidateRegistration(candidate, entity);
123 public EntityOwnershipListenerRegistration registerListener(Entity entity, EntityOwnershipListener listener) {
124 // TODO Auto-generated method stub
129 public void close() {
132 protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
133 return new EntityOwnershipShardPropsCreator();