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;
14 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
15 import org.opendaylight.controller.cluster.datastore.node.utils.transformer.NormalizedNodePruner;
16 import org.opendaylight.controller.cluster.datastore.utils.PruningDataTreeModification;
17 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
18 import org.opendaylight.controller.cluster.raft.RaftActorRecoveryCohort;
19 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationByteStringPayload;
20 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationPayload;
21 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.slf4j.Logger;
30 * Coordinates persistence recovery of journal log entries and snapshots for a shard. Each snapshot
31 * and journal log entry batch are de-serialized and applied to their own write transaction
32 * instance in parallel on a thread pool for faster recovery time. However the transactions are
33 * committed to the data store in the order the corresponding snapshot or log batch are received
34 * to preserve data store integrity.
36 * @author Thomas Pantelis
38 class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
39 private static final YangInstanceIdentifier ROOT = YangInstanceIdentifier.builder().build();
40 private final ShardDataTree store;
41 private final String shardName;
42 private final Logger log;
43 private final Set<URI> validNamespaces;
44 private PruningDataTreeModification transaction;
46 private final byte[] restoreFromSnapshot;
48 ShardRecoveryCoordinator(ShardDataTree store, SchemaContext schemaContext, byte[] restoreFromSnapshot,
49 String shardName, Logger log) {
50 this.store = Preconditions.checkNotNull(store);
51 this.restoreFromSnapshot = restoreFromSnapshot;
52 this.shardName = shardName;
54 this.validNamespaces = NormalizedNodePruner.namespaces(schemaContext);
58 public void startLogRecoveryBatch(int maxBatchSize) {
59 log.debug("{}: starting log recovery batch with max size {}", shardName, maxBatchSize);
60 transaction = new PruningDataTreeModification(store.newModification(), validNamespaces);
65 public void appendRecoveredLogEntry(Payload payload) {
66 Preconditions.checkState(transaction != null, "call startLogRecovery before calling appendRecoveredLogEntry");
69 if (payload instanceof DataTreeCandidatePayload) {
70 DataTreeCandidates.applyToModification(transaction, ((DataTreeCandidatePayload)payload).getCandidate());
72 } else if (payload instanceof CompositeModificationPayload) {
73 MutableCompositeModification.fromSerializable(
74 ((CompositeModificationPayload) payload).getModification()).apply(transaction);
76 } else if (payload instanceof CompositeModificationByteStringPayload) {
77 MutableCompositeModification.fromSerializable(
78 ((CompositeModificationByteStringPayload) payload).getModification()).apply(transaction);
81 log.error("{}: Unknown payload {} received during recovery", shardName, payload);
83 } catch (IOException e) {
84 log.error("{}: Error extracting ModificationPayload", shardName, e);
88 private void commitTransaction(PruningDataTreeModification tx) throws DataValidationFailedException {
89 store.commit(tx.getDelegate());
93 * Applies the current batched log entries to the data store.
96 public void applyCurrentLogRecoveryBatch() {
97 Preconditions.checkState(transaction != null, "call startLogRecovery before calling applyCurrentLogRecoveryBatch");
99 log.debug("{}: Applying current log recovery batch with size {}", shardName, size);
101 commitTransaction(transaction);
102 } catch (DataValidationFailedException e) {
103 log.error("{}: Failed to apply recovery batch", shardName, e);
109 * Applies a recovered snapshot to the data store.
111 * @param snapshotBytes the serialized snapshot
114 public void applyRecoverySnapshot(final byte[] snapshotBytes) {
115 log.debug("{}: Applying recovered snapshot", shardName);
117 final NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
118 final PruningDataTreeModification tx = new PruningDataTreeModification(store.newModification(), validNamespaces);
119 tx.write(ROOT, node);
121 commitTransaction(tx);
122 } catch (DataValidationFailedException e) {
123 log.error("{}: Failed to apply recovery snapshot", shardName, e);
128 public byte[] getRestoreFromSnapshot() {
129 return restoreFromSnapshot;