|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import logging |
| 3 | +import os |
| 4 | +import pytest |
| 5 | +import time |
| 6 | + |
| 7 | +from ydb.tests.library.compatibility.fixtures import MixedClusterFixture, RestartToAnotherVersionFixture, RollingUpgradeAndDowngradeFixture |
| 8 | +from ydb.tests.library.harness.util import LogLevels |
| 9 | +from ydb.tests.library.test_meta import link_test_case |
| 10 | +from ydb.tests.oss.ydb_sdk_import import ydb |
| 11 | +from ydb.tests.tools.datastreams_helpers.data_plane import write_stream, read_stream |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +class StreamingTestBase: |
| 17 | + def setup_cluster(self): |
| 18 | + logger.debug(f"setup_cluster, versions {self.versions}") |
| 19 | + |
| 20 | + if min(self.versions) < (25, 4): |
| 21 | + logger.debug("skip test, only available since 25-4") |
| 22 | + pytest.skip("Only available since 25-4") |
| 23 | + |
| 24 | + os.environ["YDB_TEST_DEFAULT_CHECKPOINTING_PERIOD_MS"] = "200" |
| 25 | + os.environ["YDB_TEST_LEASE_DURATION_SEC"] = "15" |
| 26 | + yield from super().setup_cluster( |
| 27 | + extra_feature_flags={ |
| 28 | + "enable_external_data_sources": True, |
| 29 | + "enable_streaming_queries": True |
| 30 | + }, |
| 31 | + additional_log_configs={ |
| 32 | + 'KQP_COMPUTE': LogLevels.TRACE, |
| 33 | + 'STREAMS_CHECKPOINT_COORDINATOR': LogLevels.TRACE, |
| 34 | + 'STREAMS_STORAGE_SERVICE': LogLevels.TRACE, |
| 35 | + 'FQ_ROW_DISPATCHER': LogLevels.TRACE, |
| 36 | + 'KQP_PROXY': LogLevels.DEBUG, |
| 37 | + 'KQP_EXECUTOR': LogLevels.DEBUG}, |
| 38 | + ) |
| 39 | + |
| 40 | + def create_topics(self): |
| 41 | + logger.debug("create_topics") |
| 42 | + self.input_topic = 'streaming_recipe/input_topic' |
| 43 | + self.output_topic = 'streaming_recipe/output_topic' |
| 44 | + self.consumer_name = 'consumer_name' |
| 45 | + with ydb.QuerySessionPool(self.driver) as session_pool: |
| 46 | + query = f""" |
| 47 | + CREATE TOPIC `{self.input_topic}`; |
| 48 | + CREATE TOPIC `{self.output_topic}` (CONSUMER {self.consumer_name}); |
| 49 | + """ |
| 50 | + session_pool.execute_with_retries(query) |
| 51 | + |
| 52 | + def create_external_data_source(self): |
| 53 | + logger.debug("create_external_data_source") |
| 54 | + endpoint = f"localhost:{self.cluster.nodes[1].port}" |
| 55 | + with ydb.QuerySessionPool(self.driver) as session_pool: |
| 56 | + query = f""" |
| 57 | + CREATE EXTERNAL DATA SOURCE source_name WITH ( |
| 58 | + SOURCE_TYPE="Ydb", |
| 59 | + LOCATION="{endpoint}", |
| 60 | + DATABASE_NAME="{self.database_path}", |
| 61 | + SHARED_READING="false", |
| 62 | + AUTH_METHOD="NONE"); |
| 63 | + """ |
| 64 | + session_pool.execute_with_retries(query) |
| 65 | + |
| 66 | + def create_streaming_query(self): |
| 67 | + logger.debug("create_streaming_query") |
| 68 | + with ydb.QuerySessionPool(self.driver) as session_pool: |
| 69 | + query = f""" |
| 70 | + CREATE STREAMING QUERY `my_queries/query_name` AS DO BEGIN |
| 71 | + $input = ( |
| 72 | + SELECT * FROM |
| 73 | + source_name.`{self.input_topic}` WITH ( |
| 74 | + FORMAT = 'json_each_row', |
| 75 | + SCHEMA (time String NOT NULL, level String NOT NULL, host String NOT NULL) |
| 76 | + ) |
| 77 | + ); |
| 78 | + $filtered = (SELECT * FROM $input WHERE level == 'error'); |
| 79 | +
|
| 80 | + $number_errors = ( |
| 81 | + SELECT host, COUNT(*) AS error_count, CAST(HOP_START() AS String) AS ts |
| 82 | + FROM $filtered |
| 83 | + GROUP BY |
| 84 | + HoppingWindow(CAST(time AS Timestamp), 'PT600S', 'PT600S'), |
| 85 | + host |
| 86 | + ); |
| 87 | +
|
| 88 | + $json = (SELECT ToBytes(Unwrap(Yson::SerializeJson(Yson::From(TableRow())))) |
| 89 | + FROM $number_errors |
| 90 | + ); |
| 91 | +
|
| 92 | + INSERT INTO source_name.`{self.output_topic}` |
| 93 | + SELECT * FROM $json; |
| 94 | + END DO; |
| 95 | +
|
| 96 | + """ |
| 97 | + session_pool.execute_with_retries(query) |
| 98 | + |
| 99 | + def create_simple_streaming_query(self): |
| 100 | + logger.debug("create_simple_streaming_query") |
| 101 | + with ydb.QuerySessionPool(self.driver) as session_pool: |
| 102 | + query = f""" |
| 103 | + CREATE STREAMING QUERY `my_queries/query_name` AS DO BEGIN |
| 104 | + $input = ( |
| 105 | + SELECT |
| 106 | + * |
| 107 | + FROM |
| 108 | + source_name.`{self.input_topic}` WITH ( |
| 109 | + FORMAT = 'json_each_row', |
| 110 | + SCHEMA (time String NOT NULL, level String NOT NULL, host String NOT NULL) |
| 111 | + ) |
| 112 | + ); |
| 113 | +
|
| 114 | + $json = (SELECT ToBytes(Unwrap(Yson::SerializeJson(Yson::From(TableRow())))) |
| 115 | + FROM $input |
| 116 | + ); |
| 117 | +
|
| 118 | + INSERT INTO source_name.`{self.output_topic}` |
| 119 | + SELECT * FROM $json; |
| 120 | + END DO; |
| 121 | +
|
| 122 | + """ |
| 123 | + session_pool.execute_with_retries(query) |
| 124 | + |
| 125 | + def do_write_read(self, input, expected_output): |
| 126 | + logger.debug("do_write_read") |
| 127 | + endpoint = f"localhost:{self.cluster.nodes[1].port}" |
| 128 | + time.sleep(2) |
| 129 | + logger.debug("write data to stream") |
| 130 | + write_stream(path=self.input_topic, data=input, database=self.database_path, endpoint=endpoint) |
| 131 | + logger.debug("read data from stream") |
| 132 | + assert sorted(read_stream( |
| 133 | + path=self.output_topic, |
| 134 | + messages_count=len(expected_output), |
| 135 | + consumer_name=self.consumer_name, |
| 136 | + database=self.database_path, |
| 137 | + endpoint=endpoint)) == sorted(expected_output) |
| 138 | + |
| 139 | + def do_test_part1(self): |
| 140 | + input = [ |
| 141 | + '{"time": "2025-01-01T00:00:00.000000Z", "level": "error", "host": "host-1"}', |
| 142 | + '{"time": "2025-01-01T00:04:00.000000Z", "level": "error", "host": "host-2"}', |
| 143 | + '{"time": "2025-01-01T00:08:00.000000Z", "level": "error", "host": "host-1"}', |
| 144 | + '{"time": "2025-01-01T00:12:00.000000Z", "level": "error", "host": "host-2"}', |
| 145 | + '{"time": "2025-01-01T00:12:00.000000Z", "level": "error", "host": "host-1"}'] |
| 146 | + expected_data = sorted([ |
| 147 | + '{"error_count":1,"host":"host-2","ts":"2025-01-01T00:00:00Z"}', |
| 148 | + '{"error_count":2,"host":"host-1","ts":"2025-01-01T00:00:00Z"}']) |
| 149 | + self.do_write_read(input, expected_data) |
| 150 | + |
| 151 | + def do_test_part2(self): |
| 152 | + input = [ |
| 153 | + '{"time": "2025-01-01T00:15:00.000000Z", "level": "error", "host": "host-2"}', |
| 154 | + '{"time": "2025-01-01T00:22:00.000000Z", "level": "error", "host": "host-1"}', |
| 155 | + '{"time": "2025-01-01T00:22:00.000000Z", "level": "error", "host": "host-2"}'] |
| 156 | + expected_data = sorted([ |
| 157 | + '{"error_count":2,"host":"host-2","ts":"2025-01-01T00:10:00Z"}', |
| 158 | + '{"error_count":1,"host":"host-1","ts":"2025-01-01T00:10:00Z"}']) |
| 159 | + self.do_write_read(input, expected_data) |
| 160 | + |
| 161 | + |
| 162 | +class TestStreamingMixedCluster(StreamingTestBase, MixedClusterFixture): |
| 163 | + @pytest.fixture(autouse=True, scope="function") |
| 164 | + def setup(self): |
| 165 | + yield from self.setup_cluster() |
| 166 | + |
| 167 | + @link_test_case("#27924") |
| 168 | + def test_mixed_cluster(self): |
| 169 | + self.create_topics() |
| 170 | + self.create_external_data_source() |
| 171 | + self.create_streaming_query() |
| 172 | + self.do_test_part1() |
| 173 | + self.do_test_part2() |
| 174 | + |
| 175 | + |
| 176 | +class TestStreamingRestartToAnotherVersion(StreamingTestBase, RestartToAnotherVersionFixture): |
| 177 | + @pytest.fixture(autouse=True, scope="function") |
| 178 | + def setup(self): |
| 179 | + yield from self.setup_cluster() |
| 180 | + |
| 181 | + @link_test_case("#27924") |
| 182 | + def test_restart_to_another_version(self): |
| 183 | + self.create_topics() |
| 184 | + self.create_external_data_source() |
| 185 | + self.create_streaming_query() |
| 186 | + self.do_test_part1() |
| 187 | + self.change_cluster_version() |
| 188 | + self.do_test_part2() |
| 189 | + |
| 190 | + |
| 191 | +class TestStreamingRollingUpgradeAndDowngrade(StreamingTestBase, RollingUpgradeAndDowngradeFixture): |
| 192 | + @pytest.fixture(autouse=True, scope="function") |
| 193 | + def setup(self): |
| 194 | + yield from self.setup_cluster() |
| 195 | + |
| 196 | + @link_test_case("#27924") |
| 197 | + def test_rolling_upgrage(self): |
| 198 | + self.create_topics() |
| 199 | + self.create_external_data_source() |
| 200 | + self.create_simple_streaming_query() |
| 201 | + |
| 202 | + for _ in self.roll(): # every iteration is a step in rolling upgrade process |
| 203 | + # |
| 204 | + # 2. check written data is correct during rolling upgrade |
| 205 | + # |
| 206 | + input = ['{"time": "2025-01-01T00:15:00.000000Z", "level": "error", "host": "host-2"}'] |
| 207 | + expected_data = ['{"host":"host-2","level":"error","time":"2025-01-01T00:15:00.000000Z"}'] |
| 208 | + self.do_write_read(input, expected_data) |
0 commit comments