Skip to content

Instantly share code, notes, and snippets.

@den-crane
Last active February 28, 2023 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save den-crane/84c91e67e465d06b02bf03cd65099264 to your computer and use it in GitHub Desktop.
Save den-crane/84c91e67e465d06b02bf03cd65099264 to your computer and use it in GitHub Desktop.
Buffer -> Distributed -> Replicated

Buffer -> Replicated

create database test on cluster '{cluster}' Engine=Ordinary;

create table test.test on cluster '{cluster}' (A Int64) 
Engine = ReplicatedMergeTree('/clickhouse/{cluster}/tables/{database}/{table}', '{replica}') order by A;

create table test.test_b on cluster  '{cluster}' as test.test
Engine = Buffer(test, test, 1, 1, 2, 10, 100, 10000000, 100000000);

Replica1: 
   -- emulate RO ReplicatedMergeTree
   detach table test.test; 
   attach table test.test (A Int64) Engine = ReplicatedMergeTree('/clickhouse/{cluster}/tables/{database}/xxxxx', '{replica}' order by A;
   
   for i in `seq 1 5000`; do clickhouse-client -q "insert into test.test_b values ($i)"; done;
        Received exception from server (version 22.8.13):
        Code: 242. DB::Exception: Received from localhost:9000. DB::Exception: Table is in readonly mode (replica path: /clickhouse/ch/tables/test/xxxxx/replicas/Replica1). (TABLE_IS_READ_ONLY)
        (query: insert into test.test_b values (86))
        Received exception from server (version 22.8.13):
        Code: 242. DB::Exception: Received from localhost:9000. DB::Exception: Table is in readonly mode (replica path: /clickhouse/ch/tables/test/xxxxx/replicas/Replica1). (TABLE_IS_READ_ONLY)
        (query: insert into test.test_b values (102))
        Received exception from server (version 22.8.13):

   select count() from test.test;
   ┌─count()─┐
   │       0 │
   └─────────┘

   select count() from test.test_b;
   ┌─count()─┐
   │     100-- 100 rows in the Buffer table
   └─────────┘

Replica2: 
   select count() from test.test;
   ┌─count()─┐
   │       0 │
   └─────────┘

   select count() from test.test_b;
   ┌─count()─┐
   │       0 │
   └─────────┘

Buffer -> Distributed -> Replicated

cat /etc/clickhouse-server/users.d/prefer_localhost_replica.xml
<?xml version="1.0" ?>
<clickhouse>
    <profiles>
        <default>
           <load_balancing>random</load_balancing>
           <prefer_localhost_replica>0</prefer_localhost_replica>
        </default>
    </profiles>
</clickhouse>
create table test.test on cluster '{cluster}' (A Int64) 
Engine = ReplicatedMergeTree('/clickhouse/{cluster}/tables/{database}/{table}', '{replica}')
order by A;

create table test.test_d on cluster '{cluster}' as test.test
Engine = Distributed ('{cluster}' , test, test, rand());

create table test.test_b on cluster '{cluster}' as test.test
Engine = Buffer(test, test_d, 1, 1, 2, 10, 100, 10000000, 100000000);

Replica1: 
   -- emulate RO ReplicatedMergeTree
   detach table test.test;
   attach table test.test (A Int64) 
   Engine = ReplicatedMergeTree('/clickhouse/{cluster}/tables/{database}/xxxxx', '{replica}') order by A;

   for i in `seq 1 5000`; do clickhouse-client -q "insert into test.test_b values ($i)"; done;
   No errors
   
   select count() from test.test;
   ┌─count()─┐
   │       0 │
   └─────────┘
   
Replica2:
   select count() from test.test;
   ┌─count()─┐
   │    5000 │
   └─────────┘
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment