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