Compact Data Services MongoDB Databases

Over time, MongoDB’s WiredTiger storage engine accumulates fragmented free space inside data files after delete operations. Running the compact command on a collection rewrites those files in place and releases the fragmented space back to the operating system. Use this procedure when data disk usage is high and a significant volume of objects has been deleted from ARTESCA.

Prerequisites

  • SSH access to the ARTESCA bootstrap node is required.

  • Platform Administrator privileges are required.

  • All MongoDB replica set members must be in PRIMARY or SECONDARY state before starting. If any member is in another state, resolve that issue first.

Important

compact blocks writes on the collection being processed for the duration of the operation. Plan this procedure during a maintenance window or a period of low write activity.

Assess Reclaimable Space

Before running compact, determine whether there is significant space to reclaim.

  1. Run the fragmentation analysis for the my-bucket collection on shard 0. For deployments with more than one shard, repeat for each shard (data-db-mongodb-sharded-shard1-data-0, and so on). Replace my-bucket with the target collection name if needed.

    [artesca-os@bootstrap ~]$
    
    sudo kubectl --kubeconfig=/etc/kubernetes/admin.conf exec -n zenko \
      data-db-mongodb-sharded-shard0-data-0 -c mongodb -- \
      bash -c "mongosh --quiet \$MONGODB_APP_DATABASE \
        -u root -p \$MONGODB_ROOT_PASSWORD --authenticationDatabase admin \
        --eval \"
        var stats = db.getCollection('my-bucket').stats();
        var reuse = stats.wiredTiger['block-manager']['file bytes available for reuse'];
        var ratio = stats.storageSize > 0 ? (reuse / stats.storageSize) * 100 : 0;
        print('my-bucket: storageSize=' + stats.storageSize + 'B  reclaimable=' + reuse + 'B  (' + ratio.toFixed(1) + '%)');
        \""
    
    Output Example
    my-bucket: storageSize=6442450944B  reclaimable=1932735283B  (30.0%)
    

    If the reclaimable ratio is below 20% or the collection is smaller than 5 GB, this procedure is not required.

Check Topology

Note

For standard 1-node ARTESCA deployments, the MongoDB configuration has a single shard with no secondaries. Skip the topology check and go directly to the Single-node Cluster procedure.

The compact procedure depends on the number of shards and the number of replica set members per shard.

  1. Check the number of shards from the mongos router:

    [artesca-os@bootstrap ~]$
    
    sudo kubectl --kubeconfig=/etc/kubernetes/admin.conf exec -n zenko \
      data-db-mongodb-sharded-mongos-0 -c mongos -- \
      bash -c "mongosh --quiet admin \
        -u root -p \$MONGODB_ROOT_PASSWORD --authenticationDatabase admin \
        --eval 'db.adminCommand({ listShards: 1 }).shards.forEach(function(s) { print(s._id, s.host); })'"
    
    Output Example (single shard)
    data-db-mongodb-sharded-shard-0  data-db-mongodb-sharded-shard-0/host:27017
    
  2. Check the replica set members for each shard. The following example targets shard 0:

    [artesca-os@bootstrap ~]$
    
    sudo kubectl --kubeconfig=/etc/kubernetes/admin.conf exec -n zenko \
      data-db-mongodb-sharded-shard0-data-0 -c mongodb -- \
      bash -c "mongosh --quiet admin \
        -u root -p \$MONGODB_ROOT_PASSWORD --authenticationDatabase admin \
        --eval 'rs.status().members.forEach(function(m) { print(m.name, m.stateStr, \"health=\" + m.health); })'"
    
    Output Example (single primary, no secondaries)
    data-db-mongodb-sharded-shard0-data-0.…:27017  PRIMARY  health=1
    
  3. Use the following table to determine which compact procedure to follow:

    Shards

    RS members per shard

    Action

    1

    1 (primary only)

    Follow the Single-node Cluster procedure

    1

    2+ (primary + secondaries)

    Follow the Multi-node Cluster procedure

    2+

    2+ (primary + secondaries)

    Run the multi-shard transaction check below, then follow the Multi-node Cluster procedure for each shard

    If the deployment has multiple shards (like 6 nodes cluster), verify that multi-shard transactions are not in use before proceeding:

    [artesca-os@bootstrap ~]$
    
    sudo kubectl --kubeconfig=/etc/kubernetes/admin.conf exec -n zenko \
      data-db-mongodb-sharded-mongos-0 -c mongos -- \
      bash -c "mongosh --quiet admin \
        -u root -p \$MONGODB_ROOT_PASSWORD --authenticationDatabase admin \
        --eval \"const t = db.adminCommand({ serverStatus: 1 }).transactions; \
          print('twoPhaseCommit initiated: ' + t.commitTypes.twoPhaseCommit.initiated); \
          print('singleShard committed: ' + t.commitTypes.singleShard.successful);\""
    

    If twoPhaseCommit.initiated is 0, multi-shard transactions are not in use and compact is safe. If it is non-zero, contact Scality Support.

Compact Procedure

Run compact one collection at a time, checking reclaimable space between each run. Replace <collection> with the target collection name identified in Assess Reclaimable Space.

Single-node Cluster

Follow this procedure when the shard has only one member (no secondaries).

  1. Run compact on the target collection. On a single-node cluster the only member is the primary, which requires force: true:

    [artesca-os@bootstrap ~]$
    
    sudo kubectl --kubeconfig=/etc/kubernetes/admin.conf exec -n zenko \
      data-db-mongodb-sharded-shard0-data-0 -c mongodb -- \
      bash -c "mongosh --quiet \$MONGODB_APP_DATABASE \
        -u root -p \$MONGODB_ROOT_PASSWORD --authenticationDatabase admin \
        --eval 'db.runCommand({ compact: \"<collection>\", force: true })'"
    

    The command returns only when compact completes. For large collections this can take several minutes. Refer to Monitor Progress to observe a running compact operation in a second terminal.

  2. Repeat for each collection with significant reclaimable space.

  3. Re-run the Assess Reclaimable Space step to confirm that space was reclaimed.

