|
| 1 | +import yatest.common |
| 2 | +import os |
| 3 | +import time |
| 4 | +import ydb |
| 5 | +import logging |
| 6 | + |
| 7 | +from ydb.tests.library.harness.kikimr_runner import KiKiMR |
| 8 | +from ydb.tests.library.harness.kikimr_config import KikimrConfigGenerator |
| 9 | +from ydb.tests.library.harness.util import LogLevels |
| 10 | + |
| 11 | + |
| 12 | +logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +class YdbClient: |
| 16 | + def __init__(self, endpoint, database): |
| 17 | + self.driver = ydb.Driver(endpoint=endpoint, database=database, oauth=None) |
| 18 | + self.database = database |
| 19 | + self.session_pool = ydb.QuerySessionPool(self.driver) |
| 20 | + |
| 21 | + def stop(self): |
| 22 | + self.session_pool.stop() |
| 23 | + self.driver.stop() |
| 24 | + |
| 25 | + def wait_connection(self, timeout=5): |
| 26 | + self.driver.wait(timeout, fail_fast=True) |
| 27 | + |
| 28 | + def query(self, statement): |
| 29 | + return self.session_pool.execute_with_retries(statement) |
| 30 | + |
| 31 | + |
| 32 | +class ColumnTableHelper: |
| 33 | + def __init__(self, ydb_client: YdbClient, path: str): |
| 34 | + self.ydb_client = ydb_client |
| 35 | + self.path = path |
| 36 | + |
| 37 | + def get_row_count(self) -> int: |
| 38 | + count_row: int = 0 |
| 39 | + result_set = self.ydb_client.query(f"SELECT COUNT(*) AS Rows FROM `{self.path}`") |
| 40 | + for result in result_set: |
| 41 | + for row in result.rows: |
| 42 | + count_row += row["Rows"] |
| 43 | + return count_row |
| 44 | + |
| 45 | + def get_portion_stat_by_tier(self) -> dict[str, dict[str, int]]: |
| 46 | + results = self.ydb_client.query( |
| 47 | + f"select TierName, sum(Rows) as Rows, count(*) as Portions from `{self.path}/.sys/primary_index_portion_stats` group by TierName" |
| 48 | + ) |
| 49 | + return { |
| 50 | + row["TierName"]: {"Rows": row["Rows"], "Portions": row["Portions"]} |
| 51 | + for result_set in results |
| 52 | + for row in result_set.rows |
| 53 | + } |
| 54 | + |
| 55 | + def get_blob_stat_by_tier(self) -> dict[str, (int, int)]: |
| 56 | + stmt = f""" |
| 57 | + select TierName, count(*) as Portions, sum(BlobSize) as BlobSize, sum(BlobCount) as BlobCount from ( |
| 58 | + select TabletId, PortionId, TierName, sum(BlobRangeSize) as BlobSize, count(*) as BlobCount from `{self.path}/.sys/primary_index_stats` group by TabletId, PortionId, TierName |
| 59 | + ) group by TierName |
| 60 | + """ |
| 61 | + results = self.ydb_client.query(stmt) |
| 62 | + return { |
| 63 | + row["TierName"]: {"Portions": row["Portions"], "BlobSize": row["BlobSize"], "BlobCount": row["BlobCount"]} |
| 64 | + for result_set in results |
| 65 | + for row in result_set.rows |
| 66 | + } |
| 67 | + |
| 68 | + def _coollect_volumes_column(self, column_name: str) -> tuple[int, int]: |
| 69 | + query = f'SELECT * FROM `{self.path}/.sys/primary_index_stats` WHERE Activity == 1 AND EntityName = \"{column_name}\"' |
| 70 | + result_set = self.ydb_client.query(query) |
| 71 | + raw_bytes, bytes = 0, 0 |
| 72 | + for result in result_set: |
| 73 | + for rows in result.rows: |
| 74 | + raw_bytes += rows["RawBytes"] |
| 75 | + bytes += rows["BlobRangeSize"] |
| 76 | + return raw_bytes, bytes |
| 77 | + |
| 78 | + def get_volumes_column(self, column_name: str) -> tuple[int, int]: |
| 79 | + pred_raw_bytes, pred_bytes = 0, 0 |
| 80 | + raw_bytes, bytes = self._coollect_volumes_column(column_name) |
| 81 | + while pred_raw_bytes != raw_bytes and pred_bytes != bytes: |
| 82 | + pred_raw_bytes = raw_bytes |
| 83 | + pred_bytes = bytes |
| 84 | + time.sleep(10) |
| 85 | + raw_bytes, bytes = self._coollect_volumes_column(column_name) |
| 86 | + logging.info(f"Table `{self.path}`, volumes `{column_name}` ({raw_bytes}, {bytes})") |
| 87 | + return raw_bytes, bytes |
| 88 | + |
| 89 | + |
| 90 | +class ColumnFamilyTestBase(object): |
| 91 | + @classmethod |
| 92 | + def setup_class(cls): |
| 93 | + cls._setup_ydb() |
| 94 | + |
| 95 | + @classmethod |
| 96 | + def teardown_class(cls): |
| 97 | + cls.ydb_client.stop() |
| 98 | + cls.cluster.stop() |
| 99 | + |
| 100 | + @classmethod |
| 101 | + def _setup_ydb(cls): |
| 102 | + ydb_path = yatest.common.build_path(os.environ.get("YDB_DRIVER_BINARY")) |
| 103 | + logger.info(yatest.common.execute([ydb_path, "-V"], wait=True).stdout.decode("utf-8")) |
| 104 | + config = KikimrConfigGenerator( |
| 105 | + extra_feature_flags={"enable_olap_compression": True}, |
| 106 | + column_shard_config={ |
| 107 | + "lag_for_compaction_before_tierings_ms": 0, |
| 108 | + "compaction_actualization_lag_ms": 0, |
| 109 | + "optimizer_freshness_check_duration_ms": 0, |
| 110 | + "small_portion_detect_size_limit": 0, |
| 111 | + "max_read_staleness_ms": 5000, |
| 112 | + }, |
| 113 | + ) |
| 114 | + cls.cluster = KiKiMR(config) |
| 115 | + cls.cluster.start() |
| 116 | + node = cls.cluster.nodes[1] |
| 117 | + cls.ydb_client = YdbClient(database=f"/{config.domain_name}", endpoint=f"grpc://{node.host}:{node.port}") |
| 118 | + cls.ydb_client.wait_connection() |
| 119 | + |
| 120 | + @staticmethod |
| 121 | + def wait_for(condition_func, timeout_seconds): |
| 122 | + t0 = time.time() |
| 123 | + while time.time() - t0 < timeout_seconds: |
| 124 | + if condition_func(): |
| 125 | + return True |
| 126 | + time.sleep(1) |
| 127 | + return False |
0 commit comments