# Citus chaos test cluster for pg_trickle v0.51.0 FEAT-10-01.
#
# Topology: 1 coordinator + 3 workers.
#
# Usage:
#   docker compose -f docker/docker-compose.citus.yml up -d
#   # run chaos tests
#   docker compose -f docker/docker-compose.citus.yml down -v
#
# Environment variables read by e2e_citus_chaos_tests.rs:
#   CITUS_COORDINATOR_URL       postgresql://postgres:postgres@localhost:15432/postgres
#   CITUS_COORDINATOR_CONTAINER citus-coordinator
#   CITUS_WORKER_0_CONTAINER    citus-worker-0
#   CITUS_WORKER_1_CONTAINER    citus-worker-1
#   CITUS_WORKER_2_CONTAINER    citus-worker-2
#   CITUS_NETWORK               citus_default

version: "3.8"

networks:
  citus_default:
    driver: bridge

services:
  coordinator:
    container_name: citus-coordinator
    image: citusdata/citus:12.1
    ports:
      - "15432:5432"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres
    networks:
      - citus_default
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 10

  worker-0:
    container_name: citus-worker-0
    image: citusdata/citus:12.1
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres
    networks:
      - citus_default
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 10

  worker-1:
    container_name: citus-worker-1
    image: citusdata/citus:12.1
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres
    networks:
      - citus_default
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 10

  worker-2:
    container_name: citus-worker-2
    image: citusdata/citus:12.1
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres
    networks:
      - citus_default
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 10

  # Initializer: registers workers with coordinator after all nodes are healthy.
  citus-init:
    container_name: citus-init
    image: citusdata/citus:12.1
    depends_on:
      coordinator:
        condition: service_healthy
      worker-0:
        condition: service_healthy
      worker-1:
        condition: service_healthy
      worker-2:
        condition: service_healthy
    networks:
      - citus_default
    restart: "no"
    command: >
      bash -c "
        psql postgresql://postgres:postgres@coordinator/postgres -c \"
          SELECT citus_add_node('citus-worker-0', 5432);
          SELECT citus_add_node('citus-worker-1', 5432);
          SELECT citus_add_node('citus-worker-2', 5432);
        \"
        echo 'Citus cluster initialised with 3 workers.'
      "
