|
| 1 | +import time |
| 2 | +import logging |
| 3 | +from .base import TllTieringTestBase, ColumnTableHelper |
| 4 | + |
| 5 | +logger = logging.getLogger(__name__) |
| 6 | + |
| 7 | + |
| 8 | +class TestDeleteS3Ttl(TllTieringTestBase): |
| 9 | + ''' Implements https://github.com/ydb-platform/ydb/issues/13467 ''' |
| 10 | + |
| 11 | + test_name = "delete_s3_ttl" |
| 12 | + row_count = 10 ** 7 |
| 13 | + single_upsert_row_count = 10 ** 6 |
| 14 | + cold_bucket = "cold" |
| 15 | + frozen_bucket = "frozen" |
| 16 | + days_to_cool = 1000 |
| 17 | + days_to_froze = 3000 |
| 18 | + |
| 19 | + @classmethod |
| 20 | + def setup_class(cls): |
| 21 | + super(TestDeleteS3Ttl, cls).setup_class() |
| 22 | + cls.s3_client.create_bucket(cls.cold_bucket) |
| 23 | + cls.s3_client.create_bucket(cls.frozen_bucket) |
| 24 | + |
| 25 | + def get_row_count_by_date(self, table_path: str, past_days: int) -> int: |
| 26 | + return self.ydb_client.query(f"SELECT count(*) as Rows from `{table_path}` WHERE ts < CurrentUtcTimestamp() - DateTime::IntervalFromDays({past_days})")[0].rows[0]["Rows"] |
| 27 | + |
| 28 | + def test(self): |
| 29 | + test_dir = f"{self.ydb_client.database}/{self.test_name}" |
| 30 | + table_path = f"{test_dir}/table" |
| 31 | + secret_prefix = self.test_name |
| 32 | + access_key_id_secret_name = f"{secret_prefix}_key_id" |
| 33 | + access_key_secret_secret_name = f"{secret_prefix}_key_secret" |
| 34 | + cold_eds_path = f"{test_dir}/{self.cold_bucket}" |
| 35 | + frozen_eds_path = f"{test_dir}/{self.frozen_bucket}" |
| 36 | + |
| 37 | + # Expect empty buckets to avoid unintentional data deletion/modification |
| 38 | + if self.s3_client.get_bucket_stat(self.cold_bucket) != (0, 0): |
| 39 | + raise Exception("Bucket for cold data is not empty") |
| 40 | + if self.s3_client.get_bucket_stat(self.frozen_bucket) != (0, 0): |
| 41 | + raise Exception("Bucket for frozen data is not empty") |
| 42 | + |
| 43 | + self.ydb_client.query(f""" |
| 44 | + CREATE TABLE `{table_path}` ( |
| 45 | + ts Timestamp NOT NULL, |
| 46 | + s String, |
| 47 | + val Uint64, |
| 48 | + PRIMARY KEY(ts), |
| 49 | + ) |
| 50 | + WITH (STORE = COLUMN) |
| 51 | + """ |
| 52 | + ) |
| 53 | + |
| 54 | + logger.info(f"Table {table_path} created") |
| 55 | + |
| 56 | + self.ydb_client.query(f"CREATE OBJECT {access_key_id_secret_name} (TYPE SECRET) WITH value='{self.s3_client.key_id}'") |
| 57 | + self.ydb_client.query(f"CREATE OBJECT {access_key_secret_secret_name} (TYPE SECRET) WITH value='{self.s3_client.key_secret}'") |
| 58 | + |
| 59 | + self.ydb_client.query(f""" |
| 60 | + CREATE EXTERNAL DATA SOURCE `{cold_eds_path}` WITH ( |
| 61 | + SOURCE_TYPE="ObjectStorage", |
| 62 | + LOCATION="{self.s3_client.endpoint}/{self.cold_bucket}", |
| 63 | + AUTH_METHOD="AWS", |
| 64 | + AWS_ACCESS_KEY_ID_SECRET_NAME="{access_key_id_secret_name}", |
| 65 | + AWS_SECRET_ACCESS_KEY_SECRET_NAME="{access_key_secret_secret_name}", |
| 66 | + AWS_REGION="{self.s3_client.region}" |
| 67 | + ) |
| 68 | + """) |
| 69 | + |
| 70 | + self.ydb_client.query(f""" |
| 71 | + CREATE EXTERNAL DATA SOURCE `{frozen_eds_path}` WITH ( |
| 72 | + SOURCE_TYPE="ObjectStorage", |
| 73 | + LOCATION="{self.s3_client.endpoint}/{self.frozen_bucket}", |
| 74 | + AUTH_METHOD="AWS", |
| 75 | + AWS_ACCESS_KEY_ID_SECRET_NAME="{access_key_id_secret_name}", |
| 76 | + AWS_SECRET_ACCESS_KEY_SECRET_NAME="{access_key_secret_secret_name}", |
| 77 | + AWS_REGION="{self.s3_client.region}" |
| 78 | + ) |
| 79 | + """) |
| 80 | + table = ColumnTableHelper(self.ydb_client, table_path) |
| 81 | + |
| 82 | + cur_rows = 0 |
| 83 | + while cur_rows < self.row_count: |
| 84 | + self.ydb_client.query(""" |
| 85 | + $row_count = %i; |
| 86 | + $from_us = CAST(Timestamp('2010-01-01T00:00:00.000000Z') as Uint64); |
| 87 | + $to_us = CAST(Timestamp('2030-01-01T00:00:00.000000Z') as Uint64); |
| 88 | + $dt = $to_us - $from_us; |
| 89 | + $k = ((1ul << 64) - 1) / CAST($dt - 1 as Double); |
| 90 | + $rows= ListMap(ListFromRange(0, $row_count), ($i)->{ |
| 91 | + $us = CAST(RandomNumber($i) / $k as Uint64) + $from_us; |
| 92 | + $ts = Unwrap(CAST($us as Timestamp)); |
| 93 | + return <| |
| 94 | + ts: $ts, |
| 95 | + s: 'some date:' || CAST($ts as String), |
| 96 | + val: $us |
| 97 | + |>; |
| 98 | + }); |
| 99 | + upsert into `%s` |
| 100 | + select * FROM AS_TABLE($rows); |
| 101 | + """ % (min(self.row_count - cur_rows, self.single_upsert_row_count), table_path)) |
| 102 | + cur_rows = table.get_row_count() |
| 103 | + logger.info(f"{cur_rows} rows inserted in total, portions: {table.get_portion_stat_by_tier()}, blobs: {table.get_blob_stat_by_tier()}") |
| 104 | + |
| 105 | + logger.info(f"Rows older than {self.days_to_cool} days: {self.get_row_count_by_date(table_path, self.days_to_cool)}") |
| 106 | + logger.info(f"Rows older than {self.days_to_froze} days: {self.get_row_count_by_date(table_path, self.days_to_froze)}") |
| 107 | + |
| 108 | + def portions_actualized_in_sys(): |
| 109 | + portions = table.get_portion_stat_by_tier() |
| 110 | + logger.info(f"portions: {portions}, blobs: {table.get_blob_stat_by_tier()}") |
| 111 | + if len(portions) != 1 or "__DEFAULT" not in portions: |
| 112 | + raise Exception("Data not in __DEFAULT teir") |
| 113 | + return self.row_count <= portions["__DEFAULT"]["Rows"] |
| 114 | + |
| 115 | + if not self.wait_for(lambda: portions_actualized_in_sys(), 120): |
| 116 | + raise Exception(".sys reports incorrect data portions") |
| 117 | + |
| 118 | + t0 = time.time() |
| 119 | + stmt = f""" |
| 120 | + ALTER TABLE `{table_path}` SET (TTL = |
| 121 | + Interval("P{self.days_to_cool}D") TO EXTERNAL DATA SOURCE `{cold_eds_path}`, |
| 122 | + Interval("P{self.days_to_froze}D") TO EXTERNAL DATA SOURCE `{frozen_eds_path}` |
| 123 | + ON ts |
| 124 | + ) |
| 125 | + """ |
| 126 | + logger.info(stmt) |
| 127 | + self.ydb_client.query(stmt) |
| 128 | + logger.info(f"TTL set in {time.time() - t0} seconds") |
| 129 | + |
| 130 | + def data_distributes_across_tiers(): |
| 131 | + cold_bucket_stat = self.s3_client.get_bucket_stat(self.cold_bucket) |
| 132 | + frozen_bucket_stat = self.s3_client.get_bucket_stat(self.frozen_bucket) |
| 133 | + logger.info(f"portions: {table.get_portion_stat_by_tier()}, blobs: {table.get_blob_stat_by_tier()}, cold bucket stat: {cold_bucket_stat}, frozen bucket stat: {frozen_bucket_stat}") |
| 134 | + # TODO FIXME |
| 135 | + # We can not expect proper distribution of data across tiers due to https://github.com/ydb-platform/ydb/issues/13525 |
| 136 | + # So we wait until some data appears in any bucket |
| 137 | + return cold_bucket_stat[0] != 0 or frozen_bucket_stat[0] != 0 |
| 138 | + |
| 139 | + if not self.wait_for(lambda: data_distributes_across_tiers(), 600): |
| 140 | + raise Exception("Data eviction has not been started") |
| 141 | + |
| 142 | + t0 = time.time() |
| 143 | + stmt = f""" |
| 144 | + ALTER TABLE `{table_path}` SET (TTL = |
| 145 | + Interval("P{self.days_to_cool}D") |
| 146 | + ON ts |
| 147 | + ) |
| 148 | + """ |
| 149 | + logger.info(stmt) |
| 150 | + self.ydb_client.query(stmt) |
| 151 | + logger.info(f"TTL set in {time.time() - t0} seconds") |
| 152 | + |
| 153 | + # TODO FIXME after https://github.com/ydb-platform/ydb/issues/13523 |
| 154 | + def data_deleted_from_buckets(): |
| 155 | + cold_bucket_stat = self.s3_client.get_bucket_stat(self.cold_bucket) |
| 156 | + frozen_bucket_stat = self.s3_client.get_bucket_stat(self.frozen_bucket) |
| 157 | + logger.info( |
| 158 | + f"portions: {table.get_portion_stat_by_tier()}, blobs: {table.get_blob_stat_by_tier()}, cold bucket stat: {cold_bucket_stat}, frozen bucket stat: {frozen_bucket_stat}") |
| 159 | + return cold_bucket_stat[0] == 0 and frozen_bucket_stat[0] == 0 |
| 160 | + |
| 161 | + if not self.wait_for(lambda: data_deleted_from_buckets(), 120): |
| 162 | + # raise Exception("not all data deleted") TODO FIXME after https://github.com/ydb-platform/ydb/issues/13535 |
| 163 | + pass |
0 commit comments