GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feldera/python/feldera/pipeline.py at faster-parser · feldera/feldera

Expand file tree

/

pipeline.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
2385 lines (1939 loc) · 92.6 KB
/

pipeline.py

Copy path

File metadata and controls

2385 lines (1939 loc) · 92.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import logging
import pathlib
import time
import warnings
from collections import deque
from datetime import datetime
from threading import Event
from typing import (
Any,
Callable,
Dict,
Generator,
List,
Mapping,
Optional,
)
from uuid import UUID
import pandas
import pyarrow as pa
from feldera._callback_runner import CallbackRunner
from feldera._helpers import chunk_dataframe, ensure_dataframe_has_columns
from feldera._long_operation_warning import LongOperationWarning
from feldera.enums import (
BootstrapPolicy,
CheckpointStatus,
CompletionTokenStatus,
DeploymentDesiredStatus,
DeploymentResourcesDesiredStatus,
DeploymentResourcesStatus,
DeploymentRuntimeDesiredStatus,
DeploymentRuntimeStatus,
PipelineFieldSelector,
PipelineStatus,
ProgramStatus,
StorageStatus,
TransactionStatus,
)
from feldera.output_handler import OutputHandler
from feldera.rest.errors import (
FelderaAPIError,
FelderaCommunicationError,
FelderaTimeoutError,
)
from feldera.rest.feldera_client import FelderaClient
from feldera.rest.logs import LogStream
from feldera.rest.pipeline import Pipeline as InnerPipeline
from feldera.rest.sql_table import SQLTable
from feldera.rest.sql_view import SQLView
from feldera.runtime_config import RuntimeConfig
from feldera.stats import InputEndpointStatus, OutputEndpointStatus, PipelineStatistics
from feldera.types import CheckpointMetadata
logger = logging.getLogger(__name__)
class Pipeline:
def __init__(self, client: FelderaClient):
self.client: FelderaClient = client
self._inner: InnerPipeline | None = None
@staticmethod
def _from_inner(inner: InnerPipeline, client: FelderaClient) -> "Pipeline":
pipeline = Pipeline(client)
pipeline._inner = inner
return pipeline
def refresh(self, field_selector: PipelineFieldSelector):
"""
Calls the backend to get the updated, latest version of the pipeline.
:param field_selector: Choose what pipeline information to refresh; see PipelineFieldSelector enum definition.
:raises FelderaConnectionError: If there is an issue connecting to the backend.
"""
self._inner = self.client.get_pipeline(self.name, field_selector)
def status(self) -> PipelineStatus:
"""
Return the current status of the pipeline.
"""
try:
self.refresh(PipelineFieldSelector.STATUS)
return PipelineStatus.from_str(self._inner.deployment_status)
except FelderaAPIError as err:
if err.status_code == 404:
return PipelineStatus.NOT_FOUND
else:
raise err
def wait_for_status(
self, expected_status: PipelineStatus, timeout: Optional[int] = None
) -> None:
"""
Wait for the pipeline to reach the specified status.
:param expected_status: The status to wait for
:param timeout: Maximum time to wait in seconds. If None, waits forever (default: None)
:raises TimeoutError: If the expected status is not reached within the timeout
"""
start_time = time.monotonic()
while True:
current_status = self.status()
if current_status == expected_status:
return
if timeout is not None and time.monotonic() - start_time >= timeout:
raise TimeoutError(
f"Pipeline did not reach {expected_status.name} status within {timeout} seconds"
)
time.sleep(1)
def stats(self) -> PipelineStatistics:
"""Gets the pipeline metrics and performance counters."""
return PipelineStatistics.from_dict(self.client.get_pipeline_stats(self.name))
def metrics(self, format: str = "prometheus"):
"""Gets the pipeline circuit metrics.
:param format: The metrics format, "prometheus" (default) or "json".
:return: Prometheus exposition text as `str`, or a `dict` for "json".
"""
return self.client.get_pipeline_metrics(self.name, format=format)
def logs(self, follow: bool = True) -> Generator[str, None, None]:
"""Gets the pipeline logs.
:param follow: Set False to stop after the retained logs instead of
waiting for new lines.
"""
return self.client.get_pipeline_logs(self.name, follow=follow)
def resume_logs(self, cursor: Optional[str] = None) -> LogStream:
"""
Open the pipeline logs stream, starting from the last read line (cursor).
Where :meth:`logs` replays the whole retained buffer on every connection, this
delivers only the lines following `cursor`.
The logs buffer is discarded when the runner restarts or the pipeline is deleted,
and survives a pipeline being stopped and started. A cursor issued during an
earlier lifetime of the buffer is not an error: the stream starts at the beginning
of the retained buffer, and `LogPosition.gap` names the lines that were lost.
For example, to keep reading the logs across dropped connections::
cursor = None
while True:
with pipeline.resume_logs(cursor) as stream:
for line in stream:
print(line)
cursor = stream.cursor()
:param cursor: The position to resume after, as returned by
:meth:`feldera.rest.logs.LogStream.cursor`. `None` starts at the beginning of
the retained buffer.
:return: The open stream. Close it when done reading.
"""
return self.client.resume_pipeline_logs(self.name, cursor)
def input_pandas(
self,
table_name: str,
df: pandas.DataFrame,
force: bool = False,
connector_metadata: Optional[Mapping[str, Any]] = None,
) -> Optional[str]:
"""
Push all rows in a pandas DataFrame to the pipeline.
The pipeline must either be in RUNNING or PAUSED states to push data.
An error will be raised if the pipeline is in any other state.
The dataframe must have the same columns as the table in the pipeline.
:param table_name: The name of the table to insert data into.
:param df: The pandas DataFrame to be pushed to the pipeline.
:param force: `True` to push data even if the pipeline is paused. `False` by default.
:param connector_metadata: Connector metadata for every row of the DataFrame,
as a dict of attribute names to values. ``CONNECTOR_METADATA()`` returns it
for the rows. None by default.
:returns: The completion token of the last chunk pushed, which also covers
the earlier chunks, or None if the DataFrame is empty and nothing was pushed.
:raises ValueError: If the table does not exist in the pipeline.
:raises RuntimeError: If the pipeline is not in a valid state to push data.
:raises RuntimeError: If the pipeline is paused and force is not set to `True`.
"""
status = self.status()
if status not in [
PipelineStatus.RUNNING,
PipelineStatus.PAUSED,
PipelineStatus.CONCURRENTBOOTSTRAPPING,
PipelineStatus.SYNCHRONIZING,
]:
raise RuntimeError("Pipeline must be running or paused to push data")
if not force and status == PipelineStatus.PAUSED:
raise RuntimeError("Pipeline is paused, set force=True to push data")
ensure_dataframe_has_columns(df)
pipeline = self.client.get_pipeline(self.name, PipelineFieldSelector.ALL)
if table_name.lower() != "now" and table_name.lower() not in [
tbl.name.lower() for tbl in pipeline.tables
]:
raise ValueError(
f"Cannot push to table '{table_name}': table with this name"
f" does not exist in the '{self.name}' pipeline"
)
else:
# consider validating the schema here
token = None
for datum in chunk_dataframe(df):
token = self.client.push_to_pipeline(
self.name,
table_name,
"json",
datum.to_json(orient="records", date_format="epoch"),
json_flavor="pandas",
array=True,
serialize=False,
force=force,
connector_metadata=connector_metadata,
)
return token
def input_json(
self,
table_name: str,
data: Dict | list,
update_format: str = "raw",
force: bool = False,
wait: bool = True,
connector_metadata: Optional[Mapping[str, Any]] = None,
**kwargs,
) -> str:
"""
Push this JSON data to the specified table of the pipeline.
The pipeline must either be in RUNNING or PAUSED states to push data.
An error will be raised if the pipeline is in any other state.
:param table_name: The name of the table to push data into.
:param data: The JSON encoded data to be pushed to the pipeline. The data should be in the form:
`{'col1': 'val1', 'col2': 'val2'}` or `[{'col1': 'val1', 'col2': 'val2'}, {'col1': 'val1', 'col2': 'val2'}]`
:param update_format: The update format of the JSON data to be pushed to the pipeline. Must be one of:
"raw", "insert_delete". https://docs.feldera.com/formats/json#the-insertdelete-format
:param force: `True` to push data even if the pipeline is paused. `False` by default.
:param wait: If True, blocks until this input has been processed by the pipeline
:param connector_metadata: Connector metadata for every record of this request,
as a dict of attribute names to values. ``CONNECTOR_METADATA()`` returns it
for the records, so a column declared with
``DEFAULT CAST(CONNECTOR_METADATA()['name'] AS type)`` takes its value from
the ``name`` attribute. None by default.
:param kwargs: Additional key word arguments forwarded to the Client push_to_pipeline method
:returns: The completion token to this input.
:raises ValueError: If the update format is invalid.
:raises FelderaAPIError: If the pipeline is not in a valid state to push data.
:raises RuntimeError: If the pipeline is paused and `force` is not set to `True`.
"""
status = self.status()
if not force and status == PipelineStatus.PAUSED:
raise RuntimeError("Pipeline is paused, set force=True to push data")
if update_format not in ["raw", "insert_delete"]:
raise ValueError("update_format must be one of raw or insert_delete")
array = True if isinstance(data, list) else False
return self.client.push_to_pipeline(
self.name,
table_name,
"json",
data,
update_format=update_format,
array=array,
force=force,
wait=wait,
connector_metadata=connector_metadata,
**kwargs,
)
def pause_input_connector(self, table_name: str, connector_name: str):
"""
Pause the specified input connector.
A paused input connector stops fetching data from its source. All
connectors are RUNNING by default.
Refer to the connector documentation for more information:
https://docs.feldera.com/connectors/orchestration#input-connectors
:param table_name: The name of the table that the connector is attached to.
:param connector_name: The name of the connector to pause.
:raises FelderaAPIError: If the connector is not found, or if the pipeline is not running.
"""
self.client.pause_input_connector(self.name, table_name, connector_name)
def start_input_connector(self, table_name: str, connector_name: str):
"""
Start (resume) the specified input connector.
Refer to the connector documentation for more information:
https://docs.feldera.com/connectors/orchestration#input-connectors
:param table_name: The name of the table that the connector is attached to.
:param connector_name: The name of the connector to start.
:raises FelderaAPIError: If the connector is not found, or if the pipeline is not running.
"""
self.client.start_input_connector(self.name, table_name, connector_name)
def pause_connector(self, table_name: str, connector_name: str):
"""
Pause the specified input connector.
.. deprecated::
Use :meth:`.pause_input_connector`, which names the kind of
connector it acts on, like :meth:`.pause_output_connector`.
"""
warnings.warn(
"pause_connector is deprecated; use pause_input_connector.",
DeprecationWarning,
stacklevel=2,
)
self.pause_input_connector(table_name, connector_name)
def resume_connector(self, table_name: str, connector_name: str):
"""
Start (resume) the specified input connector.
.. deprecated::
Use :meth:`.start_input_connector`, which names the kind of
connector it acts on, like :meth:`.start_output_connector`.
"""
warnings.warn(
"resume_connector is deprecated; use start_input_connector.",
DeprecationWarning,
stacklevel=2,
)
self.start_input_connector(table_name, connector_name)
def pause_output_connector(self, view_name: str, connector_name: str):
"""
Pause the specified output connector.
A paused output connector discards the output it receives instead of
writing it to its sink. Output produced while the connector is paused is
gone for good. All connectors are RUNNING by default.
Refer to the connector documentation for more information:
https://docs.feldera.com/connectors/orchestration#output-connectors
:param view_name: The name of the view that the connector is attached to.
:param connector_name: The name of the connector to pause.
:raises FelderaAPIError: If the connector is not found, or if the pipeline is not running.
"""
self.client.pause_output_connector(self.name, view_name, connector_name)
def start_output_connector(self, view_name: str, connector_name: str):
"""
Start (resume) the specified output connector.
The connector resumes with the output the pipeline produces from this
point on; output produced while it was paused is not replayed.
Refer to the connector documentation for more information:
https://docs.feldera.com/connectors/orchestration#output-connectors
:param view_name: The name of the view that the connector is attached to.
:param connector_name: The name of the connector to start.
:raises FelderaAPIError: If the connector is not found, or if the pipeline is not running.
"""
self.client.start_output_connector(self.name, view_name, connector_name)
def input_connector_stats(
self, table_name: str, connector_name: str
) -> InputEndpointStatus:
"""
Get the status of the specified input connector.
"""
return InputEndpointStatus.from_dict(
self.client.input_connector_stats(self.name, table_name, connector_name)
)
def output_connector_stats(
self, view_name: str, connector_name: str
) -> OutputEndpointStatus:
"""
Get the status of the specified output connector.
"""
return OutputEndpointStatus.from_dict(
self.client.output_connector_stats(self.name, view_name, connector_name)
)
def listen(self, view_name: str) -> OutputHandler:
"""
Follow the change stream (i.e., the output) of the provided view.
Returns an output handle to read the changes.
When the pipeline is stopped, the handle is dropped.
The handle will only receive changes from the point in time when the listener is created.
In order to receive all changes since the pipeline started, you can create the pipeline in the `PAUSED` state
using :meth:`start_paused`, attach listeners and unpause the pipeline using :meth:`resume`.
To read the changes that one input caused, pass the input's completion
token to :meth:`.OutputHandler.wait_for_token` before reading the handle.
:param view_name: The name of the view to listen to.
"""
if self.status() not in [
PipelineStatus.PAUSED,
PipelineStatus.RUNNING,
PipelineStatus.CONCURRENTBOOTSTRAPPING,
PipelineStatus.SYNCHRONIZING,
]:
raise RuntimeError("Pipeline must be running or paused to listen to output")
handler = OutputHandler(self.client, self.name, view_name)
handler.start()
return handler
def foreach_chunk(
self, view_name: str, callback: Callable[[pandas.DataFrame, int], None]
):
"""
Run the given callback on each chunk of the output of the specified view.
The callback will only receive changes from the point in time when the listener is created.
In order to receive all changes since the pipeline started, you can create the pipeline in the `PAUSED` state
using :meth:`start_paused`, attach listeners and unpause the pipeline using :meth:`resume`.
:param view_name: The name of the view.
:param callback: The callback to run on each chunk. The callback should take two arguments:
- **chunk** -> The chunk as a pandas DataFrame
- **seq_no** -> The sequence number. The sequence number is a monotonically increasing integer that
starts from 0. Note that the sequence number is unique for each chunk, but not necessarily contiguous.
Please note that the callback is run in a separate thread, so it should be thread-safe.
Please note that the callback should not block for a long time, as by default, backpressure is enabled and
will block the pipeline.
.. note::
- The callback must be thread-safe as it will be run in a separate thread.
"""
if self.status() not in [
PipelineStatus.RUNNING,
PipelineStatus.PAUSED,
PipelineStatus.CONCURRENTBOOTSTRAPPING,
PipelineStatus.SYNCHRONIZING,
]:
raise RuntimeError("Pipeline must be running or paused to listen to output")
event = Event()
handler = CallbackRunner(
self.client, self.name, view_name, callback, lambda exception: None, event
)
handler.start()
event.wait()
# Statuses in which `wait_for_completion` waits for the pipeline.
_WAIT_FOR_COMPLETION_STATUSES = (
PipelineStatus.RUNNING,
PipelineStatus.INITIALIZING,
PipelineStatus.PROVISIONING,
PipelineStatus.BOOTSTRAPPING,
PipelineStatus.CONCURRENTBOOTSTRAPPING,
PipelineStatus.SYNCHRONIZING,
)
def _is_exactly_once(self) -> bool:
"""True if the pipeline uses exactly-once fault tolerance, which
recovers all of its input after a restart."""
ft = self.runtime_config().__dict__.get("fault_tolerance") or {}
return ft.get("model") == "exactly_once"
def _not_running_error(self, status: PipelineStatus) -> RuntimeError:
"""The error for a pipeline that is not in a status to complete."""
msg = f"Pipeline must be running to wait for completion (status: {status.name}"
try:
details = self.deployment_runtime_status_details()
except FelderaAPIError:
details = None
if details:
msg += f", details: {details}"
return RuntimeError(msg + ")")
def wait_for_completion(
self,
force_stop: bool = False,
timeout_s: float | None = None,
unavailable_timeout_s: float = 60.0,
):
"""
Block until the pipeline has completed processing all input records.
This method blocks until (1) all input connectors attached to the
pipeline have finished reading their input data sources and issued
end-of-input notifications to the pipeline, and (2) all inputs received
from these connectors have been fully processed and corresponding
outputs have been sent out through the output connectors.
This method will block indefinitely if at least one of the input
connectors attached to the pipeline is a streaming connector, such as
Kafka, that does not issue the end-of-input notification.
:param force_stop: If True, the pipeline will be forcibly stopped after
completion. False by default. No checkpoints will be made.
:param timeout_s: Optional. The maximum time (in seconds) to wait for
the pipeline to complete. The default is None, which means wait
indefinitely.
:param unavailable_timeout_s: The maximum time (in seconds) to wait
for a pipeline in the UNAVAILABLE status to become available
again. The manager reports UNAVAILABLE when one status probe
fails, for example when the node is overloaded, so this status
is usually transient. 60 seconds by default.
:raises RuntimeError: If the pipeline is not running, stays
UNAVAILABLE for longer than `unavailable_timeout_s`, restarts
during the wait without exactly-once fault tolerance, or returns
unknown metrics.
"""
status = self.status()
if (
status not in self._WAIT_FOR_COMPLETION_STATUSES
and status != PipelineStatus.UNAVAILABLE
):
raise self._not_running_error(status)
start_time = time.monotonic()
unavailable_since: Optional[float] = None
first_incarnation: Optional[UUID] = None
# Reused by the warning message below; is_complete() would fetch and
# discard the same stats, hiding whether the wait is stalled or just
# slow (e.g. mid-transaction-commit).
latest_stats: Optional[PipelineStatistics] = None
wait_for_completion = LongOperationWarning(
logger,
lambda elapsed: (
f"still waiting for pipeline {self.name} to complete, "
f"waited {elapsed:.1f} seconds ({latest_stats.global_metrics.progress_summary()})"
),
lambda elapsed: (
f"pipeline {self.name} completed after {elapsed:.1f} seconds"
),
)
while True:
if timeout_s is not None:
elapsed = time.monotonic() - start_time
if elapsed > timeout_s:
raise TimeoutError(
f"timeout ({timeout_s}s) reached while waiting for"
f" pipeline '{self.name}' to complete"
)
try:
latest_stats = self.stats()
except (
FelderaAPIError,
FelderaTimeoutError,
FelderaCommunicationError,
) as err:
status = self.status()
if status in self._WAIT_FOR_COMPLETION_STATUSES:
raise
if status != PipelineStatus.UNAVAILABLE:
raise self._not_running_error(status) from err
now = time.monotonic()
if unavailable_since is None:
unavailable_since = now
elif now - unavailable_since > unavailable_timeout_s:
raise self._not_running_error(status) from err
time.sleep(1)
continue
unavailable_since = None
# Without exactly-once fault tolerance, a restarted pipeline can
# lose input, so its completion does not cover what we wait for.
incarnation = latest_stats.global_metrics.incarnation_uuid
if first_incarnation is None:
first_incarnation = incarnation
elif incarnation != first_incarnation:
if not self._is_exactly_once():
raise RuntimeError(
f"pipeline '{self.name}' restarted while waiting for"
f" completion (incarnation {first_incarnation} became"
f" {incarnation}) without exactly-once fault"
" tolerance; input received since the last"
" checkpoint may be lost"
)
logger.info(
"pipeline %s restarted while waiting for completion"
" (incarnation %s became %s); exactly-once fault tolerance"
" replays its input",
self.name,
first_incarnation,
incarnation,
)
first_incarnation = incarnation
pipeline_complete: Optional[bool] = (
latest_stats.global_metrics.pipeline_complete
)
if pipeline_complete is None:
raise RuntimeError(
"received unknown metrics from the pipeline, pipeline_complete is None"
)
elif pipeline_complete:
wait_for_completion.done()
break
wait_for_completion.check()
time.sleep(1)
if force_stop:
self.stop(force=True)
def is_complete(self) -> bool:
"""
Check if the pipeline has completed processing all input records.
Returns True if (1) all input connectors attached to the
pipeline have finished reading their input data sources and issued
end-of-input notifications to the pipeline, and (2) all inputs received
from these connectors have been fully processed and corresponding
outputs have been sent out through the output connectors.
"""
return self.stats().global_metrics.pipeline_complete
def wait_for_idle(
self,
idle_interval_s: float = 5.0,
timeout_s: float | None = None,
poll_interval_s: float = 0.2,
):
"""
Wait for the pipeline to become idle and then returns.
Idle is defined as a sufficiently long interval in which the number of
input and processed records reported by the pipeline do not change, and
they equal each other (thus, all input records present at the pipeline
have been processed).
:param idle_interval_s: Idle interval duration (default is 5.0 seconds).
:param timeout_s: Timeout waiting for idle (`None` = no timeout is enforced).
:param poll_interval_s: Polling interval, should be set substantially
smaller than the idle interval (default is 0.2 seconds).
:raises ValueError: If idle interval is larger than timeout, poll interval
is larger than timeout, or poll interval is larger than idle interval.
:raises RuntimeError: If the metrics are missing or the timeout was
reached.
"""
if timeout_s is not None and idle_interval_s > timeout_s:
raise ValueError(
f"idle interval ({idle_interval_s}s) cannot be larger than"
f" timeout ({timeout_s}s)"
)
if timeout_s is not None and poll_interval_s > timeout_s:
raise ValueError(
f"poll interval ({poll_interval_s}s) cannot be larger than"
f" timeout ({timeout_s}s)"
)
if poll_interval_s > idle_interval_s:
raise ValueError(
f"poll interval ({poll_interval_s}s) cannot be larger "
f"than idle interval ({idle_interval_s}s)"
)
start_time_s = time.monotonic()
idle_started_s = None
prev = (0, 0)
while True:
now_s = time.monotonic()
# Metrics retrieval
metrics = self.stats().global_metrics
total_input_records = metrics.total_input_records
total_processed_records = metrics.total_processed_records
if metrics.total_input_records is None:
raise RuntimeError(
"total_input_records is missing from the pipeline metrics"
)
if metrics.total_processed_records is None:
raise RuntimeError(
"""total_processed_records is missing from the pipeline \
metrics"""
)
# Idle check
unchanged = (
prev[0] == total_input_records and prev[1] == total_processed_records
)
equal = total_input_records == total_processed_records
prev = (total_input_records, total_processed_records)
if unchanged and equal:
if idle_started_s is None:
idle_started_s = now_s
else:
idle_started_s = None
if idle_started_s is not None and now_s - idle_started_s >= idle_interval_s:
return
# Timeout
if timeout_s is not None and now_s - start_time_s >= timeout_s:
raise RuntimeError(f"waiting for idle reached timeout ({timeout_s}s)")
time.sleep(poll_interval_s)
def _wait_for_adhoc_write(
self, poll_interval_s: float = 0.1, timeout_s: float | None = None
):
"""
Block until every record currently in the circuit has been processed
to completion.
This backs the ``wait`` option of :meth:`.query` and :meth:`.execute`.
An ad-hoc ``INSERT`` returns once its records have been
pushed to the circuit, but not necessarily once the circuit has
processed them, so a subsequent ad-hoc ``SELECT`` is not guaranteed to
observe the write. To close that gap, this method snapshots the number
of records that have entered the circuit
(``total_input_records - buffered_input_records``) and polls until the
pipeline reports at least that many records completed. The write's
records are part of that count, because the ``/query`` endpoint pushes
them to the circuit before returning.
The metric polled is ``total_completed_records``, so "completed" means
more than just visible to ad-hoc queries: a record is counted only once
the DBSP engine has processed it *and* every output connector has
processed the outputs derived from it. Therefore, when this method
returns, both of the following hold for the write:
- ad-hoc queries reflect the updates, and
- all output connectors have processed (sent out) the outputs the
updates produced.
:param poll_interval_s: Interval between ``/stats`` polls.
:param timeout_s: Maximum time to wait, or ``None`` to wait
indefinitely.
:raises TimeoutError: If ``timeout_s`` elapses before the records are
processed.
"""
metrics = self.stats().global_metrics
target = metrics.total_input_records - metrics.buffered_input_records
start_s = time.monotonic()
while self.stats().global_metrics.total_completed_records < target:
if timeout_s is not None and time.monotonic() - start_s >= timeout_s:
raise TimeoutError(
f"timeout ({timeout_s}s) reached while waiting for an ad-hoc"
f" write on pipeline '{self.name}' to be processed"
)
time.sleep(poll_interval_s)
def activate(
self, wait: bool = True, timeout_s: Optional[float] = None
) -> Optional[PipelineStatus]:
"""
Activates the pipeline when starting from STANDBY mode. Only applicable
when the pipeline is starting from a checkpoint in object store.
:param wait: Set True to wait for the pipeline to activate. True by
default
:param timeout_s: The maximum time (in seconds) to wait for the
pipeline to pause.
"""
return self.client.activate_pipeline(self.name, wait=wait, timeout_s=timeout_s)
def start(
self,
bootstrap_policy: Optional[BootstrapPolicy] = None,
silent_bootstrap: bool = False,
concurrent_bootstrap: bool = False,
wait: bool = True,
timeout_s: Optional[float] = None,
dismiss_error: bool = True,
reloadable_tables: Optional[List[str]] = None,
):
"""
.. _start:
Starts this pipeline.
- The pipeline must be in STOPPED state to start.
- If the pipeline is in any other state, an error will be raised.
- If the pipeline is in PAUSED state, use `.meth:resume` instead.
:param bootstrap_policy: The bootstrap policy to use.
:param silent_bootstrap: Set True to bootstrap the pipeline with output connectors disabled. False by default.
:param concurrent_bootstrap: Set True to bootstrap new and modified views
concurrently, keeping the pre-existing views live while the new ones
backfill in the background. Mutually exclusive with
`silent_bootstrap`. False by default.
:param timeout_s: The maximum time (in seconds) to wait for the
pipeline to start.
:param wait: Set True to wait for the pipeline to start. True by default
:param dismiss_error: Set True to dismiss any deployment error before starting;
set False to make it fail in that case. True by default.
:param reloadable_tables: Tables that bootstrapping may reload. Use it
with `bootstrap_policy=BootstrapPolicy.ALLOW` and `wait=True`.
If a table is modified, bootstrapping will reload the table: it truncates the table and
resets its input connectors. Write each name exactly as the pipeline
reports it in the approval diff: an unquoted SQL name in lowercase,
a quoted name without its quotes. For example, write `t1` for
`CREATE TABLE T1` and `MyTable` for `CREATE TABLE "MyTable"`.
None by default: ALLOW bootstraps without a check. If no tables are
acceptable to reload, set `reloadable_tables=[]`.
:raises RuntimeError: If the pipeline is not in STOPPED state.
:raises TableReloadNotAllowedError: If a modified table is not in
`reloadable_tables`, or the pipeline cannot compute the program
diff. The pipeline stays in the AWAITINGAPPROVAL state: call
`approve()` to bootstrap or `stop(force=True)` to reject.
:raises ValueError: If `reloadable_tables` is set with a bootstrap
policy other than ALLOW, or with `wait=False`.
"""
self.client.start_pipeline(
self.name,
bootstrap_policy=bootstrap_policy,
silent_bootstrap=silent_bootstrap,
concurrent_bootstrap=concurrent_bootstrap,
wait=wait,
timeout_s=timeout_s,
dismiss_error=dismiss_error,
reloadable_tables=reloadable_tables,
)
def start_paused(
self,
bootstrap_policy: Optional[BootstrapPolicy] = None,
silent_bootstrap: bool = False,
concurrent_bootstrap: bool = False,
wait: bool = True,
timeout_s: Optional[float] = None,
dismiss_error: bool = True,
reloadable_tables: Optional[List[str]] = None,
):
"""
Starts the pipeline in the paused state.
:param bootstrap_policy: The bootstrap policy to use.
:param silent_bootstrap: Set True to bootstrap the pipeline with output connectors disabled. False by default.
:param concurrent_bootstrap: Set True to bootstrap new and modified views
concurrently, keeping the pre-existing views live while the new ones
backfill in the background. Mutually exclusive with
`silent_bootstrap`. False by default.
:param wait: Set True to wait for the pipeline to start. True by default.
:param timeout_s: The maximum time (in seconds) to wait for the
pipeline to start (defaults to `None` = no timeout is enforced).
:param dismiss_error: Set True to dismiss any deployment error before starting;
set False to make it fail in that case. True by default.
:param reloadable_tables: Tables that bootstrapping may reload. See
:meth:`start`.
"""
return self.client.start_pipeline_as_paused(
self.name,
bootstrap_policy=bootstrap_policy,
silent_bootstrap=silent_bootstrap,
concurrent_bootstrap=concurrent_bootstrap,
wait=wait,
timeout_s=timeout_s,
dismiss_error=dismiss_error,
reloadable_tables=reloadable_tables,
)
def start_standby(
self,
bootstrap_policy: Optional[BootstrapPolicy] = None,
silent_bootstrap: bool = False,
concurrent_bootstrap: bool = False,
wait: bool = True,
timeout_s: Optional[float] = None,
dismiss_error: bool = True,
reloadable_tables: Optional[List[str]] = None,
):
"""
Starts the pipeline in the standby state.
:param bootstrap_policy: The bootstrap policy to use.
:param silent_bootstrap: Set True to bootstrap the pipeline with output connectors disabled. False by default.
:param concurrent_bootstrap: Set True to bootstrap new and modified views
concurrently, keeping the pre-existing views live while the new ones
backfill in the background. Mutually exclusive with
`silent_bootstrap`. False by default.
:param wait: Set True to wait for the pipeline to start. True by default.
:param timeout_s: The maximum time (in seconds) to wait for the
pipeline to start (defaults to `None` = no timeout is enforced).
:param dismiss_error: Set True to dismiss any deployment error before starting;
set False to make it fail in that case. True by default.
:param reloadable_tables: Tables that bootstrapping may reload. See
:meth:`start`.
"""
self.client.start_pipeline_as_standby(
self.name,
bootstrap_policy=bootstrap_policy,
silent_bootstrap=silent_bootstrap,
concurrent_bootstrap=concurrent_bootstrap,
wait=wait,
timeout_s=timeout_s,
dismiss_error=dismiss_error,
reloadable_tables=reloadable_tables,
)
def restart(
self,
bootstrap_policy: Optional[BootstrapPolicy] = None,
silent_bootstrap: bool = False,
concurrent_bootstrap: bool = False,
timeout_s: Optional[float] = None,
dismiss_error: bool = True,
reloadable_tables: Optional[List[str]] = None,
):
"""
Restarts the pipeline.
This method forcibly **STOPS** the pipeline regardless of its current
state and then starts it again. No checkpoints are made when stopping
the pipeline.
:param bootstrap_policy: The bootstrap policy to use.
:param silent_bootstrap: Set True to bootstrap the pipeline with output connectors disabled. False by default.
:param concurrent_bootstrap: Set True to bootstrap new and modified views
concurrently, keeping the pre-existing views live while the new ones
backfill in the background. Mutually exclusive with
`silent_bootstrap`. False by default.
:param timeout_s: The maximum time (in seconds) to wait for the
pipeline to restart.
:param dismiss_error: Set True to dismiss any deployment error before starting;
set False to make it fail in that case. True by default.
:param reloadable_tables: Tables that bootstrapping may reload. See
:meth:`start`.
"""
self.stop(force=True, timeout_s=timeout_s)
self.start(
bootstrap_policy=bootstrap_policy,
silent_bootstrap=silent_bootstrap,
concurrent_bootstrap=concurrent_bootstrap,
timeout_s=timeout_s,
dismiss_error=dismiss_error,
reloadable_tables=reloadable_tables,
)
def pause(self, wait: bool = True, timeout_s: Optional[float] = None):
"""
Pause the pipeline.
The pipeline can only transition to the PAUSED state from the RUNNING
state. If the pipeline is already paused, it will remain in the PAUSED
state.
:param wait: Set True to wait for the pipeline to pause. True by default
:param timeout_s: The maximum time (in seconds) to wait for the
pipeline to pause.
"""
self.client.pause_pipeline(self.name, wait=wait, timeout_s=timeout_s)
def stop(self, force: bool, wait: bool = True, timeout_s: Optional[float] = None):
"""
Stops the pipeline.
Stops the pipeline regardless of its current state.
:param force: Set True to immediately scale compute resources to zero.
Set False to automatically checkpoint before stopping.
:param wait: Set True to gracefully shutdown listeners and wait for the
View remainder of file in raw view
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.