Multi-node Cluster

Follow this procedure when mongo is configured with more than 1 replica. The safe order is:

  1. Compact each secondary first.

  2. Step down the primary.

  3. Compact the old primary.

  4. Let the old primary become primary again.

Repeat this full sequence for each collection.

  1. Export the primary and secondary pod names identified in the topology check. Adjust the pod names to match your deployment:

    [artesca-os@bootstrap ~]$
    
    export MONGO_PRIMARY_POD=data-db-mongodb-sharded-shard0-data-0
    export MONGO_SECONDARY_POD=data-db-mongodb-sharded-shard0-data-1
    

    Note

    This procedure covers one shard at a time. For deployments with more than one shard, repeat the full sequence for each shard.

  2. Run compact on the target collection on every secondary:

    [artesca-os@bootstrap ~]$
    
    sudo kubectl --kubeconfig=/etc/kubernetes/admin.conf exec -n zenko \
      "$MONGO_SECONDARY_POD" -c mongodb -- \
      bash -c "mongosh --quiet \$MONGODB_APP_DATABASE \
        -u root -p \$MONGODB_ROOT_PASSWORD --authenticationDatabase admin \
        --eval 'db.runCommand({ compact: \"<collection>\" })'"
    

    Repeat for every secondary before touching the primary.

  3. Step down the current primary to trigger a new election:

    [artesca-os@bootstrap ~]$
    
    sudo kubectl --kubeconfig=/etc/kubernetes/admin.conf exec -n zenko \
      "$MONGO_PRIMARY_POD" -c mongodb -- \
      bash -c "mongosh --quiet admin \
        -u root -p \$MONGODB_ROOT_PASSWORD --authenticationDatabase admin \
        --eval 'rs.stepDown(86400)'"
    

    Note

    The connection drops immediately after rs.stepDown runs because the pod is no longer primary. This is expected.

  4. Confirm that a new primary has been elected before continuing:

    [artesca-os@bootstrap ~]$
    
    sudo kubectl --kubeconfig=/etc/kubernetes/admin.conf exec -n zenko \
      "$MONGO_PRIMARY_POD" -c mongodb -- \
      bash -c "mongosh --quiet admin \
        -u root -p \$MONGODB_ROOT_PASSWORD --authenticationDatabase admin \
        --eval 'rs.status().members.forEach(function(m) { print(m.name, m.stateStr); })'"
    

    Run the command again until a different member appears as PRIMARY.

  5. Run compact on the old primary (now a secondary):

    [artesca-os@bootstrap ~]$
    
    sudo kubectl --kubeconfig=/etc/kubernetes/admin.conf exec -n zenko \
      "$MONGO_PRIMARY_POD" -c mongodb -- \
      bash -c "mongosh --quiet \$MONGODB_APP_DATABASE \
        -u root -p \$MONGODB_ROOT_PASSWORD --authenticationDatabase admin \
        --eval 'db.runCommand({ compact: \"<collection>\" })'"
    
  6. Once compact has returned, even if it failed, allow the old primary to be elected again. If the compact command was interrupted, for example because the session dropped, first use Monitor Progress on the old primary to confirm that no compact is still running:

    [artesca-os@bootstrap ~]$
    
    sudo kubectl --kubeconfig=/etc/kubernetes/admin.conf exec -n zenko \
      "$MONGO_PRIMARY_POD" -c mongodb -- \
      bash -c "mongosh --quiet admin \
        -u root -p \$MONGODB_ROOT_PASSWORD --authenticationDatabase admin \
        --eval 'rs.freeze(0)'"
    
  7. Confirm that the old primary has been elected again, which usually takes less than 15 seconds:

    [artesca-os@bootstrap ~]$
    
    sudo kubectl --kubeconfig=/etc/kubernetes/admin.conf exec -n zenko \
      "$MONGO_PRIMARY_POD" -c mongodb -- \
      bash -c "mongosh --quiet admin \
        -u root -p \$MONGODB_ROOT_PASSWORD --authenticationDatabase admin \
        --eval 'rs.status().members.forEach(function(m) { print(m.name, m.stateStr); })'"
    

    Run the command again until $MONGO_PRIMARY_POD appears as PRIMARY.

  8. Repeat steps 2–7 for each collection with significant reclaimable space.

  9. Re-run the Assess Reclaimable Space step to confirm that space was reclaimed.

Monitor Progress

While compact runs, open a second terminal on the bootstrap node and check the current operation on the pod being compacted:

[artesca-os@bootstrap ~]$
export POD_BEING_COMPACTED=data-db-mongodb-sharded-shard0-data-0

sudo kubectl --kubeconfig=/etc/kubernetes/admin.conf exec -n zenko \
  "$POD_BEING_COMPACTED" -c mongodb -- \
  bash -c "mongosh --quiet admin \
    -u root -p \$MONGODB_ROOT_PASSWORD --authenticationDatabase admin \
    --eval 'db.adminCommand({ currentOp: 1, op: \"command\", \"command.compact\": { \$exists: true } })'"

The output lists the active compact operation and its progress. An empty inprog array means compact has completed or has not started yet.

Note

The config server replica set (configsvr) holds only chunk metadata and rarely accumulates significant reclaimable space. Compact it using the same procedure, substituting data-db-mongodb-sharded-configsvr-0 for the shard pod names, only if the fragmentation analysis shows a high reclaimable ratio on config server pods.