2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
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;
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.
30 * @author Thomas Pantelis
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);
39 public Snapshot getRestoreFromSnapshot() {
44 private static final class WithSnapshot extends ShardRecoveryCoordinator {
45 private final Snapshot restoreFromSnapshot;
47 WithSnapshot(final ShardDataTree store, final String shardName, final Logger log, final Snapshot snapshot) {
48 super(store, shardName, log);
49 this.restoreFromSnapshot = requireNonNull(snapshot);
53 public Snapshot getRestoreFromSnapshot() {
54 return restoreFromSnapshot;
58 private final ShardDataTree store;
59 private final String shardName;
60 private final Logger log;
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);
70 static ShardRecoveryCoordinator create(final ShardDataTree store, final String shardName, final Logger log) {
71 return new Simple(store, shardName, log);
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);
80 public void startLogRecoveryBatch(final int maxBatchSize) {
81 log.debug("{}: starting log recovery batch with max size {}", shardName, maxBatchSize);
86 @SuppressWarnings("checkstyle:IllegalCatch")
87 public void appendRecoveredLogEntry(final Payload payload) {
88 checkState(open, "call startLogRecovery before calling appendRecoveredLogEntry");
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);
100 * Applies the current batched log entries to the data store.
103 public void applyCurrentLogRecoveryBatch() {
104 checkState(open, "call startLogRecovery before calling applyCurrentLogRecoveryBatch");
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);
116 * Applies a recovered snapshot to the data store.
118 * @param snapshotState the serialized snapshot
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);
128 log.debug("{}: Applying recovered snapshot", shardName);
129 final ShardSnapshotState shardSnapshotState = (ShardSnapshotState)snapshotState;
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);