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.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;
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.
33 * @author Thomas Panetelis
35 class ShardRecoveryCoordinator {
37 private final InMemoryDOMDataStore store;
38 private List<ModificationPayload> currentLogRecoveryBatch;
39 private final String shardName;
40 private final Logger log;
42 ShardRecoveryCoordinator(InMemoryDOMDataStore store, String shardName, Logger log) {
44 this.shardName = shardName;
48 void startLogRecoveryBatch(int maxBatchSize) {
49 currentLogRecoveryBatch = Lists.newArrayListWithCapacity(maxBatchSize);
51 log.debug("{}: starting log recovery batch with max size {}", shardName, maxBatchSize);
54 void appendRecoveredLogPayload(Payload payload) {
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())));
65 log.error("{}: Unknown payload {} received during recovery", shardName, payload);
67 } catch (IOException e) {
68 log.error("{}: Error extracting ModificationPayload", shardName, e);
73 private void commitTransaction(DOMStoreWriteTransaction transaction) {
74 DOMStoreThreePhaseCommitCohort commitCohort = transaction.ready();
76 commitCohort.preCommit().get();
77 commitCohort.commit().get();
78 } catch (Exception e) {
79 log.error("{}: Failed to commit Tx on recovery", shardName, e);
84 * Applies the current batched log entries to the data store.
86 void applyCurrentLogRecoveryBatch() {
87 log.debug("{}: Applying current log recovery batch with size {}", shardName, currentLogRecoveryBatch.size());
89 DOMStoreWriteTransaction writeTx = store.newWriteOnlyTransaction();
90 for(ModificationPayload payload: currentLogRecoveryBatch) {
92 MutableCompositeModification.fromSerializable(payload.getModification()).apply(writeTx);
93 } catch (Exception e) {
94 log.error("{}: Error extracting ModificationPayload", shardName, e);
98 commitTransaction(writeTx);
100 currentLogRecoveryBatch = null;
104 * Applies a recovered snapshot to the data store.
106 * @param snapshotBytes the serialized snapshot
108 void applyRecoveredSnapshot(final byte[] snapshotBytes) {
109 log.debug("{}: Applyng recovered sbapshot", shardName);
111 DOMStoreWriteTransaction writeTx = store.newWriteOnlyTransaction();
113 NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
115 writeTx.write(YangInstanceIdentifier.builder().build(), node);
117 commitTransaction(writeTx);