-
Notifications
You must be signed in to change notification settings - Fork 648
feat: retry merge_insert when possible
#3614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
wjones127
merged 13 commits into
lance-format:main
from
wjones127:merge-insert-conflict
Apr 24, 2025
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1aa62b4
feat: retry merge_insert when possible
wjones127 84eb1c9
cleanup
wjones127 d51fb3f
cleanup
wjones127 9da4562
update python lockfile
wjones127 256e92c
Use bufreader and bufwriter
wjones127 84ba0d6
memory utility
wjones127 c0943e9
buffer some data in memory
wjones127 7a79c5d
get spill tested
wjones127 112ae18
add column
wjones127 4383db0
update error message
wjones127 d8cd445
pr feedback on conflict check
wjones127 11087aa
pr feedback
wjones127 8c44511
rename
wjones127 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright The Lance Authors | ||
|
|
||
| use std::collections::HashSet; | ||
|
|
||
| use arrow_array::{Array, RecordBatch}; | ||
| use arrow_data::ArrayData; | ||
|
|
||
| /// Counts memory used by buffers of Arrow arrays and RecordBatches. | ||
| /// | ||
| /// This is meant to capture how much memory is being used by the Arrow data | ||
| /// structures as they are. It does not represent the memory used if the data | ||
| /// were to be serialized and then deserialized. In particular: | ||
| /// | ||
| /// * This does not double count memory used by buffers shared by multiple | ||
| /// arrays or batches. Round-tripped data may use more memory because of this. | ||
| /// * This counts the **total** size of the buffers, even if the array is a slice. | ||
| /// Round-tripped data may use less memory because of this. | ||
| #[derive(Default)] | ||
| pub struct MemoryAccumulator { | ||
| seen: HashSet<usize>, | ||
| total: usize, | ||
| } | ||
|
|
||
| impl MemoryAccumulator { | ||
| pub fn record_array(&mut self, array: &dyn Array) { | ||
| let data = array.to_data(); | ||
| self.record_array_data(&data); | ||
| } | ||
|
|
||
| fn record_array_data(&mut self, data: &ArrayData) { | ||
| for buffer in data.buffers() { | ||
| let ptr = buffer.as_ptr(); | ||
| if self.seen.insert(ptr as usize) { | ||
| self.total += buffer.capacity(); | ||
| } | ||
| } | ||
|
|
||
| if let Some(nulls) = data.nulls() { | ||
| let null_buf = nulls.inner().inner(); | ||
| let ptr = null_buf.as_ptr(); | ||
| if self.seen.insert(ptr as usize) { | ||
| self.total += null_buf.capacity(); | ||
| } | ||
| } | ||
|
|
||
| for child in data.child_data() { | ||
| self.record_array_data(child); | ||
| } | ||
| } | ||
|
|
||
| pub fn record_batch(&mut self, batch: &RecordBatch) { | ||
| for array in batch.columns() { | ||
| self.record_array(array); | ||
| } | ||
| } | ||
|
|
||
| pub fn total(&self) -> usize { | ||
| self.total | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::sync::Arc; | ||
|
|
||
| use arrow_array::Int32Array; | ||
| use arrow_schema::{DataType, Field, Schema}; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_memory_accumulator() { | ||
| let batch = RecordBatch::try_new( | ||
| Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)])), | ||
| vec![Arc::new(Int32Array::from(vec![1, 2, 3]))], | ||
| ) | ||
| .unwrap(); | ||
| let slice = batch.slice(1, 2); | ||
|
|
||
| let mut acc = MemoryAccumulator::default(); | ||
|
|
||
| // Should record whole buffer, not just slice | ||
| acc.record_batch(&slice); | ||
| assert_eq!(acc.total(), 3 * std::mem::size_of::<i32>()); | ||
|
|
||
| // Should not double count | ||
| acc.record_batch(&slice); | ||
| assert_eq!(acc.total(), 3 * std::mem::size_of::<i32>()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| use rand::Rng; | ||
| use std::time::Duration; | ||
|
|
||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright The Lance Authors | ||
|
|
||
| /// Computes backoff as | ||
| /// | ||
| /// ```text | ||
| /// backoff = base^attempt * unit + jitter | ||
| /// ``` | ||
| /// | ||
| /// The defaults are base=2, unit=50ms, jitter=50ms, min=0ms, max=5s. This gives | ||
| /// a backoff of 50ms, 100ms, 200ms, 400ms, 800ms, 1.6s, 3.2s, 5s, (not including jitter). | ||
| /// | ||
| /// You can have non-exponential backoff by setting base=1. | ||
| pub struct Backoff { | ||
| base: u32, | ||
| unit: u32, | ||
| jitter: i32, | ||
| min: u32, | ||
| max: u32, | ||
| attempt: u32, | ||
| } | ||
|
|
||
| impl Default for Backoff { | ||
| fn default() -> Self { | ||
| Self { | ||
| base: 2, | ||
| unit: 50, | ||
| jitter: 50, | ||
| min: 0, | ||
| max: 5000, | ||
| attempt: 0, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Backoff { | ||
| pub fn with_base(self, base: u32) -> Self { | ||
| Self { base, ..self } | ||
| } | ||
|
|
||
| pub fn with_jitter(self, jitter: i32) -> Self { | ||
| Self { jitter, ..self } | ||
| } | ||
|
|
||
| pub fn with_min(self, min: u32) -> Self { | ||
| Self { min, ..self } | ||
| } | ||
|
|
||
| pub fn with_max(self, max: u32) -> Self { | ||
| Self { max, ..self } | ||
| } | ||
|
|
||
| pub fn next_backoff(&mut self) -> Duration { | ||
| let backoff = self | ||
| .base | ||
| .saturating_pow(self.attempt) | ||
| .saturating_mul(self.unit); | ||
| let jitter = rand::thread_rng().gen_range(-self.jitter..=self.jitter); | ||
| let backoff = (backoff.saturating_add_signed(jitter)).clamp(self.min, self.max); | ||
| self.attempt += 1; | ||
| Duration::from_millis(backoff as u64) | ||
| } | ||
|
|
||
| pub fn attempt(&self) -> u32 { | ||
| self.attempt | ||
| } | ||
|
|
||
| pub fn reset(&mut self) { | ||
| self.attempt = 0; | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_backoff() { | ||
| let mut backoff = Backoff::default().with_jitter(0); | ||
| assert_eq!(backoff.next_backoff().as_millis(), 50); | ||
| assert_eq!(backoff.attempt(), 1); | ||
| assert_eq!(backoff.next_backoff().as_millis(), 100); | ||
| assert_eq!(backoff.attempt(), 2); | ||
| assert_eq!(backoff.next_backoff().as_millis(), 200); | ||
| assert_eq!(backoff.attempt(), 3); | ||
| assert_eq!(backoff.next_backoff().as_millis(), 400); | ||
| assert_eq!(backoff.attempt(), 4); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the motivation to use this instead of
get_array_memory_size? Is it because you are worried about shared buffers?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, shared / sliced buffers. Worried we or the user might have something that sliced the input data into smaller batches. Naively using
get_array_memory_sizewill double count in those cases, as SaintBacchus found while attempting this PR: #3435 (comment)