|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import ydb |
| 3 | + |
| 4 | +import time |
| 5 | +import unittest |
| 6 | +import uuid |
| 7 | + |
| 8 | + |
| 9 | +class Workload(unittest.TestCase): |
| 10 | + def __init__(self, endpoint, database, duration, mode): |
| 11 | + self.database = database |
| 12 | + self.endpoint = endpoint |
| 13 | + self.driver = ydb.Driver(ydb.DriverConfig(endpoint, database)) |
| 14 | + self.pool = ydb.QuerySessionPool(self.driver) |
| 15 | + self.duration = duration |
| 16 | + self.mode = mode |
| 17 | + self.id = f"{uuid.uuid1()}".replace("-", "_"); |
| 18 | + self.table_name = f"transfer_target_table_{mode}_{self.id}" |
| 19 | + self.topic_name = f"transfer_source_topic_{mode}_{self.id}" |
| 20 | + self.transfer_name = f"transfer_{mode}_{self.id}" |
| 21 | + |
| 22 | + def create_table(self): |
| 23 | + self.pool.execute_with_retries( |
| 24 | + f""" |
| 25 | + CREATE TABLE {self.table_name} ( |
| 26 | + partition Uint32 NOT NULL, |
| 27 | + offset Uint64 NOT NULL, |
| 28 | + message Utf8, |
| 29 | + PRIMARY KEY (partition, offset) |
| 30 | + ) WITH ( |
| 31 | + STORE = {self.mode} |
| 32 | + ); |
| 33 | + """ |
| 34 | + ) |
| 35 | + |
| 36 | + def create_topic(self): |
| 37 | + self.pool.execute_with_retries( |
| 38 | + f"CREATE TOPIC {self.topic_name};" |
| 39 | + ) |
| 40 | + |
| 41 | + def create_transfer(self): |
| 42 | + lmb = ''' |
| 43 | + $l = ($x) -> { |
| 44 | + return [ |
| 45 | + <| |
| 46 | + partition:CAST($x._partition AS Uint32), |
| 47 | + offset:CAST($x._offset AS Uint64), |
| 48 | + message:CAST($x._data AS Utf8) |
| 49 | + |> |
| 50 | + ]; |
| 51 | + }; |
| 52 | + ''' |
| 53 | + |
| 54 | + self.pool.execute_with_retries( |
| 55 | + f""" |
| 56 | + {lmb} |
| 57 | +
|
| 58 | + CREATE TRANSFER {self.transfer_name} |
| 59 | + FROM {self.topic_name} TO {self.table_name} USING $l |
| 60 | + WITH ( |
| 61 | + CONNECTION_STRING = '{self.endpoint}/?database={self.database}', |
| 62 | + FLUSH_INTERVAL = Interval('PT1S'), |
| 63 | + BATCH_SIZE_BYTES = 8388608 |
| 64 | + ); |
| 65 | + """ |
| 66 | + ) |
| 67 | + |
| 68 | + def write_to_topic(self): |
| 69 | + finished_at = time.time() + self.duration |
| 70 | + self.message_count = 0 |
| 71 | + |
| 72 | + with self.driver.topic_client.writer(self.topic_name, producer_id="producer-id") as writer: |
| 73 | + while time.time() < finished_at: |
| 74 | + writer.write(ydb.TopicWriterMessage(f"message-{time.time()}")) |
| 75 | + self.message_count += 1 |
| 76 | + |
| 77 | + def wait_transfer_finished(self): |
| 78 | + iterations = 30 |
| 79 | + |
| 80 | + last_offset = -1 |
| 81 | + |
| 82 | + for i in range(iterations): |
| 83 | + time.sleep(1) |
| 84 | + |
| 85 | + rss = self.pool.execute_with_retries( |
| 86 | + f""" |
| 87 | + SELECT MAX(offset) AS last_offset |
| 88 | + FROM {self.table_name}; |
| 89 | + """ |
| 90 | + ) |
| 91 | + rs = rss[0] |
| 92 | + last_offset = rs.rows[0].last_offset |
| 93 | + |
| 94 | + if last_offset + 1 == self.message_count: |
| 95 | + return |
| 96 | + |
| 97 | + raise Exception(f"Transfer still work after {iterations} seconds. Last offset is {last_offset}") |
| 98 | + |
| 99 | + def loop(self): |
| 100 | + self.create_table() |
| 101 | + self.create_topic() |
| 102 | + self.create_transfer() |
| 103 | + |
| 104 | + self.write_to_topic() |
| 105 | + |
| 106 | + self.wait_transfer_finished() |
| 107 | + |
| 108 | + def __enter__(self): |
| 109 | + return self |
| 110 | + |
| 111 | + def __exit__(self, exc_type, exc_val, exc_tb): |
| 112 | + self.pool.stop() |
| 113 | + self.driver.stop() |
0 commit comments