From 2344e0738e9ed65061273103f791dca56d9f42ab Mon Sep 17 00:00:00 2001 From: Frank Chen Date: Fri, 23 Sep 2022 11:32:21 +0800 Subject: [PATCH] Keep compatibility during upgrading --- src/Common/OpenTelemetryTraceContext.cpp | 6 ++--- src/Interpreters/DDLTask.cpp | 32 ++++++++++++++++-------- src/Interpreters/DDLTask.h | 5 ++++ 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/Common/OpenTelemetryTraceContext.cpp b/src/Common/OpenTelemetryTraceContext.cpp index 3e7a172bdb2..515060803d6 100644 --- a/src/Common/OpenTelemetryTraceContext.cpp +++ b/src/Common/OpenTelemetryTraceContext.cpp @@ -228,8 +228,7 @@ String TracingContext::composeTraceparentHeader() const void TracingContext::deserialize(ReadBuffer & buf) { - buf >> "tracing: " - >> this->trace_id + buf >> this->trace_id >> "\n" >> this->span_id >> "\n" @@ -241,8 +240,7 @@ void TracingContext::deserialize(ReadBuffer & buf) void TracingContext::serialize(WriteBuffer & buf) const { - buf << "tracing: " - << this->trace_id + buf << this->trace_id << "\n" << this->span_id << "\n" diff --git a/src/Interpreters/DDLTask.cpp b/src/Interpreters/DDLTask.cpp index aff47db8242..73105ae003e 100644 --- a/src/Interpreters/DDLTask.cpp +++ b/src/Interpreters/DDLTask.cpp @@ -50,21 +50,26 @@ bool HostID::isLocalAddress(UInt16 clickhouse_port) const void DDLLogEntry::assertVersion() const { - constexpr UInt64 max_version = 2; - if (version == 0 || max_version < version) + if (version == 0 + /// NORMALIZE_CREATE_ON_INITIATOR_VERSION does not change the entry format, it uses versioin 2, so there shouldn't be version 3 + || version == NORMALIZE_CREATE_ON_INITIATOR_VERSION + || version > MAX_VERSION) throw Exception(ErrorCodes::UNKNOWN_FORMAT_VERSION, "Unknown DDLLogEntry format version: {}." - "Maximum supported version is {}", version, max_version); + "Maximum supported version is {}", version, MAX_VERSION); } void DDLLogEntry::setSettingsIfRequired(ContextPtr context) { - version = context->getSettingsRef().distributed_ddl_entry_format_version; + version = context->getSettingsRef(). ; + if (version <= 0 || version > MAX_VERSION) + throw Exception(ErrorCodes::UNKNOWN_FORMAT_VERSION, "Unknown distributed_ddl_entry_format_version: {}." + "Maximum supported version is {}.", version, MAX_VERSION); /// NORMALIZE_CREATE_ON_INITIATOR_VERSION does not affect entry format in ZooKeeper if (version == NORMALIZE_CREATE_ON_INITIATOR_VERSION) version = SETTINGS_IN_ZK_VERSION; - if (version == SETTINGS_IN_ZK_VERSION) + if (version >= SETTINGS_IN_ZK_VERSION) settings.emplace(context->getSettingsRef().changes()); } @@ -94,7 +99,8 @@ String DDLLogEntry::toString() const wb << "settings: " << serializeAST(ast) << "\n"; } - wb << this->tracing_context; + if (version >= OPENTELEMETRY_ENABLED_VERSION) + wb << "tracing: " << this->tracing_context; return wb.str(); } @@ -108,7 +114,7 @@ void DDLLogEntry::parse(const String & data) Strings host_id_strings; rb >> "query: " >> escape >> query >> "\n"; - if (version == 1) + if (version == OLDEST_VERSION) { rb >> "hosts: " >> host_id_strings >> "\n"; @@ -117,9 +123,8 @@ void DDLLogEntry::parse(const String & data) else initiator.clear(); } - else if (version == 2) + else if (version >= SETTINGS_IN_ZK_VERSION) { - if (!rb.eof() && *rb.position() == 'h') rb >> "hosts: " >> host_id_strings >> "\n"; if (!rb.eof() && *rb.position() == 'i') @@ -136,8 +141,13 @@ void DDLLogEntry::parse(const String & data) } } - if (!rb.eof() && *rb.position() == 't') - rb >> this->tracing_context; + if (version >= OPENTELEMETRY_ENABLED_VERSION) + { + if (!rb.eof() && *rb.position() == 't') + rb >> "tracing: " >> this->tracing_context; + } + + assertEOF(rb); if (!host_id_strings.empty()) { diff --git a/src/Interpreters/DDLTask.h b/src/Interpreters/DDLTask.h index fc85188a865..7217ee2b98b 100644 --- a/src/Interpreters/DDLTask.h +++ b/src/Interpreters/DDLTask.h @@ -70,6 +70,11 @@ struct DDLLogEntry static constexpr const UInt64 OLDEST_VERSION = 1; static constexpr const UInt64 SETTINGS_IN_ZK_VERSION = 2; static constexpr const UInt64 NORMALIZE_CREATE_ON_INITIATOR_VERSION = 3; + static constexpr const UInt64 OPENTELEMETRY_ENABLED_VERSION = 4; + /// Add new version here + + /// Remember to update the value below once new version is added + static constexpr const UInt64 MAX_VERSION = 4; UInt64 version = 1; String query;