-
Notifications
You must be signed in to change notification settings - Fork 30
chore(refactor): refactor partition generator to take any stream slicer #39
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
Merged
maxi297
merged 17 commits into
main
from
issue-10552/introduce-concurrent-stream-slicer
Nov 14, 2024
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4aaf1e7
Remove Partition.close
maxi297 35290eb
[ISSUE #10552] move stream slicer concept in concurrent CDK
maxi297 ddd299e
One retriever per thread
maxi297 a924706
ruff format/lint
maxi297 640e7e6
Merge branch 'main' into issue-10552/remove-partition-close-method
aaronsteers a330425
Merge branch 'issue-10552/remove-partition-close-method' into issue-1…
aaronsteers c28d1c1
Auto-fix lint and format issues
0bbfc52
Merge branch 'issue-10552/remove-partition-close-method' into issue-1…
aaronsteers 0e17ad3
Add return type
maxi297 1ecc20d
Add return type
maxi297 6f6ef92
add comments and tests
maxi297 d9bce25
format
maxi297 d060ff0
Merge branch 'main' into issue-10552/introduce-concurrent-stream-slicer
maxi297 ed78fe9
Update airbyte_cdk/sources/declarative/stream_slicers/declarative_par…
maxi297 eb00f8b
Merge branch 'main' into issue-10552/introduce-concurrent-stream-slicer
maxi297 77555f6
improve slice hasher
maxi297 54a99ab
fix tests for slice
maxi297 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
85 changes: 85 additions & 0 deletions
85
airbyte_cdk/sources/declarative/stream_slicers/declarative_partition_generator.py
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,85 @@ | ||
# Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
maxi297 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
from typing import Iterable, Optional, Mapping, Any, Callable | ||
|
||
from airbyte_cdk.sources.declarative.retrievers import Retriever | ||
from airbyte_cdk.sources.message import MessageRepository | ||
from airbyte_cdk.sources.streams.concurrent.partitions.partition import Partition | ||
from airbyte_cdk.sources.streams.concurrent.partitions.partition_generator import PartitionGenerator | ||
from airbyte_cdk.sources.streams.concurrent.partitions.record import Record | ||
from airbyte_cdk.sources.streams.concurrent.partitions.stream_slicer import StreamSlicer | ||
from airbyte_cdk.sources.types import StreamSlice | ||
from airbyte_cdk.utils.slice_hasher import SliceHasher | ||
|
||
|
||
class DeclarativePartitionFactory: | ||
def __init__( | ||
self, | ||
stream_name: str, | ||
json_schema: Mapping[str, Any], | ||
retriever_factory: Callable[[], Retriever], | ||
message_repository: MessageRepository, | ||
) -> None: | ||
""" | ||
The DeclarativePartitionFactory takes a retriever_factory and not a retriever directly. The reason is that our components are not | ||
thread safe and classes like `DefaultPaginator` may not work because multiple threads can access and modify a shared field across each other. | ||
In order to avoid these problems, we will create one retriever per thread which should make the processing thread-safe. | ||
""" | ||
self._stream_name = stream_name | ||
self._json_schema = json_schema | ||
self._retriever_factory = retriever_factory | ||
self._message_repository = message_repository | ||
|
||
def create(self, stream_slice: StreamSlice) -> Partition: | ||
return DeclarativePartition( | ||
self._stream_name, | ||
self._json_schema, | ||
self._retriever_factory(), | ||
self._message_repository, | ||
stream_slice, | ||
) | ||
|
||
|
||
class DeclarativePartition(Partition): | ||
def __init__( | ||
self, | ||
stream_name: str, | ||
json_schema: Mapping[str, Any], | ||
retriever: Retriever, | ||
message_repository: MessageRepository, | ||
stream_slice: StreamSlice, | ||
): | ||
self._stream_name = stream_name | ||
self._json_schema = json_schema | ||
self._retriever = retriever | ||
self._message_repository = message_repository | ||
maxi297 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self._stream_slice = stream_slice | ||
self._hash = SliceHasher.hash(self._stream_name, self._stream_slice) | ||
|
||
def read(self) -> Iterable[Record]: | ||
for stream_data in self._retriever.read_records(self._json_schema, self._stream_slice): | ||
if isinstance(stream_data, Mapping): | ||
yield Record(stream_data, self) | ||
else: | ||
self._message_repository.emit_message(stream_data) | ||
|
||
def to_slice(self) -> Optional[Mapping[str, Any]]: | ||
return self._stream_slice | ||
|
||
def stream_name(self) -> str: | ||
return self._stream_name | ||
|
||
def __hash__(self) -> int: | ||
return self._hash | ||
|
||
|
||
class StreamSlicerPartitionGenerator(PartitionGenerator): | ||
def __init__( | ||
self, partition_factory: DeclarativePartitionFactory, stream_slicer: StreamSlicer | ||
) -> None: | ||
self._partition_factory = partition_factory | ||
self._stream_slicer = stream_slicer | ||
|
||
def generate(self) -> Iterable[Partition]: | ||
for stream_slice in self._stream_slicer.stream_slices(): | ||
yield self._partition_factory.create(stream_slice) |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.