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 org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
15 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.DatastoreConfigurationMXBeanImpl;
16 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
17 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
18 import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
20 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
26 import org.opendaylight.yangtools.concepts.ListenerRegistration;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
37 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
39 private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStore.class);
40 public static final int REGISTER_DATA_CHANGE_LISTENER_TIMEOUT_FACTOR = 24; // 24 times the usual operation timeout
42 private final ActorContext actorContext;
44 private AutoCloseable closeable;
46 private DatastoreConfigurationMXBeanImpl datastoreConfigMXBean;
48 public DistributedDataStore(ActorSystem actorSystem, ClusterWrapper cluster,
49 Configuration configuration, DatastoreContext datastoreContext) {
50 Preconditions.checkNotNull(actorSystem, "actorSystem should not be null");
51 Preconditions.checkNotNull(cluster, "cluster should not be null");
52 Preconditions.checkNotNull(configuration, "configuration should not be null");
53 Preconditions.checkNotNull(datastoreContext, "datastoreContext should not be null");
55 String type = datastoreContext.getDataStoreType();
57 String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
59 LOG.info("Creating ShardManager : {}", shardManagerId);
61 String shardDispatcher =
62 new Dispatchers(actorSystem.dispatchers()).getDispatcherPath(Dispatchers.DispatcherType.Shard);
64 actorContext = new ActorContext(actorSystem, actorSystem.actorOf(
65 ShardManager.props(cluster, configuration, datastoreContext)
66 .withDispatcher(shardDispatcher).withMailbox(ActorContext.MAILBOX), shardManagerId ),
67 cluster, configuration, datastoreContext);
69 datastoreConfigMXBean = new DatastoreConfigurationMXBeanImpl(datastoreContext.getDataStoreMXBeanType());
70 datastoreConfigMXBean.setContext(datastoreContext);
71 datastoreConfigMXBean.registerMBean();
74 public DistributedDataStore(ActorContext actorContext) {
75 this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
78 public void setCloseable(AutoCloseable closeable) {
79 this.closeable = closeable;
82 @SuppressWarnings("unchecked")
84 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
85 ListenerRegistration<L> registerChangeListener(
86 final YangInstanceIdentifier path, L listener,
87 AsyncDataBroker.DataChangeScope scope) {
89 Preconditions.checkNotNull(path, "path should not be null");
90 Preconditions.checkNotNull(listener, "listener should not be null");
92 LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
94 String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
96 final DataChangeListenerRegistrationProxy listenerRegistrationProxy =
97 new DataChangeListenerRegistrationProxy(shardName, actorContext, listener);
98 listenerRegistrationProxy.init(path, scope);
100 return listenerRegistrationProxy;
104 public DOMStoreTransactionChain createTransactionChain() {
105 return new TransactionChainProxy(actorContext);
109 public DOMStoreReadTransaction newReadOnlyTransaction() {
110 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY);
114 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
115 actorContext.acquireTxCreationPermit();
116 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY);
120 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
121 actorContext.acquireTxCreationPermit();
122 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE);
126 public void onGlobalContextUpdated(SchemaContext schemaContext) {
127 actorContext.setSchemaContext(schemaContext);
131 public void close() {
132 datastoreConfigMXBean.unregisterMBean();
134 if(closeable != null) {
137 } catch (Exception e) {
138 LOG.debug("Error closing insance", e);
142 actorContext.shutdown();
146 ActorContext getActorContext() {