IMDS: trim down commit overhead
[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.collect.Lists;
11 import java.io.IOException;
12 import java.util.List;
13 import org.opendaylight.controller.cluster.datastore.modification.ModificationPayload;
14 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
15 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
16 import org.opendaylight.controller.cluster.raft.RaftActorRecoveryCohort;
17 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationByteStringPayload;
18 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationPayload;
19 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
26 import org.slf4j.Logger;
27
28 /**
29  * Coordinates persistence recovery of journal log entries and snapshots for a shard. Each snapshot
30  * and journal log entry batch are de-serialized and applied to their own write transaction
31  * instance in parallel on a thread pool for faster recovery time. However the transactions are
32  * committed to the data store in the order the corresponding snapshot or log batch are received
33  * to preserve data store integrity.
34  *
35  * @author Thomas Pantelis
36  */
37 class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
38     private static final YangInstanceIdentifier ROOT = YangInstanceIdentifier.builder().build();
39     private final ShardDataTree store;
40     private List<ModificationPayload> currentLogRecoveryBatch;
41     private final String shardName;
42     private final Logger log;
43
44     ShardRecoveryCoordinator(ShardDataTree store, String shardName, Logger log) {
45         this.store = store;
46         this.shardName = shardName;
47         this.log = log;
48     }
49
50     @Override
51     public void startLogRecoveryBatch(int maxBatchSize) {
52         currentLogRecoveryBatch = Lists.newArrayListWithCapacity(maxBatchSize);
53
54         log.debug("{}: starting log recovery batch with max size {}", shardName, maxBatchSize);
55     }
56
57     @Override
58     public void appendRecoveredLogEntry(Payload payload) {
59         try {
60             if(payload instanceof ModificationPayload) {
61                 currentLogRecoveryBatch.add((ModificationPayload) payload);
62             } else if (payload instanceof CompositeModificationPayload) {
63                 currentLogRecoveryBatch.add(new ModificationPayload(MutableCompositeModification.fromSerializable(
64                         ((CompositeModificationPayload) payload).getModification())));
65             } else if (payload instanceof CompositeModificationByteStringPayload) {
66                 currentLogRecoveryBatch.add(new ModificationPayload(MutableCompositeModification.fromSerializable(
67                         ((CompositeModificationByteStringPayload) payload).getModification())));
68             } else {
69                 log.error("{}: Unknown payload {} received during recovery", shardName, payload);
70             }
71         } catch (IOException e) {
72             log.error("{}: Error extracting ModificationPayload", shardName, e);
73         }
74
75     }
76
77     private void commitTransaction(ReadWriteShardDataTreeTransaction transaction) {
78         DOMStoreThreePhaseCommitCohort commitCohort = store.finishTransaction(transaction);
79         try {
80             commitCohort.preCommit().get();
81             commitCohort.commit().get();
82         } catch (Exception e) {
83             log.error("{}: Failed to commit Tx on recovery", shardName, e);
84         }
85     }
86
87     /**
88      * Applies the current batched log entries to the data store.
89      */
90     @Override
91     public void applyCurrentLogRecoveryBatch() {
92         log.debug("{}: Applying current log recovery batch with size {}", shardName, currentLogRecoveryBatch.size());
93
94         ReadWriteShardDataTreeTransaction writeTx = store.newReadWriteTransaction(shardName + "-recovery", null);
95         DataTreeModification snapshot = writeTx.getSnapshot();
96         for (ModificationPayload payload : currentLogRecoveryBatch) {
97             try {
98                 MutableCompositeModification.fromSerializable(payload.getModification()).apply(snapshot);
99             } catch (Exception e) {
100                 log.error("{}: Error extracting ModificationPayload", shardName, e);
101             }
102         }
103
104         commitTransaction(writeTx);
105
106         currentLogRecoveryBatch = null;
107     }
108
109     /**
110      * Applies a recovered snapshot to the data store.
111      *
112      * @param snapshotBytes the serialized snapshot
113      */
114     @Override
115     public void applyRecoverySnapshot(final byte[] snapshotBytes) {
116         log.debug("{}: Applying recovered snapshot", shardName);
117
118         // Intentionally bypass normal transaction to side-step persistence/replication
119         final DataTree tree = store.getDataTree();
120         DataTreeModification writeTx = tree.takeSnapshot().newModification();
121
122         NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
123
124         writeTx.write(ROOT, node);
125         writeTx.ready();
126         try {
127             tree.validate(writeTx);
128             tree.commit(tree.prepare(writeTx));
129         } catch (DataValidationFailedException e) {
130             log.error("{}: Failed to validate recovery snapshot", shardName, e);
131         }
132     }
133 }