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,
38 DatastoreContextConfigAdminOverlay.Listener, AutoCloseable {
40 private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStore.class);
41 public static final int REGISTER_DATA_CHANGE_LISTENER_TIMEOUT_FACTOR = 24; // 24 times the usual operation timeout
43 private final ActorContext actorContext;
45 private AutoCloseable closeable;
47 private DatastoreConfigurationMXBeanImpl datastoreConfigMXBean;
49 public DistributedDataStore(ActorSystem actorSystem, ClusterWrapper cluster,
50 Configuration configuration, DatastoreContext datastoreContext) {
51 Preconditions.checkNotNull(actorSystem, "actorSystem should not be null");
52 Preconditions.checkNotNull(cluster, "cluster should not be null");
53 Preconditions.checkNotNull(configuration, "configuration should not be null");
54 Preconditions.checkNotNull(datastoreContext, "datastoreContext should not be null");
56 String type = datastoreContext.getDataStoreType();
58 String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
60 LOG.info("Creating ShardManager : {}", shardManagerId);
62 String shardDispatcher =
63 new Dispatchers(actorSystem.dispatchers()).getDispatcherPath(Dispatchers.DispatcherType.Shard);
65 actorContext = new ActorContext(actorSystem, actorSystem.actorOf(
66 ShardManager.props(cluster, configuration, datastoreContext)
67 .withDispatcher(shardDispatcher).withMailbox(ActorContext.MAILBOX), shardManagerId ),
68 cluster, configuration, datastoreContext);
70 datastoreConfigMXBean = new DatastoreConfigurationMXBeanImpl(datastoreContext.getDataStoreMXBeanType());
71 datastoreConfigMXBean.setContext(datastoreContext);
72 datastoreConfigMXBean.registerMBean();
75 public DistributedDataStore(ActorContext actorContext) {
76 this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
79 public void setCloseable(AutoCloseable closeable) {
80 this.closeable = closeable;
83 @SuppressWarnings("unchecked")
85 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
86 ListenerRegistration<L> registerChangeListener(
87 final YangInstanceIdentifier path, L listener,
88 AsyncDataBroker.DataChangeScope scope) {
90 Preconditions.checkNotNull(path, "path should not be null");
91 Preconditions.checkNotNull(listener, "listener should not be null");
93 LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
95 String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
97 final DataChangeListenerRegistrationProxy listenerRegistrationProxy =
98 new DataChangeListenerRegistrationProxy(shardName, actorContext, listener);
99 listenerRegistrationProxy.init(path, scope);
101 return listenerRegistrationProxy;
105 public DOMStoreTransactionChain createTransactionChain() {
106 return new TransactionChainProxy(actorContext);
110 public DOMStoreReadTransaction newReadOnlyTransaction() {
111 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY);
115 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
116 actorContext.acquireTxCreationPermit();
117 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY);
121 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
122 actorContext.acquireTxCreationPermit();
123 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE);
127 public void onGlobalContextUpdated(SchemaContext schemaContext) {
128 actorContext.setSchemaContext(schemaContext);
132 public void onDatastoreContextUpdated(DatastoreContext context) {
133 LOG.info("DatastoreContext updated for data store {}", actorContext.getDataStoreType());
135 actorContext.setDatastoreContext(context);
136 datastoreConfigMXBean.setContext(context);
140 public void close() {
141 datastoreConfigMXBean.unregisterMBean();
143 if(closeable != null) {
146 } catch (Exception e) {
147 LOG.debug("Error closing insance", e);
151 actorContext.shutdown();
155 ActorContext getActorContext() {