# Copyright (C) 2020-present MongoDB, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the Server Side Public License, version 1,
# as published by MongoDB, Inc.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# Server Side Public License for more details.
#
# You should have received a copy of the Server Side Public License
# along with this program. If not, see
# <http://www.mongodb.com/licensing/server-side-public-license>.
#
# As a special exception, the copyright holders give permission to link the
# code of portions of this program with the OpenSSL library under certain
# conditions as described in each individual source file and distribute
# linked combinations including the program with the OpenSSL library. You
# must comply with the Server Side Public License in all respects for
# all of the code used other than as permitted herein. If you modify file(s)
# with this exception, you may extend this exception to your version of the
# file(s), but you are not obligated to do so. If you do not wish to do so,
# delete this exception statement from your version. If you delete this
# exception statement from all source files in the program, then also delete
# it in the license file.
#

# Server parameters for resharding.

global:
    cpp_namespace: "mongo::resharding"

imports:
    - "mongo/db/basic_types.idl"

server_parameters:
    reshardingCoordinatorServiceMaxThreadCount:
        description: The max number of threads in the resharding coordinator's thread pool.
        set_at: startup
        cpp_vartype: int
        cpp_varname: gReshardingCoordinatorServiceMaxThreadCount
        default: 3
        validator:
            gte: 1
            lte: 256
        redact: false

    reshardingDonorServiceMaxThreadCount:
        description: The max number of threads in the resharding donor's thread pool.
        set_at: startup
        cpp_vartype: int
        cpp_varname: gReshardingDonorServiceMaxThreadCount
        default: 2
        validator:
            gte: 1
            lte: 256
        redact: false

    reshardingRecipientServiceMaxThreadCount:
        description: The max number of threads in the resharding recipient's thread pool.
        set_at: startup
        cpp_vartype: int
        cpp_varname: gReshardingRecipientServiceMaxThreadCount
        default: 8
        validator:
            gte: 1
            lte: 256
        redact: false

    reshardingCollectionClonerBatchSizeInBytes:
        description: >-
            Limit for the number of bytes of data inserted per storage transaction (WriteUnitOfWork)
            by ReshardingCollectionCloner.
        set_at: [startup, runtime]
        cpp_vartype: AtomicWord<int>
        cpp_varname: gReshardingCollectionClonerBatchSizeInBytes
        default:
            expr: 100 * 1024
        validator:
            gte: 1
        redact: false

    reshardingCollectionClonerWriteThreadCount:
        description: >-
            Number of writer threads used by ReshardingCollectionCloner.
        set_at: runtime
        cpp_vartype: int
        cpp_varname: gReshardingCollectionClonerWriteThreadCount
        default:
            expr: 2
        validator:
            gte: 1
        redact: false

    reshardingTxnClonerProgressBatchSize:
        description: >-
            Number of config.transactions records from a donor shard to process before recording the
            lsid locally to resume from on primary failover or server restart.
        set_at: [startup, runtime]
        cpp_vartype: AtomicWord<int>
        cpp_varname: gReshardingTxnClonerProgressBatchSize
        default: 1000
        validator:
            gte: 1
        redact: false

    reshardingOplogBatchTaskCount:
        description: >-
            The number of subtasks to divide a single oplog batch into so that it may be applied
            concurrently by different threads.
        set_at: [startup, runtime]
        cpp_vartype: AtomicWord<int>
        cpp_varname: gReshardingOplogBatchTaskCount
        default:
            expr: (gReshardingRecipientServiceMaxThreadCount + 1) / 2
            is_constexpr: false
        validator:
            gte: 1
            lte: 256
        redact: false

    reshardingOplogBatchLimitOperations:
        description: >-
            The maximum number of operations for ReshardingOplogApplier to apply in a single batch.
        set_at: [startup, runtime]
        cpp_vartype: AtomicWord<int>
        cpp_varname: gReshardingOplogBatchLimitOperations
        default:
            expr: 5 * 1000
        validator:
            gte: 1
            lte:
                expr: 1000 * 1000
        redact: false

    reshardingOplogBatchLimitBytes:
        description: >-
            The maximum size in bytes for ReshardingOplogApplier to apply in a single batch.
        set_at: [startup, runtime]
        cpp_vartype: AtomicWord<int>
        cpp_varname: gReshardingOplogBatchLimitBytes
        default:
            expr: 100 * 1024 * 1024
        validator:
            gte:
                expr: 16 * 1024 * 1024
            lte:
                expr: 100 * 1024 * 1024
        redact: false

    reshardingOplogApplierMaxLockRequestTimeoutMillis:
        description: >-
            The max number of milliseconds that the resharding oplog applier will wait for lock
            acquisition.
        set_at: [startup, runtime]
        cpp_vartype: AtomicWord<int>
        cpp_varname: gReshardingOplogApplierMaxLockRequestTimeoutMillis
        default: 5
        redact: false

    reshardingMinimumOperationDurationMillis:
        description: >-
            Controls the minimum duration of resharding operations, and allows transactions and 
            retryable writes started after the current resharding operation to complete within the 
            period defined by the parameter. If this value is lowered then you will likely want to lower 
            reshardingDelayBeforeRemainingOperationTimeQueryMillis as well.
        set_at: [startup, runtime]
        cpp_vartype: AtomicWord<int>
        cpp_varname: gReshardingMinimumOperationDurationMillis
        default:
            expr: 5 * 60 * 1000
        validator:
            gte: 0
        redact: false
    
    reshardingDelayBeforeRemainingOperationTimeQueryMillis:
        description: >-
            Initial delay before querying for remaining operation time from recipient shards.
            The delay allows for applying more oplog entries before calculating time remaining, giving
            a more accurate value.
            Note we will have this delay every time we happen to have a failover occur.
        set_at: [startup, runtime]
        cpp_vartype: AtomicWord<int>
        cpp_varname: gReshardingDelayBeforeRemainingOperationTimeQueryMillis
        default:
            expr: 30 * 1000
        validator:
            gte: 0
        redact: false
    
    reshardingCriticalSectionTimeoutMillis:
        description: >-
            The upper limit on how long to wait to hear back from recipient shards reaching strict
            consistency after engaging the critical section.
        set_at: [startup, runtime]
        cpp_vartype: AtomicWord<int>
        cpp_varname: gReshardingCriticalSectionTimeoutMillis
        default:
            expr: 5 * 1000
        validator:
            gte: 0
        redact: false

    remainingReshardingOperationTimeThresholdMillis:
        description: >-
            Controls the threshold for engaging the critical section of resharding operations. 
            Once the remaining operation time on all recipients falls below this threshold, the 
            resharding coordinator will engage the critical section and block incoming writes.
        set_at: [startup, runtime]
        cpp_vartype: AtomicWord<int>
        cpp_varname: gRemainingReshardingOperationTimeThresholdMillis
        default:
            expr: 2 * 1000
        validator:
            gte: 0
        redact: false

    reshardingCoordinatorQuiescePeriodMillis:
        description: >-
            Controls the amount of time a resharding operation will remain in a quiesced state on
            the coordinator, after it completes or aborts, to allow a user to reconnect to it and
            retrieve the result. This applies only to resharding operations where the user
            provided a reshardingUUID; if no UUID is provided there is no quiesce period.
        set_at: startup
        cpp_vartype: int
        cpp_varname: gReshardingCoordinatorQuiescePeriodMillis
        default:
            expr: 15 * 60 * 1000
        validator:
            gte: 0
        redact: false
