|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with 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, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use std::collections::HashMap; |
| 19 | + |
| 20 | +use uuid::Uuid; |
| 21 | + |
| 22 | +use super::snapshot::{ |
| 23 | + DefaultManifestProcess, MergeManifestProcess, SnapshotProduceAction, SnapshotProduceOperation, |
| 24 | +}; |
| 25 | +use super::{ |
| 26 | + Transaction, MANIFEST_MERGE_ENABLED, MANIFEST_MERGE_ENABLED_DEFAULT, MANIFEST_MIN_MERGE_COUNT, |
| 27 | + MANIFEST_MIN_MERGE_COUNT_DEFAULT, MANIFEST_TARGET_SIZE_BYTES, |
| 28 | + MANIFEST_TARGET_SIZE_BYTES_DEFAULT, |
| 29 | +}; |
| 30 | +use crate::error::Result; |
| 31 | +use crate::spec::{DataFile, ManifestEntry, ManifestFile, Operation}; |
| 32 | +use crate::transaction::rewrite_files::RewriteFilesOperation; |
| 33 | + |
| 34 | +pub const USE_STARTING_SEQUENCE_NUMBER: &str = "use-starting-sequence-number"; |
| 35 | +pub const USE_STARTING_SEQUENCE_NUMBER_DEFAULT: bool = true; |
| 36 | + |
| 37 | +/// Transaction action for rewriting files. |
| 38 | +pub struct OverwriteFilesAction<'a> { |
| 39 | + snapshot_produce_action: SnapshotProduceAction<'a>, |
| 40 | + target_size_bytes: u32, |
| 41 | + min_count_to_merge: u32, |
| 42 | + merge_enabled: bool, |
| 43 | +} |
| 44 | + |
| 45 | +struct OverwriteFilesOperation { |
| 46 | + inner: RewriteFilesOperation, |
| 47 | +} |
| 48 | + |
| 49 | +impl<'a> OverwriteFilesAction<'a> { |
| 50 | + pub fn new( |
| 51 | + tx: Transaction<'a>, |
| 52 | + snapshot_id: i64, |
| 53 | + commit_uuid: Uuid, |
| 54 | + key_metadata: Vec<u8>, |
| 55 | + snapshot_properties: HashMap<String, String>, |
| 56 | + ) -> Result<Self> { |
| 57 | + let target_size_bytes: u32 = tx |
| 58 | + .current_table |
| 59 | + .metadata() |
| 60 | + .properties() |
| 61 | + .get(MANIFEST_TARGET_SIZE_BYTES) |
| 62 | + .and_then(|s| s.parse().ok()) |
| 63 | + .unwrap_or(MANIFEST_TARGET_SIZE_BYTES_DEFAULT); |
| 64 | + |
| 65 | + let min_count_to_merge: u32 = tx |
| 66 | + .current_table |
| 67 | + .metadata() |
| 68 | + .properties() |
| 69 | + .get(MANIFEST_MIN_MERGE_COUNT) |
| 70 | + .and_then(|s| s.parse().ok()) |
| 71 | + .unwrap_or(MANIFEST_MIN_MERGE_COUNT_DEFAULT); |
| 72 | + |
| 73 | + let merge_enabled = tx |
| 74 | + .current_table |
| 75 | + .metadata() |
| 76 | + .properties() |
| 77 | + .get(MANIFEST_MERGE_ENABLED) |
| 78 | + .and_then(|s| s.parse().ok()) |
| 79 | + .unwrap_or(MANIFEST_MERGE_ENABLED_DEFAULT); |
| 80 | + |
| 81 | + let snapshot_produce_action = SnapshotProduceAction::new( |
| 82 | + tx, |
| 83 | + snapshot_id, |
| 84 | + key_metadata, |
| 85 | + commit_uuid, |
| 86 | + snapshot_properties, |
| 87 | + ) |
| 88 | + .unwrap(); |
| 89 | + |
| 90 | + Ok(Self { |
| 91 | + snapshot_produce_action, |
| 92 | + target_size_bytes, |
| 93 | + min_count_to_merge, |
| 94 | + merge_enabled, |
| 95 | + }) |
| 96 | + } |
| 97 | + |
| 98 | + /// Add data files to the snapshot. |
| 99 | +
|
| 100 | + pub fn add_data_files( |
| 101 | + mut self, |
| 102 | + data_files: impl IntoIterator<Item = DataFile>, |
| 103 | + ) -> Result<Self> { |
| 104 | + self.snapshot_produce_action.add_data_files(data_files)?; |
| 105 | + Ok(self) |
| 106 | + } |
| 107 | + |
| 108 | + /// Add remove files to the snapshot. |
| 109 | + pub fn delete_files( |
| 110 | + mut self, |
| 111 | + remove_data_files: impl IntoIterator<Item = DataFile>, |
| 112 | + ) -> Result<Self> { |
| 113 | + self.snapshot_produce_action |
| 114 | + .delete_files(remove_data_files)?; |
| 115 | + Ok(self) |
| 116 | + } |
| 117 | + |
| 118 | + /// Finished building the action and apply it to the transaction. |
| 119 | + pub async fn apply(self) -> Result<Transaction<'a>> { |
| 120 | + let inner = RewriteFilesOperation; |
| 121 | + |
| 122 | + if self.merge_enabled { |
| 123 | + let process = |
| 124 | + MergeManifestProcess::new(self.target_size_bytes, self.min_count_to_merge); |
| 125 | + self.snapshot_produce_action |
| 126 | + .apply(OverwriteFilesOperation { inner }, process) |
| 127 | + .await |
| 128 | + } else { |
| 129 | + self.snapshot_produce_action |
| 130 | + .apply(OverwriteFilesOperation { inner }, DefaultManifestProcess) |
| 131 | + .await |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + pub fn with_starting_sequence_number(mut self, seq: i64) -> Result<Self> { |
| 136 | + // If the compaction should use the sequence number of the snapshot at compaction start time for |
| 137 | + // new data files, instead of using the sequence number of the newly produced snapshot. |
| 138 | + // This avoids commit conflicts with updates that add newer equality deletes at a higher sequence number. |
| 139 | + let use_starting_sequence_number = self |
| 140 | + .snapshot_produce_action |
| 141 | + .tx |
| 142 | + .current_table |
| 143 | + .metadata() |
| 144 | + .properties() |
| 145 | + .get(USE_STARTING_SEQUENCE_NUMBER) |
| 146 | + .and_then(|s| s.parse().ok()) |
| 147 | + .unwrap_or(USE_STARTING_SEQUENCE_NUMBER_DEFAULT); |
| 148 | + |
| 149 | + if !use_starting_sequence_number { |
| 150 | + return Err(crate::error::Error::new( |
| 151 | + crate::ErrorKind::Unexpected, |
| 152 | + "Cannot set data file sequence number when use-starting-sequence-number is false" |
| 153 | + .to_string(), |
| 154 | + )); |
| 155 | + } |
| 156 | + |
| 157 | + self.snapshot_produce_action |
| 158 | + .set_new_data_file_sequence_number(seq); |
| 159 | + |
| 160 | + Ok(self) |
| 161 | + } |
| 162 | + |
| 163 | + pub fn with_starting_sequence_number_from_branch(mut self, branch: &str) -> Result<Self> { |
| 164 | + // If the compaction should use the sequence number of the snapshot at compaction start time for |
| 165 | + // new data files, instead of using the sequence number of the newly produced snapshot. |
| 166 | + // This avoids commit conflicts with updates that add newer equality deletes at a higher sequence number. |
| 167 | + let use_starting_sequence_number = self |
| 168 | + .snapshot_produce_action |
| 169 | + .tx |
| 170 | + .current_table |
| 171 | + .metadata() |
| 172 | + .properties() |
| 173 | + .get(USE_STARTING_SEQUENCE_NUMBER) |
| 174 | + .and_then(|s| s.parse().ok()) |
| 175 | + .unwrap_or(USE_STARTING_SEQUENCE_NUMBER_DEFAULT); |
| 176 | + |
| 177 | + if !use_starting_sequence_number { |
| 178 | + return Err(crate::error::Error::new( |
| 179 | + crate::ErrorKind::Unexpected, |
| 180 | + "Cannot set data file sequence number when use-starting-sequence-number is false" |
| 181 | + .to_string(), |
| 182 | + )); |
| 183 | + } |
| 184 | + |
| 185 | + if let Some(snapshot) = self |
| 186 | + .snapshot_produce_action |
| 187 | + .tx |
| 188 | + .current_table |
| 189 | + .metadata() |
| 190 | + .snapshot_for_ref(branch) |
| 191 | + { |
| 192 | + self.snapshot_produce_action |
| 193 | + .set_new_data_file_sequence_number(snapshot.sequence_number()); |
| 194 | + } |
| 195 | + |
| 196 | + Ok(self) |
| 197 | + } |
| 198 | + |
| 199 | + pub fn with_to_branch(mut self, to_branch: String) -> Self { |
| 200 | + self.snapshot_produce_action.set_target_branch(to_branch); |
| 201 | + self |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +impl SnapshotProduceOperation for OverwriteFilesOperation { |
| 206 | + fn operation(&self) -> Operation { |
| 207 | + Operation::Overwrite |
| 208 | + } |
| 209 | + |
| 210 | + async fn delete_entries( |
| 211 | + &self, |
| 212 | + snapshot_produce: &SnapshotProduceAction<'_>, |
| 213 | + ) -> Result<Vec<ManifestEntry>> { |
| 214 | + self.inner.delete_entries(snapshot_produce).await |
| 215 | + } |
| 216 | + |
| 217 | + async fn existing_manifest( |
| 218 | + &self, |
| 219 | + snapshot_produce: &mut SnapshotProduceAction<'_>, |
| 220 | + ) -> Result<Vec<ManifestFile>> { |
| 221 | + self.inner.existing_manifest(snapshot_produce).await |
| 222 | + } |
| 223 | +} |
0 commit comments