624e68dd8662b6a85fa09380352b46758c0f5732
[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.opendaylight.yangtools.yang.model.api.SchemaContext;
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 final SchemaContext schemaContext;
45     private PruningDataTreeModification transaction;
46     private int size;
47     private final byte[] restoreFromSnapshot;
48
49     ShardRecoveryCoordinator(ShardDataTree store, SchemaContext schemaContext, byte[] restoreFromSnapshot,
50             String shardName, Logger log) {
51         this.store = Preconditions.checkNotNull(store);
52         this.restoreFromSnapshot = restoreFromSnapshot;
53         this.shardName = shardName;
54         this.log = log;
55         this.schemaContext = schemaContext;
56     }
57
58     @Override
59     public void startLogRecoveryBatch(int maxBatchSize) {
60         log.debug("{}: starting log recovery batch with max size {}", shardName, maxBatchSize);
61         transaction = new PruningDataTreeModification(store.newModification(), store.getDataTree(), schemaContext);
62         size = 0;
63     }
64
65     @Override
66     public void appendRecoveredLogEntry(Payload payload) {
67         Preconditions.checkState(transaction != null, "call startLogRecovery before calling appendRecoveredLogEntry");
68
69         try {
70             if (payload instanceof DataTreeCandidateSupplier) {
71                 final Entry<Optional<TransactionIdentifier>, DataTreeCandidate> e =
72                         ((DataTreeCandidateSupplier)payload).getCandidate();
73
74                 DataTreeCandidates.applyToModification(transaction, e.getValue());
75                 size++;
76
77                 if (e.getKey().isPresent()) {
78                     // FIXME: BUG-5280: propagate transaction state
79                 }
80             } else {
81                 log.error("{}: Unknown payload {} received during recovery", shardName, payload);
82             }
83         } catch (IOException e) {
84             log.error("{}: Error extracting payload", shardName, e);
85         }
86     }
87
88     private void commitTransaction(PruningDataTreeModification tx) throws DataValidationFailedException {
89         store.commit(tx.getResultingModification());
90     }
91
92     /**
93      * Applies the current batched log entries to the data store.
94      */
95     @Override
96     public void applyCurrentLogRecoveryBatch() {
97         Preconditions.checkState(transaction != null, "call startLogRecovery before calling applyCurrentLogRecoveryBatch");
98
99         log.debug("{}: Applying current log recovery batch with size {}", shardName, size);
100         try {
101             commitTransaction(transaction);
102         } catch (Exception e) {
103             File file = new File(System.getProperty("karaf.data", "."),
104                     "failed-recovery-batch-" + shardName + ".out");
105             DataTreeModificationOutput.toFile(file, transaction.getResultingModification());
106             throw new RuntimeException(String.format(
107                     "%s: Failed to apply recovery batch. Modification data was written to file %s",
108                     shardName, file), e);
109         }
110         transaction = null;
111     }
112
113     /**
114      * Applies a recovered snapshot to the data store.
115      *
116      * @param snapshotBytes the serialized snapshot
117      */
118     @Override
119     public void applyRecoverySnapshot(final byte[] snapshotBytes) {
120         log.debug("{}: Applying recovered snapshot", shardName);
121
122         final NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
123         final PruningDataTreeModification tx = new PruningDataTreeModification(store.newModification(),
124                 store.getDataTree(), schemaContext);
125         tx.write(YangInstanceIdentifier.EMPTY, node);
126         try {
127             commitTransaction(tx);
128         } catch (Exception e) {
129             File file = new File(System.getProperty("karaf.data", "."),
130                     "failed-recovery-snapshot-" + shardName + ".xml");
131             NormalizedNodeXMLOutput.toFile(file, node);
132             throw new RuntimeException(String.format(
133                     "%s: Failed to apply recovery snapshot. Node data was written to file %s",
134                     shardName, file), e);
135         }
136     }
137
138     @Override
139     public byte[] getRestoreFromSnapshot() {
140         return restoreFromSnapshot;
141     }
142 }