2018-07-18 05:22:01 +00:00
|
|
|
import os.path as p
|
|
|
|
import time
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from helpers.cluster import ClickHouseCluster
|
|
|
|
from helpers.test_tools import TSV
|
|
|
|
|
|
|
|
import json
|
2018-07-26 04:36:28 +00:00
|
|
|
import subprocess
|
2018-07-18 05:22:01 +00:00
|
|
|
|
|
|
|
|
2019-01-22 12:18:18 +00:00
|
|
|
# TODO: add test for run-time offset update in CH, if we manually update it on Kafka side.
|
|
|
|
# TODO: add test for mat. view is working.
|
|
|
|
# TODO: add test for SELECT LIMIT is working.
|
|
|
|
# TODO: modify tests to respect `skip_broken_messages` setting.
|
|
|
|
|
2018-07-18 05:22:01 +00:00
|
|
|
cluster = ClickHouseCluster(__file__)
|
2018-08-01 17:23:50 +00:00
|
|
|
instance = cluster.add_instance('instance',
|
|
|
|
main_configs=['configs/kafka.xml'],
|
|
|
|
with_kafka=True)
|
2019-02-08 14:20:25 +00:00
|
|
|
kafka_id = ''
|
2018-08-01 17:23:50 +00:00
|
|
|
|
2018-07-18 05:22:01 +00:00
|
|
|
|
2019-02-07 16:40:16 +00:00
|
|
|
# Helpers
|
2018-07-18 05:22:01 +00:00
|
|
|
|
2019-02-07 16:40:16 +00:00
|
|
|
def check_kafka_is_available():
|
2018-08-01 17:23:50 +00:00
|
|
|
p = subprocess.Popen(('docker',
|
|
|
|
'exec',
|
|
|
|
'-i',
|
2018-08-27 16:15:39 +00:00
|
|
|
kafka_id,
|
2018-08-01 17:23:50 +00:00
|
|
|
'/usr/bin/kafka-broker-api-versions',
|
|
|
|
'--bootstrap-server',
|
|
|
|
'PLAINTEXT://localhost:9092'),
|
|
|
|
stdout=subprocess.PIPE)
|
2019-02-07 16:40:16 +00:00
|
|
|
p.communicate()
|
2018-07-26 04:36:28 +00:00
|
|
|
return p.returncode == 0
|
|
|
|
|
2018-08-01 17:23:50 +00:00
|
|
|
|
2019-02-07 16:40:16 +00:00
|
|
|
def wait_kafka_is_available(max_retries=50):
|
|
|
|
retries = 0
|
|
|
|
while True:
|
|
|
|
if check_kafka_is_available():
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
retries += 1
|
|
|
|
if retries > max_retries:
|
|
|
|
raise "Kafka is not available"
|
|
|
|
print("Waiting for Kafka to start up")
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
|
|
def kafka_produce(topic, messages):
|
2018-08-01 17:23:50 +00:00
|
|
|
p = subprocess.Popen(('docker',
|
|
|
|
'exec',
|
|
|
|
'-i',
|
2018-08-27 16:15:39 +00:00
|
|
|
kafka_id,
|
2018-08-01 17:23:50 +00:00
|
|
|
'/usr/bin/kafka-console-producer',
|
|
|
|
'--broker-list',
|
|
|
|
'localhost:9092',
|
|
|
|
'--topic',
|
2019-02-11 11:54:30 +00:00
|
|
|
topic,
|
|
|
|
'--sync',
|
|
|
|
'--message-send-max-retries',
|
|
|
|
'100'),
|
2018-08-01 17:23:50 +00:00
|
|
|
stdin=subprocess.PIPE)
|
2018-07-26 04:36:28 +00:00
|
|
|
p.communicate(messages)
|
|
|
|
p.stdin.close()
|
2019-02-11 11:54:30 +00:00
|
|
|
print("Produced {} messages".format(len(messages.splitlines())))
|
2018-07-26 04:36:28 +00:00
|
|
|
|
2018-07-18 05:22:01 +00:00
|
|
|
|
2019-02-11 11:54:30 +00:00
|
|
|
# Since everything is async and shaky when receiving messages from Kafka,
|
|
|
|
# we may want to try and check results multiple times in a loop.
|
|
|
|
def kafka_check_result(result, check=False):
|
2019-02-07 16:40:16 +00:00
|
|
|
fpath = p.join(p.dirname(__file__), 'test_kafka_json.reference')
|
|
|
|
with open(fpath) as reference:
|
2019-02-11 11:54:30 +00:00
|
|
|
if check:
|
|
|
|
assert TSV(result) == TSV(reference)
|
|
|
|
else:
|
|
|
|
return TSV(result) == TSV(reference)
|
2019-02-07 16:40:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Fixtures
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def kafka_cluster():
|
|
|
|
try:
|
2019-02-08 14:20:25 +00:00
|
|
|
global kafka_id
|
2019-02-07 16:40:16 +00:00
|
|
|
cluster.start()
|
2019-02-08 14:20:25 +00:00
|
|
|
kafka_id = instance.cluster.kafka_docker_id
|
2019-02-11 11:54:30 +00:00
|
|
|
print("kafka_id is {}".format(kafka_id))
|
2019-02-07 16:40:16 +00:00
|
|
|
instance.query('CREATE DATABASE test')
|
|
|
|
|
|
|
|
yield cluster
|
|
|
|
|
|
|
|
finally:
|
|
|
|
cluster.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def kafka_setup_teardown():
|
|
|
|
instance.query('DROP TABLE IF EXISTS test.kafka')
|
|
|
|
wait_kafka_is_available()
|
2019-02-11 11:54:30 +00:00
|
|
|
print("kafka is available - running test")
|
2019-02-07 16:40:16 +00:00
|
|
|
yield # run test
|
|
|
|
instance.query('DROP TABLE test.kafka')
|
|
|
|
|
|
|
|
|
|
|
|
# Tests
|
|
|
|
|
|
|
|
def test_kafka_settings_old_syntax(kafka_cluster):
|
|
|
|
instance.query('''
|
|
|
|
CREATE TABLE test.kafka (key UInt64, value UInt64)
|
|
|
|
ENGINE = Kafka('kafka1:9092', 'old', 'old', 'JSONEachRow', '\\n');
|
|
|
|
''')
|
|
|
|
|
|
|
|
# Don't insert malformed messages since old settings syntax
|
|
|
|
# doesn't support skipping of broken messages.
|
|
|
|
messages = ''
|
|
|
|
for i in range(50):
|
|
|
|
messages += json.dumps({'key': i, 'value': i}) + '\n'
|
|
|
|
kafka_produce('old', messages)
|
|
|
|
|
2019-02-11 11:54:30 +00:00
|
|
|
result = ''
|
|
|
|
for i in range(50):
|
|
|
|
result += instance.query('SELECT * FROM test.kafka')
|
|
|
|
if kafka_check_result(result):
|
|
|
|
break
|
|
|
|
kafka_check_result(result, True)
|
2019-02-07 16:40:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_kafka_settings_new_syntax(kafka_cluster):
|
|
|
|
instance.query('''
|
|
|
|
CREATE TABLE test.kafka (key UInt64, value UInt64)
|
|
|
|
ENGINE = Kafka
|
|
|
|
SETTINGS
|
|
|
|
kafka_broker_list = 'kafka1:9092',
|
|
|
|
kafka_topic_list = 'new',
|
|
|
|
kafka_group_name = 'new',
|
|
|
|
kafka_format = 'JSONEachRow',
|
|
|
|
kafka_row_delimiter = '\\n',
|
|
|
|
kafka_skip_broken_messages = 1;
|
|
|
|
''')
|
2019-01-22 12:18:18 +00:00
|
|
|
|
2018-07-26 04:36:28 +00:00
|
|
|
messages = ''
|
2019-01-22 12:18:18 +00:00
|
|
|
for i in range(25):
|
2018-07-26 04:36:28 +00:00
|
|
|
messages += json.dumps({'key': i, 'value': i}) + '\n'
|
2019-02-07 16:40:16 +00:00
|
|
|
kafka_produce('new', messages)
|
2019-01-22 12:18:18 +00:00
|
|
|
|
2019-02-07 16:40:16 +00:00
|
|
|
# Insert couple of malformed messages.
|
|
|
|
kafka_produce('new', '}{very_broken_message,\n')
|
|
|
|
kafka_produce('new', '}another{very_broken_message,\n')
|
2019-01-22 12:18:18 +00:00
|
|
|
|
|
|
|
messages = ''
|
|
|
|
for i in range(25, 50):
|
|
|
|
messages += json.dumps({'key': i, 'value': i}) + '\n'
|
2019-02-07 16:40:16 +00:00
|
|
|
kafka_produce('new', messages)
|
2019-01-22 12:18:18 +00:00
|
|
|
|
2019-02-11 11:54:30 +00:00
|
|
|
result = ''
|
|
|
|
for i in range(50):
|
|
|
|
result += instance.query('SELECT * FROM test.kafka')
|
|
|
|
if kafka_check_result(result):
|
|
|
|
break
|
|
|
|
kafka_check_result(result, True)
|
2018-08-27 16:15:39 +00:00
|
|
|
|
2018-08-01 17:23:50 +00:00
|
|
|
|
2019-02-07 16:40:16 +00:00
|
|
|
def test_kafka_csv_with_delimiter(kafka_cluster):
|
2018-08-01 17:23:50 +00:00
|
|
|
instance.query('''
|
|
|
|
CREATE TABLE test.kafka (key UInt64, value UInt64)
|
2019-02-07 16:40:16 +00:00
|
|
|
ENGINE = Kafka
|
|
|
|
SETTINGS
|
|
|
|
kafka_broker_list = 'kafka1:9092',
|
|
|
|
kafka_topic_list = 'csv',
|
|
|
|
kafka_group_name = 'csv',
|
|
|
|
kafka_format = 'CSV',
|
|
|
|
kafka_row_delimiter = '\\n';
|
2018-08-01 17:23:50 +00:00
|
|
|
''')
|
2019-01-22 12:18:18 +00:00
|
|
|
|
2019-02-07 16:40:16 +00:00
|
|
|
messages = ''
|
|
|
|
for i in range(50):
|
|
|
|
messages += '{i}, {i}\n'.format(i=i)
|
|
|
|
kafka_produce('csv', messages)
|
2019-01-22 12:18:18 +00:00
|
|
|
|
2019-02-11 11:54:30 +00:00
|
|
|
result = ''
|
|
|
|
for i in range(50):
|
|
|
|
result += instance.query('SELECT * FROM test.kafka')
|
|
|
|
if kafka_check_result(result):
|
|
|
|
break
|
|
|
|
kafka_check_result(result, True)
|
2018-07-26 14:40:33 +00:00
|
|
|
|
2018-08-01 17:23:50 +00:00
|
|
|
|
2019-02-07 16:40:16 +00:00
|
|
|
def test_kafka_tsv_with_delimiter(kafka_cluster):
|
2018-08-01 17:23:50 +00:00
|
|
|
instance.query('''
|
|
|
|
CREATE TABLE test.kafka (key UInt64, value UInt64)
|
|
|
|
ENGINE = Kafka
|
|
|
|
SETTINGS
|
|
|
|
kafka_broker_list = 'kafka1:9092',
|
2019-02-07 16:40:16 +00:00
|
|
|
kafka_topic_list = 'tsv',
|
|
|
|
kafka_group_name = 'tsv',
|
|
|
|
kafka_format = 'TSV',
|
|
|
|
kafka_row_delimiter = '\\n';
|
2018-08-01 17:23:50 +00:00
|
|
|
''')
|
2019-01-22 12:18:18 +00:00
|
|
|
|
2019-02-07 16:40:16 +00:00
|
|
|
messages = ''
|
|
|
|
for i in range(50):
|
|
|
|
messages += '{i}\t{i}\n'.format(i=i)
|
|
|
|
kafka_produce('tsv', messages)
|
2019-01-22 12:18:18 +00:00
|
|
|
|
2019-02-11 11:54:30 +00:00
|
|
|
result = ''
|
|
|
|
for i in range(50):
|
|
|
|
result += instance.query('SELECT * FROM test.kafka')
|
|
|
|
if kafka_check_result(result):
|
|
|
|
break
|
|
|
|
kafka_check_result(result, True)
|
2018-08-01 17:23:50 +00:00
|
|
|
|
|
|
|
|
2019-02-07 16:40:16 +00:00
|
|
|
def test_kafka_materialized_view(kafka_cluster):
|
2019-01-22 12:18:18 +00:00
|
|
|
instance.query('''
|
|
|
|
DROP TABLE IF EXISTS test.view;
|
|
|
|
DROP TABLE IF EXISTS test.consumer;
|
|
|
|
CREATE TABLE test.kafka (key UInt64, value UInt64)
|
|
|
|
ENGINE = Kafka
|
|
|
|
SETTINGS
|
|
|
|
kafka_broker_list = 'kafka1:9092',
|
|
|
|
kafka_topic_list = 'json',
|
|
|
|
kafka_group_name = 'json',
|
|
|
|
kafka_format = 'JSONEachRow',
|
2019-02-07 16:40:16 +00:00
|
|
|
kafka_row_delimiter = '\\n';
|
2019-01-22 12:18:18 +00:00
|
|
|
CREATE TABLE test.view (key UInt64, value UInt64)
|
|
|
|
ENGINE = MergeTree()
|
|
|
|
ORDER BY key;
|
|
|
|
CREATE MATERIALIZED VIEW test.consumer TO test.view AS
|
|
|
|
SELECT * FROM test.kafka;
|
|
|
|
''')
|
|
|
|
|
2019-02-07 16:40:16 +00:00
|
|
|
messages = ''
|
|
|
|
for i in range(50):
|
|
|
|
messages += json.dumps({'key': i, 'value': i}) + '\n'
|
|
|
|
kafka_produce('json', messages)
|
|
|
|
|
2019-02-11 11:54:30 +00:00
|
|
|
for i in range(20):
|
2019-02-07 16:40:16 +00:00
|
|
|
time.sleep(1)
|
|
|
|
result = instance.query('SELECT * FROM test.view')
|
2019-02-11 11:54:30 +00:00
|
|
|
if kafka_check_result(result):
|
2019-02-07 16:40:16 +00:00
|
|
|
break
|
2019-02-11 11:54:30 +00:00
|
|
|
kafka_check_result(result, True)
|
2019-01-22 12:18:18 +00:00
|
|
|
|
|
|
|
instance.query('''
|
|
|
|
DROP TABLE test.consumer;
|
2019-02-07 16:40:16 +00:00
|
|
|
DROP TABLE test.view;
|
2019-01-22 12:18:18 +00:00
|
|
|
''')
|
|
|
|
|
|
|
|
|
2018-07-26 14:40:33 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
cluster.start()
|
|
|
|
raw_input("Cluster created, press any key to destroy...")
|
|
|
|
cluster.shutdown()
|