Skip to content

Commit

Permalink
Add ManifestMode and schema evolution test
Browse files Browse the repository at this point in the history
  • Loading branch information
prdoyle committed Jul 19, 2023
1 parent 03c9a84 commit 10af6e3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.vena.bosk.drivers.mongo.Formatter.DocumentFields;
import io.vena.bosk.drivers.mongo.MappedDiagnosticContext.MDCScope;
import io.vena.bosk.drivers.mongo.MongoDriverSettings.InitialDatabaseUnavailableMode;
import io.vena.bosk.drivers.mongo.MongoDriverSettings.ManifestMode;
import io.vena.bosk.exceptions.FlushFailureException;
import io.vena.bosk.exceptions.InitializationFailureException;
import io.vena.bosk.exceptions.InvalidTypeException;
Expand Down Expand Up @@ -407,12 +408,14 @@ private FormatDriver<R> newPreferredFormatDriver() {
}

private FormatDriver<R> detectFormat() throws UninitializedCollectionException, UnrecognizedFormatException {
try (MongoCursor<Document> cursor = collection.find(new BsonDocument("_id", MANIFEST_ID)).cursor()) {
if (cursor.hasNext()) {
LOGGER.debug("Found manifest");
validateManifest(cursor.next());
} else {
LOGGER.debug("Manifest is missing; assuming Sequoia format");
if (driverSettings.experimental().manifestMode() == ManifestMode.ENABLED) {
try (MongoCursor<Document> cursor = collection.find(new BsonDocument("_id", MANIFEST_ID)).cursor()) {
if (cursor.hasNext()) {
LOGGER.debug("Found manifest");
validateManifest(cursor.next());
} else {
LOGGER.debug("Manifest is missing; assuming Sequoia format");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class MongoDriverSettings {
@Builder
public static class Experimental {
@Default long changeStreamInitialWaitMS = 20;
@Default ManifestMode manifestMode = ManifestMode.ENABLED;
}

/**
Expand Down Expand Up @@ -68,4 +69,9 @@ public enum InitialDatabaseUnavailableMode {
*/
FAIL
}

public enum ManifestMode {
DISABLED,
ENABLED,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.vena.bosk.Reference;
import io.vena.bosk.StateTreeNode;
import io.vena.bosk.drivers.mongo.Formatter.DocumentFields;
import io.vena.bosk.drivers.mongo.MongoDriverSettings.ManifestMode;
import io.vena.bosk.exceptions.FlushFailureException;
import io.vena.bosk.exceptions.InvalidTypeException;
import java.io.IOException;
Expand Down Expand Up @@ -161,7 +162,9 @@ public void initializeCollection(StateAndMetadata<R> priorContents) {
LOGGER.trace("| Options: {}", options);
UpdateResult result = collection.updateOne(filter, update, options);
LOGGER.debug("| Result: {}", result);
writeManifest();
if (settings.experimental().manifestMode() == ManifestMode.ENABLED) {
writeManifest();
}
}

private void writeManifest() {
Expand All @@ -180,12 +183,14 @@ private void writeManifest() {
*/
@Override
public void onEvent(ChangeStreamDocument<Document> event) throws UnprocessableEventException {
if (event.getDocumentKey() == null) {
throw new UnprocessableEventException("Null document key", event.getOperationType());
}
if (MANIFEST_ID.equals(event.getDocumentKey().get("_id"))) {
onManifestEvent(event);
return;
if (settings.experimental().manifestMode() == ManifestMode.ENABLED) {
if (event.getDocumentKey() == null) {
throw new UnprocessableEventException("Null document key", event.getOperationType());
}
if (MANIFEST_ID.equals(event.getDocumentKey().get("_id"))) {
onManifestEvent(event);
return;
}
}
if (!DOCUMENT_FILTER.equals(event.getDocumentKey())) {
LOGGER.debug("Ignoring event for unrecognized document key: {}", event.getDocumentKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import io.vena.bosk.Bosk;
import io.vena.bosk.Reference;
import io.vena.bosk.annotations.ReferencePath;
import io.vena.bosk.drivers.mongo.MongoDriverSettings.DatabaseFormat;
import io.vena.bosk.drivers.mongo.MongoDriverSettings.Experimental;
import io.vena.bosk.drivers.mongo.MongoDriverSettings.ManifestMode;
import io.vena.bosk.drivers.state.TestEntity;
import io.vena.bosk.exceptions.InvalidTypeException;
import io.vena.bosk.junit.ParametersByName;
Expand All @@ -24,9 +24,9 @@ public class SchemaEvolutionTest {
private final Helper toHelper;

@ParametersByName
SchemaEvolutionTest(DatabaseFormat fromFormat, DatabaseFormat toFormat) {
fromHelper = new Helper(fromFormat);
toHelper = new Helper(toFormat);
SchemaEvolutionTest(ManifestMode fromMode, ManifestMode toMode) {
fromHelper = new Helper(fromMode);
toHelper = new Helper(toMode);
}

@BeforeAll
Expand All @@ -50,13 +50,13 @@ void afterEach(TestInfo testInfo) {
}

@SuppressWarnings("unused")
static Stream<DatabaseFormat> fromFormat() {
return Stream.of(DatabaseFormat.values());
static Stream<ManifestMode> fromMode() {
return Stream.of(ManifestMode.values());
}

@SuppressWarnings("unused")
static Stream<DatabaseFormat> toFormat() {
return Stream.of(DatabaseFormat.values());
static Stream<ManifestMode> toMode() {
return fromMode();
}

@ParametersByName
Expand All @@ -80,14 +80,14 @@ private static Bosk<TestEntity> newBosk(Helper helper) {
static final class Helper extends AbstractMongoDriverTest {
final String name;

public Helper(DatabaseFormat format) {
public Helper(ManifestMode manifestMode) {
super(MongoDriverSettings.builder()
.database(SchemaEvolutionTest.class.getSimpleName())
.preferredDatabaseFormat(format)
.experimental(Experimental.builder()
.manifestMode(manifestMode)
.build())
);
this.name = format.name();
this.name = manifestMode.name();
}
}

Expand Down

0 comments on commit 10af6e3

Please sign in to comment.