# 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:
  zookeeper:
    image: zookeeper:3.9.1
    expose:
      - 2181
    networks:
      - sw
    environment:
      - ALLOW_ANONYMOUS_LOGIN=yes
    healthcheck:
      test: ["CMD", "nc", "-nz", "127.0.0.1", "2181"]
      interval: 15s
      timeout: 10s
      retries: 120

  bookie:
    image: apachepulsar/pulsar:3.1.1
    restart: on-failure
    hostname: bookie
    networks:
      - sw
    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

  broker:
    image: apachepulsar/pulsar:3.1.1
    restart: on-failure
    hostname: broker
    networks:
      - sw
    environment:
      - webServicePort=8080
      - 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:
      - sw
    depends_on:
      broker:
        condition: service_healthy
    command: bash -c "bin/pulsar-perf consume -m 1 sw-topic"

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

  otel-collector:
    image: ${OTEL_COLLECTOR_IMAGE}:${OTEL_COLLECTOR_IMAGE_TAG}
    networks:
      - sw
    command: [ "--config=/etc/otel-collector-config.yaml" ]
    volumes:
      - ./config/pulsar/otel-collector-config.yaml:/etc/otel-collector-config.yaml
    expose:
      - 55678
    depends_on:
      oap:
        condition: service_healthy

networks:
  sw:
