# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version: '3'

services:
  oap:
    extends:
      file: ../../script/docker-compose/base-compose.yml
      service: oap
    ports:
      - "12800:12800"
    networks:
      - e2e

  banyandb:
    extends:
      file: ../../script/docker-compose/base-compose.yml
      service: banyandb
    ports:
      - 17912

  # Start zookeeper
  zookeeper:
    image: zookeeper:3.9.1
    expose:
      - 2181
    networks:
      - e2e
    environment:
      - ALLOW_ANONYMOUS_LOGIN=yes
    healthcheck:
      test: ["CMD", "nc", "-nz", "127.0.0.1", "2181"]
      interval: 15s
      timeout: 10s
      retries: 120

  # Start bookie
  bookie:
    image: apachepulsar/pulsar:3.1.1
    restart: on-failure
    hostname: bookie
    networks:
      - e2e
    environment:
      - clusterName=pulsar-cluster
      - zkServers=zookeeper:2181
      - metadataServiceUri=metadata-store:zk:zookeeper:2181
      - advertisedAddress=bookie
      - BOOKIE_MEM=-Xms512m -Xmx512m -XX:MaxDirectMemorySize=256m
      - allowLoopback=true
    depends_on:
      zookeeper:
        condition: service_healthy
    expose:
      - 8000
    ports:
      - "8000:8000"
    command: bash -c "bin/apply-config-from-env.py conf/bookkeeper.conf && exec bin/pulsar bookie"
    healthcheck:
      test: ["CMD", "nc", "-nz", "127.0.0.1", "8000"]
      interval: 15s
      timeout: 10s
      retries: 120

  # Start broker
  broker:
    image: apachepulsar/pulsar:3.1.1
    restart: on-failure
    hostname: broker
    networks:
      - e2e
    environment:
      - metadataStoreUrl=zk:zookeeper:2181
      - zookeeperServers=zookeeper:2181
      - clusterName=pulsar-cluster
      - managedLedgerDefaultEnsembleSize=1
      - managedLedgerDefaultWriteQuorum=1
      - managedLedgerDefaultAckQuorum=1
      - advertisedAddress=broker
      - advertisedListeners=external:pulsar://127.0.0.1:6650
      - PULSAR_MEM=-Xms512m -Xmx512m -XX:MaxDirectMemorySize=256m
    depends_on:
      zookeeper:
        condition: service_healthy
    expose:
      - 8080
      - 6650
    ports:
      - "6650:6650"
      - "8080:8080"
    command: bash -c "bin/pulsar initialize-cluster-metadata --cluster pulsar-cluster --zookeeper zookeeper:2181 --configuration-store zookeeper:2181 --web-service-url http://broker:8080 --broker-service-url pulsar://broker:6650 && bin/apply-config-from-env.py conf/broker.conf && exec bin/pulsar broker"
    healthcheck:
      test: ["CMD", "nc", "-nz", "127.0.0.1", "8080"]
      interval: 15s
      timeout: 10s
      retries: 120

  consume-perf:
    image: apachepulsar/pulsar:3.1.1
    networks:
      - e2e
    depends_on:
      broker:
        condition: service_healthy
    command: bash -c "bin/pulsar-perf consume -m 100000 my-topic"

  produce-perf:
    image: apachepulsar/pulsar:3.1.1
    networks:
      - e2e
    depends_on:
      broker:
        condition: service_healthy
      consume-perf:
        condition: service_started
    command: bash -c "bin/pulsar-perf produce -m 100000 my-topic"

  otel-collector:
    image: otel/opentelemetry-collector:${OTEL_COLLECTOR_VERSION}
    networks:
      - e2e
    command: [ "--config=/etc/otel-collector-config.yaml" ]
    volumes:
      - ./otel-collector-config.yaml:/etc/otel-collector-config.yaml
    expose:
      - 55678
    depends_on:
      oap:
        condition: service_healthy
networks:
  e2e:
