searchd.cpp 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621
  1. //
  2. // $Id$
  3. //
  4. //
  5. // Copyright (c) 2001-2006, Andrew Aksyonoff. All rights reserved.
  6. //
  7. // This program is free software; you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License. You should have
  9. // received a copy of the GPL license along with this program; if you
  10. // did not, you can find it at http://www.gnu.org/
  11. //
  12. #include "sphinx.h"
  13. #include "sphinxutils.h"
  14. #include "sphinxexcerpt.h"
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <signal.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22. #include <time.h>
  23. #include <stdarg.h>
  24. #include <limits.h>
  25. /////////////////////////////////////////////////////////////////////////////
  26. #if USE_WINDOWS
  27. // Win-specific headers and calls
  28. #include <io.h>
  29. #include <winsock2.h>
  30. #define sphSockRecv(_sock,_buf,_len,_flags) ::recv(_sock,_buf,_len,_flags)
  31. #define sphSockSend(_sock,_buf,_len,_flags) ::send(_sock,_buf,_len,_flags)
  32. #define sphSockClose(_sock) ::closesocket(_sock)
  33. #else
  34. // UNIX-specific headers and calls
  35. #include <unistd.h>
  36. #include <netinet/in.h>
  37. #include <sys/file.h>
  38. #include <sys/socket.h>
  39. #include <sys/time.h>
  40. #include <sys/wait.h>
  41. #include <netdb.h>
  42. // for cache
  43. #include <zlib.h>
  44. #include <sys/mman.h>
  45. #include <md5.h>
  46. #define sphSockRecv(_sock,_buf,_len,_flags) ::recv(_sock,_buf,_len,_flags)
  47. #define sphSockSend(_sock,_buf,_len,_flags) ::send(_sock,_buf,_len,_flags)
  48. #define sphSockClose(_sock) ::close(_sock)
  49. #endif
  50. /////////////////////////////////////////////////////////////////////////////
  51. // MISC LOCAL CLASSES
  52. /////////////////////////////////////////////////////////////////////////////
  53. struct ServedIndex_t
  54. {
  55. CSphIndex * m_pIndex;
  56. const CSphSchema * m_pSchema; ///< pointer to index schema, managed by the index itself
  57. CSphDict * m_pDict;
  58. ISphTokenizer * m_pTokenizer;
  59. CSphString * m_pLockFile;
  60. CSphString * m_pIndexPath;
  61. public:
  62. ServedIndex_t ();
  63. ~ServedIndex_t ();
  64. void Reset ();
  65. };
  66. struct SearchFailure_t
  67. {
  68. public:
  69. CSphString m_sIndex; ///< searched index name
  70. CSphString m_sError; ///< search error message
  71. public:
  72. SearchFailure_t () {}
  73. SearchFailure_t ( const char * sIndex, const char * sErrorTemplate, ... )
  74. {
  75. char sBuf [ 2048 ];
  76. va_list ap;
  77. va_start ( ap, sErrorTemplate );
  78. vsnprintf ( sBuf, sizeof(sBuf), sErrorTemplate, ap );
  79. va_end ( ap );
  80. m_sIndex = sIndex;
  81. m_sError = sBuf;
  82. }
  83. public:
  84. bool operator == ( const SearchFailure_t & r ) const
  85. {
  86. return m_sIndex==r.m_sIndex && m_sError==r.m_sError;
  87. }
  88. bool operator < ( const SearchFailure_t & r ) const
  89. {
  90. int iRes = strcmp ( m_sError.cstr(), r.m_sError.cstr() );
  91. if ( !iRes )
  92. iRes = strcmp ( m_sIndex.cstr(), r.m_sIndex.cstr() );
  93. return iRes<0;
  94. }
  95. const SearchFailure_t & operator = ( const SearchFailure_t & r )
  96. {
  97. m_sIndex = r.m_sIndex;
  98. m_sError = r.m_sError;
  99. return *this;
  100. }
  101. };
  102. typedef CSphVector<SearchFailure_t,8> SearchFailuresLog_t;
  103. /////////////////////////////////////////////////////////////////////////////
  104. enum ESphLogLevel
  105. {
  106. LOG_FATAL = 0,
  107. LOG_WARNING = 1,
  108. LOG_INFO = 2
  109. };
  110. static bool g_bLogStdout = true;
  111. static int g_iLogFile = STDOUT_FILENO;
  112. static ESphLogLevel g_eLogLevel = LOG_INFO;
  113. static bool g_bCacheEnable = false;
  114. static CSphString g_sCacheDir ( "qcache" );
  115. static int g_iCacheTTL = 300;
  116. static bool g_bCacheGzip = false;
  117. static int g_iReadTimeout = 5; // sec
  118. static int g_iChildren = 0;
  119. static int g_iMaxChildren = 0;
  120. static int g_iSocket = 0;
  121. static int g_iQueryLogFile = -1;
  122. static int g_iHUP = 0;
  123. static const char * g_sPidFile = NULL;
  124. static bool g_bHeadDaemon = false;
  125. static int g_iMaxMatches = 1000;
  126. static SmallStringHash_T < ServedIndex_t > g_hIndexes;
  127. /////////////////////////////////////////////////////////////////////////////
  128. /// known commands
  129. enum SearchdCommand_e
  130. {
  131. SEARCHD_COMMAND_SEARCH = 0,
  132. SEARCHD_COMMAND_EXCERPT = 1,
  133. SEARCHD_COMMAND_UPDATE = 2,
  134. SEARCHD_COMMAND_TOTAL
  135. };
  136. /// known command versions
  137. enum
  138. {
  139. VER_COMMAND_SEARCH = 0x106,
  140. VER_COMMAND_EXCERPT = 0x100,
  141. VER_COMMAND_UPDATE = 0x100
  142. };
  143. /// known status return codes
  144. enum SearchdStatus_e
  145. {
  146. SEARCHD_OK = 0, ///< general success, command-specific reply follows
  147. SEARCHD_ERROR = 1, ///< general failure, error message follows
  148. SEARCHD_RETRY = 2, ///< temporary failure, error message follows, client should retry later
  149. SEARCHD_WARNING = 3 ///< general success, warning message and command-specific reply follow
  150. };
  151. /////////////////////////////////////////////////////////////////////////////
  152. // MACHINE-DEPENDENT STUFF
  153. /////////////////////////////////////////////////////////////////////////////
  154. #if USE_WINDOWS
  155. // Windows hacks
  156. #undef EINTR
  157. #define LOCK_EX 0
  158. #define LOCK_UN 1
  159. #define STDIN_FILENO fileno(stdin)
  160. #define STDOUT_FILENO fileno(stdout)
  161. #define STDERR_FILENO fileno(stderr)
  162. #define ETIMEDOUT WSAETIMEDOUT
  163. #define EWOULDBLOCK WSAEWOULDBLOCK
  164. #define EINPROGRESS WSAEINPROGRESS
  165. #define EINTR WSAEINTR
  166. #define socklen_t int
  167. #define vsnprintf _vsnprintf
  168. void flock ( int, int )
  169. {
  170. }
  171. void sleep ( int iSec )
  172. {
  173. Sleep ( iSec*1000 );
  174. }
  175. void ctime_r ( time_t * tNow, char * sBuf )
  176. {
  177. strcpy ( sBuf, ctime(tNow) );
  178. }
  179. int getpid ()
  180. {
  181. return 0;
  182. }
  183. #endif // USE_WINDOWS
  184. /////////////////////////////////////////////////////////////////////////////
  185. // MISC
  186. /////////////////////////////////////////////////////////////////////////////
  187. void Shutdown ()
  188. {
  189. if ( g_iSocket )
  190. sphSockClose ( g_iSocket );
  191. if ( g_sPidFile )
  192. unlink ( g_sPidFile );
  193. g_hIndexes.Reset ();
  194. }
  195. /////////////////////////////////////////////////////////////////////////////
  196. ServedIndex_t::ServedIndex_t ()
  197. {
  198. Reset ();
  199. }
  200. void ServedIndex_t::Reset ()
  201. {
  202. m_pIndex = NULL;
  203. m_pDict = NULL;
  204. m_pTokenizer= NULL;
  205. m_pLockFile = NULL;
  206. m_pIndexPath= NULL;
  207. }
  208. ServedIndex_t::~ServedIndex_t ()
  209. {
  210. if ( m_pLockFile && g_bHeadDaemon )
  211. unlink ( m_pLockFile->cstr() );
  212. SafeDelete ( m_pIndex );
  213. SafeDelete ( m_pDict );
  214. SafeDelete ( m_pTokenizer );
  215. SafeDelete ( m_pLockFile );
  216. SafeDelete ( m_pIndexPath );
  217. }
  218. /////////////////////////////////////////////////////////////////////////////
  219. // LOGGING
  220. /////////////////////////////////////////////////////////////////////////////
  221. void sphLockEx ( int iFile )
  222. {
  223. #if HAVE_LOCK_EX || USE_WINDOWS
  224. flock ( iFile, LOCK_EX );
  225. #else
  226. #ifdef HAVE_F_SETLKW
  227. fcntl ( iFile, F_SETLKW, F_WRLCK );
  228. #endif
  229. #endif
  230. }
  231. void sphLockUn ( int iFile )
  232. {
  233. #if HAVE_LOCK_EX || USE_WINDOWS
  234. flock ( iFile, LOCK_UN );
  235. #else
  236. #ifdef HAVE_F_SETLK
  237. fcntl ( iFile, F_SETLKW, F_UNLCK );
  238. #endif
  239. #endif
  240. }
  241. void sphLog ( ESphLogLevel eLevel, const char * sFmt, va_list ap )
  242. {
  243. if ( eLevel>g_eLogLevel || g_iLogFile<0 )
  244. return;
  245. time_t tNow;
  246. char sTimeBuf[128];
  247. time ( &tNow );
  248. ctime_r ( &tNow, sTimeBuf );
  249. sTimeBuf [ strlen(sTimeBuf)-1 ] = '\0';
  250. const char * sBanner = "";
  251. if ( eLevel==LOG_FATAL ) sBanner = "FATAL: ";
  252. if ( eLevel==LOG_WARNING ) sBanner = "WARNING: ";
  253. char sBuf [ 1024 ];
  254. if ( !isatty ( g_iLogFile ) )
  255. snprintf ( sBuf, sizeof(sBuf)-1, "[%s] [%5d] %s", sTimeBuf, (int)getpid(), sBanner );
  256. else
  257. strcpy ( sBuf, sBanner );
  258. int iLen = strlen(sBuf);
  259. vsnprintf ( sBuf+iLen, sizeof(sBuf)-iLen-1, sFmt, ap );
  260. strncat ( sBuf, "\n", sizeof(sBuf) );
  261. sphLockEx ( g_iLogFile );
  262. lseek ( g_iLogFile, 0, SEEK_END );
  263. write ( g_iLogFile, sBuf, strlen(sBuf) );
  264. sphLockUn ( g_iLogFile );
  265. if ( g_bLogStdout && g_iLogFile!=STDOUT_FILENO )
  266. {
  267. write ( STDOUT_FILENO, sBuf, strlen(sBuf) );
  268. }
  269. }
  270. void sphFatal ( const char * sFmt, ... )
  271. {
  272. va_list ap;
  273. va_start ( ap, sFmt );
  274. sphLog ( LOG_FATAL, sFmt, ap );
  275. va_end ( ap );
  276. Shutdown ();
  277. exit ( 1 );
  278. }
  279. void sphWarning ( const char * sFmt, ... )
  280. {
  281. va_list ap;
  282. va_start ( ap, sFmt );
  283. sphLog ( LOG_WARNING, sFmt, ap );
  284. va_end ( ap );
  285. }
  286. void sphInfo ( const char * sFmt, ... )
  287. {
  288. va_list ap;
  289. va_start ( ap, sFmt );
  290. sphLog ( LOG_INFO, sFmt, ap );
  291. va_end ( ap );
  292. }
  293. /////////////////////////////////////////////////////////////////////////////
  294. // NETWORK STUFF
  295. /////////////////////////////////////////////////////////////////////////////
  296. int MAX_PACKET_SIZE = 8*1024*1024;
  297. const int SEARCHD_MAX_ATTRS = 256;
  298. const int SEARCHD_MAX_ATTR_VALUES = 4096;
  299. const char * sphSockError ( int iErr=0 )
  300. {
  301. #if USE_WINDOWS
  302. if ( iErr==0 )
  303. iErr = WSAGetLastError ();
  304. static char sBuf [ 256 ];
  305. _snprintf ( sBuf, sizeof(sBuf), "WSA error %d", iErr );
  306. return sBuf;
  307. #else
  308. return strerror ( errno );
  309. #endif
  310. }
  311. int sphSockGetErrno ()
  312. {
  313. #if USE_WINDOWS
  314. return WSAGetLastError();
  315. #else
  316. return errno;
  317. #endif
  318. }
  319. void sphSockSetErrno ( int iErr )
  320. {
  321. #if USE_WINDOWS
  322. WSASetLastError ( iErr );
  323. #else
  324. errno = iErr;
  325. #endif
  326. }
  327. int sphCreateServerSocket ( DWORD uAddr, int iPort )
  328. {
  329. static struct sockaddr_in iaddr;
  330. iaddr.sin_family = AF_INET;
  331. iaddr.sin_addr.s_addr = uAddr;
  332. iaddr.sin_port = htons ( (short)iPort );
  333. char sAddr [ 256 ];
  334. DWORD uHost = ntohl(uAddr);
  335. snprintf ( sAddr, sizeof(sAddr), "%d.%d.%d.%d:%d",
  336. (uHost>>24) & 0xff, (uHost>>16) & 0xff, (uHost>>8) & 0xff, uHost & 0xff,
  337. iPort );
  338. sphInfo ( "creating server socket on %s", sAddr );
  339. int iSock = socket ( AF_INET, SOCK_STREAM, 0 );
  340. if ( iSock<0 )
  341. sphFatal ( "failed to create server socket on %s: %s", sAddr, sphSockError() );
  342. int iOn = 1;
  343. if ( setsockopt ( iSock, SOL_SOCKET, SO_REUSEADDR, (char*)&iOn, sizeof(iOn) ) )
  344. sphFatal ( "setsockopt() failed: %s", sphSockError() );
  345. int iTries = 12;
  346. int iRes;
  347. do
  348. {
  349. iRes = bind ( iSock, (struct sockaddr *)&iaddr, sizeof(iaddr) );
  350. if ( iRes==0 )
  351. break;
  352. sphInfo ( "failed to bind on %s, retrying...", sAddr );
  353. sleep ( 15 );
  354. } while ( --iTries>0 );
  355. if ( iRes )
  356. sphFatal ( "failed to bind on %s", sAddr );
  357. return iSock;
  358. }
  359. int sphSetSockNB ( int iSock )
  360. {
  361. #if USE_WINDOWS
  362. u_long uMode = 1;
  363. return ioctlsocket ( iSock, FIONBIO, &uMode );
  364. #else
  365. return fcntl ( iSock, F_SETFL, O_NONBLOCK );
  366. #endif
  367. }
  368. /////////////////////////////////////////////////////////////////////////////
  369. STATIC_SIZE_ASSERT ( DWORD, 4 );
  370. int sphSockRead ( int iSock, void * buf, int iLen )
  371. {
  372. assert ( iLen>0 );
  373. #if USE_WINDOWS
  374. #pragma warning(disable:4127) // conditional expr is const
  375. #pragma warning(disable:4389) // signed/unsigned mismatch
  376. #endif // USE_WINDOWS
  377. int iTimeout = 1000*Max ( 1, g_iReadTimeout ); // ms to wait total
  378. int iLeftMs = iTimeout; // ms to wait left
  379. int iLeftBytes = iLen; // bytes to read left
  380. float tmStart = sphLongTimer ();
  381. char * pBuf = (char*) buf;
  382. int iRes = -1, iErr = 0;
  383. while ( iLeftBytes>0 && iLeftMs>0 )
  384. {
  385. fd_set fdRead;
  386. FD_ZERO ( &fdRead );
  387. FD_SET ( iSock, &fdRead );
  388. fd_set fdExcept;
  389. FD_ZERO ( &fdExcept );
  390. FD_SET ( iSock, &fdExcept );
  391. struct timeval tv;
  392. tv.tv_sec = iLeftMs / 1000;
  393. tv.tv_usec = iLeftMs % 1000;
  394. iRes = ::select ( iSock+1, &fdRead, NULL, &fdExcept, &tv );
  395. // if there was EINTR, retry
  396. if ( iRes==-1 )
  397. {
  398. iErr = sphSockGetErrno();
  399. if ( iErr==EINTR )
  400. {
  401. iLeftMs = iTimeout - (int)( 1000.0f*( sphLongTimer() - tmStart ) );
  402. continue;
  403. }
  404. sphSockSetErrno ( iErr );
  405. return -1;
  406. }
  407. // if there was a timeout, report it as an error
  408. if ( iRes==0 )
  409. {
  410. sphSockSetErrno ( ETIMEDOUT );
  411. return -1;
  412. }
  413. // try to recv next chunk
  414. iRes = sphSockRecv ( iSock, pBuf, iLeftBytes, 0 );
  415. // if there was EINTR, retry
  416. if ( iRes==-1 )
  417. {
  418. iErr = sphSockGetErrno();
  419. if ( iErr==EINTR )
  420. {
  421. iLeftMs -= (int)( 1000.0f*( sphLongTimer() - tmStart ) );
  422. continue;
  423. }
  424. sphSockSetErrno ( iErr );
  425. return -1;
  426. }
  427. // update
  428. pBuf += iRes;
  429. iLeftBytes -= iRes;
  430. iLeftMs = iTimeout - (int)( 1000.0f*( sphLongTimer() - tmStart ) );
  431. }
  432. // if there was a timeout, report it as an error
  433. if ( iLeftBytes!=0 )
  434. {
  435. sphSockSetErrno ( ETIMEDOUT );
  436. return -1;
  437. }
  438. return iLen;
  439. #if USE_WINDOWS
  440. #pragma warning(default:4127) // conditional expr is const
  441. #pragma warning(default:4389) // signed/unsigned mismatch
  442. #endif // USE_WINDOWS
  443. }
  444. /////////////////////////////////////////////////////////////////////////////
  445. // NETWORK BUFFERS
  446. /////////////////////////////////////////////////////////////////////////////
  447. /// fixed-memory response buffer
  448. /// tracks usage, and flushes to network when necessary
  449. class NetOutputBuffer_c
  450. {
  451. public:
  452. NetOutputBuffer_c ( int iSock );
  453. bool SendInt ( int iValue ) { return SendT<int> ( htonl ( iValue ) ); }
  454. bool SendDword ( DWORD iValue ) { return SendT<DWORD> ( htonl ( iValue ) ); }
  455. bool SendWord ( WORD iValue ) { return SendT<WORD> ( htons ( iValue ) ); }
  456. bool SendString ( const char * sStr );
  457. bool Flush ();
  458. bool GetError () { return m_bError; }
  459. int GetSentCount () { return m_iSent; }
  460. protected:
  461. BYTE m_dBuffer[8192]; ///< my buffer
  462. BYTE * m_pBuffer; ///< my current buffer position
  463. int m_iSock; ///< my socket
  464. bool m_bError; ///< if there were any write errors
  465. int m_iSent;
  466. protected:
  467. bool SetError ( bool bValue ); ///< set error flag
  468. bool FlushIf ( int iToAdd ); ///< flush if there's not enough free space to add iToAdd bytes
  469. template < typename T > bool SendT ( T tValue );
  470. bool SendBytes ( const void * pBuf, int iLen );
  471. };
  472. /// generic request buffer
  473. class InputBuffer_c
  474. {
  475. public:
  476. InputBuffer_c ();
  477. virtual ~InputBuffer_c ();
  478. int GetInt () { return ntohl ( GetT<int> () ); }
  479. WORD GetWord () { return ntohs ( GetT<WORD> () ); }
  480. DWORD GetDword () { return ntohl ( GetT<DWORD> () ); }
  481. BYTE GetByte () { return GetT<BYTE> (); }
  482. CSphString GetString ();
  483. int GetDwords ( DWORD ** pBuffer, int iMax, const char * sErrorTemplate );
  484. bool GetError () { return m_bError; }
  485. virtual void SendErrorReply ( const char *, ... ) = 0;
  486. protected:
  487. bool m_bError;
  488. BYTE * m_pBuf;
  489. int m_iLen;
  490. BYTE * m_pCur;
  491. protected:
  492. void SetError ( bool bError ) { m_bError = bError; }
  493. bool GetBytes ( void * pBuf, int iLen );
  494. template < typename T > T GetT ();
  495. };
  496. /// simple memory request buffer
  497. class MemInputBuffer_c : public InputBuffer_c
  498. {
  499. public:
  500. MemInputBuffer_c ( const char * sFrom, int iLen );
  501. virtual void SendErrorReply ( const char *, ... ) {}
  502. };
  503. /// simple network request buffer
  504. class NetInputBuffer_c : public InputBuffer_c
  505. {
  506. public:
  507. NetInputBuffer_c ( int iSock );
  508. bool ReadFrom ( int iLen );
  509. virtual void SendErrorReply ( const char *, ... );
  510. protected:
  511. int m_iSock;
  512. };
  513. /////////////////////////////////////////////////////////////////////////////
  514. NetOutputBuffer_c::NetOutputBuffer_c ( int iSock )
  515. : m_pBuffer ( m_dBuffer )
  516. , m_iSock ( iSock )
  517. , m_bError ( false )
  518. , m_iSent ( 0 )
  519. {
  520. assert ( m_iSock>0 );
  521. }
  522. template < typename T > bool NetOutputBuffer_c::SendT ( T tValue )
  523. {
  524. if ( m_bError )
  525. return false;
  526. FlushIf ( sizeof(T) );
  527. *(T*)m_pBuffer = tValue;
  528. m_pBuffer += sizeof(T);
  529. assert ( m_pBuffer<m_dBuffer+sizeof(m_dBuffer) );
  530. return true;
  531. }
  532. bool NetOutputBuffer_c::SendString ( const char * sStr )
  533. {
  534. if ( m_bError )
  535. return false;
  536. FlushIf ( sizeof(DWORD) );
  537. int iLen = strlen(sStr);
  538. *(int*)m_pBuffer = htonl(iLen);
  539. m_pBuffer += sizeof(int);
  540. return SendBytes ( sStr, iLen );
  541. }
  542. bool NetOutputBuffer_c::SendBytes ( const void * pBuf, int iLen )
  543. {
  544. BYTE * pMy = (BYTE*)pBuf;
  545. while ( iLen>0 && !m_bError )
  546. {
  547. int iLeft = sizeof(m_dBuffer) - ( m_pBuffer - m_dBuffer );
  548. if ( iLen<=iLeft )
  549. {
  550. memcpy ( m_pBuffer, pMy, iLen );
  551. m_pBuffer += iLen;
  552. break;
  553. }
  554. memcpy ( m_pBuffer, pMy, iLeft );
  555. m_pBuffer += iLeft;
  556. Flush ();
  557. pMy += iLeft;
  558. iLen -= iLeft;
  559. }
  560. return !m_bError;
  561. }
  562. bool NetOutputBuffer_c::Flush ()
  563. {
  564. if ( m_bError )
  565. return false;
  566. int iLen = m_pBuffer-m_dBuffer;
  567. if ( iLen==0 )
  568. return true;
  569. assert ( iLen>0 );
  570. assert ( iLen<=(int)sizeof(m_dBuffer) );
  571. int iRes = sphSockSend ( m_iSock, (char*)&m_dBuffer[0], iLen, 0 );
  572. m_bError = ( iRes!=iLen );
  573. m_iSent += iLen;
  574. m_pBuffer = m_dBuffer;
  575. return !m_bError;
  576. }
  577. bool NetOutputBuffer_c::FlushIf ( int iToAdd )
  578. {
  579. if ( m_pBuffer+iToAdd >= m_dBuffer+sizeof(m_dBuffer) )
  580. return Flush ();
  581. return !m_bError;
  582. }
  583. /////////////////////////////////////////////////////////////////////////////
  584. InputBuffer_c::InputBuffer_c ()
  585. : m_bError ( true )
  586. , m_iLen ( 0 )
  587. {
  588. m_pBuf = new BYTE [ MAX_PACKET_SIZE ];
  589. m_pCur = m_pBuf;
  590. }
  591. InputBuffer_c::~InputBuffer_c ()
  592. {
  593. SafeDeleteArray ( m_pBuf );
  594. }
  595. template < typename T > T InputBuffer_c::GetT ()
  596. {
  597. if ( m_bError || ( m_pCur+sizeof(T) > m_pBuf+m_iLen ) )
  598. {
  599. SetError ( true );
  600. return 0;
  601. }
  602. T iRes = *(T*)m_pCur;
  603. m_pCur += sizeof(T);
  604. return iRes;
  605. }
  606. CSphString InputBuffer_c::GetString ()
  607. {
  608. CSphString sRes;
  609. int iLen = GetInt ();
  610. if ( m_bError || iLen<0 || iLen>MAX_PACKET_SIZE || ( m_pCur+iLen > m_pBuf+m_iLen ) )
  611. {
  612. SetError ( true );
  613. return sRes;
  614. }
  615. sRes.SetBinary ( (char*)m_pCur, iLen );
  616. m_pCur += iLen;
  617. return sRes;
  618. }
  619. bool InputBuffer_c::GetBytes ( void * pBuf, int iLen )
  620. {
  621. assert ( pBuf );
  622. assert ( iLen>0 && iLen<=MAX_PACKET_SIZE );
  623. if ( m_bError || ( m_pCur+iLen > m_pBuf+m_iLen ) )
  624. {
  625. SetError ( true );
  626. return false;
  627. }
  628. memcpy ( pBuf, m_pCur, iLen );
  629. m_pCur += iLen;
  630. return true;;
  631. }
  632. int InputBuffer_c::GetDwords ( DWORD ** ppBuffer, int iMax, const char * sErrorTemplate )
  633. {
  634. assert ( ppBuffer );
  635. assert ( !(*ppBuffer) );
  636. int iCount = GetInt ();
  637. if ( iCount<0 || iCount>iMax )
  638. {
  639. SendErrorReply ( sErrorTemplate, iCount, iMax );
  640. SetError ( true );
  641. return -1;
  642. }
  643. if ( iCount )
  644. {
  645. assert ( !(*ppBuffer) ); // potential leak
  646. (*ppBuffer) = new DWORD [ iCount ];
  647. if ( !GetBytes ( (*ppBuffer), sizeof(int)*iCount ) )
  648. {
  649. SafeDeleteArray ( (*ppBuffer) );
  650. return -1;
  651. }
  652. for ( int i=0; i<iCount; i++ )
  653. (*ppBuffer)[i] = htonl ( (*ppBuffer)[i] );
  654. }
  655. return iCount;
  656. }
  657. /////////////////////////////////////////////////////////////////////////////
  658. NetInputBuffer_c::NetInputBuffer_c ( int iSock )
  659. : m_iSock ( iSock )
  660. {
  661. }
  662. bool NetInputBuffer_c::ReadFrom ( int iLen )
  663. {
  664. assert ( iLen>0 );
  665. assert ( iLen<=MAX_PACKET_SIZE );
  666. assert ( m_iSock>0 );
  667. m_pCur = m_pBuf;
  668. int iGot = sphSockRead ( m_iSock, m_pBuf, iLen );
  669. if ( iGot!=iLen )
  670. {
  671. m_bError = true;
  672. m_iLen = 0;
  673. return false;
  674. }
  675. m_bError = false;
  676. m_iLen = iLen;
  677. return true;
  678. }
  679. void NetInputBuffer_c::SendErrorReply ( const char * sTemplate, ... )
  680. {
  681. char dBuf [ 2048 ];
  682. const int iHeaderLen = 12;
  683. const int iMaxStrLen = sizeof(dBuf) - iHeaderLen - 1;
  684. // fill header
  685. WORD * p0 = (WORD*)&dBuf[0];
  686. p0[0] = htons(SEARCHD_ERROR); // error code
  687. p0[1] = 0; // version doesn't matter
  688. // fill error string
  689. char * sBuf = dBuf + iHeaderLen;
  690. va_list ap;
  691. va_start ( ap, sTemplate );
  692. vsnprintf ( sBuf, iMaxStrLen, sTemplate, ap );
  693. va_end ( ap );
  694. sBuf[iMaxStrLen] = '\0';
  695. int iStrLen = strlen(sBuf);
  696. // fixup lengths
  697. DWORD * p4 = (DWORD*)&dBuf[4];
  698. p4[0] = htonl(4+iStrLen);
  699. p4[1] = htonl(iStrLen);
  700. // send!
  701. sphSockSend ( m_iSock, dBuf, iHeaderLen+iStrLen, 0 );
  702. }
  703. /////////////////////////////////////////////////////////////////////////////
  704. MemInputBuffer_c::MemInputBuffer_c ( const char * sFrom, int iLen )
  705. {
  706. if ( iLen<0 || iLen>MAX_PACKET_SIZE )
  707. {
  708. m_bError = true;
  709. return;
  710. }
  711. memcpy ( m_pBuf, sFrom, iLen );
  712. m_iLen = iLen;
  713. m_pCur = m_pBuf;
  714. m_bError = false;
  715. }
  716. /////////////////////////////////////////////////////////////////////////////
  717. // SIMPLE FILE-BASED QUERY CACHE
  718. /////////////////////////////////////////////////////////////////////////////
  719. #if !USE_WINDOWS
  720. /// my simple cache
  721. class CSphCache
  722. {
  723. public:
  724. CSphCache ( const char * sCacheDir, int iCacheTTL, bool bUseGzip );
  725. bool ReadFromFile ( const CSphQuery & tQuery, const char * sIndexName, const char * sIndexFileName, CSphQueryResult * pRes );
  726. bool StoreResult ( const CSphQuery & tQuery, const char * sIndexName, const CSphQueryResult * pRes );
  727. private:
  728. char m_sCacheFileName [ 16*2 + 1 ];
  729. CSphString m_sCacheDir;
  730. int m_iCacheTTL;
  731. bool m_bUseGzip;
  732. void GenerateCacheFileName ( const CSphQuery & tQuery );
  733. };
  734. CSphCache::CSphCache ( const char * sCacheDir, int iCacheTTL, bool bUseGzip )
  735. {
  736. memset ( m_sCacheFileName, 0, sizeof(m_sCacheFileName) );
  737. m_sCacheDir = sCacheDir;
  738. m_iCacheTTL = iCacheTTL;
  739. m_bUseGzip = bUseGzip;
  740. }
  741. bool CSphCache::ReadFromFile ( const CSphQuery & tQuery, const char * sIndexName, const char * sIndexFileName, CSphQueryResult * pRes )
  742. {
  743. // build filename, and check the cached result file
  744. GenerateCacheFileName ( tQuery );
  745. char sBuf [ SPH_MAX_FILENAME_LEN ];
  746. snprintf ( sBuf, sizeof(sBuf), "%s/%s/%c%c/%c%c/%s", m_sCacheDir.cstr(),
  747. sIndexName, m_sCacheFileName[0], m_sCacheFileName[1], m_sCacheFileName[2], m_sCacheFileName[3], m_sCacheFileName );
  748. struct stat stFileInfo;
  749. if ( lstat ( sBuf, &stFileInfo) < 0 )
  750. return false; // cache miss; uncached
  751. if ( m_iCacheTTL>0 && tQuery.m_eSort==SPH_SORT_TIME_SEGMENTS )
  752. if ( ( time(NULL) - stFileInfo.st_mtime ) > m_iCacheTTL )
  753. return false; // cache miss; TTL expired
  754. // check index modification time
  755. // FIXME! should query this from index!
  756. char sBufIndex [ SPH_MAX_FILENAME_LEN ];
  757. snprintf ( sBufIndex, sizeof(sBufIndex), "%s.spi", sIndexFileName );
  758. struct stat stIndexInfo;
  759. if ( lstat ( sBufIndex, &stIndexInfo) < 0 )
  760. {
  761. sphWarning ( "failed to lstat '%s': errno=%d, err=%s", sBufIndex, errno, strerror(errno) );
  762. return false;
  763. }
  764. if ( stIndexInfo.st_mtime>=stFileInfo.st_mtime )
  765. return false; // cache miss; index is newer that the cached result
  766. // read the data
  767. int fCache = open ( sBuf, O_RDONLY );
  768. if ( fCache<0 )
  769. {
  770. sphWarning ( "failed to open '%s': errno=%d, err=%s", sBuf, errno, strerror(errno) );
  771. return false;
  772. }
  773. int * pMapFile = NULL;
  774. int iFileSize = 0;
  775. if ( m_bUseGzip )
  776. {
  777. gzFile fgzCache = gzdopen ( fCache, "r" );
  778. if ( fgzCache==Z_NULL )
  779. {
  780. close ( fCache );
  781. sphWarning ( "failed to gzdopen '%s': errno=%d, err=%s", errno, strerror(errno) );
  782. return false;
  783. }
  784. int iGzipBuffer = 32768; // start with a 32K buffer
  785. int iGzipLeft = iGzipBuffer;
  786. pMapFile = new int [ iGzipBuffer/sizeof(int) ];
  787. BYTE * pCur = (BYTE *)pMapFile;
  788. for ( ;; )
  789. {
  790. // read next chunk
  791. int iRead = gzread ( fgzCache, pCur, iGzipLeft );
  792. // if it's EOF, we're done
  793. if ( iRead==0 && gzeof ( fgzCache ) )
  794. break;
  795. // if it's error, bail out
  796. if ( iRead<=0 )
  797. {
  798. sphWarning ( "failed to gzread '%s': gzread error", sBuf );
  799. SafeDeleteArray ( pMapFile );
  800. gzclose ( fgzCache );
  801. close ( fCache );
  802. return false;
  803. }
  804. // update counters
  805. pCur += iRead;
  806. iFileSize += iRead;
  807. iGzipLeft -= iRead;
  808. if ( iFileSize<iGzipBuffer )
  809. continue;
  810. assert ( iFileSize==iGzipBuffer );
  811. // realloc the buffer, if necessary
  812. if ( iGzipBuffer>=1048576 )
  813. {
  814. sphWarning ( "failed to gzread '%s': too big (over 1M)", sBuf );
  815. SafeDeleteArray ( pMapFile );
  816. gzclose ( fgzCache );
  817. close ( fCache );
  818. return false;
  819. }
  820. int * pNew = new int [ 2*iGzipBuffer/sizeof(int) ];
  821. memcpy ( pNew, pMapFile, iGzipBuffer );
  822. SafeDeleteArray ( pMapFile );
  823. pMapFile = pNew;
  824. pCur = ((BYTE*)pNew) + iGzipBuffer;
  825. iGzipBuffer *= 2;
  826. }
  827. gzclose ( fgzCache );
  828. } else
  829. {
  830. iFileSize = lseek ( fCache, 0, SEEK_END );
  831. pMapFile = (int*)mmap ( 0, (size_t)iFileSize, PROT_READ,
  832. MAP_SHARED | MAP_NORESERVE, fCache, 0L );
  833. if ( pMapFile==(int*)MAP_FAILED )
  834. {
  835. sphWarning ( "failed to mmap '%s': errno=%d, err=%s", sBuf, errno, strerror(errno) );
  836. close ( fCache );
  837. return false;
  838. }
  839. }
  840. // parse cached result
  841. assert ( pMapFile );
  842. assert ( iFileSize>0 );
  843. bool bOK = false;
  844. for ( ;; )
  845. {
  846. if ( iFileSize<3*(int)sizeof(int) )
  847. {
  848. sphWarning ( "failed to read header from '%s': file too short", sBuf );
  849. break;
  850. }
  851. int * pCur = pMapFile;
  852. char * pEnd = ((char*)pMapFile) + iFileSize;
  853. int iAttrs = *pCur++; // !COMMIT serialize whole schema here
  854. pRes->m_iNumWords = *pCur++;
  855. int iMatches = *pCur++;
  856. pRes->m_dMatches.Resize ( iMatches );
  857. pRes->m_iTotalMatches = *pCur++;
  858. if ( iFileSize < (int)sizeof(int)*( 3 + iMatches*4 ) )
  859. {
  860. sphWarning ( "failed to read matches from '%s': file too short", sBuf );
  861. break;
  862. }
  863. for ( int i=0; i<iMatches; i++ )
  864. {
  865. CSphMatch & tMatch = pRes->m_dMatches[i];
  866. tMatch.m_iDocID = *pCur++;
  867. tMatch.m_iWeight = *pCur++;
  868. if ( iAttrs )
  869. {
  870. assert ( !tMatch.m_pAttrs );
  871. tMatch.m_iAttrs = iAttrs;
  872. tMatch.m_pAttrs = new DWORD [ iAttrs ]; // !COMMIT pool these alloc
  873. for ( int j=0; j<iAttrs; j++ )
  874. tMatch.m_pAttrs[j] = *pCur++;
  875. }
  876. }
  877. int i;
  878. for ( i=0; i<pRes->m_iNumWords; i++ )
  879. {
  880. if ( ((char*)pCur) + sizeof(int) > pEnd )
  881. {
  882. sphWarning ( "failed to read query term length from '%s': file too short", sBuf );
  883. break;
  884. }
  885. int iWordLen = *pCur++;
  886. if ( ((char*)pCur) + iWordLen + 2*sizeof(int) > pEnd )
  887. {
  888. sphWarning ( "failed to read query term data from '%s': file too short", sBuf );
  889. break;
  890. }
  891. pRes->m_tWordStats[i].m_sWord.SetBinary ( (char*)pCur, iWordLen );
  892. pCur = (int*)( ((char*)pCur) + iWordLen );
  893. pRes->m_tWordStats[i].m_iDocs = *pCur++;
  894. pRes->m_tWordStats[i].m_iHits = *pCur++;
  895. }
  896. if ( i!=pRes->m_iNumWords )
  897. break;
  898. bOK = true;
  899. break;
  900. }
  901. if ( m_bUseGzip )
  902. {
  903. SafeDeleteArray ( pMapFile );
  904. } else
  905. {
  906. munmap ( pMapFile, iFileSize );
  907. }
  908. close ( fCache );
  909. return bOK;
  910. }
  911. bool CSphCache::StoreResult ( const CSphQuery & tQuery, const char * sIndexName, const CSphQueryResult * pRes )
  912. {
  913. GenerateCacheFileName ( tQuery );
  914. // create the dir if it doesn't exist yet
  915. char sBuf [ SPH_MAX_FILENAME_LEN ];
  916. snprintf ( sBuf, sizeof(sBuf), "%s/%s/%c%c/%c%c", m_sCacheDir.cstr(), sIndexName, m_sCacheFileName[0], m_sCacheFileName[1],m_sCacheFileName[2], m_sCacheFileName[3] );
  917. struct stat stDir;
  918. if ( lstat ( sBuf, &stDir)!=0 )
  919. {
  920. snprintf ( sBuf, sizeof(sBuf), "%s/%s", m_sCacheDir.cstr(), sIndexName );
  921. mkdir ( sBuf, 0755 );
  922. snprintf ( sBuf, sizeof(sBuf), "%s/%s/%c%c", m_sCacheDir.cstr(), sIndexName, m_sCacheFileName[0], m_sCacheFileName[1] );
  923. mkdir ( sBuf, 0755 );
  924. snprintf ( sBuf, sizeof(sBuf), "%s/%s/%c%c/%c%c", m_sCacheDir.cstr(), sIndexName, m_sCacheFileName[0], m_sCacheFileName[1],m_sCacheFileName[2], m_sCacheFileName[3] );
  925. mkdir ( sBuf, 0755 );
  926. }
  927. // create the file
  928. snprintf ( sBuf, sizeof(sBuf), "%s/%s/%c%c/%c%c/%s", m_sCacheDir.cstr(), sIndexName,
  929. m_sCacheFileName[0], m_sCacheFileName[1], m_sCacheFileName[2], m_sCacheFileName[3], m_sCacheFileName );
  930. int fCache = open ( sBuf, O_CREAT | O_TRUNC | O_WRONLY, 0644 );
  931. if ( fCache<0 )
  932. {
  933. sphWarning ( "failed to create '%s': errno=%d, err=%s", sBuf, errno, strerror(errno) );
  934. return false;
  935. }
  936. // create the gzip stream
  937. gzFile fgzCache = Z_NULL;
  938. if ( m_bUseGzip )
  939. fgzCache = gzdopen ( fCache, "wb" );
  940. int iBufLen = sizeof(int)*( 4 + 4*pRes->m_dMatches.GetLength() + 3*pRes->m_iNumWords );
  941. for ( int i=0; i<pRes->m_iNumWords; i++ )
  942. iBufLen += strlen ( pRes->m_tWordStats[i].m_sWord.cstr() );
  943. int * pBuf = new int [ 1 + iBufLen/sizeof(int) ];
  944. int * pCur = pBuf;
  945. int iAttrs = pRes->m_tSchema.m_dAttrs.GetLength ();
  946. *pCur++ = iAttrs; // !COMMIT serialize whole schema here
  947. *pCur++ = pRes->m_iNumWords;
  948. *pCur++ = pRes->m_dMatches.GetLength();
  949. *pCur++ = pRes->m_iTotalMatches;
  950. ARRAY_FOREACH ( i, pRes->m_dMatches )
  951. {
  952. const CSphMatch & tMatch = pRes->m_dMatches[i];
  953. *pCur++ = tMatch.m_iDocID;
  954. *pCur++ = tMatch.m_iWeight;
  955. assert ( tMatch.m_iAttrs==iAttrs );
  956. for ( int j=0; j<iAttrs; j++ )
  957. *pCur++ = tMatch.m_pAttrs[j];
  958. }
  959. for ( int i=0; i<pRes->m_iNumWords; i++ )
  960. {
  961. int iLen = strlen ( pRes->m_tWordStats[i].m_sWord.cstr() );
  962. *pCur++ = iLen;
  963. memcpy ( pCur, pRes->m_tWordStats[i].m_sWord.cstr(), iLen );
  964. pCur = (int*)( ((char*)pCur) + iLen );
  965. *pCur++ = pRes->m_tWordStats[i].m_iDocs;
  966. *pCur++ = pRes->m_tWordStats[i].m_iHits;
  967. }
  968. assert ( ((char*)pCur)==((char*)pBuf) + iBufLen );
  969. bool bOK = true;
  970. if ( m_bUseGzip )
  971. {
  972. if ( gzwrite ( fgzCache, pBuf, iBufLen )<0 )
  973. {
  974. sphWarning ( "failed to gzwrite '%s': errno=%d, err=%s", errno, strerror(errno) ); // FIXME! should use gzerror everywhere
  975. bOK = false;
  976. }
  977. gzclose ( fgzCache );
  978. } else
  979. {
  980. if ( write ( fCache, pBuf, iBufLen )<0 )
  981. {
  982. sphWarning ( "failed to write '%s': errno=%d, err=%s", errno, strerror(errno) );
  983. bOK = false;
  984. }
  985. close ( fCache );
  986. }
  987. SafeDeleteArray ( pBuf );
  988. return true;
  989. }
  990. void CSphCache::GenerateCacheFileName ( const CSphQuery & tQuery )
  991. {
  992. md5_state_t tState;
  993. md5_byte_t tDigest[16];
  994. char sBuf[2048];
  995. int iLen = snprintf ( sBuf, sizeof(sBuf), "%s-%d-%d-%d-%d",
  996. tQuery.m_sQuery.cstr(),tQuery.m_eMode, tQuery.m_eSort,
  997. tQuery.m_iMinID, tQuery.m_iMaxID );
  998. md5_init ( &tState );
  999. md5_append ( &tState, (md5_byte_t*)sBuf, iLen );
  1000. if ( tQuery.m_iWeights>0 )
  1001. md5_append( &tState, (md5_byte_t*)tQuery.m_pWeights, tQuery.m_iWeights*sizeof(DWORD) );
  1002. // !COMMIT pass all filters to md5 too
  1003. md5_finish ( &tState, tDigest );
  1004. for ( int iDigest=0; iDigest<16; ++iDigest )
  1005. snprintf ( m_sCacheFileName + iDigest*2, 3, "%02x", tDigest[iDigest] );
  1006. }
  1007. #endif // !USE_WINDOWS
  1008. /////////////////////////////////////////////////////////////////////////////
  1009. // DISTRIBUTED QUERIES
  1010. /////////////////////////////////////////////////////////////////////////////
  1011. /// remote agent state
  1012. enum AgentState_e
  1013. {
  1014. AGENT_UNUSED, ///< agent is unused for this request
  1015. AGENT_CONNECT, ///< connecting to agent
  1016. AGENT_HELLO, ///< waiting for "VER x" hello
  1017. AGENT_QUERY, ///< query sent, wating for reply
  1018. AGENT_REPLY ///< reading reply
  1019. };
  1020. /// remote agent host/port
  1021. struct Agent_t
  1022. {
  1023. public:
  1024. CSphString m_sHost; ///< remote searchd host
  1025. int m_iPort; ///< remote searchd port, 0 if local
  1026. CSphString m_sIndexes; ///< remote index names to query
  1027. int m_iSock; ///< socket number, -1 if not connected
  1028. AgentState_e m_eState; ///< current state
  1029. CSphQueryResult m_tRes; ///< query result
  1030. bool m_bFailure; ///< whether query was succesful
  1031. int m_iReplyStatus; ///< reply status code
  1032. int m_iReplySize; ///< how many reply bytes are there
  1033. int m_iReplyRead; ///< how many reply bytes are alredy received
  1034. char * m_pReplyBuf; ///< reply buffer
  1035. protected:
  1036. int m_iAddrType;
  1037. DWORD m_uAddr;
  1038. public:
  1039. Agent_t ()
  1040. : m_iPort ( -1 )
  1041. , m_iSock ( -1 )
  1042. , m_eState ( AGENT_UNUSED )
  1043. , m_bFailure ( false )
  1044. , m_iReplyStatus ( -1 )
  1045. , m_iReplySize ( 0 )
  1046. , m_iReplyRead ( 0 )
  1047. , m_pReplyBuf ( NULL )
  1048. , m_uAddr ( 0 )
  1049. {
  1050. }
  1051. ~Agent_t ()
  1052. {
  1053. SafeDeleteArray ( m_pReplyBuf );
  1054. Close ();
  1055. }
  1056. void Close ()
  1057. {
  1058. if ( m_iSock>0 )
  1059. {
  1060. sphSockClose ( m_iSock );
  1061. m_iSock = -1;
  1062. m_eState = AGENT_UNUSED;
  1063. SafeDeleteArray ( m_pReplyBuf );
  1064. }
  1065. }
  1066. void SetAddr ( int iAddrType, int iAddrLen, const char * pAddr )
  1067. {
  1068. assert ( pAddr );
  1069. assert ( iAddrLen==sizeof(m_uAddr) );
  1070. m_iAddrType = iAddrType;
  1071. memcpy ( &m_uAddr, pAddr, iAddrLen );
  1072. }
  1073. int GetAddrType () const
  1074. {
  1075. return m_iAddrType;
  1076. }
  1077. int GetAddrLen () const
  1078. {
  1079. return sizeof(m_uAddr);
  1080. }
  1081. const char * GetAddr () const
  1082. {
  1083. return (const char *)&m_uAddr;
  1084. }
  1085. };
  1086. /// distributed index
  1087. struct DistributedIndex_t
  1088. {
  1089. CSphVector<Agent_t,16> m_dAgents; ///< remote agents
  1090. CSphVector<CSphString> m_dLocal; ///< local indexes
  1091. int m_iAgentConnectTimeout; ///< in msec
  1092. int m_iAgentQueryTimeout; ///< in msec
  1093. DistributedIndex_t ()
  1094. : m_iAgentConnectTimeout ( 1000 )
  1095. , m_iAgentQueryTimeout ( 3000 )
  1096. {
  1097. }
  1098. };
  1099. static SmallStringHash_T < DistributedIndex_t > g_hDistIndexes;
  1100. /////////////////////////////////////////////////////////////////////////////
  1101. void ConnectToRemoteAgent ( Agent_t * pAgent, const char * sIndexName, SearchFailuresLog_t & dFailures )
  1102. {
  1103. assert ( pAgent );
  1104. assert ( pAgent->m_iPort>0 );
  1105. assert ( pAgent->GetAddr() );
  1106. assert ( pAgent->m_iSock<0 );
  1107. pAgent->m_eState = AGENT_UNUSED;
  1108. struct sockaddr_in sa;
  1109. memset ( &sa, 0, sizeof(sa) );
  1110. memcpy ( &sa.sin_addr, pAgent->GetAddr(), pAgent->GetAddrLen() );
  1111. sa.sin_family = (short)pAgent->GetAddrType();
  1112. sa.sin_port = htons ( (unsigned short)pAgent->m_iPort );
  1113. pAgent->m_iSock = socket ( pAgent->GetAddrType(), SOCK_STREAM, 0 );
  1114. if ( pAgent->m_iSock<0 )
  1115. {
  1116. dFailures.Add ( SearchFailure_t ( sIndexName, "agent '%s:%d': socket() failed",
  1117. pAgent->m_sHost.cstr(), pAgent->m_iPort ) );
  1118. return;
  1119. }
  1120. if ( sphSetSockNB ( pAgent->m_iSock )<0 )
  1121. {
  1122. dFailures.Add ( SearchFailure_t ( sIndexName, "agent '%s:%d': sphSetSockNB() failed",
  1123. pAgent->m_sHost.cstr(), pAgent->m_iPort ) );
  1124. return;
  1125. }
  1126. if ( connect ( pAgent->m_iSock, (struct sockaddr*)&sa, sizeof(sa) )<0 )
  1127. {
  1128. int iErr = sphSockGetErrno();
  1129. if ( iErr!=EINPROGRESS && iErr!=EINTR && iErr!=EWOULDBLOCK ) // check for EWOULDBLOCK is for winsock only
  1130. {
  1131. pAgent->Close ();
  1132. dFailures.Add ( SearchFailure_t ( sIndexName, "agent '%s:%d': connect() failed: %s",
  1133. pAgent->m_sHost.cstr(), pAgent->m_iPort, sphSockError(iErr) ) );
  1134. return;
  1135. } else
  1136. {
  1137. // connection in progress
  1138. pAgent->m_eState = AGENT_CONNECT;
  1139. }
  1140. } else
  1141. {
  1142. // socket connected, ready to read hello message
  1143. pAgent->m_eState = AGENT_HELLO;
  1144. }
  1145. }
  1146. #if USE_WINDOWS
  1147. #pragma warning(disable:4127) // conditional expr is const
  1148. #pragma warning(disable:4389) // signed/unsigned mismatch
  1149. #endif // USE_WINDOWS
  1150. int QueryRemoteAgents ( const char * sIndexName, DistributedIndex_t & tDist, const CSphQuery & tQuery, int iMode,
  1151. SearchFailuresLog_t & dFailures )
  1152. {
  1153. int iTimeout = tDist.m_iAgentConnectTimeout;
  1154. int iAgents = 0;
  1155. assert ( iTimeout>=0 );
  1156. int iPassed = 0;
  1157. float tmStart = sphLongTimer ();
  1158. while ( iPassed<=iTimeout )
  1159. {
  1160. fd_set fdsRead, fdsWrite;
  1161. FD_ZERO ( &fdsRead );
  1162. FD_ZERO ( &fdsWrite );
  1163. int iMax = 0;
  1164. bool bDone = true;
  1165. ARRAY_FOREACH ( i, tDist.m_dAgents )
  1166. {
  1167. const Agent_t & tAgent = tDist.m_dAgents[i];
  1168. if ( tAgent.m_eState==AGENT_CONNECT || tAgent.m_eState==AGENT_HELLO )
  1169. {
  1170. assert ( tAgent.m_iPort>0 );
  1171. assert ( tAgent.m_iSock>0 );
  1172. FD_SET ( tAgent.m_iSock, ( tAgent.m_eState==AGENT_CONNECT ) ? &fdsWrite : &fdsRead );
  1173. iMax = Max ( iMax, tAgent.m_iSock );
  1174. bDone = false;
  1175. }
  1176. }
  1177. if ( bDone )
  1178. break;
  1179. iPassed = int ( 1000.0f*( sphLongTimer() - tmStart ) );
  1180. int iToWait = Max ( iTimeout-iPassed, 0 );
  1181. struct timeval tvTimeout;
  1182. tvTimeout.tv_sec = iToWait / 1000; // full seconds
  1183. tvTimeout.tv_usec = ( iToWait % 1000 ) * 1000; // remainder is msec, so *1000 for usec
  1184. // FIXME! check exceptfds for connect() failure as well, so that actively refused
  1185. // connections would not stall for a full timeout
  1186. if ( select ( 1+iMax, &fdsRead, &fdsWrite, NULL, &tvTimeout )<=0 )
  1187. continue;
  1188. ARRAY_FOREACH ( i, tDist.m_dAgents )
  1189. {
  1190. Agent_t & tAgent = tDist.m_dAgents[i];
  1191. // check if connection completed
  1192. if ( tAgent.m_eState==AGENT_CONNECT && FD_ISSET ( tAgent.m_iSock, &fdsWrite ) )
  1193. {
  1194. int iErr = 0;
  1195. socklen_t iErrLen = sizeof(iErr);
  1196. getsockopt ( tAgent.m_iSock, SOL_SOCKET, SO_ERROR, (char*)&iErr, &iErrLen );
  1197. if ( iErr )
  1198. {
  1199. // connect() failure
  1200. dFailures.Add ( SearchFailure_t ( sIndexName, "agent '%s:%d': connect() failed (err=%d)",
  1201. tAgent.m_sHost.cstr(), tAgent.m_iPort, iErr ) );
  1202. tAgent.Close ();
  1203. } else
  1204. {
  1205. // connect() success
  1206. tAgent.m_eState = AGENT_HELLO;
  1207. }
  1208. continue;
  1209. }
  1210. // check if hello was received
  1211. if ( tAgent.m_eState==AGENT_HELLO && FD_ISSET ( tAgent.m_iSock, &fdsRead ) )
  1212. {
  1213. // read reply
  1214. int iRemoteVer;
  1215. int iRes = sphSockRecv ( tAgent.m_iSock, (char*)&iRemoteVer, sizeof(iRemoteVer), 0 );
  1216. iRemoteVer = ntohl ( iRemoteVer );
  1217. if ( iRes!=sizeof(iRemoteVer) || iRemoteVer<=0 )
  1218. {
  1219. dFailures.Add ( SearchFailure_t ( sIndexName, "agent '%s:%d': expected protocol v.%d, got v.%d",
  1220. tAgent.m_sHost.cstr(), tAgent.m_iPort,
  1221. SPHINX_SEARCHD_PROTO, iRemoteVer ) );
  1222. tAgent.Close ();
  1223. continue;
  1224. }
  1225. // do query!
  1226. int iReqSize = 72 + 4*tQuery.m_iWeights
  1227. + strlen ( tQuery.m_sSortBy.cstr() )
  1228. + strlen ( tQuery.m_sQuery.cstr() )
  1229. + strlen ( tAgent.m_sIndexes.cstr() )
  1230. + strlen ( tQuery.m_sGroupBy.cstr() );
  1231. ARRAY_FOREACH ( j, tQuery.m_dFilters )
  1232. {
  1233. const CSphFilter & tFilter = tQuery.m_dFilters[j];
  1234. iReqSize +=
  1235. 12
  1236. + strlen ( tFilter.m_sAttrName.cstr() )
  1237. + 4*tFilter.m_iValues
  1238. + ( tFilter.m_iValues ? 0 : 8 );
  1239. }
  1240. NetOutputBuffer_c tOut ( tAgent.m_iSock );
  1241. // say hello
  1242. tOut.SendDword ( SPHINX_SEARCHD_PROTO );
  1243. // request header
  1244. tOut.SendWord ( SEARCHD_COMMAND_SEARCH ); // command id
  1245. tOut.SendWord ( VER_COMMAND_SEARCH ); // command version
  1246. tOut.SendInt ( iReqSize-12 ); // request body length
  1247. tOut.SendInt ( 0 ); // offset is 0
  1248. tOut.SendInt ( tQuery.m_iMaxMatches ); // limit is MAX_MATCHES
  1249. tOut.SendInt ( iMode ); // match mode
  1250. tOut.SendInt ( tQuery.m_eSort ); // sort mode
  1251. tOut.SendString ( tQuery.m_sSortBy.cstr() ); // sort attr
  1252. tOut.SendString ( tQuery.m_sQuery.cstr() ); // query
  1253. tOut.SendInt ( tQuery.m_iWeights );
  1254. for ( int j=0; j<tQuery.m_iWeights; j++ )
  1255. tOut.SendInt ( tQuery.m_pWeights[j] ); // weights
  1256. tOut.SendString ( tAgent.m_sIndexes.cstr() ); // indexes
  1257. tOut.SendInt ( tQuery.m_iMinID ); // id/ts ranges
  1258. tOut.SendInt ( tQuery.m_iMaxID );
  1259. tOut.SendInt ( tQuery.m_dFilters.GetLength() );
  1260. ARRAY_FOREACH ( j, tQuery.m_dFilters )
  1261. {
  1262. const CSphFilter & tFilter = tQuery.m_dFilters[j];
  1263. tOut.SendString ( tFilter.m_sAttrName.cstr() );
  1264. tOut.SendInt ( tFilter.m_iValues );
  1265. for ( int k=0; k<tFilter.m_iValues; k++ )
  1266. tOut.SendInt ( tFilter.m_pValues[k] );
  1267. if ( !tFilter.m_iValues )
  1268. {
  1269. tOut.SendDword ( tFilter.m_uMinValue );
  1270. tOut.SendDword ( tFilter.m_uMaxValue );
  1271. }
  1272. tOut.SendInt ( tFilter.m_bExclude );
  1273. }
  1274. tOut.SendInt ( tQuery.m_eGroupFunc );
  1275. tOut.SendString ( tQuery.m_sGroupBy.cstr() );
  1276. tOut.SendInt ( tQuery.m_iMaxMatches );
  1277. tOut.SendInt ( tQuery.m_bSortByGroup );
  1278. tOut.Flush ();
  1279. // FIXME! handle flush failure
  1280. tAgent.m_eState = AGENT_QUERY;
  1281. iAgents++;
  1282. }
  1283. }
  1284. }
  1285. ARRAY_FOREACH ( i, tDist.m_dAgents )
  1286. {
  1287. // check if connection timed out
  1288. Agent_t & tAgent = tDist.m_dAgents[i];
  1289. if ( tAgent.m_eState!=AGENT_QUERY && tAgent.m_eState!=AGENT_UNUSED )
  1290. {
  1291. tAgent.Close ();
  1292. dFailures.Add ( SearchFailure_t ( sIndexName, "agent '%s:%d': %s() timed out",
  1293. tAgent.m_sHost.cstr(), tAgent.m_iPort,
  1294. tAgent.m_eState==AGENT_HELLO ? "read" : "connect" ) );
  1295. }
  1296. }
  1297. return iAgents;
  1298. }
  1299. int WaitForRemoteAgents ( const char * sIndexName, DistributedIndex_t & tDist, CSphQueryResult * pRes, int iTimeout,
  1300. SearchFailuresLog_t & dFailures )
  1301. {
  1302. assert ( pRes );
  1303. assert ( iTimeout>=0 );
  1304. int iAgents = 0;
  1305. int iPassed = 0;
  1306. float tmStart = sphLongTimer ();
  1307. while ( iPassed<=iTimeout )
  1308. {
  1309. fd_set fdsRead;
  1310. FD_ZERO ( &fdsRead );
  1311. int iMax = 0;
  1312. bool bDone = true;
  1313. ARRAY_FOREACH ( iAgent, tDist.m_dAgents )
  1314. {
  1315. Agent_t & tAgent = tDist.m_dAgents[iAgent];
  1316. if ( tAgent.m_eState==AGENT_QUERY || tAgent.m_eState==AGENT_REPLY )
  1317. {
  1318. assert ( tAgent.m_iPort>0 );
  1319. assert ( tAgent.m_iSock>0 );
  1320. FD_SET ( tAgent.m_iSock, &fdsRead );
  1321. iMax = Max ( iMax, tAgent.m_iSock );
  1322. bDone = false;
  1323. }
  1324. }
  1325. if ( bDone )
  1326. break;
  1327. iPassed = int ( 1000.0f*( sphLongTimer() - tmStart ) );
  1328. int iToWait = Max ( iTimeout-iPassed, 0 );
  1329. struct timeval tvTimeout;
  1330. tvTimeout.tv_sec = iToWait / 1000; // full seconds
  1331. tvTimeout.tv_usec = ( iToWait % 1000 ) * 1000; // remainder is msec, so *1000 for usec
  1332. if ( select ( 1+iMax, &fdsRead, NULL, NULL, &tvTimeout )<=0 )
  1333. continue;
  1334. ARRAY_FOREACH ( iAgent, tDist.m_dAgents )
  1335. {
  1336. Agent_t & tAgent = tDist.m_dAgents[iAgent];
  1337. if (!( tAgent.m_eState==AGENT_QUERY || tAgent.m_eState==AGENT_REPLY ))
  1338. continue;
  1339. if ( !FD_ISSET ( tAgent.m_iSock, &fdsRead ) )
  1340. continue;
  1341. // if there was no reply yet, read reply header
  1342. bool bFailure = true;
  1343. do
  1344. {
  1345. if ( tAgent.m_eState==AGENT_QUERY )
  1346. {
  1347. // try to read
  1348. struct
  1349. {
  1350. WORD m_iStatus;
  1351. WORD m_iVer;
  1352. int m_iLength;
  1353. } tReplyHeader;
  1354. STATIC_SIZE_ASSERT ( tReplyHeader, 8 );
  1355. if ( sphSockRecv ( tAgent.m_iSock, (char*)&tReplyHeader, sizeof(tReplyHeader), 0 )!=sizeof(tReplyHeader) )
  1356. {
  1357. // bail out if failed
  1358. dFailures.Add ( SearchFailure_t ( sIndexName,
  1359. "agent '%s:%d': failed to receive reply header",
  1360. tAgent.m_sHost.cstr(), tAgent.m_iPort ) );
  1361. break;
  1362. }
  1363. tReplyHeader.m_iStatus = ntohs ( tReplyHeader.m_iStatus );
  1364. tReplyHeader.m_iVer = ntohs ( tReplyHeader.m_iVer );
  1365. tReplyHeader.m_iLength = ntohl ( tReplyHeader.m_iLength );
  1366. // check the packet
  1367. if ( tReplyHeader.m_iLength<0 ) // FIXME! add reasonable max packet len too
  1368. {
  1369. dFailures.Add ( SearchFailure_t ( sIndexName,
  1370. "agent '%s:%d': ill-formed reply length (status=%d, len=%d)",
  1371. tAgent.m_sHost.cstr(), tAgent.m_iPort,
  1372. tReplyHeader.m_iStatus, tReplyHeader.m_iLength ) );
  1373. break;
  1374. }
  1375. // header received, switch the status
  1376. assert ( tAgent.m_pReplyBuf==NULL );
  1377. tAgent.m_eState = AGENT_REPLY;
  1378. tAgent.m_pReplyBuf = new char [ tReplyHeader.m_iLength ];
  1379. tAgent.m_iReplySize = tReplyHeader.m_iLength;
  1380. tAgent.m_iReplyRead = 0;
  1381. tAgent.m_iReplyStatus = tReplyHeader.m_iStatus;
  1382. if ( !tAgent.m_pReplyBuf )
  1383. {
  1384. // bail out if failed
  1385. dFailures.Add ( SearchFailure_t ( sIndexName,
  1386. "agent '%s:%d': failed to alloc %d bytes for reply buffer",
  1387. tAgent.m_sHost.cstr(), tAgent.m_iPort,
  1388. tAgent.m_iReplySize ) );
  1389. break;
  1390. }
  1391. }
  1392. // if we are reading reply, read another chunk
  1393. if ( tAgent.m_eState==AGENT_REPLY )
  1394. {
  1395. // do read
  1396. assert ( tAgent.m_iReplyRead<tAgent.m_iReplySize );
  1397. int iRes = sphSockRecv ( tAgent.m_iSock, tAgent.m_pReplyBuf+tAgent.m_iReplyRead,
  1398. tAgent.m_iReplySize-tAgent.m_iReplyRead, 0 );
  1399. // bail out if read failed
  1400. if ( iRes<0 )
  1401. {
  1402. dFailures.Add ( SearchFailure_t ( sIndexName,
  1403. "agent '%s:%d': failed to receive reply body: %s",
  1404. tAgent.m_sHost.cstr(), tAgent.m_iPort,
  1405. sphSockError() ) );
  1406. break;
  1407. }
  1408. assert ( iRes>0 );
  1409. assert ( tAgent.m_iReplyRead+iRes<=tAgent.m_iReplySize );
  1410. tAgent.m_iReplyRead += iRes;
  1411. }
  1412. // if reply was fully received, parse it
  1413. if ( tAgent.m_eState==AGENT_REPLY && tAgent.m_iReplyRead==tAgent.m_iReplySize )
  1414. {
  1415. MemInputBuffer_c tReq ( tAgent.m_pReplyBuf, tAgent.m_iReplySize );
  1416. if ( tReq.GetError() )
  1417. {
  1418. dFailures.Add ( SearchFailure_t ( sIndexName,
  1419. "agent '%s:%d': reply length out of bounds (len=%d, max_packet_size=%d)",
  1420. tAgent.m_sHost.cstr(), tAgent.m_iPort,
  1421. tAgent.m_iReplySize, MAX_PACKET_SIZE ) );
  1422. break;
  1423. }
  1424. if ( tAgent.m_iReplyStatus==SEARCHD_WARNING )
  1425. {
  1426. CSphString sAgentWarning = tReq.GetString ();
  1427. dFailures.Add ( SearchFailure_t ( sIndexName,
  1428. "agent '%s:%d': remote warning (status=%d, error=%s)",
  1429. tAgent.m_sHost.cstr(), tAgent.m_iPort,
  1430. tAgent.m_iReplyStatus, sAgentWarning.cstr() ) );
  1431. } else if ( tAgent.m_iReplyStatus!=SEARCHD_OK )
  1432. {
  1433. CSphString sAgentError = tReq.GetString ();
  1434. dFailures.Add ( SearchFailure_t ( sIndexName,
  1435. "agent '%s:%d': remote error (status=%d, error=%s)",
  1436. tAgent.m_sHost.cstr(), tAgent.m_iPort,
  1437. tAgent.m_iReplyStatus, sAgentError.cstr() ) );
  1438. break;
  1439. }
  1440. // get schema
  1441. CSphSchema & tSchema = tAgent.m_tRes.m_tSchema;
  1442. tSchema.m_dFields.Resize ( tReq.GetInt() ); // FIXME! add a sanity check
  1443. ARRAY_FOREACH ( j, tSchema.m_dFields )
  1444. tSchema.m_dFields[j].m_sName = tReq.GetString ();
  1445. tSchema.m_dAttrs.Resize ( tReq.GetInt() ); // FIXME! add a sanity check
  1446. ARRAY_FOREACH ( j, tSchema.m_dAttrs )
  1447. {
  1448. tSchema.m_dAttrs[j].m_sName = tReq.GetString ();
  1449. tSchema.m_dAttrs[j].m_eAttrType = (ESphAttrType) tReq.GetDword (); // FIXME! add a sanity check
  1450. }
  1451. // get matches
  1452. int iMatches = tReq.GetInt ();
  1453. if ( iMatches<0 || iMatches>g_iMaxMatches )
  1454. {
  1455. dFailures.Add ( SearchFailure_t ( sIndexName,
  1456. "agent '%s:%d': invalid match count received (count=%d)",
  1457. tAgent.m_sHost.cstr(), tAgent.m_iPort, iMatches ) );
  1458. break;
  1459. }
  1460. assert ( !tAgent.m_tRes.m_dMatches.GetLength() );
  1461. int iAttrs = tSchema.m_dAttrs.GetLength();
  1462. if ( iMatches )
  1463. {
  1464. tAgent.m_tRes.m_dMatches.Resize ( iMatches );
  1465. ARRAY_FOREACH ( i, tAgent.m_tRes.m_dMatches )
  1466. {
  1467. CSphMatch & tMatch = tAgent.m_tRes.m_dMatches[i];
  1468. tMatch.Reset ( iAttrs );
  1469. tMatch.m_iDocID = tReq.GetInt ();
  1470. tMatch.m_iWeight = tReq.GetInt ();
  1471. for ( int j=0; j<iAttrs; j++ )
  1472. tMatch.m_pAttrs[j] = tReq.GetDword ();
  1473. }
  1474. }
  1475. // read totals (retrieved count, total count, query time, word count)
  1476. int iRetrieved = tReq.GetInt ();
  1477. tAgent.m_tRes.m_iTotalMatches = tReq.GetInt ();
  1478. tAgent.m_tRes.m_iQueryTime = tReq.GetInt ();
  1479. tAgent.m_tRes.m_iNumWords = tReq.GetInt ();
  1480. if ( iRetrieved!=iMatches )
  1481. {
  1482. dFailures.Add ( SearchFailure_t ( sIndexName,
  1483. "agent '%s:%d': expected %d retrieved documents, got %d",
  1484. tAgent.m_sHost.cstr(), tAgent.m_iPort,
  1485. iMatches, iRetrieved ) );
  1486. break;
  1487. }
  1488. bool bSetWords = false;
  1489. if ( pRes->m_iNumWords && tAgent.m_tRes.m_iNumWords!=pRes->m_iNumWords )
  1490. {
  1491. dFailures.Add ( SearchFailure_t ( sIndexName,
  1492. "agent '%s:%d': expected %d query words, got %d",
  1493. tAgent.m_sHost.cstr(), tAgent.m_iPort,
  1494. pRes->m_iNumWords, tAgent.m_tRes.m_iNumWords ) );
  1495. break;
  1496. }
  1497. if ( !pRes->m_iNumWords )
  1498. {
  1499. pRes->m_iNumWords = tAgent.m_tRes.m_iNumWords;
  1500. bSetWords = true;
  1501. }
  1502. // read words
  1503. int i;
  1504. for ( i=0; i<pRes->m_iNumWords; i++ )
  1505. {
  1506. CSphString sWord = tReq.GetString ();
  1507. // set it in result if not yet, or check if already
  1508. if ( bSetWords )
  1509. {
  1510. pRes->m_tWordStats[i].m_sWord = sWord;
  1511. } else
  1512. {
  1513. if ( pRes->m_tWordStats[i].m_sWord!=sWord )
  1514. break;
  1515. }
  1516. pRes->m_tWordStats[i].m_iDocs += tReq.GetInt (); // update docs count
  1517. pRes->m_tWordStats[i].m_iHits += tReq.GetInt (); // update hits count
  1518. }
  1519. if ( tReq.GetError() )
  1520. {
  1521. dFailures.Add ( SearchFailure_t ( sIndexName,
  1522. "agent '%s:%d': incomplete reply",
  1523. tAgent.m_sHost.cstr(), tAgent.m_iPort ) );
  1524. break;
  1525. }
  1526. if ( i!=pRes->m_iNumWords )
  1527. {
  1528. dFailures.Add ( SearchFailure_t ( sIndexName,
  1529. "agent '%s:%d': expected %d words, got %d",
  1530. tAgent.m_sHost.cstr(), tAgent.m_iPort,
  1531. i, pRes->m_iNumWords ) );
  1532. break;
  1533. }
  1534. // all is well
  1535. iAgents++;
  1536. tAgent.Close ();
  1537. }
  1538. bFailure = false;
  1539. } while ( false );
  1540. if ( bFailure )
  1541. {
  1542. tAgent.Close ();
  1543. tAgent.m_tRes.m_dMatches.Reset ();
  1544. }
  1545. tAgent.m_bFailure = bFailure;
  1546. }
  1547. }
  1548. // close timed-out agents
  1549. ARRAY_FOREACH ( iAgent, tDist.m_dAgents )
  1550. {
  1551. Agent_t & tAgent = tDist.m_dAgents[iAgent];
  1552. if ( tAgent.m_eState==AGENT_QUERY )
  1553. {
  1554. assert ( !tAgent.m_tRes.m_dMatches.GetLength() );
  1555. tAgent.Close ();
  1556. dFailures.Add ( SearchFailure_t ( sIndexName,
  1557. "agent '%s:%d': query timed out",
  1558. tAgent.m_sHost.cstr(), tAgent.m_iPort ) );
  1559. }
  1560. }
  1561. return iAgents;
  1562. }
  1563. #if USE_WINDOWS
  1564. #pragma warning(default:4127) // conditional expr is const
  1565. #pragma warning(default:4389) // signed/unsigned mismatch
  1566. #endif // USE_WINDOWS
  1567. /////////////////////////////////////////////////////////////////////////////
  1568. // SIGNALS
  1569. /////////////////////////////////////////////////////////////////////////////
  1570. #if !USE_WINDOWS
  1571. void sigchld ( int )
  1572. {
  1573. signal ( SIGCHLD, sigchld );
  1574. while ( waitpid ( 0, (int *)0, WNOHANG | WUNTRACED ) > 0 )
  1575. g_iChildren--;
  1576. }
  1577. void sigterm ( int )
  1578. {
  1579. sphInfo ( "caught SIGTERM, shutting down" );
  1580. Shutdown ();
  1581. exit ( 0 );
  1582. }
  1583. void sighup ( int )
  1584. {
  1585. sphInfo ( "rotating indices: caught SIGHUP, waiting for children to exit" );
  1586. g_iHUP = 1;
  1587. }
  1588. #endif // !USE_WINDOWS
  1589. /////////////////////////////////////////////////////////////////////////////
  1590. // THE SERVER
  1591. /////////////////////////////////////////////////////////////////////////////
  1592. inline bool operator < ( const CSphMatch & a, const CSphMatch & b )
  1593. {
  1594. return a.m_iDocID < b.m_iDocID;
  1595. };
  1596. /////////////////////////////////////////////////////////////////////////////
  1597. bool CheckSortAndSchema ( const CSphSchema ** ppFirst, ISphMatchQueue ** ppTop, CSphQuery & tQuery,
  1598. const CSphSchema * pServed, const char * sServedName, InputBuffer_c & tReq )
  1599. {
  1600. assert ( ppFirst );
  1601. assert ( pServed );
  1602. if ( !*ppFirst )
  1603. {
  1604. // lookup proper attribute index to sort by
  1605. *ppFirst = pServed;
  1606. // lookup proper attribute index to group by
  1607. if ( !tQuery.SetSchema ( *pServed ) )
  1608. {
  1609. tReq.SendErrorReply ( "index '%s': group-by attribute '%s' not found",
  1610. sServedName, tQuery.m_sGroupBy.cstr() );
  1611. return false;
  1612. }
  1613. assert ( tQuery.m_sGroupBy.IsEmpty() || tQuery.GetGroupByAttr()>=0 );
  1614. // spawn queue and set sort-by attribute
  1615. assert ( !*ppTop );
  1616. CSphString sError;
  1617. *ppTop = sphCreateQueue ( &tQuery, *pServed, sError );
  1618. if (! (*ppTop) )
  1619. {
  1620. tReq.SendErrorReply ( "index '%s': failed to create sorting queue: %s",
  1621. sServedName, sError.cstr() );
  1622. return false;
  1623. }
  1624. } else
  1625. {
  1626. assert ( *ppTop );
  1627. // check schemas
  1628. CSphString sError;
  1629. ESphSchemaCompare eComp = pServed->CompareTo ( **ppFirst, sError );
  1630. if ( eComp==SPH_SCHEMAS_INCOMPATIBLE )
  1631. {
  1632. tReq.SendErrorReply ( "index '%s': incompatible schemas: %s", sServedName, sError.cstr() );
  1633. return false;
  1634. }
  1635. // FIXME!!! warn if schemas are compatible but not equal!
  1636. }
  1637. return true;
  1638. }
  1639. struct OldQuery_t
  1640. {
  1641. int m_iVersion;
  1642. int m_iGroups;
  1643. DWORD * m_pGroups;
  1644. DWORD m_iMinTS;
  1645. DWORD m_iMaxTS;
  1646. DWORD m_iMinGID;
  1647. DWORD m_iMaxGID;
  1648. OldQuery_t ( int iVer )
  1649. : m_iVersion ( iVer )
  1650. , m_iGroups ( 0 )
  1651. , m_pGroups ( NULL )
  1652. , m_iMinTS ( 0 )
  1653. , m_iMaxTS ( UINT_MAX )
  1654. , m_iMinGID ( 0 )
  1655. , m_iMaxGID ( UINT_MAX )
  1656. {}
  1657. };
  1658. bool FixupQuery ( CSphQuery * pQuery, OldQuery_t * pOldQuery,
  1659. const CSphSchema * pSchema, const char * sIndexName, InputBuffer_c & tReq )
  1660. {
  1661. // already?
  1662. if ( !pOldQuery->m_iVersion )
  1663. return true;
  1664. if ( pOldQuery->m_iGroups>0 || pOldQuery->m_iMinGID!=0 || pOldQuery->m_iMaxGID!=UINT_MAX )
  1665. {
  1666. int iAttr = -1;
  1667. ARRAY_FOREACH ( i, pSchema->m_dAttrs )
  1668. if ( pSchema->m_dAttrs[i].m_eAttrType==SPH_ATTR_INTEGER )
  1669. {
  1670. iAttr = i;
  1671. break;
  1672. }
  1673. if ( iAttr<0 )
  1674. {
  1675. tReq.SendErrorReply ( "index '%s': no group attribute found", sIndexName );
  1676. return false;
  1677. }
  1678. CSphFilter tFilter;
  1679. tFilter.m_sAttrName = pSchema->m_dAttrs[iAttr].m_sName;
  1680. tFilter.m_iValues = pOldQuery->m_iGroups;
  1681. tFilter.m_pValues = pOldQuery->m_pGroups;
  1682. tFilter.m_uMinValue = pOldQuery->m_iMinGID;
  1683. tFilter.m_uMaxValue = pOldQuery->m_iMaxGID;
  1684. pQuery->m_dFilters.Add ( tFilter );
  1685. }
  1686. if ( pOldQuery->m_iMinTS!=0 || pOldQuery->m_iMaxTS!=UINT_MAX )
  1687. {
  1688. int iAttr = -1;
  1689. ARRAY_FOREACH ( i, pSchema->m_dAttrs )
  1690. if ( pSchema->m_dAttrs[i].m_eAttrType==SPH_ATTR_TIMESTAMP )
  1691. {
  1692. iAttr = i;
  1693. break;
  1694. }
  1695. if ( iAttr<0 )
  1696. {
  1697. tReq.SendErrorReply ( "index '%s': no timestamp attribute found", sIndexName );
  1698. return false;
  1699. }
  1700. CSphFilter tFilter;
  1701. tFilter.m_sAttrName = pSchema->m_dAttrs[iAttr].m_sName;
  1702. tFilter.m_uMinValue = pOldQuery->m_iMinTS;
  1703. tFilter.m_uMaxValue = pOldQuery->m_iMaxTS;
  1704. pQuery->m_dFilters.Add ( tFilter );
  1705. }
  1706. pOldQuery->m_iVersion = 0;
  1707. return true;
  1708. }
  1709. struct StrBuf_t
  1710. {
  1711. protected:
  1712. char m_sBuf [ 2048 ];
  1713. char * m_pBuf;
  1714. int m_iLeft;
  1715. public:
  1716. StrBuf_t ()
  1717. {
  1718. memset ( m_sBuf, 0, sizeof(m_sBuf) );
  1719. m_iLeft = sizeof(m_sBuf)-1;
  1720. m_pBuf = m_sBuf;
  1721. }
  1722. const char * cstr ()
  1723. {
  1724. return m_sBuf;
  1725. }
  1726. int GetLength ()
  1727. {
  1728. return sizeof(m_sBuf)-1-m_iLeft;
  1729. }
  1730. bool Append ( const char * s, bool bWhole )
  1731. {
  1732. int iLen = strlen(s);
  1733. if ( bWhole && m_iLeft<iLen )
  1734. return false;
  1735. iLen = Min ( m_iLeft, iLen );
  1736. memcpy ( m_pBuf, s, iLen );
  1737. m_pBuf += iLen;
  1738. m_iLeft -= iLen;
  1739. return true;
  1740. }
  1741. const StrBuf_t & operator += ( const char * s )
  1742. {
  1743. Append ( s, false );
  1744. return *this;
  1745. }
  1746. };
  1747. void ReportSearchFailures ( StrBuf_t & sReport, SearchFailuresLog_t & dFailures )
  1748. {
  1749. if ( !dFailures.GetLength() )
  1750. return;
  1751. // collapse same messages
  1752. dFailures.Sort ();
  1753. int iSpanStart = 0;
  1754. for ( int i=1; i<=dFailures.GetLength(); i++ )
  1755. {
  1756. // keep scanning while error text is the same
  1757. if ( i!=dFailures.GetLength() )
  1758. if ( dFailures[i].m_sError==dFailures[i-1].m_sError )
  1759. continue;
  1760. // build current span
  1761. StrBuf_t sSpan;
  1762. if ( iSpanStart )
  1763. sSpan += "; ";
  1764. sSpan += "index ";
  1765. for ( int j=iSpanStart; j<i; j++ )
  1766. {
  1767. if ( j!=iSpanStart )
  1768. sSpan += ",";
  1769. sSpan += "'";
  1770. sSpan += dFailures[j].m_sIndex.cstr();
  1771. sSpan += "'";
  1772. }
  1773. sSpan += ": ";
  1774. if ( !sSpan.Append ( dFailures[iSpanStart].m_sError.cstr(), true ) )
  1775. break;
  1776. // flush current span
  1777. if ( !sReport.Append ( sSpan.cstr(), true ) )
  1778. break;
  1779. // done
  1780. iSpanStart = i;
  1781. }
  1782. }
  1783. void HandleCommandSearch ( int iSock, int iVer, InputBuffer_c & tReq )
  1784. {
  1785. CSphQuery tQuery;
  1786. assert ( sizeof(tQuery.m_eSort)==4 );
  1787. // check major command version
  1788. if ( (iVer>>8)!=(VER_COMMAND_SEARCH>>8) )
  1789. {
  1790. tReq.SendErrorReply ( "major command version mismatch (expected v.%d.x, got v.%d.%d)",
  1791. VER_COMMAND_SEARCH>>8, iVer>>8, iVer&0xff );
  1792. return;
  1793. }
  1794. if ( iVer>VER_COMMAND_SEARCH )
  1795. {
  1796. tReq.SendErrorReply ( "client version is higher than daemon version (client is v.%d.%d, daemon is v.%d.%d)",
  1797. iVer>>8, iVer&0xff, VER_COMMAND_SEARCH>>8, VER_COMMAND_SEARCH&0xff );
  1798. return;
  1799. }
  1800. /////////////////
  1801. // parse request
  1802. /////////////////
  1803. OldQuery_t tOldQuery ( iVer );
  1804. // v.1.0. mode, limits, weights, ID/TS ranges
  1805. tQuery.m_iOffset = tReq.GetInt ();
  1806. tQuery.m_iLimit = tReq.GetInt ();
  1807. tQuery.m_eMode = (ESphMatchMode) tReq.GetInt ();
  1808. tQuery.m_eSort = (ESphSortOrder) tReq.GetInt ();
  1809. if ( iVer<=0x101 )
  1810. tOldQuery.m_iGroups = tReq.GetDwords ( &tOldQuery.m_pGroups, SEARCHD_MAX_ATTR_VALUES, "invalid group count %d (should be in 0..%d range)" );
  1811. if ( iVer>=0x102 )
  1812. {
  1813. tQuery.m_sSortBy = tReq.GetString ();
  1814. tQuery.m_sSortBy.ToLower ();
  1815. }
  1816. tQuery.m_sQuery = tReq.GetString ();
  1817. tQuery.m_iWeights = tReq.GetDwords ( (DWORD**)&tQuery.m_pWeights, SPH_MAX_FIELDS, "invalid weight count %d (should be in 0..%d range)" );
  1818. CSphString sIndexes = tReq.GetString ();
  1819. tQuery.m_iMinID = tReq.GetDword ();
  1820. tQuery.m_iMaxID = tReq.GetDword ();
  1821. // upto v.1.1
  1822. if ( iVer<=0x101 )
  1823. {
  1824. tOldQuery.m_iMinTS = tReq.GetDword ();
  1825. tOldQuery.m_iMaxTS = tReq.GetDword ();
  1826. }
  1827. // v.1.1 specific
  1828. if ( iVer==0x101 )
  1829. {
  1830. tOldQuery.m_iMinGID = tReq.GetDword ();
  1831. tOldQuery.m_iMaxGID = tReq.GetDword ();
  1832. }
  1833. // !COMMIT use min/max ts/gid
  1834. // v.1.2
  1835. if ( iVer>=0x102 )
  1836. {
  1837. int iAttrFilters = tReq.GetInt ();
  1838. if ( iAttrFilters>SEARCHD_MAX_ATTRS )
  1839. {
  1840. tReq.SendErrorReply ( "too much attribute filters (req=%d, max=%d)", iAttrFilters, SEARCHD_MAX_ATTRS );
  1841. return;
  1842. }
  1843. tQuery.m_dFilters.Resize ( iAttrFilters );
  1844. ARRAY_FOREACH ( i, tQuery.m_dFilters )
  1845. {
  1846. CSphFilter & tFilter = tQuery.m_dFilters[i];
  1847. tFilter.m_sAttrName = tReq.GetString ();
  1848. tFilter.m_sAttrName.ToLower ();
  1849. tFilter.m_iValues = tReq.GetDwords ( &tFilter.m_pValues, SEARCHD_MAX_ATTR_VALUES,
  1850. "invalid attribute set length %d (should be in 0..%d range)" );
  1851. if ( !tFilter.m_iValues )
  1852. {
  1853. // 0 length means this is range, not set
  1854. tFilter.m_uMinValue = tReq.GetDword ();
  1855. tFilter.m_uMaxValue = tReq.GetDword ();
  1856. }
  1857. if ( iVer>=0x106 )
  1858. tFilter.m_bExclude = !!tReq.GetDword ();
  1859. }
  1860. }
  1861. // v.1.3
  1862. if ( iVer>=0x103 )
  1863. {
  1864. tQuery.m_eGroupFunc = (ESphGroupBy) tReq.GetDword ();
  1865. tQuery.m_sGroupBy = tReq.GetString ();
  1866. tQuery.m_sGroupBy.ToLower ();
  1867. }
  1868. // v.1.4
  1869. tQuery.m_iMaxMatches = g_iMaxMatches;
  1870. if ( iVer>=0x104 )
  1871. tQuery.m_iMaxMatches = tReq.GetInt ();
  1872. // v.1.5
  1873. tQuery.m_bSortByGroup = true;
  1874. if ( iVer>=0x105 )
  1875. if ( !tReq.GetInt() )
  1876. tQuery.m_bSortByGroup = false;
  1877. // additional checks
  1878. if ( tReq.GetError() )
  1879. {
  1880. tReq.SendErrorReply ( "invalid or truncated request" );
  1881. return;
  1882. }
  1883. if ( tQuery.m_iMinID>tQuery.m_iMaxID )
  1884. {
  1885. tReq.SendErrorReply ( "invalid ID range specified in query" );
  1886. return;
  1887. }
  1888. if ( tQuery.m_eMode<0 || tQuery.m_eMode>SPH_MATCH_TOTAL )
  1889. {
  1890. tReq.SendErrorReply ( "invalid match mode %d", tQuery.m_eMode );
  1891. return;
  1892. }
  1893. if ( tQuery.m_iMaxMatches<1 || tQuery.m_iMaxMatches>g_iMaxMatches )
  1894. {
  1895. tReq.SendErrorReply ( "per-query max_matches=%d out of bounds (per-server max_matches=%d)",
  1896. tQuery.m_iMaxMatches, g_iMaxMatches );
  1897. return;
  1898. }
  1899. if ( tQuery.m_iOffset<0 || tQuery.m_iOffset>=tQuery.m_iMaxMatches )
  1900. {
  1901. tReq.SendErrorReply ( "offset out of bounds (offset=%d, max_matches=%d)",
  1902. tQuery.m_iOffset, tQuery.m_iMaxMatches );
  1903. return;
  1904. }
  1905. if ( tQuery.m_iLimit<0 )
  1906. {
  1907. tReq.SendErrorReply ( "limit out of bounds (limit=%d)", tQuery.m_iLimit );
  1908. return;
  1909. }
  1910. ////////////////
  1911. // do searching
  1912. ////////////////
  1913. // do search
  1914. float tmStart = sphLongTimer ();
  1915. const CSphSchema * pFirst = NULL;
  1916. CSphQueryResult * pRes = new CSphQueryResult ();
  1917. ISphMatchQueue * pTop = NULL;
  1918. #define REMOVE_DUPES 1
  1919. SearchFailuresLog_t dFailures;
  1920. int iTries = 0;
  1921. int iSuccesses = 0;
  1922. if ( g_hDistIndexes(sIndexes) )
  1923. {
  1924. // search through specified distributed index
  1925. DistributedIndex_t & tDist = g_hDistIndexes[sIndexes];
  1926. iTries += tDist.m_dAgents.GetLength();
  1927. // start connecting to remote agents
  1928. ARRAY_FOREACH ( i, tDist.m_dAgents )
  1929. ConnectToRemoteAgent ( &tDist.m_dAgents[i], sIndexes.cstr(), dFailures );
  1930. // connect to remote agents and query them first
  1931. int iRemote = QueryRemoteAgents ( sIndexes.cstr(), tDist, tQuery, tQuery.m_eMode, dFailures );
  1932. // while the remote queries are running, do local searches
  1933. // FIXME! what if the remote agents finish early, could they timeout?
  1934. float tmQuery = -sphLongTimer ();
  1935. ARRAY_FOREACH ( i, tDist.m_dLocal )
  1936. {
  1937. const ServedIndex_t & tServed = g_hIndexes [ tDist.m_dLocal[i] ];
  1938. assert ( tServed.m_pIndex );
  1939. assert ( tServed.m_pDict );
  1940. assert ( tServed.m_pTokenizer );
  1941. // check/set sort-by attr and schema
  1942. if ( !CheckSortAndSchema ( &pFirst, &pTop, tQuery, tServed.m_pSchema, tDist.m_dLocal[i].cstr(), tReq ) )
  1943. return;
  1944. // do query
  1945. tQuery.m_pTokenizer = tServed.m_pTokenizer;
  1946. iTries++;
  1947. if ( !tServed.m_pIndex->QueryEx ( tServed.m_pDict, &tQuery, pRes, pTop ) )
  1948. dFailures.Add ( SearchFailure_t ( tDist.m_dLocal[i].cstr(), "%s", pRes->m_sError.cstr() ) );
  1949. else
  1950. iSuccesses++;
  1951. #if REMOVE_DUPES
  1952. // group-by queries remove dupes themselves
  1953. if ( tQuery.GetGroupByAttr()<0 )
  1954. sphFlattenQueue ( pTop, pRes );
  1955. #endif
  1956. }
  1957. tmQuery += sphLongTimer ();
  1958. if ( !iRemote && !tDist.m_dLocal.GetLength() )
  1959. {
  1960. tReq.SendErrorReply ( "all remote agents unreachable, no local indexes configured" );
  1961. return;
  1962. }
  1963. // wait for remote queries to complete
  1964. while ( iRemote )
  1965. {
  1966. int iMsecLeft = tDist.m_iAgentQueryTimeout - int(tmQuery*1000.0f);
  1967. int iReplys = WaitForRemoteAgents ( sIndexes.cstr(), tDist, pRes, Max ( iMsecLeft, 0 ), dFailures );
  1968. // check if there were valid (though might be 0-matches) replys
  1969. iSuccesses += iReplys;
  1970. if ( !iReplys )
  1971. break;
  1972. // merge in remote results
  1973. ARRAY_FOREACH ( iAgent, tDist.m_dAgents )
  1974. {
  1975. Agent_t & tAgent = tDist.m_dAgents[iAgent];
  1976. if ( tAgent.m_bFailure )
  1977. continue;
  1978. // merge this agent's results
  1979. // FIXME! should check schema before; but sometimes it's empty
  1980. if ( !tAgent.m_tRes.m_dMatches.GetLength() )
  1981. continue;
  1982. // check/set sort-by attr and schema
  1983. char sName [ 1024 ];
  1984. snprintf ( sName, sizeof(sName), "%s:%d:%s",
  1985. tAgent.m_sHost.cstr(), tAgent.m_iPort, tAgent.m_sIndexes.cstr() );
  1986. if ( !CheckSortAndSchema ( &pFirst, &pTop, tQuery, &tAgent.m_tRes.m_tSchema, sName, tReq ) )
  1987. return;
  1988. if ( tQuery.GetGroupByAttr()<0 )
  1989. {
  1990. ARRAY_FOREACH ( i, tAgent.m_tRes.m_dMatches )
  1991. pRes->m_dMatches.Add ( tAgent.m_tRes.m_dMatches[i] );
  1992. } else
  1993. {
  1994. ARRAY_FOREACH ( i, tAgent.m_tRes.m_dMatches )
  1995. pTop->Push ( tAgent.m_tRes.m_dMatches[i] );
  1996. }
  1997. tAgent.m_tRes.m_dMatches.Reset ();
  1998. // merge this agent's stats
  1999. pRes->m_iTotalMatches += tAgent.m_tRes.m_iTotalMatches;
  2000. }
  2001. break;
  2002. }
  2003. // if there were no local indexes, schema in pRes was not yet set,
  2004. // so we have to copy it from first available agent schema
  2005. if ( !tDist.m_dLocal.GetLength() && pRes->m_iTotalMatches )
  2006. {
  2007. assert ( pFirst );
  2008. assert ( pRes->m_tSchema.m_dAttrs.GetLength()==0 );
  2009. assert ( pRes->m_tSchema.m_dFields.GetLength()==0 );
  2010. pRes->m_tSchema = *pFirst;
  2011. }
  2012. } else if ( sIndexes=="*" )
  2013. {
  2014. // search through all local indexes
  2015. g_hIndexes.IterateStart ();
  2016. while ( g_hIndexes.IterateNext () )
  2017. {
  2018. const ServedIndex_t & tServed = g_hIndexes.IterateGet ();
  2019. assert ( tServed.m_pIndex );
  2020. assert ( tServed.m_pDict );
  2021. assert ( tServed.m_pTokenizer );
  2022. const char * sIndexName = g_hIndexes.IterateGetKey().cstr();
  2023. // check/set sort-by attr and schema
  2024. if ( !CheckSortAndSchema ( &pFirst, &pTop, tQuery, tServed.m_pSchema, sIndexName, tReq ) )
  2025. return;
  2026. // fixup old queries
  2027. if ( !FixupQuery ( &tQuery, &tOldQuery, tServed.m_pSchema, sIndexName, tReq ) )
  2028. return;
  2029. // do query
  2030. iTries++;
  2031. tQuery.m_pTokenizer = tServed.m_pTokenizer;
  2032. if ( !tServed.m_pIndex->QueryEx ( tServed.m_pDict, &tQuery, pRes, pTop ) )
  2033. dFailures.Add ( SearchFailure_t ( sIndexName, "%s", pRes->m_sError.cstr() ) );
  2034. else
  2035. iSuccesses++;
  2036. #if REMOVE_DUPES
  2037. // group-by queries remove dupes themselves
  2038. if ( tQuery.GetGroupByAttr()<0 )
  2039. sphFlattenQueue ( pTop, pRes );
  2040. #endif
  2041. }
  2042. if ( !iTries )
  2043. {
  2044. tReq.SendErrorReply ( "no local indexes configured" );
  2045. return;
  2046. }
  2047. } else
  2048. {
  2049. // search through the specified local indexes
  2050. CSphString sSplit = sIndexes;
  2051. char * p = (char*)sSplit.cstr();
  2052. while ( *p )
  2053. {
  2054. // skip non-alphas
  2055. while ( (*p) && !sphIsAlpha(*p) ) p++;
  2056. if ( !(*p) ) break;
  2057. // this is my next index name
  2058. const char * sNext = p;
  2059. while ( sphIsAlpha(*p) ) p++;
  2060. assert ( sNext!=p );
  2061. if ( *p ) *p++ = '\0'; // if it was not the end yet, we'll continue from next char
  2062. // check that this one exists
  2063. if ( !g_hIndexes.Exists ( sNext ) )
  2064. {
  2065. tReq.SendErrorReply ( "invalid index '%s' specified in request", sNext );
  2066. return;
  2067. }
  2068. const ServedIndex_t & tServed = g_hIndexes[sNext];
  2069. assert ( tServed.m_pIndex );
  2070. assert ( tServed.m_pDict );
  2071. assert ( tServed.m_pTokenizer );
  2072. #if !USE_WINDOWS
  2073. CSphCache tCache ( g_sCacheDir.cstr(), g_iCacheTTL, g_bCacheGzip );
  2074. if ( !g_bCacheEnable
  2075. || !tCache.ReadFromFile ( tQuery, sNext, tServed.m_pIndexPath->cstr(), pRes ) )
  2076. #endif
  2077. {
  2078. // check/set sort-by attr and schema
  2079. if ( !CheckSortAndSchema ( &pFirst, &pTop, tQuery, tServed.m_pSchema, sNext, tReq ) )
  2080. return;
  2081. // fixup old queries
  2082. if ( !FixupQuery ( &tQuery, &tOldQuery, tServed.m_pSchema, sNext, tReq ) )
  2083. return;
  2084. // do query
  2085. tQuery.m_pTokenizer = tServed.m_pTokenizer;
  2086. iTries++;
  2087. if ( !tServed.m_pIndex->QueryEx ( tServed.m_pDict, &tQuery, pRes, pTop ) )
  2088. dFailures.Add ( SearchFailure_t ( sNext, "%s", pRes->m_sError.cstr() ) );
  2089. else
  2090. iSuccesses++;
  2091. #if REMOVE_DUPES
  2092. // group-by queries remove dupes themselves
  2093. if ( tQuery.GetGroupByAttr()<0 )
  2094. sphFlattenQueue ( pTop, pRes );
  2095. #endif
  2096. #if !USE_WINDOWS
  2097. if ( g_bCacheEnable )
  2098. tCache.StoreResult ( tQuery, sNext, pRes );
  2099. #endif
  2100. }
  2101. }
  2102. if ( !iTries )
  2103. {
  2104. tReq.SendErrorReply ( "no valid indexes specified in request" );
  2105. return;
  2106. }
  2107. }
  2108. // build report
  2109. StrBuf_t sFailures;
  2110. ReportSearchFailures ( sFailures, dFailures );
  2111. // if there were no succesful searches at all, this is an error
  2112. assert ( iTries );
  2113. if ( !iSuccesses )
  2114. {
  2115. tReq.SendErrorReply ( "%s", sFailures.cstr() );
  2116. return;
  2117. }
  2118. #if REMOVE_DUPES
  2119. if ( tQuery.GetGroupByAttr()>=0 )
  2120. {
  2121. // group-by queries remove dupes themselves, so just flatten
  2122. sphFlattenQueue ( pTop, pRes );
  2123. } else if ( iSuccesses!=1 )
  2124. {
  2125. // if there was only 1 index searched, it's already properly flattened
  2126. pRes->m_dMatches.Sort ();
  2127. ARRAY_FOREACH ( i, pRes->m_dMatches )
  2128. {
  2129. if ( i==0 || pRes->m_dMatches[i].m_iDocID!=pRes->m_dMatches[i-1].m_iDocID )
  2130. pTop->Push ( pRes->m_dMatches[i] );
  2131. }
  2132. pRes->m_dMatches.Reset ();
  2133. sphFlattenQueue ( pTop, pRes );
  2134. }
  2135. #else
  2136. sphFlattenQueue ( pTop, pRes );
  2137. #endif
  2138. pRes->m_iQueryTime = int ( 1000.0f*( sphLongTimer() - tmStart ) );
  2139. // log query
  2140. if ( g_iQueryLogFile>=0 )
  2141. {
  2142. time_t tNow;
  2143. char sTimeBuf[128], sGroupBuf[128];
  2144. char sBuf[1024];
  2145. time ( &tNow );
  2146. ctime_r ( &tNow, sTimeBuf );
  2147. sTimeBuf [ strlen(sTimeBuf)-1 ] = '\0';
  2148. sGroupBuf[0] = '\0';
  2149. if ( tQuery.GetGroupByAttr()>=0 )
  2150. snprintf ( sGroupBuf, sizeof(sGroupBuf), " @%s", tQuery.m_sGroupBy.cstr() );
  2151. static const char * sModes [ SPH_MATCH_TOTAL ] = { "all", "any", "phr", "bool", "ext" };
  2152. static const char * sSort [ SPH_SORT_TOTAL ] = { "rel", "attr-", "attr+", "tsegs", "ext" };
  2153. snprintf ( sBuf, sizeof(sBuf), "[%s] %d.%03d sec [%s/%d/%s %d (%d,%d)%s] [%s] %s\n",
  2154. sTimeBuf, pRes->m_iQueryTime/1000, pRes->m_iQueryTime%1000,
  2155. sModes [ tQuery.m_eMode ], tQuery.m_dFilters.GetLength(), sSort [ tQuery.m_eSort ],
  2156. pRes->m_iTotalMatches, tQuery.m_iOffset, tQuery.m_iLimit, sGroupBuf,
  2157. sIndexes.cstr(), tQuery.m_sQuery.cstr() );
  2158. sphLockEx ( g_iQueryLogFile );
  2159. lseek ( g_iQueryLogFile, 0, SEEK_END );
  2160. write ( g_iQueryLogFile, sBuf, strlen(sBuf) );
  2161. sphLockUn ( g_iQueryLogFile );
  2162. }
  2163. //////////////////////
  2164. // serve the response
  2165. //////////////////////
  2166. // calc response length
  2167. int iRespLen = 20; // header
  2168. if ( iVer>=0x102 ) // schema
  2169. {
  2170. iRespLen += 8; // 4 for field count, 4 for attr count
  2171. ARRAY_FOREACH ( i, pRes->m_tSchema.m_dFields )
  2172. iRespLen += 4 + strlen ( pRes->m_tSchema.m_dFields[i].m_sName.cstr() ); // namelen, name
  2173. ARRAY_FOREACH ( i, pRes->m_tSchema.m_dAttrs )
  2174. iRespLen += 8 + strlen ( pRes->m_tSchema.m_dAttrs[i].m_sName.cstr() ); // namelen, name, type
  2175. }
  2176. int iCount = Max ( Min ( tQuery.m_iLimit, pRes->m_dMatches.GetLength()-tQuery.m_iOffset ), 0 );
  2177. if ( iVer<=0x101 )
  2178. iRespLen += 16*iCount; // matches
  2179. else
  2180. iRespLen += ( 8+4*pRes->m_tSchema.m_dAttrs.GetLength() )*iCount; // matches
  2181. for ( int i=0; i<pRes->m_iNumWords; i++ ) // per-word stats
  2182. iRespLen += 12 + strlen ( pRes->m_tWordStats[i].m_sWord.cstr() ); // wordlen, word, docs, hits
  2183. bool bWarning = ( iVer>=0x106 && dFailures.GetLength() );
  2184. if ( bWarning )
  2185. iRespLen += 4 + strlen ( sFailures.cstr() );
  2186. // send header
  2187. NetOutputBuffer_c tOut ( iSock );
  2188. tOut.SendWord ( (WORD)( bWarning ? SEARCHD_WARNING : SEARCHD_OK ) );
  2189. tOut.SendWord ( VER_COMMAND_SEARCH );
  2190. tOut.SendInt ( iRespLen );
  2191. // send warning
  2192. if ( bWarning )
  2193. tOut.SendString ( sFailures.cstr() );
  2194. // send schema
  2195. if ( iVer>=0x102 )
  2196. {
  2197. tOut.SendInt ( pRes->m_tSchema.m_dFields.GetLength() );
  2198. ARRAY_FOREACH ( i, pRes->m_tSchema.m_dFields )
  2199. tOut.SendString ( pRes->m_tSchema.m_dFields[i].m_sName.cstr() );
  2200. tOut.SendInt ( pRes->m_tSchema.m_dAttrs.GetLength() );
  2201. ARRAY_FOREACH ( i, pRes->m_tSchema.m_dAttrs )
  2202. {
  2203. tOut.SendString ( pRes->m_tSchema.m_dAttrs[i].m_sName.cstr() );
  2204. tOut.SendDword ( (DWORD)pRes->m_tSchema.m_dAttrs[i].m_eAttrType );
  2205. }
  2206. }
  2207. // send matches
  2208. int iGIDIndex = -1;
  2209. int iTSIndex = -1;
  2210. if ( iVer<=0x101 )
  2211. ARRAY_FOREACH ( i, pRes->m_tSchema.m_dAttrs )
  2212. {
  2213. if ( iTSIndex<0 && pRes->m_tSchema.m_dAttrs[i].m_eAttrType==SPH_ATTR_TIMESTAMP )
  2214. iTSIndex = i;
  2215. if ( iGIDIndex<0 && pRes->m_tSchema.m_dAttrs[i].m_eAttrType==SPH_ATTR_INTEGER )
  2216. iGIDIndex = i;
  2217. }
  2218. tOut.SendInt ( iCount );
  2219. for ( int i=0; i<iCount; i++ )
  2220. {
  2221. const CSphMatch & tMatch = pRes->m_dMatches[tQuery.m_iOffset+i];
  2222. tOut.SendDword ( tMatch.m_iDocID );
  2223. if ( iVer<=0x101 )
  2224. {
  2225. tOut.SendDword ( iGIDIndex>=0 ? tMatch.m_pAttrs[iGIDIndex] : 1 );
  2226. tOut.SendDword ( iTSIndex>=0 ? tMatch.m_pAttrs[iTSIndex] : 1 );
  2227. tOut.SendInt ( tMatch.m_iWeight );
  2228. } else
  2229. {
  2230. tOut.SendInt ( tMatch.m_iWeight );
  2231. assert ( tMatch.m_iAttrs==pRes->m_tSchema.m_dAttrs.GetLength() );
  2232. for ( int j=0; j<tMatch.m_iAttrs; j++ )
  2233. tOut.SendDword ( tMatch.m_pAttrs[j] );
  2234. }
  2235. }
  2236. tOut.SendInt ( pRes->m_dMatches.GetLength() );
  2237. tOut.SendInt ( pRes->m_iTotalMatches );
  2238. tOut.SendInt ( pRes->m_iQueryTime );
  2239. tOut.SendInt ( pRes->m_iNumWords );
  2240. for ( int i=0; i<pRes->m_iNumWords; i++ )
  2241. {
  2242. tOut.SendString ( pRes->m_tWordStats[i].m_sWord.cstr() );
  2243. tOut.SendInt ( pRes->m_tWordStats[i].m_iDocs );
  2244. tOut.SendInt ( pRes->m_tWordStats[i].m_iHits );
  2245. }
  2246. tOut.Flush ();
  2247. assert ( tOut.GetError()==true || tOut.GetSentCount()==iRespLen+8 );
  2248. SafeDelete ( pRes );
  2249. SafeDelete ( pTop );
  2250. }
  2251. /////////////////////////////////////////////////////////////////////////////
  2252. void HandleCommandExcerpt ( int iSock, int iVer, InputBuffer_c & tReq )
  2253. {
  2254. // check major command version
  2255. if ( (iVer>>8)!=(VER_COMMAND_EXCERPT>>8) )
  2256. {
  2257. tReq.SendErrorReply ( "major command version mismatch (expected v.%d.x, got v.%d.%d)",
  2258. VER_COMMAND_EXCERPT>>8, iVer>>8, iVer&0xff );
  2259. return;
  2260. }
  2261. /////////////////////////////
  2262. // parse and process request
  2263. /////////////////////////////
  2264. const int EXCERPT_MAX_ENTRIES = 1024;
  2265. const int EXCERPT_FLAG_REMOVESPACES = 1;
  2266. // v.1.0
  2267. ExcerptQuery_t q;
  2268. tReq.GetInt (); // mode field is for now reserved and ignored
  2269. int iFlags = tReq.GetInt ();
  2270. CSphString sIndex = tReq.GetString ();
  2271. if ( !g_hIndexes ( sIndex ) )
  2272. {
  2273. tReq.SendErrorReply ( "invalid local index '%s' specified in request", sIndex.cstr() );
  2274. return;
  2275. }
  2276. CSphDict * pDict = g_hIndexes[sIndex].m_pDict;
  2277. ISphTokenizer * pTokenizer = g_hIndexes[sIndex].m_pTokenizer;
  2278. q.m_sWords = tReq.GetString ();
  2279. q.m_sBeforeMatch = tReq.GetString ();
  2280. q.m_sAfterMatch = tReq.GetString ();
  2281. q.m_sChunkSeparator = tReq.GetString ();
  2282. q.m_iLimit = tReq.GetInt ();
  2283. q.m_iAround = tReq.GetInt ();
  2284. q.m_bRemoveSpaces = ( iFlags & EXCERPT_FLAG_REMOVESPACES );
  2285. int iCount = tReq.GetInt ();
  2286. if ( iCount<0 || iCount>EXCERPT_MAX_ENTRIES )
  2287. {
  2288. tReq.SendErrorReply ( "invalid entries count %d", iCount );
  2289. return;
  2290. }
  2291. CSphVector < char *, 32 > dExcerpts;
  2292. for ( int i=0; i<iCount; i++ )
  2293. {
  2294. q.m_sSource = tReq.GetString ();
  2295. if ( tReq.GetError() )
  2296. {
  2297. tReq.SendErrorReply ( "invalid or truncated request" );
  2298. return;
  2299. }
  2300. dExcerpts.Add ( sphBuildExcerpt ( q, pDict, pTokenizer ) );
  2301. }
  2302. ////////////////
  2303. // serve result
  2304. ////////////////
  2305. int iRespLen = 0;
  2306. ARRAY_FOREACH ( i, dExcerpts )
  2307. iRespLen += 4 + strlen ( dExcerpts[i] );
  2308. NetOutputBuffer_c tOut ( iSock );
  2309. tOut.SendWord ( SEARCHD_OK );
  2310. tOut.SendWord ( VER_COMMAND_EXCERPT );
  2311. tOut.SendInt ( iRespLen );
  2312. ARRAY_FOREACH ( i, dExcerpts )
  2313. {
  2314. tOut.SendString ( dExcerpts[i] );
  2315. SafeDeleteArray ( dExcerpts[i] );
  2316. }
  2317. tOut.Flush ();
  2318. assert ( tOut.GetError()==true || tOut.GetSentCount()==iRespLen+8 );
  2319. }
  2320. /////////////////////////////////////////////////////////////////////////////
  2321. void HandleCommandUpdate ( int iSock, int iVer, InputBuffer_c & tReq )
  2322. {
  2323. // check major command version
  2324. if ( (iVer>>8)!=(VER_COMMAND_UPDATE>>8) )
  2325. {
  2326. tReq.SendErrorReply ( "major command version mismatch (expected v.%d.x, got v.%d.%d)",
  2327. VER_COMMAND_UPDATE>>8, iVer>>8, iVer&0xff );
  2328. return;
  2329. }
  2330. // obtain and check index name
  2331. CSphString sIndex = tReq.GetString ();
  2332. if ( !g_hIndexes ( sIndex ) )
  2333. {
  2334. tReq.SendErrorReply ( "invalid local index '%s' specified in request", sIndex.cstr() );
  2335. return;
  2336. }
  2337. assert ( g_hIndexes[sIndex].m_pIndex );
  2338. // obtain update data
  2339. CSphAttrUpdate_t tUpd;
  2340. tUpd.m_dAttrs.Resize ( tReq.GetDword() ); // FIXME! check this
  2341. ARRAY_FOREACH ( i, tUpd.m_dAttrs )
  2342. tUpd.m_dAttrs[i].m_sName = tReq.GetString ();
  2343. int iStride = 1+tUpd.m_dAttrs.GetLength();
  2344. tUpd.m_iUpdates = tReq.GetInt (); // FIXME! check this
  2345. tUpd.m_pUpdates = new DWORD [ tUpd.m_iUpdates*iStride ];
  2346. for ( int i=0; i<tUpd.m_iUpdates*iStride; i++ )
  2347. tUpd.m_pUpdates[i] = tReq.GetDword ();
  2348. // check buffer
  2349. if ( tReq.GetError() )
  2350. {
  2351. tReq.SendErrorReply ( "invalid or truncated request" );
  2352. return;
  2353. }
  2354. // do update
  2355. CSphString sError;
  2356. int iUpdated = g_hIndexes[sIndex].m_pIndex->UpdateAttributes ( tUpd, sError );
  2357. if ( iUpdated<0 )
  2358. {
  2359. tReq.SendErrorReply ( "index '%s': %s", sIndex.cstr(), sError.cstr() );
  2360. return;
  2361. }
  2362. NetOutputBuffer_c tOut ( iSock );
  2363. tOut.SendWord ( SEARCHD_OK );
  2364. tOut.SendWord ( VER_COMMAND_UPDATE);
  2365. tOut.SendInt ( sizeof(DWORD) );
  2366. tOut.SendInt ( iUpdated );
  2367. tOut.Flush ();
  2368. assert ( tOut.GetError()==true || tOut.GetSentCount()==8+sizeof(DWORD) );
  2369. }
  2370. /////////////////////////////////////////////////////////////////////////////
  2371. void HandleClient ( int iSock, const char * sClientIP )
  2372. {
  2373. NetInputBuffer_c tBuf ( iSock );
  2374. // send my version
  2375. DWORD uServer = SPHINX_SEARCHD_PROTO;
  2376. if ( sphSockSend ( iSock, (char*)&uServer, sizeof(DWORD), 0 )!=sizeof(DWORD) )
  2377. {
  2378. sphWarning ( "failed to send server version (client=%s)", sClientIP );
  2379. return;
  2380. }
  2381. // get client version and request
  2382. tBuf.ReadFrom ( 12 ); // FIXME! magic
  2383. tBuf.GetInt (); // client version is for now unused
  2384. int iCommand = tBuf.GetWord ();
  2385. int iCommandVer = tBuf.GetWord ();
  2386. int iLength = tBuf.GetInt ();
  2387. if ( tBuf.GetError() )
  2388. {
  2389. sphWarning ( "failed to receive client version and request (client=%s, error=%s)", sClientIP, sphSockError() );
  2390. return;
  2391. }
  2392. // check request
  2393. if ( iCommand<0 || iCommand>=SEARCHD_COMMAND_TOTAL
  2394. || iLength<=0 || iLength>MAX_PACKET_SIZE )
  2395. {
  2396. // unknown command, default response header
  2397. tBuf.SendErrorReply ( "unknown command (code=%d)", iCommand );
  2398. // if request length is insane, low level comm is broken, so we bail out
  2399. if ( iLength<=0 || iLength>MAX_PACKET_SIZE )
  2400. {
  2401. sphWarning ( "ill-formed client request (length=%d out of bounds)", iLength );
  2402. return;
  2403. }
  2404. }
  2405. // get request body
  2406. assert ( iLength>0 && iLength<=MAX_PACKET_SIZE );
  2407. if ( !tBuf.ReadFrom ( iLength ) )
  2408. {
  2409. sphWarning ( "failed to receive client request body (client=%s)", sClientIP );
  2410. return;
  2411. }
  2412. // handle known commands
  2413. assert ( iCommand>=0 && iCommand<SEARCHD_COMMAND_TOTAL );
  2414. switch ( iCommand )
  2415. {
  2416. case SEARCHD_COMMAND_SEARCH: HandleCommandSearch ( iSock, iCommandVer, tBuf ); break;
  2417. case SEARCHD_COMMAND_EXCERPT: HandleCommandExcerpt ( iSock, iCommandVer, tBuf ); break;
  2418. case SEARCHD_COMMAND_UPDATE: HandleCommandUpdate ( iSock, iCommandVer, tBuf ); break;
  2419. default: assert ( 0 && "INTERNAL ERROR: unhandled command" ); break;
  2420. }
  2421. }
  2422. /////////////////////////////////////////////////////////////////////////////
  2423. bool IsReadable ( const char * sPath )
  2424. {
  2425. int iFD = ::open ( sPath, O_RDONLY );
  2426. if ( iFD<0 )
  2427. return false;
  2428. close ( iFD );
  2429. return true;
  2430. }
  2431. bool TryRename ( const char * sIndex, const char * sPrefix, const char * sFromPostfix, const char * sToPostfix, bool bFatal )
  2432. {
  2433. char sFrom [ SPH_MAX_FILENAME_LEN ];
  2434. char sTo [ SPH_MAX_FILENAME_LEN ];
  2435. snprintf ( sFrom, sizeof(sFrom), "%s%s", sPrefix, sFromPostfix );
  2436. snprintf ( sTo, sizeof(sTo), "%s%s", sPrefix, sToPostfix );
  2437. if ( rename ( sFrom, sTo ) )
  2438. {
  2439. if ( bFatal )
  2440. {
  2441. sphFatal ( "rotating index '%s': rollback rename '%s' to '%s' failed: %s",
  2442. sIndex, sFrom, sTo, strerror(errno) );
  2443. } else
  2444. {
  2445. sphWarning ( "rotating index '%s': rename '%s' to '%s' failed: %s",
  2446. sIndex, sFrom, sTo, strerror(errno) );
  2447. }
  2448. return false;
  2449. }
  2450. return true;
  2451. }
  2452. bool RotateIndex ( ServedIndex_t & tIndex, const char * sIndex )
  2453. {
  2454. char sFile [ SPH_MAX_FILENAME_LEN ];
  2455. const char * sPath = tIndex.m_pIndexPath->cstr();
  2456. // whatever happens, we won't retry
  2457. g_iHUP = 0;
  2458. sphInfo ( "rotating index '%s': children exited, trying to rotate", sIndex );
  2459. // check files
  2460. const int EXT_COUNT = 4;
  2461. const char * dNew[EXT_COUNT] = { ".new.sph", ".new.spa", ".new.spi", ".new.spd" };
  2462. const char * dOld[EXT_COUNT] = { ".old.sph", ".old.spa", ".old.spi", ".old.spd" };
  2463. const char * dCur[EXT_COUNT] = { ".sph", ".spa", ".spi", ".spd" };
  2464. for ( int i=0; i<EXT_COUNT; i++ )
  2465. {
  2466. snprintf ( sFile, sizeof(sFile), "%s%s", sPath, dNew[i] );
  2467. if ( !IsReadable ( sFile ) )
  2468. {
  2469. sphWarning ( "rotating index '%s': '%s' unreadable: %s",
  2470. sIndex, sFile, strerror(errno) );
  2471. return false;
  2472. }
  2473. }
  2474. // rename current to old
  2475. for ( int i=0; i<EXT_COUNT; i++ )
  2476. {
  2477. if ( TryRename ( sIndex, sPath, dCur[i], dOld[i], false ) )
  2478. continue;
  2479. // rollback
  2480. for ( int j=0; j<i; j++ )
  2481. TryRename ( sIndex, sPath, dOld[j], dCur[j], true );
  2482. return false;
  2483. }
  2484. // rename new to current
  2485. for ( int i=0; i<EXT_COUNT; i++ )
  2486. {
  2487. if ( TryRename ( sIndex, sPath, dNew[i], dCur[i], false ) )
  2488. continue;
  2489. // rollback new ones we already renamed
  2490. for ( int j=0; j<i; j++ )
  2491. TryRename ( sIndex, sPath, dCur[j], dNew[j], true );
  2492. // rollback old ones
  2493. for ( int j=0; j<EXT_COUNT; j++ )
  2494. TryRename ( sIndex, sPath, dOld[j], dCur[j], true );
  2495. }
  2496. // try to create new index
  2497. CSphIndex * pNewIndex = sphCreateIndexPhrase ( sPath );
  2498. const CSphSchema * pNewSchema = pNewIndex ? pNewIndex->Preload() : NULL;
  2499. if ( !pNewIndex || !pNewSchema )
  2500. {
  2501. if ( !pNewIndex )
  2502. sphWarning ( "rotating index '%s': failed to create new index object", sIndex );
  2503. else
  2504. sphWarning ( "rotating index '%s': failed to preload schema/docinfo", sIndex );
  2505. // try to recover
  2506. for ( int j=0; j<EXT_COUNT; j++ )
  2507. {
  2508. TryRename ( sIndex, sPath, dCur[j], dNew[j], true );
  2509. TryRename ( sIndex, sPath, dOld[j], dCur[j], true );
  2510. }
  2511. SafeDelete ( pNewIndex );
  2512. return false;
  2513. }
  2514. // uff. all done
  2515. SafeDelete ( tIndex.m_pIndex );
  2516. tIndex.m_pIndex = pNewIndex;
  2517. tIndex.m_pSchema = pNewSchema;
  2518. sphInfo ( "rotating index '%s': success", sIndex );
  2519. return true;
  2520. }
  2521. /////////////////////////////////////////////////////////////////////////////
  2522. int main ( int argc, char **argv )
  2523. {
  2524. int rsock;
  2525. struct sockaddr_in remote_iaddr;
  2526. socklen_t len;
  2527. CSphConfig conf;
  2528. fprintf ( stdout, SPHINX_BANNER );
  2529. //////////////////////
  2530. // parse command line
  2531. //////////////////////
  2532. const char * sOptConfig = "sphinx.conf";
  2533. bool bOptConsole = false;
  2534. int i;
  2535. for ( i=1; i<argc; i++ )
  2536. {
  2537. if ( strcasecmp ( argv[i], "--config" )==0 && (i+1)<argc )
  2538. {
  2539. struct stat tStat;
  2540. if ( !stat ( argv[i+1], &tStat ) )
  2541. sOptConfig = argv[i+1];
  2542. else
  2543. sphWarning ( "failed to stat config file '%s', using default 'sphinx.conf'", argv[i+1] );
  2544. i++;
  2545. } else if ( strcasecmp ( argv[i], "--console" )==0 )
  2546. {
  2547. bOptConsole = true;
  2548. } else
  2549. {
  2550. break;
  2551. }
  2552. }
  2553. if ( i!=argc )
  2554. {
  2555. fprintf ( stdout, "ERROR: malformed or unknown option near '%s'.\n\n", argv[i] );
  2556. fprintf ( stdout, "usage: searchd [--config file.conf] [--console]\n" );
  2557. return 1;
  2558. }
  2559. #if USE_WINDOWS
  2560. sphWarning ( "forcing --console mode on Windows" );
  2561. bOptConsole = true;
  2562. #endif // USE_WINDOWS
  2563. /////////////////////
  2564. // parse config file
  2565. /////////////////////
  2566. CSphConfigParser cp;
  2567. sphInfo ( "using config file '%s'...", sOptConfig );
  2568. // FIXME! add key validation here. g_dSphKeysCommon, g_dSphKeysSearchd
  2569. if ( !cp.Parse ( sOptConfig ) )
  2570. sphFatal ( "failed to parse config file '%s'", sOptConfig );
  2571. const CSphConfig & hConf = cp.m_tConf;
  2572. if ( !hConf.Exists ( "searchd" ) || !hConf["searchd"].Exists ( "searchd" ) )
  2573. sphFatal ( "'searchd' config section not found in '%s'", sOptConfig );
  2574. const CSphConfigSection & hSearchd = hConf["searchd"]["searchd"];
  2575. if ( !hConf.Exists ( "index" ) )
  2576. sphFatal ( "no indexes found in '%s'", sOptConfig );
  2577. #define CONF_CHECK(_hash,_key,_msg,_add) \
  2578. if (!( _hash.Exists ( _key ) )) \
  2579. sphFatal ( "mandatory option '%s' not found " _msg, _key, _add );
  2580. CONF_CHECK ( hSearchd, "port", "in 'searchd' section", "" );
  2581. if ( !bOptConsole )
  2582. CONF_CHECK ( hSearchd, "pid_file", "in 'searchd' section", "" );
  2583. int iPort = hSearchd["port"].intval();
  2584. if ( !iPort )
  2585. sphFatal ( "expected valid 'port', got '%s'", hSearchd["port"].cstr() );
  2586. if ( hSearchd.Exists ( "read_timeout" ) && hSearchd["read_timeout"].intval()>=0 )
  2587. g_iReadTimeout = hSearchd["read_timeout"].intval();
  2588. if ( hSearchd.Exists ( "max_children" ) && hSearchd["max_children"].intval()>=0 )
  2589. g_iMaxChildren = hSearchd["max_children"].intval();
  2590. if ( hSearchd("max_matches") )
  2591. {
  2592. int iMax = hSearchd["max_matches"].intval();
  2593. if ( iMax<0 || iMax>10000000 )
  2594. {
  2595. sphWarning ( "max_matches=%d out of bounds; using default 1000", iMax );
  2596. } else
  2597. {
  2598. g_iMaxMatches = iMax;
  2599. }
  2600. }
  2601. if ( hSearchd("cache_dir") )
  2602. {
  2603. // FIXME! add more validation
  2604. g_bCacheEnable = true;
  2605. g_sCacheDir = hSearchd["cache_dir"];
  2606. if ( hSearchd("cache_ttl") )
  2607. g_iCacheTTL = Max ( hSearchd["cache_ttl"].intval(), 1 );
  2608. if ( hSearchd("cache_gzip") && hSearchd["cache_gzip"].intval()!=0 )
  2609. g_bCacheGzip = true;
  2610. }
  2611. //////////////////////
  2612. // build indexes hash
  2613. //////////////////////
  2614. int iValidIndexes = 0;
  2615. hConf["index"].IterateStart ();
  2616. while ( hConf["index"].IterateNext() )
  2617. {
  2618. const CSphConfigSection & hIndex = hConf["index"].IterateGet();
  2619. const char * sIndexName = hConf["index"].IterateGetKey().cstr();
  2620. if ( hIndex("type") && hIndex["type"]=="distributed" )
  2621. {
  2622. ///////////////////////////////
  2623. // configure distributed index
  2624. ///////////////////////////////
  2625. DistributedIndex_t tIdx;
  2626. // add local agents
  2627. for ( CSphVariant * pLocal = hIndex("local"); pLocal; pLocal = pLocal->m_pNext )
  2628. {
  2629. if ( !g_hIndexes ( pLocal->cstr() ) )
  2630. {
  2631. sphWarning ( "index '%s': no such local index '%s' - NOT SERVING",
  2632. sIndexName, pLocal->cstr() );
  2633. continue;
  2634. }
  2635. tIdx.m_dLocal.Add ( pLocal->cstr() );
  2636. }
  2637. // add remote agents
  2638. for ( CSphVariant * pAgent = hIndex("agent"); pAgent; pAgent = pAgent->m_pNext )
  2639. {
  2640. Agent_t tAgent;
  2641. // extract host name
  2642. const char * p = pAgent->cstr();
  2643. while ( sphIsAlpha(*p) || *p=='.' || *p=='-' ) p++;
  2644. if ( p==pAgent->cstr() )
  2645. {
  2646. sphWarning ( "index '%s': agent '%s': host name expected - NOT SERVING",
  2647. sIndexName, pAgent->cstr() );
  2648. continue;
  2649. }
  2650. if ( *p++!=':' )
  2651. {
  2652. sphWarning ( "index '%s': agent '%s': colon expected near '%s' - NOT SERVING",
  2653. sIndexName, pAgent->cstr(), p );
  2654. continue;
  2655. }
  2656. tAgent.m_sHost = pAgent->SubString ( 0, p-1-pAgent->cstr() );
  2657. // extract port
  2658. if ( !isdigit(*p) )
  2659. {
  2660. sphWarning ( "index '%s': agent '%s': port number expected near '%s' - NOT SERVING",
  2661. sIndexName, pAgent->cstr(), p );
  2662. continue;
  2663. }
  2664. tAgent.m_iPort = atoi(p);
  2665. if ( tAgent.m_iPort<=0 || tAgent.m_iPort>=65536 )
  2666. {
  2667. sphWarning ( "index '%s': agent '%s': invalid port number near '%s' - NOT SERVING",
  2668. sIndexName, pAgent->cstr(), p );
  2669. continue;
  2670. }
  2671. // extract index list
  2672. while ( isdigit(*p) ) p++;
  2673. if ( *p++!=':' )
  2674. {
  2675. sphWarning ( "index '%s': agent '%s': colon expected near '%s' - NOT SERVING",
  2676. sIndexName, pAgent->cstr(), p );
  2677. continue;
  2678. }
  2679. while ( isspace(*p) )
  2680. p++;
  2681. const char * sIndexList = p;
  2682. while ( sphIsAlpha(*p) || isspace(*p) || *p==',' )
  2683. p++;
  2684. if ( *p )
  2685. {
  2686. sphWarning ( "index '%s': agent '%s': index list expected near '%s' - NOT SERVING",
  2687. sIndexName, pAgent->cstr(), p );
  2688. continue;
  2689. }
  2690. tAgent.m_sIndexes = sIndexList;
  2691. // lookup address
  2692. struct hostent * hp = gethostbyname ( tAgent.m_sHost.cstr() );
  2693. if ( !hp )
  2694. {
  2695. sphWarning ( "index '%s': agent '%s': failed to lookup host name - NOT SERVING",
  2696. sIndexName, pAgent->cstr() );
  2697. continue;
  2698. }
  2699. tAgent.SetAddr ( hp->h_addrtype, hp->h_length, hp->h_addr_list[0] );
  2700. // done
  2701. tIdx.m_dAgents.Add ( tAgent );
  2702. }
  2703. // configure options
  2704. if ( hIndex("agent_connect_timeout") )
  2705. {
  2706. if ( hIndex["agent_connect_timeout"].intval()<=0 )
  2707. sphWarning ( "index '%s': connect_timeout must be positive, ignored", sIndexName );
  2708. else
  2709. tIdx.m_iAgentConnectTimeout = hIndex["agent_connect_timeout"].intval();
  2710. }
  2711. if ( hIndex("agent_query_timeout") )
  2712. {
  2713. if ( hIndex["agent_query_timeout"].intval()<=0 )
  2714. sphWarning ( "index '%s': query_timeout must be positive, ignored", sIndexName );
  2715. else
  2716. tIdx.m_iAgentQueryTimeout = hIndex["agent_query_timeout"].intval();
  2717. }
  2718. // finally, check and add distributed index to global table
  2719. if ( tIdx.m_dAgents.GetLength()==0 && tIdx.m_dLocal.GetLength()==0 )
  2720. {
  2721. sphWarning ( "index '%s': no valid local/remote indexes in distributed index - NOT SERVING",
  2722. sIndexName );
  2723. continue;
  2724. } else
  2725. {
  2726. if ( !g_hDistIndexes.Add ( tIdx, sIndexName ) )
  2727. {
  2728. sphWarning ( "index '%s': duplicate name in hash?! INTERNAL ERROR - NOT SERVING", sIndexName );
  2729. continue;
  2730. }
  2731. }
  2732. } else
  2733. {
  2734. /////////////////////////
  2735. // configure local index
  2736. /////////////////////////
  2737. // check path
  2738. if ( !hIndex.Exists ( "path" ) )
  2739. {
  2740. sphWarning ( "index '%s': key 'path' not found' - NOT SERVING", sIndexName );
  2741. continue;
  2742. }
  2743. // configure charset_type
  2744. CSphString sError;
  2745. ISphTokenizer * pTokenizer = sphConfTokenizer ( hIndex, sError );
  2746. if ( !pTokenizer )
  2747. {
  2748. sphWarning ( "index '%s': %s - NOT SERVING", sIndexName, sError.cstr() );
  2749. continue;
  2750. }
  2751. // configure morphology
  2752. DWORD iMorph = sphConfMorphology ( hIndex, pTokenizer->IsUtf8() );
  2753. if ( iMorph==SPH_MORPH_UNKNOWN )
  2754. sphWarning ( "index '%s': unknown morphology type '%s' - ignored",
  2755. sIndexName, hIndex["morphology"].cstr() );
  2756. // create add this one to served hashes
  2757. ServedIndex_t tIdx;
  2758. tIdx.m_pIndex = sphCreateIndexPhrase ( hIndex["path"].cstr() );
  2759. tIdx.m_pSchema = tIdx.m_pIndex->Preload() ;
  2760. if ( !tIdx.m_pSchema )
  2761. {
  2762. sphWarning ( "index '%s': failed to preload schema and docinfos - NOT SERVING", sIndexName );
  2763. continue;
  2764. }
  2765. tIdx.m_pDict = new CSphDict_CRC32 ( iMorph );
  2766. tIdx.m_pDict->LoadStopwords ( hIndex.Exists ( "stopwords" ) ? hIndex["stopwords"].cstr() : NULL, pTokenizer );
  2767. tIdx.m_pTokenizer = pTokenizer;
  2768. tIdx.m_pIndexPath = new CSphString ( hIndex["path"] );
  2769. if ( !bOptConsole )
  2770. {
  2771. // check lock file
  2772. char sTmp [ 1024 ];
  2773. snprintf ( sTmp, sizeof(sTmp), "%s.spl", hIndex["path"].cstr() );
  2774. sTmp [ sizeof(sTmp)-1 ] = '\0';
  2775. struct stat tStat;
  2776. if ( !stat ( sTmp, &tStat ) )
  2777. {
  2778. sphWarning ( "index '%s': lock file '%s' exists - NOT SERVING", sIndexName, sTmp );
  2779. continue;
  2780. }
  2781. // create lock file
  2782. FILE * fp = fopen ( sTmp, "w" );
  2783. if ( !fp )
  2784. sphFatal ( "index '%s': failed to create lock file '%s''", sIndexName, sTmp );
  2785. fprintf ( fp, "%d", getpid() );
  2786. fclose ( fp );
  2787. tIdx.m_pLockFile = new CSphString ( sTmp );
  2788. }
  2789. if ( !g_hIndexes.Add ( tIdx, sIndexName ) )
  2790. {
  2791. sphWarning ( "index '%s': duplicate name in hash?! INTERNAL ERROR - NOT SERVING", sIndexName );
  2792. continue;
  2793. }
  2794. tIdx.Reset (); // so that the dtor wouln't delete everything
  2795. }
  2796. iValidIndexes++;
  2797. }
  2798. if ( !iValidIndexes )
  2799. sphFatal ( "no valid indexes to serve" );
  2800. ///////////
  2801. // startup
  2802. ///////////
  2803. // handle my signals
  2804. #if !USE_WINDOWS
  2805. signal ( SIGCHLD, sigchld );
  2806. signal ( SIGTERM, sigterm );
  2807. signal ( SIGINT, sigterm );
  2808. signal ( SIGHUP, sighup );
  2809. #endif // !USE_WINDOWS
  2810. // daemonize
  2811. if ( !bOptConsole )
  2812. {
  2813. // create log
  2814. const char * sLog = "searchd.log";
  2815. if ( hSearchd.Exists ( "log" ) )
  2816. sLog = hSearchd["log"].cstr();
  2817. umask ( 066 );
  2818. g_iLogFile = open ( sLog, O_CREAT | O_RDWR | O_APPEND, S_IREAD | S_IWRITE );
  2819. if ( g_iLogFile<0 )
  2820. {
  2821. g_iLogFile = STDOUT_FILENO;
  2822. sphFatal ( "failed to write log file '%s'", sLog );
  2823. }
  2824. // create query log if required
  2825. if ( hSearchd.Exists ( "query_log" ) )
  2826. {
  2827. g_iQueryLogFile = open ( hSearchd["query_log"].cstr(), O_CREAT | O_RDWR | O_APPEND,
  2828. S_IREAD | S_IWRITE );
  2829. if ( g_iQueryLogFile<0 )
  2830. sphFatal ( "failed to write query log file '%s'", hSearchd["query_log"].cstr() );
  2831. }
  2832. // do daemonize
  2833. int iDevNull = open ( "/dev/null", O_RDWR );
  2834. close ( STDIN_FILENO );
  2835. close ( STDOUT_FILENO );
  2836. close ( STDERR_FILENO );
  2837. dup2 ( iDevNull, STDIN_FILENO );
  2838. dup2 ( iDevNull, STDOUT_FILENO );
  2839. dup2 ( iDevNull, STDERR_FILENO );
  2840. g_bLogStdout = false;
  2841. #if !USE_WINDOWS
  2842. switch ( fork() )
  2843. {
  2844. case -1:
  2845. // error
  2846. Shutdown ();
  2847. sphFatal ( "fork() failed (reason: %s)", strerror ( errno ) );
  2848. exit ( 1 );
  2849. case 0:
  2850. // daemonized child
  2851. break;
  2852. default:
  2853. // tty-controlled parent
  2854. exit ( 0 );
  2855. }
  2856. #endif
  2857. } else
  2858. {
  2859. // if we're running in console mode, dump queries to tty as well
  2860. g_iQueryLogFile = g_iLogFile;
  2861. }
  2862. ////////////////////
  2863. // network startup
  2864. ////////////////////
  2865. // init WSA on Windows
  2866. #if USE_WINDOWS
  2867. WSADATA tWSAData;
  2868. int iStartupErr = WSAStartup ( WINSOCK_VERSION, &tWSAData );
  2869. if ( iStartupErr )
  2870. sphFatal ( "failed to initialize WinSock2: %s", sphSockError(iStartupErr) );
  2871. #endif
  2872. // create and bind socket
  2873. DWORD uAddr = htonl(INADDR_ANY);
  2874. if ( hSearchd("address") )
  2875. {
  2876. struct hostent * pHost = gethostbyname ( hSearchd["address"].cstr() );
  2877. if ( !pHost || pHost->h_addrtype!=AF_INET )
  2878. {
  2879. sphWarning ( "no AF_INET address associated with %s, listening on INADDR_ANY", hSearchd["address"].cstr() );
  2880. } else
  2881. {
  2882. assert ( sizeof(DWORD)==pHost->h_length );
  2883. memcpy ( &uAddr, pHost->h_addr, sizeof(uAddr) );
  2884. }
  2885. }
  2886. g_iSocket = sphCreateServerSocket ( uAddr, iPort );
  2887. listen ( g_iSocket, 5 );
  2888. // we're almost good, and can create .pid file now
  2889. if ( !bOptConsole )
  2890. {
  2891. // i'm the main one
  2892. g_bHeadDaemon = true;
  2893. // create pid
  2894. g_sPidFile = hSearchd["pid_file"].cstr();
  2895. FILE * fp = fopen ( g_sPidFile, "w" );
  2896. if ( !fp )
  2897. sphFatal ( "failed to write pid file '%s'", g_sPidFile );
  2898. fprintf ( fp, "%d", getpid() );
  2899. fclose ( fp );
  2900. }
  2901. /////////////////
  2902. // serve clients
  2903. /////////////////
  2904. sphInfo ( "accepting connections" );
  2905. for ( ;; )
  2906. {
  2907. #if SPH_DEBUG_LEAKS
  2908. static int iHeadAllocs = sphAllocsCount ();
  2909. static int iHeadCheckpoint = sphAllocsLastID ();
  2910. if ( iHeadAllocs!=sphAllocsCount() )
  2911. {
  2912. sphLockEx ( g_iLogFile );
  2913. lseek ( g_iLogFile, 0, SEEK_END );
  2914. sphAllocsDump ( g_iLogFile, iHeadCheckpoint );
  2915. sphLockUn ( g_iLogFile );
  2916. iHeadAllocs = sphAllocsCount ();
  2917. iHeadCheckpoint = sphAllocsLastID ();
  2918. }
  2919. #endif
  2920. // try to rotate indices
  2921. if ( g_iHUP && !g_iChildren )
  2922. {
  2923. g_hIndexes.IterateStart();
  2924. while ( g_hIndexes.IterateNext() )
  2925. {
  2926. ServedIndex_t & tIndex = g_hIndexes.IterateGet();
  2927. const char * sIndex = g_hIndexes.IterateGetKey().cstr();
  2928. assert ( tIndex.m_pIndex );
  2929. assert ( tIndex.m_pLockFile );
  2930. assert ( tIndex.m_pIndexPath );
  2931. if ( !RotateIndex ( tIndex, sIndex ) )
  2932. sphWarning ( "rotating index '%s': using old index", sIndex );
  2933. }
  2934. }
  2935. #if USE_WINDOWS
  2936. #pragma warning(disable:4127) // conditional expr is const
  2937. #pragma warning(disable:4389) // signed/unsigned mismatch
  2938. #endif // USE_WINDOWS
  2939. // we can't simply accept, because of the need to react to HUPs
  2940. fd_set fdsAccept;
  2941. FD_ZERO ( &fdsAccept );
  2942. FD_SET ( g_iSocket, &fdsAccept );
  2943. #if USE_WINDOWS
  2944. #pragma warning(default:4127) // conditional expr is const
  2945. #pragma warning(default:4389) // signed/unsigned mismatch
  2946. #endif // USE_WINDOWS
  2947. struct timeval tvTimeout;
  2948. tvTimeout.tv_sec = 1;
  2949. tvTimeout.tv_usec = 0;
  2950. if ( select ( 1+g_iSocket, &fdsAccept, NULL, &fdsAccept, &tvTimeout )<=0 )
  2951. continue;
  2952. // select says something interesting happened, so let's accept
  2953. len = sizeof ( remote_iaddr );
  2954. rsock = accept ( g_iSocket, (struct sockaddr*)&remote_iaddr, &len );
  2955. int iErr = sphSockGetErrno();
  2956. if ( rsock<0 && ( iErr==EINTR || iErr==EAGAIN || iErr==EWOULDBLOCK ) )
  2957. continue;
  2958. if ( rsock<0 )
  2959. sphFatal ( "accept() failed (reason: %s)", sphSockError(iErr) );
  2960. if ( ( g_iMaxChildren && g_iChildren>=g_iMaxChildren ) || g_iHUP )
  2961. {
  2962. const char * sMessage = "server maxed out, retry in a second";
  2963. int iRespLen = 4 + strlen(sMessage);
  2964. NetOutputBuffer_c tOut ( rsock );
  2965. tOut.SendInt ( SPHINX_SEARCHD_PROTO );
  2966. tOut.SendWord ( SEARCHD_RETRY );
  2967. tOut.SendWord ( 0 ); // version doesn't matter
  2968. tOut.SendInt ( iRespLen );
  2969. tOut.SendString ( sMessage );
  2970. tOut.Flush ();
  2971. sphWarning ( "maxed out, dismissing client" );
  2972. sphSockClose ( rsock );
  2973. continue;
  2974. }
  2975. char sClientIP [ 256 ];
  2976. DWORD uClientIP = ntohl ( remote_iaddr.sin_addr.s_addr );
  2977. snprintf ( sClientIP, sizeof(sClientIP), "%d.%d.%d.%d:%d",
  2978. (uClientIP>>24) & 0xff, (uClientIP>>16) & 0xff, (uClientIP>>8) & 0xff, uClientIP & 0xff,
  2979. (int)ntohs(remote_iaddr.sin_port) );
  2980. if ( bOptConsole )
  2981. {
  2982. HandleClient ( rsock, sClientIP );
  2983. sphSockClose ( rsock );
  2984. continue;
  2985. }
  2986. #if !USE_WINDOWS
  2987. switch ( fork() )
  2988. {
  2989. // fork() failed
  2990. case -1:
  2991. sphFatal ( "fork() failed (reason: %s)", strerror ( errno ) );
  2992. // child process, handle client
  2993. case 0:
  2994. HandleClient ( rsock, sClientIP );
  2995. sphSockClose ( rsock );
  2996. exit ( 0 );
  2997. break;
  2998. // parent process, continue accept()ing
  2999. default:
  3000. g_iChildren++;
  3001. sphSockClose ( rsock );
  3002. break;
  3003. }
  3004. #endif // !USE_WINDOWS
  3005. }
  3006. }
  3007. //
  3008. // $Id$
  3009. //