Merge "Replaced Helium Notification Broker with new Notifcation Broker."
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardSnapshotCohort.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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 package org.opendaylight.controller.cluster.datastore;
9
10 import com.google.common.base.Preconditions;
11 import akka.actor.ActorRef;
12 import java.util.concurrent.ExecutionException;
13 import org.opendaylight.controller.cluster.datastore.identifiers.ShardTransactionIdentifier;
14 import org.opendaylight.controller.cluster.datastore.messages.CreateSnapshot;
15 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
16 import org.opendaylight.controller.cluster.raft.RaftActorSnapshotCohort;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.slf4j.Logger;
20
21 /**
22  * Participates in raft snapshotting on behalf of a Shard actor.
23  *
24  * @author Thomas Pantelis
25  */
26 class ShardSnapshotCohort implements RaftActorSnapshotCohort {
27
28     private static final YangInstanceIdentifier DATASTORE_ROOT = YangInstanceIdentifier.builder().build();
29
30     private int createSnapshotTransactionCounter;
31     private final ShardTransactionActorFactory transactionActorFactory;
32     private final ShardDataTree store;
33     private final Logger log;
34     private final String logId;
35
36     ShardSnapshotCohort(ShardTransactionActorFactory transactionActorFactory, ShardDataTree store,
37             Logger log, String logId) {
38         this.transactionActorFactory = transactionActorFactory;
39         this.store = Preconditions.checkNotNull(store);
40         this.log = log;
41         this.logId = logId;
42     }
43
44     @Override
45     public void createSnapshot(ActorRef actorRef) {
46         // Create a transaction actor. We are really going to treat the transaction as a worker
47         // so that this actor does not get block building the snapshot. THe transaction actor will
48         // after processing the CreateSnapshot message.
49
50         ShardTransactionIdentifier transactionID = new ShardTransactionIdentifier(
51                 "createSnapshot" + ++createSnapshotTransactionCounter);
52
53         ActorRef createSnapshotTransaction = transactionActorFactory.newShardTransaction(
54                 TransactionProxy.TransactionType.READ_ONLY, transactionID, "", DataStoreVersions.CURRENT_VERSION);
55
56         createSnapshotTransaction.tell(CreateSnapshot.INSTANCE, actorRef);
57     }
58
59     @Override
60     public void applySnapshot(byte[] snapshotBytes) {
61         // Since this will be done only on Recovery or when this actor is a Follower
62         // we can safely commit everything in here. We not need to worry about event notifications
63         // as they would have already been disabled on the follower
64
65         log.info("{}: Applying snapshot", logId);
66
67         try {
68             ReadWriteShardDataTreeTransaction transaction = store.newReadWriteTransaction("snapshot-" + logId, null);
69
70             NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
71
72             // delete everything first
73             transaction.getSnapshot().delete(DATASTORE_ROOT);
74
75             // Add everything from the remote node back
76             transaction.getSnapshot().write(DATASTORE_ROOT, node);
77             syncCommitTransaction(transaction);
78         } catch (InterruptedException | ExecutionException e) {
79             log.error("{}: An exception occurred when applying snapshot", logId, e);
80         } finally {
81             log.info("{}: Done applying snapshot", logId);
82         }
83
84     }
85
86     void syncCommitTransaction(final ReadWriteShardDataTreeTransaction transaction)
87             throws ExecutionException, InterruptedException {
88         ShardDataTreeCohort commitCohort = store.finishTransaction(transaction);
89         commitCohort.preCommit().get();
90         commitCohort.commit().get();
91     }
92 }