diff --git a/dbms/src/Formats/ProtobufWriter.cpp b/dbms/src/Formats/ProtobufWriter.cpp index bca3449cb59..fcaedcab2a0 100644 --- a/dbms/src/Formats/ProtobufWriter.cpp +++ b/dbms/src/Formats/ProtobufWriter.cpp @@ -141,7 +141,8 @@ void ProtobufWriter::SimpleWriter::endMessage() size_t size_of_message = buffer.size() - num_bytes_skipped; writeVarint(size_of_message, out); for (const auto & piece : pieces) - out.write(reinterpret_cast(&buffer[piece.start]), piece.end - piece.start); + if (piece.end > piece.start) + out.write(reinterpret_cast(&buffer[piece.start]), piece.end - piece.start); buffer.clear(); pieces.clear(); num_bytes_skipped = 0; diff --git a/libs/libdaemon/src/BaseDaemon.cpp b/libs/libdaemon/src/BaseDaemon.cpp index 1ba03168496..aa4993acead 100644 --- a/libs/libdaemon/src/BaseDaemon.cpp +++ b/libs/libdaemon/src/BaseDaemon.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -501,6 +502,35 @@ void BaseDaemon::closeFDs() } } +namespace +{ +/// In debug version on Linux, increase oom score so that clickhouse is killed +/// first, instead of some service. Use a carefully chosen random score of 555: +/// the maximum is 1000, and chromium uses 300 for its tab processes. Ignore +/// whatever errors that occur, because it's just a debugging aid and we don't +/// care if it breaks. +#if defined(__linux__) && !defined(NDEBUG) +void debugIncreaseOOMScore() +{ + const std::string new_score = "555"; + try + { + DB::WriteBufferFromFile buf("/proc/self/oom_score_adj"); + buf.write(new_score.c_str(), new_score.size()); + } + catch (const Poco::Exception & e) + { + LOG_WARNING(&Logger::root(), "Failed to adjust OOM score: '" + + e.displayText() + "'."); + return; + } + LOG_INFO(&Logger::root(), "Set OOM score adjustment to " + new_score); +} +#else +void debugIncreaseOOMScore() {} +#endif +} + void BaseDaemon::initialize(Application & self) { closeFDs(); @@ -630,6 +660,7 @@ void BaseDaemon::initialize(Application & self) initializeTerminationAndSignalProcessing(); logRevision(); + debugIncreaseOOMScore(); for (const auto & key : DB::getMultipleKeysFromConfig(config(), "", "graphite")) {