BUG 2676 : Use shard-dispatcher for ShardManager and Shard actors
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DistributedDataStore.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.datastore;
10
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.shardstrategy.ShardStrategyFactory;
16 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
17 import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  *
35  */
36 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
37
38     private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStore.class);
39     public static final int REGISTER_DATA_CHANGE_LISTENER_TIMEOUT_FACTOR = 24; // 24 times the usual operation timeout
40
41     private final ActorContext actorContext;
42
43     public DistributedDataStore(ActorSystem actorSystem, ClusterWrapper cluster,
44             Configuration configuration, DatastoreContext datastoreContext) {
45         Preconditions.checkNotNull(actorSystem, "actorSystem should not be null");
46         Preconditions.checkNotNull(cluster, "cluster should not be null");
47         Preconditions.checkNotNull(configuration, "configuration should not be null");
48         Preconditions.checkNotNull(datastoreContext, "datastoreContext should not be null");
49
50         String type = datastoreContext.getDataStoreType();
51
52         String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
53
54         LOG.info("Creating ShardManager : {}", shardManagerId);
55
56         String shardDispatcher =
57                 new Dispatchers(actorSystem.dispatchers()).getDispatcherPath(Dispatchers.DispatcherType.Shard);
58
59         actorContext = new ActorContext(actorSystem, actorSystem.actorOf(
60                 ShardManager.props(cluster, configuration, datastoreContext)
61                         .withDispatcher(shardDispatcher).withMailbox(ActorContext.MAILBOX), shardManagerId ),
62                 cluster, configuration, datastoreContext);
63     }
64
65     public DistributedDataStore(ActorContext actorContext) {
66         this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
67     }
68
69     @SuppressWarnings("unchecked")
70     @Override
71     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
72                                               ListenerRegistration<L> registerChangeListener(
73         final YangInstanceIdentifier path, L listener,
74         AsyncDataBroker.DataChangeScope scope) {
75
76         Preconditions.checkNotNull(path, "path should not be null");
77         Preconditions.checkNotNull(listener, "listener should not be null");
78
79         LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
80
81         String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
82
83         final DataChangeListenerRegistrationProxy listenerRegistrationProxy =
84                 new DataChangeListenerRegistrationProxy(shardName, actorContext, listener);
85         listenerRegistrationProxy.init(path, scope);
86
87         return listenerRegistrationProxy;
88     }
89
90     @Override
91     public DOMStoreTransactionChain createTransactionChain() {
92         return new TransactionChainProxy(actorContext);
93     }
94
95     @Override
96     public DOMStoreReadTransaction newReadOnlyTransaction() {
97         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY);
98     }
99
100     @Override
101     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
102         actorContext.acquireTxCreationPermit();
103         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY);
104     }
105
106     @Override
107     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
108         actorContext.acquireTxCreationPermit();
109         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE);
110     }
111
112     @Override
113     public void onGlobalContextUpdated(SchemaContext schemaContext) {
114         actorContext.setSchemaContext(schemaContext);
115     }
116
117     @Override
118     public void close() throws Exception {
119         actorContext.shutdown();
120     }
121
122     @VisibleForTesting
123     ActorContext getActorContext() {
124         return actorContext;
125     }
126 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.