services:
  db00:
    image: mariadb:11.4
    container_name: db00
    environment:
      - MYSQL_ROOT_PASSWORD=secret
      - MYSQL_DATABASE=mydb
      - MYSQL_USER=myuser
      - MYSQL_PASSWORD=mypassword
      - EXPECTED_CLUSTER_SIZE=1
    command:
      - --wsrep-on=ON
      - --wsrep-provider=/usr/lib/galera/libgalera_smm.so
      - --wsrep-cluster-name=galera_cluster
      - --wsrep-cluster-address=gcomm://
      - --wsrep-node-name=db00
      - --wsrep-node-address=db00
      - --wsrep-sst-method=mariabackup
      - --wsrep-sst-auth=root:secret
      # Explicit receive address prevents IST host resolution failures
      - --wsrep-sst-receive-address=db00
      - --binlog-format=ROW
      - --default-storage-engine=InnoDB
      - --innodb-autoinc-lock-mode=2
      - --local-infile=1
    healthcheck:
      test: ["CMD", "/healthcheck.sh"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 120s
    volumes:
      - db00_data:/var/lib/mysql
      - ./scripts/healthcheck.sh:/healthcheck.sh
    networks:
      galera_net:
        aliases:
          - db00
    ports:
      - "3306:3306"
    restart: "on-failure"

  db01:
    image: mariadb:11.4
    container_name: db01
    environment:
      - MYSQL_ROOT_PASSWORD=secret
      - EXPECTED_CLUSTER_SIZE=2
    command:
      - --wsrep-on=ON
      - --wsrep-provider=/usr/lib/galera/libgalera_smm.so
      - --wsrep-cluster-name=galera_cluster
      - --wsrep-cluster-address=gcomm://db00,db01,db02
      - --wsrep-node-name=db01
      - --wsrep-node-address=db01
      - --wsrep-sst-method=mariabackup
      - --wsrep-sst-auth=root:secret
      - --wsrep-sst-receive-address=db01
      # db01 uses db00 as donor on first join; after sync it becomes donor for db02
      - --wsrep-sst-donor=db00
      - --binlog-format=ROW
      - --default-storage-engine=InnoDB
      - --innodb-autoinc-lock-mode=2
      - --local-infile=1
    depends_on:
      db00:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "/healthcheck.sh"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 180s
    volumes:
      - db01_data:/var/lib/mysql
      - ./scripts/healthcheck.sh:/healthcheck.sh
    networks:
      galera_net:
        aliases:
          - db01
    ports:
      - "3307:3306"
    restart: "on-failure"

  db02:
    image: mariadb:11.4
    container_name: db02
    environment:
      - MYSQL_ROOT_PASSWORD=secret
      - EXPECTED_CLUSTER_SIZE=3
    command:
      - --wsrep-on=ON
      - --wsrep-provider=/usr/lib/galera/libgalera_smm.so
      - --wsrep-cluster-name=galera_cluster
      - --wsrep-cluster-address=gcomm://db00,db01,db02
      - --wsrep-node-name=db02
      - --wsrep-node-address=db02
      - --wsrep-sst-method=mariabackup
      - --wsrep-sst-auth=root:secret
      - --wsrep-sst-receive-address=db02
      # Prefer db01 as donor to keep db00 free for writes during SST
      - --wsrep-sst-donor=db01
      # Force full SST instead of IST to avoid segfault in libgalera_smm.so
      # triggered during IST handshake (exit code 139 / SIGSEGV) seen in 11.4.10
      - --wsrep-provider-options=gcache.size=0
      - --binlog-format=ROW
      - --default-storage-engine=InnoDB
      - --innodb-autoinc-lock-mode=2
      - --local-infile=1
    depends_on:
      # Chain dependency: db02 waits for db01 to be fully healthy before joining.
      # This ensures db00's GCache has settled after the db01 SST completes,
      # preventing a race condition that triggers a SIGSEGV in libgalera_smm.so
      # during IST handshake. Do NOT flatten this to depend only on db00.
      db01:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "/healthcheck.sh"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 180s
    volumes:
      - db02_data:/var/lib/mysql
      - ./scripts/healthcheck.sh:/healthcheck.sh
    networks:
      galera_net:
        aliases:
          - db02
    ports:
      - "3308:3306"
    restart: "on-failure"

  cluster_monitor:
    image: mariadb:11.4
    container_name: cluster_monitor
    # Require only a quorum (2 nodes) rather than all 3,
    # so a single node failure doesn't prevent monitoring from starting
    depends_on:
      db00:
        condition: service_healthy
      db01:
        condition: service_healthy
    environment:
      - MYSQL_ROOT_PASSWORD=secret
    volumes:
      - ./scripts/cluster_health_check.sh:/cluster_health_check.sh
    networks:
      - galera_net
    entrypoint: ["/bin/bash", "/cluster_health_check.sh"]
    # Changed from "no" to "on-failure" so it retries if cluster isn't ready
    restart: "on-failure"

  mdb5_workload:
    image: us-central1-docker.pkg.dev/molten-verve-216720/antithesis-pdev-repository/mdb3-workload
    container_name: mdb5_workload
    environment:
      - MYSQL_ROOT_PASSWORD=secret
    # Same quorum-based dependency as cluster_monitor
    depends_on:
      db00:
        condition: service_healthy
      db01:
        condition: service_healthy
    networks:
      - galera_net
    entrypoint: ["sleep", "infinity"]
    # Changed from "no" to "on-failure" so it retries if cluster isn't ready
    restart: "on-failure"

volumes:
  db00_data:
  db01_data:
  db02_data:

networks:
  galera_net:
    driver: bridge