Skip to content

Commit ab7ab68

Browse files
maxi297aaronsteersoctavia-squidington-iii
authored
chore(refactor): Remove Partition.close (#32)
Co-authored-by: Aaron ("AJ") Steers <[email protected]> Co-authored-by: octavia-squidington-iii <[email protected]>
1 parent 39786d2 commit ab7ab68

File tree

8 files changed

+22
-57
lines changed

8 files changed

+22
-57
lines changed

airbyte_cdk/sources/concurrent_source/concurrent_read_processor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ def on_partition_complete_sentinel(
114114

115115
try:
116116
if sentinel.is_successful:
117-
partition.close()
117+
stream = self._stream_name_to_instance[partition.stream_name()]
118+
stream.cursor.close_partition(partition)
118119
except Exception as exception:
119120
self._flag_exception(partition.stream_name(), exception)
120121
yield AirbyteTracedException.from_exception(

airbyte_cdk/sources/file_based/stream/concurrent/adapters.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -226,16 +226,13 @@ def __init__(
226226
sync_mode: SyncMode,
227227
cursor_field: Optional[List[str]],
228228
state: Optional[MutableMapping[str, Any]],
229-
cursor: "AbstractConcurrentFileBasedCursor",
230229
):
231230
self._stream = stream
232231
self._slice = _slice
233232
self._message_repository = message_repository
234233
self._sync_mode = sync_mode
235234
self._cursor_field = cursor_field
236235
self._state = state
237-
self._cursor = cursor
238-
self._is_closed = False
239236

240237
def read(self) -> Iterable[Record]:
241238
try:
@@ -289,13 +286,6 @@ def to_slice(self) -> Optional[Mapping[str, Any]]:
289286
file = self._slice["files"][0]
290287
return {"files": [file]}
291288

292-
def close(self) -> None:
293-
self._cursor.close_partition(self)
294-
self._is_closed = True
295-
296-
def is_closed(self) -> bool:
297-
return self._is_closed
298-
299289
def __hash__(self) -> int:
300290
if self._slice:
301291
# Convert the slice to a string so that it can be hashed
@@ -352,7 +342,6 @@ def generate(self) -> Iterable[FileBasedStreamPartition]:
352342
self._sync_mode,
353343
self._cursor_field,
354344
self._state,
355-
self._cursor,
356345
)
357346
)
358347
self._cursor.set_pending_partitions(pending_partitions)

airbyte_cdk/sources/streams/concurrent/adapters.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ def create_from_stream(
9696
else SyncMode.incremental,
9797
[cursor_field] if cursor_field is not None else None,
9898
state,
99-
cursor,
10099
),
101100
name=stream.name,
102101
namespace=stream.namespace,
@@ -259,7 +258,6 @@ def __init__(
259258
sync_mode: SyncMode,
260259
cursor_field: Optional[List[str]],
261260
state: Optional[MutableMapping[str, Any]],
262-
cursor: Cursor,
263261
):
264262
"""
265263
:param stream: The stream to delegate to
@@ -272,8 +270,6 @@ def __init__(
272270
self._sync_mode = sync_mode
273271
self._cursor_field = cursor_field
274272
self._state = state
275-
self._cursor = cursor
276-
self._is_closed = False
277273

278274
def read(self) -> Iterable[Record]:
279275
"""
@@ -323,13 +319,6 @@ def __hash__(self) -> int:
323319
def stream_name(self) -> str:
324320
return self._stream.name
325321

326-
def close(self) -> None:
327-
self._cursor.close_partition(self)
328-
self._is_closed = True
329-
330-
def is_closed(self) -> bool:
331-
return self._is_closed
332-
333322
def __repr__(self) -> str:
334323
return f"StreamPartition({self._stream.name}, {self._slice})"
335324

@@ -349,7 +338,6 @@ def __init__(
349338
sync_mode: SyncMode,
350339
cursor_field: Optional[List[str]],
351340
state: Optional[MutableMapping[str, Any]],
352-
cursor: Cursor,
353341
):
354342
"""
355343
:param stream: The stream to delegate to
@@ -360,7 +348,6 @@ def __init__(
360348
self._sync_mode = sync_mode
361349
self._cursor_field = cursor_field
362350
self._state = state
363-
self._cursor = cursor
364351

365352
def generate(self) -> Iterable[Partition]:
366353
for s in self._stream.stream_slices(
@@ -373,7 +360,6 @@ def generate(self) -> Iterable[Partition]:
373360
self._sync_mode,
374361
self._cursor_field,
375362
self._state,
376-
self._cursor,
377363
)
378364

379365

@@ -451,7 +437,6 @@ def generate(self) -> Iterable[Partition]:
451437
self._sync_mode,
452438
self._cursor_field,
453439
self._state,
454-
self._cursor,
455440
)
456441

457442

airbyte_cdk/sources/streams/concurrent/partitions/partition.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,6 @@ def stream_name(self) -> str:
4040
"""
4141
pass
4242

43-
@abstractmethod
44-
def close(self) -> None:
45-
"""
46-
Closes the partition.
47-
"""
48-
pass
49-
50-
@abstractmethod
51-
def is_closed(self) -> bool:
52-
"""
53-
Returns whether the partition is closed.
54-
:return:
55-
"""
56-
pass
57-
5843
@abstractmethod
5944
def __hash__(self) -> int:
6045
"""

unit_tests/sources/file_based/stream/concurrent/test_adapters.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,12 @@ def test_file_based_stream_partition(transformer, expected_records):
124124
cursor_field = None
125125
state = None
126126
partition = FileBasedStreamPartition(
127-
stream, _slice, message_repository, sync_mode, cursor_field, state, _ANY_CURSOR
127+
stream,
128+
_slice,
129+
message_repository,
130+
sync_mode,
131+
cursor_field,
132+
state,
128133
)
129134

130135
a_log_message = AirbyteMessage(
@@ -168,7 +173,6 @@ def test_file_based_stream_partition_raising_exception(exception_type, expected_
168173
_ANY_SYNC_MODE,
169174
_ANY_CURSOR_FIELD,
170175
_ANY_STATE,
171-
_ANY_CURSOR,
172176
)
173177

174178
stream.read_records.side_effect = Exception()
@@ -204,7 +208,12 @@ def test_file_based_stream_partition_hash(_slice, expected_hash):
204208
stream = Mock()
205209
stream.name = "stream"
206210
partition = FileBasedStreamPartition(
207-
stream, _slice, Mock(), _ANY_SYNC_MODE, _ANY_CURSOR_FIELD, _ANY_STATE, _ANY_CURSOR
211+
stream,
212+
_slice,
213+
Mock(),
214+
_ANY_SYNC_MODE,
215+
_ANY_CURSOR_FIELD,
216+
_ANY_STATE,
208217
)
209218

210219
_hash = partition.__hash__()

unit_tests/sources/file_based/stream/concurrent/test_file_based_concurrent_cursor.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ def test_add_file(
214214
SyncMode.full_refresh,
215215
FileBasedConcurrentCursor.CURSOR_FIELD,
216216
initial_state,
217-
cursor,
218217
)
219218
for uri, timestamp in pending_files
220219
]

unit_tests/sources/streams/concurrent/test_adapters.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def test_stream_partition_generator(sync_mode):
7676
stream.stream_slices.return_value = stream_slices
7777

7878
partition_generator = StreamPartitionGenerator(
79-
stream, message_repository, _ANY_SYNC_MODE, _ANY_CURSOR_FIELD, _ANY_STATE, _ANY_CURSOR
79+
stream, message_repository, _ANY_SYNC_MODE, _ANY_CURSOR_FIELD, _ANY_STATE
8080
)
8181

8282
partitions = list(partition_generator.generate())
@@ -115,9 +115,7 @@ def test_stream_partition(transformer, expected_records):
115115
sync_mode = SyncMode.full_refresh
116116
cursor_field = None
117117
state = None
118-
partition = StreamPartition(
119-
stream, _slice, message_repository, sync_mode, cursor_field, state, _ANY_CURSOR
120-
)
118+
partition = StreamPartition(stream, _slice, message_repository, sync_mode, cursor_field, state)
121119

122120
a_log_message = AirbyteMessage(
123121
type=MessageType.LOG,
@@ -162,7 +160,6 @@ def test_stream_partition_raising_exception(exception_type, expected_display_mes
162160
_ANY_SYNC_MODE,
163161
_ANY_CURSOR_FIELD,
164162
_ANY_STATE,
165-
_ANY_CURSOR,
166163
)
167164

168165
stream.read_records.side_effect = Exception()
@@ -188,7 +185,7 @@ def test_stream_partition_hash(_slice, expected_hash):
188185
stream = Mock()
189186
stream.name = "stream"
190187
partition = StreamPartition(
191-
stream, _slice, Mock(), _ANY_SYNC_MODE, _ANY_CURSOR_FIELD, _ANY_STATE, _ANY_CURSOR
188+
stream, _slice, Mock(), _ANY_SYNC_MODE, _ANY_CURSOR_FIELD, _ANY_STATE
192189
)
193190

194191
_hash = partition.__hash__()

unit_tests/sources/streams/concurrent/test_concurrent_read_processor.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ def test_handle_on_partition_complete_sentinel_with_messages_from_repository(sel
249249
]
250250
assert messages == expected_messages
251251

252-
partition.close.assert_called_once()
252+
self._stream.cursor.close_partition.assert_called_once()
253253

254254
@freezegun.freeze_time("2020-01-01T00:00:00")
255255
def test_handle_on_partition_complete_sentinel_yields_status_message_if_the_stream_is_done(
@@ -298,14 +298,14 @@ def test_handle_on_partition_complete_sentinel_yields_status_message_if_the_stre
298298
)
299299
]
300300
assert messages == expected_messages
301-
self._a_closed_partition.close.assert_called_once()
301+
self._another_stream.cursor.close_partition.assert_called_once()
302302

303303
@freezegun.freeze_time("2020-01-01T00:00:00")
304304
def test_given_exception_on_partition_complete_sentinel_then_yield_error_trace_message_and_stream_is_incomplete(
305305
self,
306306
) -> None:
307307
self._a_closed_partition.stream_name.return_value = self._stream.name
308-
self._a_closed_partition.close.side_effect = ValueError
308+
self._stream.cursor.close_partition.side_effect = ValueError
309309

310310
handler = ConcurrentReadProcessor(
311311
[self._stream],
@@ -375,7 +375,7 @@ def test_handle_on_partition_complete_sentinel_yields_no_status_message_if_the_s
375375

376376
expected_messages = []
377377
assert messages == expected_messages
378-
partition.close.assert_called_once()
378+
self._stream.cursor.close_partition.assert_called_once()
379379

380380
@freezegun.freeze_time("2020-01-01T00:00:00")
381381
def test_on_record_no_status_message_no_repository_messge(self):
@@ -733,7 +733,7 @@ def test_given_partition_completion_is_not_success_then_do_not_close_partition(s
733733
)
734734
)
735735

736-
assert self._an_open_partition.close.call_count == 0
736+
assert self._stream.cursor.close_partition.call_count == 0
737737

738738
def test_is_done_is_false_if_there_are_any_instances_to_read_from(self):
739739
stream_instances_to_read_from = [self._stream]

0 commit comments

Comments
 (0)