*/
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 static com.google.common.base.Preconditions.checkState;
+import static java.util.Objects.requireNonNull;
+
+import java.io.File;
+import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
+import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
+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.persisted.Snapshot;
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;
* 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 {
+abstract class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
+ private static final class Simple extends ShardRecoveryCoordinator {
+ Simple(final ShardDataTree store, final String shardName, final Logger log) {
+ super(store, shardName, log);
+ }
+
+ @Override
+ public Snapshot getRestoreFromSnapshot() {
+ return null;
+ }
+ }
+
+ private static final class WithSnapshot extends ShardRecoveryCoordinator {
+ private final Snapshot restoreFromSnapshot;
+
+ WithSnapshot(final ShardDataTree store, final String shardName, final Logger log, final Snapshot snapshot) {
+ super(store, shardName, log);
+ this.restoreFromSnapshot = requireNonNull(snapshot);
+ }
- private final InMemoryDOMDataStore store;
- private List<ModificationPayload> currentLogRecoveryBatch;
+ @Override
+ public Snapshot getRestoreFromSnapshot() {
+ return restoreFromSnapshot;
+ }
+ }
+
+ private final ShardDataTree store;
private final String shardName;
private final Logger log;
- ShardRecoveryCoordinator(InMemoryDOMDataStore store, String shardName, Logger log) {
- this.store = store;
- this.shardName = shardName;
- this.log = log;
+ private boolean open;
+
+ ShardRecoveryCoordinator(final ShardDataTree store, final String shardName, final Logger log) {
+ this.store = requireNonNull(store);
+ this.shardName = requireNonNull(shardName);
+ this.log = requireNonNull(log);
}
- @Override
- public void startLogRecoveryBatch(int maxBatchSize) {
- currentLogRecoveryBatch = Lists.newArrayListWithCapacity(maxBatchSize);
+ static ShardRecoveryCoordinator create(final ShardDataTree store, final String shardName, final Logger log) {
+ return new Simple(store, shardName, log);
+ }
- log.debug("{}: starting log recovery batch with max size {}", shardName, maxBatchSize);
+ static ShardRecoveryCoordinator forSnapshot(final ShardDataTree store, final String shardName, final Logger log,
+ final Snapshot snapshot) {
+ return new WithSnapshot(store, shardName, log, snapshot);
}
@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 startLogRecoveryBatch(final int maxBatchSize) {
+ log.debug("{}: starting log recovery batch with max size {}", shardName, maxBatchSize);
+ open = true;
}
- private void commitTransaction(DOMStoreWriteTransaction transaction) {
- DOMStoreThreePhaseCommitCohort commitCohort = transaction.ready();
+ @Override
+ @SuppressWarnings("checkstyle:IllegalCatch")
+ public void appendRecoveredLogEntry(final Payload payload) {
+ checkState(open, "call startLogRecovery before calling appendRecoveredLogEntry");
+
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);
}
}
*/
@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);
+ 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-recovery-" + kind + "-" + shardName + ".xml");
+ NormalizedNodeXMLOutput.toFile(file, node);
+ return file;
}
/**
* Applies a recovered snapshot to the data store.
*
- * @param snapshotBytes the serialized snapshot
+ * @param snapshotState the serialized snapshot
*/
@Override
- public void applyRecoverySnapshot(final byte[] snapshotBytes) {
- log.debug("{}: Applyng recovered sbapshot", shardName);
-
- DOMStoreWriteTransaction writeTx = store.newWriteOnlyTransaction();
-
- NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
-
- writeTx.write(YangInstanceIdentifier.builder().build(), node);
+ @SuppressWarnings("checkstyle:IllegalCatch")
+ public void applyRecoverySnapshot(final Snapshot.State snapshotState) {
+ if (!(snapshotState instanceof ShardSnapshotState)) {
+ log.debug("{}: applyRecoverySnapshot ignoring snapshot: {}", shardName, snapshotState);
+ return;
+ }
- commitTransaction(writeTx);
+ log.debug("{}: Applying recovered snapshot", shardName);
+ final ShardSnapshotState shardSnapshotState = (ShardSnapshotState)snapshotState;
+ try {
+ store.applyRecoverySnapshot(shardSnapshotState);
+ } catch (Exception e) {
+ final ShardDataTreeSnapshot shardSnapshot = shardSnapshotState.getSnapshot();
+ final File f = writeRoot("snapshot", shardSnapshot.getRootNode().orElse(null));
+ throw new IllegalStateException(String.format(
+ "%s: Failed to apply recovery snapshot %s. Node data was written to file %s",
+ shardName, shardSnapshot, f), e);
+ }
}
}