apiVersion: v1
data:
  config-file: |
    force-enable-fulltext-index = true
    enable-telemetry = false
    mem-quota-query = 7340032000
    new_collations_enabled_on_first_bootstrap = true
    split-table = false
    disaggregated-tiflash = true
    display-tiflash-ru = "all"
    use-autoscaler = true
    use-columnar = true
    use-columnar-fts = true
    is-tiflashcompute-fixed-pool = false
    enable-ru-limit = true
    index-limit = 512
    tikv-api-service-addr = "http://tikv-api-service.tidb-serverless.svc.cluster.local:19500"
    enable-dist-task = true
    enable-set-table-ttl = true
    enable-auto-analyze-sys-table = true
    analyze-table-threshold = 100
    external-storage-path = "s3://prod-ap-southeast-1-stashing-area/"
    enable-zero-backend = true
    enable-unredact-logger = true
    max-index-length = 12288
    enable-manager-notifier = true
    max-import-data-size = 10995116277760 # 10TiB
    analyze-always-skip-wide-columns = true
    auto-id-client-timeout= "3s"

    [instance]
      tidb_stmt_summary_enable_persistent = true
      tidb_stmt_summary_filename = "/var/log/tidb/statementlog"
      tidb_stmt_summary_file_max_size = 25
      tidb_stmt_summary_file_max_backups = 3

    [status]
      record-db-label = true

    [log]
      [log.file]
        max-backups = 3

    [performance]
      tcp-keep-alive = true

    [security]
      enable-sem = true
      sem-level = "config"
      sem-path = "/etc/tidb/sem-config.json"
      auth-token-jwks = "/var/lib/tidb-auth-token/tidb_auth_token_jwks.json"
      session-token-signing-cert = "/var/lib/tidb-session-cert/tls.crt"
      session-token-signing-key = "/var/lib/tidb-session-cert/tls.key"

    [extended-error-msgs]
      user-prefix-error =  "see https://docs.pingcap.com/tidbcloud/select-cluster-tier#user-name-prefix for more details"
      require-secure-transport-error = "see https://docs.pingcap.com/tidbcloud/secure-connections-to-serverless-tier-clusters for more details"
      max-sleep-seconds-error = "see https://docs.pingcap.com/tidbcloud/serverless-tier-limitations#sql for more details"
      invisible-table-error = "see https://docs.pingcap.com/tidbcloud/limited-sql-features#system-tables for more details"
      invisible-sysvar-error = "see https://docs.pingcap.com/tidbcloud/limited-sql-features#system-variables for more details"
      serverless-not-support-error = "see https://docs.pingcap.com/tidbcloud/limited-sql-features#statements for more details"

    [tikv-client]
      remote-coprocessor-addr = "tikv-worker-service.tidb-serverless.svc:9500"
      txn-chunk-writer-addr = "tikv-worker-service.tidb-serverless.svc:19000"

    [cse]
      columnar-store-type = "columnar"

    [tidb-worker]
      enable = true
      role = "master"
      registry-addr = "root:@tcp(serverless-cluster-tidb.tidb-serverless.svc:4000)/serverless"
      tidb-pool = "canary-vip-tidb-pool"
      api-server = "http://scaler-svc.tidb-worker:9080"

    [tiflash-replicas]
      min-count = 2
      constraints = [{"key" = "engine", "op" = "in", "values" = ["tiflash"]}, {"key" = "engine_role", "op" = "in", "values" = ["write"]}]
      group-id = "enable_s3_wn_region"
      extra-s3-rule = false

    [audit-log]
      enable = false
      format = "JSON"
      path = "/var/log/tidb/tidb-audit.log"
      max-filesize = 65
      max-lifetime = 86400
      reserved-backups = 1
      reserved-days = 0
      redacted = false
      encrypt-key = ""

    [ru-config]
      write-base-cost = 0.5
      write-per-batch-base-cost = 0.5
      write-cost-per-byte = 0.00048828  # 1/1024/2

    [pd-client]
      use-resource-manager-proxy = false
      enable-grpc-client-metrics = true

    [hosted-embedding]
      enabled = true
      api-endpoint = "https://inference-api-1e5e8743477d.staging.aws.tidbcloud.com"
      api-key-path = "/var/lib/tidb-inference-service/api_key"

  sem-config: |
    {
    "restricted_databases" : [ "metrics_schema"],

    "restricted_tables" : [
        {
            "schema": "mysql",
            "name": "expr_pushdown_blacklist"
        },
        {
            "schema": "mysql",
            "name": "gc_delete_range"
        },
        {
            "schema": "mysql",
            "name": "gc_delete_range_done"
        },
        {
            "schema": "mysql",
            "name": "opt_rule_blacklist"
        },
        {
            "schema": "mysql",
            "name": "tidb"
        },
        {
            "schema": "mysql",
            "name": "global_variables"
        },
        {
            "schema": "information_schema",
            "name": "attributes"
        },
        {
            "schema": "information_schema",
            "name": "cluster_config"
        },
        {
            "schema": "information_schema",
            "name": "cluster_hardware"
        },
        {
            "schema": "information_schema",
            "name": "cluster_info"
        },
        {
            "schema": "information_schema",
            "name": "cluster_load"
        },
        {
            "schema": "information_schema",
            "name": "cluster_log"
        },
        {
            "schema": "information_schema",
            "name": "cluster_slow_query"
        },
        {
            "schema": "information_schema",
            "name": "cluster_statements_summary"
        },
        {
            "schema": "information_schema",
            "name": "cluster_statements_summary_evicted"
        },
        {
            "schema": "information_schema",
            "name": "cluster_systeminfo"
        },
        {
            "schema": "information_schema",
            "name": "inspection_result"
        },
        {
            "schema": "information_schema",
            "name": "inspection_rules"
        },
        {
            "schema": "information_schema",
            "name": "inspection_summary"
        },
        {
            "schema": "information_schema",
            "name": "metrics_summary"
        },
        {
            "schema": "information_schema",
            "name": "metrics_summary_by_label"
        },
        {
            "schema": "information_schema",
            "name": "metrics_tables"
        },
        {
            "schema": "information_schema",
            "name": "placement_policies"
        },
        {
            "schema": "information_schema",
            "name": "resource_groups"
        },
        {
            "schema": "information_schema",
            "name": "slow_query"
        },
        {
            "schema": "information_schema",
            "name": "statements_summary"
        },
        {
            "schema": "information_schema",
            "name": "statements_summary_evicted"
        },
        {
            "schema": "information_schema",
            "name": "tidb_hot_regions"
        },
        {
            "schema": "information_schema",
            "name": "tidb_hot_regions_history"
        },
        {
            "schema": "information_schema",
            "name": "tidb_servers_info"
        },
        {
            "schema": "information_schema",
            "name": "tiflash_segments"
        },
        {
            "schema": "information_schema",
            "name": "tiflash_tables"
        },
        {
            "schema": "information_schema",
            "name": "tiflash_tables"
        },
        {
            "schema": "information_schema",
            "name": "tikv_region_peers"
        },
        {
            "schema": "information_schema",
            "name": "tikv_region_status"
        },
        {
            "schema": "information_schema",
            "name": "tikv_store_status"
        },
        {
            "schema": "performance_schema",
            "name": "pd_profile_allocs"
        },
        {
            "schema": "performance_schema",
            "name": "pd_profile_block"
        },
        {
            "schema": "performance_schema",
            "name": "pd_profile_cpu"
        },
        {
            "schema": "performance_schema",
            "name": "pd_profile_goroutines"
        },
        {
            "schema": "performance_schema",
            "name": "pd_profile_memory"
        },
        {
            "schema": "performance_schema",
            "name": "pd_profile_mutex"
        },
        {
            "schema": "performance_schema",
            "name": "tidb_profile_allocs"
        },
        {
            "schema": "performance_schema",
            "name": "tidb_profile_block"
        },
        {
            "schema": "performance_schema",
            "name": "tidb_profile_cpu"
        },
        {
            "schema": "performance_schema",
            "name": "tidb_profile_goroutines"
        },
        {
            "schema": "performance_schema",
            "name": "tidb_profile_memory"
        },
        {
            "schema": "performance_schema",
            "name": "tidb_profile_mutex"
        },
        {
            "schema": "performance_schema",
            "name": "tikv_profile_cpu"
        }
    ],

    "restricted_variables" : [
        {
            "name": "hostname",
            "restriction-type": "replace",
            "readonly": true,
            "value": "localhost"
        },
        {
            "name": "datadir",
            "restriction-type": "replace",
            "readonly": true,
            "value": "/usr/local/mysql/data/"
        },
        {
            "name": "plugin_dir",
            "restriction-type": "hidden"
        },
        {
            "name": "plugin_load",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_analyze_skip_column_types",
            "readonly": true
        },
        {
            "name": "tidb_check_mb4_value_in_utf8",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_config",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_enable_collect_execution_info",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_enable_slow_log",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_enable_telemetry",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_expensive_query_time_threshold",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_force_priority",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_general_log",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_memory_usage_alarm_ratio",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_metric_query_range_duration",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_metric_query_step",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_opt_write_row_id",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_pprof_sql_cpu",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_record_plan_in_slow_log",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_redact_log",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_restricted_read_only",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_row_format_version",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_slow_query_file",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_slow_log_threshold",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_slow_txn_log_threshold",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_top_sql_max_meta_count",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_top_sql_max_time_series_count",
            "restriction-type": "hidden"
        },
        {
            "name": "ddl_slow_threshold",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_enable_stmt_summary",
            "restriction-type": "hidden",
            "readonly": true
        },
        {
            "name": "tidb_stmt_summary_internal_query",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_stmt_summary_refresh_interval",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_stmt_summary_history_size",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_stmt_summary_max_stmt_count",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_stmt_summary_max_sql_length",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_stmt_summary_enable_persistent",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_stmt_summary_file_max_size",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_stmt_summary_file_max_days",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_stmt_summary_file_max_backups",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_stmt_summary_filename",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_audit_redact_log",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_enable_resource_control",
            "restriction-type": "hidden"
        },
        {
            "name": "tidb_enable_vector_type",
            "restriction-type": "hidden"
        },
        {
            "name": "interactive_timeout",
            "readonly": true
        },
        {
            "name": "max_allowed_packet",
            "readonly": true
        },
        {
            "name": "skip_name_resolve",
            "readonly": true
        },
        {
            "name": "sql_log_bin",
            "readonly": true
        },
        {
            "name": "tidb_ddl_disk_quota",
            "readonly": true
        },
        {
            "name": "tidb_ddl_enable_fast_reorg",
            "readonly": true
        },
        {
            "name": "tidb_ddl_error_count_limit",
            "readonly": true
        },
        {
            "name": "tidb_ddl_flashback_concurrency",
            "readonly": true
        },
        {
            "name": "tidb_ddl_reorg_batch_size",
            "readonly": true
        },
        {
            "name": "tidb_ddl_reorg_priority",
            "readonly": true
        },
        {
            "name": "tidb_ddl_reorg_worker_cnt",
            "readonly": true
        },
        {
            "name": "tidb_enable_1pc",
            "readonly": true
        },
        {
            "name": "tidb_enable_async_commit",
            "readonly": true
        },
        {
            "name": "tidb_enable_auto_analyze",
            "readonly": true
        },
        {
            "name": "tidb_enable_ddl",
            "readonly": true
        },
        {
            "name": "tidb_enable_gc_aware_memory_track",
            "readonly": true
        },
        {
            "name": "tidb_enable_gogc_tuner",
            "readonly": true
        },
        {
            "name": "tidb_enable_local_txn",
            "readonly": true
        },
        {
            "name": "tidb_enable_resource_control",
            "readonly": true
        },
        {
            "name": "tidb_enable_stmt_summary",
            "readonly": true
        },
        {
            "name": "tidb_enable_top_sql",
            "readonly": true
        },
        {
            "name": "tidb_enable_tso_follower_proxy",
            "readonly": true
        },
        {
            "name": "tidb_gc_concurrency",
            "readonly": true
        },
        {
            "name": "tidb_gc_enable",
            "readonly": true
        },
        {
            "name": "tidb_gc_max_wait_time",
            "readonly": true
        },
        {
            "name": "tidb_gc_run_interval",
            "readonly": true
        },
        {
            "name": "tidb_gc_scan_lock_mode",
            "readonly": true
        },
        {
            "name": "tidb_generate_binary_plan",
            "readonly": true
        },
        {
            "name": "tidb_gogc_tuner_threshold",
            "readonly": true
        },
        {
            "name": "tidb_guarantee_linearizability",
            "readonly": true
        },
        {
            "name": "tidb_log_file_max_days",
            "readonly": true
        },
        {
            "name": "tidb_scatter_region",
            "readonly": true
        },
        {
            "name": "tidb_server_memory_limit",
            "readonly": true
        },
        {
            "name": "tidb_server_memory_limit_gc_trigger",
            "readonly": true
        },
        {
            "name": "tidb_server_memory_limit_sess_min_size",
            "readonly": true
        },
        {
            "name": "tidb_simplified_metrics",
            "readonly": true
        },
        {
            "name": "tidb_stats_load_sync_wait",
            "readonly": true
        },
        {
            "name": "tidb_sysproc_scan_concurrency",
            "readonly": true
        },
        {
            "name": "tidb_tso_client_batch_max_wait_time",
            "readonly": true
        },
        {
            "name": "tidb_ttl_delete_batch_size",
            "readonly": true
        },
        {
            "name": "tidb_ttl_delete_rate_limit",
            "readonly": true
        },
        {
            "name": "tidb_ttl_delete_worker_count",
            "readonly": true
        },
        {
            "name": "tidb_ttl_job_enable",
            "readonly": true
        },
        {
            "name": "tidb_ttl_job_schedule_window_end_time",
            "readonly": true
        },
        {
            "name": "tidb_ttl_job_schedule_window_start_time",
            "readonly": true
        },
        {
            "name": "tidb_ttl_running_tasks",
            "readonly": true
        },
        {
            "name": "tidb_ttl_scan_batch_size",
            "readonly": true
        },
        {
            "name": "tidb_ttl_scan_worker_count",
            "readonly": true
        },
        {
            "name": "tidb_wait_split_region_finish",
            "readonly": true
        },
        {
            "name": "tidb_wait_split_region_timeout",
            "readonly": true
        },
        {
            "name": "txn_scope",
            "readonly": true
        },
        {
            "name": "validate_password.enable",
            "readonly": true
        },
        {
            "name": "tidb_pessimistic_txn_fair_locking",
            "readonly": true
        },
        {
            "name": "tidb_replica_read",
            "readonly": true
        },
        {
            "name": "tidb_enable_dist_task",
            "readonly": true
        },
        {
            "name": "tidb_cloud_storage_uri",
            "restriction-type": "hidden"
        }
    ],

     "restricted_status" : [
        {
            "name": "tidb_gc_leader_desc",
            "restriction-type": "hidden"
        }
    ],

    "restricted_static_privileges_col" : ["File_priv","Shutdown_priv"]
    }

  startup-script: |
    #!/bin/sh

    # This script is used to start tidb containers in kubernetes cluster

    # Use DownwardAPIVolumeFiles to store informations of the cluster:
    # https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/#the-downward-api
    #
    #   runmode="normal/debug"
    #
    set -uo pipefail

    urlencode() {
      local length="${#1}"
      for (( i = 0; i < length; i++ )); do
        local c="${1:i:1}"
        case $c in
          [a-zA-Z0-9.~_-]) printf "$c" ;;
          *) printf '%%%02X' "'$c" ;;
        esac
      done
    }

    ANNOTATIONS="/etc/podinfo/annotations"

    if [[ ! -f "${ANNOTATIONS}" ]]
    then
        echo "${ANNOTATIONS} does't exist, exiting."
        exit 1
    fi
    source ${ANNOTATIONS} 2>/dev/null
    runmode=${runmode:-normal}
    if [[ X${runmode} == Xdebug ]]
    then
        echo "entering debug mode."
        tail -f /dev/null
    fi

    # Use HOSTNAME if POD_NAME is unset for backward compatibility.
    POD_NAME=${POD_NAME:-$HOSTNAME}
    ARGS="--store=tikv \
    --host=0.0.0.0 \
    --path=serverless-cluster-pd.tidb-serverless.svc:2379 \
    --config=/etc/tidb/tidb.toml \
    --proxy-protocol-networks=* \
    --proxy-protocol-fallbackable \
    --max-idle-seconds=600 \
    --standby
    "

    if [[ X${BINLOG_ENABLED:-} == Xtrue ]]
    then
        ARGS="${ARGS} --enable-binlog=true"
    fi

    SLOW_LOG_FILE=${SLOW_LOG_FILE:-""}
    if [[ ! -z "${SLOW_LOG_FILE}" ]]
    then
        ARGS="${ARGS} --log-slow-query=${SLOW_LOG_FILE:-}"
    fi

    term_handler() {
      echo "start to graceful shutdown[${POD_IP}]"
      kill -TERM $tidb_server_pid
      wait $tidb_server_pid
      exit
    }
    trap term_handler TERM
    # Set in pod env when TiDB 7.5 is ready.
    export GracefulCloseConnectionsTimeout=180s

    while true; do
      echo "start tidb-server[${POD_IP}] ..."
      echo "/tidb-server ${ARGS}"
      /tidb-server ${ARGS} & tidb_server_pid=$!
      wait $tidb_server_pid
      ret=$?
      echo "tidb-server exited with code $ret, cleaning up temp data at /tmp/_tidb..."
      rm -rf /tmp/_tidb || true
      echo "report free ..."
      set +o pipefail
      normalRestartLogPath="/tmp/tidb-normal-restart.log"
      normalRestartLog=""
      if [[ -f "${normalRestartLogPath}" ]]
      then
        normalRestartLog=$(cat ${normalRestartLogPath})
        echo "found normal restart log: ${normalRestartLog}"
      fi
      if [ $ret -ne 0 ]; then
        echo "tidb-server[${POD_IP}] exited with error code $ret"
        curl --connect-timeout 5 -s -k "https://manager-server.${MANAGER_NS}.svc:8000/api/tidb/free?pod_name=${POD_NAME}&pod_ip=${POD_IP}&ns=${NAMESPACE}&normal_restart_log=$(urlencode "$normalRestartLog")"
      fi
      set -o pipefail
    done
kind: ConfigMap
metadata:
  labels:
    app.kubernetes.io/component: serverless-tidb
    app.kubernetes.io/name: tidb-cluster
  name: serverless-tidb-config
