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.ModificationPayload;
15 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
16 import org.opendaylight.controller.cluster.datastore.node.utils.transformer.NormalizedNodePruner;
17 import org.opendaylight.controller.cluster.datastore.utils.PruningDataTreeModification;
18 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
19 import org.opendaylight.controller.cluster.raft.RaftActorRecoveryCohort;
20 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationByteStringPayload;
21 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationPayload;
22 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.slf4j.Logger;
33 * Coordinates persistence recovery of journal log entries and snapshots for a shard. Each snapshot
34 * and journal log entry batch are de-serialized and applied to their own write transaction
35 * instance in parallel on a thread pool for faster recovery time. However the transactions are
36 * committed to the data store in the order the corresponding snapshot or log batch are received
37 * to preserve data store integrity.
39 * @author Thomas Pantelis
41 class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
42 private static final YangInstanceIdentifier ROOT = YangInstanceIdentifier.builder().build();
43 private final DataTree store;
44 private final String shardName;
45 private final Logger log;
46 private final Set<URI> validNamespaces;
47 private PruningDataTreeModification transaction;
50 ShardRecoveryCoordinator(ShardDataTree store, SchemaContext schemaContext, String shardName, Logger log) {
51 this.store = store.getDataTree();
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.takeSnapshot().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 ModificationPayload) {
73 MutableCompositeModification.fromSerializable(
74 ((ModificationPayload) payload).getModification()).apply(transaction);
76 } else if (payload instanceof CompositeModificationPayload) {
77 MutableCompositeModification.fromSerializable(
78 ((CompositeModificationPayload) payload).getModification()).apply(transaction);
80 } else if (payload instanceof CompositeModificationByteStringPayload) {
81 MutableCompositeModification.fromSerializable(
82 ((CompositeModificationByteStringPayload) payload).getModification()).apply(transaction);
85 log.error("{}: Unknown payload {} received during recovery", shardName, payload);
87 } catch (IOException | ClassNotFoundException e) {
88 log.error("{}: Error extracting ModificationPayload", shardName, e);
92 private void commitTransaction(PruningDataTreeModification tx) throws DataValidationFailedException {
93 DataTreeModification delegate = tx.getDelegate();
95 store.validate(delegate);
96 store.commit(store.prepare(delegate));
100 * Applies the current batched log entries to the data store.
103 public void applyCurrentLogRecoveryBatch() {
104 Preconditions.checkState(transaction != null, "call startLogRecovery before calling applyCurrentLogRecoveryBatch");
106 log.debug("{}: Applying current log recovery batch with size {}", shardName, size);
108 commitTransaction(transaction);
109 } catch (DataValidationFailedException e) {
110 log.error("{}: Failed to apply recovery batch", shardName, e);
116 * Applies a recovered snapshot to the data store.
118 * @param snapshotBytes the serialized snapshot
121 public void applyRecoverySnapshot(final byte[] snapshotBytes) {
122 log.debug("{}: Applying recovered snapshot", shardName);
124 final NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
125 final PruningDataTreeModification tx = new PruningDataTreeModification(store.takeSnapshot().newModification(), validNamespaces);
126 tx.write(ROOT, node);
128 commitTransaction(tx);
129 } catch (DataValidationFailedException e) {
130 log.error("{}: Failed to apply recovery snapshot", shardName, e);