Skip to content

Commit

Permalink
Fix: Use Replace for manifest.
Browse files Browse the repository at this point in the history
A while back* we stopped deleting the manifest document during
refurbish. The reason was that if we delete the manifest on a refurbish,
it's pretty much inevitable that every FormatDriver would need to
disconnect and reconnect, even if it were not otherwise necessary,
because the database would temporarily be in a state where the
FormatDriver cannot determine whether its local state is correct
anymore. Even a FormatDriver refurbishing to its own format, which
should not cause a disruption at all, would cause an otherwise
unnecessary reconnection cycle.

One consequence of this change was that the UPDATE operation used to
change from, say, sequoia to pando format, would simply write to the
manifest's "pando" field, leaving the manifest document in an invalid
state where it has format configurations for both sequoia and pando.

This change fixes this by using a REPLACE instead of an UPDATE.

* Commit f40bfad
  • Loading branch information
prdoyle committed Feb 9, 2024
1 parent 2035d54 commit 9c5629a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.CountOptions;
import com.mongodb.client.model.ReplaceOptions;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.model.changestream.ChangeStreamDocument;
import com.mongodb.client.model.changestream.OperationType;
Expand Down Expand Up @@ -49,6 +50,7 @@
import static com.mongodb.client.model.Projections.fields;
import static com.mongodb.client.model.Projections.include;
import static com.mongodb.client.model.changestream.OperationType.INSERT;
import static com.mongodb.client.model.changestream.OperationType.REPLACE;
import static io.vena.bosk.Path.parseParameterized;
import static io.vena.bosk.drivers.mongo.BsonSurgeon.docSegments;
import static io.vena.bosk.drivers.mongo.Formatter.REVISION_ZERO;
Expand Down Expand Up @@ -242,11 +244,10 @@ public void initializeCollection(StateAndMetadata<R> priorContents) {
private void writeManifest() {
BsonDocument doc = new BsonDocument("_id", MANIFEST_ID);
doc.putAll((BsonDocument) formatter.object2bsonValue(Manifest.forPando(format), Manifest.class));
BsonDocument update = new BsonDocument("$set", doc);
BsonDocument filter = new BsonDocument("_id", MANIFEST_ID);
UpdateOptions options = new UpdateOptions().upsert(true);
LOGGER.debug("| Initial manifest: {}", doc);
UpdateResult result = collection.updateOne(filter, update, options);
ReplaceOptions options = new ReplaceOptions().upsert(true);
UpdateResult result = collection.replaceOne(filter, doc, options);
LOGGER.debug("| Manifest result: {}", result);
}

Expand Down Expand Up @@ -447,7 +448,8 @@ private Reference<?> documentID2MainRef(String pipedPath, ChangeStreamDocument<B
* so incompatible database changes don't go unnoticed.
*/
private void onManifestEvent(ChangeStreamDocument<BsonDocument> event) throws UnprocessableEventException {
if (event.getOperationType() == INSERT) {
LOGGER.debug("onManifestEvent({})", event.getOperationType().name());
if (event.getOperationType() == INSERT || event.getOperationType() == REPLACE) {
BsonDocument manifestDoc = requireNonNull(event.getFullDocument());
Manifest manifest;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.ReplaceOptions;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.model.changestream.ChangeStreamDocument;
import com.mongodb.client.model.changestream.OperationType;
Expand Down Expand Up @@ -35,6 +36,7 @@
import static com.mongodb.client.model.Projections.fields;
import static com.mongodb.client.model.Projections.include;
import static com.mongodb.client.model.changestream.OperationType.INSERT;
import static com.mongodb.client.model.changestream.OperationType.REPLACE;
import static io.vena.bosk.drivers.mongo.Formatter.REVISION_ZERO;
import static io.vena.bosk.drivers.mongo.Formatter.dottedFieldNameOf;
import static io.vena.bosk.drivers.mongo.Formatter.enclosingReference;
Expand Down Expand Up @@ -134,7 +136,8 @@ BsonState loadBsonState() throws UninitializedCollectionException {
BsonDocument document = cursor.next();
return new BsonState(
document.getDocument(DocumentFields.state.name(), null),
document.getInt64(DocumentFields.revision.name(), null), Formatter.getDiagnosticAttributesIfAny(document)
document.getInt64(DocumentFields.revision.name(), null),
Formatter.getDiagnosticAttributesIfAny(document)
);
} catch (NoSuchElementException e) {
throw new UninitializedCollectionException("No existing document", e);
Expand Down Expand Up @@ -169,11 +172,10 @@ private void writeManifest() {
assert settings.experimental().manifestMode() == CREATE_IF_ABSENT;
BsonDocument doc = new BsonDocument("_id", MANIFEST_ID);
doc.putAll((BsonDocument) formatter.object2bsonValue(Manifest.forSequoia(), Manifest.class));
BsonDocument update = new BsonDocument("$set", doc);
BsonDocument filter = new BsonDocument("_id", MANIFEST_ID);
UpdateOptions options = new UpdateOptions().upsert(true);
LOGGER.debug("| Initial manifest: {}", doc);
UpdateResult result = collection.updateOne(filter, update, options);
ReplaceOptions options = new ReplaceOptions().upsert(true);
UpdateResult result = collection.replaceOne(filter, doc, options);
LOGGER.debug("| Manifest result: {}", result);
}

Expand Down Expand Up @@ -250,7 +252,8 @@ public void onEvent(ChangeStreamDocument<BsonDocument> event) throws Unprocessab
* so incompatible database changes don't go unnoticed.
*/
private void onManifestEvent(ChangeStreamDocument<BsonDocument> event) throws UnprocessableEventException {
if (event.getOperationType() == INSERT) {
LOGGER.debug("onManifestEvent({})", event.getOperationType().name());
if (event.getOperationType() == INSERT || event.getOperationType() == REPLACE) {
BsonDocument manifest = requireNonNull(event.getFullDocument());
manifest.remove("_id");
try {
Expand Down

0 comments on commit 9c5629a

Please sign in to comment.