-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
🐛 CDK: fix bug with limit parameter for incremental stream #5833
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b6578ae
CDK: fix bug with limit parameter for incremental stream
a04fcd8
Fix reviewer comment
16d1233
Fix reviewer comment
bc9b01d
Fix reviewer comment
50c00b0
Fix review comments
avida 21097c3
Fix review comments
18f4830
fix review comments
avida 4aa578c
fix review comment
avida 1713689
bumped version
avida c7a3867
Merge remote-tracking branch 'origin/master' into drezchykov/5832-cdk…
avida 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 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 |
---|---|---|
|
@@ -137,29 +137,40 @@ def _read_stream( | |
|
||
use_incremental = configured_stream.sync_mode == SyncMode.incremental and stream_instance.supports_incremental | ||
if use_incremental: | ||
record_iterator = self._read_incremental(logger, stream_instance, configured_stream, connector_state) | ||
record_iterator = self._read_incremental(logger, stream_instance, configured_stream, connector_state, internal_config) | ||
else: | ||
record_iterator = self._read_full_refresh(stream_instance, configured_stream) | ||
record_iterator = self._read_full_refresh(stream_instance, configured_stream, internal_config) | ||
|
||
record_counter = 0 | ||
stream_name = configured_stream.stream.name | ||
logger.info(f"Syncing stream: {stream_name} ") | ||
for record in record_iterator: | ||
if record.type == MessageType.RECORD: | ||
if internal_config.limit and record_counter >= internal_config.limit: | ||
logger.info(f"Reached limit defined by internal config ({internal_config.limit}), stop reading") | ||
break | ||
record_counter += 1 | ||
yield record | ||
|
||
logger.info(f"Read {record_counter} records from {stream_name} stream") | ||
|
||
@staticmethod | ||
def _limit_reached(internal_config: InternalConfig, records_counter: int) -> bool: | ||
""" | ||
Check if record count reached liimt set by internal config. | ||
:param internal_config - internal CDK configuration separated from user defined config | ||
:records_counter - number of records already red | ||
:return True if limit reached, False otherwise | ||
""" | ||
if internal_config.limit: | ||
if records_counter >= internal_config.limit: | ||
return True | ||
return False | ||
|
||
def _read_incremental( | ||
self, | ||
logger: AirbyteLogger, | ||
stream_instance: Stream, | ||
configured_stream: ConfiguredAirbyteStream, | ||
connector_state: MutableMapping[str, Any], | ||
internal_config: InternalConfig, | ||
) -> Iterator[AirbyteMessage]: | ||
stream_name = configured_stream.stream.name | ||
stream_state = connector_state.get(stream_name, {}) | ||
|
@@ -170,31 +181,43 @@ def _read_incremental( | |
slices = stream_instance.stream_slices( | ||
cursor_field=configured_stream.cursor_field, sync_mode=SyncMode.incremental, stream_state=stream_state | ||
) | ||
total_records_counter = 0 | ||
for slice in slices: | ||
record_counter = 0 | ||
records = stream_instance.read_records( | ||
sync_mode=SyncMode.incremental, | ||
stream_slice=slice, | ||
stream_state=stream_state, | ||
cursor_field=configured_stream.cursor_field or None, | ||
) | ||
for record_data in records: | ||
record_counter += 1 | ||
for record_counter, record_data in enumerate(records, start=1): | ||
yield self._as_airbyte_record(stream_name, record_data) | ||
stream_state = stream_instance.get_updated_state(stream_state, record_data) | ||
if checkpoint_interval and record_counter % checkpoint_interval == 0: | ||
yield self._checkpoint_state(stream_name, stream_state, connector_state, logger) | ||
|
||
total_records_counter += 1 | ||
if self._limit_reached(internal_config, total_records_counter): | ||
# Break from slice loop to save state and exit from _read_incremental function. | ||
break | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so we still going to read all slices, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
|
||
yield self._checkpoint_state(stream_name, stream_state, connector_state, logger) | ||
if self._limit_reached(internal_config, total_records_counter): | ||
return | ||
|
||
def _read_full_refresh(self, stream_instance: Stream, configured_stream: ConfiguredAirbyteStream) -> Iterator[AirbyteMessage]: | ||
def _read_full_refresh( | ||
self, stream_instance: Stream, configured_stream: ConfiguredAirbyteStream, internal_config: InternalConfig | ||
) -> Iterator[AirbyteMessage]: | ||
slices = stream_instance.stream_slices(sync_mode=SyncMode.full_refresh, cursor_field=configured_stream.cursor_field) | ||
total_records_counter = 0 | ||
for slice in slices: | ||
records = stream_instance.read_records( | ||
stream_slice=slice, sync_mode=SyncMode.full_refresh, cursor_field=configured_stream.cursor_field | ||
) | ||
for record in records: | ||
yield self._as_airbyte_record(configured_stream.stream.name, record) | ||
total_records_counter += 1 | ||
if self._limit_reached(internal_config, total_records_counter): | ||
return | ||
|
||
def _checkpoint_state(self, stream_name, stream_state, connector_state, logger): | ||
logger.info(f"Setting state of {stream_name} stream to {stream_state}") | ||
|
This file contains 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
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.
I think we still need this because we might have limit > size(slice)
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.
Got you idea, but don't think its good to put this condition back, check out my proposal (updated PR)