|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import pytest |
| 3 | +import random |
| 4 | +import threading |
| 5 | +from ydb.tests.library.compatibility.fixtures import RestartToAnotherVersionFixture |
| 6 | +from ydb.tests.oss.ydb_sdk_import import ydb |
| 7 | + |
| 8 | +TABLE_NAME = "table" |
| 9 | + |
| 10 | + |
| 11 | +class TestStatisticsTLI(RestartToAnotherVersionFixture): |
| 12 | + @pytest.fixture(autouse=True, scope="function") |
| 13 | + def setup(self): |
| 14 | + yield from self.setup_cluster( |
| 15 | + ) |
| 16 | + |
| 17 | + def write_data(self): |
| 18 | + def operation(session): |
| 19 | + for _ in range(100): |
| 20 | + random_key = random.randint(1, 1000) |
| 21 | + session.transaction().execute( |
| 22 | + f""" |
| 23 | + UPSERT INTO {TABLE_NAME} (key, value) VALUES ({random_key}, 'Hello, YDB {random_key}!') |
| 24 | + """, |
| 25 | + commit_tx=True |
| 26 | + ) |
| 27 | + |
| 28 | + driver = ydb.Driver( |
| 29 | + ydb.DriverConfig( |
| 30 | + database='/Root', |
| 31 | + endpoint=self.endpoint |
| 32 | + ) |
| 33 | + ) |
| 34 | + driver.wait() |
| 35 | + |
| 36 | + with ydb.QuerySessionPool(driver) as session_pool: |
| 37 | + session_pool.retry_operation_sync(operation) |
| 38 | + |
| 39 | + driver.stop() |
| 40 | + |
| 41 | + def read_data(self): |
| 42 | + def operation(session): |
| 43 | + for _ in range(100): |
| 44 | + queries = [ |
| 45 | + f"SELECT value FROM {TABLE_NAME} WHERE key > 1 AND key <= 200;", |
| 46 | + f"UPSERT INTO {TABLE_NAME} (key, value) SELECT key, value FROM {TABLE_NAME} WHERE key > 1 AND key <= 200;" |
| 47 | + f"UPSERT INTO {TABLE_NAME} (key, value) SELECT key, value FROM {TABLE_NAME} WHERE key > 200 AND key <= 400;" |
| 48 | + f"SELECT value FROM {TABLE_NAME} WHERE key > 200 AND key <= 400;", |
| 49 | + f"UPSERT INTO {TABLE_NAME} (key, value) SELECT key, value FROM {TABLE_NAME} WHERE key > 200 AND key <= 400;" |
| 50 | + f"UPSERT INTO {TABLE_NAME} (key, value) SELECT key, value FROM {TABLE_NAME} WHERE key > 400 AND key <= 600;" |
| 51 | + f"SELECT value FROM {TABLE_NAME} WHERE key > 400 AND key <= 600;", |
| 52 | + f"UPSERT INTO {TABLE_NAME} (key, value) SELECT key, value FROM {TABLE_NAME} WHERE key > 400 AND key <= 600;" |
| 53 | + f"UPSERT INTO {TABLE_NAME} (key, value) SELECT key, value FROM {TABLE_NAME} WHERE key > 600 AND key <= 800;" |
| 54 | + f"SELECT value FROM {TABLE_NAME} WHERE key > 600 AND key <= 800;", |
| 55 | + f"UPSERT INTO {TABLE_NAME} (key, value) SELECT key, value FROM {TABLE_NAME} WHERE key > 600 AND key <= 800;" |
| 56 | + f"UPSERT INTO {TABLE_NAME} (key, value) SELECT key, value FROM {TABLE_NAME} WHERE key > 800 AND key <= 1000;" |
| 57 | + f"SELECT value FROM {TABLE_NAME} WHERE key > 800 AND key <= 1000;" |
| 58 | + ] |
| 59 | + |
| 60 | + for query in queries: |
| 61 | + session.transaction().execute(query, commit_tx=True) |
| 62 | + |
| 63 | + driver = ydb.Driver( |
| 64 | + ydb.DriverConfig( |
| 65 | + database='/Root', |
| 66 | + endpoint=self.endpoint |
| 67 | + ) |
| 68 | + ) |
| 69 | + driver.wait() |
| 70 | + |
| 71 | + with ydb.QuerySessionPool(driver) as session_pool: |
| 72 | + session_pool.retry_operation_sync(operation) |
| 73 | + |
| 74 | + driver.stop() |
| 75 | + |
| 76 | + def generate_tli(self): |
| 77 | + # Create threads for write and read operations |
| 78 | + threads = [ |
| 79 | + threading.Thread(target=self.write_data), |
| 80 | + threading.Thread(target=self.read_data) |
| 81 | + ] |
| 82 | + |
| 83 | + # Start threads |
| 84 | + for thread in threads: |
| 85 | + thread.start() |
| 86 | + |
| 87 | + # Wait for all threads to complete |
| 88 | + for thread in threads: |
| 89 | + thread.join() |
| 90 | + |
| 91 | + def check_partition_stats(self): |
| 92 | + with ydb.QuerySessionPool(self.driver) as session_pool: |
| 93 | + query = """SELECT * FROM `.sys/partition_stats`;""" |
| 94 | + result_sets = session_pool.execute_with_retries(query) |
| 95 | + |
| 96 | + assert len(result_sets[0].rows) > 0 |
| 97 | + |
| 98 | + def create_table(self): |
| 99 | + with ydb.QuerySessionPool(self.driver) as session_pool: |
| 100 | + query = f""" |
| 101 | + CREATE TABLE {TABLE_NAME} ( |
| 102 | + key Int64 NOT NULL, |
| 103 | + value Utf8 NOT NULL, |
| 104 | + PRIMARY KEY (key) |
| 105 | + ) |
| 106 | + WITH ( |
| 107 | + PARTITION_AT_KEYS=(0, 300, 600, 900) |
| 108 | + ) |
| 109 | + """ |
| 110 | + session_pool.execute_with_retries(query) |
| 111 | + |
| 112 | + def test_statistics_tli(self): |
| 113 | + self.create_table() |
| 114 | + |
| 115 | + self.generate_tli() |
| 116 | + self.check_partition_stats() |
| 117 | + |
| 118 | + self.change_cluster_version() |
| 119 | + |
| 120 | + self.generate_tli() |
| 121 | + self.check_partition_stats() |
| 122 | + |
| 123 | + |
| 124 | +class TestStatisticsFollowers(RestartToAnotherVersionFixture): |
| 125 | + @pytest.fixture(autouse=True, scope="function") |
| 126 | + def setup(self): |
| 127 | + |
| 128 | + yield from self.setup_cluster( |
| 129 | + extra_feature_flags={ |
| 130 | + "enable_follower_stats": True |
| 131 | + } |
| 132 | + ) |
| 133 | + |
| 134 | + def write_data(self): |
| 135 | + def operation(session): |
| 136 | + for _ in range(100): |
| 137 | + random_key = random.randint(1, 1000) |
| 138 | + session.transaction().execute( |
| 139 | + f""" |
| 140 | + UPSERT INTO {TABLE_NAME} (key, value) VALUES ({random_key}, 'Hello, YDB {random_key}!') |
| 141 | + """, |
| 142 | + commit_tx=True |
| 143 | + ) |
| 144 | + |
| 145 | + with ydb.QuerySessionPool(self.driver) as session_pool: |
| 146 | + session_pool.retry_operation_sync(operation) |
| 147 | + |
| 148 | + def check_statistics(self): |
| 149 | + queries = [ |
| 150 | + "SELECT * FROM `.sys/partition_stats`" |
| 151 | + ] |
| 152 | + |
| 153 | + with ydb.QuerySessionPool(self.driver) as session_pool: |
| 154 | + for query in queries: |
| 155 | + result_sets = session_pool.execute_with_retries(query) |
| 156 | + assert len(result_sets[0].rows) > 0 |
| 157 | + |
| 158 | + def create_table(self): |
| 159 | + with ydb.QuerySessionPool(self.driver) as session_pool: |
| 160 | + query = f""" |
| 161 | + CREATE TABLE {TABLE_NAME} ( |
| 162 | + key Int64 NOT NULL, |
| 163 | + value Utf8 NOT NULL, |
| 164 | + PRIMARY KEY (key) |
| 165 | + ) |
| 166 | + WITH ( |
| 167 | + AUTO_PARTITIONING_BY_SIZE = ENABLED, |
| 168 | + AUTO_PARTITIONING_PARTITION_SIZE_MB = 1, |
| 169 | + READ_REPLICAS_SETTINGS = "PER_AZ:1" |
| 170 | + ); |
| 171 | + """ |
| 172 | + session_pool.execute_with_retries(query) |
| 173 | + |
| 174 | + def test_statistics_followers(self): |
| 175 | + self.create_table() |
| 176 | + |
| 177 | + self.write_data() |
| 178 | + self.check_statistics() |
| 179 | + |
| 180 | + self.change_cluster_version() |
| 181 | + |
| 182 | + self.write_data() |
| 183 | + self.check_statistics() |
0 commit comments