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 com.google.common.base.Preconditions;
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;
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.
28 * @author Thomas Pantelis
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;
38 ShardRecoveryCoordinator(final ShardDataTree store, final Snapshot restoreFromSnapshot, final String shardName,
40 this.store = Preconditions.checkNotNull(store);
41 this.shardName = Preconditions.checkNotNull(shardName);
42 this.log = Preconditions.checkNotNull(log);
44 this.restoreFromSnapshot = restoreFromSnapshot;
48 public void startLogRecoveryBatch(final int maxBatchSize) {
49 log.debug("{}: starting log recovery batch with max size {}", shardName, maxBatchSize);
54 @SuppressWarnings("checkstyle:IllegalCatch")
55 public void appendRecoveredLogEntry(final Payload payload) {
56 Preconditions.checkState(open, "call startLogRecovery before calling appendRecoveredLogEntry");
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);
68 * Applies the current batched log entries to the data store.
71 public void applyCurrentLogRecoveryBatch() {
72 Preconditions.checkState(open, "call startLogRecovery before calling applyCurrentLogRecoveryBatch");
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);
84 * Applies a recovered snapshot to the data store.
86 * @param snapshotState the serialized snapshot
89 @SuppressWarnings("checkstyle:IllegalCatch")
90 public void applyRecoverySnapshot(final Snapshot.State snapshotState) {
91 if (!(snapshotState instanceof ShardSnapshotState)) {
92 log.debug("{}: applyRecoverySnapshot ignoring snapshot: {}", snapshotState);
95 log.debug("{}: Applying recovered snapshot", shardName);
97 ShardDataTreeSnapshot shardSnapshot = ((ShardSnapshotState)snapshotState).getSnapshot();
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);
109 public Snapshot getRestoreFromSnapshot() {
110 return restoreFromSnapshot;