x-defaults: &defaults
  restart: unless-stopped
  logging:
    driver: json-file
    options:
      max-size: 100m
      max-file: '3'

x-mariadb-galera: &mariadb-galera
  <<: *defaults
  image: ${GLOBAL_REGISTRY:-}mariadb:${MARIADB_VERSION:-11.7.2}
  environment: &galera-env
    TZ: ${TZ:-UTC}
    MARIADB_ROOT_PASSWORD: ${MARIADB_ROOT_PASSWORD:-galera}
    MARIADB_GALERA_CLUSTER_NAME: ${MARIADB_GALERA_CLUSTER_NAME:-galera_cluster}
    MARIADB_GALERA_CLUSTER_ADDRESS: gcomm://mariadb-galera-1,mariadb-galera-2,mariadb-galera-3
  command:
    - --wsrep-new-cluster
    - --wsrep_node_address=${WSREP_NODE_ADDRESS}
    - --wsrep_cluster_name=${MARIADB_GALERA_CLUSTER_NAME:-galera_cluster}
    - --wsrep_cluster_address=gcomm://mariadb-galera-1,mariadb-galera-2,mariadb-galera-3
    - --wsrep_sst_method=rsync
    - --wsrep_on=ON
    - --wsrep_provider=/usr/lib/galera/libgalera_smm.so
    - --binlog_format=row
    - --default_storage_engine=InnoDB
    - --innodb_autoinc_lock_mode=2
  deploy:
    resources:
      limits:
        cpus: ${MARIADB_CPU_LIMIT:-2.0}
        memory: ${MARIADB_MEMORY_LIMIT:-2G}
      reservations:
        cpus: ${MARIADB_CPU_RESERVATION:-0.1}
        memory: ${MARIADB_MEMORY_RESERVATION:-1G}
  healthcheck:
    test: [CMD, healthcheck.sh, --connect, --innodb_initialized]
    interval: 30s
    timeout: 10s
    retries: 3
    start_period: 60s

services:
  mariadb-galera-1:
    <<: *mariadb-galera
    hostname: mariadb-galera-1
    ports:
      - '${MARIADB_PORT_1_OVERRIDE:-3306}:3306'
    environment:
      <<: *galera-env
      WSREP_NODE_ADDRESS: mariadb-galera-1
    command:
      - --wsrep-new-cluster
      - --wsrep_node_address=mariadb-galera-1
      - --wsrep_cluster_name=${MARIADB_GALERA_CLUSTER_NAME:-galera_cluster}
      - --wsrep_cluster_address=gcomm://mariadb-galera-1,mariadb-galera-2,mariadb-galera-3
      - --wsrep_sst_method=rsync
      - --wsrep_on=ON
      - --wsrep_provider=/usr/lib/galera/libgalera_smm.so
      - --binlog_format=row
      - --default_storage_engine=InnoDB
      - --innodb_autoinc_lock_mode=2
    volumes:
      - mariadb_galera_1_data:/var/lib/mysql

  mariadb-galera-2:
    <<: *mariadb-galera
    hostname: mariadb-galera-2
    ports:
      - '${MARIADB_PORT_2_OVERRIDE:-3307}:3306'
    environment:
      <<: *galera-env
      WSREP_NODE_ADDRESS: mariadb-galera-2
    command:
      - --wsrep_node_address=mariadb-galera-2
      - --wsrep_cluster_name=${MARIADB_GALERA_CLUSTER_NAME:-galera_cluster}
      - --wsrep_cluster_address=gcomm://mariadb-galera-1,mariadb-galera-2,mariadb-galera-3
      - --wsrep_sst_method=rsync
      - --wsrep_on=ON
      - --wsrep_provider=/usr/lib/galera/libgalera_smm.so
      - --binlog_format=row
      - --default_storage_engine=InnoDB
      - --innodb_autoinc_lock_mode=2
    volumes:
      - mariadb_galera_2_data:/var/lib/mysql
    depends_on:
      mariadb-galera-1:
        condition: service_healthy

  mariadb-galera-3:
    <<: *mariadb-galera
    hostname: mariadb-galera-3
    ports:
      - '${MARIADB_PORT_3_OVERRIDE:-3308}:3306'
    environment:
      <<: *galera-env
      WSREP_NODE_ADDRESS: mariadb-galera-3
    command:
      - --wsrep_node_address=mariadb-galera-3
      - --wsrep_cluster_name=${MARIADB_GALERA_CLUSTER_NAME:-galera_cluster}
      - --wsrep_cluster_address=gcomm://mariadb-galera-1,mariadb-galera-2,mariadb-galera-3
      - --wsrep_sst_method=rsync
      - --wsrep_on=ON
      - --wsrep_provider=/usr/lib/galera/libgalera_smm.so
      - --binlog_format=row
      - --default_storage_engine=InnoDB
      - --innodb_autoinc_lock_mode=2
    volumes:
      - mariadb_galera_3_data:/var/lib/mysql
    depends_on:
      mariadb-galera-1:
        condition: service_healthy

volumes:
  mariadb_galera_1_data:
  mariadb_galera_2_data:
  mariadb_galera_3_data:
