dockerdocker-composeclickhouse

Cannot create the replica table Code: 253, Exception: Replica /clickhouse/tables/[...] already exists (version 21.8.10.19 Docker


I am trying to set up a ClickHouse cluster with 1 shard and 2 replicas using docker. For testing purposes, I only have one zookeeper in a single container and two ClickHouse containers. Creating non replicated tables is possible, but it is not possible to use replicated tables.

when I create a replicated table

CREATE TABLE company_db.g ON CLUSTER company_cluster
(
    `payload` String
)
ENGINE = ReplicatedMergeTree
ORDER BY payload

ClickHouse returns

┌─host────────┬─port─┬─status─┬─error───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬─num_hosts_remaining─┬─num_hosts_active─┐
│ clickhouse0 │ 9000 │    253 │ Code: 253, e.displayText() = DB::Exception: Replica /clickhouse/tables/0/company_db/g/replicas/clickhouse1 already exists (version 21.8.10.19 (official build)) │                   1 │                1 │
└─────────────┴──────┴────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴─────────────────────┴──────────────────┘
┌─host────────┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
│ clickhouse1 │ 9000 │      0 │       │                   0 │                0 │
└─────────────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘
↓ Progress: 0.00 rows, 0.00 B (0.00 rows/s., 0.00 B/s.)  0%
2 rows in set. Elapsed: 0.170 sec. 

Received exception from server (version 21.8.10):
Code: 253. DB::Exception: Received from localhost:9000. DB::Exception: There was an error on [clickhouse0:9000]: Code: 253, e.displayText() = DB::Exception: Replica /clickhouse/tables/0/company_db/g/replicas/clickhouse1 already exists (version 21.8.10.19 (official build)).

docker-compose.yaml

version: '3.5'

services:

  zookeeper0:
    image: zookeeper
    container_name: zookeeper0
    hostname: zookeeper0
    ports:
      - 2181
    networks:
      clickhouse-network:

  clickhouse0:
    image: yandex/clickhouse-server
    container_name: clickhouse0
    hostname: clickhouse0
    build:
      context: .
      dockerfile: clickhouse.dockerfile
      args:
        shard: 0
        replica: 0
    ports: &ch_ports
      - 8123
      - 9000
      - 9009
      - 9100
    networks:
      clickhouse-network:

    volumes:
      - ${PWD}/clickhouse0:/var/lib/clickhouse
    depends_on:  &ch_dependancies
      - zookeeper0

  clickhouse1:
    image: yandex/clickhouse-server
    container_name: clickhouse1
    hostname: clickhouse1
    build:
      context: .
      dockerfile: clickhouse.dockerfile
      args:
        shard: 0
        replica: 1
    ports: 
      *ch_ports
    networks:
      clickhouse-network:
    volumes:
      - ${PWD}/clickhouse1:/var/lib/clickhouse
    depends_on:
      *ch_dependancies

networks:
  clickhouse-network:

spefic_onfig.xml

<yandex>
     <!-- Listen wildcard address to allow accepting connections from other containers and host network. -->
    <listen_host>::</listen_host>
    <listen_host>0.0.0.0</listen_host>
    <listen_try>1</listen_try>

    <interserver_http_port>9009</interserver_http_port>

    <zookeeper>
        <node index="1">
            <host>zookeeper0</host>
            <port>2181</port>
        </node>

    </zookeeper>

    <remote_servers>
        <company_cluster>
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>clickhouse0</host>
                    <port>9000</port>
                </replica>
                <replica>
                    <host>clickhouse1</host>
                    <port>9000</port>
                </replica>
            </shard>
        </company_cluster>
    </remote_servers>

    <default_replica_path>/clickhouse/tables/{shard}/{database}/{table}</default_replica_path>
    <default_replica_name>{replica}</default_replica_name>

    <interserver_http_host>clickhouse${REPLICA}</interserver_http_host>
    
     <macros>
        <shard>${SHARD}</shard>
        <replica>clickhouse${REPLICA}</replica>
    </macros>

</yandex>

Solution

  • I believe it was caused by a network issue.

    I corrected up my docker-compose.yml

        networks:
          clickhouse-network:
    

    by

        networks:
          - clickhouse-network
    

    then I ran

    docker-compose down && docker volume prune -f && docker-compose up -d