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.ActorSystem;
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.base.Preconditions;
14 import java.util.concurrent.CountDownLatch;
15 import java.util.concurrent.TimeUnit;
16 import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
17 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.DatastoreConfigurationMXBeanImpl;
18 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
19 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
20 import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
22 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
39 public class DistributedDataStore implements DOMStore, SchemaContextListener,
40 DatastoreContextConfigAdminOverlay.Listener, AutoCloseable {
42 private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStore.class);
43 private static final String UNKNOWN_TYPE = "unknown";
45 private static final long READY_WAIT_FACTOR = 3;
47 private final ActorContext actorContext;
48 private final long waitTillReadyTimeInMillis;
51 private AutoCloseable closeable;
53 private DatastoreConfigurationMXBeanImpl datastoreConfigMXBean;
55 private CountDownLatch waitTillReadyCountDownLatch = new CountDownLatch(1);
57 private final String type;
59 public DistributedDataStore(ActorSystem actorSystem, ClusterWrapper cluster,
60 Configuration configuration, DatastoreContext datastoreContext) {
61 Preconditions.checkNotNull(actorSystem, "actorSystem should not be null");
62 Preconditions.checkNotNull(cluster, "cluster should not be null");
63 Preconditions.checkNotNull(configuration, "configuration should not be null");
64 Preconditions.checkNotNull(datastoreContext, "datastoreContext should not be null");
66 this.type = datastoreContext.getDataStoreType();
68 String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
70 LOG.info("Creating ShardManager : {}", shardManagerId);
72 String shardDispatcher =
73 new Dispatchers(actorSystem.dispatchers()).getDispatcherPath(Dispatchers.DispatcherType.Shard);
75 actorContext = new ActorContext(actorSystem, actorSystem.actorOf(
76 ShardManager.props(cluster, configuration, datastoreContext, waitTillReadyCountDownLatch)
77 .withDispatcher(shardDispatcher).withMailbox(ActorContext.MAILBOX), shardManagerId ),
78 cluster, configuration, datastoreContext);
80 this.waitTillReadyTimeInMillis =
81 actorContext.getDatastoreContext().getShardLeaderElectionTimeout().duration().toMillis() * READY_WAIT_FACTOR;
84 datastoreConfigMXBean = new DatastoreConfigurationMXBeanImpl(datastoreContext.getDataStoreMXBeanType());
85 datastoreConfigMXBean.setContext(datastoreContext);
86 datastoreConfigMXBean.registerMBean();
89 public DistributedDataStore(ActorContext actorContext) {
90 this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
91 this.type = UNKNOWN_TYPE;
92 this.waitTillReadyTimeInMillis =
93 actorContext.getDatastoreContext().getShardLeaderElectionTimeout().duration().toMillis() * READY_WAIT_FACTOR;
97 public void setCloseable(AutoCloseable closeable) {
98 this.closeable = closeable;
101 @SuppressWarnings("unchecked")
103 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
104 ListenerRegistration<L> registerChangeListener(
105 final YangInstanceIdentifier path, L listener,
106 AsyncDataBroker.DataChangeScope scope) {
108 Preconditions.checkNotNull(path, "path should not be null");
109 Preconditions.checkNotNull(listener, "listener should not be null");
111 LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
113 String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
115 final DataChangeListenerRegistrationProxy listenerRegistrationProxy =
116 new DataChangeListenerRegistrationProxy(shardName, actorContext, listener);
117 listenerRegistrationProxy.init(path, scope);
119 return listenerRegistrationProxy;
123 public DOMStoreTransactionChain createTransactionChain() {
124 return new TransactionChainProxy(actorContext);
128 public DOMStoreReadTransaction newReadOnlyTransaction() {
129 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY);
133 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
134 actorContext.acquireTxCreationPermit();
135 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY);
139 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
140 actorContext.acquireTxCreationPermit();
141 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE);
145 public void onGlobalContextUpdated(SchemaContext schemaContext) {
146 actorContext.setSchemaContext(schemaContext);
150 public void onDatastoreContextUpdated(DatastoreContext context) {
151 LOG.info("DatastoreContext updated for data store {}", actorContext.getDataStoreType());
153 actorContext.setDatastoreContext(context);
154 datastoreConfigMXBean.setContext(context);
158 public void close() {
159 datastoreConfigMXBean.unregisterMBean();
161 if(closeable != null) {
164 } catch (Exception e) {
165 LOG.debug("Error closing insance", e);
169 actorContext.shutdown();
173 ActorContext getActorContext() {
177 public void waitTillReady(){
178 LOG.info("Beginning to wait for data store to become ready : {}", type);
181 waitTillReadyCountDownLatch.await(waitTillReadyTimeInMillis, TimeUnit.MILLISECONDS);
183 LOG.debug("Data store {} is now ready", type);
184 } catch (InterruptedException e) {
185 LOG.error("Interrupted when trying to wait for shards to become leader in a reasonable amount of time - giving up");
190 public CountDownLatch getWaitTillReadyCountDownLatch() {
191 return waitTillReadyCountDownLatch;