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;
11 import java.io.IOException;
12 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
13 import org.opendaylight.controller.cluster.datastore.utils.PruningDataTreeModification;
14 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
15 import org.opendaylight.controller.cluster.raft.RaftActorRecoveryCohort;
16 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationByteStringPayload;
17 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationPayload;
18 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.slf4j.Logger;
27 * Coordinates persistence recovery of journal log entries and snapshots for a shard. Each snapshot
28 * and journal log entry batch are de-serialized and applied to their own write transaction
29 * instance in parallel on a thread pool for faster recovery time. However the transactions are
30 * committed to the data store in the order the corresponding snapshot or log batch are received
31 * to preserve data store integrity.
33 * @author Thomas Pantelis
35 class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
36 private final ShardDataTree store;
37 private final String shardName;
38 private final Logger log;
39 private final SchemaContext schemaContext;
40 private PruningDataTreeModification transaction;
42 private final byte[] restoreFromSnapshot;
44 ShardRecoveryCoordinator(ShardDataTree store, SchemaContext schemaContext, byte[] restoreFromSnapshot,
45 String shardName, Logger log) {
46 this.store = Preconditions.checkNotNull(store);
47 this.restoreFromSnapshot = restoreFromSnapshot;
48 this.shardName = shardName;
50 this.schemaContext = schemaContext;
54 public void startLogRecoveryBatch(int maxBatchSize) {
55 log.debug("{}: starting log recovery batch with max size {}", shardName, maxBatchSize);
56 transaction = new PruningDataTreeModification(store.newModification(), store.getDataTree(), schemaContext);
61 public void appendRecoveredLogEntry(Payload payload) {
62 Preconditions.checkState(transaction != null, "call startLogRecovery before calling appendRecoveredLogEntry");
65 if (payload instanceof DataTreeCandidatePayload) {
66 DataTreeCandidates.applyToModification(transaction, ((DataTreeCandidatePayload)payload).getCandidate());
68 } else if (payload instanceof CompositeModificationPayload) {
69 MutableCompositeModification.fromSerializable(
70 ((CompositeModificationPayload) payload).getModification()).apply(transaction);
72 } else if (payload instanceof CompositeModificationByteStringPayload) {
73 MutableCompositeModification.fromSerializable(
74 ((CompositeModificationByteStringPayload) payload).getModification()).apply(transaction);
77 log.error("{}: Unknown payload {} received during recovery", shardName, payload);
79 } catch (IOException e) {
80 log.error("{}: Error extracting ModificationPayload", shardName, e);
84 private void commitTransaction(PruningDataTreeModification tx) throws DataValidationFailedException {
85 store.commit(tx.getResultingModification());
89 * Applies the current batched log entries to the data store.
92 public void applyCurrentLogRecoveryBatch() {
93 Preconditions.checkState(transaction != null, "call startLogRecovery before calling applyCurrentLogRecoveryBatch");
95 log.debug("{}: Applying current log recovery batch with size {}", shardName, size);
97 commitTransaction(transaction);
98 } catch (DataValidationFailedException e) {
99 log.error("{}: Failed to apply recovery batch", shardName, e);
105 * Applies a recovered snapshot to the data store.
107 * @param snapshotBytes the serialized snapshot
110 public void applyRecoverySnapshot(final byte[] snapshotBytes) {
111 log.debug("{}: Applying recovered snapshot", shardName);
113 final NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
114 final PruningDataTreeModification tx = new PruningDataTreeModification(store.newModification(),
115 store.getDataTree(), schemaContext);
116 tx.write(YangInstanceIdentifier.EMPTY, node);
118 commitTransaction(tx);
119 } catch (DataValidationFailedException e) {
120 log.error("{}: Failed to apply recovery snapshot", shardName, e);
125 public byte[] getRestoreFromSnapshot() {
126 return restoreFromSnapshot;