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 akka.actor.ActorRef;
12 import akka.actor.ActorSystem;
13 import com.google.common.util.concurrent.ListeningExecutorService;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
16 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
17 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
18 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
19 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
20 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
27 import org.opendaylight.yangtools.concepts.ListenerRegistration;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import java.util.concurrent.Executors;
40 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
42 private static final Logger
43 LOG = LoggerFactory.getLogger(DistributedDataStore.class);
45 private static final int DEFAULT_EXECUTOR_POOL_SIZE = 10;
47 private final String type;
48 private final ActorContext actorContext;
50 private SchemaContext schemaContext;
55 * Executor used to run FutureTask's
57 * This is typically used when we need to make a request to an actor and
58 * wait for it's response and the consumer needs to be provided a Future.
60 * FIXME : Make the thread pool size configurable.
62 private final ListeningExecutorService executor =
63 MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(DEFAULT_EXECUTOR_POOL_SIZE));
65 public DistributedDataStore(ActorSystem actorSystem, String type, ClusterWrapper cluster, Configuration configuration) {
66 this(new ActorContext(actorSystem, actorSystem
67 .actorOf(ShardManager.props(type, cluster, configuration),
68 "shardmanager-" + type), cluster, configuration), type);
71 public DistributedDataStore(ActorContext actorContext, String type) {
73 this.actorContext = actorContext;
78 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerChangeListener(
79 YangInstanceIdentifier path, L listener,
80 AsyncDataBroker.DataChangeScope scope) {
82 ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(
83 DataChangeListener.props(schemaContext,listener,path ));
85 String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
87 Object result = actorContext.executeLocalShardOperation(shardName,
88 new RegisterChangeListener(path, dataChangeListenerActor.path(),
90 ActorContext.ASK_DURATION
94 RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
95 return new DataChangeListenerRegistrationProxy(actorContext
96 .actorSelection(reply.getListenerRegistrationPath()), listener,
97 dataChangeListenerActor);
101 "No local shard for shardName {} was found so returning a noop registration",
103 return new NoOpDataChangeListenerRegistration(listener);
111 public DOMStoreTransactionChain createTransactionChain() {
112 return new TransactionChainProxy(actorContext, executor, schemaContext);
116 public DOMStoreReadTransaction newReadOnlyTransaction() {
117 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY,
118 executor, schemaContext);
122 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
123 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY,
124 executor, schemaContext);
128 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
129 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE,
130 executor, schemaContext);
133 @Override public void onGlobalContextUpdated(SchemaContext schemaContext) {
134 this.schemaContext = schemaContext;
135 actorContext.getShardManager().tell(
136 new UpdateSchemaContext(schemaContext), null);
139 @Override public void close() throws Exception {
140 actorContext.shutdown();