diff --git a/common/include/optional.hpp b/common/include/optional.hpp index dd68e640..abe36329 100644 --- a/common/include/optional.hpp +++ b/common/include/optional.hpp @@ -28,6 +28,7 @@ using std::optional; #else #include +#include namespace datasketches { @@ -115,7 +116,7 @@ class optional { template void emplace(Args&&... args) noexcept(std::is_nothrow_constructible::value) { reset(); // destroys the current value, if any, as std::optional::emplace does - new (&value_) T(args...); + new (&value_) T(std::forward(args)...); initialized_ = true; } diff --git a/quantiles/include/quantiles_sketch.hpp b/quantiles/include/quantiles_sketch.hpp index e995e3e3..b6841e18 100644 --- a/quantiles/include/quantiles_sketch.hpp +++ b/quantiles/include/quantiles_sketch.hpp @@ -213,6 +213,10 @@ class quantiles_sketch { * Updates this sketch with the given data item. * If cross-language portability is required, callers should ensure that * the input string uses a compatible encoding (valid UTF-8). + * If an allocation or a copy of the item throws, or the comparator throws while the item is compared + * with the minimum and maximum items, the item is not added and the sketch is unchanged. + * If the comparator throws while items are sorted or merged, or does not impose a consistent + * total order, the state of the sketch is undefined. * @param item from a stream of items */ template @@ -222,6 +226,9 @@ class quantiles_sketch { * Merges another sketch into this one. * If sketches contain strings, callers are responsible for ensuring that * both sketches were built using compatible string encodings. + * If an allocation fails, this sketch is unchanged. If a copy of an item throws, this sketch + * stays valid and usable, but may hold part of other. If the comparator throws while items + * are sorted or merged, the state of this sketch is undefined. * @param other sketch to merge into this one */ template @@ -532,10 +539,28 @@ class quantiles_sketch { bool is_sorted, const Comparator& comparator = Comparator(), const Allocator& allocator = Allocator()); void grow_base_buffer(); - void process_full_base_buffer(); - - // returns true if size adjusted, else false - bool grow_levels_if_needed(); + // adds an item to the base buffer and counts it; if this throws, the sketch is unchanged, + // unless the comparator throws + template + void insert_base(FwdT&& item); + // the base buffer is full with the new item, which brings n to new_n but is not counted yet + void process_full_base_buffer(uint64_t new_n); + + // adds the levels needed for n items; returns true if size adjusted, else false + bool grow_levels_if_needed(uint64_t n); + // removes the empty levels above those that n needs, which a failed update or merge can leave + // (the iterator expects no levels above the highest level in use) + void trim_levels(); + + // allocates everything a merge into tgt that brings it to new_n items, num_base_items of them + // through the base buffer, needs, so that the rest of the merge allocates nothing in tgt + static void reserve_for_merge(quantiles_sketch& tgt, uint64_t new_n, uint64_t num_base_items); + // compares first, then sets the min and max items of tgt to cover src + template + static void merge_min_max(quantiles_sketch& tgt, FwdSk&& src); + // merge() without the cleanup after a failure; other is not empty + template + void merge_unchecked(FwdSk&& other); // buffers should be pre-sized to target capacity as appropriate template diff --git a/quantiles/include/quantiles_sketch_impl.hpp b/quantiles/include/quantiles_sketch_impl.hpp index a3466f3a..6b28f56b 100644 --- a/quantiles/include/quantiles_sketch_impl.hpp +++ b/quantiles/include/quantiles_sketch_impl.hpp @@ -212,23 +212,41 @@ template template void quantiles_sketch::update(FwdT&& item) { if (!check_update_item(item)) { return; } - if (is_empty()) { - min_item_.emplace(item); - max_item_.emplace(item); - } else { - if (comparator_(item, *min_item_)) *min_item_ = item; - if (comparator_(*max_item_, item)) *max_item_ = item; + // compare and copy first, but change nothing until the item has been fully processed, + // so that an exception leaves the sketch as it was + const bool is_new_min = is_empty() || comparator_(item, *min_item_); + const bool is_new_max = is_empty() || comparator_(*max_item_, item); + optional new_min; + optional new_max; + if (is_new_min) new_min.emplace(item); + if (is_new_max) new_max.emplace(item); + reset_sorted_view(); + try { + insert_base(std::forward(item)); + } catch (...) { + trim_levels(); + throw; } + if (is_new_min) min_item_ = std::move(new_min); + if (is_new_max) max_item_ = std::move(new_max); +} +template +template +void quantiles_sketch::insert_base(FwdT&& item) { + // allocate first // if exceed capacity, grow until size 2k -- assumes eager processing if (base_buffer_.size() + 1 > base_buffer_.capacity()) grow_base_buffer(); - - base_buffer_.push_back(std::forward(item)); + const bool fills_base_buffer = base_buffer_.size() + 1 == 2 * k_; + if (fills_base_buffer) grow_levels_if_needed(n_ + 1); // make sure there will be enough levels for the propagation + + base_buffer_.push_back(std::forward(item)); // a failed copy leaves the base buffer as it was + if (fills_base_buffer) { + process_full_base_buffer(n_ + 1); + } else if (base_buffer_.size() > 1) { + is_base_buffer_sorted_ = false; + } ++n_; - - if (base_buffer_.size() > 1) is_base_buffer_sorted_ = false; - if (base_buffer_.size() == 2 * k_) process_full_base_buffer(); - reset_sorted_view(); } template @@ -236,12 +254,25 @@ template void quantiles_sketch::merge(FwdSk&& other) { if (other.is_empty()) { return; // nothing to do - } else if (!other.is_estimation_mode()) { + } + reset_sorted_view(); + try { + merge_unchecked(std::forward(other)); + } catch (...) { + trim_levels(); // a failed merge can leave levels added for the items it did not merge + throw; + } +} + +template +template +void quantiles_sketch::merge_unchecked(FwdSk&& other) { + if (!other.is_estimation_mode()) { // other is exact, stream in regardless of k - for (auto item : other.base_buffer_) { + reserve_for_merge(*this, n_ + other.n_, other.n_); // allocate first, so that a failure changes nothing + for (auto& item : other.base_buffer_) { update(conditional_forward(item)); } - reset_sorted_view(); return; } @@ -259,18 +290,18 @@ void quantiles_sketch::merge(FwdSk&& other) { } else { // exact or empty quantiles_sketch sk_copy(std::forward(other)); - if (k_ <= other.get_k()) { + if (k_ <= sk_copy.get_k()) { if (!is_empty()) { - for (uint16_t i = 0; i < base_buffer_.size(); ++i) { - sk_copy.update(std::move(base_buffer_[i])); - } + // allocate first, so that no item is moved out of this sketch unless the merge succeeds + reserve_for_merge(sk_copy, sk_copy.get_n() + n_, base_buffer_.size()); + merge_min_max(sk_copy, std::move(*this)); + for (auto& item : base_buffer_) sk_copy.insert_base(std::move(item)); } } else { // k_ > other.get_k() downsampling_merge(sk_copy, std::move(*this)); } *this = std::move(sk_copy); } - reset_sorted_view(); } template @@ -889,10 +920,8 @@ void quantiles_sketch::grow_base_buffer() { } template -void quantiles_sketch::process_full_base_buffer() { - // make sure there will be enough levels for the propagation - grow_levels_if_needed(); // note: n_ was already incremented by update() before this - +void quantiles_sketch::process_full_base_buffer(uint64_t new_n) { + // the levels were grown by the caller: from here on, only moves and comparisons std::sort(base_buffer_.begin(), base_buffer_.end(), comparator_); in_place_propagate_carry(0, levels_[0], // unused here, but 0 is guaranteed to exist @@ -900,26 +929,62 @@ void quantiles_sketch::process_full_base_buffer() { true, *this); base_buffer_.clear(); is_base_buffer_sorted_ = true; - if (n_ / (2 * k_) != bit_pattern_) { - throw std::logic_error("Internal error: n / 2k (" + std::to_string(n_ / 2 * k_) + if (new_n / (2 * k_) != bit_pattern_) { + throw std::logic_error("Internal error: n / 2k (" + std::to_string(new_n / (2 * k_)) + " != bit_pattern " + std::to_string(bit_pattern_)); } } template -bool quantiles_sketch::grow_levels_if_needed() { +bool quantiles_sketch::grow_levels_if_needed(uint64_t n) { + const uint8_t levels_needed = compute_levels_needed(k_, n); + const bool grow = levels_needed > levels_.size(); // levels_needed == 0 is possible during merges + if (grow) { + levels_.reserve(levels_needed); + while (levels_.size() < levels_needed) { + Level empty_level(allocator_); + empty_level.reserve(k_); + levels_.push_back(std::move(empty_level)); + } + } + // Growing levels_ copies the levels instead of moving them if moving a level can throw, + // as it can with some allocators, and a copy of an empty level has no capacity. + // Every level needs capacity k, since zip_buffer() fills a level without allocating. + // This also completes a previous call that failed here. + for (auto& level : levels_) { + if (level.capacity() < k_) level.reserve(k_); + } + return grow; +} + +template +void quantiles_sketch::trim_levels() { const uint8_t levels_needed = compute_levels_needed(k_, n_); - if (levels_needed == 0) - return false; // don't need levels and might have small base buffer. Possible during merges. + while (levels_.size() > levels_needed) levels_.pop_back(); +} - // from here on, assume full size base buffer (2k) and at least one additional level - if (levels_needed <= levels_.size()) - return false; +template +void quantiles_sketch::reserve_for_merge(quantiles_sketch& tgt, uint64_t new_n, uint64_t num_base_items) { + // the base buffer grows only as far as the merge needs; a full base buffer has capacity 2k + const uint64_t base_capacity = std::min(2 * tgt.k_, tgt.base_buffer_.size() + num_base_items); + if (tgt.base_buffer_.capacity() < base_capacity) tgt.base_buffer_.reserve(static_cast(base_capacity)); + tgt.grow_levels_if_needed(new_n); +} - Level empty_level(allocator_); - empty_level.reserve(k_); - levels_.push_back(std::move(empty_level)); - return true; +template +template +void quantiles_sketch::merge_min_max(quantiles_sketch& tgt, FwdSk&& src) { + // can't just check is_empty() since min and max might not have been set if + // there were no base buffer items added via update() + const bool is_new_min = !tgt.min_item_ || tgt.comparator_(*src.min_item_, *tgt.min_item_); + const bool is_new_max = !tgt.max_item_ || tgt.comparator_(*tgt.max_item_, *src.max_item_); + // copy first, so that a failed copy leaves tgt as it was + optional new_min; + optional new_max; + if (is_new_min) new_min.emplace(conditional_forward(*src.min_item_)); + if (is_new_max) new_max.emplace(conditional_forward(*src.max_item_)); + if (is_new_min) tgt.min_item_ = std::move(new_min); + if (is_new_max) tgt.max_item_ = std::move(new_max); } template @@ -939,8 +1004,14 @@ void quantiles_sketch::in_place_propagate_carry(uint8_t starting_level, zip_buffer(buf_size_2k, sketch.levels_[ending_level]); } else { // merge_into version of computation - for (uint16_t i = 0; i < k; ++i) { - sketch.levels_[ending_level].push_back(conditional_forward(buf_size_k[i])); + // the ending level is empty, so a failed copy can be undone by clearing it + try { + for (uint16_t i = 0; i < k; ++i) { + sketch.levels_[ending_level].push_back(conditional_forward(buf_size_k[i])); + } + } catch (...) { + sketch.levels_[ending_level].clear(); + throw; } } @@ -1015,7 +1086,6 @@ void quantiles_sketch::merge_two_size_k_buffers(Level& src_1, Level& sr auto end1 = src_1.end(), end2 = src_2.end(); auto it1 = src_1.begin(), it2 = src_2.begin(); - // TODO: probably actually doing copies given Level&? while (it1 != end1 && it2 != end2) { if (comparator(*it1, *it2)) { dst.push_back(std::move(*it1++)); @@ -1025,13 +1095,16 @@ void quantiles_sketch::merge_two_size_k_buffers(Level& src_1, Level& sr } if (it1 != end1) { - dst.insert(dst.end(), it1, end1); + dst.insert(dst.end(), std::make_move_iterator(it1), std::make_move_iterator(end1)); } else { if (it2 == end2) { throw std::logic_error("it2 unexpectedly already at end of range"); } - dst.insert(dst.end(), it2, end2); + dst.insert(dst.end(), std::make_move_iterator(it2), std::make_move_iterator(end2)); } } +// Both merges allocate everything first, so that an allocation failure leaves tgt unchanged. +// After that, a copy of an item from a const src is the only thing that can throw, +// and n is advanced with each step, so that tgt stays valid if one does. template template void quantiles_sketch::standard_merge(quantiles_sketch& tgt, FwdSk&& src) { @@ -1042,27 +1115,18 @@ void quantiles_sketch::standard_merge(quantiles_sketch& tgt, FwdSk&& sr return; } - uint64_t new_n = src.get_n() + tgt.get_n(); + const uint64_t new_n = src.get_n() + tgt.get_n(); + reserve_for_merge(tgt, new_n, src.base_buffer_.size()); + Level scratch_buf(tgt.allocator_); + scratch_buf.reserve(2 * tgt.get_k()); - // move items from src's base buffer - for (uint16_t i = 0; i < src.base_buffer_.size(); ++i) { - tgt.update(conditional_forward(src.base_buffer_[i])); - } + merge_min_max(tgt, std::forward(src)); - // check (after moving raw items) if we need to extend levels array - uint8_t levels_needed = compute_levels_needed(tgt.get_k(), new_n); - if (levels_needed > tgt.levels_.size()) { - tgt.levels_.reserve(levels_needed); - while (tgt.levels_.size() < levels_needed) { - Level empty_level(tgt.allocator_); - empty_level.reserve(tgt.get_k()); - tgt.levels_.push_back(std::move(empty_level)); - } + // move items from src's base buffer + for (auto& item : src.base_buffer_) { + tgt.insert_base(conditional_forward(item)); } - Level scratch_buf(tgt.allocator_); - scratch_buf.reserve(2 * tgt.get_k()); - uint64_t src_pattern = src.bit_pattern_; for (uint8_t src_lvl = 0; src_pattern != 0; ++src_lvl, src_pattern >>= 1) { if ((src_pattern & 1) > 0) { @@ -1070,31 +1134,14 @@ void quantiles_sketch::standard_merge(quantiles_sketch& tgt, FwdSk&& sr // propagate-carry in_place_propagate_carry(src_lvl, - src.levels_[src_lvl], scratch_buf, + conditional_forward(src.levels_[src_lvl]), scratch_buf, false, tgt); - // update n_ at the end + tgt.n_ += static_cast(2 * tgt.get_k()) << src_lvl; } } - tgt.n_ = new_n; - if ((tgt.get_n() / (2 * tgt.get_k())) != tgt.bit_pattern_) { + if (tgt.n_ != new_n || (tgt.get_n() / (2 * tgt.get_k())) != tgt.bit_pattern_) { throw std::logic_error("Failed internal consistency check after standard_merge()"); } - - // update min and max items - // can't just check is_empty() since min and max might not have been set if - // there were no base buffer items added via update() - if (!tgt.min_item_) { - tgt.min_item_.emplace(conditional_forward(*src.min_item_)); - } else { - if (tgt.comparator_(*src.min_item_, *tgt.min_item_)) - *tgt.min_item_ = conditional_forward(*src.min_item_); - } - if (!tgt.max_item_) { - tgt.max_item_.emplace(conditional_forward(*src.max_item_)); - } else { - if (tgt.comparator_(*tgt.max_item_, *src.max_item_)) - *tgt.max_item_ = conditional_forward(*src.max_item_); - } } template @@ -1111,65 +1158,38 @@ void quantiles_sketch::downsampling_merge(quantiles_sketch& tgt, FwdSk& const uint8_t lg_sample_factor = count_trailing_zeros_in_u32(downsample_factor); const uint64_t new_n = src.get_n() + tgt.get_n(); - - // move items from src's base buffer - for (uint16_t i = 0; i < src.base_buffer_.size(); ++i) { - tgt.update(conditional_forward(src.base_buffer_[i])); - } - - // check (after moving raw items) if we need to extend levels array - const uint8_t levels_needed = compute_levels_needed(tgt.get_k(), new_n); - if (levels_needed > tgt.levels_.size()) { - tgt.levels_.reserve(levels_needed); - while (tgt.levels_.size() < levels_needed) { - Level empty_level(tgt.allocator_); - empty_level.reserve(tgt.get_k()); - tgt.levels_.push_back(std::move(empty_level)); - } - } - + reserve_for_merge(tgt, new_n, src.base_buffer_.size()); Level down_buf(tgt.allocator_); down_buf.reserve(tgt.get_k()); - Level scratch_buf(tgt.allocator_); scratch_buf.reserve(2 * tgt.get_k()); + merge_min_max(tgt, std::forward(src)); + + // move items from src's base buffer + for (auto& item : src.base_buffer_) { + tgt.insert_base(conditional_forward(item)); + } + uint64_t src_pattern = src.bit_pattern_; for (uint8_t src_lvl = 0; src_pattern != 0; ++src_lvl, src_pattern >>= 1) { if ((src_pattern & 1) > 0) { down_buf.clear(); scratch_buf.clear(); - // zip with stride, leaving input buffer intact - zip_buffer_with_stride(src.levels_[src_lvl], down_buf, downsample_factor); + // zip with stride; a failed copy changes only down_buf + zip_buffer_with_stride(conditional_forward(src.levels_[src_lvl]), down_buf, downsample_factor); // propagate-carry in_place_propagate_carry(src_lvl + lg_sample_factor, - down_buf, scratch_buf, + std::move(down_buf), scratch_buf, false, tgt); - // update n_ at the end + tgt.n_ += static_cast(2 * tgt.get_k()) << (src_lvl + lg_sample_factor); } } - tgt.n_ = new_n; - if ((tgt.get_n() / (2 * tgt.get_k())) != tgt.bit_pattern_) { + if (tgt.n_ != new_n || (tgt.get_n() / (2 * tgt.get_k())) != tgt.bit_pattern_) { throw std::logic_error("Failed internal consistency check after downsampling_merge()"); } - - // update min and max items - // can't just check is_empty() since min and max might not have been set if - // there were no base buffer items added via update() - if (!tgt.min_item_) { - tgt.min_item_.emplace(conditional_forward(*src.min_item_)); - } else { - if (tgt.comparator_(*src.min_item_, *tgt.min_item_)) - *tgt.min_item_ = conditional_forward(*src.min_item_); - } - if (!tgt.max_item_) { - tgt.max_item_.emplace(conditional_forward(*src.max_item_)); - } else { - if (tgt.comparator_(*tgt.max_item_, *src.max_item_)) - *tgt.max_item_ = conditional_forward(*src.max_item_); - } } template diff --git a/quantiles/test/CMakeLists.txt b/quantiles/test/CMakeLists.txt index dd999b8c..345eff02 100644 --- a/quantiles/test/CMakeLists.txt +++ b/quantiles/test/CMakeLists.txt @@ -38,6 +38,7 @@ add_test( target_sources(quantiles_test PRIVATE quantiles_sketch_test.cpp + quantiles_sketch_allocation_test.cpp quantiles_compatibility_test.cpp kolmogorov_smirnov_test.cpp ) diff --git a/quantiles/test/quantiles_sketch_allocation_test.cpp b/quantiles/test/quantiles_sketch_allocation_test.cpp new file mode 100644 index 00000000..3be32e64 --- /dev/null +++ b/quantiles/test/quantiles_sketch_allocation_test.cpp @@ -0,0 +1,275 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include +#include +#include +#include + +#include +#include + +namespace datasketches { + +namespace { + +// Counts allocations, and throws std::bad_alloc on the allocation numbered fail_at (counting from 0). +struct allocation_control { + long count = 0; + long fail_at = -1; + void arm(long at) { count = 0; fail_at = at; } + void disarm() { fail_at = -1; } +}; + +template +struct failing_allocator: test_allocator { + allocation_control* control; + template struct rebind { using other = failing_allocator; }; + explicit failing_allocator(allocation_control& c): test_allocator(0), control(&c) {} + template + failing_allocator(const failing_allocator& other): test_allocator(0), control(other.control) {} + T* allocate(std::size_t n, const T* hint = nullptr) { + if (control->fail_at >= 0 && control->count++ == control->fail_at) throw std::bad_alloc(); + return test_allocator::allocate(n, hint); + } +}; + +template +bool operator==(const failing_allocator& a, const failing_allocator& b) { return a.control == b.control; } +template +bool operator!=(const failing_allocator& a, const failing_allocator& b) { return !(a == b); } + +using quantiles_failing_sketch = quantiles_sketch, failing_allocator>; + +quantiles_failing_sketch make_sketch(uint16_t k, int n, int offset, allocation_control& control) { + quantiles_failing_sketch sketch(k, std::less(), failing_allocator(control)); + for (int i = 0; i < n; ++i) sketch.update(static_cast((i * 7919 + offset) % 10007)); + return sketch; +} + +// serialize() sorts the base buffer, which changes how later updates fill it, +// so the bytes of a sketch under test are taken from a copy +template +typename S::vector_bytes bytes_of(const S& sketch) { + S copy(sketch); + return copy.serialize(); +} + +void seed_random(uint32_t seed) { + random_utils::random_bit.seed(seed); + random_utils::rand.seed(seed); +} + +long copies_allowed = -1; // copies left before a copy throws; negative means no limit + +// An item whose copy constructor throws once copies_allowed runs out. +// Each item owns memory from test_allocator, so an item that is never destroyed shows up as a leak. +struct fragile_item { + int* value; + explicit fragile_item(int v): value(test_allocator(0).allocate(1)) { *value = v; } + fragile_item(const fragile_item& other): value(nullptr) { + if (copies_allowed == 0) throw std::runtime_error("copy failed"); + if (copies_allowed > 0) --copies_allowed; + value = test_allocator(0).allocate(1); + *value = *other.value; + } + fragile_item(fragile_item&& other) noexcept: value(other.value) { other.value = nullptr; } + fragile_item& operator=(const fragile_item& other) { + fragile_item copy(other); + std::swap(value, copy.value); + return *this; + } + fragile_item& operator=(fragile_item&& other) noexcept { std::swap(value, other.value); return *this; } + ~fragile_item() { if (value != nullptr) test_allocator(0).deallocate(value, 1); } +}; + +struct fragile_less { + bool operator()(const fragile_item& a, const fragile_item& b) const { return *a.value < *b.value; } +}; + +using quantiles_fragile_sketch = quantiles_sketch>; + +quantiles_fragile_sketch make_fragile_sketch(uint16_t k, int n, int offset) { + quantiles_fragile_sketch sketch(k, fragile_less(), test_allocator(0)); + for (int i = 0; i < n; ++i) sketch.update(fragile_item((i * 7919 + offset) % 10007)); + return sketch; +} + +// the retained items must account for n, and every item must be a live object +void check_consistent(const quantiles_fragile_sketch& sketch) { + uint64_t total_weight = 0; + uint32_t num_items = 0; + for (const auto pair: sketch) { + REQUIRE(pair.first.value != nullptr); + total_weight += pair.second; + ++num_items; + } + REQUIRE(total_weight == sketch.get_n()); + REQUIRE(num_items == sketch.get_num_retained()); + if (!sketch.is_empty()) { + REQUIRE(*sketch.get_min_item().value <= *sketch.get_max_item().value); + } +} + +struct merge_config { uint16_t k; int n; uint16_t other_k; int other_n; }; + +// every path through merge(): same k, a target with larger or smaller k, +// an exact or empty target, and an exact source +const merge_config merge_configs[] = { + {16, 500, 16, 300}, + {32, 500, 16, 300}, + {16, 500, 32, 300}, + {16, 10, 16, 300}, + {32, 10, 16, 300}, + {16, 0, 16, 300}, + {16, 500, 16, 10}, + {16, 0, 16, 10}, +}; + +} // namespace + +TEST_CASE("quantiles sketch allocation: failure at each allocation of the updates", "[quantiles_sketch]") { + allocation_control control; + const uint16_t k = 16; + const int n = 1000; + bool completed = false; + for (long fail_at = 0; !completed; ++fail_at) { + CAPTURE(fail_at); + seed_random(1); + quantiles_failing_sketch sketch(k, std::less(), failing_allocator(control)); + int failed_item = -1; + control.arm(fail_at); + for (int i = 0; i < n; ++i) { + try { + sketch.update(i); + } catch (const std::bad_alloc&) { + failed_item = i; + break; + } + } + control.disarm(); + if (failed_item < 0) { + completed = true; + } else { + REQUIRE(sketch.get_n() == static_cast(failed_item)); + for (int i = failed_item + 1; i < n; ++i) sketch.update(i); // keep going, as an application would + } + // the same as a sketch that never saw the failed item + seed_random(1); + quantiles_failing_sketch expected(k, std::less(), failing_allocator(control)); + for (int i = 0; i < n; ++i) if (i != failed_item) expected.update(i); + REQUIRE(sketch.serialize() == expected.serialize()); + } +} + +TEST_CASE("quantiles sketch allocation: failure at each allocation of a merge", "[quantiles_sketch]") { + allocation_control control; + for (const merge_config& c: merge_configs) { + for (const bool move_other: {false, true}) { + CAPTURE(c.k, c.n, c.other_k, c.other_n, move_other); + seed_random(2); + auto expected = make_sketch(c.k, c.n, 1, control); + expected.merge(make_sketch(c.other_k, c.other_n, 2, control)); + const auto expected_bytes = expected.serialize(); + bool completed = false; + for (long fail_at = 0; !completed; ++fail_at) { + CAPTURE(fail_at); + seed_random(2); + auto sketch = make_sketch(c.k, c.n, 1, control); + auto other = make_sketch(c.other_k, c.other_n, 2, control); + const auto other_copy = other; // for a retry: a copy draws no random bits + const auto bytes_before = bytes_of(sketch); + control.arm(fail_at); + try { + if (move_other) sketch.merge(std::move(other)); else sketch.merge(other); + completed = true; + } catch (const std::bad_alloc&) {} + control.disarm(); + if (!completed) { + REQUIRE(bytes_of(sketch) == bytes_before); // unchanged + // and still usable: the merge can be repeated + sketch.merge(other_copy); + } + REQUIRE(sketch.serialize() == expected_bytes); + } + } + } +} + +TEST_CASE("quantiles sketch allocation: failure at each item copy of the updates", "[quantiles_sketch]") { + test_allocator_total_bytes = 0; + test_allocator_net_allocations = 0; + { + quantiles_fragile_sketch sketch(4, fragile_less(), test_allocator(0)); + for (int i = 0; i < 200; ++i) { + const fragile_item item((i * 7919) % 10007); + const uint64_t n_before = sketch.get_n(); + // fail each copy that this update makes in turn + bool completed = false; + for (long fail_at = 0; !completed; ++fail_at) { + copies_allowed = fail_at; + try { + sketch.update(item); + completed = true; + } catch (const std::runtime_error&) {} + copies_allowed = -1; + REQUIRE(sketch.get_n() == n_before + (completed ? 1 : 0)); + check_consistent(sketch); + } + } + } + REQUIRE(test_allocator_total_bytes == 0); + REQUIRE(test_allocator_net_allocations == 0); +} + +TEST_CASE("quantiles sketch allocation: failure at each item copy of a merge", "[quantiles_sketch]") { + test_allocator_total_bytes = 0; + test_allocator_net_allocations = 0; + for (const merge_config& c: merge_configs) { + CAPTURE(c.k, c.n, c.other_k, c.other_n); + const auto other = make_fragile_sketch(c.other_k, c.other_n, 2); + bool completed = false; + for (long fail_at = 0; !completed; ++fail_at) { + CAPTURE(fail_at); + auto sketch = make_fragile_sketch(c.k, c.n, 1); + const uint64_t n_before = sketch.get_n(); + copies_allowed = fail_at; + try { + sketch.merge(other); + completed = true; + } catch (const std::runtime_error&) {} + copies_allowed = -1; + REQUIRE(sketch.get_n() >= n_before); + REQUIRE(sketch.get_n() <= n_before + other.get_n()); + if (completed) REQUIRE(sketch.get_n() == n_before + other.get_n()); + check_consistent(sketch); + check_consistent(other); + REQUIRE(other.get_n() == static_cast(c.other_n)); + // the sketch stays usable + sketch.merge(other); + sketch.update(fragile_item(-1)); + check_consistent(sketch); + REQUIRE(*sketch.get_min_item().value == -1); + } + } + REQUIRE(test_allocator_total_bytes == 0); + REQUIRE(test_allocator_net_allocations == 0); +} + +} /* namespace datasketches */