# Volumes required by the rt nodes, publisher and susbcriber
volumes:
  rt-data-0:        # persistent volume for rt-data-0
  rt-data-1:        # persistent volume for rt-data-1
  rt-data-2:        # persistent volume for rt-data-2
  rt-data:          # persistent volume for rt-data 
  rt-data-session:  # shared volume across all rt containers, used to orchestrate hard/soft reset
  subscriber-0:     # persistent volume for simple subscriber
  publisher-0:      # persistent volume for simple publisher

# Bridge network across all containers
x-kxnet:
  &kxnet
  networks:
    - kx
# Intra-RT ports, used for comms within the rt cluster:
#      - 127.0.0.1:4000:4000/tcp
#      - 127.0.0.1:5009:5009/tcp
#      - 127.0.0.1:7000:7000/udp
#      - 127.0.0.1:7100:7100/tcp
#      - 127.0.0.1:8000:8000/udp
#      - 127.0.0.1:9000:9000/udp
# Internal ports, used for comms between the internal publishers/subscribers and the rt cluster:
#      - 127.0.0.1:5001:5001/tcp
#      - 127.0.0.1:5002:5002/tcp
# External ports, used for comms between the external publishers and the rt cluster:
#      - 127.0.0.1:5000:5000/tcp
# External ports, used for comms between the external subscribers and the rt cluster:
#      - 127.0.0.1:5006:5006/tcp

# Environment variables common to all RT sequencers
x-rt-env:
  &rt-env
    environment:
      - KDB_LICENSE_B64
      - RT_TOPIC_PREFIX
      - RT_SINK
      - RAFT_HEARTBEAT
      - RAFT_LOG_SIZE
      - RAFT_CHUNK_SIZE
      - RT_LOGLEVEL_CONSOLE
      - RT_SEQ_SESSION_PATH
      - RT_EXTERN_PREFIX
      - RT_QURAFT_LOG_LEVEL
      - RT_LOG_LEADER
      - RT_NO_ORDINAL
      - REPLICATOR_DELETE_FILES_PUSH
      - REPLICATOR_DELETE_FILES_PULL

# Startup command line common to all RT sequencers
x-rt-command:
  &rt-command
    command:
      [
        '-p',
        '6001',
        '-in',
        '$in_dir',
        '-out',
        '$out_dir',
        '-cp',
        '$state_dir',
        '-size',
        '$RT_REPLICAS',
        '-limit',
        '$limit',
        '-time',
        '$time',
        '-disk',
        '$disk'
      ]


networks:
  kx:
    name: kx
    driver: bridge

services:
  # RT services - these are different services (not replicas), each with its own volume
  rt-data-0:
    image: ${rt_sequencer}:${rt_sequencer_ver}
    profiles:
      - one-node
      - ""
    # Need to set hostname so rt nodes/publishers/subscribers can find each other
    hostname: rt-data-0
    # enrol_client.sh requires the container_name to be set
    container_name: rt-data-0
    <<: [*rt-command, *rt-env, *kxnet]
    # Have to run as root (default is nobody) in order to write to docker volume
    user: root
    restart: unless-stopped
    ports:
      - 127.0.0.1:5000:5000/tcp
      - 127.0.0.1:5001:5001/tcp
      - 127.0.0.1:5002:5002/tcp
      - 127.0.0.1:5006:5006/tcp
    volumes:
      - rt-data-0:/s
      - rt-data-session:/rt-session
      - ${server_certs_dir}:/cert
      - ${server_certs_dir}:/cert-sub

  rt-data-1:
    image: ${rt_sequencer}:${rt_sequencer_ver}
    profiles:
      - ""
    # Need to set hostname so rt nodes/publishers/subscribers can find each other
    hostname: rt-data-1
    # enrol_client.sh requires the container_name to be set
    container_name: rt-data-1
    <<: [*rt-command, *rt-env, *kxnet]
    # Have to run as root (default is nobody) in order to write to docker volume
    user: root
    restart: unless-stopped
    ports:
      - 127.0.0.2:5000:5000/tcp
      - 127.0.0.2:5001:5001/tcp
      - 127.0.0.2:5002:5002/tcp
      - 127.0.0.2:5006:5006/tcp
    volumes:
      - rt-data-1:/s
      - rt-data-session:/rt-session
      - ${server_certs_dir}:/cert
      - ${server_certs_dir}:/cert-sub

  rt-data-2:
    image: ${rt_sequencer}:${rt_sequencer_ver}
    profiles:
      - ""
    # Need to set hostname so rt nodes/publishers/subscribers can find each other
    hostname: rt-data-2
    # enrol_client.sh requires the container_name to be set
    container_name: rt-data-2
    <<: [*rt-command, *rt-env, *kxnet]
    # Have to run as root (default is nobody) in order to write to docker volume
    user: root
    restart: unless-stopped
    ports:
      - 127.0.0.3:5000:5000/tcp
      - 127.0.0.3:5001:5001/tcp
      - 127.0.0.3:5002:5002/tcp
      - 127.0.0.3:5006:5006/tcp
    volumes:
      - rt-data-2:/s
      - rt-data-session:/rt-session
      - ${server_certs_dir}:/cert
      - ${server_certs_dir}:/cert-sub

  rt-data:
    image: ${rt_sequencer}:${rt_sequencer_ver}
    profiles:
      - no-ordinal
    # Need to set hostname so rt nodes/publishers/subscribers can find each other
    hostname: rt-data
    # enrol_client.sh requires the container_name to be set
    container_name: rt-data
    <<: [*rt-command, *rt-env, *kxnet]
    # Have to run as root (default is nobody) in order to write to docker volume
    user: root
    restart: unless-stopped
    ports:
      - 127.0.0.1:5000:5000/tcp
      - 127.0.0.1:5001:5001/tcp
      - 127.0.0.1:5002:5002/tcp
      - 127.0.0.1:5006:5006/tcp
    volumes:
      - rt-data:/s
      - rt-data-session:/rt-session
      - ${server_certs_dir}:/cert
      - ${server_certs_dir}:/cert-sub
    # Hack to support $RT_NO_ORDINAL until run-raft-seq.q is properly fixed
    extra_hosts:
      - "rt-data-0:127.0.0.1"

  subscriber-0:
    image: ${rt_subscriber}:${rt_subscriber_ver}
    command: [ '-p', '6000' ]
    # Have to run as root (default is nobody) in order to write to docker volume
    user: root
    # Open a TTY for interactive publishing
    tty: true
    stdin_open: true
    environment:
      - KDB_LICENSE_B64
      - RT_TOPIC_PREFIX
      - RT_LOG_PATH
      - RT_STREAM
      - RT_REPLICAS
      - RT_NO_ORDINAL
      - TEST_SUB_METHOD
    volumes:
      - subscriber-0:/s
    <<: *kxnet

  publisher-0:
    image: ${rt_publisher}:${rt_publisher_ver}
    # It is necessary to fix the hostname of each publisher so it can resume its publishing session after a docker compose down then up
    hostname: publisher-0
    command: [ '-p', '6000' ]
    # Have to run as root (default is nobody) in order to write to docker volume
    user: root
    # Open a TTY to inspect received messages
    tty: true
    stdin_open: true
    environment:
      - KDB_LICENSE_B64
      - RT_TOPIC_PREFIX
      - RT_LOG_PATH
      - RT_STREAM
      - PUB_TIMER_FREQ
      - PUB_TIMER_MSG
      - RT_REPLICAS
      - RT_NO_ORDINAL
    volumes:
      - publisher-0:/s
    <<: *kxnet
