|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import argparse |
| 19 | +import asyncio |
| 20 | +from collections import namedtuple |
| 21 | + |
| 22 | +from apache_iggy import IggyClient, StreamDetails, TopicDetails |
| 23 | +from apache_iggy import SendMessage as Message |
| 24 | +from loguru import logger |
| 25 | + |
| 26 | +STREAM_NAME = "sample-stream" |
| 27 | +TOPIC_NAME = "sample-topic" |
| 28 | +STREAM_ID = 1 |
| 29 | +TOPIC_ID = 1 |
| 30 | +PARTITION_ID = 1 |
| 31 | +BATCHES_LIMIT = 5 |
| 32 | + |
| 33 | +ArgNamespace = namedtuple("ArgNamespace", ["connection_string"]) |
| 34 | + |
| 35 | + |
| 36 | +def parse_args() -> argparse.Namespace: |
| 37 | + parser = argparse.ArgumentParser() |
| 38 | + parser.add_argument( |
| 39 | + "connection_string", |
| 40 | + help=( |
| 41 | + "Connection string for Iggy client, e.g. 'iggy+tcp://iggy:iggy@127.0.0.1:8090'" |
| 42 | + ), |
| 43 | + default="iggy+tcp://iggy:iggy@127.0.0.1:8090", |
| 44 | + type=str, |
| 45 | + ) |
| 46 | + return parser.parse_args() |
| 47 | + |
| 48 | + |
| 49 | +async def main(): |
| 50 | + args: ArgNamespace = parse_args() |
| 51 | + client = IggyClient.from_connection_string(args.connection_string) |
| 52 | + logger.info("Connecting to Iggy") |
| 53 | + await client.connect() |
| 54 | + logger.info("Connected") |
| 55 | + await init_system(client) |
| 56 | + await produce_messages(client) |
| 57 | + |
| 58 | + |
| 59 | +async def init_system(client: IggyClient): |
| 60 | + try: |
| 61 | + logger.info(f"Creating stream with name {STREAM_NAME}...") |
| 62 | + stream: StreamDetails = await client.get_stream(STREAM_NAME) |
| 63 | + if stream is None: |
| 64 | + await client.create_stream(name=STREAM_NAME, stream_id=STREAM_ID) |
| 65 | + logger.info("Stream was created successfully.") |
| 66 | + else: |
| 67 | + logger.warning(f"Stream {stream.name} already exists with ID {stream.id}") |
| 68 | + |
| 69 | + except Exception as error: |
| 70 | + logger.error(f"Error creating stream: {error}") |
| 71 | + logger.exception(error) |
| 72 | + |
| 73 | + try: |
| 74 | + logger.info(f"Creating topic {TOPIC_NAME} in stream {STREAM_NAME}") |
| 75 | + topic: TopicDetails = await client.get_topic(STREAM_NAME, TOPIC_NAME) |
| 76 | + if topic is None: |
| 77 | + await client.create_topic( |
| 78 | + stream=STREAM_NAME, |
| 79 | + partitions_count=1, |
| 80 | + name=TOPIC_NAME, |
| 81 | + replication_factor=1, |
| 82 | + ) |
| 83 | + logger.info("Topic was created successfully.") |
| 84 | + else: |
| 85 | + logger.warning(f"Topic {topic.name} already exists with ID {topic.id}") |
| 86 | + except Exception as error: |
| 87 | + logger.error(f"Error creating topic {error}") |
| 88 | + logger.exception(error) |
| 89 | + |
| 90 | + |
| 91 | +async def produce_messages(client: IggyClient): |
| 92 | + interval = 0.5 # 500 milliseconds in seconds for asyncio.sleep |
| 93 | + logger.info( |
| 94 | + f"Messages will be sent to stream: {STREAM_NAME}, topic: {TOPIC_NAME}, partition: {PARTITION_ID} with interval {interval * 1000} ms." |
| 95 | + ) |
| 96 | + current_id = 0 |
| 97 | + messages_per_batch = 10 |
| 98 | + n_sent_batches = 0 |
| 99 | + while n_sent_batches < BATCHES_LIMIT: |
| 100 | + messages = [] |
| 101 | + for _ in range(messages_per_batch): |
| 102 | + current_id += 1 |
| 103 | + payload = f"message-{current_id}" |
| 104 | + message = Message(payload) |
| 105 | + messages.append(message) |
| 106 | + logger.info( |
| 107 | + f"Attempting to send batch of {messages_per_batch} messages. Batch ID: {current_id // messages_per_batch}" |
| 108 | + ) |
| 109 | + try: |
| 110 | + await client.send_messages( |
| 111 | + stream=STREAM_NAME, |
| 112 | + topic=TOPIC_NAME, |
| 113 | + partitioning=PARTITION_ID, |
| 114 | + messages=messages, |
| 115 | + ) |
| 116 | + n_sent_batches += 1 |
| 117 | + logger.info( |
| 118 | + f"Successfully sent batch of {messages_per_batch} messages. Batch ID: {current_id // messages_per_batch}" |
| 119 | + ) |
| 120 | + except Exception as error: |
| 121 | + logger.error(f"Exception type: {type(error).__name__}, message: {error}") |
| 122 | + logger.exception(error) |
| 123 | + |
| 124 | + await asyncio.sleep(interval) |
| 125 | + logger.info(f"Sent {n_sent_batches} batches of messages, exiting.") |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + asyncio.run(main()) |
0 commit comments