From ee2b84f3830249c154a9bbc4519b5ed7e58e3a60 Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Thu, 22 Aug 2024 11:30:48 +0800 Subject: [PATCH] feat(mongo): Replace `UPDATE_UPSERT_AFTER_OPTIONS` with `ensureIdSegment` --- .../src/main/java/me/ahoo/cosid/mongo/Documents.java | 9 ++++++++- .../me/ahoo/cosid/mongo/MongoIdSegmentCollection.java | 5 +++-- .../cosid/mongo/MongoIdSegmentDistributorFactory.java | 11 +++-------- .../reactive/MongoReactiveIdSegmentCollection.java | 2 +- .../MongoReactiveIdSegmentDistributorFactory.java | 9 ++------- .../GroupedMongoReactiveIdSegmentDistributorTest.java | 2 +- .../cosid/mongo/MongoIdSegmentDistributorTest.java | 2 +- .../mongo/MongoReactiveIdSegmentDistributorTest.java | 2 +- .../segment/CosIdMongoSegmentAutoConfiguration.java | 5 ++--- 9 files changed, 22 insertions(+), 25 deletions(-) diff --git a/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/Documents.java b/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/Documents.java index 03fe608200..87853ce4c0 100644 --- a/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/Documents.java +++ b/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/Documents.java @@ -20,8 +20,15 @@ public interface Documents { String ID_FIELD = "_id"; - + FindOneAndUpdateOptions UPDATE_AFTER_OPTIONS = new FindOneAndUpdateOptions() .returnDocument(ReturnDocument.AFTER) .maxTime(BlockingAdapter.DEFAULT_TIME_OUT.toMillis(), java.util.concurrent.TimeUnit.MILLISECONDS); + + FindOneAndUpdateOptions UPDATE_UPSERT_AFTER_OPTIONS = new FindOneAndUpdateOptions() + .upsert(true) + .returnDocument(ReturnDocument.AFTER) + .maxTime(BlockingAdapter.DEFAULT_TIME_OUT.toMillis(), java.util.concurrent.TimeUnit.MILLISECONDS); + + } diff --git a/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/MongoIdSegmentCollection.java b/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/MongoIdSegmentCollection.java index a901f42fee..1511ad3069 100644 --- a/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/MongoIdSegmentCollection.java +++ b/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/MongoIdSegmentCollection.java @@ -38,14 +38,15 @@ public long incrementAndGet(String namespacedName, long step) { Document afterDoc = cosidCollection.findOneAndUpdate( Filters.eq(Documents.ID_FIELD, namespacedName), incrementAndGetUpdates(step), - Documents.UPDATE_AFTER_OPTIONS); + Documents.UPDATE_UPSERT_AFTER_OPTIONS); assert afterDoc != null; Preconditions.checkNotNull(afterDoc, "IdSegment[%s] can not be null!", namespacedName); Long lastMaxId = afterDoc.getLong(IdSegmentOperates.LAST_MAX_ID_FIELD); return Objects.requireNonNull(lastMaxId); } - + + @Deprecated @Override public boolean ensureIdSegment(String segmentName, long offset) { if (log.isInfoEnabled()) { diff --git a/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/MongoIdSegmentDistributorFactory.java b/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/MongoIdSegmentDistributorFactory.java index d4b1db8bd0..f1b9fe39aa 100644 --- a/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/MongoIdSegmentDistributorFactory.java +++ b/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/MongoIdSegmentDistributorFactory.java @@ -28,20 +28,15 @@ */ public class MongoIdSegmentDistributorFactory implements IdSegmentDistributorFactory { private final MongoDatabase mongoDatabase; - private final boolean enableAutoInitIdSegment; - - public MongoIdSegmentDistributorFactory(MongoDatabase mongoDatabase, boolean enableAutoInitIdSegment) { + + public MongoIdSegmentDistributorFactory(MongoDatabase mongoDatabase) { this.mongoDatabase = mongoDatabase; - this.enableAutoInitIdSegment = enableAutoInitIdSegment; } @Override public IdSegmentDistributor create(IdSegmentDistributorDefinition definition) { MongoIdSegmentCollection cosIdSegmentCollection = new MongoIdSegmentCollection(mongoDatabase.getCollection(COLLECTION_NAME)); - if (enableAutoInitIdSegment) { - cosIdSegmentCollection.ensureIdSegment(definition.getNamespacedName(), definition.getOffset()); - } - + return new MongoIdSegmentDistributor(definition.getNamespace(), definition.getName(), definition.getStep(), diff --git a/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/reactive/MongoReactiveIdSegmentCollection.java b/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/reactive/MongoReactiveIdSegmentCollection.java index 0e31e0ca84..127e5133c9 100644 --- a/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/reactive/MongoReactiveIdSegmentCollection.java +++ b/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/reactive/MongoReactiveIdSegmentCollection.java @@ -44,7 +44,7 @@ public long incrementAndGet(String namespacedName, long step) { Publisher publisher = cosidCollection.findOneAndUpdate( Filters.eq(Documents.ID_FIELD, namespacedName), incrementAndGetUpdates(step), - Documents.UPDATE_AFTER_OPTIONS); + Documents.UPDATE_UPSERT_AFTER_OPTIONS); Document afterDoc = BlockingAdapter.block(publisher); assert afterDoc != null; diff --git a/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/reactive/MongoReactiveIdSegmentDistributorFactory.java b/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/reactive/MongoReactiveIdSegmentDistributorFactory.java index 3d94368447..7f185e2418 100644 --- a/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/reactive/MongoReactiveIdSegmentDistributorFactory.java +++ b/cosid-mongo/src/main/java/me/ahoo/cosid/mongo/reactive/MongoReactiveIdSegmentDistributorFactory.java @@ -25,19 +25,14 @@ public class MongoReactiveIdSegmentDistributorFactory implements IdSegmentDistributorFactory { private final MongoDatabase mongoDatabase; - private final boolean enableAutoInitIdSegment; - - public MongoReactiveIdSegmentDistributorFactory(MongoDatabase mongoDatabase, boolean enableAutoInitIdSegment) { + + public MongoReactiveIdSegmentDistributorFactory(MongoDatabase mongoDatabase) { this.mongoDatabase = mongoDatabase; - this.enableAutoInitIdSegment = enableAutoInitIdSegment; } @Override public IdSegmentDistributor create(IdSegmentDistributorDefinition definition) { IdSegmentCollection idSegmentCollection = new MongoReactiveIdSegmentCollection(mongoDatabase.getCollection(COLLECTION_NAME)); - if (enableAutoInitIdSegment) { - idSegmentCollection.ensureIdSegment(definition.getNamespacedName(), definition.getOffset()); - } return new MongoIdSegmentDistributor(definition.getNamespace(), definition.getName(), diff --git a/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/GroupedMongoReactiveIdSegmentDistributorTest.java b/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/GroupedMongoReactiveIdSegmentDistributorTest.java index 2df95e9095..6d01e6ff6c 100644 --- a/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/GroupedMongoReactiveIdSegmentDistributorTest.java +++ b/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/GroupedMongoReactiveIdSegmentDistributorTest.java @@ -34,7 +34,7 @@ void setup() { idSegmentInitializer = new MongoReactiveIdSegmentInitializer(mongoDatabase); idSegmentInitializer.ensureCosIdCollection(); distributorFactory = - new MongoReactiveIdSegmentDistributorFactory(mongoDatabase, true); + new MongoReactiveIdSegmentDistributorFactory(mongoDatabase); } @Override diff --git a/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoIdSegmentDistributorTest.java b/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoIdSegmentDistributorTest.java index e28a6ac977..8a9ba6b930 100644 --- a/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoIdSegmentDistributorTest.java +++ b/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoIdSegmentDistributorTest.java @@ -33,7 +33,7 @@ void setup() { idSegmentInitializer = new MongoIdSegmentInitializer(mongoDatabase); idSegmentInitializer.ensureCosIdCollection(); distributorFactory = - new MongoIdSegmentDistributorFactory(mongoDatabase, true); + new MongoIdSegmentDistributorFactory(mongoDatabase); } @Override diff --git a/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoReactiveIdSegmentDistributorTest.java b/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoReactiveIdSegmentDistributorTest.java index 2f3a4d0db2..35f9b1962d 100644 --- a/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoReactiveIdSegmentDistributorTest.java +++ b/cosid-mongo/src/test/java/me/ahoo/cosid/mongo/MongoReactiveIdSegmentDistributorTest.java @@ -46,7 +46,7 @@ void setup() { idSegmentInitializer.ensureCosIdCollection(); distributorFactory = - new MongoReactiveIdSegmentDistributorFactory(mongoDatabase, true); + new MongoReactiveIdSegmentDistributorFactory(mongoDatabase); } @Override diff --git a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdMongoSegmentAutoConfiguration.java b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdMongoSegmentAutoConfiguration.java index 10fcd66ee9..9251e87bd2 100644 --- a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdMongoSegmentAutoConfiguration.java +++ b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdMongoSegmentAutoConfiguration.java @@ -77,7 +77,7 @@ public MongoIdSegmentDistributorFactory mongoIdSegmentDistributorFactory(MongoCl segmentIdProperties.getDistributor().getMongo().getDatabase() ); idSegmentInitializer.ensureCosIdCollection(); - return new MongoIdSegmentDistributorFactory(mongoDatabase, true); + return new MongoIdSegmentDistributorFactory(mongoDatabase); } } @@ -103,8 +103,7 @@ public MongoReactiveIdSegmentDistributorFactory mongoReactiveIdSegmentDistributo segmentIdProperties.getDistributor().getMongo().getDatabase() ); return new MongoReactiveIdSegmentDistributorFactory( - mongoDatabase, - true); + mongoDatabase); } } }