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