Skip to content

Commit

Permalink
[Enhancement] upgrade some thirdparty
Browse files Browse the repository at this point in the history
upgrade brpc to 1.9.0
upgrade glog to 0.7.1

brpc has support thrift extends https://github.com/apache/brpc/blob/master/docs/en/thrift.md
we could use brpc async interface instead of using thread pool simulate an async interface

glog has support change vlog level dynamic in google/glog#650

Signed-off-by: stdpain <[email protected]>
  • Loading branch information
stdpain committed Jul 12, 2024
1 parent 5de7f8a commit c89717a
Show file tree
Hide file tree
Showing 6 changed files with 339 additions and 20 deletions.
2 changes: 0 additions & 2 deletions be/src/service/service_be/starrocks_be.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,7 @@ void start_be(const std::vector<StorePath>& paths, bool as_cn) {
brpc::FLAGS_max_body_size = config::brpc_max_body_size;

// Configure keepalive.
#ifdef WITH_BRPC_KEEPALIVE
brpc::FLAGS_socket_keepalive = config::brpc_socket_keepalive;
#endif

brpc::FLAGS_socket_max_unwritten_bytes = config::brpc_socket_max_unwritten_bytes;
auto brpc_server = std::make_unique<brpc::Server>();
Expand Down
3 changes: 0 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ WITH_GCOV=OFF
WITH_BENCH=OFF
WITH_CLANG_TIDY=OFF
WITH_STARCACHE=ON
WITH_BRPC_KEEPALIVE=OFF
USE_STAROS=OFF
BUILD_JAVA_EXT=ON
OUTPUT_COMPILE_TIME=OFF
Expand Down Expand Up @@ -236,7 +235,6 @@ else
--with-clang-tidy) WITH_CLANG_TIDY=ON; shift ;;
--without-java-ext) BUILD_JAVA_EXT=OFF; shift ;;
--without-starcache) WITH_STARCACHE=OFF; shift ;;
--with-brpc-keepalive) WITH_BRPC_KEEPALIVE=ON; shift ;;
--output-compile-time) OUTPUT_COMPILE_TIME=ON; shift ;;
-h) HELP=1; shift ;;
--help) HELP=1; shift ;;
Expand Down Expand Up @@ -372,7 +370,6 @@ if [ ${BUILD_BE} -eq 1 ] ; then
-DWITH_CLANG_TIDY=${WITH_CLANG_TIDY} \
-DWITH_COMPRESS=${WITH_COMPRESS} \
-DWITH_STARCACHE=${WITH_STARCACHE} \
-DWITH_BRPC_KEEPALIVE=${WITH_BRPC_KEEPALIVE} \
-DUSE_STAROS=${USE_STAROS} \
-DENABLE_FAULT_INJECTION=${ENABLE_FAULT_INJECTION} \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
Expand Down
9 changes: 2 additions & 7 deletions thirdparty/build-thirdparty.sh
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,8 @@ build_glog() {
check_if_source_exist $GLOG_SOURCE
cd $TP_SOURCE_DIR/$GLOG_SOURCE

# to generate config.guess and config.sub to support aarch64
rm -rf config.*
autoreconf -i
$CMAKE_CMD -G "${CMAKE_GENERATOR}" -DCMAKE_INSTALL_PREFIX=$TP_INSTALL_DIR -DBUILD_SHARED_LIBS=OFF -DCMAKE_POSITION_INDEPENDENT_CODE=ON

LDFLAGS="-L${TP_LIB_DIR}" \
CPPFLAGS="-I${TP_INCLUDE_DIR}" \
./configure --prefix=$TP_INSTALL_DIR --enable-frame-pointers --disable-shared --enable-static
make -j$PARALLEL
make install
}
Expand Down Expand Up @@ -560,7 +555,7 @@ build_brpc() {
cd $TP_SOURCE_DIR/$BRPC_SOURCE
CMAKE_GENERATOR="Unix Makefiles"
BUILD_SYSTEM='make'
./config_brpc.sh --headers="$TP_INSTALL_DIR/include /usr/include" --libs="$TP_INSTALL_DIR/bin $TP_INSTALL_DIR/lib /usr/lib" --with-glog
PATH=$PATH:$TP_INSTALL_DIR/bin/ ./config_brpc.sh --headers="$TP_INSTALL_DIR/include /usr/include" --libs="$TP_INSTALL_DIR/bin $TP_INSTALL_DIR/lib /usr/lib" --with-glog --with-thrift
make -j$PARALLEL
cp -rf output/* ${TP_INSTALL_DIR}/
if [ -f $TP_INSTALL_DIR/lib/libbrpc.a ]; then
Expand Down
4 changes: 4 additions & 0 deletions thirdparty/download-thirdparty.sh
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ if [ ! -f $PATCHED_MARK ] && [ $GLOG_SOURCE == "glog-0.4.0" ]; then
patch -p1 < $TP_PATCH_DIR/glog-0.4.0-remove-unwind-dependency.patch
touch $PATCHED_MARK
fi
if [ ! -f $PATCHED_MARK ] && [ $GLOG_SOURCE == "glog-0.7.1" ]; then
patch -p1 < $TP_PATCH_DIR/glog-0.7.1.patch
touch $PATCHED_MARK
fi
cd -
echo "Finished patching $GLOG_SOURCE"

Expand Down
325 changes: 325 additions & 0 deletions thirdparty/patches/glog-0.7.1.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,325 @@
commit 1ffb05c3d13cde8cf527105a137bde08e9dacd9e
Author: stdpain <[email protected]>
Date: Fri Jul 12 13:55:27 2024 +0800

patch for starrocks

diff --git a/src/flags.cc b/src/flags.cc
index c4c2aa4..7329c80 100644
--- a/src/flags.cc
+++ b/src/flags.cc
@@ -137,6 +137,10 @@ GLOG_DEFINE_uint32(max_log_size, 1800,
GLOG_DEFINE_bool(stop_logging_if_full_disk, false,
"Stop attempting to log to disk if the disk is full.");

+GLOG_DEFINE_string(log_split_method, "day", "split log by size, day, hour");
+
+GLOG_DEFINE_int32(log_filenum_quota, 10, "max log file num in log dir");
+
GLOG_DEFINE_string(log_backtrace_at, "",
"Emit a backtrace when logging at file:linenum.");

diff --git a/src/glog/flags.h b/src/glog/flags.h
index cb542b9..31d5e6e 100644
--- a/src/glog/flags.h
+++ b/src/glog/flags.h
@@ -182,6 +182,10 @@ DECLARE_string(logmailer);

DECLARE_bool(symbolize_stacktrace);

+DECLARE_int32(log_filenum_quota);
+
+DECLARE_string(log_split_method);
+
#pragma pop_macro("DECLARE_VARIABLE")
#pragma pop_macro("DECLARE_bool")
#pragma pop_macro("DECLARE_string")
diff --git a/src/logging.cc b/src/logging.cc
index fc63995..16b2cb1 100644
--- a/src/logging.cc
+++ b/src/logging.cc
@@ -31,6 +31,7 @@

#include "glog/logging.h"

+#include <list>
#include <algorithm>
#include <cassert>
#include <chrono>
@@ -443,6 +444,14 @@ class PrefixFormatter {

std::unique_ptr<PrefixFormatter> g_prefix_formatter;

+typedef struct filetime {
+ std::string name;
+ time_t time;
+ bool operator < (const struct filetime& o) const {
+ return o.time > time;
+ }
+}Filetime;
+
// Encapsulates all file-system related state
class LogFileObject : public base::Logger {
public:
@@ -474,6 +483,8 @@ class LogFileObject : public base::Logger {
// acquiring lock_.
void FlushUnlocked(const std::chrono::system_clock::time_point& now);

+ void CheckFileNumQuota();
+
private:
static const uint32 kRolloverAttemptFrequency = 0x20;

@@ -496,6 +507,10 @@ class LogFileObject : public base::Logger {
// optional argument time_pid_string
// REQUIRES: lock_ is held
bool CreateLogfile(const string& time_pid_string);
+
+ std::list<Filetime> file_list_;
+ bool inited_;
+ struct ::tm tm_time_;
};

// Encapsulate all log cleaner related states
@@ -707,7 +722,7 @@ inline void LogDestination::FlushLogFiles(int min_severity) {
// all this stuff.
std::lock_guard<std::mutex> l{log_mutex};
for (int i = min_severity; i < NUM_SEVERITIES; i++) {
- LogDestination* log = log_destination(static_cast<LogSeverity>(i));
+ LogDestination* log = log_destinations_[i].get();
if (log != nullptr) {
log->logger_->Flush();
}
@@ -904,8 +919,12 @@ inline void LogDestination::LogToAllLogfiles(
ColoredWriteToStderr(severity, message, len);
} else {
for (int i = severity; i >= 0; --i) {
- LogDestination::MaybeLogToLogfile(static_cast<LogSeverity>(i), timestamp,
- message, len);
+ if (severity >= 1) {
+ LogDestination::MaybeLogToLogfile(static_cast<LogSeverity>(1), timestamp, message, len);
+ LogDestination::MaybeLogToLogfile(static_cast<LogSeverity>(0), timestamp, message, len);
+ } else if (severity == 0) {
+ LogDestination::MaybeLogToLogfile(static_cast<LogSeverity>(0), timestamp, message, len);
+ } else {}
}
}
}
@@ -997,7 +1016,8 @@ LogFileObject::LogFileObject(LogSeverity severity, const char* base_filename)
filename_extension_(),
severity_(severity),
rollover_attempt_(kRolloverAttemptFrequency - 1),
- start_time_(std::chrono::system_clock::now()) {}
+ start_time_(std::chrono::system_clock::now()),
+ inited_(false) {}

LogFileObject::~LogFileObject() {
std::lock_guard<std::mutex> l{mutex_};
@@ -1058,14 +1078,9 @@ bool LogFileObject::CreateLogfile(const string& time_pid_string) {
}
string_filename += filename_extension_;
const char* filename = string_filename.c_str();
- // only write to files, create if non-existant.
- int flags = O_WRONLY | O_CREAT;
- if (FLAGS_timestamp_in_logfile_name) {
- // demand that the file is unique for our timestamp (fail if it exists).
- flags = flags | O_EXCL;
- }
- FileDescriptor fd{
- open(filename, flags, static_cast<mode_t>(FLAGS_logfile_mode))};
+ int fdh = open(filename, O_WRONLY | O_CREAT /* | O_EXCL */ | O_APPEND, 0664);
+ FileDescriptor fd{fdh};
+
if (!fd) return false;
#ifdef HAVE_FCNTL
// Mark the file close-on-exec. We don't really care if this fails
@@ -1112,6 +1127,9 @@ bool LogFileObject::CreateLogfile(const string& time_pid_string) {
}
}
#endif
+ Filetime ft;
+ ft.name = string_filename;
+ file_list_.push_back(ft);
// We try to create a symlink called <program_name>.<severity>,
// which is easier to use. (Every time we create a new logfile,
// we destroy the old symlink and create a new one, so it always
@@ -1155,6 +1173,55 @@ bool LogFileObject::CreateLogfile(const string& time_pid_string) {
return true; // Everything worked
}

