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.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;
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.
35 * @author Thomas Pantelis
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;
44 ShardRecoveryCoordinator(ShardDataTree store, String shardName, Logger log) {
46 this.shardName = shardName;
51 public void startLogRecoveryBatch(int maxBatchSize) {
52 currentLogRecoveryBatch = Lists.newArrayListWithCapacity(maxBatchSize);
54 log.debug("{}: starting log recovery batch with max size {}", shardName, maxBatchSize);
58 public void appendRecoveredLogEntry(Payload payload) {
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())));
69 log.error("{}: Unknown payload {} received during recovery", shardName, payload);
71 } catch (IOException e) {
72 log.error("{}: Error extracting ModificationPayload", shardName, e);
77 private void commitTransaction(ReadWriteShardDataTreeTransaction transaction) {
78 DOMStoreThreePhaseCommitCohort commitCohort = store.finishTransaction(transaction);
80 commitCohort.preCommit().get();
81 commitCohort.commit().get();
82 } catch (Exception e) {
83 log.error("{}: Failed to commit Tx on recovery", shardName, e);
88 * Applies the current batched log entries to the data store.
91 public void applyCurrentLogRecoveryBatch() {
92 log.debug("{}: Applying current log recovery batch with size {}", shardName, currentLogRecoveryBatch.size());
94 ReadWriteShardDataTreeTransaction writeTx = store.newReadWriteTransaction(shardName + "-recovery", null);
95 DataTreeModification snapshot = writeTx.getSnapshot();
96 for (ModificationPayload payload : currentLogRecoveryBatch) {
98 MutableCompositeModification.fromSerializable(payload.getModification()).apply(snapshot);
99 } catch (Exception e) {
100 log.error("{}: Error extracting ModificationPayload", shardName, e);
104 commitTransaction(writeTx);
106 currentLogRecoveryBatch = null;
110 * Applies a recovered snapshot to the data store.
112 * @param snapshotBytes the serialized snapshot
115 public void applyRecoverySnapshot(final byte[] snapshotBytes) {
116 log.debug("{}: Applying recovered snapshot", shardName);
118 // Intentionally bypass normal transaction to side-step persistence/replication
119 final DataTree tree = store.getDataTree();
120 DataTreeModification writeTx = tree.takeSnapshot().newModification();
122 NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
124 writeTx.write(ROOT, node);
127 tree.validate(writeTx);
128 tree.commit(tree.prepare(writeTx));
129 } catch (DataValidationFailedException e) {
130 log.error("{}: Failed to validate recovery snapshot", shardName, e);