From 38cda75d97345d4f4be4043bca2204a1209d1506 Mon Sep 17 00:00:00 2001 From: Stas Klinov Date: Tue, 5 Nov 2024 18:12:50 +0100 Subject: [PATCH] added log of the SphinxQL query that successfully processed by buddy into the query log; fixed #2235 --- src/searchd.cpp | 24 ++++++++++++++++++ src/searchdaemon.h | 1 + src/searchdbuddy.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) diff --git a/src/searchd.cpp b/src/searchd.cpp index 6db4da4092..c21a98ac25 100644 --- a/src/searchd.cpp +++ b/src/searchd.cpp @@ -3422,6 +3422,30 @@ void LogSphinxqlError ( const char * sStmt, const Str_t& sError ) sphWrite ( g_iQueryLogFile, tBuf.cstr(), tBuf.GetLength() ); } +void LogSphinxqlBuddyQuery ( const Str_t sQuery, const CSphQueryResultMeta & tMeta ) +{ + if ( g_eLogFormat!=LOG_FORMAT_SPHINXQL || g_iQueryLogFile<0 || IsEmpty ( sQuery ) ) + return; + + StringBuilder_c tBuf; + + // time, conn id, wall, found + int iQueryTime = Max ( tMeta.m_iQueryTime, 0 ); + int iRealTime = Max ( tMeta.m_iRealQueryTime, 0 ); + + tBuf << "/* "; + FormatTimeConnClient ( tBuf ); + tBuf << " real " << FixedFrac ( iRealTime ) << " wall " << FixedFrac ( iQueryTime ); + + if ( tMeta.m_iMultiplier>1 ) + tBuf << " x" << tMeta.m_iMultiplier; + tBuf << " found " << tMeta.m_iTotalMatches << " */ "; + + tBuf << sQuery << '\n'; + + sphSeek ( g_iQueryLogFile, 0, SEEK_END ); + sphWrite ( g_iQueryLogFile, tBuf.cstr(), tBuf.GetLength() ); +} void ReportIndexesName ( int iSpanStart, int iSpandEnd, const CSphVector & dLog, StringBuilder_c & sOut ) { diff --git a/src/searchdaemon.h b/src/searchdaemon.h index ddb79af1b4..00bcc0c558 100644 --- a/src/searchdaemon.h +++ b/src/searchdaemon.h @@ -1454,6 +1454,7 @@ namespace session } void LogSphinxqlError ( const char * sStmt, const Str_t& sError ); +void LogSphinxqlBuddyQuery ( const Str_t sQuery, const CSphQueryResultMeta & tMeta ); int GetDaemonLogBufSize (); // that is used from sphinxql command over API diff --git a/src/searchdbuddy.cpp b/src/searchdbuddy.cpp index 70e14a21f2..20e4736546 100644 --- a/src/searchdbuddy.cpp +++ b/src/searchdbuddy.cpp @@ -768,6 +768,65 @@ static bool ConvertErrorMessage ( const char * sStmt, std::pair tSave return true; } +template +bool ConvertValue ( const char * sName, const JsonObj_c & tMeta, T & tVal ) +{ + JsonObj_c tSrcVal = tMeta.GetItem ( sName ); + if ( !tSrcVal ) + return false; + + if ( !tSrcVal.IsStr() ) + return false; + + int64_t iVal = 0; + double fVal = 0.0; + ESphJsonType eType; + if ( !sphJsonStringToNumber ( tSrcVal.SzVal(), strlen ( tSrcVal.SzVal() ), eType, iVal, fVal ) ) + return false; + + if ( eType==JSON_INT64 ) + tVal = (T)iVal; + else + tVal = (T)fVal; + return true; +} + +static void LogBuddyQuery ( const Str_t sSrcQuery, const JsonObj_c & tBudyyReply ) +{ + CSphFixedVector dBuf ( sSrcQuery.second + CSphString::GetGap() ); + const char * sCur = sSrcQuery.first; + const char * sEnd = sCur + sSrcQuery.second; + BYTE * sDst = dBuf.Begin(); + + while ( sCur m_iMatches + ConvertValue ( "total", tSrcMeta, tLogMeta.m_iMatches ); + + // total_found => m_iTotalMatches + ConvertValue ( "total_found", tSrcMeta, tLogMeta.m_iTotalMatches ); + + // time => m_iQueryTime \ m_iRealQueryTime + float fTime = 0.0f; + if ( ConvertValue ( "time", tSrcMeta, fTime ) ) + tLogMeta.m_iRealQueryTime = tLogMeta.m_iQueryTime = (int)( fTime * 1000.0f ); + + // total_relation => null + } + + LogSphinxqlBuddyQuery ( sLogQuery, tLogMeta ); +} + void ProcessSqlQueryBuddy ( Str_t sSrcQuery, Str_t tError, std::pair tSavedPos, BYTE & uPacketID, GenericOutputBuffer_c & tOut ) { auto tReplyRaw = BuddyQuery ( false, tError, Str_t(), sSrcQuery, HTTP_GET, VecTraits_T() ); @@ -809,6 +868,7 @@ void ProcessSqlQueryBuddy ( Str_t sSrcQuery, Str_t tError, std::pair std::unique_ptr tBuddyRows ( CreateSqlRowBuffer ( &uPacketID, &tOut ) ); ConvertJsonDataset ( tReplyParsed.m_tMessage, sSrcQuery.first, *tBuddyRows ); + LogBuddyQuery ( sSrcQuery, tReplyParsed.m_tRoot ); } #ifdef _WIN32