|
| 1 | +import logging |
| 2 | +from .base import ColumnFamilyTestBase |
| 3 | +from typing import Callable |
| 4 | +from ydb.tests.library.common.helpers import plain_or_under_sanitizer |
| 5 | +from ydb.tests.olap.common.thread_helper import TestThread, TestThreads |
| 6 | +from ydb.tests.olap.common.column_table_helper import ColumnTableHelper |
| 7 | + |
| 8 | +logger = logging.getLogger(__name__) |
| 9 | + |
| 10 | + |
| 11 | +class TestAlterCompression(ColumnFamilyTestBase): |
| 12 | + class_name = "alter_compression" |
| 13 | + |
| 14 | + @classmethod |
| 15 | + def setup_class(cls): |
| 16 | + super(TestAlterCompression, cls).setup_class() |
| 17 | + |
| 18 | + def upsert_and_wait_portions(self, table: ColumnTableHelper, number_rows_for_insert: int, count_upsert: int): |
| 19 | + prev_number_rows: int = table.get_row_count() |
| 20 | + for _ in range(count_upsert): |
| 21 | + self.ydb_client.query( |
| 22 | + """ |
| 23 | + $row_count = %i; |
| 24 | + $prev_count = %i; |
| 25 | + $rows= ListMap(ListFromRange(0, $row_count), ($i) -> { |
| 26 | + return <| |
| 27 | + value: $i + $prev_count, |
| 28 | + value1: $i + $prev_count, |
| 29 | + |>; |
| 30 | + }); |
| 31 | + UPSERT INTO `%s` |
| 32 | + SELECT * FROM AS_TABLE($rows); |
| 33 | + """ |
| 34 | + % (number_rows_for_insert, prev_number_rows, table.path) |
| 35 | + ) |
| 36 | + prev_number_rows += number_rows_for_insert |
| 37 | + logger.info( |
| 38 | + f"{prev_number_rows} rows inserted in {table.path}. portions: {table.get_portion_stat_by_tier()}, blobs: {table.get_blob_stat_by_tier()}" |
| 39 | + ) |
| 40 | + assert table.get_row_count() == prev_number_rows |
| 41 | + |
| 42 | + if not self.wait_for( |
| 43 | + lambda: len(table.get_portion_stat_by_tier()) != 0, plain_or_under_sanitizer(70, 140) |
| 44 | + ): |
| 45 | + raise Exception("not all portions have been updated") |
| 46 | + |
| 47 | + if not self.wait_for( |
| 48 | + lambda: table.get_portion_stat_by_tier()['__DEFAULT']['Rows'] == number_rows_for_insert * count_upsert, plain_or_under_sanitizer(70, 140) |
| 49 | + ): |
| 50 | + raise Exception("not all portions have been updated") |
| 51 | + |
| 52 | + def add_family_in_create(self, name: str, settings: str): |
| 53 | + return f"FAMILY {name} ({settings})" |
| 54 | + |
| 55 | + def test_all_supported_compression(self): |
| 56 | + ''' Implements https://github.com/ydb-platform/ydb/issues/13640 ''' |
| 57 | + |
| 58 | + single_upsert_rows_count: int = 10**5 |
| 59 | + upsert_count: int = 10 |
| 60 | + test_name: str = "all_supported_compression" |
| 61 | + test_dir: str = f"{self.ydb_client.database}/{self.class_name}/{test_name}" |
| 62 | + tables_path: list[str] = [ |
| 63 | + f"{test_dir}/off_compression", |
| 64 | + f"{test_dir}/lz4_compression", |
| 65 | + f"{test_dir}/zstd_compression", |
| 66 | + ] |
| 67 | + add_defaut_family: Callable[[str], str] = lambda settings: self.add_family_in_create(name='default', settings=settings) |
| 68 | + tables_family: list[str] = [ |
| 69 | + add_defaut_family('COMPRESSION = "off"'), |
| 70 | + add_defaut_family('COMPRESSION = "lz4"'), |
| 71 | + add_defaut_family('COMPRESSION = "zstd"'), |
| 72 | + ] |
| 73 | + |
| 74 | + for i in range(2, 22): |
| 75 | + tables_path.append(f"{test_dir}/zstd_{i}_compression") |
| 76 | + tables_family.append(add_defaut_family(f'COMPRESSION = "zstd", COMPRESSION_LEVEL = {i}')) |
| 77 | + |
| 78 | + assert len(tables_path) == len(tables_family) |
| 79 | + |
| 80 | + tables: list[ColumnTableHelper] = [] |
| 81 | + for table_path, table_family in zip(tables_path, tables_family): |
| 82 | + self.ydb_client.query( |
| 83 | + f""" |
| 84 | + CREATE TABLE `{table_path}` ( |
| 85 | + value Uint64 NOT NULL, |
| 86 | + value1 Uint64, |
| 87 | + PRIMARY KEY(value), |
| 88 | + {table_family} |
| 89 | + ) |
| 90 | + WITH (STORE = COLUMN) |
| 91 | + """ |
| 92 | + ) |
| 93 | + logger.info(f"Table {table_path} created") |
| 94 | + tables.append(ColumnTableHelper(self.ydb_client, table_path)) |
| 95 | + |
| 96 | + assert len(tables) == len(tables_path) |
| 97 | + |
| 98 | + tasks: TestThreads = TestThreads() |
| 99 | + for table in tables: |
| 100 | + tasks.append(TestThread(target=self.upsert_and_wait_portions, args=[table, single_upsert_rows_count, upsert_count])) |
| 101 | + |
| 102 | + tasks.start_and_wait_all() |
| 103 | + |
| 104 | + volumes_without_compression: tuple[int, int] = tables[0].get_volumes_column("value") |
| 105 | + for table in tables: |
| 106 | + assert table.get_portion_stat_by_tier()['__DEFAULT']['Rows'] == single_upsert_rows_count * upsert_count |
| 107 | + assert upsert_count * single_upsert_rows_count * 8 == volumes_without_compression[0] |
| 108 | + |
| 109 | + for i in range(1, len(tables_path)): |
| 110 | + volumes: tuple[int, int] = tables[i].get_volumes_column("value") |
| 111 | + koef: float = volumes_without_compression[1] / volumes[1] |
| 112 | + logging.info( |
| 113 | + f"compression in `{tables[i].path}` {volumes_without_compression[1]} / {volumes[1]}: {koef}" |
| 114 | + ) |
| 115 | + assert koef > 1 |
| 116 | + |
| 117 | + def test_availability_data(self): |
| 118 | + ''' Implements https://github.com/ydb-platform/ydb/issues/13643 ''' |
| 119 | + |
| 120 | + single_upsert_rows_count: int = 10 ** 2 |
| 121 | + upsert_rows_count: int = 10 |
| 122 | + rows_count: int = 0 |
| 123 | + test_name: str = "availability_data" |
| 124 | + test_dir: str = f"{self.ydb_client.database}/{self.class_name}/{test_name}" |
| 125 | + tables_path: str = f"{test_dir}/test_table" |
| 126 | + |
| 127 | + tables_family: list[str] = [ |
| 128 | + self.add_family_in_create(name = 'default', settings = 'COMPRESSION = "off"'), |
| 129 | + self.add_family_in_create(name = 'family_lz4', settings = 'COMPRESSION = "lz4"'), |
| 130 | + self.add_family_in_create(name = 'family_zstd', settings = 'COMPRESSION = "zstd"'), |
| 131 | + self.add_family_in_create(name = 'family_zstd_10', settings = 'COMPRESSION = "zstd", COMPRESSION_LEVEL = 10'), |
| 132 | + ] |
| 133 | + |
| 134 | + self.ydb_client.query( |
| 135 | + f""" |
| 136 | + CREATE TABLE `{tables_path}` ( |
| 137 | + value Uint64 NOT NULL, |
| 138 | + value1 Uint64, |
| 139 | + PRIMARY KEY(value), |
| 140 | + {','.join(tables_family)} |
| 141 | + ) |
| 142 | + WITH (STORE = COLUMN) |
| 143 | + """ |
| 144 | + ) |
| 145 | + logger.info(f"Table {tables_path} created") |
| 146 | + test_table: ColumnTableHelper = ColumnTableHelper(self.ydb_client, tables_path) |
| 147 | + |
| 148 | + def check_data(table: ColumnTableHelper, rows_cont: int): |
| 149 | + assert table.get_row_count() == rows_cont |
| 150 | + count_row: int = 0 |
| 151 | + result_set = self.ydb_client.query(f"SELECT * FROM `{table.path}` ORDER BY `value`") |
| 152 | + for result in result_set: |
| 153 | + logging.info(result) |
| 154 | + for row in result.rows: |
| 155 | + assert row['value'] == count_row and row['value1'] == count_row |
| 156 | + count_row += 1 |
| 157 | + assert count_row == rows_cont |
| 158 | + |
| 159 | + self.upsert_and_wait_portions(test_table, single_upsert_rows_count, upsert_rows_count) |
| 160 | + rows_count += single_upsert_rows_count * upsert_rows_count |
| 161 | + check_data(table=test_table, rows_cont=rows_count) |
| 162 | + |
| 163 | + self.ydb_client.query(f"ALTER TABLE `{tables_path}` ALTER COLUMN `value` SET FAMILY family_lz4") |
| 164 | + self.upsert_and_wait_portions(test_table, single_upsert_rows_count, upsert_rows_count) |
| 165 | + rows_count += single_upsert_rows_count * upsert_rows_count |
| 166 | + check_data(table=test_table, rows_cont=rows_count) |
| 167 | + |
| 168 | + self.ydb_client.query(f"ALTER TABLE `{tables_path}` ALTER COLUMN `value1` SET FAMILY family_zstd") |
| 169 | + self.upsert_and_wait_portions(test_table, single_upsert_rows_count, upsert_rows_count) |
| 170 | + rows_count += single_upsert_rows_count * upsert_rows_count |
| 171 | + check_data(table=test_table, rows_cont=rows_count) |
| 172 | + |
| 173 | + self.ydb_client.query(f"ALTER TABLE `{tables_path}` ALTER COLUMN `value` SET FAMILY family_zstd_10") |
| 174 | + self.ydb_client.query(f"ALTER TABLE `{tables_path}` ALTER COLUMN `value1` SET FAMILY family_zstd_10") |
| 175 | + self.upsert_and_wait_portions(test_table, single_upsert_rows_count, upsert_rows_count) |
| 176 | + rows_count += single_upsert_rows_count * upsert_rows_count |
| 177 | + check_data(table=test_table, rows_cont=rows_count) |
0 commit comments