Merge "config-persister-feature-adapter to push configs from karaf features"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DistributedDataStore.java
1 /*
2  * Copyright (c) 2014 Cisco 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
9 package org.opendaylight.controller.cluster.datastore;
10
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;
36
37 /**
38  *
39  */
40 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
41
42     private static final Logger
43         LOG = LoggerFactory.getLogger(DistributedDataStore.class);
44
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;
48
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;
52
53     private final String type;
54     private final ActorContext actorContext;
55
56     private SchemaContext schemaContext;
57
58     /**
59      * Executor used to run FutureTask's
60      *
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.
63      */
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"));
73
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);
78     }
79
80     public DistributedDataStore(ActorContext actorContext, String type) {
81         this.type = type;
82         this.actorContext = actorContext;
83     }
84
85
86     @Override
87     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerChangeListener(
88         YangInstanceIdentifier path, L listener,
89         AsyncDataBroker.DataChangeScope scope) {
90
91         ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(
92             DataChangeListener.props(schemaContext,listener,path ));
93
94         String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
95
96         Object result = actorContext.executeLocalShardOperation(shardName,
97             new RegisterChangeListener(path, dataChangeListenerActor.path(),
98                 scope),
99             ActorContext.ASK_DURATION
100         );
101
102         if (result != null) {
103             RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
104             return new DataChangeListenerRegistrationProxy(actorContext
105                 .actorSelection(reply.getListenerRegistrationPath()), listener,
106                 dataChangeListenerActor);
107         }
108
109         LOG.debug(
110             "No local shard for shardName {} was found so returning a noop registration",
111             shardName);
112         return new NoOpDataChangeListenerRegistration(listener);
113     }
114
115
116
117
118
119     @Override
120     public DOMStoreTransactionChain createTransactionChain() {
121         return new TransactionChainProxy(actorContext, executor, schemaContext);
122     }
123
124     @Override
125     public DOMStoreReadTransaction newReadOnlyTransaction() {
126         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY,
127             executor, schemaContext);
128     }
129
130     @Override
131     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
132         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY,
133             executor, schemaContext);
134     }
135
136     @Override
137     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
138         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE,
139             executor, schemaContext);
140     }
141
142     @Override public void onGlobalContextUpdated(SchemaContext schemaContext) {
143         this.schemaContext = schemaContext;
144         actorContext.getShardManager().tell(
145             new UpdateSchemaContext(schemaContext), null);
146     }
147
148     @Override public void close() throws Exception {
149         actorContext.shutdown();
150
151     }
152 }