---
# 3 shards plus with each 2 replicas simutlated network delay
services:
  node1: &node
    image: "redis:${REDIS_VERSION:-8}"
    command: >
      redis-server
      --maxmemory            64mb
      --maxmemory-policy     allkeys-lru
      --appendonly           yes
      --cluster-enabled      yes
      --cluster-config-file  nodes.conf
      --cluster-node-timeout 5000
    restart: "${RESTART_POLICY:-always}"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: "7s"
      timeout: "5s"
      retries: 10
    ports:
      - "6379:6379"
  node2:
    <<: *node
    ports:
      - "6380:6379"
  node3:
    <<: *node
    ports:
      - "6381:6379"
  node4:
    <<: *node
    ports:
      - "6382:6379"
  node5: &far_node
    <<: *node
    command: >
      bash -c "apt-get update > /dev/null
      && apt-get install --no-install-recommends --no-install-suggests -y iproute2 iputils-ping > /dev/null
      && rm -rf /var/lib/apt/lists/*
      && (tc qdisc add dev eth0 root netem delay ${DELAY_TIME:-0ms} || echo skipped)
      && redis-server
      --maxmemory            64mb
      --maxmemory-policy     allkeys-lru
      --appendonly           yes
      --cluster-enabled      yes
      --cluster-config-file  nodes.conf
      --cluster-node-timeout 5000"
    cap_add:
      - NET_ADMIN
    ports:
      - "6383:6379"
  node6:
    <<: *node
    ports:
      - "6384:6379"
  node7:
    <<: *far_node
    ports:
      - "6385:6379"
  node8:
    <<: *node
    ports:
      - "6386:6379"
  node9:
    <<: *far_node
    ports:
      - "6387:6379"
  clustering:
    image: "redis:${REDIS_VERSION:-8}"
    command: >
      bash -c "apt-get update > /dev/null
      && apt-get install --no-install-recommends --no-install-suggests -y dnsutils > /dev/null
      && rm -rf /var/lib/apt/lists/*
      && yes yes | redis-cli --cluster create
      $$(dig node1 +short):6379
      $$(dig node2 +short):6379
      $$(dig node3 +short):6379
      $$(dig node4 +short):6379
      $$(dig node5 +short):6379
      $$(dig node6 +short):6379
      $$(dig node7 +short):6379
      $$(dig node8 +short):6379
      $$(dig node9 +short):6379
      --cluster-replicas 2"
    depends_on:
      node1:
        condition: service_healthy
      node2:
        condition: service_healthy
      node3:
        condition: service_healthy
      node4:
        condition: service_healthy
      node5:
        condition: service_healthy
      node6:
        condition: service_healthy
      node7:
        condition: service_healthy
      node8:
        condition: service_healthy
      node9:
        condition: service_healthy
  envoy:
    image: envoyproxy/envoy:v1.35.3
    restart: "${RESTART_POLICY:-always}"
    command:
      - envoy
      - -c
      - /etc/envoy/envoy.yaml
      - -l
      - warn
    ports:
      - "8080:10000"
      - "7000:10001"
    volumes:
      - ./test/proxy/envoy.yaml:/etc/envoy/envoy.yaml
    depends_on:
      clustering:
        condition: service_completed_successfully
  redis-cluster-proxy:
    image: ghcr.io/redis-rb/redis-cluster-proxy:latest
    restart: "${RESTART_POLICY:-always}"
    command:
      - "--bind"
      - "0.0.0.0"
      - "node1:6379"
    ports:
      - "7001:7777"
    depends_on:
      clustering:
        condition: service_completed_successfully
