2 * Copyright (c) 2014 Cisco 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
9 package org.opendaylight.controller.cluster.datastore;
11 import java.util.concurrent.Executors;
13 import akka.actor.ActorRef;
14 import akka.actor.ActorSystem;
16 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
17 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
18 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
19 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
20 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
22 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import com.google.common.util.concurrent.ListeningExecutorService;
37 import com.google.common.util.concurrent.MoreExecutors;
42 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
44 private static final Logger
45 LOG = LoggerFactory.getLogger(DistributedDataStore.class);
47 private static final int DEFAULT_EXECUTOR_POOL_SIZE = 10;
49 private final String type;
50 private final ActorContext actorContext;
52 private SchemaContext schemaContext;
57 * Executor used to run FutureTask's
59 * This is typically used when we need to make a request to an actor and
60 * wait for it's response and the consumer needs to be provided a Future.
62 * FIXME : Make the thread pool size configurable.
64 private final ListeningExecutorService executor =
65 MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(DEFAULT_EXECUTOR_POOL_SIZE));
67 public DistributedDataStore(ActorSystem actorSystem, String type, ClusterWrapper cluster, Configuration configuration) {
68 this(new ActorContext(actorSystem, actorSystem
69 .actorOf(ShardManager.props(type, cluster, configuration),
70 "shardmanager-" + type), cluster, configuration), type);
73 public DistributedDataStore(ActorContext actorContext, String type) {
75 this.actorContext = actorContext;
80 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerChangeListener(
81 YangInstanceIdentifier path, L listener,
82 AsyncDataBroker.DataChangeScope scope) {
84 ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(
85 DataChangeListener.props(schemaContext,listener,path ));
87 String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
89 Object result = actorContext.executeLocalShardOperation(shardName,
90 new RegisterChangeListener(path, dataChangeListenerActor.path(),
91 scope).toSerializable(),
92 ActorContext.ASK_DURATION
96 RegisterChangeListenerReply reply = RegisterChangeListenerReply
97 .fromSerializable(actorContext.getActorSystem(), result);
98 return new DataChangeListenerRegistrationProxy(actorContext
99 .actorSelection(reply.getListenerRegistrationPath()), listener,
100 dataChangeListenerActor);
104 "No local shard for shardName {} was found so returning a noop registration",
106 return new NoOpDataChangeListenerRegistration(listener);
114 public DOMStoreTransactionChain createTransactionChain() {
115 return new TransactionChainProxy(actorContext, executor, schemaContext);
119 public DOMStoreReadTransaction newReadOnlyTransaction() {
120 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY,
121 executor, schemaContext);
125 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
126 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY,
127 executor, schemaContext);
131 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
132 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE,
133 executor, schemaContext);
136 @Override public void onGlobalContextUpdated(SchemaContext schemaContext) {
137 this.schemaContext = schemaContext;
138 actorContext.getShardManager().tell(
139 new UpdateSchemaContext(schemaContext), null);
142 @Override public void close() throws Exception {
143 actorContext.shutdown();