2020-02-17 18:07:22 +00:00
|
|
|
#include <Storages/MergeTree/ReplicatedMergeTreeAltersSequence.h>
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2022-06-15 20:08:45 +00:00
|
|
|
int ReplicatedMergeTreeAltersSequence::getHeadAlterVersion(std::unique_lock<std::mutex> & /*state_lock*/) const
|
2020-02-17 18:07:22 +00:00
|
|
|
{
|
|
|
|
/// If queue empty, than we don't have version
|
|
|
|
if (!queue_state.empty())
|
|
|
|
return queue_state.begin()->first;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReplicatedMergeTreeAltersSequence::addMutationForAlter(int alter_version, std::lock_guard<std::mutex> & /*state_lock*/)
|
|
|
|
{
|
|
|
|
/// Metadata alter can be added before, or
|
|
|
|
/// maybe already finished if we startup after metadata alter was finished.
|
2022-04-18 10:18:43 +00:00
|
|
|
if (!queue_state.contains(alter_version))
|
2020-02-17 18:07:22 +00:00
|
|
|
queue_state.emplace(alter_version, AlterState{.metadata_finished=true, .data_finished=false});
|
|
|
|
else
|
|
|
|
queue_state[alter_version].data_finished = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReplicatedMergeTreeAltersSequence::addMetadataAlter(
|
2020-12-25 15:47:17 +00:00
|
|
|
int alter_version, std::lock_guard<std::mutex> & /*state_lock*/)
|
2020-02-17 18:07:22 +00:00
|
|
|
{
|
2020-12-25 15:47:17 +00:00
|
|
|
/// Data alter (mutation) always added before. See ReplicatedMergeTreeQueue::pullLogsToQueue.
|
2020-12-29 14:57:44 +00:00
|
|
|
/// So mutation already added to this sequence or doesn't exist.
|
2022-04-18 10:18:43 +00:00
|
|
|
if (!queue_state.contains(alter_version))
|
2020-12-25 15:47:17 +00:00
|
|
|
queue_state.emplace(alter_version, AlterState{.metadata_finished=false, .data_finished=true});
|
|
|
|
else
|
2020-02-17 18:07:22 +00:00
|
|
|
queue_state[alter_version].metadata_finished = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReplicatedMergeTreeAltersSequence::finishMetadataAlter(int alter_version, std::unique_lock<std::mutex> & /*state_lock*/)
|
|
|
|
{
|
2020-02-19 08:45:09 +00:00
|
|
|
/// Sequence must not be empty
|
2020-02-17 18:07:22 +00:00
|
|
|
assert(!queue_state.empty());
|
2020-02-19 08:45:09 +00:00
|
|
|
/// Alters have to be finished in order
|
2020-02-17 18:07:22 +00:00
|
|
|
assert(queue_state.begin()->first == alter_version);
|
|
|
|
|
|
|
|
/// If metadata stage finished (or was never added) than we can remove this alter
|
|
|
|
if (queue_state[alter_version].data_finished)
|
|
|
|
queue_state.erase(alter_version);
|
|
|
|
else
|
|
|
|
queue_state[alter_version].metadata_finished = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReplicatedMergeTreeAltersSequence::finishDataAlter(int alter_version, std::lock_guard<std::mutex> & /*state_lock*/)
|
|
|
|
{
|
2020-02-19 08:45:09 +00:00
|
|
|
/// Queue can be empty after load of finished mutation without move of mutation pointer
|
2020-02-17 18:07:22 +00:00
|
|
|
if (queue_state.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
/// Mutations may finish multiple times (for example, after server restart, before update of mutation pointer)
|
|
|
|
if (alter_version >= queue_state.begin()->first)
|
|
|
|
{
|
2020-02-19 08:45:09 +00:00
|
|
|
/// All alter versions bigger than head must present in queue.
|
2022-04-18 10:18:43 +00:00
|
|
|
assert(queue_state.contains(alter_version));
|
2020-02-19 08:45:09 +00:00
|
|
|
|
2020-02-17 18:07:22 +00:00
|
|
|
if (queue_state[alter_version].metadata_finished)
|
|
|
|
queue_state.erase(alter_version);
|
|
|
|
else
|
|
|
|
queue_state[alter_version].data_finished = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-15 20:08:45 +00:00
|
|
|
bool ReplicatedMergeTreeAltersSequence::canExecuteDataAlter(int alter_version, std::unique_lock<std::mutex> & /*state_lock*/) const
|
2020-02-17 18:07:22 +00:00
|
|
|
{
|
2020-02-19 08:45:09 +00:00
|
|
|
/// Queue maybe empty when we start after server shutdown
|
|
|
|
/// and have some MUTATE_PART records in queue
|
2020-02-17 18:07:22 +00:00
|
|
|
if (queue_state.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/// All versions smaller than head, can be executed
|
|
|
|
if (alter_version < queue_state.begin()->first)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return queue_state.at(alter_version).metadata_finished;
|
|
|
|
}
|
|
|
|
|
2022-06-15 20:08:45 +00:00
|
|
|
bool ReplicatedMergeTreeAltersSequence::canExecuteMetaAlter(int alter_version, std::unique_lock<std::mutex> & /*state_lock*/) const
|
2020-02-17 18:07:22 +00:00
|
|
|
{
|
2020-02-18 17:31:49 +00:00
|
|
|
assert(!queue_state.empty());
|
2020-02-17 18:07:22 +00:00
|
|
|
|
|
|
|
/// We can execute only alters of metadata which are in head.
|
|
|
|
return queue_state.begin()->first == alter_version;
|
|
|
|
}
|
|
|
|
}
|