From d1ca202152951162b4ef6e86aa7a8483af839e00 Mon Sep 17 00:00:00 2001 From: hoseinaghaei Date: Thu, 15 Feb 2024 07:51:24 +0330 Subject: [PATCH] fix none replica. --- kafka_server/broker/file/read.py | 13 +++++-------- kafka_server/broker/file/segment.py | 14 +++++--------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/kafka_server/broker/file/read.py b/kafka_server/broker/file/read.py index 603369d..e4bfde4 100644 --- a/kafka_server/broker/file/read.py +++ b/kafka_server/broker/file/read.py @@ -19,17 +19,14 @@ class Read: _instances_lock = threading.Lock() _read_lock = threading.Lock() - _instance = None + _instances = {} def __new__(cls, partition: str, replica: str): - if cls._instance is None: + if f"{partition}-{replica}" not in cls._instances[f"{partition}-{replica}"]: with cls._instances_lock: - if cls._instance is None: - cls._instance = super().__new__(cls) - cls._instance.replica = replica - if replica is not None and cls._instance.replica is None: - cls._instance.segment = Segment(partition, replica) - return cls._instance + cls._instances[f"{partition}-{replica}"] = super().__new__(cls) + + return cls._instances[f"{partition}-{replica}"] def __init__(self, partition: str, replica: str): self.subscribers = None diff --git a/kafka_server/broker/file/segment.py b/kafka_server/broker/file/segment.py index 65ceb2f..075e2cd 100644 --- a/kafka_server/broker/file/segment.py +++ b/kafka_server/broker/file/segment.py @@ -18,16 +18,12 @@ class Segment: def __new__(cls, partition: str, replica: str): with cls._instances_lock: - if partition not in cls._instances: - cls._instances[partition] = super(Segment, cls).__new__(cls) - cls._instances[partition].partition = partition - cls._instances[partition].indexer = Indexer(partition, replica) - cls._instances[partition].replica = replica + if f"{partition}-{replica}" not in cls._instances: + cls._instances[f"{partition}-{replica}"] = super(Segment, cls).__new__(cls) + cls._instances[f"{partition}-{replica}"].partition = partition + cls._instances[f"{partition}-{replica}"].indexer = Indexer(partition, replica) - if replica is not None and cls._instances[partition].replica is None: - cls._instances[partition].indexer = Indexer(partition, replica) - - return cls._instances[partition] + return cls._instances[f"{partition}-{replica}"] def append(self, key: str, value: str): try: