diff --git a/kafka_server/broker/file/read.py b/kafka_server/broker/file/read.py index fbc87fc..603369d 100644 --- a/kafka_server/broker/file/read.py +++ b/kafka_server/broker/file/read.py @@ -22,11 +22,13 @@ class Read: _instance = None def __new__(cls, partition: str, replica: str): - if cls._instance is None or (cls._instance.replica is None and replica is not None): + if cls._instance is None: 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 def __init__(self, partition: str, replica: str): diff --git a/kafka_server/broker/file/segment.py b/kafka_server/broker/file/segment.py index 1b1bec9..65ceb2f 100644 --- a/kafka_server/broker/file/segment.py +++ b/kafka_server/broker/file/segment.py @@ -18,11 +18,15 @@ class Segment: def __new__(cls, partition: str, replica: str): with cls._instances_lock: - if partition not in cls._instances or (cls._instances[partition].replica is None and replica is not None): + 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 replica is not None and cls._instances[partition].replica is None: + cls._instances[partition].indexer = Indexer(partition, replica) + return cls._instances[partition] def append(self, key: str, value: str):