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