|
| 1 | +# Copyright 2018 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Helper functions for estimating the resources that a vcf_to_bq will require. |
| 15 | +
|
| 16 | +Currently, the resource estimator only estimates the disk usage that a Dataflow |
| 17 | +pipeline will take along with the `MergeVariants` step, since this can cause |
| 18 | +expensive pipeline failures late in the run. |
| 19 | +""" |
| 20 | +import logging |
| 21 | + |
| 22 | +import apache_beam as beam |
| 23 | +from apache_beam import coders |
| 24 | +from apache_beam.io.filesystem import CompressionTypes |
| 25 | +from apache_beam.io.filesystems import FileSystems |
| 26 | + |
| 27 | +from gcp_variant_transforms.beam_io import vcfio |
| 28 | + |
| 29 | + |
| 30 | +# TODO(hanjohn): Add unit tests. |
| 31 | + |
| 32 | +def _convert_variant_snippets_to_bytesize(variant): |
| 33 | + # type: (vcfio.Variant -> int) |
| 34 | + return coders.registry.get_coder(vcfio.Variant).estimate_size(variant) |
| 35 | + |
| 36 | + |
| 37 | +class SnippetSizeInfo(object): |
| 38 | + def __init__(self, |
| 39 | + raw_snippet_size, # type: int |
| 40 | + encoded_snippet_size, # type: int |
| 41 | + ): |
| 42 | + # type: (...) -> (None) |
| 43 | + self.raw = raw_snippet_size |
| 44 | + self.encoded = encoded_snippet_size |
| 45 | + |
| 46 | + |
| 47 | +class FileSizeInfo(object): |
| 48 | + def __init__(self, raw_file_size, encoded_file_size=None): |
| 49 | + # type: (int, int) -> (None) |
| 50 | + self.raw = raw_file_size |
| 51 | + self.encoded = encoded_file_size |
| 52 | + |
| 53 | + def calculateEncodedFileSize(self, snippet_size_info): |
| 54 | + # type: (SnippetSizeInfo) -> (None) |
| 55 | + """Estimate a VCF file's encoded size based on snippet analysis. |
| 56 | +
|
| 57 | + Given the raw_file_size and measurements of several VCF lines from the file, |
| 58 | + estimate how much disk the file will take after expansion due to encoding |
| 59 | + lines as `vcfio.Variant` objects. The encoded_snippet_size will be set as |
| 60 | + `self.encoded`. |
| 61 | +
|
| 62 | + This is a simple ratio problem, solving for encoded_snippet_size which is |
| 63 | + the only unknown: |
| 64 | + encoded_snippet_size / raw_snippet_size = encoded_file_size / raw_file_size |
| 65 | + """ |
| 66 | + if snippet_size_info.raw == 0: |
| 67 | + logging.error("VCF file {} reported with 0 well-formed variant lines; " |
| 68 | + "its contribution to disk resource usage will be " |
| 69 | + "ignored.".format(self.name)) |
| 70 | + self.encoded = 0 |
| 71 | + self.raw = 0 |
| 72 | + else: |
| 73 | + self.encoded = (self.raw * snippet_size_info.encoded / |
| 74 | + snippet_size_info.raw) |
| 75 | + |
| 76 | + |
| 77 | +def measure_variant_size(element): |
| 78 | + # type: (Tuple[str, str, vcfio.Variant]) -> (Tuple[str, SnippetSizeInfo]) |
| 79 | + """Measure the lengths of the raw and encoded representations of a Variant. |
| 80 | +
|
| 81 | + Given a PTable mapping file_paths to the raw (bytestring) and vcfio.Variant- |
| 82 | + encoded representations of a Variant line, have the output PTable instead map |
| 83 | + from the file_paths to a Tuple with the (raw, encoded) representation sizes. |
| 84 | +
|
| 85 | + The file_path keys are not expected to be unique. |
| 86 | + """ |
| 87 | + file_path, raw_variant, encoded_variant = element |
| 88 | + encoded_variant_size = _convert_variant_snippets_to_bytesize(encoded_variant) |
| 89 | + raw_variant_size = len(raw_variant) |
| 90 | + return file_path, SnippetSizeInfo(raw_variant_size, encoded_variant_size) |
| 91 | + |
| 92 | + |
| 93 | +def estimate_file_encoded_size(element): |
| 94 | + # type: (Tuple[str, Dict[str, Object]]) -> (Tuple[str, FileSizeInfo]) |
| 95 | + file_name, metrics = element |
| 96 | + file_size_info = metrics['whole_file_raw_size'][0] # type: FileSizeInfo |
| 97 | + snippet_size_info = metrics['snippet_stats'][0] # type: SnippetSizeInfo |
| 98 | + |
| 99 | + # Assume that the ratio of encoded size to raw disk size is roughly the same |
| 100 | + # throughout the file compared to the first several lines. |
| 101 | + file_size_info.calculateEncodedFileSize(snippet_size_info) |
| 102 | + |
| 103 | + logging.debug("File %s has raw file size %d, raw snippet size %d, encoded " |
| 104 | + "size %d. Estimated encoded file size: %d", |
| 105 | + file_name, file_size_info.raw, snippet_size_info.raw, |
| 106 | + snippet_size_info.encoded, file_size_info.encoded) |
| 107 | + return file_name, file_size_info |
| 108 | + |
| 109 | +def get_file_sizes(input_pattern): |
| 110 | + # type: (str) -> (List[FileSizeInfo]) |
| 111 | + match_results = FileSystems.match([input_pattern]) |
| 112 | + file_sizes = [] |
| 113 | + for match in match_results: |
| 114 | + for file_metadata in match.metadata_list: |
| 115 | + compression_type = CompressionTypes.detect_compression_type( |
| 116 | + file_metadata.path) |
| 117 | + if compression_type != CompressionTypes.UNCOMPRESSED: |
| 118 | + logging.error("VCF file {} is compressed; disk requirement estimator " |
| 119 | + "will not be accurate.", |
| 120 | + file_metadata.path) |
| 121 | + |
| 122 | + file_sizes.append((file_metadata.path, |
| 123 | + FileSizeInfo(file_metadata.size_in_bytes),)) |
| 124 | + return file_sizes |
| 125 | + |
| 126 | + |
| 127 | +class SnippetSizeInfoSumFn(beam.CombineFn): |
| 128 | + """Combiner Function to sum up the size fields of SnippetSizeInfos. |
| 129 | +
|
| 130 | + Example: [SnippetSizeInfo(a, b), SnippetSizeInfo(c, d)] -> |
| 131 | + SnippetSizeInfo(a+c, b+d) |
| 132 | + """ |
| 133 | + def create_accumulator(self): |
| 134 | + # type: (None) -> (Tuple[int, int]) |
| 135 | + return (0, 0) # (raw, encoded) sums |
| 136 | + |
| 137 | + def add_input(self, sums, input): |
| 138 | + # type: (Tuple[int, int], SnippetSizeInfo) -> (Tuple[int, int]) |
| 139 | + return sums[0] + input.raw, sums[1] + input.encoded |
| 140 | + |
| 141 | + def merge_accumulators(self, accumulators): |
| 142 | + # type: (Iterable[Tuple[int, int]]) -> (Tuple[int, int]) |
| 143 | + first, second = zip(*accumulators) |
| 144 | + return sum(first), sum(second) |
| 145 | + |
| 146 | + def extract_output(self, sums): |
| 147 | + # type: (Tuple[int, int]) -> (SnippetSizeInfo) |
| 148 | + return SnippetSizeInfo(*sums) |
| 149 | + |
| 150 | + |
| 151 | +class FileSizeInfoSumFn(beam.CombineFn): |
| 152 | + """Combiner Function to sum up the size fields of Tuple[str, FileSizeInfo]s. |
| 153 | +
|
| 154 | + Unlike SnippetSizeInfoSumFn, the input is a PTable mapping str to |
| 155 | + FileSizeInfo, so the input is a tuple with the FileSizeInfos as the second |
| 156 | + field. The output strips out the str key which represents the file path. |
| 157 | +
|
| 158 | + Example: [('/path/a', FileSizeInfo(a, b)), ('/path/b;, FileSizeInfo(c, d))] -> |
| 159 | + FileSizeInfo(a+c, b+d) |
| 160 | + """ |
| 161 | + def create_accumulator(self): |
| 162 | + # type: (None) -> (Tuple[int, int]) |
| 163 | + return (0, 0) # (raw, encoded) sums |
| 164 | + |
| 165 | + def add_input(self, raw_encoded, input): |
| 166 | + # type: (Tuple[int, int], Tuple[str, FileSizeInfo]) -> (Tuple[int, int]) |
| 167 | + raw, encoded = raw_encoded |
| 168 | + _, file_size_info = input |
| 169 | + return raw + file_size_info.raw, encoded + file_size_info.encoded |
| 170 | + |
| 171 | + def merge_accumulators(self, accumulators): |
| 172 | + # type: (Iterable[Tuple[int, int]]) -> (Tuple[int, int]) |
| 173 | + raw, encoded = zip(*accumulators) |
| 174 | + return sum(raw), sum(encoded) |
| 175 | + |
| 176 | + def extract_output(self, raw_encoded): |
| 177 | + # type: (Tuple[int, int]) -> (FileSizeInfo) |
| 178 | + raw, encoded = raw_encoded |
| 179 | + return FileSizeInfo(raw, encoded) |
0 commit comments