apiVersion: v1
kind: ConfigMap
metadata:
  name: cassandra-proxy-config
data:
  config.yaml: |
    mysql:
      # MySQL address in host:port form
      host: "{{MYSQL_HOST}}"

      # Per-tenant/user pool sizing limits (see tenancy.limits too)
      max_open_conns: 2800
      max_idle_conns: 500

      # Timeouts
      connect_timeout: 30s
      read_timeout: 60s
      write_timeout: 60s

      # Connection lifetime controls
      conn_max_lifetime: 30m
      conn_max_idle_time: 5m

      prepared:
        enabled: true
        max_statements: 1000
        idle_ttl: 30s
        cleanup_interval: 1m
        prewarm_on_prepare: false

      # MySQL driver TLS settings (go-sql-driver/mysql `tls` parameter)
      # mode: "true" | "skip-verify" | "preferred" | "<custom-name>"
      tls:
        mode: "preferred"
        # ca_file: "/path/to/ca.pem"
        # cert_file: "/path/to/client-cert.pem"
        # key_file: "/path/to/client-key.pem"
        # server_name: "mysql.example.com"
        insecure_skip_verify: false

      # Limits used when mapping secondary indexes to MySQL (safe defaults for InnoDB)
      max_index_bytes: 3072
      min_index_prefix_chars: 8
      min_index_prefix_bytes: 8

      partition_default_size: 40

    server:
      # Max concurrent CQL client connections allowed
      max_connections: 30000
      max_in_flight_frames: 8
      drain:
        # Flip readiness first so kube-proxy/NLB can stop routing new traffic before listeners close.
        readiness_delay: 40s
        # After listeners close, wait this long for in-flight connections to drain before force close.
        wait_timeout: 25s

      # Optional pprof/debug HTTP server address
      # debug_addr: "127.0.0.1:6060"
      debug_addr: "127.0.0.1:6060"

      # One or more listeners for Cassandra native protocol
      listeners:
        - name: "plain"
          address: "0.0.0.0:9042"
          tls:
            # disabled | manual | auto
            mode: "disabled"
            # cert_file: "/path/to/server-cert.pem"   # for manual
            # key_file: "/path/to/server-key.pem"     # for manual
            # client_ca_file: "/path/to/client-ca.pem" # for mTLS
            # client_auth: "require_and_verify"        # none|request|require|verify|require_and_verify
            # min_version: "1.2"
            # max_version: "1.3"

        - name: "tls"
          address: "0.0.0.0:9142"
          tls:
            mode: "manual"
            cert_file: "/server-cert/tls.crt"
            key_file: "/server-cert/tls.key"
      admin:
        address: "0.0.0.0:6060"
        pprof:
          enabled: true
          path: "/debug/pprof"
        metrics:
          enabled: true
          path: "/metrics"               # default
          slow_request_threshold: 250ms  # optional: logs slow requests with stage timings

    # Tenant routing rules:
    # - Default: treat "tenant:" prefix in username as tenant name.
    # - Fallback: when no regex match, use the whole username as tenant ("username").
    tenancy:
      tenant_regex: "^(?P<tenant>[^:]+):"
      tenant_regex_fallback: "username"

      # Interval to prune idle tenant pools
      prune_interval: 10m

      # Hard limits per tenant/user
      limits:
        max_proxy_connections: 30000
        max_mysql_connections: 2200

    executor:
      execute_pin_mysql_conn: false
      schema_sync_background: true
      unlogged_batch_parallelism: 16
      unlogged_batch_parallel: true
      unlogged_merge_before_parallel: true
      batch_insert_merge_max_rows: 256
      batch_insert_merge_max_bytes: 1024000
      protocol_mode: "extended_sql"

    schema_cache_ttl: 2m
    cql_meta_cache_ttl: 2m

    # Values exposed via system tables for Cassandra client compatibility.
    # Most applications can keep these defaults.
    system_tables:
      cluster_name: "Cassandra"
      datacenter: "datacenter1"
      rack: "rack1"
      # Keep host_id stable across Pod replacements so drivers continue to treat the service as one logical node.
      host_id: "{{HOST_ID}}"

      # If empty, the proxy tries to infer from the first listener or uses 127.0.0.1.
      advertised_address: ""

      # If 0, the native port is inferred from the first listener, or defaults to 9042.
      native_port: 9042

      # Defaults to 7000 when 0.
      storage_port: 7000

      release_version: "4.0"
      cql_version: "3.4.7"

      # Optional; if empty, the implementation defaults are used.
      native_protocol_version: ""

      partitioner: "org.apache.cassandra.dht.Murmur3Partitioner"

      # Optional token list used for system tables; leave empty unless you need stable tokens.
      tokens: []

      # Schema version strategy for system tables.
      # strategy: "cql_meta" (derive from MySQL schema metadata) or "fixed".
      schema_version:
        strategy: "cql_meta"
        fixed: ""

      # Optional peers shown in system.peers* for multi-node illusions.
      peers: []

      # Capability profile toggles what clients see as supported.
      capability_profile: "compat_v4"

    # Multi-instance schema metadata sync.
    # Enabled by default; set enabled: false to disable.
    schema_sync:
      enabled: true
      check_interval: 60s

    features:
      # If true, views/triggers are exposed only as metadata (not fully implemented behavior).
      metadata_only_views: false
      metadata_only_triggers: false

    logging:
      # debug | info | warn | error
      level: "info"

      # stdout | file
      output: "stdout"

      # Required when output: file
      file_path: ""

      # Pretty console logging when true; JSON when false
      development: true

      # Query redaction is enabled by default when unset.
      redact_queries: true

      # Optional additional redaction patterns
      redact_patterns: []
