ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Clickhouse] ReplicatedMergeTree 알아보기
    Database/Clickhouse 2024. 2. 18. 07:05
    728x90
    반응형

     

    - 목차

     

    Clickhouse Cluster 구현하기.

    먼저 아래의 2개 이상의 Clickhouse Node 가 실행되는 Multi Replica 구성을 만들어야합니다.

    아래 링크는 docker-compose 를 통해서 Clickhouse Cluster 를 구축하는 내용을 적은 페이지인데요.

    간단한 명령어들로 구성된 페이지이기 때문에 클러스터 구축을 선행하시는 것을 추천드립니다.

     

    https://westlife0615.tistory.com/686#7

     

    [Clickhouse] Shard & Replica Cluster 구성하기

    - 목차 들어가며. 이번 글에서는 docker-compose 를 활용하여 Clickhouse Cluster 를 구성하는 내용을 작성하려고 합니다. ClickHouse 의 docker-compose Recipes 에서 설명하는 내용을 토대로 작성하였구요. Shard 와

    westlife0615.tistory.com

     

     

    ReplicatedMergeTree Table 생성하기.

    ReplicatedMergeTree 는 MergeTree 테이블 엔진에 Replication 기능이 추가된 형태로 생각하시면 됩니다.

    ReplicatedMergeTree Table 을 생성함으로써 하나의 Shard 내부의 Replica 서버 모두 동일한 Table 을 가지게 됩니다.

     

    ReplicatedMergeTree Table 의 DDL 은 아래와 같습니다.

    MergeTree Table 을 생성하는 쿼리와 차이점은 두가지가 존재하는데요.

    ON CLUSTER 선언문을 명시해야하고, macros 를 사용한다는 점입니다.

     

    create table default.user_actions on cluster cluster_1S_2R (
        user String,
        action String,
        acted_at DateTime
    ) engine=ReplicatedMergeTree('/clickhouse/tables/{shard}/{database}/user_actions', '{replica}')
    order by acted_at
    partition by toYYYYMM(acted_at);
    +-------------+----+------+-----+-------------------+----------------+
    |host         |port|status|error|num_hosts_remaining|num_hosts_active|
    +-------------+----+------+-----+-------------------+----------------+
    |clickhouse-01|9000|0     |     |1                  |0               |
    |clickhouse-02|9000|0     |     |0                  |0               |
    +-------------+----+------+-----+-------------------+----------------+

     

    ON CLUSTER.

    cluster 는 Clickhouse 의 config.xml 에서 remote_servers 태그에서 선언한 바 있습니다.

    만약 아래와 같이 설정되어 있다면, cluster 의 이름은 cluster_1S_2R 입니다.

        <remote_servers>
            <cluster_1S_2R>
                <shard>
                    <internal_replication>true</internal_replication>
                    <replica>
                        <host>clickhouse-01</host>
                        <port>9000</port>
                    </replica>
                    <replica>
                        <host>clickhouse-02</host>
                        <port>9000</port>
                    </replica>
                </shard>
            </cluster_1S_2R>
        </remote_servers>

     

    그리고 system.clusters 이라는 시스템 테이블을 통해서도 현재 존재하는 cluster 들을 조회할 수 있습니다.

     

    select * from system.clusters;
    +-----------------+---------+------------+-----------+-------------+------------+----+--------+-------+----------------+------------+---------------+-----------------------+
    |cluster          |shard_num|shard_weight|replica_num|host_name    |host_address|port|is_local|user   |default_database|errors_count|slowdowns_count|estimated_recovery_time|
    +-----------------+---------+------------+-----------+-------------+------------+----+--------+-------+----------------+------------+---------------+-----------------------+
    |cluster_1S_2R    |1        |1           |1          |clickhouse-01|172.28.0.5  |9000|1       |default|                |0           |0              |0                      |
    |cluster_1S_2R    |1        |1           |2          |clickhouse-02|172.28.0.6  |9000|0       |default|                |0           |0              |0                      |
    +-----------------+---------+------------+-----------+-------------+------------+----+--------+-------+----------------+------------+---------------+-----------------------+

     

    macros.

    Clickhouse 의 config.xml 에서 macro 설정을 할 수 있습니다.

    C 언어의 전처리기에서 macro 라는 기능으로 변수나 함수를 설정할 수 있죠.

    Clickhouse 에서도 Clickhouse Node 에서 적용가능한 macro 설정이 존재합니다.

    그리고 이는 shard, replica 와 같은 구별을 위해서 사용됩니다.

     

    < clickhouse-01-config.xml >

    <macros>
        <shard>01</shard>
        <replica>01</replica>
        <cluster>cluster_1S_2R</cluster>
    </macros>

     

     

    < clickhouse-02-config.xml >

    <macros>
        <shard>01</shard>
        <replica>02</replica>
        <cluster>cluster_1S_2R</cluster>
    </macros>

     

     

    그래서 ReplicatedMergeTree 는 아래와 같이 shard 와 replica 를 치환 가능하게 설정합니다.

    ReplicatedMergeTree('/clickhouse/tables/{shard}/{database}/user_actions', '{replica}')

     

     

    위의 create table 쿼리를 실행하게 되면 두 Clickhouse Node 에서 생성된 테이블을 각각 확인할 수 있습니다.

     

    select * from default.user_actions;
    0 rows retrieved in 19 ms (execution: 6 ms, fetching: 13 ms)

     

    remote Function 을 통해서 아래와 같이 원격의 Clickhouse 노드에 쿼리를 요청할 수 있습니다.

    select * from remote('clickhouse-02:9000', 'default.user_actions');
    0 rows retrieved in 36 ms (execution: 29 ms, fetching: 7 ms)

     

     

    Replication 확인해보기.

    이제 Insert Query 를 실행하여 Replication 이 수행되는지를 확인해보겠습니다.

    저는 clickhouse-01 노드에 insert query 를 요청하였고, 9개의 Row 가 생성되었습니다.

     

    insert into default.user_actions(user, action, acted_at)
    values ('Andy', 'Visit', '2023-01-01 00:00:00'),
    ('Brian', 'Buy', '2023-01-01 00:01:00'),
    ('Dennis', 'View', '2023-01-01 00:02:00'),
    ('Chris', 'View', '2023-01-01 00:03:00'),
    ('Emma', 'Buy', '2023-01-01 00:04:00'),
    ('Fabian', 'Cart', '2023-01-01 00:05:00'),
    ('Gareth', 'RemoveCart', '2023-01-01 00:06:00'),
    ('Hayden', 'Cart', '2023-01-01 00:07:00'),
    ('Illa', 'View', '2023-01-01 00:08:00');
    Query id: a1bf4f07-133a-40ed-bb82-fb8c2b1e1285
    
    Ok.
    
    9 rows in set. Elapsed: 0.025 sec.

     

     

    그리고 remote Function 을 통해서 clickhouse-02 노드로 조회 요청을 전달해보겠습니다.

    아래와 같이 정상적으로 Replication 이 완료됨을 확인할 수 있습니다.

     

    select * from remote('clickhouse-02:9000', 'default.user_actions');
    Query id: b9701064-4a9e-4367-8193-62bbab01e2c6
    
    ┌─user───┬─action─────┬────────────acted_at─┐
    │ Andy   │ Visit      │ 2023-01-01 00:00:00 │
    │ Brian  │ Buy        │ 2023-01-01 00:01:00 │
    │ Dennis │ View       │ 2023-01-01 00:02:00 │
    │ Chris  │ View       │ 2023-01-01 00:03:00 │
    │ Emma   │ Buy        │ 2023-01-01 00:04:00 │
    │ Fabian │ Cart       │ 2023-01-01 00:05:00 │
    │ Gareth │ RemoveCart │ 2023-01-01 00:06:00 │
    │ Hayden │ Cart       │ 2023-01-01 00:07:00 │
    │ Illa   │ View       │ 2023-01-01 00:08:00 │
    └────────┴────────────┴─────────────────────┘
    
    9 rows in set. Elapsed: 0.005 sec.

     

     

    반응형
Designed by Tistory.