6e5886132a26d098262f9ee7a073130bb38962b6
[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 org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
13 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
14 import org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeXMLOutput;
15 import org.opendaylight.controller.cluster.raft.RaftActorRecoveryCohort;
16 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
17 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.slf4j.Logger;
20
21 /**
22  * Coordinates persistence recovery of journal log entries and snapshots for a shard. Each snapshot
23  * and journal log entry batch are de-serialized and applied to their own write transaction
24  * instance in parallel on a thread pool for faster recovery time. However the transactions are
25  * committed to the data store in the order the corresponding snapshot or log batch are received
26  * to preserve data store integrity.
27  *
28  * @author Thomas Pantelis
29  */
30 class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
31     private final ShardDataTree store;
32     private final String shardName;
33     private final Logger log;
34     private final Snapshot restoreFromSnapshot;
35
36     private boolean open;
37
38     ShardRecoveryCoordinator(final ShardDataTree store,  final Snapshot restoreFromSnapshot, final String shardName,
39             final Logger log) {
40         this.store = Preconditions.checkNotNull(store);
41         this.shardName = Preconditions.checkNotNull(shardName);
42         this.log = Preconditions.checkNotNull(log);
43
44         this.restoreFromSnapshot = restoreFromSnapshot;
45     }
46
47     @Override
48     public void startLogRecoveryBatch(final int maxBatchSize) {
49         log.debug("{}: starting log recovery batch with max size {}", shardName, maxBatchSize);
50         open = true;
51     }
52
53     @Override
54     @SuppressWarnings("checkstyle:IllegalCatch")
55     public void appendRecoveredLogEntry(final Payload payload) {
56         Preconditions.checkState(open, "call startLogRecovery before calling appendRecoveredLogEntry");
57
58         try {
59             store.applyRecoveryPayload(payload);
60         } catch (Exception e) {
61             log.error("{}: failed to apply payload {}", shardName, payload, e);
62             throw new IllegalStateException(String.format("%s: Failed to apply recovery payload %s",
63                 shardName, payload), e);
64         }
65     }
66
67     /**
68      * Applies the current batched log entries to the data store.
69      */
70     @Override
71     public void applyCurrentLogRecoveryBatch() {
72         Preconditions.checkState(open, "call startLogRecovery before calling applyCurrentLogRecoveryBatch");
73         open = false;
74     }
75
76     private File writeRoot(final String kind, final NormalizedNode<?, ?> node) {
77         final File file = new File(System.getProperty("karaf.data", "."),
78             "failed-recovery-" + kind + "-" + shardName + ".xml");
79         NormalizedNodeXMLOutput.toFile(file, node);
80         return file;
81     }
82
83     /**
84      * Applies a recovered snapshot to the data store.
85      *
86      * @param snapshotState the serialized snapshot
87      */
88     @Override
89     @SuppressWarnings("checkstyle:IllegalCatch")
90     public void applyRecoverySnapshot(final Snapshot.State snapshotState) {
91         if (!(snapshotState instanceof ShardSnapshotState)) {
92             log.debug("{}: applyRecoverySnapshot ignoring snapshot: {}", snapshotState);
93         }
94
95         log.debug("{}: Applying recovered snapshot", shardName);
96
97         ShardDataTreeSnapshot shardSnapshot = ((ShardSnapshotState)snapshotState).getSnapshot();
98         try {
99             store.applyRecoverySnapshot(shardSnapshot);
100         } catch (Exception e) {
101             final File f = writeRoot("snapshot", shardSnapshot.getRootNode().orElse(null));
102             throw new IllegalStateException(String.format(
103                     "%s: Failed to apply recovery snapshot %s. Node data was written to file %s",
104                     shardName, shardSnapshot, f), e);
105         }
106     }
107
108     @Override
109     public Snapshot getRestoreFromSnapshot() {
110         return restoreFromSnapshot;
111     }
112 }