Bug 1598: Cleanup stale ShardReadTransactions
[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 java.util.concurrent.TimeUnit;
12
13 import akka.actor.ActorRef;
14 import akka.actor.ActorSystem;
15
16 import com.google.common.base.Preconditions;
17
18 import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
19 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
20 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
21 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
22 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
23 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
24 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
25 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreConfigProperties;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
28 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
30 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import scala.concurrent.duration.Duration;
40
41 /**
42  *
43  */
44 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
45
46     private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStore.class);
47
48     private final ActorContext actorContext;
49     private final ShardContext shardContext;
50
51     public DistributedDataStore(ActorSystem actorSystem, String type, ClusterWrapper cluster,
52             Configuration configuration, DistributedDataStoreProperties dataStoreProperties) {
53         Preconditions.checkNotNull(actorSystem, "actorSystem should not be null");
54         Preconditions.checkNotNull(type, "type should not be null");
55         Preconditions.checkNotNull(cluster, "cluster should not be null");
56         Preconditions.checkNotNull(configuration, "configuration should not be null");
57
58
59         String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
60
61         LOG.info("Creating ShardManager : {}", shardManagerId);
62
63         shardContext = new ShardContext(InMemoryDOMDataStoreConfigProperties.create(
64                 dataStoreProperties.getMaxShardDataChangeExecutorPoolSize(),
65                 dataStoreProperties.getMaxShardDataChangeExecutorQueueSize(),
66                 dataStoreProperties.getMaxShardDataChangeListenerQueueSize()),
67                 Duration.create(dataStoreProperties.getShardTransactionIdleTimeoutInMinutes(),
68                         TimeUnit.MINUTES));
69
70         actorContext = new ActorContext(actorSystem, actorSystem
71             .actorOf(ShardManager.props(type, cluster, configuration, shardContext),
72                 shardManagerId ), cluster, configuration);
73     }
74
75     public DistributedDataStore(ActorContext actorContext) {
76         this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
77         this.shardContext = new ShardContext();
78     }
79
80
81     @SuppressWarnings("unchecked")
82     @Override
83     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
84                                               ListenerRegistration<L> registerChangeListener(
85         YangInstanceIdentifier path, L listener,
86         AsyncDataBroker.DataChangeScope scope) {
87
88         Preconditions.checkNotNull(path, "path should not be null");
89         Preconditions.checkNotNull(listener, "listener should not be null");
90
91         LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
92
93         ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(
94             DataChangeListener.props(listener ));
95
96         String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
97
98         Object result = actorContext.executeLocalShardOperation(shardName,
99             new RegisterChangeListener(path, dataChangeListenerActor.path(), scope),
100             ActorContext.ASK_DURATION);
101
102         if (result != null) {
103             RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
104             return new DataChangeListenerRegistrationProxy(actorContext
105                 .actorSelection(reply.getListenerRegistrationPath()), listener,
106                 dataChangeListenerActor);
107         }
108
109         LOG.debug(
110             "No local shard for shardName {} was found so returning a noop registration",
111             shardName);
112
113         return new NoOpDataChangeListenerRegistration(listener);
114     }
115
116     @Override
117     public DOMStoreTransactionChain createTransactionChain() {
118         return new TransactionChainProxy(actorContext);
119     }
120
121     @Override
122     public DOMStoreReadTransaction newReadOnlyTransaction() {
123         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY);
124     }
125
126     @Override
127     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
128         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY);
129     }
130
131     @Override
132     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
133         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE);
134     }
135
136     @Override
137     public void onGlobalContextUpdated(SchemaContext schemaContext) {
138         actorContext.setSchemaContext(schemaContext);
139     }
140
141     @Override
142     public void close() throws Exception {
143         actorContext.shutdown();
144     }
145 }