The `compaction_strategy_state` class holds strategy specific state via a `std::variant` containing different state types. When a compaction strategy performs compaction, it retrieves a reference to its state from the `compaction_strategy_state` object. If the table's compaction strategy is ALTERed while a compaction is in progress, the `compaction_strategy_state` object gets replaced, destroying the old state. This leaves the ongoing compaction holding a dangling reference, resulting in a use after free. Fix this by using `seastar::shared_ptr` for the state variant alternatives(`leveled_compaction_strategy_state_ptr` and `time_window_compaction_strategy_state_ptr`). The compaction strategies now hold a copy of the shared_ptr, ensuring the state remains valid for the duration of the compaction even if the strategy is altered. The `compaction_strategy_state` itself is still passed by reference and only the variant alternatives use shared_ptrs. This allows ongoing compactions to retain ownership of the state independently of the wrapper's lifetime. Fixes #25913 Signed-off-by: Lakshmi Narayanan Sreethar <lakshmi.sreethar@scylladb.com>
85 lines
3.2 KiB
C++
85 lines
3.2 KiB
C++
/*
|
|
* Copyright (C) 2017-present ScyllaDB
|
|
*/
|
|
|
|
/*
|
|
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <optional>
|
|
#include <vector>
|
|
#include <map>
|
|
#include <memory>
|
|
|
|
#include <seastar/core/sstring.hh>
|
|
|
|
#include "compaction_strategy_type.hh"
|
|
#include "size_tiered_compaction_strategy.hh"
|
|
#include "compaction_strategy_impl.hh"
|
|
#include "compaction_backlog_manager.hh"
|
|
#include "sstables/shared_sstable.hh"
|
|
|
|
namespace sstables {
|
|
class sstable_set_impl;
|
|
}
|
|
|
|
namespace compaction {
|
|
|
|
class leveled_manifest;
|
|
|
|
struct leveled_compaction_strategy_state {
|
|
std::optional<std::vector<std::optional<dht::decorated_key>>> last_compacted_keys;
|
|
std::vector<int> compaction_counter;
|
|
|
|
leveled_compaction_strategy_state();
|
|
};
|
|
|
|
using leveled_compaction_strategy_state_ptr = seastar::shared_ptr<leveled_compaction_strategy_state>;
|
|
|
|
class leveled_compaction_strategy : public compaction_strategy_impl {
|
|
static constexpr int32_t DEFAULT_MAX_SSTABLE_SIZE_IN_MB = 160;
|
|
static constexpr auto SSTABLE_SIZE_OPTION = "sstable_size_in_mb";
|
|
|
|
int32_t _max_sstable_size_in_mb = DEFAULT_MAX_SSTABLE_SIZE_IN_MB;
|
|
size_tiered_compaction_strategy_options _stcs_options;
|
|
private:
|
|
int32_t calculate_max_sstable_size_in_mb(std::optional<sstring> option_value) const;
|
|
|
|
leveled_compaction_strategy_state_ptr get_state(compaction_group_view& table_s) const;
|
|
public:
|
|
static unsigned ideal_level_for_input(const std::vector<sstables::shared_sstable>& input, uint64_t max_sstable_size);
|
|
static void validate_options(const std::map<sstring, sstring>& options, std::map<sstring, sstring>& unchecked_options);
|
|
|
|
leveled_compaction_strategy(const std::map<sstring, sstring>& options);
|
|
virtual future<compaction_descriptor> get_sstables_for_compaction(compaction_group_view& table_s, strategy_control& control) override;
|
|
|
|
virtual std::vector<compaction_descriptor> get_cleanup_compaction_jobs(compaction_group_view& table_s, std::vector<sstables::shared_sstable> candidates) const override;
|
|
|
|
virtual compaction_descriptor get_major_compaction_job(compaction_group_view& table_s, std::vector<sstables::shared_sstable> candidates) override;
|
|
|
|
virtual void notify_completion(compaction_group_view& table_s, const std::vector<sstables::shared_sstable>& removed, const std::vector<sstables::shared_sstable>& added) override;
|
|
|
|
// for each level > 0, get newest sstable and use its last key as last
|
|
// compacted key for the previous level.
|
|
void generate_last_compacted_keys(leveled_compaction_strategy_state&, leveled_manifest& manifest);
|
|
|
|
virtual future<int64_t> estimated_pending_compactions(compaction_group_view& table_s) const override;
|
|
|
|
virtual bool parallel_compaction() const override {
|
|
return false;
|
|
}
|
|
|
|
virtual compaction_strategy_type type() const override {
|
|
return compaction_strategy_type::leveled;
|
|
}
|
|
virtual std::unique_ptr<sstables::sstable_set_impl> make_sstable_set(const compaction_group_view& ts) const override;
|
|
|
|
virtual std::unique_ptr<compaction_backlog_tracker::impl> make_backlog_tracker() const override;
|
|
|
|
virtual compaction_descriptor get_reshaping_job(std::vector<sstables::shared_sstable> input, schema_ptr schema, reshape_config cfg) const override;
|
|
};
|
|
|
|
}
|