-
Notifications
You must be signed in to change notification settings - Fork 55
replay log on initialize #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sambott
wants to merge
1
commit into
wintoncode:master
Choose a base branch
from
sambott:state_store_replay_log
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
from collections import deque | ||
from typing import Iterator, Tuple | ||
|
||
import pytest | ||
|
||
from winton_kafka_streams.processor.serialization.serdes import IntegerSerde, StringSerde | ||
from winton_kafka_streams.state.in_memory.in_memory_state_store import InMemoryStateStore | ||
from winton_kafka_streams.state.logging.change_logging_state_store import ChangeLoggingStateStore | ||
from winton_kafka_streams.state.logging.store_change_logger import StoreChangeLogger | ||
|
||
|
||
class MockChangeLogger(StoreChangeLogger): | ||
def __init__(self): | ||
super(MockChangeLogger, self).__init__() | ||
self.change_log = deque() | ||
|
||
def log_change(self, key: bytes, value: bytes) -> None: | ||
self.change_log.append((key, value)) | ||
|
||
def __iter__(self) -> Iterator[Tuple[bytes, bytes]]: | ||
return self.change_log.__iter__() | ||
|
||
|
||
def _get_store(): | ||
inner_store = InMemoryStateStore('teststore', StringSerde(), IntegerSerde(), False) | ||
store = ChangeLoggingStateStore('teststore', StringSerde(), IntegerSerde(), False, inner_store) | ||
store._get_change_logger = lambda context: MockChangeLogger() | ||
store.initialize(None, None) | ||
return store | ||
|
||
|
||
def test_change_store_is_dict(): | ||
store = _get_store() | ||
kv_store = store.get_key_value_store() | ||
|
||
kv_store['a'] = 1 | ||
assert kv_store['a'] == 1 | ||
|
||
kv_store['a'] = 2 | ||
assert kv_store['a'] == 2 | ||
|
||
del kv_store['a'] | ||
assert kv_store.get('a') is None | ||
with pytest.raises(KeyError): | ||
_ = kv_store['a'] | ||
|
||
|
||
def test_change_log_is_written_to(): | ||
store = _get_store() | ||
kv_store = store.get_key_value_store() | ||
|
||
kv_store['a'] = 12 | ||
assert len(store.change_logger.change_log) == 1 | ||
assert store.change_logger.change_log[0] == (b'a', b'\x0c\0\0\0') | ||
|
||
del kv_store['a'] | ||
assert len(store.change_logger.change_log) == 2 | ||
assert store.change_logger.change_log[1] == (b'a', b'') | ||
|
||
|
||
def test_can_replay_log(): | ||
store = _get_store() | ||
kv_store = store.get_key_value_store() | ||
|
||
kv_store['a'] = 12 | ||
kv_store['b'] = 123 | ||
del kv_store['a'] | ||
|
||
keys = [] | ||
values = [] | ||
|
||
for k, v in store.change_logger: | ||
keys.append(k) | ||
values.append(v) | ||
|
||
assert keys == [b'a', b'b', b'a'] | ||
assert values == [b'\x0c\0\0\0', b'\x7b\0\0\0', b''] | ||
|
||
|
||
def test_rebuild_state_from_log(): | ||
store = _get_store() | ||
kv_store = store.get_key_value_store() | ||
|
||
kv_store['a'] = 12 | ||
kv_store['b'] = 123 | ||
del kv_store['a'] | ||
kv_store['c'] = 1234 | ||
|
||
log = store.change_logger | ||
|
||
# reattach previous changelog and run initialize() | ||
store = _get_store() | ||
kv_store = store.get_key_value_store() | ||
store._get_change_logger = lambda context: log | ||
store.initialize(None, None) | ||
|
||
with pytest.raises(KeyError): | ||
_ = kv_store['a'] | ||
assert kv_store['b'] == 123 | ||
assert kv_store['c'] == 1234 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,48 @@ | ||
class StoreChangeLogger: | ||
from abc import abstractmethod | ||
from typing import Iterator, Iterable, Tuple | ||
|
||
from confluent_kafka.cimpl import TopicPartition, OFFSET_BEGINNING, KafkaError | ||
|
||
from winton_kafka_streams.processor.serialization.serdes import BytesSerde | ||
from winton_kafka_streams.kafka_client_supplier import KafkaClientSupplier | ||
from winton_kafka_streams.processor._record_collector import RecordCollector | ||
|
||
|
||
class StoreChangeLogger(Iterable[Tuple[bytes, bytes]]): | ||
@abstractmethod | ||
def log_change(self, key: bytes, value: bytes) -> None: | ||
pass | ||
|
||
@abstractmethod | ||
def __iter__(self) -> Iterator[Tuple[bytes, bytes]]: | ||
pass | ||
|
||
|
||
class StoreChangeLoggerImpl(StoreChangeLogger): | ||
def __init__(self, store_name, context) -> None: | ||
self.topic = f'{context.application_id}-{store_name}-changelog' | ||
self.context = context | ||
self.partition = context.task_id.partition | ||
self.record_collector = context.state_record_collector | ||
self.client_supplier = KafkaClientSupplier(self.context.config) | ||
self.record_collector = RecordCollector(self.client_supplier.producer(), BytesSerde(), BytesSerde()) | ||
|
||
def log_change(self, key: bytes, value: bytes) -> None: | ||
if self.record_collector: | ||
self.record_collector.send(self.topic, key, value, self.context.timestamp, partition=self.partition) | ||
|
||
def __iter__(self) -> Iterator[Tuple[bytes, bytes]]: | ||
consumer = self.client_supplier.consumer() | ||
partition = TopicPartition(self.topic, self.partition, OFFSET_BEGINNING) | ||
consumer.assign([partition]) | ||
|
||
class TopicIterator(Iterator[Tuple[bytes, bytes]]): | ||
def __next__(self) -> Tuple[bytes, bytes]: | ||
msg = consumer.poll(1.0) | ||
if msg.error(): | ||
if msg.error().code() == KafkaError._PARTITION_EOF: | ||
raise StopIteration() | ||
if msg is None: | ||
raise StopIteration() | ||
return msg.key(), msg.value() | ||
|
||
return TopicIterator() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Am I missing something_ Where exactly does it replay into the actual store from the changelog?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ChangeLoggingStateStore.initialize?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah. Hooking that up with stream task is another PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A StreamTask calles StateStore.initialize(context, root) as part of its init(). The if the StateStore is a ChangeLoggingStateStore then it will replay the changelog into it's inner_state_store. See change_logging_state_store.py:25