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.identifiers.ShardManagerIdentifier;
19 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.DatastoreConfigurationMXBeanImpl;
20 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.DatastoreInfoMXBeanImpl;
21 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
22 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
23 import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
24 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
25 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
28 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
30 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
31 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTreeChangePublisher;
32 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
44 public class DistributedDataStore implements DOMStore, SchemaContextListener,
45 DatastoreContextConfigAdminOverlay.Listener, DOMStoreTreeChangePublisher, AutoCloseable {
47 private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStore.class);
48 private static final String UNKNOWN_TYPE = "unknown";
50 private static final long READY_WAIT_FACTOR = 3;
52 private final ActorContext actorContext;
53 private final long waitTillReadyTimeInMillis;
56 private AutoCloseable closeable;
58 private DatastoreConfigurationMXBeanImpl datastoreConfigMXBean;
60 private DatastoreInfoMXBeanImpl datastoreInfoMXBean;
62 private final CountDownLatch waitTillReadyCountDownLatch = new CountDownLatch(1);
64 private final String type;
66 private final TransactionContextFactory txContextFactory;
68 public DistributedDataStore(ActorSystem actorSystem, ClusterWrapper cluster,
69 Configuration configuration, DatastoreContext datastoreContext) {
70 Preconditions.checkNotNull(actorSystem, "actorSystem should not be null");
71 Preconditions.checkNotNull(cluster, "cluster should not be null");
72 Preconditions.checkNotNull(configuration, "configuration should not be null");
73 Preconditions.checkNotNull(datastoreContext, "datastoreContext should not be null");
75 this.type = datastoreContext.getDataStoreType();
77 String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
79 LOG.info("Creating ShardManager : {}", shardManagerId);
81 String shardDispatcher =
82 new Dispatchers(actorSystem.dispatchers()).getDispatcherPath(Dispatchers.DispatcherType.Shard);
84 actorContext = new ActorContext(actorSystem, createShardManager(actorSystem, cluster, configuration,
85 datastoreContext, shardDispatcher, shardManagerId ), cluster, configuration, datastoreContext);
87 this.waitTillReadyTimeInMillis =
88 actorContext.getDatastoreContext().getShardLeaderElectionTimeout().duration().toMillis() * READY_WAIT_FACTOR;
90 this.txContextFactory = TransactionContextFactory.create(actorContext);
92 datastoreConfigMXBean = new DatastoreConfigurationMXBeanImpl(datastoreContext.getDataStoreMXBeanType());
93 datastoreConfigMXBean.setContext(datastoreContext);
94 datastoreConfigMXBean.registerMBean();
96 datastoreInfoMXBean = new DatastoreInfoMXBeanImpl(datastoreContext.getDataStoreMXBeanType(), actorContext);
97 datastoreInfoMXBean.registerMBean();
100 public DistributedDataStore(ActorContext actorContext) {
101 this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
102 this.txContextFactory = TransactionContextFactory.create(actorContext);
103 this.type = UNKNOWN_TYPE;
104 this.waitTillReadyTimeInMillis =
105 actorContext.getDatastoreContext().getShardLeaderElectionTimeout().duration().toMillis() * READY_WAIT_FACTOR;
108 public void setCloseable(AutoCloseable closeable) {
109 this.closeable = closeable;
112 @SuppressWarnings("unchecked")
114 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
115 ListenerRegistration<L> registerChangeListener(
116 final YangInstanceIdentifier path, L listener,
117 AsyncDataBroker.DataChangeScope scope) {
119 Preconditions.checkNotNull(path, "path should not be null");
120 Preconditions.checkNotNull(listener, "listener should not be null");
122 LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
124 String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
126 final DataChangeListenerRegistrationProxy listenerRegistrationProxy =
127 new DataChangeListenerRegistrationProxy(shardName, actorContext, listener);
128 listenerRegistrationProxy.init(path, scope);
130 return listenerRegistrationProxy;
134 public <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(YangInstanceIdentifier treeId, L listener) {
135 Preconditions.checkNotNull(treeId, "treeId should not be null");
136 Preconditions.checkNotNull(listener, "listener should not be null");
138 final String shardName = ShardStrategyFactory.getStrategy(treeId).findShard(treeId);
139 LOG.debug("Registering tree listener: {} for tree: {} shard: {}", listener, treeId, shardName);
141 final DataTreeChangeListenerProxy<L> listenerRegistrationProxy =
142 new DataTreeChangeListenerProxy<L>(actorContext, listener);
143 listenerRegistrationProxy.init(shardName, treeId);
145 return listenerRegistrationProxy;
149 public DOMStoreTransactionChain createTransactionChain() {
150 return txContextFactory.createTransactionChain();
154 public DOMStoreReadTransaction newReadOnlyTransaction() {
155 return new TransactionProxy(txContextFactory, TransactionType.READ_ONLY);
159 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
160 actorContext.acquireTxCreationPermit();
161 return new TransactionProxy(txContextFactory, TransactionType.WRITE_ONLY);
165 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
166 actorContext.acquireTxCreationPermit();
167 return new TransactionProxy(txContextFactory, TransactionType.READ_WRITE);
171 public void onGlobalContextUpdated(SchemaContext schemaContext) {
172 actorContext.setSchemaContext(schemaContext);
176 public void onDatastoreContextUpdated(DatastoreContext context) {
177 LOG.info("DatastoreContext updated for data store {}", actorContext.getDataStoreType());
179 actorContext.setDatastoreContext(context);
180 datastoreConfigMXBean.setContext(context);
184 public void close() {
185 datastoreConfigMXBean.unregisterMBean();
186 datastoreInfoMXBean.unregisterMBean();
188 if (closeable != null) {
191 } catch (Exception e) {
192 LOG.debug("Error closing instance", e);
196 txContextFactory.close();
197 actorContext.shutdown();
198 DistributedDataStoreFactory.destroyInstance(this);
202 ActorContext getActorContext() {
206 public void waitTillReady(){
207 LOG.info("Beginning to wait for data store to become ready : {}", type);
210 if (waitTillReadyCountDownLatch.await(waitTillReadyTimeInMillis, TimeUnit.MILLISECONDS)) {
211 LOG.debug("Data store {} is now ready", type);
213 LOG.error("Shared leaders failed to settle in {} seconds, giving up", TimeUnit.MILLISECONDS.toSeconds(waitTillReadyTimeInMillis));
215 } catch (InterruptedException e) {
216 LOG.error("Interrupted while waiting for shards to settle", e);
220 private ActorRef createShardManager(ActorSystem actorSystem, ClusterWrapper cluster, Configuration configuration,
221 DatastoreContext datastoreContext, String shardDispatcher, String shardManagerId){
222 Exception lastException = null;
224 for(int i=0;i<100;i++) {
226 return actorSystem.actorOf(
227 ShardManager.props(cluster, configuration, datastoreContext, waitTillReadyCountDownLatch)
228 .withDispatcher(shardDispatcher).withMailbox(ActorContext.MAILBOX), shardManagerId);
229 } catch (Exception e){
231 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
232 LOG.debug(String.format("Could not create actor %s because of %s - waiting for sometime before retrying (retry count = %d)", shardManagerId, e.getMessage(), i));
236 throw new IllegalStateException("Failed to create Shard Manager", lastException);
240 public CountDownLatch getWaitTillReadyCountDownLatch() {
241 return waitTillReadyCountDownLatch;