Skip to content

Sync log-level with buddy #3391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/searchd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8336,7 +8336,12 @@ static bool HandleSetGlobal ( CSphString & sError, const CSphString & sName, int
if ( !HttpSetLogVerbosity ( sSetValue ) )
sError = "Unknown log_level value (http_on, http_off, http_bad_req_on, http_bad_req_off)";
} else
{
sError = "Unknown log_level value (must be one of info, debug, debugv, debugvv, replication)";
}
if ( sError.IsEmpty() )
BuddySetLogLevel ( g_eLogLevel );

return true;
}

Expand Down
61 changes: 56 additions & 5 deletions src/searchdbuddy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ static BuddyState_e TryToStart ( const char * sArgs, CSphString & sError );
static CSphString GetUrl ( const ListenerDesc_t & tDesc );
static CSphString BuddyGetPath ( const CSphString & sPath, const CSphString & sPluginDir, bool bHasBuddyPath, int iHostPort, const CSphString & sDataDir );
static void BuddyStop ();
static CSphString GetLogLevel();

#if _WIN32
static CSphString g_sBuddyBind = "--bind=0.0.0.0:9999";
Expand Down Expand Up @@ -482,12 +483,14 @@ void BuddyStart ( const CSphString & sConfigPath, const CSphString & sPluginDir,
g_dLogBuf.Resize ( 0 );
g_sPath = sPath;

g_sStartArgs.SetSprintf ( "%s --listen=%s %s %s --threads=%d",
CSphString sLogLevel = GetLogLevel();

g_sStartArgs.SetSprintf ( "%s --listen=%s %s %s --threads=%d %s",
g_sPath.cstr(),
g_sListener4Buddy.cstr(),
g_sBuddyBind.scstr(),
( bTelemetry ? "" : "--disable-telemetry" ),
iThreads );
iThreads, sLogLevel.scstr() );

sphLogDebug ( "[BUDDY] start args: %s", g_sStartArgs.cstr() );

Expand Down Expand Up @@ -572,6 +575,13 @@ static bool BuddyQueryAddErrorBody ( JsonEscapedBuilder & tBuddyQuery, const Vec
return true;
}

// disable Expect: 100-continue
// as Expect: 100-continue header option added by curl library does not work with the buddy
static void DisableExpectHeader ( StrVec_t & dHeaders )
{
dHeaders.Add ( "Expect:" );
}

static std::pair<bool, CSphString> BuddyQuery ( bool bHttp, Str_t sQueryError, Str_t sPathQuery, Str_t sQuery, http_method eRequestType, const VecTraits_T<BYTE> & dSrcHttpReply )
{
if ( !HasBuddy() )
Expand Down Expand Up @@ -604,9 +614,7 @@ static std::pair<bool, CSphString> BuddyQuery ( bool bHttp, Str_t sQueryError, S

StrVec_t dHeaders;
dHeaders.Add ( SphSprintf ( "Request-ID: %d_%u", session::GetConnID(), sphCRC32 ( sQuery.first, sQuery.second, sphRand() ) ) );
// disable Expect: 100-continue
// as Expect: 100-continue header added by curl library do not with the buddy
dHeaders.Add ( "Expect:" );
DisableExpectHeader ( dHeaders );

return PostToHelperUrl ( g_sUrlBuddy, (Str_t)tBuddyQuery, dHeaders );
}
Expand Down Expand Up @@ -947,3 +955,46 @@ CSphString BuddyGetPath ( const CSphString & sConfigPath, const CSphString & sPl
return sFullPath;
}
#endif

static const char * GetBuddyLogLevel ( ESphLogLevel eLogLevel )
{
switch ( eLogLevel )
{
case SPH_LOG_DEBUG: return "debug";
case SPH_LOG_RPL_DEBUG: return "debug";
case SPH_LOG_VERBOSE_DEBUG: return "debugv";
case SPH_LOG_VERY_VERBOSE_DEBUG: return "debugvv";

default: return "info";
}
}

CSphString GetLogLevel()
{
CSphString sLogLevel;
if ( g_eLogLevel==SPH_LOG_INFO )
return sLogLevel;

sLogLevel.SetSprintf ( "--log-level=%s", GetBuddyLogLevel ( g_eLogLevel ) );
return sLogLevel;
}

void BuddySetLogLevel ( ESphLogLevel eLogLevel )
{
if ( !HasBuddy() )
return;

JsonEscapedBuilder tBuddyQuery;
{
auto tRoot = tBuddyQuery.Object();
tBuddyQuery.NamedString ( "log_level", GetBuddyLogLevel ( eLogLevel ) );
}

StrVec_t dHeaders;
DisableExpectHeader ( dHeaders );

CSphString sUrl;
sUrl.SetSprintf ( "%s/config", g_sUrlBuddy.cstr() );

PostToHelperUrl ( sUrl, (Str_t)tBuddyQuery, dHeaders );
}
1 change: 1 addition & 0 deletions src/searchdbuddy.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ bool IsBuddyQuery ( const OptionsHash_t & hOptions );

bool ProcessHttpQueryBuddy ( HttpProcessResult_t & tRes, Str_t sSrcQuery, OptionsHash_t& hOptions, CSphVector<BYTE>& dResult, bool bNeedHttpResponse, http_method eRequestType );
void ProcessSqlQueryBuddy ( Str_t sSrcQuery, Str_t sError, std::pair<int, BYTE> tSavedPos, BYTE& uPackedID, GenericOutputBuffer_c& tOut );
void BuddySetLogLevel ( ESphLogLevel eLogLevel );
82 changes: 82 additions & 0 deletions test/clt-tests/buddy/test-log-level-buddy-sync.rec
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
––– block: ../base/start-searchd-with-buddy –––
––– input –––
apt-get install jq -y > /dev/null; echo $?
––– output –––
debconf: delaying package configuration, since apt-utils is not installed
0
––– input –––
BUDDY_PORT=$(grep "started.*at http://127.0.0.1:" /var/log/manticore/searchd.log | tail -1 | sed 's/.*http:\/\/127.0.0.1:\([0-9]*\).*/\1/'); echo "Buddy API port: $BUDDY_PORT"
––– output –––
Buddy API port: %{NUMBER}
––– input –––
sleep 2; curl -s -X GET localhost:$BUDDY_PORT/config; echo
––– output –––
{"log_level":"info"}
––– input –––
curl -s -X GET localhost:$BUDDY_PORT/config | jq -r '.log_level'
––– output –––
info
––– input –––
mysql -h0 -P9306 -e "SET GLOBAL log_level=debug;"
––– output –––
––– input –––
sleep 1; curl -s -X GET localhost:$BUDDY_PORT/config | jq -r '.log_level'
––– output –––
debug
––– input –––
mysql -h0 -P9306 -e "SET GLOBAL log_level=debugv;"
––– output –––
––– input –––
sleep 1; curl -s -X GET localhost:$BUDDY_PORT/config | jq -r '.log_level'
––– output –––
debugv
––– input –––
mysql -h0 -P9306 -e "SET GLOBAL log_level=debugvv;"
––– output –––
––– input –––
sleep 1; curl -s -X GET localhost:$BUDDY_PORT/config | jq -r '.log_level'
––– output –––
debugvv
––– input –––
mysql -h0 -P9306 -e "SET GLOBAL log_level=info;"
––– output –––
––– input –––
sleep 1; curl -s -X GET localhost:$BUDDY_PORT/config | jq -r '.log_level'
––– output –––
info
––– input –––
curl -s -X POST -d '{"log_level":"debug"}' localhost:$BUDDY_PORT/config; echo
––– output –––
{"log_level":"debug"}
––– input –––
mysql -h0 -P9306 -e "SHOW VARIABLES LIKE 'log_level';"
––– output –––
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_level | debug |
+---------------+-------+
––– input –––
curl -s -X POST -d '{"log_level":"debugvv"}' localhost:$BUDDY_PORT/config; echo
––– output –––
{"log_level":"debugvv"}
––– input –––
mysql -h0 -P9306 -e "SHOW VARIABLES LIKE 'log_level';"
––– output –––
+---------------+---------+
| Variable_name | Value |
+---------------+---------+
| log_level | debugvv |
+---------------+---------+
––– input –––
curl -s -X POST -d '{"log_level":"info"}' localhost:$BUDDY_PORT/config; echo
––– output –––
{"log_level":"info"}
––– input –––
mysql -h0 -P9306 -e "SHOW VARIABLES LIKE 'log_level';"
––– output –––
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_level | info |
+---------------+-------+
Loading