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.annotations.VisibleForTesting;
14 import com.google.common.base.Preconditions;
15 import com.google.common.util.concurrent.Uninterruptibles;
16 import java.util.concurrent.CountDownLatch;
17 import java.util.concurrent.TimeUnit;
18 import org.opendaylight.controller.cluster.datastore.config.Configuration;
19 import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
20 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.DatastoreConfigurationMXBeanImpl;
21 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.DatastoreInfoMXBeanImpl;
22 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot;
23 import org.opendaylight.controller.cluster.datastore.shardmanager.ShardManagerCreator;
24 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
25 import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
26 import org.opendaylight.controller.cluster.datastore.utils.PrimaryShardInfoFutureCache;
27 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
28 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
30 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
31 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
32 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
33 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTreeChangePublisher;
34 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
35 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort;
36 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistration;
37 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistry;
38 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
39 import org.opendaylight.yangtools.concepts.ListenerRegistration;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
50 public class DistributedDataStore implements DistributedDataStoreInterface, SchemaContextListener,
51 DatastoreContextConfigAdminOverlay.Listener, DOMStoreTreeChangePublisher, DOMDataTreeCommitCohortRegistry, AutoCloseable {
53 private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStore.class);
54 private static final String UNKNOWN_TYPE = "unknown";
56 private static final long READY_WAIT_FACTOR = 3;
58 private final ActorContext actorContext;
59 private final long waitTillReadyTimeInMillis;
61 private AutoCloseable closeable;
63 private DatastoreConfigurationMXBeanImpl datastoreConfigMXBean;
65 private DatastoreInfoMXBeanImpl datastoreInfoMXBean;
67 private final CountDownLatch waitTillReadyCountDownLatch = new CountDownLatch(1);
69 private final String type;
71 private final TransactionContextFactory txContextFactory;
73 public DistributedDataStore(ActorSystem actorSystem, ClusterWrapper cluster,
74 Configuration configuration, DatastoreContextFactory datastoreContextFactory,
75 DatastoreSnapshot restoreFromSnapshot) {
76 Preconditions.checkNotNull(actorSystem, "actorSystem should not be null");
77 Preconditions.checkNotNull(cluster, "cluster should not be null");
78 Preconditions.checkNotNull(configuration, "configuration should not be null");
79 Preconditions.checkNotNull(datastoreContextFactory, "datastoreContextFactory should not be null");
81 this.type = datastoreContextFactory.getBaseDatastoreContext().getDataStoreName();
83 String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
85 LOG.info("Creating ShardManager : {}", shardManagerId);
87 String shardDispatcher =
88 new Dispatchers(actorSystem.dispatchers()).getDispatcherPath(Dispatchers.DispatcherType.Shard);
90 PrimaryShardInfoFutureCache primaryShardInfoCache = new PrimaryShardInfoFutureCache();
92 ShardManagerCreator creator = new ShardManagerCreator().cluster(cluster).configuration(configuration).
93 datastoreContextFactory(datastoreContextFactory).waitTillReadyCountdownLatch(waitTillReadyCountDownLatch).
94 primaryShardInfoCache(primaryShardInfoCache).restoreFromSnapshot(restoreFromSnapshot);
96 actorContext = new ActorContext(actorSystem, createShardManager(actorSystem, creator, shardDispatcher,
97 shardManagerId), cluster, configuration, datastoreContextFactory.getBaseDatastoreContext(), primaryShardInfoCache);
99 this.waitTillReadyTimeInMillis =
100 actorContext.getDatastoreContext().getShardLeaderElectionTimeout().duration().toMillis() * READY_WAIT_FACTOR;
102 this.txContextFactory = TransactionContextFactory.create(actorContext);
104 datastoreConfigMXBean = new DatastoreConfigurationMXBeanImpl(
105 datastoreContextFactory.getBaseDatastoreContext().getDataStoreMXBeanType());
106 datastoreConfigMXBean.setContext(datastoreContextFactory.getBaseDatastoreContext());
107 datastoreConfigMXBean.registerMBean();
109 datastoreInfoMXBean = new DatastoreInfoMXBeanImpl(datastoreContextFactory.getBaseDatastoreContext().
110 getDataStoreMXBeanType(), actorContext);
111 datastoreInfoMXBean.registerMBean();
115 DistributedDataStore(ActorContext actorContext) {
116 this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
117 this.txContextFactory = TransactionContextFactory.create(actorContext);
118 this.type = UNKNOWN_TYPE;
119 this.waitTillReadyTimeInMillis =
120 actorContext.getDatastoreContext().getShardLeaderElectionTimeout().duration().toMillis() * READY_WAIT_FACTOR;
123 public void setCloseable(AutoCloseable closeable) {
124 this.closeable = closeable;
127 @SuppressWarnings("unchecked")
129 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
130 ListenerRegistration<L> registerChangeListener(
131 final YangInstanceIdentifier path, L listener,
132 AsyncDataBroker.DataChangeScope scope) {
134 Preconditions.checkNotNull(path, "path should not be null");
135 Preconditions.checkNotNull(listener, "listener should not be null");
137 LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
139 String shardName = actorContext.getShardStrategyFactory().getStrategy(path).findShard(path);
141 final DataChangeListenerRegistrationProxy listenerRegistrationProxy =
142 new DataChangeListenerRegistrationProxy(shardName, actorContext, listener);
143 listenerRegistrationProxy.init(path, scope);
145 return listenerRegistrationProxy;
149 public <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(YangInstanceIdentifier treeId, L listener) {
150 Preconditions.checkNotNull(treeId, "treeId should not be null");
151 Preconditions.checkNotNull(listener, "listener should not be null");
153 final String shardName = actorContext.getShardStrategyFactory().getStrategy(treeId).findShard(treeId);
154 LOG.debug("Registering tree listener: {} for tree: {} shard: {}", listener, treeId, shardName);
156 final DataTreeChangeListenerProxy<L> listenerRegistrationProxy =
157 new DataTreeChangeListenerProxy<L>(actorContext, listener);
158 listenerRegistrationProxy.init(shardName, treeId);
160 return listenerRegistrationProxy;
165 public <C extends DOMDataTreeCommitCohort> DOMDataTreeCommitCohortRegistration<C> registerCommitCohort(
166 DOMDataTreeIdentifier subtree, C cohort) {
167 YangInstanceIdentifier treeId =
168 Preconditions.checkNotNull(subtree, "subtree should not be null").getRootIdentifier();
169 Preconditions.checkNotNull(cohort, "listener should not be null");
172 final String shardName = actorContext.getShardStrategyFactory().getStrategy(treeId).findShard(treeId);
173 LOG.debug("Registering cohort: {} for tree: {} shard: {}", cohort, treeId, shardName);
175 DataTreeCohortRegistrationProxy<C> cohortProxy = new DataTreeCohortRegistrationProxy<C>(actorContext, subtree, cohort);
176 cohortProxy.init(shardName);
181 public DOMStoreTransactionChain createTransactionChain() {
182 return txContextFactory.createTransactionChain();
186 public DOMStoreReadTransaction newReadOnlyTransaction() {
187 return new TransactionProxy(txContextFactory, TransactionType.READ_ONLY);
191 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
192 actorContext.acquireTxCreationPermit();
193 return new TransactionProxy(txContextFactory, TransactionType.WRITE_ONLY);
197 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
198 actorContext.acquireTxCreationPermit();
199 return new TransactionProxy(txContextFactory, TransactionType.READ_WRITE);
203 public void onGlobalContextUpdated(SchemaContext schemaContext) {
204 actorContext.setSchemaContext(schemaContext);
208 public void onDatastoreContextUpdated(DatastoreContextFactory contextFactory) {
209 LOG.info("DatastoreContext updated for data store {}", actorContext.getDataStoreName());
211 actorContext.setDatastoreContext(contextFactory);
212 datastoreConfigMXBean.setContext(contextFactory.getBaseDatastoreContext());
216 public void close() {
217 LOG.info("Closing data store {}", type);
219 if (datastoreConfigMXBean != null) {
220 datastoreConfigMXBean.unregisterMBean();
222 if (datastoreInfoMXBean != null) {
223 datastoreInfoMXBean.unregisterMBean();
226 if (closeable != null) {
229 } catch (Exception e) {
230 LOG.debug("Error closing instance", e);
234 txContextFactory.close();
235 actorContext.shutdown();
239 public ActorContext getActorContext() {
243 public void waitTillReady(){
244 LOG.info("Beginning to wait for data store to become ready : {}", type);
247 if (waitTillReadyCountDownLatch.await(waitTillReadyTimeInMillis, TimeUnit.MILLISECONDS)) {
248 LOG.debug("Data store {} is now ready", type);
250 LOG.error("Shared leaders failed to settle in {} seconds, giving up", TimeUnit.MILLISECONDS.toSeconds(waitTillReadyTimeInMillis));
252 } catch (InterruptedException e) {
253 LOG.error("Interrupted while waiting for shards to settle", e);
257 private static ActorRef createShardManager(ActorSystem actorSystem, ShardManagerCreator creator,
258 String shardDispatcher, String shardManagerId) {
259 Exception lastException = null;
261 for(int i=0;i<100;i++) {
263 return actorSystem.actorOf(creator.props().withDispatcher(shardDispatcher).withMailbox(
264 ActorContext.BOUNDED_MAILBOX), shardManagerId);
265 } catch (Exception e){
267 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
268 LOG.debug("Could not create actor {} because of {} - waiting for sometime before retrying (retry count = {})",
269 shardManagerId, e.getMessage(), i);
273 throw new IllegalStateException("Failed to create Shard Manager", lastException);
277 public CountDownLatch getWaitTillReadyCountDownLatch() {
278 return waitTillReadyCountDownLatch;