Include lastCommittedTransactionTime in Shard JMX
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / Shard.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.ActorRef;
12 import akka.actor.ActorSelection;
13 import akka.actor.Props;
14 import akka.event.Logging;
15 import akka.event.LoggingAdapter;
16 import akka.japi.Creator;
17 import akka.serialization.Serialization;
18 import com.google.common.base.Optional;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardMBeanFactory;
21 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
22 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
23 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
24 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChain;
25 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChainReply;
26 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
27 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
28 import org.opendaylight.controller.cluster.datastore.messages.ForwardedCommitTransaction;
29 import org.opendaylight.controller.cluster.datastore.messages.PeerAddressResolved;
30 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
31 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
32 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
33 import org.opendaylight.controller.cluster.datastore.modification.Modification;
34 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
35 import org.opendaylight.controller.cluster.raft.ConfigParams;
36 import org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl;
37 import org.opendaylight.controller.cluster.raft.RaftActor;
38 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
39 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
40 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
41 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
42 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
43 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
44 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
47 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
48 import scala.concurrent.duration.FiniteDuration;
49
50 import java.util.ArrayList;
51 import java.util.Date;
52 import java.util.HashMap;
53 import java.util.List;
54 import java.util.Map;
55 import java.util.concurrent.ExecutionException;
56 import java.util.concurrent.TimeUnit;
57
58 /**
59  * A Shard represents a portion of the logical data tree <br/>
60  * <p>
61  * Our Shard uses InMemoryDataStore as it's internal representation and delegates all requests it
62  * </p>
63  */
64 public class Shard extends RaftActor {
65
66     private static final ConfigParams configParams = new ShardConfigParams();
67
68     public static final String DEFAULT_NAME = "default";
69
70     private final InMemoryDOMDataStore store;
71
72     private final Map<Object, DOMStoreThreePhaseCommitCohort>
73         modificationToCohort = new HashMap<>();
74
75     private final LoggingAdapter LOG =
76         Logging.getLogger(getContext().system(), this);
77
78     // By default persistent will be true and can be turned off using the system
79     // property persistent
80     private final boolean persistent;
81
82     private final String name;
83
84     private volatile SchemaContext schemaContext;
85
86     private final ShardStats shardMBean;
87
88     private final List<ActorSelection> dataChangeListeners = new ArrayList<>();
89
90     private Shard(String name, Map<String, String> peerAddresses) {
91         super(name, peerAddresses, Optional.of(configParams));
92
93         this.name = name;
94
95         String setting = System.getProperty("shard.persistent");
96
97         this.persistent = !"false".equals(setting);
98
99         LOG.info("Creating shard : {} persistent : {}", name, persistent);
100
101         store = InMemoryDOMDataStoreFactory.create(name, null);
102
103         shardMBean = ShardMBeanFactory.getShardStatsMBean(name);
104
105     }
106
107     public static Props props(final String name,
108         final Map<String, String> peerAddresses) {
109         return Props.create(new Creator<Shard>() {
110
111             @Override
112             public Shard create() throws Exception {
113                 return new Shard(name, peerAddresses);
114             }
115
116         });
117     }
118
119
120     @Override public void onReceiveCommand(Object message) {
121         LOG.debug("Received message {} from {}", message.getClass().toString(),
122             getSender());
123
124         if (message.getClass()
125             .equals(CreateTransactionChain.SERIALIZABLE_CLASS)) {
126             if (isLeader()) {
127                 createTransactionChain();
128             } else if (getLeader() != null) {
129                 getLeader().forward(message, getContext());
130             }
131         } else if (message instanceof RegisterChangeListener) {
132             registerChangeListener((RegisterChangeListener) message);
133         } else if (message instanceof UpdateSchemaContext) {
134             updateSchemaContext((UpdateSchemaContext) message);
135         } else if (message instanceof ForwardedCommitTransaction) {
136             handleForwardedCommit((ForwardedCommitTransaction) message);
137         } else if (message.getClass()
138             .equals(CreateTransaction.SERIALIZABLE_CLASS)) {
139             if (isLeader()) {
140                 createTransaction(CreateTransaction.fromSerializable(message));
141             } else if (getLeader() != null) {
142                 getLeader().forward(message, getContext());
143             }
144         } else if (message instanceof PeerAddressResolved) {
145             PeerAddressResolved resolved = (PeerAddressResolved) message;
146             setPeerAddress(resolved.getPeerId(), resolved.getPeerAddress());
147         } else {
148             super.onReceiveCommand(message);
149         }
150     }
151
152     private ActorRef createTypedTransactionActor(
153         CreateTransaction createTransaction, String transactionId) {
154         if (createTransaction.getTransactionType()
155             == TransactionProxy.TransactionType.READ_ONLY.ordinal()) {
156             shardMBean.incrementReadOnlyTransactionCount();
157             return getContext().actorOf(
158                 ShardTransaction
159                     .props(store.newReadOnlyTransaction(), getSelf(),
160                         schemaContext), transactionId);
161
162         } else if (createTransaction.getTransactionType()
163             == TransactionProxy.TransactionType.READ_WRITE.ordinal()) {
164             shardMBean.incrementReadWriteTransactionCount();
165             return getContext().actorOf(
166                 ShardTransaction
167                     .props(store.newReadWriteTransaction(), getSelf(),
168                         schemaContext), transactionId);
169
170
171         } else if (createTransaction.getTransactionType()
172             == TransactionProxy.TransactionType.WRITE_ONLY.ordinal()) {
173             shardMBean.incrementWriteOnlyTransactionCount();
174             return getContext().actorOf(
175                 ShardTransaction
176                     .props(store.newWriteOnlyTransaction(), getSelf(),
177                         schemaContext), transactionId);
178         } else {
179             throw new IllegalArgumentException(
180                 "CreateTransaction message has unidentified transaction type="
181                     + createTransaction.getTransactionType());
182         }
183     }
184
185     private void createTransaction(CreateTransaction createTransaction) {
186
187         String transactionId = "shard-" + createTransaction.getTransactionId();
188         LOG.info("Creating transaction : {} ", transactionId);
189         ActorRef transactionActor =
190             createTypedTransactionActor(createTransaction, transactionId);
191
192         getSender()
193             .tell(new CreateTransactionReply(
194                 Serialization.serializedActorPath(transactionActor),
195                 createTransaction.getTransactionId()).toSerializable(),
196                 getSelf());
197     }
198
199     private void commit(final ActorRef sender, Object serialized) {
200         Modification modification = MutableCompositeModification
201             .fromSerializable(serialized, schemaContext);
202         DOMStoreThreePhaseCommitCohort cohort =
203             modificationToCohort.remove(serialized);
204         if (cohort == null) {
205             LOG.error(
206                 "Could not find cohort for modification : {}", modification);
207             LOG.info("Writing modification using a new transaction");
208             DOMStoreReadWriteTransaction transaction =
209                 store.newReadWriteTransaction();
210             modification.apply(transaction);
211             DOMStoreThreePhaseCommitCohort commitCohort = transaction.ready();
212             ListenableFuture<Void> future =
213                 commitCohort.preCommit();
214             try {
215                 future.get();
216                 future = commitCohort.commit();
217                 future.get();
218             } catch (InterruptedException | ExecutionException e) {
219                 shardMBean.incrementFailedTransactionsCount();
220                 LOG.error("Failed to commit", e);
221                 return;
222             }
223             //we want to just apply the recovery commit and return
224             shardMBean.incrementCommittedTransactionCount();
225             return;
226         }
227
228         final ListenableFuture<Void> future = cohort.commit();
229         final ActorRef self = getSelf();
230         future.addListener(new Runnable() {
231             @Override
232             public void run() {
233                 try {
234                     future.get();
235                         sender
236                             .tell(new CommitTransactionReply().toSerializable(),
237                                 self);
238                         shardMBean.incrementCommittedTransactionCount();
239                         shardMBean.setLastCommittedTransactionTime(new Date());
240
241                 } catch (InterruptedException | ExecutionException e) {
242                     shardMBean.incrementFailedTransactionsCount();
243                     // FIXME : Handle this properly
244                     LOG.error(e, "An exception happened when committing");
245                 }
246             }
247         }, getContext().dispatcher());
248     }
249
250     private void handleForwardedCommit(ForwardedCommitTransaction message) {
251         Object serializedModification =
252             message.getModification().toSerializable();
253
254         modificationToCohort
255             .put(serializedModification, message.getCohort());
256
257         if (persistent) {
258             this.persistData(getSender(), "identifier",
259                 new CompositeModificationPayload(serializedModification));
260         } else {
261             this.commit(getSender(), serializedModification);
262         }
263     }
264
265     private void updateSchemaContext(UpdateSchemaContext message) {
266         this.schemaContext = message.getSchemaContext();
267         store.onGlobalContextUpdated(message.getSchemaContext());
268     }
269
270     private void registerChangeListener(
271         RegisterChangeListener registerChangeListener) {
272
273         LOG.debug("registerDataChangeListener for " + registerChangeListener
274             .getPath());
275
276
277         ActorSelection dataChangeListenerPath = getContext()
278             .system().actorSelection(
279                 registerChangeListener.getDataChangeListenerPath());
280
281
282         // Notify the listener if notifications should be enabled or not
283         // If this shard is the leader then it will enable notifications else
284         // it will not
285         dataChangeListenerPath
286             .tell(new EnableNotification(isLeader()), getSelf());
287
288         // Now store a reference to the data change listener so it can be notified
289         // at a later point if notifications should be enabled or disabled
290         dataChangeListeners.add(dataChangeListenerPath);
291
292         AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>
293             listener =
294             new DataChangeListenerProxy(schemaContext, dataChangeListenerPath);
295
296         org.opendaylight.yangtools.concepts.ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
297             registration =
298             store.registerChangeListener(registerChangeListener.getPath(),
299                 listener, registerChangeListener.getScope());
300         ActorRef listenerRegistration =
301             getContext().actorOf(
302                 DataChangeListenerRegistration.props(registration));
303
304         LOG.debug(
305             "registerDataChangeListener sending reply, listenerRegistrationPath = "
306                 + listenerRegistration.path().toString());
307
308         getSender()
309             .tell(new RegisterChangeListenerReply(listenerRegistration.path()),
310                 getSelf());
311     }
312
313     private void createTransactionChain() {
314         DOMStoreTransactionChain chain = store.createTransactionChain();
315         ActorRef transactionChain =
316             getContext().actorOf(
317                 ShardTransactionChain.props(chain, schemaContext));
318         getSender()
319             .tell(new CreateTransactionChainReply(transactionChain.path())
320                     .toSerializable(),
321                 getSelf());
322     }
323
324     @Override protected void applyState(ActorRef clientActor, String identifier,
325         Object data) {
326
327         if (data instanceof CompositeModificationPayload) {
328             Object modification =
329                 ((CompositeModificationPayload) data).getModification();
330
331             if (modification != null) {
332                 commit(clientActor, modification);
333             } else {
334                 LOG.error("modification is null - this is very unexpected");
335             }
336
337
338         } else {
339             LOG.error("Unknown state received {}", data);
340         }
341
342         ReplicatedLogEntry lastLogEntry = getLastLogEntry();
343
344         if(lastLogEntry != null){
345             shardMBean.setLastLogIndex(lastLogEntry.getIndex());
346             shardMBean.setLastLogTerm(lastLogEntry.getTerm());
347         }
348
349         shardMBean.setCommitIndex(getCommitIndex());
350         shardMBean.setLastApplied(getLastApplied());
351
352     }
353
354     @Override protected Object createSnapshot() {
355         throw new UnsupportedOperationException("createSnapshot");
356     }
357
358     @Override protected void applySnapshot(Object snapshot) {
359         throw new UnsupportedOperationException("applySnapshot");
360     }
361
362     @Override protected void onStateChanged() {
363         for (ActorSelection dataChangeListener : dataChangeListeners) {
364             dataChangeListener
365                 .tell(new EnableNotification(isLeader()), getSelf());
366         }
367
368         if (getLeaderId() != null) {
369             shardMBean.setLeader(getLeaderId());
370         }
371
372         shardMBean.setRaftState(getRaftState().name());
373         shardMBean.setCurrentTerm(getCurrentTerm());
374     }
375
376     @Override public String persistenceId() {
377         return this.name;
378     }
379
380
381     private static class ShardConfigParams extends DefaultConfigParamsImpl {
382         public static final FiniteDuration HEART_BEAT_INTERVAL =
383             new FiniteDuration(500, TimeUnit.MILLISECONDS);
384
385         @Override public FiniteDuration getHeartBeatInterval() {
386             return HEART_BEAT_INTERVAL;
387         }
388     }
389 }