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.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;
19 import com.google.common.base.Optional;
20 import com.google.common.base.Preconditions;
21 import com.google.common.util.concurrent.FutureCallback;
22 import com.google.common.util.concurrent.Futures;
23 import com.google.common.util.concurrent.ListenableFuture;
25 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
26 import org.opendaylight.controller.cluster.datastore.identifiers.ShardTransactionIdentifier;
27 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardMBeanFactory;
28 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
29 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
30 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
31 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChain;
32 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChainReply;
33 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
34 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
35 import org.opendaylight.controller.cluster.datastore.messages.ForwardedCommitTransaction;
36 import org.opendaylight.controller.cluster.datastore.messages.PeerAddressResolved;
37 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
38 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
39 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
40 import org.opendaylight.controller.cluster.datastore.modification.Modification;
41 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
42 import org.opendaylight.controller.cluster.raft.ConfigParams;
43 import org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl;
44 import org.opendaylight.controller.cluster.raft.RaftActor;
45 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
46 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
47 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
48 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
49 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
50 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
51 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
52 import org.opendaylight.yangtools.concepts.ListenerRegistration;
53 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
54 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
55 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
57 import scala.concurrent.duration.FiniteDuration;
59 import java.util.ArrayList;
60 import java.util.Date;
61 import java.util.HashMap;
62 import java.util.List;
64 import java.util.concurrent.ExecutionException;
65 import java.util.concurrent.TimeUnit;
68 * A Shard represents a portion of the logical data tree <br/>
70 * Our Shard uses InMemoryDataStore as it's internal representation and delegates all requests it
73 public class Shard extends RaftActor {
75 private static final ConfigParams configParams = new ShardConfigParams();
77 public static final String DEFAULT_NAME = "default";
79 // The state of this Shard
80 private final InMemoryDOMDataStore store;
82 private final Map<Object, DOMStoreThreePhaseCommitCohort>
83 modificationToCohort = new HashMap<>();
85 private final LoggingAdapter LOG =
86 Logging.getLogger(getContext().system(), this);
88 // By default persistent will be true and can be turned off using the system
89 // property shard.persistent
90 private final boolean persistent;
92 /// The name of this shard
93 private final ShardIdentifier name;
95 private final ShardStats shardMBean;
97 private final List<ActorSelection> dataChangeListeners = new ArrayList<>();
99 private final ShardContext shardContext;
101 private SchemaContext schemaContext;
103 private Shard(ShardIdentifier name, Map<ShardIdentifier, String> peerAddresses,
104 ShardContext shardContext) {
105 super(name.toString(), mapPeerAddresses(peerAddresses), Optional.of(configParams));
108 this.shardContext = shardContext;
110 String setting = System.getProperty("shard.persistent");
112 this.persistent = !"false".equals(setting);
114 LOG.info("Shard created : {} persistent : {}", name, persistent);
116 store = InMemoryDOMDataStoreFactory.create(name.toString(), null,
117 shardContext.getDataStoreProperties());
119 shardMBean = ShardMBeanFactory.getShardStatsMBean(name.toString());
123 private static Map<String, String> mapPeerAddresses(
124 Map<ShardIdentifier, String> peerAddresses) {
125 Map<String, String> map = new HashMap<>();
127 for (Map.Entry<ShardIdentifier, String> entry : peerAddresses
129 map.put(entry.getKey().toString(), entry.getValue());
135 public static Props props(final ShardIdentifier name,
136 final Map<ShardIdentifier, String> peerAddresses,
137 ShardContext shardContext) {
138 Preconditions.checkNotNull(name, "name should not be null");
139 Preconditions.checkNotNull(peerAddresses, "peerAddresses should not be null");
140 Preconditions.checkNotNull(shardContext, "shardContext should not be null");
142 return Props.create(new ShardCreator(name, peerAddresses, shardContext));
145 @Override public void onReceiveCommand(Object message) {
146 LOG.debug("Received message {} from {}", message.getClass().toString(),
149 if (message.getClass()
150 .equals(CreateTransactionChain.SERIALIZABLE_CLASS)) {
152 createTransactionChain();
153 } else if (getLeader() != null) {
154 getLeader().forward(message, getContext());
156 } else if (message instanceof RegisterChangeListener) {
157 registerChangeListener((RegisterChangeListener) message);
158 } else if (message instanceof UpdateSchemaContext) {
159 updateSchemaContext((UpdateSchemaContext) message);
160 } else if (message instanceof ForwardedCommitTransaction) {
161 handleForwardedCommit((ForwardedCommitTransaction) message);
162 } else if (message.getClass()
163 .equals(CreateTransaction.SERIALIZABLE_CLASS)) {
165 createTransaction(CreateTransaction.fromSerializable(message));
166 } else if (getLeader() != null) {
167 getLeader().forward(message, getContext());
169 } else if (message instanceof PeerAddressResolved) {
170 PeerAddressResolved resolved = (PeerAddressResolved) message;
171 setPeerAddress(resolved.getPeerId().toString(),
172 resolved.getPeerAddress());
174 super.onReceiveCommand(message);
178 private ActorRef createTypedTransactionActor(
179 CreateTransaction createTransaction,
180 ShardTransactionIdentifier transactionId) {
181 if (createTransaction.getTransactionType()
182 == TransactionProxy.TransactionType.READ_ONLY.ordinal()) {
184 shardMBean.incrementReadOnlyTransactionCount();
186 return getContext().actorOf(
187 ShardTransaction.props(store.newReadOnlyTransaction(), getSelf(),
188 schemaContext, shardContext), transactionId.toString());
190 } else if (createTransaction.getTransactionType()
191 == TransactionProxy.TransactionType.READ_WRITE.ordinal()) {
193 shardMBean.incrementReadWriteTransactionCount();
195 return getContext().actorOf(
196 ShardTransaction.props(store.newReadWriteTransaction(), getSelf(),
197 schemaContext, shardContext), transactionId.toString());
200 } else if (createTransaction.getTransactionType()
201 == TransactionProxy.TransactionType.WRITE_ONLY.ordinal()) {
203 shardMBean.incrementWriteOnlyTransactionCount();
205 return getContext().actorOf(
206 ShardTransaction.props(store.newWriteOnlyTransaction(), getSelf(),
207 schemaContext, shardContext), transactionId.toString());
209 throw new IllegalArgumentException(
210 "Shard="+name + ":CreateTransaction message has unidentified transaction type="
211 + createTransaction.getTransactionType());
215 private void createTransaction(CreateTransaction createTransaction) {
217 ShardTransactionIdentifier transactionId =
218 ShardTransactionIdentifier.builder()
219 .remoteTransactionId(createTransaction.getTransactionId())
221 LOG.debug("Creating transaction : {} ", transactionId);
222 ActorRef transactionActor =
223 createTypedTransactionActor(createTransaction, transactionId);
226 .tell(new CreateTransactionReply(
227 Serialization.serializedActorPath(transactionActor),
228 createTransaction.getTransactionId()).toSerializable(),
232 private void commit(final ActorRef sender, Object serialized) {
233 Modification modification = MutableCompositeModification
234 .fromSerializable(serialized, schemaContext);
235 DOMStoreThreePhaseCommitCohort cohort =
236 modificationToCohort.remove(serialized);
237 if (cohort == null) {
239 "Could not find cohort for modification : {}. Writing modification using a new transaction",
241 DOMStoreReadWriteTransaction transaction =
242 store.newReadWriteTransaction();
243 modification.apply(transaction);
244 DOMStoreThreePhaseCommitCohort commitCohort = transaction.ready();
245 ListenableFuture<Void> future =
246 commitCohort.preCommit();
249 future = commitCohort.commit();
251 } catch (InterruptedException | ExecutionException e) {
252 shardMBean.incrementFailedTransactionsCount();
253 LOG.error("Failed to commit", e);
256 //we want to just apply the recovery commit and return
257 shardMBean.incrementCommittedTransactionCount();
261 final ListenableFuture<Void> future = cohort.commit();
262 final ActorRef self = getSelf();
264 Futures.addCallback(future, new FutureCallback<Void>() {
266 public void onSuccess(Void v) {
267 sender.tell(new CommitTransactionReply().toSerializable(),self);
268 shardMBean.incrementCommittedTransactionCount();
269 shardMBean.setLastCommittedTransactionTime(new Date());
273 public void onFailure(Throwable t) {
274 LOG.error(t, "An exception happened during commit");
275 shardMBean.incrementFailedTransactionsCount();
276 sender.tell(new akka.actor.Status.Failure(t), self);
282 private void handleForwardedCommit(ForwardedCommitTransaction message) {
283 Object serializedModification =
284 message.getModification().toSerializable();
287 .put(serializedModification, message.getCohort());
290 this.persistData(getSender(), "identifier",
291 new CompositeModificationPayload(serializedModification));
293 this.commit(getSender(), serializedModification);
297 private void updateSchemaContext(UpdateSchemaContext message) {
298 this.schemaContext = message.getSchemaContext();
299 store.onGlobalContextUpdated(message.getSchemaContext());
302 private void registerChangeListener(
303 RegisterChangeListener registerChangeListener) {
305 LOG.debug("registerDataChangeListener for {}", registerChangeListener
309 ActorSelection dataChangeListenerPath = getContext()
310 .system().actorSelection(
311 registerChangeListener.getDataChangeListenerPath());
314 // Notify the listener if notifications should be enabled or not
315 // If this shard is the leader then it will enable notifications else
317 dataChangeListenerPath
318 .tell(new EnableNotification(isLeader()), getSelf());
320 // Now store a reference to the data change listener so it can be notified
321 // at a later point if notifications should be enabled or disabled
322 dataChangeListeners.add(dataChangeListenerPath);
324 AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>
325 listener = new DataChangeListenerProxy(schemaContext, dataChangeListenerPath);
327 ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
328 registration = store.registerChangeListener(registerChangeListener.getPath(),
329 listener, registerChangeListener.getScope());
330 ActorRef listenerRegistration =
331 getContext().actorOf(
332 DataChangeListenerRegistration.props(registration));
335 "registerDataChangeListener sending reply, listenerRegistrationPath = {} "
336 , listenerRegistration.path().toString());
339 .tell(new RegisterChangeListenerReply(listenerRegistration.path()),
343 private void createTransactionChain() {
344 DOMStoreTransactionChain chain = store.createTransactionChain();
345 ActorRef transactionChain = getContext().actorOf(
346 ShardTransactionChain.props(chain, schemaContext, shardContext));
347 getSender().tell(new CreateTransactionChainReply(transactionChain.path()).toSerializable(),
351 @Override protected void applyState(ActorRef clientActor, String identifier,
354 if (data instanceof CompositeModificationPayload) {
355 Object modification =
356 ((CompositeModificationPayload) data).getModification();
358 if (modification != null) {
359 commit(clientActor, modification);
362 "modification is null - this is very unexpected, clientActor = {}, identifier = {}",
363 identifier, clientActor.path().toString());
368 LOG.error("Unknown state received {}", data);
372 ReplicatedLogEntry lastLogEntry = getLastLogEntry();
374 if (lastLogEntry != null) {
375 shardMBean.setLastLogIndex(lastLogEntry.getIndex());
376 shardMBean.setLastLogTerm(lastLogEntry.getTerm());
379 shardMBean.setCommitIndex(getCommitIndex());
380 shardMBean.setLastApplied(getLastApplied());
384 @Override protected Object createSnapshot() {
385 throw new UnsupportedOperationException("createSnapshot");
388 @Override protected void applySnapshot(Object snapshot) {
389 throw new UnsupportedOperationException("applySnapshot");
392 @Override protected void onStateChanged() {
393 for (ActorSelection dataChangeListener : dataChangeListeners) {
395 .tell(new EnableNotification(isLeader()), getSelf());
398 if (getLeaderId() != null) {
399 shardMBean.setLeader(getLeaderId());
402 shardMBean.setRaftState(getRaftState().name());
403 shardMBean.setCurrentTerm(getCurrentTerm());
406 @Override public String persistenceId() {
407 return this.name.toString();
411 private static class ShardConfigParams extends DefaultConfigParamsImpl {
412 public static final FiniteDuration HEART_BEAT_INTERVAL =
413 new FiniteDuration(500, TimeUnit.MILLISECONDS);
415 @Override public FiniteDuration getHeartBeatInterval() {
416 return HEART_BEAT_INTERVAL;
420 private static class ShardCreator implements Creator<Shard> {
422 private static final long serialVersionUID = 1L;
424 final ShardIdentifier name;
425 final Map<ShardIdentifier, String> peerAddresses;
426 final ShardContext shardContext;
428 ShardCreator(ShardIdentifier name, Map<ShardIdentifier, String> peerAddresses,
429 ShardContext shardContext) {
431 this.peerAddresses = peerAddresses;
432 this.shardContext = shardContext;
436 public Shard create() throws Exception {
437 return new Shard(name, peerAddresses, shardContext);