Remove ModificationPayload class
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardRecoveryCoordinator.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
10 import com.google.common.base.Preconditions;
11 import java.io.IOException;
12 import java.net.URI;
13 import java.util.Set;
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;
28
29 /**
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.
35  *
36  * @author Thomas Pantelis
37  */
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;
45     private int size;
46     private final byte[] restoreFromSnapshot;
47
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;
53         this.log = log;
54         this.validNamespaces = NormalizedNodePruner.namespaces(schemaContext);
55     }
56
57     @Override
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);
61         size = 0;
62     }
63
64     @Override
65     public void appendRecoveredLogEntry(Payload payload) {
66         Preconditions.checkState(transaction != null, "call startLogRecovery before calling appendRecoveredLogEntry");
67
68         try {
69             if (payload instanceof DataTreeCandidatePayload) {
70                 DataTreeCandidates.applyToModification(transaction, ((DataTreeCandidatePayload)payload).getCandidate());
71                 size++;
72             } else if (payload instanceof CompositeModificationPayload) {
73                 MutableCompositeModification.fromSerializable(
74                     ((CompositeModificationPayload) payload).getModification()).apply(transaction);
75                 size++;
76             } else if (payload instanceof CompositeModificationByteStringPayload) {
77                 MutableCompositeModification.fromSerializable(
78                         ((CompositeModificationByteStringPayload) payload).getModification()).apply(transaction);
79                 size++;
80             } else {
81                 log.error("{}: Unknown payload {} received during recovery", shardName, payload);
82             }
83         } catch (IOException e) {
84             log.error("{}: Error extracting ModificationPayload", shardName, e);
85         }
86     }
87
88     private void commitTransaction(PruningDataTreeModification tx) throws DataValidationFailedException {
89         store.commit(tx.getDelegate());
90     }
91
92     /**
93      * Applies the current batched log entries to the data store.
94      */
95     @Override
96     public void applyCurrentLogRecoveryBatch() {
97         Preconditions.checkState(transaction != null, "call startLogRecovery before calling applyCurrentLogRecoveryBatch");
98
99         log.debug("{}: Applying current log recovery batch with size {}", shardName, size);
100         try {
101             commitTransaction(transaction);
102         } catch (DataValidationFailedException e) {
103             log.error("{}: Failed to apply recovery batch", shardName, e);
104         }
105         transaction = null;
106     }
107
108     /**
109      * Applies a recovered snapshot to the data store.
110      *
111      * @param snapshotBytes the serialized snapshot
112      */
113     @Override
114     public void applyRecoverySnapshot(final byte[] snapshotBytes) {
115         log.debug("{}: Applying recovered snapshot", shardName);
116
117         final NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
118         final PruningDataTreeModification tx = new PruningDataTreeModification(store.newModification(), validNamespaces);
119         tx.write(ROOT, node);
120         try {
121             commitTransaction(tx);
122         } catch (DataValidationFailedException e) {
123             log.error("{}: Failed to apply recovery snapshot", shardName, e);
124         }
125     }
126
127     @Override
128     public byte[] getRestoreFromSnapshot() {
129         return restoreFromSnapshot;
130     }
131 }