diff options
author | ivanzhukov <ivanzhukov@yandex-team.ru> | 2022-02-10 16:49:41 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:41 +0300 |
commit | ca3252a147a429eac4ba8221857493c58dcd09b5 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/monlib/encode/spack | |
parent | 0892d79ab411592ad25175c4bdadbcb09b466cf5 (diff) | |
download | ydb-ca3252a147a429eac4ba8221857493c58dcd09b5.tar.gz |
Restoring authorship annotation for <ivanzhukov@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/monlib/encode/spack')
-rw-r--r-- | library/cpp/monlib/encode/spack/compression.cpp | 24 | ||||
-rw-r--r-- | library/cpp/monlib/encode/spack/compression.h | 20 | ||||
-rw-r--r-- | library/cpp/monlib/encode/spack/spack_v1.h | 10 | ||||
-rw-r--r-- | library/cpp/monlib/encode/spack/spack_v1_decoder.cpp | 84 | ||||
-rw-r--r-- | library/cpp/monlib/encode/spack/spack_v1_encoder.cpp | 26 | ||||
-rw-r--r-- | library/cpp/monlib/encode/spack/spack_v1_ut.cpp | 236 | ||||
-rw-r--r-- | library/cpp/monlib/encode/spack/ya.make | 6 |
7 files changed, 203 insertions, 203 deletions
diff --git a/library/cpp/monlib/encode/spack/compression.cpp b/library/cpp/monlib/encode/spack/compression.cpp index ff8c043bc4..0d2152fc85 100644 --- a/library/cpp/monlib/encode/spack/compression.cpp +++ b/library/cpp/monlib/encode/spack/compression.cpp @@ -241,7 +241,7 @@ namespace NMonitoring { // TFramedOutputStream /////////////////////////////////////////////////////////////////////////////// template <typename TCodecAlg, typename TCheckSumAlg> - class TFramedCompressStream final: public IFramedCompressStream { + class TFramedCompressStream final: public IFramedCompressStream { public: explicit TFramedCompressStream(IOutputStream* out) : Out_(out) @@ -277,13 +277,13 @@ namespace NMonitoring { } } - void FlushWithoutEmptyFrame() override { + void FlushWithoutEmptyFrame() override { if (Out_ && !Uncompressed_.Empty()) { WriteCompressedFrame(); } } - void FinishAndWriteEmptyFrame() override { + void FinishAndWriteEmptyFrame() override { if (Out_) { Y_DEFER { Out_ = nullptr; @@ -297,14 +297,14 @@ namespace NMonitoring { } } - void DoFlush() override { - FlushWithoutEmptyFrame(); - } - - void DoFinish() override { - FinishAndWriteEmptyFrame(); - } - + void DoFlush() override { + FlushWithoutEmptyFrame(); + } + + void DoFinish() override { + FinishAndWriteEmptyFrame(); + } + void WriteCompressedFrame() { static const auto framePayload = sizeof(TFrameHeader) + sizeof(TFrameFooter); const auto maxFrameSize = ui64(TCodecAlg::MaxCompressedLength(Uncompressed_.Size())) + framePayload; @@ -364,7 +364,7 @@ namespace NMonitoring { Y_FAIL("invalid compression algorithm"); } - THolder<IFramedCompressStream> CompressedOutput(IOutputStream* out, ECompression alg) { + THolder<IFramedCompressStream> CompressedOutput(IOutputStream* out, ECompression alg) { switch (alg) { case ECompression::IDENTITY: return nullptr; diff --git a/library/cpp/monlib/encode/spack/compression.h b/library/cpp/monlib/encode/spack/compression.h index dc14cb561a..f74d8b424e 100644 --- a/library/cpp/monlib/encode/spack/compression.h +++ b/library/cpp/monlib/encode/spack/compression.h @@ -7,13 +7,13 @@ namespace NMonitoring { -class IFramedCompressStream: public IOutputStream { -public: - virtual void FlushWithoutEmptyFrame() = 0; - virtual void FinishAndWriteEmptyFrame() = 0; -}; - -THolder<IInputStream> CompressedInput(IInputStream* in, ECompression alg); -THolder<IFramedCompressStream> CompressedOutput(IOutputStream* out, ECompression alg); - -} // namespace NMonitoring +class IFramedCompressStream: public IOutputStream { +public: + virtual void FlushWithoutEmptyFrame() = 0; + virtual void FinishAndWriteEmptyFrame() = 0; +}; + +THolder<IInputStream> CompressedInput(IInputStream* in, ECompression alg); +THolder<IFramedCompressStream> CompressedOutput(IOutputStream* out, ECompression alg); + +} // namespace NMonitoring diff --git a/library/cpp/monlib/encode/spack/spack_v1.h b/library/cpp/monlib/encode/spack/spack_v1.h index ddc305fc0d..cf1c9417b9 100644 --- a/library/cpp/monlib/encode/spack/spack_v1.h +++ b/library/cpp/monlib/encode/spack/spack_v1.h @@ -15,9 +15,9 @@ class IInputStream; class IOutputStream; namespace NMonitoring { - class TSpackDecodeError: public yexception { - }; - + class TSpackDecodeError: public yexception { + }; + constexpr auto EncodeMetricType(EMetricType mt) noexcept { return static_cast<std::underlying_type_t<EMetricType>>(mt); } @@ -98,9 +98,9 @@ namespace NMonitoring { IMetricEncoderPtr EncoderSpackV1( IOutputStream* out, ETimePrecision timePrecision, - ECompression compression, + ECompression compression, EMetricsMergingMode mergingMode = EMetricsMergingMode::DEFAULT - ); + ); IMetricEncoderPtr EncoderSpackV12( IOutputStream* out, diff --git a/library/cpp/monlib/encode/spack/spack_v1_decoder.cpp b/library/cpp/monlib/encode/spack/spack_v1_decoder.cpp index e1d0629694..1f445fc80d 100644 --- a/library/cpp/monlib/encode/spack/spack_v1_decoder.cpp +++ b/library/cpp/monlib/encode/spack/spack_v1_decoder.cpp @@ -3,9 +3,9 @@ #include "compression.h" #include <library/cpp/monlib/encode/buffered/string_pool.h> -#include <library/cpp/monlib/exception/exception.h> +#include <library/cpp/monlib/exception/exception.h> #include <library/cpp/monlib/metrics/histogram_collector.h> -#include <library/cpp/monlib/metrics/metric.h> +#include <library/cpp/monlib/metrics/metric.h> #include <util/generic/yexception.h> #include <util/generic/buffer.h> @@ -18,8 +18,8 @@ namespace NMonitoring { namespace { -#define DECODE_ENSURE(COND, ...) MONLIB_ENSURE_EX(COND, TSpackDecodeError() << __VA_ARGS__) - +#define DECODE_ENSURE(COND, ...) MONLIB_ENSURE_EX(COND, TSpackDecodeError() << __VA_ARGS__) + constexpr ui64 LABEL_SIZE_LIMIT = 128_MB; /////////////////////////////////////////////////////////////////////// @@ -37,15 +37,15 @@ namespace NMonitoring { c->OnStreamBegin(); // (1) read header - size_t readBytes = In_->Read(&Header_, sizeof(Header_)); - DECODE_ENSURE(readBytes == sizeof(Header_), "not enough data in input stream to read header"); + size_t readBytes = In_->Read(&Header_, sizeof(Header_)); + DECODE_ENSURE(readBytes == sizeof(Header_), "not enough data in input stream to read header"); - ui8 version = ((Header_.Version >> 8) & 0xff); - DECODE_ENSURE(version == 1, "versions mismatch (expected: 1, got: " << +version << ')'); + ui8 version = ((Header_.Version >> 8) & 0xff); + DECODE_ENSURE(version == 1, "versions mismatch (expected: 1, got: " << +version << ')'); - DECODE_ENSURE(Header_.HeaderSize >= sizeof(Header_), "invalid header size"); - if (size_t skipBytes = Header_.HeaderSize - sizeof(Header_)) { - DECODE_ENSURE(In_->Skip(skipBytes) == skipBytes, "input stream unexpectedly ended"); + DECODE_ENSURE(Header_.HeaderSize >= sizeof(Header_), "invalid header size"); + if (size_t skipBytes = Header_.HeaderSize - sizeof(Header_)) { + DECODE_ENSURE(In_->Skip(skipBytes) == skipBytes, "input stream unexpectedly ended"); } if (Header_.MetricCount == 0) { @@ -55,27 +55,27 @@ namespace NMonitoring { } // if compression enabled all below reads must go throught decompressor - auto compressedIn = CompressedInput(In_, DecodeCompression(Header_.Compression)); + auto compressedIn = CompressedInput(In_, DecodeCompression(Header_.Compression)); if (compressedIn) { In_ = compressedIn.Get(); } - TimePrecision_ = DecodeTimePrecision(Header_.TimePrecision); + TimePrecision_ = DecodeTimePrecision(Header_.TimePrecision); - const ui64 labelSizeTotal = ui64(Header_.LabelNamesSize) + Header_.LabelValuesSize; + const ui64 labelSizeTotal = ui64(Header_.LabelNamesSize) + Header_.LabelValuesSize; - DECODE_ENSURE(labelSizeTotal <= LABEL_SIZE_LIMIT, "Label names & values size of " << HumanReadableSize(labelSizeTotal, SF_BYTES) + DECODE_ENSURE(labelSizeTotal <= LABEL_SIZE_LIMIT, "Label names & values size of " << HumanReadableSize(labelSizeTotal, SF_BYTES) << " exceeds the limit which is " << HumanReadableSize(LABEL_SIZE_LIMIT, SF_BYTES)); // (2) read string pools TVector<char> namesBuf(Header_.LabelNamesSize); readBytes = In_->Load(namesBuf.data(), namesBuf.size()); - DECODE_ENSURE(readBytes == Header_.LabelNamesSize, "not enough data to read label names pool"); + DECODE_ENSURE(readBytes == Header_.LabelNamesSize, "not enough data to read label names pool"); TStringPool labelNames(namesBuf.data(), namesBuf.size()); TVector<char> valuesBuf(Header_.LabelValuesSize); readBytes = In_->Load(valuesBuf.data(), valuesBuf.size()); - DECODE_ENSURE(readBytes == Header_.LabelValuesSize, "not enough data to read label values pool"); + DECODE_ENSURE(readBytes == Header_.LabelValuesSize, "not enough data to read label values pool"); TStringPool labelValues(valuesBuf.data(), valuesBuf.size()); // (3) read common time @@ -180,7 +180,7 @@ namespace NMonitoring { break; default: - throw TSpackDecodeError() << "Unsupported metric type: " << metricType; + throw TSpackDecodeError() << "Unsupported metric type: " << metricType; } } @@ -212,25 +212,25 @@ namespace NMonitoring { IHistogramSnapshotPtr ReadHistogram() { ui32 bucketsCount = ReadVarint(); - auto s = TExplicitHistogramSnapshot::New(bucketsCount); + auto s = TExplicitHistogramSnapshot::New(bucketsCount); if (SV1_00 == Header_.Version) { // v1.0 - for (ui32 i = 0; i < bucketsCount; i++) { - i64 bound = ReadFixed<i64>(); - double doubleBound = (bound != Max<i64>()) - ? static_cast<double>(bound) - : Max<double>(); - - (*s)[i].first = doubleBound; - } - } else { - for (ui32 i = 0; i < bucketsCount; i++) { - double doubleBound = ReadFixed<double>(); - (*s)[i].first = doubleBound; - } + for (ui32 i = 0; i < bucketsCount; i++) { + i64 bound = ReadFixed<i64>(); + double doubleBound = (bound != Max<i64>()) + ? static_cast<double>(bound) + : Max<double>(); + + (*s)[i].first = doubleBound; + } + } else { + for (ui32 i = 0; i < bucketsCount; i++) { + double doubleBound = ReadFixed<double>(); + (*s)[i].first = doubleBound; + } } - + // values for (ui32 i = 0; i < bucketsCount; i++) { (*s)[i].second = ReadFixed<ui64>(); @@ -265,7 +265,7 @@ namespace NMonitoring { inline T ReadFixed() { T value; size_t readBytes = In_->Load(&value, sizeof(T)); - DECODE_ENSURE(readBytes == sizeof(T), "no enough data to read " << TypeName<T>()); + DECODE_ENSURE(readBytes == sizeof(T), "no enough data to read " << TypeName<T>()); return value; } @@ -277,16 +277,16 @@ namespace NMonitoring { IInputStream* In_; TString MetricNameLabel_; ETimePrecision TimePrecision_; - TSpackHeader Header_; - }; // class TDecoderSpackV1 + TSpackHeader Header_; + }; // class TDecoderSpackV1 -#undef DECODE_ENSURE - } // namespace +#undef DECODE_ENSURE + } // namespace EValueType DecodeValueType(ui8 byte) { EValueType result; if (!TryDecodeValueType(byte, &result)) { - throw TSpackDecodeError() << "unknown value type: " << byte; + throw TSpackDecodeError() << "unknown value type: " << byte; } return result; } @@ -320,7 +320,7 @@ namespace NMonitoring { ETimePrecision DecodeTimePrecision(ui8 byte) { ETimePrecision result; if (!TryDecodeTimePrecision(byte, &result)) { - throw TSpackDecodeError() << "unknown time precision: " << byte; + throw TSpackDecodeError() << "unknown time precision: " << byte; } return result; } @@ -344,7 +344,7 @@ namespace NMonitoring { EMetricType DecodeMetricType(ui8 byte) { EMetricType result; if (!TryDecodeMetricType(byte, &result)) { - throw TSpackDecodeError() << "unknown metric type: " << byte; + throw TSpackDecodeError() << "unknown metric type: " << byte; } return result; } @@ -419,7 +419,7 @@ namespace NMonitoring { ECompression DecodeCompression(ui8 byte) { ECompression result; if (!TryDecodeCompression(byte, &result)) { - throw TSpackDecodeError() << "unknown compression alg: " << byte; + throw TSpackDecodeError() << "unknown compression alg: " << byte; } return result; } diff --git a/library/cpp/monlib/encode/spack/spack_v1_encoder.cpp b/library/cpp/monlib/encode/spack/spack_v1_encoder.cpp index a965739ad7..a2b0bb5f50 100644 --- a/library/cpp/monlib/encode/spack/spack_v1_encoder.cpp +++ b/library/cpp/monlib/encode/spack/spack_v1_encoder.cpp @@ -22,11 +22,11 @@ namespace NMonitoring { TEncoderSpackV1( IOutputStream* out, ETimePrecision timePrecision, - ECompression compression, + ECompression compression, EMetricsMergingMode mergingMode, ESpackV1Version version, TStringBuf metricNameLabel - ) + ) : Out_(out) , TimePrecision_(timePrecision) , Compression_(compression) @@ -34,7 +34,7 @@ namespace NMonitoring { , MetricName_(Version_ >= SV1_02 ? LabelNamesPool_.PutIfAbsent(metricNameLabel) : nullptr) { MetricsMergingMode_ = mergingMode; - + LabelNamesPool_.SetSorted(true); LabelValuesPool_.SetSorted(true); } @@ -77,16 +77,16 @@ namespace NMonitoring { LabelNamesPool_.Build(); LabelValuesPool_.Build(); - // Sort all points uniquely by ts -- the size can decrease - ui64 pointsCount = 0; + // Sort all points uniquely by ts -- the size can decrease + ui64 pointsCount = 0; for (TMetric& metric : Metrics_) { if (metric.TimeSeries.Size() > 1) { metric.TimeSeries.SortByTs(); - } - + } + pointsCount += metric.TimeSeries.Size(); - } - + } + // (1) write header TSpackHeader header; header.Version = Version_; @@ -97,7 +97,7 @@ namespace NMonitoring { header.LabelValuesSize = static_cast<ui32>( LabelValuesPool_.BytesSize() + LabelValuesPool_.Count()); header.MetricCount = Metrics_.size(); - header.PointsCount = pointsCount; + header.PointsCount = pointsCount; Out_->Write(&header, sizeof(header)); // if compression enabled all below writes must go throught compressor @@ -258,7 +258,7 @@ namespace NMonitoring { WriteVarUInt32(Out_, count); for (ui32 i = 0; i < count; i++) { - double bound = histogram.UpperBound(i); + double bound = histogram.UpperBound(i); Out_->Write(&bound, sizeof(bound)); } for (ui32 i = 0; i < count; i++) { @@ -299,9 +299,9 @@ namespace NMonitoring { IMetricEncoderPtr EncoderSpackV1( IOutputStream* out, ETimePrecision timePrecision, - ECompression compression, + ECompression compression, EMetricsMergingMode mergingMode - ) { + ) { return MakeHolder<TEncoderSpackV1>(out, timePrecision, compression, mergingMode, SV1_01, ""); } diff --git a/library/cpp/monlib/encode/spack/spack_v1_ut.cpp b/library/cpp/monlib/encode/spack/spack_v1_ut.cpp index c75f89e48e..fe778eb7e0 100644 --- a/library/cpp/monlib/encode/spack/spack_v1_ut.cpp +++ b/library/cpp/monlib/encode/spack/spack_v1_ut.cpp @@ -49,9 +49,9 @@ void AssertPointEqual(const NProto::TPoint& p, TInstant time, i64 value) { } Y_UNIT_TEST_SUITE(TSpackTest) { - ui8 expectedHeader_v1_0[] = { + ui8 expectedHeader_v1_0[] = { 0x53, 0x50, // magic "SP" (fixed ui16) - // minor, major + // minor, major 0x00, 0x01, // version (fixed ui16) 0x18, 0x00, // header size (fixed ui16) 0x00, // time precision (fixed ui8) @@ -62,19 +62,19 @@ Y_UNIT_TEST_SUITE(TSpackTest) { 0x08, 0x00, 0x00, 0x00, // points count (fixed ui32) }; - ui8 expectedHeader[] = { - 0x53, 0x50, // magic "SP" (fixed ui16) - // minor, major - 0x01, 0x01, // version (fixed ui16) - 0x18, 0x00, // header size (fixed ui16) - 0x00, // time precision (fixed ui8) - 0x00, // compression algorithm (fixed ui8) + ui8 expectedHeader[] = { + 0x53, 0x50, // magic "SP" (fixed ui16) + // minor, major + 0x01, 0x01, // version (fixed ui16) + 0x18, 0x00, // header size (fixed ui16) + 0x00, // time precision (fixed ui8) + 0x00, // compression algorithm (fixed ui8) 0x0d, 0x00, 0x00, 0x00, // label names size (fixed ui32) 0x40, 0x00, 0x00, 0x00, // labels values size (fixed ui32) 0x08, 0x00, 0x00, 0x00, // metric count (fixed ui32) 0x08, 0x00, 0x00, 0x00, // points count (fixed ui32) - }; - + }; + ui8 expectedStringPools[] = { 0x6e, 0x61, 0x6d, 0x65, 0x00, // "name\0" 0x70, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x00, // "project\0" @@ -150,7 +150,7 @@ Y_UNIT_TEST_SUITE(TSpackTest) { 0x05, // label value index (varint) 0x0b, 0x63, 0xfe, 0x59, // time in seconds (fixed ui32) 0x06, // histogram buckets count (varint) - 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // histogram bucket bounds (array of fixed ui64) + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // histogram bucket bounds (array of fixed ui64) 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -165,27 +165,27 @@ Y_UNIT_TEST_SUITE(TSpackTest) { }; ui8 expectedMetric5[] = { - 0x16, // types (HIST | ONE_WITH_TS) (fixed ui8) - 0x00, // flags (fixed ui8) + 0x16, // types (HIST | ONE_WITH_TS) (fixed ui8) + 0x00, // flags (fixed ui8) 0x01, // metric labels count (varint) - 0x00, // label name index (varint) + 0x00, // label name index (varint) 0x05, // label value index (varint) - 0x0b, 0x63, 0xfe, 0x59, // time in seconds (fixed ui32) - 0x06, // histogram buckets count (varint) - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, // histogram bucket bounds (array of doubles) - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x40, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x40, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x40, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0x7f, - 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // histogram bucket values - 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - }; - + 0x0b, 0x63, 0xfe, 0x59, // time in seconds (fixed ui32) + 0x06, // histogram buckets count (varint) + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, // histogram bucket bounds (array of doubles) + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x40, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x40, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x40, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0x7f, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // histogram bucket values + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + }; + ui8 expectedMetric6[] = { 0x12, // types (IGAUGE | ONE_WITH_TS) (fixed ui8) 0x00, // flags (fixed ui8) @@ -292,9 +292,9 @@ Y_UNIT_TEST_SUITE(TSpackTest) { e->OnLabel("name", "q2"); e->OnLabelsEnd(); } - // Only the last value will be encoded - e->OnUint64(TInstant::Zero(), 10); - e->OnUint64(TInstant::Zero(), 13); + // Only the last value will be encoded + e->OnUint64(TInstant::Zero(), 10); + e->OnUint64(TInstant::Zero(), 13); e->OnUint64(TInstant::Zero(), 17); e->OnMetricEnd(); } @@ -305,8 +305,8 @@ Y_UNIT_TEST_SUITE(TSpackTest) { e->OnLabel("name", "q3"); e->OnLabelsEnd(); } - e->OnUint64(now, 10); - e->OnUint64(now, 13); + e->OnUint64(now, 10); + e->OnUint64(now, 13); e->OnUint64(now, 17); e->OnMetricEnd(); } @@ -410,98 +410,98 @@ Y_UNIT_TEST_SUITE(TSpackTest) { } NProto::TMultiSamplesList GetMergingMetricSamples(EMetricsMergingMode mergingMode) { - TBuffer buffer; - TBufferOutput out(buffer); - - auto e = EncoderSpackV1( - &out, - ETimePrecision::SECONDS, - ECompression::IDENTITY, - mergingMode - ); - - e->OnStreamBegin(); - for (size_t i = 0; i != 3; ++i) { + TBuffer buffer; + TBufferOutput out(buffer); + + auto e = EncoderSpackV1( + &out, + ETimePrecision::SECONDS, + ECompression::IDENTITY, + mergingMode + ); + + e->OnStreamBegin(); + for (size_t i = 0; i != 3; ++i) { e->OnMetricBegin(EMetricType::COUNTER); - { - e->OnLabelsBegin(); + { + e->OnLabelsBegin(); e->OnLabel("name", "my_counter"); - e->OnLabelsEnd(); - } - e->OnUint64(TInstant::Zero() + TDuration::Seconds(i), i + 1); + e->OnLabelsEnd(); + } + e->OnUint64(TInstant::Zero() + TDuration::Seconds(i), i + 1); e->OnMetricEnd(); - } - e->OnStreamEnd(); - e->Close(); - - NProto::TMultiSamplesList samples; + } + e->OnStreamEnd(); + e->Close(); + + NProto::TMultiSamplesList samples; IMetricEncoderPtr eProto = EncoderProtobuf(&samples); - TBufferInput in(buffer); - DecodeSpackV1(&in, eProto.Get()); - - return samples; - } - + TBufferInput in(buffer); + DecodeSpackV1(&in, eProto.Get()); + + return samples; + } + Y_UNIT_TEST(SpackEncoderMergesMetrics) { - { + { NProto::TMultiSamplesList samples = GetMergingMetricSamples(EMetricsMergingMode::DEFAULT); - - UNIT_ASSERT_EQUAL(samples.SamplesSize(), 3); - UNIT_ASSERT_EQUAL(samples.GetSamples(0).GetPoints(0).GetUint64(), 1); - UNIT_ASSERT_EQUAL(samples.GetSamples(1).GetPoints(0).GetUint64(), 2); - UNIT_ASSERT_EQUAL(samples.GetSamples(2).GetPoints(0).GetUint64(), 3); - } - - { + + UNIT_ASSERT_EQUAL(samples.SamplesSize(), 3); + UNIT_ASSERT_EQUAL(samples.GetSamples(0).GetPoints(0).GetUint64(), 1); + UNIT_ASSERT_EQUAL(samples.GetSamples(1).GetPoints(0).GetUint64(), 2); + UNIT_ASSERT_EQUAL(samples.GetSamples(2).GetPoints(0).GetUint64(), 3); + } + + { NProto::TMultiSamplesList samples = GetMergingMetricSamples(EMetricsMergingMode::MERGE_METRICS); - - UNIT_ASSERT_EQUAL(samples.SamplesSize(), 1); - - auto sample0 = samples.GetSamples(0); - UNIT_ASSERT_EQUAL(sample0.GetPoints(0).GetUint64(), 1); - UNIT_ASSERT_EQUAL(sample0.GetPoints(1).GetUint64(), 2); - UNIT_ASSERT_EQUAL(sample0.GetPoints(2).GetUint64(), 3); - } - } - - void DecodeDataToSamples(NProto::TMultiSamplesList & samples, ui16 version) { + + UNIT_ASSERT_EQUAL(samples.SamplesSize(), 1); + + auto sample0 = samples.GetSamples(0); + UNIT_ASSERT_EQUAL(sample0.GetPoints(0).GetUint64(), 1); + UNIT_ASSERT_EQUAL(sample0.GetPoints(1).GetUint64(), 2); + UNIT_ASSERT_EQUAL(sample0.GetPoints(2).GetUint64(), 3); + } + } + + void DecodeDataToSamples(NProto::TMultiSamplesList & samples, ui16 version) { IMetricEncoderPtr e = EncoderProtobuf(&samples); - TBuffer data(expectedSize); + TBuffer data(expectedSize); if (SV1_00 == version) { // v1.0 - data.Append(reinterpret_cast<char*>(expectedHeader_v1_0), Y_ARRAY_SIZE(expectedHeader_v1_0)); - } else { + data.Append(reinterpret_cast<char*>(expectedHeader_v1_0), Y_ARRAY_SIZE(expectedHeader_v1_0)); + } else { data.Append(reinterpret_cast<char*>(expectedHeader), Y_ARRAY_SIZE(expectedHeader)); - } - data.Append(reinterpret_cast<char*>(expectedStringPools), Y_ARRAY_SIZE(expectedStringPools)); - data.Append(reinterpret_cast<char*>(expectedCommonTime), Y_ARRAY_SIZE(expectedCommonTime)); - data.Append(reinterpret_cast<char*>(expectedCommonLabels), Y_ARRAY_SIZE(expectedCommonLabels)); + } + data.Append(reinterpret_cast<char*>(expectedStringPools), Y_ARRAY_SIZE(expectedStringPools)); + data.Append(reinterpret_cast<char*>(expectedCommonTime), Y_ARRAY_SIZE(expectedCommonTime)); + data.Append(reinterpret_cast<char*>(expectedCommonLabels), Y_ARRAY_SIZE(expectedCommonLabels)); data.Append(reinterpret_cast<char*>(expectedMetric1), Y_ARRAY_SIZE(expectedMetric1)); data.Append(reinterpret_cast<char*>(expectedMetric2), Y_ARRAY_SIZE(expectedMetric2)); data.Append(reinterpret_cast<char*>(expectedMetric3), Y_ARRAY_SIZE(expectedMetric3)); data.Append(reinterpret_cast<char*>(expectedMetric4), Y_ARRAY_SIZE(expectedMetric4)); if (SV1_00 == version) { // v1.0 data.Append(reinterpret_cast<char*>(expectedMetric5_v1_0), Y_ARRAY_SIZE(expectedMetric5_v1_0)); - } else { + } else { data.Append(reinterpret_cast<char*>(expectedMetric5), Y_ARRAY_SIZE(expectedMetric5)); } data.Append(reinterpret_cast<char*>(expectedMetric6), Y_ARRAY_SIZE(expectedMetric6)); data.Append(reinterpret_cast<char*>(expectedMetric7), Y_ARRAY_SIZE(expectedMetric7)); data.Append(reinterpret_cast<char*>(expectedMetric8), Y_ARRAY_SIZE(expectedMetric8)); - TBufferInput in(data); - DecodeSpackV1(&in, e.Get()); - } - - void DecodeDataToSamples(NProto::TMultiSamplesList & samples) { - TSpackHeader header; + TBufferInput in(data); + DecodeSpackV1(&in, e.Get()); + } + + void DecodeDataToSamples(NProto::TMultiSamplesList & samples) { + TSpackHeader header; header.Version = SV1_01; - DecodeDataToSamples(samples, header.Version); - } - - Y_UNIT_TEST(Decode) { - NProto::TMultiSamplesList samples; - DecodeDataToSamples(samples); - + DecodeDataToSamples(samples, header.Version); + } + + Y_UNIT_TEST(Decode) { + NProto::TMultiSamplesList samples; + DecodeDataToSamples(samples); + UNIT_ASSERT_VALUES_EQUAL( TInstant::MilliSeconds(samples.GetCommonTime()), TInstant::Seconds(1500000000)); @@ -684,20 +684,20 @@ Y_UNIT_TEST_SUITE(TSpackTest) { Y_UNIT_TEST(CompressionLz4) { TestCompression(ECompression::LZ4); } - - Y_UNIT_TEST(Decode_v1_0_histograms) { - // Check that histogram bounds decoded from different versions are the same - NProto::TMultiSamplesList samples, samples_v1_0; - DecodeDataToSamples(samples); + + Y_UNIT_TEST(Decode_v1_0_histograms) { + // Check that histogram bounds decoded from different versions are the same + NProto::TMultiSamplesList samples, samples_v1_0; + DecodeDataToSamples(samples); DecodeDataToSamples(samples_v1_0, /*version = */ SV1_00); - - const NProto::THistogram& pointHistogram = samples.GetSamples(4).GetPoints(0).GetHistogram(); - const NProto::THistogram& pointHistogram_v1_0 = samples_v1_0.GetSamples(4).GetPoints(0).GetHistogram(); - - for (size_t i = 0; i < pointHistogram.BoundsSize(); i++) { - UNIT_ASSERT_DOUBLES_EQUAL(pointHistogram.GetBounds(i), pointHistogram_v1_0.GetBounds(i), Min<double>()); - } - } + + const NProto::THistogram& pointHistogram = samples.GetSamples(4).GetPoints(0).GetHistogram(); + const NProto::THistogram& pointHistogram_v1_0 = samples_v1_0.GetSamples(4).GetPoints(0).GetHistogram(); + + for (size_t i = 0; i < pointHistogram.BoundsSize(); i++) { + UNIT_ASSERT_DOUBLES_EQUAL(pointHistogram.GetBounds(i), pointHistogram_v1_0.GetBounds(i), Min<double>()); + } + } Y_UNIT_TEST(SimpleV12) { ui8 expectedSerialized[] = { diff --git a/library/cpp/monlib/encode/spack/ya.make b/library/cpp/monlib/encode/spack/ya.make index f6439c5b03..78d3061291 100644 --- a/library/cpp/monlib/encode/spack/ya.make +++ b/library/cpp/monlib/encode/spack/ya.make @@ -13,9 +13,9 @@ SRCS( ) PEERDIR( - library/cpp/monlib/encode/buffered - library/cpp/monlib/exception - + library/cpp/monlib/encode/buffered + library/cpp/monlib/exception + contrib/libs/lz4 contrib/libs/xxhash contrib/libs/zlib |