+void LogFileObject::CheckFileNumQuota() {
+ struct dirent *entry;
+ DIR *dp;
+ const vector<string> & log_dirs = GetLoggingDirectories();
+ if (log_dirs.size() < 1) return;
+
+ //fprintf(stderr, "log dir: %s\n", log_dirs[0].c_str());
+ // list file in log dir
+
+ dp = opendir(log_dirs[0].c_str());
+ if (dp == NULL) {
+ fprintf(stderr, "open log dir %s fail\n", log_dirs[0].c_str());
+ return;
+ }
+
+ file_list_.clear();
+ while ((entry = readdir(dp)) != NULL) {
+ if (DT_DIR == entry->d_type ||
+ DT_LNK == entry->d_type) {
+ continue;
+ }
+ std::string filename = std::string(entry->d_name);
+ //fprintf(stderr, "filename: %s\n", filename.c_str());
+ if (filename.find(symlink_basename_ + '.' + LogSeverityNames[severity_]) == 0) {
+ std::string filepath = log_dirs[0] + "/" + filename;
+ struct stat fstat;
+ if (::stat(filepath.c_str(), &fstat) < 0) {
+ fprintf(stderr, "state %s fail\n", filepath.c_str());
+ closedir(dp);
+ return;
+ }
+ //fprintf(stderr, "filepath: %s\n", filepath.c_str());
+ Filetime file_time;
+ file_time.time = fstat.st_mtime;
+ file_time.name = filepath;
+ file_list_.push_back(file_time);
+ }
+ }
+
+ closedir(dp);
+ file_list_.sort();
+
+ while (FLAGS_log_filenum_quota > 0 && file_list_.size() >= FLAGS_log_filenum_quota) {
+ // fprintf(stderr, "delete %s\n", file_list_.front().name.c_str());
+ unlink(file_list_.front().name.c_str());
+ file_list_.pop_front();
+ }
+}
+
void LogFileObject::Write(
bool force_flush, const std::chrono::system_clock::time_point& timestamp,
const char* message, size_t message_len) {
@@ -1171,16 +1238,58 @@ void LogFileObject::Write(
filename_extension_);
}
};
+ std::time_t t = std::chrono::system_clock::to_time_t(timestamp);

