diff options
-rw-r--r-- | jstests/core/timeseries/timeseries_compact.js | 37 | ||||
-rw-r--r-- | src/mongo/db/catalog/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/db/catalog/collection_compact.cpp | 11 | ||||
-rw-r--r-- | src/mongo/db/commands/compact.cpp | 4 |
4 files changed, 50 insertions, 3 deletions
diff --git a/jstests/core/timeseries/timeseries_compact.js b/jstests/core/timeseries/timeseries_compact.js new file mode 100644 index 00000000000..b5a3b22f1d7 --- /dev/null +++ b/jstests/core/timeseries/timeseries_compact.js @@ -0,0 +1,37 @@ +/** + * Tests that the regular collection compact command can be run against a time-series collection. + * + * The test runs commands that are not allowed with security token: compact. + * @tags: [ + * # Compact is not available on mongos. + * assumes_against_mongod_not_mongos, + * multiversion_incompatible, + * not_allowed_with_security_token, + * # Cannot compact when using the in-memory storage engine. + * requires_persistence, + * requires_timeseries, + * uses_compact, + * ] + */ +(function() { +"use strict"; + +load("jstests/core/timeseries/libs/timeseries.js"); + +TimeseriesTest.run(() => { + const coll = db.timeseries_compact; + coll.drop(); + + const timeFieldName = "time"; + assert.commandWorked( + db.createCollection(coll.getName(), {timeseries: {timeField: timeFieldName}})); + + const numDocs = 5; + for (let i = 0; i < numDocs; i++) { + assert.commandWorked(coll.insert({[timeFieldName]: ISODate(), x: i})); + } + + assert.commandWorked(db.runCommand({compact: coll.getName(), force: true})); + assert.commandWorked(db.runCommand({compact: "system.buckets." + coll.getName(), force: true})); +}); +})(); diff --git a/src/mongo/db/catalog/SConscript b/src/mongo/db/catalog/SConscript index d8e35deb6a4..310a916d2d1 100644 --- a/src/mongo/db/catalog/SConscript +++ b/src/mongo/db/catalog/SConscript @@ -540,6 +540,7 @@ env.Library( '$BUILD_DIR/mongo/db/stats/top', '$BUILD_DIR/mongo/db/storage/index_entry_comparison', '$BUILD_DIR/mongo/db/storage/key_string', + '$BUILD_DIR/mongo/db/timeseries/catalog_helper', '$BUILD_DIR/mongo/db/timeseries/timeseries_conversion_util', '$BUILD_DIR/mongo/db/timeseries/timeseries_options', '$BUILD_DIR/mongo/db/ttl_collection_cache', diff --git a/src/mongo/db/catalog/collection_compact.cpp b/src/mongo/db/catalog/collection_compact.cpp index 371ad9638a6..dbd1bfdf886 100644 --- a/src/mongo/db/catalog/collection_compact.cpp +++ b/src/mongo/db/catalog/collection_compact.cpp @@ -39,6 +39,7 @@ #include "mongo/db/index/index_descriptor.h" #include "mongo/db/index_builds_coordinator.h" #include "mongo/db/operation_context.h" +#include "mongo/db/timeseries/catalog_helper.h" #include "mongo/logv2/log.h" #include "mongo/util/assert_util.h" @@ -55,12 +56,18 @@ CollectionPtr getCollectionForCompact(OperationContext* opCtx, const NamespaceString& collectionNss) { invariant(opCtx->lockState()->isCollectionLockedForMode(collectionNss, MODE_IX)); + NamespaceString resolvedNs = collectionNss; + if (auto timeseriesOptions = timeseries::getTimeseriesOptions( + opCtx, collectionNss, /*convertToBucketsNamespace=*/true)) { + resolvedNs = collectionNss.makeTimeseriesBucketsNamespace(); + } + auto collectionCatalog = CollectionCatalog::get(opCtx); - CollectionPtr collection = collectionCatalog->lookupCollectionByNamespace(opCtx, collectionNss); + CollectionPtr collection = collectionCatalog->lookupCollectionByNamespace(opCtx, resolvedNs); if (!collection) { std::shared_ptr<const ViewDefinition> view = - collectionCatalog->lookupView(opCtx, collectionNss); + collectionCatalog->lookupView(opCtx, resolvedNs); uassert(ErrorCodes::CommandNotSupportedOnView, "can't compact a view", !view); uasserted(ErrorCodes::NamespaceNotFound, "collection does not exist"); } diff --git a/src/mongo/db/commands/compact.cpp b/src/mongo/db/commands/compact.cpp index 8de15530e3f..a3100c06340 100644 --- a/src/mongo/db/commands/compact.cpp +++ b/src/mongo/db/commands/compact.cpp @@ -96,7 +96,9 @@ public: !replCoord->getMemberState().primary() || cmdObj["force"].trueValue()); // Items in system.* cannot be moved as there might be pointers to them. - uassert(ErrorCodes::InvalidNamespace, "can't compact a system namespace", !nss.isSystem()); + uassert(ErrorCodes::InvalidNamespace, + "can't compact a system namespace", + !nss.isSystem() || nss.isTimeseriesBucketsCollection()); // This command is internal to the storage engine and should not block oplog application. ShouldNotConflictWithSecondaryBatchApplicationBlock noPBWMBlock(opCtx->lockState()); |