apache_beam.runners.interactive.pipeline_instrument module

Module to instrument interactivity to the given pipeline.

For internal use only; no backwards-compatibility guarantees. This module accesses current interactive environment and analyzes given pipeline to transform original pipeline into a one-shot pipeline with interactivity.

class apache_beam.runners.interactive.pipeline_instrument.PipelineInstrument(pipeline, options=None)[source]

Bases: object

A pipeline instrument for pipeline to be executed by interactive runner.

This module should never depend on underlying runner that interactive runner delegates. It instruments the original instance of pipeline directly by appending or replacing transforms with help of cache. It provides interfaces to recover states of original pipeline. It’s the interactive runner’s responsibility to coordinate supported underlying runners to run the pipeline instrumented and recover the original pipeline states if needed.

instrumented_pipeline_proto()[source]

Always returns a new instance of portable instrumented proto.

has_unbounded_source

Returns whether the pipeline has any REPLACEABLE_UNBOUNDED_SOURCES.

cacheables

Finds cacheable PCollections from the pipeline.

The function only treats the result as cacheables since there is no guarantee whether PCollections that need to be cached have been cached or not. A PCollection needs to be cached when it’s bound to a user defined variable in the source code. Otherwise, the PCollection is not reusable nor introspectable which nullifies the need of cache.

pcolls_to_pcoll_id

Returns a dict mapping str(PCollection)s to IDs.

original_pipeline_proto

Returns the portable proto representation of the pipeline before instrumentation.

original_pipeline

Returns a snapshot of the pipeline before instrumentation.

instrument()[source]

Instruments original pipeline with cache.

For cacheable output PCollection, if cache for the key doesn’t exist, do _write_cache(); for cacheable input PCollection, if cache for the key exists, do _read_cache(). No instrument in any other situation.

Modifies:
self._pipeline
preprocess()[source]

Pre-processes the pipeline.

Since the pipeline instance in the class might not be the same instance defined in the user code, the pre-process will figure out the relationship of cacheable PCollections between these 2 instances by replacing ‘pcoll’ fields in the cacheable dictionary with ones from the running instance.

cache_key(pcoll)[source]

Gets the identifier of a cacheable PCollection in cache.

If the pcoll is not a cacheable, return ‘’. The key is what the pcoll would use as identifier if it’s materialized in cache. It doesn’t mean that there would definitely be such cache already. Also, the pcoll can come from the original user defined pipeline object or an equivalent pcoll from a transformed copy of the original pipeline.

cacheable_var_by_pcoll_id(pcoll_id)[source]

Retrieves the variable name of a PCollection.

In source code, PCollection variables are defined in the user pipeline. When it’s converted to the runner api representation, each PCollection referenced in the user pipeline is assigned a unique-within-pipeline pcoll_id. Given such pcoll_id, retrieves the str variable name defined in user pipeline for that referenced PCollection. If the PCollection is not watched, return None.

apache_beam.runners.interactive.pipeline_instrument.pin(pipeline, options=None)[source]

Creates PipelineInstrument for a pipeline and its options with cache.

This is the shorthand for doing 3 steps: 1) compute once for metadata of the given runner pipeline and everything watched from user pipelines; 2) associate info between the runner pipeline and its corresponding user pipeline, eliminate data from other user pipelines if there are any; 3) mutate the runner pipeline to apply interactivity.

apache_beam.runners.interactive.pipeline_instrument.cacheables(pcolls_to_pcoll_id)[source]

Finds PCollections that need to be cached for analyzed PCollections.

The function only treats the result as cacheables since there is no guarantee whether PCollections that need to be cached have been cached or not. A PCollection needs to be cached when it’s bound to a user defined variable in the source code. Otherwise, the PCollection is not reusable nor introspectable which nullifies the need of cache. There might be multiple pipelines defined and watched, this will only return for PCollections with pcolls_to_pcoll_id analyzed. The check is not strict because pcoll_id is not unique across multiple pipelines. Additional check needs to be done during instrument.

apache_beam.runners.interactive.pipeline_instrument.cacheable_key(pcoll, pcolls_to_pcoll_id, pcoll_version_map=None)[source]
apache_beam.runners.interactive.pipeline_instrument.has_unbounded_source(pipeline)[source]

Checks if a given pipeline has replaceable unbounded sources.

apache_beam.runners.interactive.pipeline_instrument.pcolls_to_pcoll_id(pipeline, original_context)[source]

Returns a dict mapping PCollections string to PCollection IDs.

Using a PipelineVisitor to iterate over every node in the pipeline, records the mapping from PCollections to PCollections IDs. This mapping will be used to query cached PCollections.

Returns:(dict from str to str) a dict mapping str(pcoll) to pcoll_id.