Merge "Set unable-to-connect(netconf node) if no sources are available"
[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.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.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.slf4j.Logger;
25
26 /**
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.
32  *
33  * @author Thomas Panetelis
34  */
35 class ShardRecoveryCoordinator {
36
37     private final InMemoryDOMDataStore store;
38     private List<ModificationPayload> currentLogRecoveryBatch;
39     private final String shardName;
40     private final Logger log;
41
42     ShardRecoveryCoordinator(InMemoryDOMDataStore store, String shardName, Logger log) {
43         this.store = store;
44         this.shardName = shardName;
45         this.log = log;
46     }
47
48     void startLogRecoveryBatch(int maxBatchSize) {
49         currentLogRecoveryBatch = Lists.newArrayListWithCapacity(maxBatchSize);
50
51         log.debug("{}: starting log recovery batch with max size {}", shardName, maxBatchSize);
52     }
53
54     void appendRecoveredLogPayload(Payload payload) {
55         try {
56             if(payload instanceof ModificationPayload) {
57                 currentLogRecoveryBatch.add((ModificationPayload) payload);
58             } else if (payload instanceof CompositeModificationPayload) {
59                 currentLogRecoveryBatch.add(new ModificationPayload(MutableCompositeModification.fromSerializable(
60                         ((CompositeModificationPayload) payload).getModification())));
61             } else if (payload instanceof CompositeModificationByteStringPayload) {
62                 currentLogRecoveryBatch.add(new ModificationPayload(MutableCompositeModification.fromSerializable(
63                         ((CompositeModificationByteStringPayload) payload).getModification())));
64             } else {
65                 log.error("{}: Unknown payload {} received during recovery", shardName, payload);
66             }
67         } catch (IOException e) {
68             log.error("{}: Error extracting ModificationPayload", shardName, e);
69         }
70
71     }
72
73     private void commitTransaction(DOMStoreWriteTransaction transaction) {
74         DOMStoreThreePhaseCommitCohort commitCohort = transaction.ready();
75         try {
76             commitCohort.preCommit().get();
77             commitCohort.commit().get();
78         } catch (Exception e) {
79             log.error("{}: Failed to commit Tx on recovery", shardName, e);
80         }
81     }
82
83     /**
84      * Applies the current batched log entries to the data store.
85      */
86     void applyCurrentLogRecoveryBatch() {
87         log.debug("{}: Applying current log recovery batch with size {}", shardName, currentLogRecoveryBatch.size());
88
89         DOMStoreWriteTransaction writeTx = store.newWriteOnlyTransaction();
90         for(ModificationPayload payload: currentLogRecoveryBatch) {
91             try {
92                 MutableCompositeModification.fromSerializable(payload.getModification()).apply(writeTx);
93             } catch (Exception e) {
94                 log.error("{}: Error extracting ModificationPayload", shardName, e);
95             }
96         }
97
98         commitTransaction(writeTx);
99
100         currentLogRecoveryBatch = null;
101     }
102
103     /**
104      * Applies a recovered snapshot to the data store.
105      *
106      * @param snapshotBytes the serialized snapshot
107      */
108     void applyRecoveredSnapshot(final byte[] snapshotBytes) {
109         log.debug("{}: Applyng recovered sbapshot", shardName);
110
111         DOMStoreWriteTransaction writeTx = store.newWriteOnlyTransaction();
112
113         NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
114
115         writeTx.write(YangInstanceIdentifier.builder().build(), node);
116
117         commitTransaction(writeTx);
118     }
119 }