github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/runners/interactive/caching/read_cache.py (about) 1 # 2 # Licensed to the Apache Software Foundation (ASF) under one or more 3 # contributor license agreements. See the NOTICE file distributed with 4 # this work for additional information regarding copyright ownership. 5 # The ASF licenses this file to You under the Apache License, Version 2.0 6 # (the "License"); you may not use this file except in compliance with 7 # the License. You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 # 17 18 """Module to read cache of computed PCollections. 19 20 For internal use only; no backward-compatibility guarantees. 21 """ 22 # pytype: skip-file 23 24 from typing import Tuple 25 26 import apache_beam as beam 27 from apache_beam.portability.api import beam_runner_api_pb2 28 from apache_beam.runners.interactive import cache_manager as cache 29 from apache_beam.runners.interactive.caching.cacheable import Cacheable 30 from apache_beam.runners.interactive.caching.reify import unreify_from_cache 31 from apache_beam.runners.pipeline_context import PipelineContext 32 from apache_beam.transforms.ptransform import PTransform 33 34 35 class ReadCache: 36 """Class that facilitates reading cache of computed PCollections. 37 """ 38 def __init__( 39 self, 40 pipeline: beam_runner_api_pb2.Pipeline, 41 context: PipelineContext, 42 cache_manager: cache.CacheManager, 43 cacheable: Cacheable): 44 self._pipeline = pipeline 45 self._context = context 46 self._cache_manager = cache_manager 47 self._cacheable = cacheable 48 self._key = repr(cacheable.to_key()) 49 50 def read_cache(self) -> Tuple[str, str]: 51 """Reads cache of the cacheable PCollection and wires the cache into the 52 pipeline proto. Returns the pipeline-scoped ids of the cacheable PCollection 53 and the cache reading output PCollection that replaces it. 54 55 First, it creates a temporary pipeline instance on top of the existing 56 component_id_map from the self._pipeline's context so that both pipelines 57 share the context and have no conflict component ids. 58 Second, it instantiates a _ReadCacheTransform to build the temporary 59 pipeline with a subgraph under top level transforms that reads the cache of 60 a cacheable PCollection. 61 Third, it copies components of the subgraph from the temporary pipeline to 62 self._pipeline, skipping components that are not in the temporary pipeline 63 but presents in the component_id_map of self._pipeline. Since to_runner_api 64 generates components for all entries in the component_id_map, those 65 component ids from the context shared by self._pipeline need to be ignored. 66 Last, it replaces inputs of all transforms that consume the cacheable 67 PCollection with the output PCollection of the _ReadCacheTransform so that 68 the whole pipeline computes with data from the cache. The pipeline 69 fragment of reading the cacheable PCollection is now disconnected from the 70 rest of the pipeline and can be pruned later. 71 """ 72 template, read_output = self._build_runner_api_template() 73 output_id = self._context.pcollections.get_id(read_output) 74 source_id = self._context.pcollections.get_id(self._cacheable.pcoll) 75 # Copy cache reading subgraph from the template to the pipeline proto. 76 for pcoll_id in template.components.pcollections: 77 if pcoll_id in self._pipeline.components.pcollections: 78 continue 79 self._pipeline.components.pcollections[pcoll_id].CopyFrom( 80 template.components.pcollections[pcoll_id]) 81 for coder_id in template.components.coders: 82 if coder_id in self._pipeline.components.coders: 83 continue 84 self._pipeline.components.coders[coder_id].CopyFrom( 85 template.components.coders[coder_id]) 86 for windowing_strategy_id in template.components.windowing_strategies: 87 if (windowing_strategy_id in 88 self._pipeline.components.windowing_strategies): 89 continue 90 self._pipeline.components.windowing_strategies[ 91 windowing_strategy_id].CopyFrom( 92 template.components.windowing_strategies[windowing_strategy_id]) 93 template_root_transform_id = template.root_transform_ids[0] 94 root_transform_id = self._pipeline.root_transform_ids[0] 95 for transform_id in template.components.transforms: 96 if (transform_id == template_root_transform_id or 97 transform_id in self._pipeline.components.transforms): 98 continue 99 self._pipeline.components.transforms[transform_id].CopyFrom( 100 template.components.transforms[transform_id]) 101 self._pipeline.components.transforms[ 102 root_transform_id].subtransforms.extend( 103 template.components.transforms[template_root_transform_id]. 104 subtransforms) 105 106 # Replace all the input pcoll of source_id with output pcoll of output_id 107 # from cache reading. 108 for transform in self._pipeline.components.transforms.values(): 109 inputs = transform.inputs 110 if source_id in inputs.values(): 111 keys_need_replacement = set() 112 for key in inputs: 113 if inputs[key] == source_id: 114 keys_need_replacement.add(key) 115 for key in keys_need_replacement: 116 inputs[key] = output_id 117 118 return source_id, output_id 119 120 def _build_runner_api_template( 121 self) -> Tuple[beam_runner_api_pb2.Pipeline, beam.pvalue.PCollection]: 122 transform = _ReadCacheTransform(self._cache_manager, self._key) 123 tmp_pipeline = beam.Pipeline() 124 tmp_pipeline.component_id_map = self._context.component_id_map 125 read_output = tmp_pipeline | 'source_cache_' >> transform 126 return tmp_pipeline.to_runner_api(), read_output 127 128 129 class _ReadCacheTransform(PTransform): 130 """A composite transform encapsulates reading cache of PCollections. 131 """ 132 def __init__(self, cache_manager: cache.CacheManager, key: str): 133 self._cache_manager = cache_manager 134 self._key = key 135 136 def expand(self, pcoll: beam.pvalue.PCollection) -> beam.pvalue.PCollection: 137 return unreify_from_cache( 138 pipeline=pcoll.pipeline, 139 cache_key=self._key, 140 cache_manager=self._cache_manager)