Use YangInstanceIdentifier.EMPTY
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardRecoveryCoordinator.java
1 /*
2  * Copyright (c) 2014 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 java.io.IOException;
12 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
13 import org.opendaylight.controller.cluster.datastore.utils.PruningDataTreeModification;
14 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
15 import org.opendaylight.controller.cluster.raft.RaftActorRecoveryCohort;
16 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationByteStringPayload;
17 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationPayload;
18 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.slf4j.Logger;
25
26 /**
27  * Coordinates persistence recovery of journal log entries and snapshots for a shard. Each snapshot
28  * and journal log entry batch are de-serialized and applied to their own write transaction
29  * instance in parallel on a thread pool for faster recovery time. However the transactions are
30  * committed to the data store in the order the corresponding snapshot or log batch are received
31  * to preserve data store integrity.
32  *
33  * @author Thomas Pantelis
34  */
35 class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
36     private final ShardDataTree store;
37     private final String shardName;
38     private final Logger log;
39     private final SchemaContext schemaContext;
40     private PruningDataTreeModification transaction;
41     private int size;
42     private final byte[] restoreFromSnapshot;
43
44     ShardRecoveryCoordinator(ShardDataTree store, SchemaContext schemaContext, byte[] restoreFromSnapshot,
45             String shardName, Logger log) {
46         this.store = Preconditions.checkNotNull(store);
47         this.restoreFromSnapshot = restoreFromSnapshot;
48         this.shardName = shardName;
49         this.log = log;
50         this.schemaContext = schemaContext;
51     }
52
53     @Override
54     public void startLogRecoveryBatch(int maxBatchSize) {
55         log.debug("{}: starting log recovery batch with max size {}", shardName, maxBatchSize);
56         transaction = new PruningDataTreeModification(store.newModification(), store.getDataTree(), schemaContext);
57         size = 0;
58     }
59
60     @Override
61     public void appendRecoveredLogEntry(Payload payload) {
62         Preconditions.checkState(transaction != null, "call startLogRecovery before calling appendRecoveredLogEntry");
63
64         try {
65             if (payload instanceof DataTreeCandidatePayload) {
66                 DataTreeCandidates.applyToModification(transaction, ((DataTreeCandidatePayload)payload).getCandidate());
67                 size++;
68             } else if (payload instanceof CompositeModificationPayload) {
69                 MutableCompositeModification.fromSerializable(
70                     ((CompositeModificationPayload) payload).getModification()).apply(transaction);
71                 size++;
72             } else if (payload instanceof CompositeModificationByteStringPayload) {
73                 MutableCompositeModification.fromSerializable(
74                         ((CompositeModificationByteStringPayload) payload).getModification()).apply(transaction);
75                 size++;
76             } else {
77                 log.error("{}: Unknown payload {} received during recovery", shardName, payload);
78             }
79         } catch (IOException e) {
80             log.error("{}: Error extracting ModificationPayload", shardName, e);
81         }
82     }
83
84     private void commitTransaction(PruningDataTreeModification tx) throws DataValidationFailedException {
85         store.commit(tx.getResultingModification());
86     }
87
88     /**
89      * Applies the current batched log entries to the data store.
90      */
91     @Override
92     public void applyCurrentLogRecoveryBatch() {
93         Preconditions.checkState(transaction != null, "call startLogRecovery before calling applyCurrentLogRecoveryBatch");
94
95         log.debug("{}: Applying current log recovery batch with size {}", shardName, size);
96         try {
97             commitTransaction(transaction);
98         } catch (DataValidationFailedException e) {
99             log.error("{}: Failed to apply recovery batch", shardName, e);
100         }
101         transaction = null;
102     }
103
104     /**
105      * Applies a recovered snapshot to the data store.
106      *
107      * @param snapshotBytes the serialized snapshot
108      */
109     @Override
110     public void applyRecoverySnapshot(final byte[] snapshotBytes) {
111         log.debug("{}: Applying recovered snapshot", shardName);
112
113         final NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
114         final PruningDataTreeModification tx = new PruningDataTreeModification(store.newModification(),
115                 store.getDataTree(), schemaContext);
116         tx.write(YangInstanceIdentifier.EMPTY, node);
117         try {
118             commitTransaction(tx);
119         } catch (DataValidationFailedException e) {
120             log.error("{}: Failed to apply recovery snapshot", shardName, e);
121         }
122     }
123
124     @Override
125     public byte[] getRestoreFromSnapshot() {
126         return restoreFromSnapshot;
127     }
128 }