Acquire SchemaContext from ShardDataTree
[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.File;
12 import java.io.IOException;
13 import java.util.Map.Entry;
14 import java.util.Optional;
15 import org.opendaylight.controller.cluster.datastore.persisted.DataTreeCandidateSupplier;
16 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
17 import org.opendaylight.controller.cluster.datastore.utils.DataTreeModificationOutput;
18 import org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeXMLOutput;
19 import org.opendaylight.controller.cluster.datastore.utils.PruningDataTreeModification;
20 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
21 import org.opendaylight.controller.cluster.raft.RaftActorRecoveryCohort;
22 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
28 import org.slf4j.Logger;
29
30 /**
31  * Coordinates persistence recovery of journal log entries and snapshots for a shard. Each snapshot
32  * and journal log entry batch are de-serialized and applied to their own write transaction
33  * instance in parallel on a thread pool for faster recovery time. However the transactions are
34  * committed to the data store in the order the corresponding snapshot or log batch are received
35  * to preserve data store integrity.
36  *
37  * @author Thomas Pantelis
38  */
39 class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
40     private final ShardDataTree store;
41     private final String shardName;
42     private final Logger log;
43     private PruningDataTreeModification transaction;
44     private int size;
45     private final byte[] restoreFromSnapshot;
46
47     ShardRecoveryCoordinator(ShardDataTree store,  byte[] restoreFromSnapshot, String shardName, Logger log) {
48         this.store = Preconditions.checkNotNull(store);
49         this.restoreFromSnapshot = restoreFromSnapshot;
50         this.shardName = Preconditions.checkNotNull(shardName);
51         this.log = Preconditions.checkNotNull(log);
52     }
53
54     @Override
55     public void startLogRecoveryBatch(int maxBatchSize) {
56         log.debug("{}: starting log recovery batch with max size {}", shardName, maxBatchSize);
57         transaction = new PruningDataTreeModification(store.newModification(), store.getDataTree(),
58             store.getSchemaContext());
59         size = 0;
60     }
61
62     @Override
63     public void appendRecoveredLogEntry(Payload payload) {
64         Preconditions.checkState(transaction != null, "call startLogRecovery before calling appendRecoveredLogEntry");
65
66         try {
67             if (payload instanceof DataTreeCandidateSupplier) {
68                 final Entry<Optional<TransactionIdentifier>, DataTreeCandidate> e =
69                         ((DataTreeCandidateSupplier)payload).getCandidate();
70
71                 DataTreeCandidates.applyToModification(transaction, e.getValue());
72                 size++;
73
74                 if (e.getKey().isPresent()) {
75                     // FIXME: BUG-5280: propagate transaction state
76                 }
77             } else {
78                 log.error("{}: Unknown payload {} received during recovery", shardName, payload);
79             }
80         } catch (IOException e) {
81             log.error("{}: Error extracting payload", shardName, e);
82         }
83     }
84
85     private void commitTransaction(PruningDataTreeModification tx) throws DataValidationFailedException {
86         store.commit(tx.getResultingModification());
87     }
88
89     /**
90      * Applies the current batched log entries to the data store.
91      */
92     @Override
93     public void applyCurrentLogRecoveryBatch() {
94         Preconditions.checkState(transaction != null, "call startLogRecovery before calling applyCurrentLogRecoveryBatch");
95
96         log.debug("{}: Applying current log recovery batch with size {}", shardName, size);
97         try {
98             commitTransaction(transaction);
99         } catch (Exception e) {
100             File file = new File(System.getProperty("karaf.data", "."),
101                     "failed-recovery-batch-" + shardName + ".out");
102             DataTreeModificationOutput.toFile(file, transaction.getResultingModification());
103             throw new RuntimeException(String.format(
104                     "%s: Failed to apply recovery batch. Modification data was written to file %s",
105                     shardName, file), e);
106         }
107         transaction = null;
108     }
109
110     /**
111      * Applies a recovered snapshot to the data store.
112      *
113      * @param snapshotBytes the serialized snapshot
114      */
115     @Override
116     public void applyRecoverySnapshot(final byte[] snapshotBytes) {
117         log.debug("{}: Applying recovered snapshot", shardName);
118
119         final NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
120         final PruningDataTreeModification tx = new PruningDataTreeModification(store.newModification(),
121                 store.getDataTree(), store.getSchemaContext());
122         tx.write(YangInstanceIdentifier.EMPTY, node);
123         try {
124             commitTransaction(tx);
125         } catch (Exception e) {
126             File file = new File(System.getProperty("karaf.data", "."),
127                     "failed-recovery-snapshot-" + shardName + ".xml");
128             NormalizedNodeXMLOutput.toFile(file, node);
129             throw new RuntimeException(String.format(
130                     "%s: Failed to apply recovery snapshot. Node data was written to file %s",
131                     shardName, file), e);
132         }
133     }
134
135     @Override
136     public byte[] getRestoreFromSnapshot() {
137         return restoreFromSnapshot;
138     }
139 }