2021-11-08 18:56:09 +00:00
|
|
|
#pragma once
|
|
|
|
#include <Common/TransactionID.h>
|
2022-01-14 14:03:00 +00:00
|
|
|
#include <Interpreters/StorageID.h>
|
|
|
|
|
|
|
|
namespace Poco
|
|
|
|
{
|
|
|
|
class Logger;
|
|
|
|
}
|
2021-11-08 18:56:09 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2022-03-16 19:16:26 +00:00
|
|
|
/// This structure allows to pass more information about a part that transaction is trying to create/remove.
|
|
|
|
/// It's useful for logging and for exception messages.
|
2022-01-14 14:03:00 +00:00
|
|
|
struct TransactionInfoContext
|
|
|
|
{
|
2022-03-16 19:16:26 +00:00
|
|
|
/// To which table a part belongs
|
2022-01-14 14:03:00 +00:00
|
|
|
StorageID table = StorageID::createEmpty();
|
2022-03-16 19:16:26 +00:00
|
|
|
/// Name of a part that transaction is trying to create/remove
|
2022-01-14 14:03:00 +00:00
|
|
|
String part_name;
|
2022-03-16 19:16:26 +00:00
|
|
|
/// Optional: name of part that covers `part_name` if transaction is trying to remove `part_name`
|
2022-01-14 14:03:00 +00:00
|
|
|
String covering_part;
|
|
|
|
|
|
|
|
TransactionInfoContext(StorageID id, String part) : table(std::move(id)), part_name(std::move(part)) {}
|
|
|
|
};
|
|
|
|
|
2022-03-18 11:01:26 +00:00
|
|
|
/// This structure contains metadata of an object (currently it's used for data parts in MergeTree only)
|
|
|
|
/// that allows to determine when and by which transaction it has been created/removed
|
2021-11-08 18:56:09 +00:00
|
|
|
struct VersionMetadata
|
|
|
|
{
|
2022-03-18 11:01:26 +00:00
|
|
|
/// ID of transaction that has created/is trying to create this object
|
2022-01-28 17:47:37 +00:00
|
|
|
TransactionID creation_tid = Tx::EmptyTID;
|
2022-03-18 11:01:26 +00:00
|
|
|
/// ID of transaction that has removed/is trying to remove this object
|
2022-01-28 17:47:37 +00:00
|
|
|
TransactionID removal_tid = Tx::EmptyTID;
|
2021-11-08 18:56:09 +00:00
|
|
|
|
2022-03-18 11:01:26 +00:00
|
|
|
/// Hash of removal_tid, used to lock an object for removal
|
2022-01-28 17:47:37 +00:00
|
|
|
std::atomic<TIDHash> removal_tid_lock = 0;
|
2021-11-08 18:56:09 +00:00
|
|
|
|
2022-03-18 11:01:26 +00:00
|
|
|
/// CSN of transaction that has created this object
|
2022-01-28 17:47:37 +00:00
|
|
|
std::atomic<CSN> creation_csn = Tx::UnknownCSN;
|
2022-03-18 11:01:26 +00:00
|
|
|
/// CSN of transaction that has removed this object
|
2022-01-28 17:47:37 +00:00
|
|
|
std::atomic<CSN> removal_csn = Tx::UnknownCSN;
|
2021-11-08 18:56:09 +00:00
|
|
|
|
2022-01-31 22:27:55 +00:00
|
|
|
/// Checks if an object is visible for transaction or not.
|
2021-11-08 18:56:09 +00:00
|
|
|
bool isVisible(const MergeTreeTransaction & txn);
|
2022-03-14 20:43:34 +00:00
|
|
|
bool isVisible(CSN snapshot_version, TransactionID current_tid = Tx::EmptyTID);
|
2021-11-08 18:56:09 +00:00
|
|
|
|
2022-01-28 17:47:37 +00:00
|
|
|
TransactionID getCreationTID() const { return creation_tid; }
|
|
|
|
TransactionID getRemovalTID() const;
|
2021-11-08 18:56:09 +00:00
|
|
|
|
2022-03-18 11:01:26 +00:00
|
|
|
/// Looks an object for removal, throws if it's already locked by concurrent transaction
|
2022-03-14 20:43:34 +00:00
|
|
|
bool tryLockRemovalTID(const TransactionID & tid, const TransactionInfoContext & context, TIDHash * locked_by_id = nullptr);
|
|
|
|
void lockRemovalTID(const TransactionID & tid, const TransactionInfoContext & context);
|
2022-03-18 11:01:26 +00:00
|
|
|
/// Unlocks an object for removal (when transaction is rolling back)
|
2022-03-14 20:43:34 +00:00
|
|
|
void unlockRemovalTID(const TransactionID & tid, const TransactionInfoContext & context);
|
2021-11-08 18:56:09 +00:00
|
|
|
|
2022-02-17 21:26:37 +00:00
|
|
|
bool isRemovalTIDLocked() const;
|
2021-11-08 18:56:09 +00:00
|
|
|
|
|
|
|
/// It can be called only from MergeTreeTransaction or on server startup
|
2022-02-14 19:50:08 +00:00
|
|
|
void setCreationTID(const TransactionID & tid, TransactionInfoContext * context);
|
2021-11-08 18:56:09 +00:00
|
|
|
|
2022-01-31 22:27:55 +00:00
|
|
|
/// Checks if it's safe to remove outdated version of an object
|
|
|
|
bool canBeRemoved();
|
2022-03-14 20:43:34 +00:00
|
|
|
bool canBeRemovedImpl(CSN oldest_snapshot_version);
|
2021-12-30 13:15:28 +00:00
|
|
|
|
|
|
|
void write(WriteBuffer & buf) const;
|
|
|
|
void read(ReadBuffer & buf);
|
|
|
|
|
2022-02-17 21:26:37 +00:00
|
|
|
enum WhichCSN { CREATION, REMOVAL };
|
|
|
|
void writeCSN(WriteBuffer & buf, WhichCSN which_csn, bool internal = false) const;
|
2022-03-08 19:11:47 +00:00
|
|
|
void writeRemovalTID(WriteBuffer & buf, bool clear = false) const;
|
2022-02-17 21:26:37 +00:00
|
|
|
|
2021-12-30 13:15:28 +00:00
|
|
|
String toString(bool one_line = true) const;
|
2022-01-14 14:03:00 +00:00
|
|
|
|
|
|
|
Poco::Logger * log;
|
|
|
|
VersionMetadata();
|
2021-11-08 18:56:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DataTypePtr getTransactionIDDataType();
|
|
|
|
|
|
|
|
}
|