*
* @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 = Preconditions.checkNotNull(snapshot);
+ }
+
+ @Override
+ public Snapshot getRestoreFromSnapshot() {
+ return restoreFromSnapshot;
+ }
+ }
+
private final ShardDataTree store;
private final String shardName;
private final Logger log;
- private final Snapshot restoreFromSnapshot;
private boolean open;
- ShardRecoveryCoordinator(final ShardDataTree store, final Snapshot restoreFromSnapshot, final String shardName,
- final Logger log) {
+ ShardRecoveryCoordinator(final ShardDataTree store, final String shardName, final Logger log) {
this.store = Preconditions.checkNotNull(store);
this.shardName = Preconditions.checkNotNull(shardName);
this.log = Preconditions.checkNotNull(log);
+ }
+
+ static ShardRecoveryCoordinator create(final ShardDataTree store, final String shardName, final Logger log) {
+ return new Simple(store, shardName, log);
+ }
- this.restoreFromSnapshot = restoreFromSnapshot;
+ static ShardRecoveryCoordinator forSnapshot(final ShardDataTree store, final String shardName, final Logger log,
+ final Snapshot snapshot) {
+ return new WithSnapshot(store, shardName, log, snapshot);
}
@Override
shardName, shardSnapshot, f), e);
}
}
-
- @Override
- public Snapshot getRestoreFromSnapshot() {
- return restoreFromSnapshot;
- }
}
package org.opendaylight.controller.cluster.datastore;
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import com.google.common.base.Optional;
import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
import org.opendaylight.yangtools.yang.data.impl.schema.tree.SchemaValidationFailedException;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ShardRecoveryCoordinatorTest extends AbstractTest {
+ private static final Logger FOO_LOGGER = LoggerFactory.getLogger("foo");
private ShardDataTree peopleDataTree;
private SchemaContext peopleSchemaContext;
private SchemaContext carsSchemaContext;
+ private ShardRecoveryCoordinator coordinator;
@Before
public void setUp() {
final Shard mockShard = Mockito.mock(Shard.class);
peopleDataTree = new ShardDataTree(mockShard, peopleSchemaContext, TreeType.OPERATIONAL);
+ coordinator = ShardRecoveryCoordinator.create(peopleDataTree, "foobar", FOO_LOGGER);
+ coordinator.startLogRecoveryBatch(10);
}
@Test
public void testAppendRecoveredLogEntryCommitTransactionPayload() throws IOException {
- final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
- null, "foobar", LoggerFactory.getLogger("foo"));
- coordinator.startLogRecoveryBatch(10);
try {
coordinator.appendRecoveredLogEntry(CommitTransactionPayload.create(nextTransactionId(), createCar()));
} catch (final SchemaValidationFailedException e) {
@Test
public void testApplyRecoverySnapshot() {
- final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
- null, "foobar", LoggerFactory.getLogger("foo"));
- coordinator.startLogRecoveryBatch(10);
-
coordinator.applyRecoverySnapshot(createSnapshot());
- assertEquals(false, readCars(peopleDataTree).isPresent());
- assertEquals(true, readPeople(peopleDataTree).isPresent());
+ assertFalse(readCars(peopleDataTree).isPresent());
+ assertTrue(readPeople(peopleDataTree).isPresent());
}
@Test
public void testApplyCurrentLogRecoveryBatch() {
- final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
- null, "foobar", LoggerFactory.getLogger("foo"));
- coordinator.startLogRecoveryBatch(10);
-
try {
coordinator.applyCurrentLogRecoveryBatch();
} catch (final IllegalArgumentException e) {