2021-01-27 21:46:19 +00:00
|
|
|
import pytest
|
2022-07-25 15:32:51 +00:00
|
|
|
|
|
|
|
# FIXME Tests with MaterializedPostgresSQL are temporarily disabled
|
|
|
|
# https://github.com/ClickHouse/ClickHouse/issues/36898
|
|
|
|
# https://github.com/ClickHouse/ClickHouse/issues/38677
|
|
|
|
# https://github.com/ClickHouse/ClickHouse/pull/39272#issuecomment-1190087190
|
|
|
|
|
|
|
|
pytestmark = pytest.mark.skip
|
|
|
|
|
2021-01-27 21:46:19 +00:00
|
|
|
import time
|
|
|
|
import psycopg2
|
|
|
|
import os.path as p
|
|
|
|
|
|
|
|
from helpers.cluster import ClickHouseCluster
|
|
|
|
from helpers.test_tools import assert_eq_with_retry
|
|
|
|
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
|
|
|
|
from helpers.test_tools import TSV
|
|
|
|
|
2021-07-01 08:20:13 +00:00
|
|
|
import threading
|
|
|
|
|
2021-01-27 21:46:19 +00:00
|
|
|
cluster = ClickHouseCluster(__file__)
|
2022-03-22 16:39:58 +00:00
|
|
|
instance = cluster.add_instance(
|
|
|
|
"instance",
|
|
|
|
main_configs=["configs/log_conf.xml"],
|
|
|
|
with_postgres=True,
|
|
|
|
stay_alive=True,
|
|
|
|
)
|
2021-01-27 21:46:19 +00:00
|
|
|
|
|
|
|
postgres_table_template = """
|
|
|
|
CREATE TABLE IF NOT EXISTS {} (
|
2021-02-18 18:14:05 +00:00
|
|
|
key Integer NOT NULL, value Integer, PRIMARY KEY(key))
|
2021-01-27 21:46:19 +00:00
|
|
|
"""
|
|
|
|
|
2021-07-01 08:20:13 +00:00
|
|
|
queries = [
|
2022-04-03 16:39:14 +00:00
|
|
|
"INSERT INTO {} select i, i from generate_series(0, 10000) as t(i);",
|
|
|
|
"DELETE FROM {} WHERE (value*value) % 3 = 0;",
|
|
|
|
"UPDATE {} SET value = value + 125 WHERE key % 2 = 0;",
|
|
|
|
"UPDATE {} SET key=key+20000 WHERE key%2=0",
|
|
|
|
"INSERT INTO {} select i, i from generate_series(40000, 50000) as t(i);",
|
|
|
|
"DELETE FROM {} WHERE key % 10 = 0;",
|
|
|
|
"UPDATE {} SET value = value + 101 WHERE key % 2 = 1;",
|
|
|
|
"UPDATE {} SET key=key+80000 WHERE key%2=1",
|
|
|
|
"DELETE FROM {} WHERE value % 2 = 0;",
|
|
|
|
"UPDATE {} SET value = value + 2000 WHERE key % 5 = 0;",
|
|
|
|
"INSERT INTO {} select i, i from generate_series(200000, 250000) as t(i);",
|
|
|
|
"DELETE FROM {} WHERE value % 3 = 0;",
|
|
|
|
"UPDATE {} SET value = value * 2 WHERE key % 3 = 0;",
|
|
|
|
"UPDATE {} SET key=key+500000 WHERE key%2=1",
|
|
|
|
"INSERT INTO {} select i, i from generate_series(1000000, 1050000) as t(i);",
|
|
|
|
"DELETE FROM {} WHERE value % 9 = 2;",
|
|
|
|
"UPDATE {} SET key=key+10000000",
|
|
|
|
"UPDATE {} SET value = value + 2 WHERE key % 3 = 1;",
|
|
|
|
"DELETE FROM {} WHERE value%5 = 0;",
|
2022-03-22 16:39:58 +00:00
|
|
|
]
|
2021-07-01 08:20:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.timeout(30)
|
2022-03-22 16:39:58 +00:00
|
|
|
def check_tables_are_synchronized(
|
|
|
|
table_name, order_by="key", postgres_database="postgres_database"
|
|
|
|
):
|
2022-04-08 11:50:24 +00:00
|
|
|
while True:
|
|
|
|
expected = instance.query(
|
|
|
|
"select * from {}.{} order by {};".format(
|
|
|
|
postgres_database, table_name, order_by
|
|
|
|
)
|
2022-03-22 16:39:58 +00:00
|
|
|
)
|
|
|
|
result = instance.query(
|
|
|
|
"select * from test.{} order by {};".format(table_name, order_by)
|
|
|
|
)
|
2022-04-08 11:50:24 +00:00
|
|
|
if result == expected:
|
|
|
|
break
|
2021-07-01 08:20:13 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
assert result == expected
|
2021-07-01 08:20:13 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
|
|
|
|
def get_postgres_conn(
|
|
|
|
ip, port, database=False, auto_commit=True, database_name="postgres_database"
|
|
|
|
):
|
2021-01-27 21:46:19 +00:00
|
|
|
if database == True:
|
2022-03-22 16:39:58 +00:00
|
|
|
conn_string = "host={} port={} dbname='{}' user='postgres' password='mysecretpassword'".format(
|
|
|
|
ip, port, database_name
|
|
|
|
)
|
2021-01-27 21:46:19 +00:00
|
|
|
else:
|
2022-03-22 16:39:58 +00:00
|
|
|
conn_string = (
|
|
|
|
"host={} port={} user='postgres' password='mysecretpassword'".format(
|
|
|
|
ip, port
|
|
|
|
)
|
|
|
|
)
|
2021-06-03 19:45:27 +00:00
|
|
|
|
2021-01-27 21:46:19 +00:00
|
|
|
conn = psycopg2.connect(conn_string)
|
2021-06-03 19:45:27 +00:00
|
|
|
if auto_commit:
|
|
|
|
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
|
|
|
|
conn.autocommit = True
|
2021-01-27 21:46:19 +00:00
|
|
|
return conn
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
|
2021-01-27 21:46:19 +00:00
|
|
|
def create_postgres_db(cursor, name):
|
|
|
|
cursor.execute("CREATE DATABASE {}".format(name))
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
|
|
|
|
def create_clickhouse_postgres_db(ip, port, name="postgres_database"):
|
|
|
|
instance.query(
|
|
|
|
"""
|
2021-06-03 19:45:27 +00:00
|
|
|
CREATE DATABASE {}
|
2022-03-22 16:39:58 +00:00
|
|
|
ENGINE = PostgreSQL('{}:{}', '{}', 'postgres', 'mysecretpassword')""".format(
|
|
|
|
name, ip, port, name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-06-03 19:45:27 +00:00
|
|
|
|
2022-04-08 11:50:24 +00:00
|
|
|
def create_materialized_table(ip, port, table_name="postgresql_replica"):
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query(
|
2022-04-03 16:39:14 +00:00
|
|
|
f"""
|
2022-04-05 17:01:41 +00:00
|
|
|
CREATE TABLE test.{table_name} (key Int64, value Int64)
|
2021-06-27 19:09:17 +00:00
|
|
|
ENGINE = MaterializedPostgreSQL(
|
2022-04-03 16:39:14 +00:00
|
|
|
'{ip}:{port}', 'postgres_database', '{table_name}', 'postgres', 'mysecretpassword')
|
|
|
|
PRIMARY KEY key; """
|
2022-03-22 16:39:58 +00:00
|
|
|
)
|
|
|
|
|
2021-02-18 18:14:05 +00:00
|
|
|
|
|
|
|
def create_postgres_table(cursor, table_name, replica_identity_full=False):
|
2021-02-12 18:21:55 +00:00
|
|
|
cursor.execute("DROP TABLE IF EXISTS {}".format(table_name))
|
2021-01-27 21:46:19 +00:00
|
|
|
cursor.execute(postgres_table_template.format(table_name))
|
2021-02-18 18:14:05 +00:00
|
|
|
if replica_identity_full:
|
2022-03-22 16:39:58 +00:00
|
|
|
cursor.execute("ALTER TABLE {} REPLICA IDENTITY FULL;".format(table_name))
|
2021-02-18 18:14:05 +00:00
|
|
|
|
2021-01-27 21:46:19 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
def postgresql_replica_check_result(
|
|
|
|
result, check=False, ref_file="test_postgresql_replica.reference"
|
|
|
|
):
|
2021-01-27 21:46:19 +00:00
|
|
|
fpath = p.join(p.dirname(__file__), ref_file)
|
|
|
|
with open(fpath) as reference:
|
|
|
|
if check:
|
|
|
|
assert TSV(result) == TSV(reference)
|
|
|
|
else:
|
|
|
|
return TSV(result) == TSV(reference)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def started_cluster():
|
|
|
|
try:
|
|
|
|
cluster.start()
|
2022-03-22 16:39:58 +00:00
|
|
|
conn = get_postgres_conn(ip=cluster.postgres_ip, port=cluster.postgres_port)
|
2021-01-27 21:46:19 +00:00
|
|
|
cursor = conn.cursor()
|
2022-03-22 16:39:58 +00:00
|
|
|
create_postgres_db(cursor, "postgres_database")
|
|
|
|
create_clickhouse_postgres_db(
|
|
|
|
ip=cluster.postgres_ip, port=cluster.postgres_port
|
|
|
|
)
|
2021-06-03 19:45:27 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query("CREATE DATABASE test")
|
2021-01-27 21:46:19 +00:00
|
|
|
yield cluster
|
|
|
|
|
|
|
|
finally:
|
|
|
|
cluster.shutdown()
|
|
|
|
|
|
|
|
|
2021-02-22 12:35:53 +00:00
|
|
|
@pytest.mark.timeout(320)
|
2021-01-27 21:46:19 +00:00
|
|
|
def test_initial_load_from_snapshot(started_cluster):
|
2022-03-22 16:39:58 +00:00
|
|
|
conn = get_postgres_conn(
|
|
|
|
ip=started_cluster.postgres_ip,
|
|
|
|
port=started_cluster.postgres_port,
|
|
|
|
database=True,
|
|
|
|
)
|
2021-01-27 21:46:19 +00:00
|
|
|
cursor = conn.cursor()
|
2022-03-22 16:39:58 +00:00
|
|
|
create_postgres_table(cursor, "postgresql_replica")
|
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT number, number from numbers(50)"
|
|
|
|
)
|
2021-01-27 21:46:19 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query("DROP TABLE IF EXISTS test.postgresql_replica")
|
|
|
|
create_materialized_table(
|
|
|
|
ip=started_cluster.postgres_ip, port=started_cluster.postgres_port
|
|
|
|
)
|
2021-01-27 21:46:19 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT * FROM test.postgresql_replica ORDER BY key;")
|
2021-02-06 00:17:54 +00:00
|
|
|
while postgresql_replica_check_result(result) == False:
|
|
|
|
time.sleep(0.2)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT * FROM test.postgresql_replica ORDER BY key;")
|
2021-02-06 00:17:54 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
cursor.execute("DROP TABLE postgresql_replica;")
|
2021-01-27 21:46:19 +00:00
|
|
|
postgresql_replica_check_result(result, True)
|
2023-05-03 18:06:46 +00:00
|
|
|
instance.query(f"DROP TABLE test.postgresql_replica SYNC")
|
2021-01-27 21:46:19 +00:00
|
|
|
|
|
|
|
|
2021-02-22 12:35:53 +00:00
|
|
|
@pytest.mark.timeout(320)
|
2021-01-30 12:05:14 +00:00
|
|
|
def test_no_connection_at_startup(started_cluster):
|
2022-03-22 16:39:58 +00:00
|
|
|
conn = get_postgres_conn(
|
|
|
|
ip=started_cluster.postgres_ip,
|
|
|
|
port=started_cluster.postgres_port,
|
|
|
|
database=True,
|
|
|
|
)
|
2021-01-30 12:05:14 +00:00
|
|
|
cursor = conn.cursor()
|
2022-03-22 16:39:58 +00:00
|
|
|
create_postgres_table(cursor, "postgresql_replica")
|
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT number, number from numbers(50)"
|
|
|
|
)
|
|
|
|
|
|
|
|
instance.query("DROP TABLE IF EXISTS test.postgresql_replica")
|
|
|
|
create_materialized_table(
|
|
|
|
ip=started_cluster.postgres_ip, port=started_cluster.postgres_port
|
|
|
|
)
|
2021-01-30 12:05:14 +00:00
|
|
|
time.sleep(3)
|
2021-05-03 18:38:44 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query("DETACH TABLE test.postgresql_replica")
|
|
|
|
started_cluster.pause_container("postgres1")
|
2021-05-03 18:38:44 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query("ATTACH TABLE test.postgresql_replica")
|
2021-05-03 18:38:44 +00:00
|
|
|
time.sleep(3)
|
2022-03-22 16:39:58 +00:00
|
|
|
started_cluster.unpause_container("postgres1")
|
2021-01-30 12:05:14 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
2021-01-30 12:05:14 +00:00
|
|
|
while int(result) == 0:
|
2022-03-22 16:39:58 +00:00
|
|
|
time.sleep(0.5)
|
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
2021-01-30 12:05:14 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT * FROM test.postgresql_replica ORDER BY key;")
|
|
|
|
cursor.execute("DROP TABLE postgresql_replica;")
|
2021-01-30 12:05:14 +00:00
|
|
|
postgresql_replica_check_result(result, True)
|
2023-05-03 18:06:46 +00:00
|
|
|
instance.query(f"DROP TABLE test.postgresql_replica SYNC")
|
2021-01-30 12:05:14 +00:00
|
|
|
|
2021-01-31 19:03:03 +00:00
|
|
|
|
2021-02-22 12:35:53 +00:00
|
|
|
@pytest.mark.timeout(320)
|
2021-01-31 19:03:03 +00:00
|
|
|
def test_detach_attach_is_ok(started_cluster):
|
2022-03-22 16:39:58 +00:00
|
|
|
conn = get_postgres_conn(
|
|
|
|
ip=started_cluster.postgres_ip,
|
|
|
|
port=started_cluster.postgres_port,
|
|
|
|
database=True,
|
|
|
|
)
|
2021-01-31 19:03:03 +00:00
|
|
|
cursor = conn.cursor()
|
2022-03-22 16:39:58 +00:00
|
|
|
create_postgres_table(cursor, "postgresql_replica")
|
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT number, number from numbers(50)"
|
|
|
|
)
|
2021-01-31 19:03:03 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query("DROP TABLE IF EXISTS test.postgresql_replica")
|
|
|
|
create_materialized_table(
|
|
|
|
ip=started_cluster.postgres_ip, port=started_cluster.postgres_port
|
|
|
|
)
|
2021-01-31 19:03:03 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
|
|
|
while int(result) == 0:
|
2021-02-04 21:05:43 +00:00
|
|
|
time.sleep(0.2)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
2021-02-04 21:05:43 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT * FROM test.postgresql_replica ORDER BY key;")
|
2021-01-31 19:03:03 +00:00
|
|
|
postgresql_replica_check_result(result, True)
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query("DETACH TABLE test.postgresql_replica")
|
|
|
|
instance.query("ATTACH TABLE test.postgresql_replica")
|
2021-01-31 19:03:03 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT * FROM test.postgresql_replica ORDER BY key;")
|
2021-02-22 12:35:53 +00:00
|
|
|
while postgresql_replica_check_result(result) == False:
|
|
|
|
time.sleep(0.5)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT * FROM test.postgresql_replica ORDER BY key;")
|
2021-02-22 12:35:53 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
cursor.execute("DROP TABLE postgresql_replica;")
|
2021-01-31 19:03:03 +00:00
|
|
|
postgresql_replica_check_result(result, True)
|
2023-05-03 18:06:46 +00:00
|
|
|
instance.query(f"DROP TABLE test.postgresql_replica SYNC")
|
2021-01-31 19:03:03 +00:00
|
|
|
|
|
|
|
|
2021-02-22 12:35:53 +00:00
|
|
|
@pytest.mark.timeout(320)
|
2021-02-03 16:13:18 +00:00
|
|
|
def test_replicating_insert_queries(started_cluster):
|
2022-03-22 16:39:58 +00:00
|
|
|
conn = get_postgres_conn(
|
|
|
|
ip=started_cluster.postgres_ip,
|
|
|
|
port=started_cluster.postgres_port,
|
|
|
|
database=True,
|
|
|
|
)
|
2021-01-31 19:03:03 +00:00
|
|
|
cursor = conn.cursor()
|
2022-03-22 16:39:58 +00:00
|
|
|
create_postgres_table(cursor, "postgresql_replica")
|
2021-01-31 19:03:03 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT number, number from numbers(10)"
|
|
|
|
)
|
2021-01-31 19:03:03 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query("DROP TABLE IF EXISTS test.postgresql_replica")
|
|
|
|
create_materialized_table(
|
|
|
|
ip=started_cluster.postgres_ip, port=started_cluster.postgres_port
|
|
|
|
)
|
2021-01-31 19:03:03 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
|
|
|
while int(result) != 10:
|
2021-02-04 21:05:43 +00:00
|
|
|
time.sleep(0.2)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
2021-02-04 21:05:43 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT 10 + number, 10 + number from numbers(10)"
|
|
|
|
)
|
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT 20 + number, 20 + number from numbers(10)"
|
|
|
|
)
|
2021-01-31 19:03:03 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
|
|
|
while int(result) != 30:
|
2021-02-06 00:17:54 +00:00
|
|
|
time.sleep(0.2)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
2021-01-31 19:03:03 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT 30 + number, 30 + number from numbers(10)"
|
|
|
|
)
|
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT 40 + number, 40 + number from numbers(10)"
|
|
|
|
)
|
2021-01-31 19:03:03 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
|
|
|
while int(result) != 50:
|
2021-02-06 00:17:54 +00:00
|
|
|
time.sleep(0.2)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
2021-01-31 19:03:03 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT * FROM test.postgresql_replica ORDER BY key;")
|
|
|
|
cursor.execute("DROP TABLE postgresql_replica;")
|
2021-02-03 16:13:18 +00:00
|
|
|
postgresql_replica_check_result(result, True)
|
2023-05-03 18:06:46 +00:00
|
|
|
instance.query(f"DROP TABLE test.postgresql_replica SYNC")
|
2021-02-03 16:13:18 +00:00
|
|
|
|
2021-01-31 19:03:03 +00:00
|
|
|
|
2021-02-22 12:35:53 +00:00
|
|
|
@pytest.mark.timeout(320)
|
2021-02-03 16:13:18 +00:00
|
|
|
def test_replicating_delete_queries(started_cluster):
|
2022-03-22 16:39:58 +00:00
|
|
|
conn = get_postgres_conn(
|
|
|
|
ip=started_cluster.postgres_ip,
|
|
|
|
port=started_cluster.postgres_port,
|
|
|
|
database=True,
|
|
|
|
)
|
2021-02-03 16:13:18 +00:00
|
|
|
cursor = conn.cursor()
|
2022-03-22 16:39:58 +00:00
|
|
|
create_postgres_table(cursor, "postgresql_replica")
|
2021-02-03 16:13:18 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT number, number from numbers(50)"
|
|
|
|
)
|
2021-02-03 16:13:18 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query("DROP TABLE IF EXISTS test.postgresql_replica")
|
|
|
|
create_materialized_table(
|
|
|
|
ip=started_cluster.postgres_ip, port=started_cluster.postgres_port
|
|
|
|
)
|
2021-02-03 16:13:18 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT * FROM test.postgresql_replica ORDER BY key;")
|
2021-02-06 00:17:54 +00:00
|
|
|
while postgresql_replica_check_result(result) == False:
|
2021-02-04 21:05:43 +00:00
|
|
|
time.sleep(0.2)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT * FROM test.postgresql_replica ORDER BY key;")
|
2021-02-04 21:05:43 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT 50 + number, 50 + number from numbers(50)"
|
|
|
|
)
|
2021-02-03 16:13:18 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
2021-02-06 00:17:54 +00:00
|
|
|
while int(result) != 100:
|
|
|
|
time.sleep(0.5)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
2021-02-03 16:13:18 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
cursor.execute("DELETE FROM postgresql_replica WHERE key > 49;")
|
2021-02-03 16:13:18 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT * FROM test.postgresql_replica ORDER BY key;")
|
2021-02-06 00:17:54 +00:00
|
|
|
while postgresql_replica_check_result(result) == False:
|
|
|
|
time.sleep(0.5)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT * FROM test.postgresql_replica ORDER BY key;")
|
2021-02-06 00:17:54 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
cursor.execute("DROP TABLE postgresql_replica;")
|
2021-01-31 19:03:03 +00:00
|
|
|
postgresql_replica_check_result(result, True)
|
|
|
|
|
|
|
|
|
2021-02-22 12:35:53 +00:00
|
|
|
@pytest.mark.timeout(320)
|
2021-02-04 09:33:31 +00:00
|
|
|
def test_replicating_update_queries(started_cluster):
|
2022-03-22 16:39:58 +00:00
|
|
|
conn = get_postgres_conn(
|
|
|
|
ip=started_cluster.postgres_ip,
|
|
|
|
port=started_cluster.postgres_port,
|
|
|
|
database=True,
|
|
|
|
)
|
2021-02-04 09:33:31 +00:00
|
|
|
cursor = conn.cursor()
|
2022-03-22 16:39:58 +00:00
|
|
|
create_postgres_table(cursor, "postgresql_replica")
|
2021-02-04 09:33:31 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT number, number + 10 from numbers(50)"
|
|
|
|
)
|
2021-02-04 09:33:31 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query("DROP TABLE IF EXISTS test.postgresql_replica")
|
|
|
|
create_materialized_table(
|
|
|
|
ip=started_cluster.postgres_ip, port=started_cluster.postgres_port
|
|
|
|
)
|
2021-02-04 09:33:31 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
|
|
|
while int(result) != 50:
|
2021-02-04 21:05:43 +00:00
|
|
|
time.sleep(0.2)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
2021-02-04 21:05:43 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
cursor.execute("UPDATE postgresql_replica SET value = value - 10;")
|
2021-02-04 09:33:31 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT * FROM test.postgresql_replica ORDER BY key;")
|
2021-02-06 00:17:54 +00:00
|
|
|
while postgresql_replica_check_result(result) == False:
|
|
|
|
time.sleep(0.5)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT * FROM test.postgresql_replica ORDER BY key;")
|
2021-02-06 00:17:54 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
cursor.execute("DROP TABLE postgresql_replica;")
|
2021-02-04 17:17:16 +00:00
|
|
|
postgresql_replica_check_result(result, True)
|
|
|
|
|
|
|
|
|
2021-02-22 12:35:53 +00:00
|
|
|
@pytest.mark.timeout(320)
|
2021-02-04 17:17:16 +00:00
|
|
|
def test_resume_from_written_version(started_cluster):
|
2022-03-22 16:39:58 +00:00
|
|
|
conn = get_postgres_conn(
|
|
|
|
ip=started_cluster.postgres_ip,
|
|
|
|
port=started_cluster.postgres_port,
|
|
|
|
database=True,
|
|
|
|
)
|
2021-02-04 17:17:16 +00:00
|
|
|
cursor = conn.cursor()
|
2022-03-22 16:39:58 +00:00
|
|
|
create_postgres_table(cursor, "postgresql_replica")
|
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT number, number + 10 from numbers(50)"
|
|
|
|
)
|
2021-02-04 17:17:16 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query("DROP TABLE IF EXISTS test.postgresql_replica")
|
|
|
|
create_materialized_table(
|
|
|
|
ip=started_cluster.postgres_ip, port=started_cluster.postgres_port
|
|
|
|
)
|
2021-02-04 17:17:16 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
|
|
|
while int(result) != 50:
|
2021-02-04 21:05:43 +00:00
|
|
|
time.sleep(0.2)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
2021-02-04 21:05:43 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT 50 + number, 50 + number from numbers(50)"
|
|
|
|
)
|
2021-02-04 17:17:16 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
|
|
|
while int(result) != 100:
|
2021-02-06 00:17:54 +00:00
|
|
|
time.sleep(0.2)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
2021-02-04 17:17:16 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query("DETACH TABLE test.postgresql_replica")
|
2021-02-04 17:17:16 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
cursor.execute("DELETE FROM postgresql_replica WHERE key > 49;")
|
|
|
|
cursor.execute("UPDATE postgresql_replica SET value = value - 10;")
|
2021-02-04 17:17:16 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query("ATTACH TABLE test.postgresql_replica")
|
2021-02-04 17:17:16 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT * FROM test.postgresql_replica ORDER BY key;")
|
2021-02-06 00:17:54 +00:00
|
|
|
while postgresql_replica_check_result(result) == False:
|
|
|
|
time.sleep(0.5)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT * FROM test.postgresql_replica ORDER BY key;")
|
2021-02-06 00:17:54 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
cursor.execute("DROP TABLE postgresql_replica;")
|
2021-02-04 09:33:31 +00:00
|
|
|
postgresql_replica_check_result(result, True)
|
|
|
|
|
|
|
|
|
2021-02-22 12:35:53 +00:00
|
|
|
@pytest.mark.timeout(320)
|
2021-02-04 21:05:43 +00:00
|
|
|
def test_many_replication_messages(started_cluster):
|
2022-03-22 16:39:58 +00:00
|
|
|
conn = get_postgres_conn(
|
|
|
|
ip=started_cluster.postgres_ip,
|
|
|
|
port=started_cluster.postgres_port,
|
|
|
|
database=True,
|
|
|
|
)
|
2021-02-04 21:05:43 +00:00
|
|
|
cursor = conn.cursor()
|
2022-03-22 16:39:58 +00:00
|
|
|
create_postgres_table(cursor, "postgresql_replica")
|
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT number, number from numbers(100000)"
|
|
|
|
)
|
|
|
|
|
|
|
|
instance.query("DROP TABLE IF EXISTS test.postgresql_replica")
|
|
|
|
create_materialized_table(
|
|
|
|
ip=started_cluster.postgres_ip, port=started_cluster.postgres_port
|
|
|
|
)
|
|
|
|
|
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
|
|
|
while int(result) != 100000:
|
2021-02-04 21:05:43 +00:00
|
|
|
time.sleep(0.2)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
2021-02-06 12:28:42 +00:00
|
|
|
print("SYNC OK")
|
2021-02-04 21:05:43 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT number, number from numbers(100000, 100000)"
|
|
|
|
)
|
2021-02-04 21:05:43 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
|
|
|
while int(result) != 200000:
|
2021-02-04 21:05:43 +00:00
|
|
|
time.sleep(1)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
2021-02-06 00:17:54 +00:00
|
|
|
print("INSERT OK")
|
2021-02-04 21:05:43 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT key FROM test.postgresql_replica ORDER BY key;")
|
2021-02-04 21:05:43 +00:00
|
|
|
expected = instance.query("SELECT number from numbers(200000)")
|
2022-03-22 16:39:58 +00:00
|
|
|
assert result == expected
|
2021-02-04 21:05:43 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
cursor.execute("UPDATE postgresql_replica SET value = key + 1 WHERE key < 100000;")
|
2021-02-04 21:05:43 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query(
|
|
|
|
"SELECT key FROM test.postgresql_replica WHERE value = key + 1 ORDER BY key;"
|
|
|
|
)
|
2021-02-04 21:05:43 +00:00
|
|
|
expected = instance.query("SELECT number from numbers(100000)")
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
while result != expected:
|
2021-02-04 21:05:43 +00:00
|
|
|
time.sleep(1)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query(
|
|
|
|
"SELECT key FROM test.postgresql_replica WHERE value = key + 1 ORDER BY key;"
|
|
|
|
)
|
2021-02-06 00:17:54 +00:00
|
|
|
print("UPDATE OK")
|
2021-02-04 21:05:43 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
cursor.execute("DELETE FROM postgresql_replica WHERE key % 2 = 1;")
|
|
|
|
cursor.execute("DELETE FROM postgresql_replica WHERE key != value;")
|
2021-02-04 21:05:43 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query(
|
|
|
|
"SELECT count() FROM (SELECT * FROM test.postgresql_replica);"
|
|
|
|
)
|
|
|
|
while int(result) != 50000:
|
2021-02-04 21:05:43 +00:00
|
|
|
time.sleep(1)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query(
|
|
|
|
"SELECT count() FROM (SELECT * FROM test.postgresql_replica);"
|
|
|
|
)
|
2021-02-06 00:17:54 +00:00
|
|
|
print("DELETE OK")
|
2021-02-04 21:05:43 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
cursor.execute("DROP TABLE postgresql_replica;")
|
2021-02-04 21:05:43 +00:00
|
|
|
|
|
|
|
|
2021-02-22 12:35:53 +00:00
|
|
|
@pytest.mark.timeout(320)
|
2021-02-06 12:28:42 +00:00
|
|
|
def test_connection_loss(started_cluster):
|
2022-03-22 16:39:58 +00:00
|
|
|
conn = get_postgres_conn(
|
|
|
|
ip=started_cluster.postgres_ip,
|
|
|
|
port=started_cluster.postgres_port,
|
|
|
|
database=True,
|
|
|
|
)
|
2021-02-06 00:17:54 +00:00
|
|
|
cursor = conn.cursor()
|
2022-03-22 16:39:58 +00:00
|
|
|
create_postgres_table(cursor, "postgresql_replica")
|
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT number, number from numbers(50)"
|
|
|
|
)
|
2021-02-06 00:17:54 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query("DROP TABLE IF EXISTS test.postgresql_replica")
|
|
|
|
create_materialized_table(
|
|
|
|
ip=started_cluster.postgres_ip, port=started_cluster.postgres_port
|
|
|
|
)
|
2021-02-06 00:17:54 +00:00
|
|
|
|
2021-02-06 12:28:42 +00:00
|
|
|
i = 50
|
|
|
|
while i < 100000:
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT {} + number, number from numbers(10000)".format(
|
|
|
|
i
|
|
|
|
)
|
|
|
|
)
|
2021-02-06 12:28:42 +00:00
|
|
|
i += 10000
|
2021-02-06 00:17:54 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
started_cluster.pause_container("postgres1")
|
2021-02-06 12:28:42 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
2021-02-06 12:28:42 +00:00
|
|
|
print(int(result))
|
|
|
|
time.sleep(6)
|
2021-02-06 00:17:54 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
started_cluster.unpause_container("postgres1")
|
2021-02-06 00:17:54 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
2021-02-06 12:28:42 +00:00
|
|
|
while int(result) < 100050:
|
|
|
|
time.sleep(1)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
2021-02-06 00:17:54 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
cursor.execute("DROP TABLE postgresql_replica;")
|
|
|
|
assert int(result) == 100050
|
2021-02-06 00:17:54 +00:00
|
|
|
|
|
|
|
|
2021-04-08 20:39:56 +00:00
|
|
|
@pytest.mark.timeout(320)
|
|
|
|
def test_clickhouse_restart(started_cluster):
|
2022-06-08 11:13:57 +00:00
|
|
|
pytest.skip("Temporary disabled (FIXME)")
|
2022-03-22 16:39:58 +00:00
|
|
|
conn = get_postgres_conn(
|
|
|
|
ip=started_cluster.postgres_ip,
|
|
|
|
port=started_cluster.postgres_port,
|
|
|
|
database=True,
|
|
|
|
)
|
2021-04-08 20:39:56 +00:00
|
|
|
cursor = conn.cursor()
|
2022-03-22 16:39:58 +00:00
|
|
|
create_postgres_table(cursor, "postgresql_replica")
|
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT number, number from numbers(50)"
|
|
|
|
)
|
2021-04-08 20:39:56 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query("DROP TABLE IF EXISTS test.postgresql_replica")
|
|
|
|
create_materialized_table(
|
|
|
|
ip=started_cluster.postgres_ip, port=started_cluster.postgres_port
|
|
|
|
)
|
2021-04-08 20:39:56 +00:00
|
|
|
|
|
|
|
i = 50
|
|
|
|
while i < 100000:
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT {} + number, number from numbers(10000)".format(
|
|
|
|
i
|
|
|
|
)
|
|
|
|
)
|
2021-04-08 20:39:56 +00:00
|
|
|
i += 10000
|
|
|
|
|
|
|
|
instance.restart_clickhouse()
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
2021-04-08 20:39:56 +00:00
|
|
|
while int(result) < 100050:
|
|
|
|
time.sleep(1)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
2021-04-08 20:39:56 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
cursor.execute("DROP TABLE postgresql_replica;")
|
2021-04-08 20:39:56 +00:00
|
|
|
print(result)
|
2022-03-22 16:39:58 +00:00
|
|
|
assert int(result) == 100050
|
2021-04-08 20:39:56 +00:00
|
|
|
|
|
|
|
|
2021-04-11 17:19:20 +00:00
|
|
|
def test_rename_table(started_cluster):
|
2022-03-22 16:39:58 +00:00
|
|
|
conn = get_postgres_conn(
|
|
|
|
ip=started_cluster.postgres_ip,
|
|
|
|
port=started_cluster.postgres_port,
|
|
|
|
database=True,
|
|
|
|
)
|
2021-04-11 17:19:20 +00:00
|
|
|
cursor = conn.cursor()
|
2022-03-22 16:39:58 +00:00
|
|
|
create_postgres_table(cursor, "postgresql_replica")
|
2021-04-11 17:19:20 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query("DROP TABLE IF EXISTS test.postgresql_replica")
|
|
|
|
create_materialized_table(
|
|
|
|
ip=started_cluster.postgres_ip, port=started_cluster.postgres_port
|
|
|
|
)
|
2021-04-11 17:19:20 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT number, number from numbers(25)"
|
|
|
|
)
|
2021-05-12 22:47:41 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
2021-05-12 22:47:41 +00:00
|
|
|
while int(result) != 25:
|
2021-04-11 17:19:20 +00:00
|
|
|
time.sleep(0.5)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
2021-04-11 17:19:20 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query(
|
|
|
|
"RENAME TABLE test.postgresql_replica TO test.postgresql_replica_renamed"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
int(instance.query("SELECT count() FROM test.postgresql_replica_renamed;"))
|
|
|
|
== 25
|
|
|
|
)
|
2021-05-12 22:47:41 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT number, number from numbers(25, 25)"
|
|
|
|
)
|
2021-04-11 17:19:20 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica_renamed;")
|
2021-04-11 17:19:20 +00:00
|
|
|
while int(result) != 50:
|
|
|
|
time.sleep(0.5)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica_renamed;")
|
2021-04-11 17:19:20 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query(
|
|
|
|
"SELECT * FROM test.postgresql_replica_renamed ORDER BY key;"
|
|
|
|
)
|
2021-05-12 22:47:41 +00:00
|
|
|
postgresql_replica_check_result(result, True)
|
2022-03-22 16:39:58 +00:00
|
|
|
cursor.execute("DROP TABLE postgresql_replica;")
|
|
|
|
instance.query("DROP TABLE IF EXISTS test.postgresql_replica_renamed")
|
2021-04-11 17:19:20 +00:00
|
|
|
|
2021-05-12 22:47:41 +00:00
|
|
|
|
|
|
|
def test_virtual_columns(started_cluster):
|
2022-03-22 16:39:58 +00:00
|
|
|
conn = get_postgres_conn(
|
|
|
|
ip=started_cluster.postgres_ip,
|
|
|
|
port=started_cluster.postgres_port,
|
|
|
|
database=True,
|
|
|
|
)
|
2021-05-12 22:47:41 +00:00
|
|
|
cursor = conn.cursor()
|
2022-03-22 16:39:58 +00:00
|
|
|
create_postgres_table(cursor, "postgresql_replica")
|
2021-05-12 22:47:41 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query("DROP TABLE IF EXISTS test.postgresql_replica")
|
|
|
|
create_materialized_table(
|
|
|
|
ip=started_cluster.postgres_ip, port=started_cluster.postgres_port
|
|
|
|
)
|
2021-05-12 22:47:41 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT number, number from numbers(10)"
|
|
|
|
)
|
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
2021-05-12 22:47:41 +00:00
|
|
|
while int(result) != 10:
|
2021-04-11 17:19:20 +00:00
|
|
|
time.sleep(0.5)
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica;")
|
2021-05-12 22:47:41 +00:00
|
|
|
|
2023-05-04 16:35:18 +00:00
|
|
|
# just check that it works, no check with `expected` because _version is taken as LSN, which will be different each time.
|
2022-03-22 16:39:58 +00:00
|
|
|
result = instance.query(
|
|
|
|
"SELECT key, value, _sign, _version FROM test.postgresql_replica;"
|
|
|
|
)
|
2021-05-12 22:47:41 +00:00
|
|
|
print(result)
|
2022-03-22 16:39:58 +00:00
|
|
|
cursor.execute("DROP TABLE postgresql_replica;")
|
2023-05-03 18:06:46 +00:00
|
|
|
instance.query(f"DROP TABLE test.postgresql_replica SYNC")
|
2021-04-11 17:19:20 +00:00
|
|
|
|
|
|
|
|
2021-07-01 08:20:13 +00:00
|
|
|
def test_abrupt_connection_loss_while_heavy_replication(started_cluster):
|
|
|
|
instance.query("DROP DATABASE IF EXISTS test_database")
|
2022-03-22 16:39:58 +00:00
|
|
|
conn = get_postgres_conn(
|
|
|
|
ip=started_cluster.postgres_ip,
|
|
|
|
port=started_cluster.postgres_port,
|
|
|
|
database=True,
|
|
|
|
)
|
2021-07-01 08:20:13 +00:00
|
|
|
cursor = conn.cursor()
|
2022-04-03 16:39:14 +00:00
|
|
|
table_name = "postgresql_replica"
|
|
|
|
create_postgres_table(cursor, table_name)
|
2021-07-01 08:20:13 +00:00
|
|
|
|
2022-04-03 16:39:14 +00:00
|
|
|
instance.query(f"DROP TABLE IF EXISTS test.{table_name}")
|
2022-03-22 16:39:58 +00:00
|
|
|
create_materialized_table(
|
|
|
|
ip=started_cluster.postgres_ip, port=started_cluster.postgres_port
|
|
|
|
)
|
2021-07-01 08:20:13 +00:00
|
|
|
|
|
|
|
for i in range(len(queries)):
|
2022-04-03 16:39:14 +00:00
|
|
|
query = queries[i].format(table_name)
|
2021-07-01 08:20:13 +00:00
|
|
|
cursor.execute(query)
|
2022-04-03 16:39:14 +00:00
|
|
|
print("query {}".format(query.format(table_name)))
|
2021-07-01 08:20:13 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
started_cluster.pause_container("postgres1")
|
2021-07-01 08:20:13 +00:00
|
|
|
|
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica")
|
2022-03-22 16:39:58 +00:00
|
|
|
print(result) # Just debug
|
2021-07-01 08:20:13 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
started_cluster.unpause_container("postgres1")
|
2021-07-01 08:20:13 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
check_tables_are_synchronized("postgresql_replica")
|
2021-07-01 08:20:13 +00:00
|
|
|
|
|
|
|
result = instance.query("SELECT count() FROM test.postgresql_replica")
|
2022-03-22 16:39:58 +00:00
|
|
|
print(result) # Just debug
|
2023-05-03 18:06:46 +00:00
|
|
|
instance.query(f"DROP TABLE test.postgresql_replica SYNC")
|
2021-07-01 08:20:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_abrupt_server_restart_while_heavy_replication(started_cluster):
|
2022-04-25 18:08:05 +00:00
|
|
|
# FIXME (kssenii) temporary disabled
|
2022-05-04 09:45:36 +00:00
|
|
|
if instance.is_built_with_sanitizer():
|
2022-04-25 14:37:50 +00:00
|
|
|
pytest.skip("Temporary disabled (FIXME)")
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
conn = get_postgres_conn(
|
|
|
|
ip=started_cluster.postgres_ip,
|
|
|
|
port=started_cluster.postgres_port,
|
|
|
|
database=True,
|
|
|
|
)
|
2021-07-01 08:20:13 +00:00
|
|
|
cursor = conn.cursor()
|
2022-04-03 16:39:14 +00:00
|
|
|
table_name = "postgresql_replica_697"
|
|
|
|
create_postgres_table(cursor, table_name)
|
2021-07-01 08:20:13 +00:00
|
|
|
|
2022-04-08 11:50:24 +00:00
|
|
|
instance.query(f"INSERT INTO postgres_database.{table_name} SELECT -1, 1")
|
2023-05-03 18:06:46 +00:00
|
|
|
instance.query(f"DROP TABLE IF EXISTS test.{table_name} SYNC")
|
2022-03-22 16:39:58 +00:00
|
|
|
create_materialized_table(
|
2022-04-08 11:50:24 +00:00
|
|
|
ip=started_cluster.postgres_ip,
|
|
|
|
port=started_cluster.postgres_port,
|
|
|
|
table_name=table_name,
|
2022-03-22 16:39:58 +00:00
|
|
|
)
|
2021-07-01 08:20:13 +00:00
|
|
|
|
2022-04-05 17:01:41 +00:00
|
|
|
n = 1
|
|
|
|
while int(instance.query(f"select count() from test.{table_name}")) != 1:
|
|
|
|
sleep(1)
|
|
|
|
n += 1
|
|
|
|
if n > 10:
|
2022-04-08 11:50:24 +00:00
|
|
|
break
|
2022-04-05 17:01:41 +00:00
|
|
|
|
2021-07-01 08:20:13 +00:00
|
|
|
for query in queries:
|
2022-04-03 16:39:14 +00:00
|
|
|
cursor.execute(query.format(table_name))
|
|
|
|
print("query {}".format(query.format(table_name)))
|
2021-07-01 08:20:13 +00:00
|
|
|
|
|
|
|
instance.restart_clickhouse()
|
|
|
|
|
2022-04-03 16:39:14 +00:00
|
|
|
result = instance.query(f"SELECT count() FROM test.{table_name}")
|
2022-03-22 16:39:58 +00:00
|
|
|
print(result) # Just debug
|
2021-07-01 08:20:13 +00:00
|
|
|
|
2022-04-03 16:39:14 +00:00
|
|
|
check_tables_are_synchronized(table_name)
|
2021-07-01 08:20:13 +00:00
|
|
|
|
2022-04-03 16:39:14 +00:00
|
|
|
result = instance.query(f"SELECT count() FROM test.{table_name}")
|
2022-03-22 16:39:58 +00:00
|
|
|
print(result) # Just debug
|
2023-05-03 18:06:46 +00:00
|
|
|
instance.query(f"DROP TABLE test.{table_name} SYNC")
|
2021-07-01 08:20:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_drop_table_immediately(started_cluster):
|
2022-03-22 16:39:58 +00:00
|
|
|
conn = get_postgres_conn(
|
|
|
|
ip=started_cluster.postgres_ip,
|
|
|
|
port=started_cluster.postgres_port,
|
|
|
|
database=True,
|
|
|
|
)
|
2021-07-01 08:20:13 +00:00
|
|
|
cursor = conn.cursor()
|
2022-03-22 16:39:58 +00:00
|
|
|
create_postgres_table(cursor, "postgresql_replica")
|
|
|
|
instance.query(
|
|
|
|
"INSERT INTO postgres_database.postgresql_replica SELECT number, number from numbers(100000)"
|
|
|
|
)
|
|
|
|
|
|
|
|
instance.query("DROP TABLE IF EXISTS test.postgresql_replica")
|
|
|
|
create_materialized_table(
|
|
|
|
ip=started_cluster.postgres_ip, port=started_cluster.postgres_port
|
|
|
|
)
|
|
|
|
instance.query("DROP TABLE test.postgresql_replica")
|
|
|
|
create_materialized_table(
|
|
|
|
ip=started_cluster.postgres_ip, port=started_cluster.postgres_port
|
|
|
|
)
|
|
|
|
check_tables_are_synchronized("postgresql_replica")
|
2023-05-03 18:06:46 +00:00
|
|
|
instance.query(f"DROP TABLE test.postgresql_replica SYNC")
|
2022-03-22 16:39:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-01-27 21:46:19 +00:00
|
|
|
cluster.start()
|
|
|
|
input("Cluster created, press any key to destroy...")
|
|
|
|
cluster.shutdown()
|