X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FShardRecoveryCoordinator.java;h=70a701075ec6b5867e7a765629fcc65d6b72d8cb;hb=bfb38b42aa055a2478a5972f15ca4246dee796e9;hp=01a124b6977c801e3f273c57341efe91d97c52b2;hpb=f9a9cd1ea40d2477ccb16b03c71a87595226595a;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardRecoveryCoordinator.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardRecoveryCoordinator.java index 01a124b697..70a701075e 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardRecoveryCoordinator.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardRecoveryCoordinator.java @@ -7,20 +7,13 @@ */ package org.opendaylight.controller.cluster.datastore; -import com.google.common.collect.Lists; -import java.io.IOException; -import java.util.List; -import org.opendaylight.controller.cluster.datastore.modification.ModificationPayload; -import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification; -import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils; +import com.google.common.base.Preconditions; +import com.google.common.base.Throwables; +import java.io.File; +import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot; +import org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeXMLOutput; import org.opendaylight.controller.cluster.raft.RaftActorRecoveryCohort; -import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationByteStringPayload; -import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationPayload; import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload; -import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore; -import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort; -import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.slf4j.Logger; @@ -31,55 +24,40 @@ import org.slf4j.Logger; * committed to the data store in the order the corresponding snapshot or log batch are received * to preserve data store integrity. * - * @author Thomas Panetelis + * @author Thomas Pantelis */ class ShardRecoveryCoordinator implements RaftActorRecoveryCohort { - - private final InMemoryDOMDataStore store; - private List currentLogRecoveryBatch; + private final ShardDataTree store; private final String shardName; private final Logger log; + private final byte[] restoreFromSnapshot; + + private boolean open; + + ShardRecoveryCoordinator(final ShardDataTree store, final byte[] restoreFromSnapshot, final String shardName, final Logger log) { + this.store = Preconditions.checkNotNull(store); + this.shardName = Preconditions.checkNotNull(shardName); + this.log = Preconditions.checkNotNull(log); - ShardRecoveryCoordinator(InMemoryDOMDataStore store, String shardName, Logger log) { - this.store = store; - this.shardName = shardName; - this.log = log; + this.restoreFromSnapshot = restoreFromSnapshot; } @Override - public void startLogRecoveryBatch(int maxBatchSize) { - currentLogRecoveryBatch = Lists.newArrayListWithCapacity(maxBatchSize); - + public void startLogRecoveryBatch(final int maxBatchSize) { log.debug("{}: starting log recovery batch with max size {}", shardName, maxBatchSize); + open = true; } @Override - public void appendRecoveredLogEntry(Payload payload) { - try { - if(payload instanceof ModificationPayload) { - currentLogRecoveryBatch.add((ModificationPayload) payload); - } else if (payload instanceof CompositeModificationPayload) { - currentLogRecoveryBatch.add(new ModificationPayload(MutableCompositeModification.fromSerializable( - ((CompositeModificationPayload) payload).getModification()))); - } else if (payload instanceof CompositeModificationByteStringPayload) { - currentLogRecoveryBatch.add(new ModificationPayload(MutableCompositeModification.fromSerializable( - ((CompositeModificationByteStringPayload) payload).getModification()))); - } else { - log.error("{}: Unknown payload {} received during recovery", shardName, payload); - } - } catch (IOException e) { - log.error("{}: Error extracting ModificationPayload", shardName, e); - } - - } + public void appendRecoveredLogEntry(final Payload payload) { + Preconditions.checkState(open, "call startLogRecovery before calling appendRecoveredLogEntry"); - private void commitTransaction(DOMStoreWriteTransaction transaction) { - DOMStoreThreePhaseCommitCohort commitCohort = transaction.ready(); try { - commitCohort.preCommit().get(); - commitCohort.commit().get(); + store.applyRecoveryPayload(payload); } catch (Exception e) { - log.error("{}: Failed to commit Tx on recovery", shardName, e); + log.error("{}: failed to apply payload {}", shardName, payload, e); + throw new IllegalStateException(String.format("%s: Failed to apply recovery payload %s", + shardName, payload), e); } } @@ -88,20 +66,15 @@ class ShardRecoveryCoordinator implements RaftActorRecoveryCohort { */ @Override public void applyCurrentLogRecoveryBatch() { - log.debug("{}: Applying current log recovery batch with size {}", shardName, currentLogRecoveryBatch.size()); - - DOMStoreWriteTransaction writeTx = store.newWriteOnlyTransaction(); - for(ModificationPayload payload: currentLogRecoveryBatch) { - try { - MutableCompositeModification.fromSerializable(payload.getModification()).apply(writeTx); - } catch (Exception e) { - log.error("{}: Error extracting ModificationPayload", shardName, e); - } - } - - commitTransaction(writeTx); + Preconditions.checkState(open, "call startLogRecovery before calling applyCurrentLogRecoveryBatch"); + open = false; + } - currentLogRecoveryBatch = null; + private File writeRoot(final String kind, final NormalizedNode node) { + final File file = new File(System.getProperty("karaf.data", "."), + "failed-" + kind + "-snapshot-" + shardName + ".xml"); + NormalizedNodeXMLOutput.toFile(file, node); + return file; } /** @@ -111,14 +84,29 @@ class ShardRecoveryCoordinator implements RaftActorRecoveryCohort { */ @Override public void applyRecoverySnapshot(final byte[] snapshotBytes) { - log.debug("{}: Applyng recovered sbapshot", shardName); + log.debug("{}: Applying recovered snapshot", shardName); - DOMStoreWriteTransaction writeTx = store.newWriteOnlyTransaction(); + final ShardDataTreeSnapshot snapshot; + try { + snapshot = ShardDataTreeSnapshot.deserialize(snapshotBytes); + } catch (Exception e) { + log.error("{}: failed to deserialize snapshot", shardName, e); + throw Throwables.propagate(e); + } - NormalizedNode node = SerializationUtils.deserializeNormalizedNode(snapshotBytes); + try { + store.applyRecoverySnapshot(snapshot); + } catch (Exception e) { + log.error("{}: failed to apply snapshot {}", shardName, snapshot, e); - writeTx.write(YangInstanceIdentifier.builder().build(), node); + final File f = writeRoot("recovery", snapshot.getRootNode().orElse(null)); + throw new IllegalStateException(String.format( + "%s: Failed to apply recovery snapshot. Node data was written to file %s", shardName, f), e); + } + } - commitTransaction(writeTx); + @Override + public byte[] getRestoreFromSnapshot() { + return restoreFromSnapshot; } }