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.util.PropertyUtils;
29 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
40 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
42 private static final Logger
43 LOG = LoggerFactory.getLogger(DistributedDataStore.class);
45 private static final String EXECUTOR_MAX_POOL_SIZE_PROP =
46 "mdsal.dist-datastore-executor-pool.size";
47 private static final int DEFAULT_EXECUTOR_MAX_POOL_SIZE = 10;
49 private static final String EXECUTOR_MAX_QUEUE_SIZE_PROP =
50 "mdsal.dist-datastore-executor-queue.size";
51 private static final int DEFAULT_EXECUTOR_MAX_QUEUE_SIZE = 5000;
53 private final String type;
54 private final ActorContext actorContext;
56 private SchemaContext schemaContext;
59 * Executor used to run FutureTask's
61 * This is typically used when we need to make a request to an actor and
62 * wait for it's response and the consumer needs to be provided a Future.
64 private final ListeningExecutorService executor =
65 MoreExecutors.listeningDecorator(
66 SpecialExecutors.newBlockingBoundedFastThreadPool(
67 PropertyUtils.getIntSystemProperty(
68 EXECUTOR_MAX_POOL_SIZE_PROP,
69 DEFAULT_EXECUTOR_MAX_POOL_SIZE),
70 PropertyUtils.getIntSystemProperty(
71 EXECUTOR_MAX_QUEUE_SIZE_PROP,
72 DEFAULT_EXECUTOR_MAX_QUEUE_SIZE), "DistDataStore"));
74 public DistributedDataStore(ActorSystem actorSystem, String type, ClusterWrapper cluster, Configuration configuration) {
75 this(new ActorContext(actorSystem, actorSystem
76 .actorOf(ShardManager.props(type, cluster, configuration),
77 "shardmanager-" + type), cluster, configuration), type);
80 public DistributedDataStore(ActorContext actorContext, String type) {
82 this.actorContext = actorContext;
87 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerChangeListener(
88 YangInstanceIdentifier path, L listener,
89 AsyncDataBroker.DataChangeScope scope) {
91 ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(
92 DataChangeListener.props(schemaContext,listener,path ));
94 String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
96 Object result = actorContext.executeLocalShardOperation(shardName,
97 new RegisterChangeListener(path, dataChangeListenerActor.path(),
99 ActorContext.ASK_DURATION
102 if (result != null) {
103 RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
104 return new DataChangeListenerRegistrationProxy(actorContext
105 .actorSelection(reply.getListenerRegistrationPath()), listener,
106 dataChangeListenerActor);
110 "No local shard for shardName {} was found so returning a noop registration",
112 return new NoOpDataChangeListenerRegistration(listener);
120 public DOMStoreTransactionChain createTransactionChain() {
121 return new TransactionChainProxy(actorContext, executor, schemaContext);
125 public DOMStoreReadTransaction newReadOnlyTransaction() {
126 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY,
127 executor, schemaContext);
131 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
132 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY,
133 executor, schemaContext);
137 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
138 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE,
139 executor, schemaContext);
142 @Override public void onGlobalContextUpdated(SchemaContext schemaContext) {
143 this.schemaContext = schemaContext;
144 actorContext.getShardManager().tell(
145 new UpdateSchemaContext(schemaContext), null);
148 @Override public void close() throws Exception {
149 actorContext.shutdown();