Revert immutable settings

This commit is contained in:
alesapin 2019-08-13 13:56:58 +03:00
parent 9e78781378
commit 1b68d79c59
2 changed files with 49 additions and 213 deletions

View File

@ -34,19 +34,20 @@ namespace ErrorCodes
template <typename Type>
String SettingNumber<Type>::toString() const
{
return DB::toString(getValue());
return DB::toString(value);
}
template <typename Type>
Field SettingNumber<Type>::toField() const
{
return getValue();
return value;
}
template <typename Type>
void SettingNumber<Type>::set(Type x)
{
data.store(Data{x, true}, std::memory_order_relaxed);
value = x;
changed = true;
}
template <typename Type>
@ -58,14 +59,6 @@ void SettingNumber<Type>::set(const Field & x)
set(applyVisitor(FieldVisitorConvertToNumber<Type>(), x));
}
template <typename Type>
SettingNumber<Type> & SettingNumber<Type>::operator= (const SettingNumber & o)
{
data.store(o.data.load(std::memory_order_relaxed), std::memory_order_relaxed);
return *this;
}
template <typename Type>
void SettingNumber<Type>::set(const String & x)
{
@ -100,9 +93,9 @@ template <typename Type>
void SettingNumber<Type>::serialize(WriteBuffer & buf) const
{
if constexpr (std::is_integral_v<Type> && std::is_unsigned_v<Type>)
writeVarUInt(static_cast<UInt64>(getValue()), buf);
writeVarUInt(static_cast<UInt64>(value), buf);
else if constexpr (std::is_integral_v<Type> && std::is_signed_v<Type>)
writeVarInt(static_cast<Int64>(getValue()), buf);
writeVarInt(static_cast<Int64>(value), buf);
else
{
static_assert(std::is_floating_point_v<Type>);
@ -140,28 +133,22 @@ template struct SettingNumber<float>;
template struct SettingNumber<bool>;
SettingMaxThreads & SettingMaxThreads::operator= (const SettingMaxThreads & o)
{
data.store(o.data.load(std::memory_order_relaxed), std::memory_order_relaxed);
return *this;
}
String SettingMaxThreads::toString() const
{
auto d = data.load(std::memory_order_relaxed);
/// Instead of the `auto` value, we output the actual value to make it easier to see.
return d.is_auto ? ("auto(" + DB::toString(d.value) + ")") : DB::toString(d.value);
return is_auto ? ("auto(" + DB::toString(value) + ")") : DB::toString(value);
}
Field SettingMaxThreads::toField() const
{
auto d = data.load(std::memory_order_relaxed);
return d.is_auto ? 0 : d.value;
return is_auto ? 0 : value;
}
void SettingMaxThreads::set(UInt64 x)
{
data.store({x ? x : getAutoValue(), x == 0, true});
value = x ? x : getAutoValue();
is_auto = x == 0;
changed = true;
}
void SettingMaxThreads::set(const Field & x)
@ -182,8 +169,7 @@ void SettingMaxThreads::set(const String & x)
void SettingMaxThreads::serialize(WriteBuffer & buf) const
{
auto d = data.load(std::memory_order_relaxed);
writeVarUInt(d.is_auto ? 0 : d.value, buf);
writeVarUInt(is_auto ? 0 : value, buf);
}
void SettingMaxThreads::deserialize(ReadBuffer & buf)
@ -195,7 +181,8 @@ void SettingMaxThreads::deserialize(ReadBuffer & buf)
void SettingMaxThreads::setAuto()
{
data.store({getAutoValue(), true, isChanged()});
value = getAutoValue();
is_auto = true;
}
UInt64 SettingMaxThreads::getAutoValue() const
@ -204,54 +191,22 @@ UInt64 SettingMaxThreads::getAutoValue() const
return res;
}
void SettingMaxThreads::setChanged(bool changed)
{
auto d = data.load(std::memory_order_relaxed);
data.store({d.value, d.is_auto, changed});
}
template <SettingTimespanIO io_unit>
SettingTimespan<io_unit> & SettingTimespan<io_unit>::operator= (const SettingTimespan & o)
{
std::shared_lock lock_o(o.mutex);
value = o.value;
changed = o.changed;
return *this;
}
template <SettingTimespanIO io_unit>
SettingTimespan<io_unit>::SettingTimespan(const SettingTimespan & o)
{
std::shared_lock lock_o(o.mutex);
value = o.value;
changed = o.changed;
}
template <SettingTimespanIO io_unit>
void SettingTimespan<io_unit>::setChanged(bool c)
{
std::unique_lock lock(mutex);
changed = c;
}
template <SettingTimespanIO io_unit>
String SettingTimespan<io_unit>::toString() const
{
return DB::toString(getValue().totalMicroseconds() / microseconds_per_io_unit);
return DB::toString(value.totalMicroseconds() / microseconds_per_io_unit);
}
template <SettingTimespanIO io_unit>
Field SettingTimespan<io_unit>::toField() const
{
return getValue().totalMicroseconds() / microseconds_per_io_unit;
return value.totalMicroseconds() / microseconds_per_io_unit;
}
template <SettingTimespanIO io_unit>
void SettingTimespan<io_unit>::set(const Poco::Timespan & x)
{
std::unique_lock lock(mutex);
value = x;
changed = true;
}
@ -280,7 +235,7 @@ void SettingTimespan<io_unit>::set(const String & x)
template <SettingTimespanIO io_unit>
void SettingTimespan<io_unit>::serialize(WriteBuffer & buf) const
{
writeVarUInt(getValue().totalMicroseconds() / microseconds_per_io_unit, buf);
writeVarUInt(value.totalMicroseconds() / microseconds_per_io_unit, buf);
}
template <SettingTimespanIO io_unit>
@ -294,47 +249,23 @@ void SettingTimespan<io_unit>::deserialize(ReadBuffer & buf)
template struct SettingTimespan<SettingTimespanIO::SECOND>;
template struct SettingTimespan<SettingTimespanIO::MILLISECOND>;
SettingString & SettingString::operator= (const SettingString & o)
{
std::shared_lock lock_o(o.mutex);
value = o.value;
changed = o.changed;
return *this;
}
SettingString::SettingString(const SettingString & o)
{
std::shared_lock lock(o.mutex);
value = o.value;
changed = o.changed;
}
String SettingString::toString() const
{
std::shared_lock lock(mutex);
return value;
}
Field SettingString::toField() const
{
std::shared_lock lock(mutex);
return value;
}
void SettingString::set(const String & x)
{
std::unique_lock lock(mutex);
value = x;
changed = true;
}
void SettingString::setChanged(bool c)
{
std::unique_lock lock(mutex);
changed = c;
}
void SettingString::set(const Field & x)
{
set(safeGet<const String &>(x));
@ -352,15 +283,10 @@ void SettingString::deserialize(ReadBuffer & buf)
set(s);
}
SettingChar & SettingChar::operator= (const SettingChar & o)
{
data.store(o.data.load(std::memory_order_relaxed), std::memory_order_relaxed);
return *this;
}
String SettingChar::toString() const
{
return String(1, getValue());
return String(1, value);
}
Field SettingChar::toField() const
@ -370,7 +296,8 @@ Field SettingChar::toField() const
void SettingChar::set(char x)
{
data.store({x, true});
value = x;
changed = true;
}
void SettingChar::set(const String & x)
@ -400,19 +327,6 @@ void SettingChar::deserialize(ReadBuffer & buf)
}
template <typename EnumType, typename Tag>
SettingEnum<EnumType, Tag> & SettingEnum<EnumType, Tag>::operator= (const SettingEnum & o)
{
data.store(o.data.load(std::memory_order_relaxed), std::memory_order_relaxed);
return *this;
}
template <typename EnumType, typename Tag>
void SettingEnum<EnumType, Tag>::set(EnumType x)
{
data.store({x, true}, std::memory_order_relaxed);
}
template <typename EnumType, typename Tag>
void SettingEnum<EnumType, Tag>::serialize(WriteBuffer & buf) const
{
@ -428,7 +342,6 @@ void SettingEnum<EnumType, Tag>::deserialize(ReadBuffer & buf)
}
#define IMPLEMENT_SETTING_ENUM(ENUM_NAME, LIST_OF_NAMES_MACRO, ERROR_CODE_FOR_UNEXPECTED_NAME) \
IMPLEMENT_SETTING_ENUM_WITH_TAG(ENUM_NAME, void, LIST_OF_NAMES_MACRO, ERROR_CODE_FOR_UNEXPECTED_NAME)
@ -438,7 +351,7 @@ void SettingEnum<EnumType, Tag>::deserialize(ReadBuffer & buf)
{ \
using EnumType = ENUM_NAME; \
using UnderlyingType = std::underlying_type<EnumType>::type; \
switch (static_cast<UnderlyingType>(getValue())) \
switch (static_cast<UnderlyingType>(value)) \
{ \
LIST_OF_NAMES_MACRO(IMPLEMENT_SETTING_ENUM_TO_STRING_HELPER_) \
} \

View File

@ -8,8 +8,6 @@
#include <Core/Types.h>
#include <ext/singleton.h>
#include <unordered_map>
#include <atomic>
#include <shared_mutex>
namespace DB
@ -34,25 +32,13 @@ namespace ErrorCodes
template <typename Type>
struct SettingNumber
{
struct Data
{
Type value;
bool changed;
};
Type value;
bool changed = false;
std::atomic<Data> data;
SettingNumber(Type x = 0) : data{{x, false}} {}
SettingNumber(const SettingNumber & o) : data{o.data.load(std::memory_order_relaxed)} {}
bool isChanged() const { return data.load(std::memory_order_relaxed).changed; }
void setChanged(bool changed) { data.store({getValue(), changed}, std::memory_order_relaxed); }
operator Type() const { return getValue(); }
Type getValue() const { return data.load(std::memory_order_relaxed).value; }
SettingNumber(Type x = 0) : value(x) {}
operator Type() const { return value; }
SettingNumber & operator= (Type x) { set(x); return *this; }
SettingNumber & operator= (const SettingNumber & o);
/// Serialize to a test string.
String toString() const;
@ -87,26 +73,14 @@ using SettingBool = SettingNumber<bool>;
*/
struct SettingMaxThreads
{
struct Data
{
UInt64 value;
bool is_auto;
bool changed;
};
UInt64 value;
bool is_auto;
bool changed = false;
std::atomic<Data> data;
SettingMaxThreads(UInt64 x = 0) : data{{x ? x : getAutoValue(), x == 0, false}} {}
SettingMaxThreads(const SettingMaxThreads & o) : data{o.data.load(std::memory_order_relaxed)} {}
bool isChanged() const { return data.load(std::memory_order_relaxed).changed; }
void setChanged(bool changed);
operator UInt64() const { return getValue(); }
UInt64 getValue() const { return data.load(std::memory_order_relaxed).value; }
SettingMaxThreads(UInt64 x = 0) : value(x ? x : getAutoValue()), is_auto(x == 0) {}
operator UInt64() const { return value; }
SettingMaxThreads & operator= (UInt64 x) { set(x); return *this; }
SettingMaxThreads & operator= (const SettingMaxThreads & o);
String toString() const;
Field toField() const;
@ -118,7 +92,6 @@ struct SettingMaxThreads
void serialize(WriteBuffer & buf) const;
void deserialize(ReadBuffer & buf);
bool isAuto() const { return data.load(std::memory_order_relaxed).is_auto; }
void setAuto();
UInt64 getAutoValue() const;
};
@ -129,37 +102,16 @@ enum class SettingTimespanIO { MILLISECOND, SECOND };
template <SettingTimespanIO io_unit>
struct SettingTimespan
{
mutable std::shared_mutex mutex;
Poco::Timespan value;
bool changed = false;
SettingTimespan(UInt64 x = 0) : value(x * microseconds_per_io_unit) {}
SettingTimespan(const SettingTimespan & o);
operator Poco::Timespan() const { return getValue(); }
Poco::Timespan getValue() const { std::shared_lock lock(mutex); return value; }
operator Poco::Timespan() const { return value; }
SettingTimespan & operator= (const Poco::Timespan & x) { set(x); return *this; }
SettingTimespan & operator= (const SettingTimespan & o);
Poco::Timespan::TimeDiff totalSeconds() const
{
std::shared_lock lock(mutex);
return value.totalSeconds();
}
Poco::Timespan::TimeDiff totalMilliseconds() const
{
std::shared_lock lock(mutex);
return value.totalMilliseconds();
}
bool isChanged() const
{
std::shared_lock lock(mutex);
return changed;
}
void setChanged(bool changed);
Poco::Timespan::TimeDiff totalSeconds() const { return value.totalSeconds(); }
Poco::Timespan::TimeDiff totalMilliseconds() const { return value.totalMilliseconds(); }
String toString() const;
Field toField() const;
@ -182,19 +134,13 @@ using SettingMilliseconds = SettingTimespan<SettingTimespanIO::MILLISECOND>;
struct SettingString
{
mutable std::shared_mutex mutex;
String value;
bool changed = false;
SettingString(const String & x = String{}) : value(x) {}
SettingString(const SettingString & o);
operator String() const { return getValue(); }
String getValue() const { std::shared_lock lock(mutex); return value; }
operator String() const { return value; }
SettingString & operator= (const String & x) { set(x); return *this; }
SettingString & operator= (const SettingString & o);
bool isChanged() const { std::shared_lock lock(mutex); return changed; }
void setChanged(bool changed);
String toString() const;
Field toField() const;
@ -210,25 +156,13 @@ struct SettingString
struct SettingChar
{
public:
struct Data
{
char value;
bool changed;
};
char value;
bool changed = false;
std::atomic<Data> data;
SettingChar(char x = '\0') : data({x, false}) {}
SettingChar(const SettingChar & o) : data{o.data.load(std::memory_order_relaxed)} {}
operator char() const { return getValue(); }
char getValue() const { return data.load(std::memory_order_relaxed).value; }
SettingChar(char x = '\0') : value(x) {}
operator char() const { return value; }
SettingChar & operator= (char x) { set(x); return *this; }
SettingChar & operator= (const SettingChar & o);
bool isChanged() const { return data.load(std::memory_order_relaxed).changed; }
void setChanged(bool changed) { data.store({getValue(), changed}, std::memory_order_relaxed);}
String toString() const;
Field toField() const;
@ -246,30 +180,18 @@ public:
template <typename EnumType, typename Tag = void>
struct SettingEnum
{
struct Data
{
EnumType value;
bool changed;
};
EnumType value;
bool changed = false;
std::atomic<Data> data;
SettingEnum(EnumType x) : data({x, false}) {}
SettingEnum(const SettingEnum & o) : data{o.data.load(std::memory_order_relaxed)} {}
operator EnumType() const { return getValue(); }
EnumType getValue() const { return data.load(std::memory_order_relaxed).value; }
SettingEnum(EnumType x) : value(x) {}
operator EnumType() const { return value; }
SettingEnum & operator= (EnumType x) { set(x); return *this; }
SettingEnum & operator= (const SettingEnum & o);
bool isChanged() const { return data.load(std::memory_order_relaxed).changed; }
void setChanged(bool changed) { data.store({getValue(), changed}, std::memory_order_relaxed);}
String toString() const;
Field toField() const { return toString(); }
void set(EnumType x);
void set(EnumType x) { value = x; changed = true; }
void set(const Field & x) { set(safeGet<const String &>(x)); }
void set(const String & x);
@ -386,7 +308,6 @@ private:
Derived & castToDerived() { return *static_cast<Derived *>(this); }
const Derived & castToDerived() const { return *static_cast<const Derived *>(this); }
using IsChangedFunction = bool (*)(const Derived &);
using GetStringFunction = String (*)(const Derived &);
using GetFieldFunction = Field (*)(const Derived &);
using SetStringFunction = void (*)(Derived &, const String &);
@ -397,7 +318,7 @@ private:
struct MemberInfo
{
IsChangedFunction is_changed;
size_t offset_of_changed;
StringRef name;
StringRef description;
/// Can be updated after first load for config/definition.
@ -412,7 +333,7 @@ private:
DeserializeFunction deserialize;
CastValueWithoutApplyingFunction cast_value_without_applying;
bool isChanged(const Derived & collection) const { return is_changed(collection); }
bool isChanged(const Derived & collection) const { return *reinterpret_cast<const bool*>(reinterpret_cast<const UInt8*>(&collection) + offset_of_changed); }
};
class MemberInfos
@ -772,7 +693,8 @@ public:
#define IMPLEMENT_SETTINGS_COLLECTION_ADD_MUTABLE_MEMBER_INFO_HELPER_(TYPE, NAME, DEFAULT, DESCRIPTION) \
add({[](const Derived & d) { return d.NAME.isChanged(); }, \
static_assert(std::is_same_v<decltype(std::declval<Derived>().NAME.changed), bool>); \
add({offsetof(Derived, NAME.changed), \
StringRef(#NAME, strlen(#NAME)), StringRef(#DESCRIPTION, strlen(#DESCRIPTION)), true, \
&Functions::NAME##_getString, &Functions::NAME##_getField, \
&Functions::NAME##_setString, &Functions::NAME##_setField, \
@ -780,7 +702,8 @@ public:
&Functions::NAME##_castValueWithoutApplying });
#define IMPLEMENT_SETTINGS_COLLECTION_ADD_IMMUTABLE_MEMBER_INFO_HELPER_(TYPE, NAME, DEFAULT, DESCRIPTION) \
add({[](const Derived & d) { return d.NAME.isChanged(); }, \
static_assert(std::is_same_v<decltype(std::declval<Derived>().NAME.changed), bool>); \
add({offsetof(Derived, NAME.changed), \
StringRef(#NAME, strlen(#NAME)), StringRef(#DESCRIPTION, strlen(#DESCRIPTION)), false, \
&Functions::NAME##_getString, &Functions::NAME##_getField, \
&Functions::NAME##_setString, &Functions::NAME##_setField, \