// Remove old logs
ScopedExit<decltype(cleanupLogs)> cleanupAtEnd{cleanupLogs};

- if (file_length_ >> 20U >= MaxLogSize() || PidHasChanged()) {
+ struct ::tm tm_time;
+ bool is_split = false;
+ if ("day" == FLAGS_log_split_method) {
+ localtime_r(&t, &tm_time);
+ if (tm_time.tm_year != tm_time_.tm_year
+ || tm_time.tm_mon != tm_time_.tm_mon
+ || tm_time.tm_mday != tm_time_.tm_mday) {
+ is_split = true;
+ }
+ } else if ("hour" == FLAGS_log_split_method) {
+ localtime_r(&t, &tm_time);
+ if (tm_time.tm_year != tm_time_.tm_year
+ || tm_time.tm_mon != tm_time_.tm_mon
+ || tm_time.tm_mday != tm_time_.tm_mday
+ || tm_time.tm_hour != tm_time_.tm_hour) {
+ is_split = true;
+ }
+ } else if (static_cast<int>(file_length_ >> 20) >= MaxLogSize()) {
+ // PidHasChanged()) {
+ is_split = true;
+ }
+
+ if (is_split) {
file_ = nullptr;
file_length_ = bytes_since_flush_ = dropped_mem_length_ = 0;
rollover_attempt_ = kRolloverAttemptFrequency - 1;
}

+ if ((file_ == NULL) && (!inited_) && (FLAGS_log_split_method == "size")) {
+ CheckFileNumQuota();
+ const char* filename = file_list_.back().name.c_str();
+ int fd = open(filename, O_WRONLY | O_CREAT /* | O_EXCL */ | O_APPEND, 0664);
+ if (fd != -1) {
+#ifdef HAVE_FCNTL
+ // Mark the file close-on-exec. We don't really care if this fails
+ fcntl(fd, F_SETFD, FD_CLOEXEC);
+#endif
+ file_.reset(fopen(filename, "a+"));
+ if (file_ == NULL) { // Man, we're screwed!, try to create new log file
+ close(fd);
+ }
+ fseek(file_.get(), 0, SEEK_END);
+ file_length_ = bytes_since_flush_ = ftell(file_.get());
+ inited_ = true;
+ }
+ }
+
// If there's no destination file, make one before outputting
if (file_ == nullptr) {
// Try to rollover the log file every 32 log messages. The only time
@@ -1190,21 +1299,35 @@ void LogFileObject::Write(
rollover_attempt_ = 0;

struct ::tm tm_time;
- std::time_t t = std::chrono::system_clock::to_time_t(timestamp);

- if (FLAGS_log_utc_time) {
- gmtime_r(&t, &tm_time);
+ if (!inited_) {
+ CheckFileNumQuota();
+ inited_ = true;
} else {
- localtime_r(&t, &tm_time);
+ while (FLAGS_log_filenum_quota > 0 && file_list_.size() >= FLAGS_log_filenum_quota) {
+ unlink(file_list_.front().name.c_str());
+ file_list_.pop_front();
+ }
}
+ localtime_r(&t, &tm_time);

// The logfile's filename will have the date/time & pid in it
ostringstream time_pid_stream;
time_pid_stream.fill('0');
- time_pid_stream << 1900 + tm_time.tm_year << setw(2) << 1 + tm_time.tm_mon
- << setw(2) << tm_time.tm_mday << '-' << setw(2)
- << tm_time.tm_hour << setw(2) << tm_time.tm_min << setw(2)
- << tm_time.tm_sec << '.' << GetMainThreadPid();
+ time_pid_stream << 1900 + tm_time.tm_year
+ << setw(2) << 1 + tm_time.tm_mon
+ << setw(2) << tm_time.tm_mday;
+
+ if ("hour" == FLAGS_log_split_method) {
+ time_pid_stream << setw(2) << tm_time.tm_hour;
+ } else if ("day" != FLAGS_log_split_method) {
+ time_pid_stream << '-'
+ << setw(2) << tm_time.tm_hour
+ << setw(2) << tm_time.tm_min
+ << setw(2) << tm_time.tm_sec;
+ }
+
+ tm_time_ = tm_time;
const string& time_pid_string = time_pid_stream.str();

if (base_filename_selected_) {
@@ -1238,8 +1361,8 @@ void LogFileObject::Write(
// deadlock. Simply use a name like invalid-user.
if (uidname.empty()) uidname = "invalid-user";

- stripped_filename = stripped_filename + '.' + hostname + '.' + uidname +
- ".log." + LogSeverityNames[severity_] + '.';
+ stripped_filename = stripped_filename + "." + LogSeverityNames[severity_] + ".log.";
+
// We're going to (potentially) try to put logs in several different dirs
const vector<string>& log_dirs = GetLoggingDirectories();

@@ -1263,7 +1386,7 @@ void LogFileObject::Write(
}

// Write a header message into the log file
- if (FLAGS_log_file_header) {
+ if (false) {
ostringstream file_header_stream;
file_header_stream.fill('0');
file_header_stream << "Log file created at: " << 1900 + tm_time.tm_year
Loading

0 comments on commit c89717a

Please sign in to comment.